* Refactored s&r actions
authorSergio Villar Senin <svillar@igalia.com>
Tue, 8 May 2007 17:06:47 +0000 (17:06 +0000)
committerSergio Villar Senin <svillar@igalia.com>
Tue, 8 May 2007 17:06:47 +0000 (17:06 +0000)
* Added some documentation
* Added s&r CSM handlers

pmo-trunk-r1791

src/dbus_api/modest-dbus-callbacks.c
src/maemo/modest-main-window.c
src/modest-mail-operation.c
src/modest-ui-actions.c
src/modest-ui-actions.h

index a8e1b22..b7fc890 100644 (file)
@@ -409,9 +409,14 @@ static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retv
 static gboolean
 on_idle_send_receive(gpointer user_data)
 {
-       ModestWindow* main_window = modest_window_mgr_get_main_window(
-               modest_runtime_get_window_mgr ());
-       do_send_receive(main_window);
+       ModestWindow *win;
+
+       /* Pick the main window if it exists */
+       win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
+
+       /* Send & receive all if "Update automatically" is set */
+       /* TODO: check the auto-update parametter in the configuration */
+       modest_ui_actions_do_send_receive_all (win);
        
        return FALSE; /* Do not call this callback again. */
 }
index b9ec7f8..629379b 100644 (file)
@@ -106,6 +106,8 @@ static void set_toolbar_mode                  (ModestMainWindow *self,
 static void on_show_account_action_activated  (GtkAction *action,
                                               gpointer user_data);
 
+static void on_send_receive_csm_activated     (GtkMenuItem *item,
+                                              gpointer user_data);
 /* list my signals */
 enum {
        /* MY_SIGNAL_1, */
@@ -358,7 +360,7 @@ on_connection_changed (TnyDevice *device, gboolean online, ModestMainWindow *sel
         * as per the specification:
         * (without the check for >0 accounts, though that is not specified): */
        if (online) {
-               do_send_receive (MODEST_WINDOW (self));
+               modest_ui_actions_do_send_receive (NULL, MODEST_WINDOW (self));
        }
 }
 
@@ -435,7 +437,7 @@ connect_signals (ModestMainWindow *self)
 gboolean
 sync_accounts_cb (ModestMainWindow *win)
 {
-       do_send_receive (MODEST_WINDOW(win));
+       modest_ui_actions_do_send_receive (NULL, MODEST_WINDOW (win));
        return FALSE; /* Do not call this idle handler again. */
 }
 
@@ -551,6 +553,9 @@ modest_main_window_new (void)
                                      modest_conf_get_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL));
        hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
 
