2007-06-02 Armin Burgmeier <armin@openismus.com>
[modest] / src / maemo / modest-connection-specific-smtp-window.c
index 35a8b38..26278ce 100644 (file)
@@ -3,7 +3,7 @@
 #include "modest-connection-specific-smtp-window.h"
 #include "modest-connection-specific-smtp-edit-window.h"
 #include <modest-account-mgr-helpers.h>
-#include <maemo/modest-maemo-ui-constants.h>
+#include "widgets/modest-ui-constants.h"
 
 #include <modest-runtime.h>
 #include <tny-maemo-conic-device.h>
@@ -115,6 +115,11 @@ modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpW
        object_class->finalize = modest_connection_specific_smtp_window_finalize;
 }
 
+/* libconic does not return a list of connections in scratchbox,
+ * so enable this to put a fake row in the list,
+ * so we can test other parts of the code. */
+/* #define DEBUG_WITHOUT_LIBCONIC 1 */
+
 void
 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
        const gchar* account_name)
@@ -129,18 +134,28 @@ modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSp
        TnyDevice *device = modest_runtime_get_device ();
        g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
        
+       /* Get the list of Internet Access Points: */
+       #ifdef DEBUG_WITHOUT_LIBCONIC
+       GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
+       #else
        TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
+       GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
+       #endif
        
-       /* Get the list of Internet Access Points: */
-       GSList* list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
-       printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps));
+       /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
        
        GSList* iter = list_iaps;
        while (iter) {
                ConIcIap *iap = (ConIcIap*)iter->data;
                if (iap) {
+                       #ifdef DEBUG_WITHOUT_LIBCONIC
+                       const gchar *name = "debug name";
+                       const gchar *id = "debug id";
+                       #else
                        const gchar *name = con_ic_iap_get_name (iap);
                        const gchar *id = con_ic_iap_get_id (iap);
+                       #endif
+                       
                        printf ("debug: iac name=%s, id=%s\n", name, id);
                        
                        /* Get any already-associated connection-specific server account: */
@@ -164,8 +179,10 @@ modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSp
                iter = g_slist_next (iter);     
        }
                
+       #ifndef DEBUG_WITHOUT_LIBCONIC
        if (list_iaps)
                tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
+       #endif
                
        update_model_server_names (self);
 }
@@ -191,23 +208,28 @@ on_button_edit (GtkButton *button, gpointer user_data)
                                    MODEL_COL_SERVER_ACCOUNT_DATA, &data,
                                    -1);
        
+               /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
                /* TODO: Is 0 an allowed libconic IAP ID? 
                 * If not then we should check for it. */
                
                /* Get existing server account data if a server account is already specified: */
-               ModestServerAccountData *data_retrieved = NULL;
+               gboolean data_was_retrieved = FALSE;
                if (server_account_name && !data) {
-                       data_retrieved = modest_account_mgr_get_server_account_data (priv->account_manager, 
+                       data = modest_account_mgr_get_server_account_data (priv->account_manager, 
                                server_account_name);
-                       data =  data_retrieved;
+                       if (data)
+                               data_was_retrieved = TRUE;
                }
                
                GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
                modest_connection_specific_smtp_edit_window_set_connection (
                        MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
                        
-               if (data_retrieved)
-                       modest_account_mgr_free_server_account_data (priv->account_manager, data_retrieved);
+               /* Delete data, unless it was data from the rowmodel: */
+               if (data_was_retrieved) {
+                       modest_account_mgr_free_server_account_data (priv->account_manager, data);
+                       data = NULL;
+               }
                        
                gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
                gint response = gtk_dialog_run (GTK_DIALOG (window));
@@ -222,13 +244,18 @@ on_button_edit (GtkButton *button, gpointer user_data)
                        }
                        
                        /* Get the new account data and save it in the row for later:
-                        * We free this in finalize(). */
+                        * We free this in finalize(),
+                        * and save it to our configuration in 
+                        * modest_connection_specific_smtp_window_save_server_accounts(). */
                        data = modest_connection_specific_smtp_edit_window_get_settings (
                                                MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
-                                               priv->account_manager, server_account_name);    
+                                               priv->account_manager);
+                       
+                       const gchar* server_name = data ? data->hostname : NULL;
                        gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
