+
+ return FALSE;
+}
+
+static void
+set_toolbar_mode (ModestMainWindow *self,
+ ModestToolBarModes mode)
+{
+ ModestWindowPrivate *parent_priv = NULL;
+ ModestMainWindowPrivate *priv = NULL;
+ GtkAction *sort_action = NULL, *refresh_action = NULL, *cancel_action = NULL;
+
+ g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
+
+ parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+ g_return_if_fail (GTK_IS_TOOLBAR(parent_priv->toolbar));
+
+ sort_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarSort");
+ refresh_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarSendReceive");
+ cancel_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarCancel");
+
+ /* Sets current toolbar mode */
+ priv->current_toolbar_mode = mode;
+
+ /* Show and hide toolbar items */
+ switch (mode) {
+ case TOOLBAR_MODE_NORMAL:
+ if (sort_action)
+ gtk_action_set_visible (sort_action, TRUE);
+ if (refresh_action)
+ gtk_action_set_visible (refresh_action, TRUE);
+ if (priv->progress_toolitem) {
+ gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), FALSE);
+ gtk_widget_hide (priv->progress_toolitem);
+ }
+ if (priv->progress_bar)
+ gtk_widget_hide (priv->progress_bar);
+
+ if (cancel_action)
+ gtk_action_set_visible (cancel_action, FALSE);
+
+ /* Hide toolbar if optimized view is enabled */
+ if (priv->optimized_view)
+ gtk_widget_hide (GTK_WIDGET(parent_priv->toolbar));
+ break;
+ case TOOLBAR_MODE_TRANSFER:
+ if (sort_action)
+ gtk_action_set_visible (sort_action, FALSE);
+ if (refresh_action)
+ gtk_action_set_visible (refresh_action, FALSE);
+ if (cancel_action)
+ gtk_action_set_visible (cancel_action, TRUE);
+ if (priv->progress_toolitem) {
+ gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
+ gtk_widget_show (priv->progress_toolitem);
+ }
+ if (priv->progress_bar)
+ gtk_widget_show (priv->progress_bar);
+
+ /* Show toolbar if it's hiden (optimized view ) */
+ if (priv->optimized_view)
+ gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
+ break;
+ default:
+ g_return_if_reached ();
+ }
+}
+
+static void
+cancel_progressbar (GtkToolButton *toolbutton,
+ ModestMainWindow *self)
+{
+ GSList *tmp;
+ ModestMainWindowPrivate *priv;
+
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+ /* Get operation observers and cancel its current operation */
+ tmp = priv->progress_widgets;
+ while (tmp) {
+ modest_progress_object_cancel_current_operation (MODEST_PROGRESS_OBJECT(tmp->data));
+ tmp=g_slist_next(tmp);
+ }
+}
+
+static gboolean
+observers_empty (ModestMainWindow *self)
+{
+ GSList *tmp = NULL;
+ ModestMainWindowPrivate *priv;
+ gboolean is_empty = TRUE;
+ guint pending_ops = 0;
+
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+ tmp = priv->progress_widgets;
+
+ /* Check all observers */
+ while (tmp && is_empty) {
+ pending_ops = modest_progress_object_num_pending_operations (MODEST_PROGRESS_OBJECT(tmp->data));
+ is_empty = pending_ops == 0;
+
+ tmp = g_slist_next(tmp);
+ }
+
+ return is_empty;
+}
+
+static void
+on_queue_changed (ModestMailOperationQueue *queue,
+ ModestMailOperation *mail_op,
+ ModestMailOperationQueueNotification type,
+ ModestMainWindow *self)
+{
+ ModestMainWindowPrivate *priv;
+ ModestMailOperationTypeOperation op_type;
+ ModestToolBarModes mode;
+ GSList *tmp;
+ gboolean mode_changed = FALSE;
+/* ModestMailOperationStatus status; */
+
+ g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
+ priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+ /* Get toolbar mode from operation id*/
+ op_type = modest_mail_operation_get_type_operation (mail_op);
+ switch (op_type) {
+ case MODEST_MAIL_OPERATION_TYPE_SEND:
+ case MODEST_MAIL_OPERATION_TYPE_RECEIVE:
+ mode = TOOLBAR_MODE_TRANSFER;
+ if (priv->current_toolbar_mode == TOOLBAR_MODE_NORMAL)
+ mode_changed = TRUE;
+ break;
+ default:
+ mode = TOOLBAR_MODE_NORMAL;
+
+ }
+
+
+ /* Add operation observers and change toolbar if neccessary*/
+ tmp = priv->progress_widgets;
+ switch (type) {
+ case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
+ if (mode == TOOLBAR_MODE_TRANSFER) {
+ if (mode_changed)
+ set_toolbar_transfer_mode(self);
+ while (tmp) {
+ modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
+ mail_op);
+ tmp = g_slist_next (tmp);
+ }
+ }
+ break;
+ case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
+ /* Change toolbar mode */
+ 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);
+
+ }
+ }
+
+ break;
+ }
+
+}
+
+static void
+on_show_account_action_activated (GtkAction *action,
+ gpointer user_data)
+{
+ ModestAccountData *acc_data;
+ 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, acc_name);
+
+ /* 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);
+ modest_window_set_active_account (MODEST_WINDOW (self), acc_data->account_name);
+ }
+
+ /* Free */
+ modest_account_mgr_free_account_data (mgr, acc_data);
+}
+
+static void
+refresh_account (const gchar *account_name)
+{
+ ModestWindow *win;
+
+ win = MODEST_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
+
+ /* If account_name == NULL, we must update all (option All) */
+ if (!account_name)
+ modest_ui_actions_do_send_receive_all (win);
+ else
+ modest_ui_actions_do_send_receive (account_name, win);
+}
+
+static void
+on_refresh_account_action_activated (GtkAction *action,
+ gpointer user_data)
+{
+ refresh_account ((const gchar*) user_data);
+}
+
+static void
+on_send_receive_csm_activated (GtkMenuItem *item,
+ gpointer user_data)
+{
+ refresh_account ((const gchar*) user_data);