+       /* Get device name */
+       modest_maemo_utils_get_device_name ();
+
        /* folder view */
        query = tny_folder_store_query_new ();
        tny_folder_store_query_add_item (query, NULL,
@@ -562,9 +567,6 @@ modest_main_window_new (void)
        modest_folder_view_set_style (priv->folder_view,
                                      MODEST_FOLDER_VIEW_STYLE_SHOW_ONE);
 
-       /* Get device name */
-       modest_maemo_utils_get_device_name ();
-
        /* header view */
        priv->header_view  =
                MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
@@ -613,7 +615,6 @@ modest_main_window_new (void)
        /* do send & receive when we are idle */
        g_idle_add ((GSourceFunc)sync_accounts_cb, self);
        
-
        HildonProgram *app = hildon_program_get_instance ();
        hildon_program_add_window (app, HILDON_WINDOW (self));
        
@@ -857,10 +858,6 @@ compare_display_names (ModestAccountData *a,
        return strcmp (a->display_name, b->display_name);
 }
 
-/*
- * TODO: modify the menu dynamically. Add handlers to each item of the
- * menu when created
- */
 static void 
 on_account_update (TnyAccountStore *account_store, 
                   gchar *accout_name,
@@ -940,6 +937,10 @@ on_account_update (TnyAccountStore *account_store,
                        priv->accounts_popup = gtk_menu_new ();
                item = gtk_menu_item_new_with_label (_("mcen_me_toolbar_sendreceive_all"));
                gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
+               g_signal_connect (G_OBJECT (item), 
+                                 "activate", 
+                                 G_CALLBACK (on_send_receive_csm_activated),
+                                 NULL);
                item = gtk_separator_menu_item_new ();
                gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
        }
@@ -992,6 +993,12 @@ on_account_update (TnyAccountStore *account_store,
                /* Create item and add it to the send&receive CSM */
                item = gtk_menu_item_new_with_label (display_name);
                gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
+               g_signal_connect_data (G_OBJECT (item), 
+                                      "activate", 
+                                      G_CALLBACK (on_send_receive_csm_activated),
+                                      g_strdup (account_data->account_name),
+                                      (GClosureNotify) g_free,
+                                      0);
 
                /* Frees */
                g_free (display_name);
@@ -1372,12 +1379,13 @@ on_queue_changed (ModestMailOperationQueue *queue,
                break;
        case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
                if (mode == TOOLBAR_MODE_TRANSFER) {
+
                        while (tmp) {
                                modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
                                                                         mail_op);
                                tmp = g_slist_next (tmp);
                        }
-                       
+
                        /* If no more operations are being observed, NORMAL mode is enabled again */
                        if (observers_empty (self))
                                set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
@@ -1394,19 +1402,38 @@ on_show_account_action_activated  (GtkAction *action,
        ModestMainWindow *self;
        ModestMainWindowPrivate *priv;
        ModestAccountMgr *mgr;
+       const gchar *acc_name;
 
        self = MODEST_MAIN_WINDOW (user_data);
        priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
 
        /* Get account data */
+       acc_name = gtk_action_get_name (action);
        mgr = modest_runtime_get_account_mgr ();
-       acc_data = modest_account_mgr_get_account_data (mgr, gtk_action_get_name (action));
+       acc_data = modest_account_mgr_get_account_data (mgr, acc_name);
 
-       /* Set the new visible account */
-       if (acc_data->store_account) 
+       /* Set the new visible & active account */
+       if (acc_data->store_account) { 
                modest_folder_view_set_account_id_of_visible_server_account (priv->folder_view,
                                                                             acc_data->store_account->account_name);
+       }
 
        /* Free */
        modest_account_mgr_free_account_data (mgr, acc_data);
 }
+
+static void
+on_send_receive_csm_activated (GtkMenuItem *item,
+                              gpointer user_data)
+{
+       ModestWindow *win;
+
+       win = MODEST_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
+
+       /* If user_data == NULL, we must update all (CSM option All) */
+       if (!user_data) {
+               modest_ui_actions_do_send_receive_all (win);
+       } else {
+               modest_ui_actions_do_send_receive ((const gchar *)user_data, win);
+       }
+}
index 547b82e..74d33bc 100644 (file)
@@ -540,7 +540,8 @@ modest_mail_operation_update_account (ModestMailOperation *self,
 
        /* g_message ("tny_folder_store_get_folders_async"); */
        tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
-                                           folders, update_folders_cb, NULL, update_folders_status_cb, self);
+                                           folders, update_folders_cb, NULL, 
+                                           update_folders_status_cb, self);
        
        return TRUE;
 }
index b5c53e7..432f499 100644 (file)
@@ -788,86 +788,75 @@ gboolean check_for_connection (const gchar *account_name)
                return TRUE;
        }
 }
-       
-static void
-do_send_receive_current_or_default (ModestWindow *win)
+
+/*
+ * This function performs the send & receive required actions. The
+ * window it's used to create the mail operation. Tipically it should
+ * be allways the main window, but we pass it as argument in order to
+ * be more flexible.
+ */
+void
+modest_ui_actions_do_send_receive (const gchar *account_name, ModestWindow *win)
 {
-       gchar *account_name;
+       gchar *acc_name = NULL;
 
-       g_message ("%s: online? %s", __FUNCTION__,  
-               tny_device_is_online(modest_runtime_get_device()) ? "yes":"no");
-                               
-       /* As per the UI spec, only the active account should be affected, 
-        * else the default folder if there is no active account: */                            
-       account_name =
-               g_strdup(modest_window_get_active_account(MODEST_WINDOW(win)));
-       if (!account_name)
-               account_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
+       /* If no account name was provided get the current account, if
+          there is none either then pick the default one */
        if (!account_name) {
-               g_printerr ("modest: cannot get default account\n");
-               return;
+               acc_name = g_strdup (modest_window_get_active_account(win));
+               if (!acc_name)
+                       acc_name  = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr());
+               if (!acc_name) {
+                       g_printerr ("modest: cannot get default account\n");
+                       return;
+               }
+       } else {
+               acc_name = g_strdup (account_name);
        }
-       
-       /* Do not continue if no suitable connection is open: */
-       if (check_for_connection (account_name)) {
+
+       /* Send & receive. Do not continue if no suitable connection
+          is open */
+       if (check_for_connection (acc_name)) {
                /* As per the UI spec,
                 * for POP accounts, we should receive,
                 * for IMAP we should synchronize everything, including receiving,
                 * for SMTP we should send,
                 * first receiving, then sending:
                 */
-               if (!action_receive(account_name, win))
+               if (!action_receive(acc_name, win))
                        g_printerr ("modest: failed to receive\n");
-               if (!action_send(account_name))
+               if (!action_send(acc_name))
                        g_printerr ("modest: failed to send\n");
-               
-               g_free (account_name);
        }
+       /* Free */
+       g_free (acc_name);
 }
 