-                                           MODEL_COL_SERVER_ACCOUNT_DATA, data,
-                                           -1);
+                                       MODEL_COL_SERVER_ACCOUNT_DATA, data,
+                                       MODEL_COL_SERVER_NAME, server_name,
+                                       -1);
                }
        }
        
@@ -264,6 +291,12 @@ on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow
 static void
 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
 {
+       ModestWindowMgr *mgr;
+
+       /* Specify a default size, because the GtkTreeView's default requested size  
+        * is not big enough: */
+       gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
+       
        /* This seems to be necessary to make the window show at the front with decoration.
         * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
         * then the window will be below the others. */
@@ -277,12 +310,17 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
         * with a string for the name, a string for the server name, and an int for the ID.
         * This must match our MODEL_COLS enum constants.
         */
-       priv->model = GTK_TREE_MODEL (gtk_list_store_new (4, 
+       priv->model = GTK_TREE_MODEL (gtk_list_store_new (5, 
                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
 
        /* Setup the tree view: */
        priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
 
+       /* Show the column headers,
+        * which does not seem to be the default on Maemo.
+        */                     
+       gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
+       
        /* name column:
         * The ID model column in not shown in the view. */
        GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
@@ -306,6 +344,14 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
        /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
        
        GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
+
+       /* Introductory note: */
+       GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
+       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+       /* So that it is shown without being truncated: */
+       gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
+       gtk_widget_show (label);
+       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
        
        /* Put the treeview in a scrolled window and add it to the box: */
        GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
@@ -342,6 +388,15 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
        g_signal_connect (sel, "changed",
                          G_CALLBACK(on_selection_changed), self);
        on_selection_changed (sel, self);
+       
+       /* When this window is shown, hibernation should not be possible, 
+        * because there is no sensible way to save the state: */
+       mgr = modest_runtime_get_window_mgr ();
+       modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr, 
+                                                                    GTK_WINDOW (self)); 
+
+       /* Set window title */
+       gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
 }
 
 ModestConnectionSpecificSmtpWindow*
@@ -387,15 +442,16 @@ modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpe
                                        priv->account_name, connection_name);
                                server_account_name = modest_account_mgr_get_unused_account_name (
                                        priv->account_manager, name_start, TRUE /* server account. */);
+                               g_assert (server_account_name);
                                g_free (name_start);
                                
                                success = modest_account_mgr_add_server_account (priv->account_manager,
-                                       server_account_name,
-                                       data->hostname,
-                                       data->username, data->password,
-                                       MODEST_PROTOCOL_TRANSPORT_SMTP,
-                                       data->security,
-                                       data->secure_auth);
+                                                                                server_account_name,
+                                                                                data->hostname, 0,
+                                                                                data->username, data->password,
+                                                                                MODEST_PROTOCOL_TRANSPORT_SMTP,
+                                                                                data->security,
+                                                                                data->secure_auth);
                                        
                                /* associate the specific server account with this connection for this account: */
                                success = success && modest_account_mgr_set_connection_specific_smtp (
@@ -410,12 +466,12 @@ modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpe
                                /* Change an existing server account: */
                                success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
                                        MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
+                                               
+                               modest_server_account_set_username (priv->account_manager, server_account_name,
+                                       data->username);
                                                        
-                               success = success &&  modest_account_mgr_set_string (priv->account_manager, server_account_name,
-                                       MODEST_ACCOUNT_USERNAME, data->username, TRUE /* server account */);
-                                                       
-                               success = success &&  modest_account_mgr_set_string (priv->account_manager, server_account_name,
-                                       MODEST_ACCOUNT_PASSWORD, data->password, TRUE /*  server account */);
+                               modest_server_account_set_password (priv->account_manager, server_account_name,
+                                       data->password);
                                                
                                modest_server_account_set_secure_auth (priv->account_manager, server_account_name, 
                                        data->secure_auth);
@@ -465,6 +521,10 @@ void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
                                            MODEL_COL_SERVER_NAME, hostname,
                                            -1);
                        g_free (hostname);
+               } else {
+                       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
+                                           MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
+                                           -1);
                }
                        
                /* Get next row: */