-
-static void
-do_send_receive_auto (ModestWindow *win)
+/*
+ * Refreshes all accounts. This function will be used by automatic
+ * updates
+ */
+void
+modest_ui_actions_do_send_receive_all (ModestWindow *win)
 {
-       g_message ("%s: online? %s", __FUNCTION__,  
-               tny_device_is_online(modest_runtime_get_device()) ? "yes":"no");
+       GSList *account_names, *iter;
 
-       /* TODO: Delete the item->data strings as well as the list? */
-       GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
-                               TRUE /* enabled accounts only */);
-       GSList *iter = account_names;
+       account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr(), 
+                                                         TRUE);
+
+       iter = account_names;
        while (iter) {                  
-               const gchar * account_name = (const char*) iter->data;
-               
-               /* Do not continue if no suitable connection is open: */
-               if (account_name && check_for_connection (account_name)) {
-                       /* As per the UI spec,
-                        * for POP accounts, we should receive,
-                        * for IMAP we should synchronize everything, including receiving,
-                        * for SMTP we should send,
-                        * first receiving, then sending:
-                        */
-                       if (!action_receive(account_name, win))
-                               g_printerr ("modest: failed to receive for account %s\n", account_name);
-                       if (!action_send(account_name))
-                               g_printerr ("modest: failed to send for account %s\n", account_name);
-               }
+               modest_ui_actions_do_send_receive ((const char*) iter->data, win);
+               iter = g_slist_next (iter);
        }
        
-       
+       g_slist_foreach (account_names, (GFunc) g_free, NULL);
        g_slist_free (account_names);
 }
 
-void
-do_send_receive (ModestWindow *win)
-{
-       const gboolean auto_update = TRUE; /* TODO: Get gconf setting. */
-       if (auto_update)
-               do_send_receive_current_or_default (win);
-       else
-               do_send_receive_auto (win);
-}
-
+/*
+ * Handler of the click on Send&Receive button in the main toolbar
+ */
 void
 modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
 {
@@ -878,16 +867,14 @@ modest_ui_actions_on_send_receive (GtkAction *action,  ModestWindow *win)
        g_slist_free (account_names);
        
        /* If not, allow the user to create an account before trying to send/receive. */
-       if (!accounts_exist) {
+       if (!accounts_exist)
                modest_ui_actions_on_accounts (NULL, win);
-       }
-               
-       do_send_receive (win);
+       
+       /* Refresh the active account */
+       modest_ui_actions_do_send_receive (NULL, win);
 }
 
 
-
-
 void
 modest_ui_actions_toggle_header_list_view (GtkAction *action, ModestMainWindow *main_window)
 {
@@ -1094,9 +1081,9 @@ modest_ui_actions_on_folder_selection_changed (ModestFolderView *folder_view,
 
        if (TNY_IS_FOLDER (folder_store)) {
 
-               modest_main_window_set_contents_style (main_window, MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
-
                if (selected) {
+                       modest_main_window_set_contents_style (main_window, 
+                                                              MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS);
                        modest_header_view_set_folder (MODEST_HEADER_VIEW(header_view),
                                                       TNY_FOLDER (folder_store));
                        modest_widget_memory_restore (conf, G_OBJECT(header_view),
index 045e00c..29b73d1 100644 (file)
@@ -162,10 +162,37 @@ void     modest_ui_actions_on_select_editor_background_color      (GtkAction *ac
 void     modest_ui_actions_on_insert_image             (GtkAction *action,
                                                        ModestMsgEditWindow *window);
 
-/* TODO: Is there a better place for this? */
-void do_send_receive (ModestWindow *win);
+/**
+ * modest_ui_actions_do_send_receive_all:
+ * @win: the window that will be used as source of the refresh mail operation
+ * 
+ * Refreshes all the accounts
+ **/
+void    modest_ui_actions_do_send_receive_all          (ModestWindow *win);
+
+/**
+ * modest_ui_actions_do_send_receive:
+ * @account_name: the name of the Modest account or NULL
+ * @win: the window that will be used as source of the refresh mail operation
+ * 
+ * Refreshes the Modest account whose name is passed as argument. If
+ * NULL is passed as #account_name then this function refreses the
+ * active account, if no active account is defined then it picks the
+ * default account
+ **/
+void    modest_ui_actions_do_send_receive              (const gchar *account_name,
+                                                       ModestWindow *win);
 
-void    modest_ui_actions_on_send_receive              (GtkAction *action, ModestWindow *win);
+/**
+ * modest_ui_actions_on_send_receive:
+ * @action: a #GtkAction
+ * @win: the Window that contains the action
+ * 
+ * Handles the activation of the send receive action, for example
+ * clicks on Send&Receive button in the main toolbar
+ **/
+void    modest_ui_actions_on_send_receive              (GtkAction *action, 
+                                                       ModestWindow *win);
 
 void     modest_ui_actions_on_new_folder               (GtkAction *action,
                                                        ModestMainWindow *main_window);