Added option to folders window menu
[modest] / src / hildon2 / modest-folder-window.c
index a171a99..c7493bc 100644 (file)
@@ -92,6 +92,19 @@ static gboolean on_map_event (GtkWidget *widget,
                              GdkEvent *event,
                              gpointer userdata);
 static void update_progress_hint (ModestFolderWindow *self);
+static void on_queue_changed    (ModestMailOperationQueue *queue,
+                                ModestMailOperation *mail_op,
+                                ModestMailOperationQueueNotification type,
+                                ModestFolderWindow *self);
+static void on_activity_changed (ModestFolderView *view,
+                                gboolean activity,
+                                ModestFolderWindow *folder_window);
+static void on_visible_account_changed (ModestFolderView *folder_view,
+                                       const gchar *account_id,
+                                       gpointer user_data);
+static void on_account_changed (TnyAccountStore *account_store,
+                               TnyAccount *account,
+                               gpointer user_data);
 
 typedef struct _ModestFolderWindowPrivate ModestFolderWindowPrivate;
 struct _ModestFolderWindowPrivate {
@@ -105,6 +118,7 @@ struct _ModestFolderWindowPrivate {
 
        gchar *current_store_account;
        gboolean progress_hint;
+       guint queue_change_handler;
 };
 #define MODEST_FOLDER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                                          MODEST_TYPE_FOLDER_WINDOW, \
@@ -174,6 +188,7 @@ modest_folder_window_init (ModestFolderWindow *obj)
                                            "applications_email_folderview");
        priv->progress_hint = FALSE;
        priv->current_store_account = NULL;
+       priv->queue_change_handler = 0;
 }
 
 static void
@@ -201,41 +216,16 @@ modest_folder_window_disconnect_signals (ModestWindow *self)
        ModestFolderWindowPrivate *priv;
        priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
 
+       if (g_signal_handler_is_connected (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
+                                          priv->queue_change_handler))
+               g_signal_handler_disconnect (G_OBJECT (modest_runtime_get_mail_operation_queue ()), 
+                                            priv->queue_change_handler);
+
        modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
        priv->sighandlers = NULL;
 }
 
 static void
-on_visible_account_changed (ModestFolderView *folder_view,
-                           const gchar *account_id,
-                           gpointer user_data)
-{
-       if (account_id) {
-               TnyAccount *acc = 
-                       modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
-                                                                    MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
-                                                                    account_id);
-               if (acc) {
-                       const gchar *name;
-                       gchar *title = NULL;
-
-                       name = modest_tny_account_get_parent_modest_account_name_for_server_account (acc);
-                       title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
-                                                                    name);
-                       if (title) {
-                               gtk_window_set_title (GTK_WINDOW (user_data), title);
-                               g_free (title);
-                       } else {
-                               gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
-                       }
-                       g_object_unref (acc);
-               }
-       } else {
-               gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
-       }
-}
-
-static void
 connect_signals (ModestFolderWindow *self)
 {
        ModestFolderWindowPrivate *priv;
@@ -243,8 +233,8 @@ connect_signals (ModestFolderWindow *self)
        priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
 
        /* folder view */
-       priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, 
-                                                      G_OBJECT (priv->folder_view), "folder-activated", 
+       priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                      G_OBJECT (priv->folder_view), "folder-activated",
                                                       G_CALLBACK (on_folder_activated), self);
 
        priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
@@ -257,12 +247,20 @@ connect_signals (ModestFolderWindow *self)
                                                       "visible-account-changed",
                                                       G_CALLBACK (on_visible_account_changed), self);
 
-       priv->sighandlers = 
-               modest_signal_mgr_connect (priv->sighandlers,
-                                          G_OBJECT (priv->new_message_button),
-                                          "clicked",
-                                          G_CALLBACK (modest_ui_actions_on_new_msg), self);
+       priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                      G_OBJECT (priv->folder_view),
+                                                      "activity-changed",
+                                                      G_CALLBACK (on_activity_changed), self);
+
+       priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                      G_OBJECT (priv->new_message_button),
+                                                      "clicked",
+                                                      G_CALLBACK (modest_ui_actions_on_new_msg), self);
 
+       priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                      G_OBJECT (modest_runtime_get_account_store()),
+                                                      "account-changed",
+                                                      G_CALLBACK (on_account_changed), self);
 }
 
 ModestWindow *
@@ -278,11 +276,19 @@ modest_folder_window_new (TnyFolderStoreQuery *query)
        guint accel_key;
        GdkModifierType accel_mods;
        GtkAccelGroup *accel_group;
+       GtkWidget *top_alignment;
        
        self  = MODEST_FOLDER_WINDOW(g_object_new(MODEST_TYPE_FOLDER_WINDOW, NULL));
        priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
 
        pannable = hildon_pannable_area_new ();
+
+       priv->queue_change_handler =
+               g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
+                                 "queue-changed",
+                                 G_CALLBACK (on_queue_changed),
+                                 self);
+
        priv->folder_view  = modest_platform_create_folder_view (query);
        modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
                                           MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
@@ -293,7 +299,7 @@ modest_folder_window_new (TnyFolderStoreQuery *query)
                          G_CALLBACK (edit_mode_changed), (gpointer) self);
 
        action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
-       priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
+       priv->new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
 
        hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
        new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
@@ -311,14 +317,20 @@ modest_folder_window_new (TnyFolderStoreQuery *query)
                                                  TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
 
        priv->top_vbox = gtk_vbox_new (0, FALSE);
+       top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
+       gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment),
+                                  0, 0,
+                                  HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
 
        gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
-       gtk_box_pack_end (GTK_BOX (priv->top_vbox), pannable, TRUE, TRUE, 0);
+       gtk_container_add (GTK_CONTAINER (top_alignment), pannable);
+       gtk_box_pack_end (GTK_BOX (priv->top_vbox), top_alignment, TRUE, TRUE, 0);
        gtk_container_add (GTK_CONTAINER (self), priv->top_vbox);
 
        gtk_widget_show (priv->folder_view);
        gtk_widget_show (pannable);
        gtk_widget_show (priv->top_vbox);
+       gtk_widget_show (top_alignment);
 
        connect_signals (MODEST_FOLDER_WINDOW (self));
 
@@ -437,6 +449,26 @@ free_refs:
 
 }
 
+void
+modest_folder_window_set_mailbox (ModestFolderWindow *self,
+                                 const gchar *mailbox)
+{
+       ModestFolderWindowPrivate *priv = NULL;
+
+       g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
+       priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
+
+       modest_folder_view_set_mailbox (MODEST_FOLDER_VIEW (priv->folder_view), mailbox);
+       modest_window_set_active_mailbox (MODEST_WINDOW (self), mailbox);
+}
+
+static void
+edit_account (GtkButton *button,
+             ModestFolderWindow *self)
+{
+
+}
+
 static void
 setup_menu (ModestFolderWindow *self)
 {
@@ -457,15 +489,17 @@ setup_menu (ModestFolderWindow *self)
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_folder_window_delete));
 
        /* send receive actions should be only one visible always */
-       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), 
-                                          _("mcen_me_inbox_sendandreceive"), 
-                                          NULL,
+       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
                                           APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive));
 
        modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
                                           APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
+
+       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_edit_account"), NULL,
+                                          APP_MENU_CALLBACK (edit_account),
+                                          MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
 }
 
 static void
@@ -481,17 +515,16 @@ on_folder_activated (ModestFolderView *folder_view,
 
        priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
 
-       if (!folder)
-               return;
-
-       if (!TNY_IS_FOLDER (folder))
+       if (!folder || !TNY_IS_FOLDER (folder))
                return;
 
        /* We cannot open noselect folders (fake ones) */
        if (tny_folder_get_caps (folder) & TNY_FOLDER_CAPS_NOSELECT)
                return;
 
-       headerwin = modest_header_window_new (folder, modest_window_get_active_account (MODEST_WINDOW (self)));
+       headerwin = modest_header_window_new (folder, 
+                                             modest_window_get_active_account (MODEST_WINDOW (self)),
+                                             modest_window_get_active_mailbox (MODEST_WINDOW (self)));
 
        if (modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
                                               MODEST_WINDOW (headerwin),
@@ -601,16 +634,52 @@ on_map_event (GtkWidget *widget,
        return FALSE;
 }
 
+static gboolean
+has_active_operations (ModestFolderWindow *self)
+{
+       GSList *operations = NULL, *node;
+       ModestMailOperationQueue *queue;
+       gboolean has_active = FALSE;
+
+       queue = modest_runtime_get_mail_operation_queue ();
+       operations = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (self));
+
+       for (node = operations; node != NULL; node = g_slist_next (node)) {
+               if (!modest_mail_operation_is_finished (MODEST_MAIL_OPERATION (node->data))) {
+                       has_active = TRUE;
+                       break;
+               }
+       }
+
+       if (operations) {
+               g_slist_foreach (operations, (GFunc) g_object_unref, NULL);
+               g_slist_free (operations);
+       }
+
+       return has_active;
+}
+
 static void
 update_progress_hint (ModestFolderWindow *self)
 {
        ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
 
-       if (!priv->current_store_account)
-               return;
+       if (has_active_operations (self)) {
+               priv->progress_hint = TRUE;
+       } else {
+               priv->progress_hint = FALSE;
+       }
+
+       if (!priv->progress_hint && priv->current_store_account) {
+               priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
+                                                                                          priv->current_store_account);
+       }
+
+       if (!priv->progress_hint) {
+               priv->progress_hint = modest_folder_view_get_activity (MODEST_FOLDER_VIEW (priv->folder_view));
+       }
 
-       priv->progress_hint = modest_window_mgr_has_progress_operation_on_account (modest_runtime_get_window_mgr (),
-                                                                                  priv->current_store_account);
+       modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
 
        if (GTK_WIDGET_VISIBLE (self)) {
                hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint ? 1:0);
@@ -631,3 +700,154 @@ modest_folder_window_transfer_mode_enabled (ModestFolderWindow *self)
 
        return priv->progress_hint;
 }
+
+static void 
+on_mail_operation_started (ModestMailOperation *mail_op,
+                          gpointer user_data)
+{
+       ModestFolderWindow *self;
+       ModestMailOperationTypeOperation op_type;
+       GObject *source = NULL;
+
+       self = MODEST_FOLDER_WINDOW (user_data);
+       op_type = modest_mail_operation_get_type_operation (mail_op);
+       source = modest_mail_operation_get_source(mail_op);
+       if (G_OBJECT (self) == source) {
+               update_progress_hint (self);
+       }
+       g_object_unref (source);
+}
+
+static void 
+on_mail_operation_finished (ModestMailOperation *mail_op,
+                           gpointer user_data)
+{
+       ModestFolderWindow *self;
+
+       self = MODEST_FOLDER_WINDOW (user_data);
+
+       /* Don't disable the progress hint if there are more pending
+          operations from this window */
+       update_progress_hint (self);
+
+       modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (self));
+}
+
+static void
+on_queue_changed (ModestMailOperationQueue *queue,
+                 ModestMailOperation *mail_op,
+                 ModestMailOperationQueueNotification type,
+                 ModestFolderWindow *self)
+{
+       ModestFolderWindowPrivate *priv;
+
+       priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
+
+       /* If this operations was created by another window, do nothing */
+       if (!modest_mail_operation_is_mine (mail_op, G_OBJECT(self))) 
+               return;
+
+       if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED) {
+               priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                              G_OBJECT (mail_op),
+                                                              "operation-started",
+                                                              G_CALLBACK (on_mail_operation_started),
+                                                              self);
+               priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
+                                                              G_OBJECT (mail_op),
+                                                              "operation-finished",
+                                                              G_CALLBACK (on_mail_operation_finished),
+                                                              self);
+       } else if (type == MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED) {
+               priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
+                                                                 G_OBJECT (mail_op),
+                                                                 "operation-started");
+               priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
+                                                                 G_OBJECT (mail_op),
+                                                                 "operation-finished");
+       }
+}
+
+static void
+on_activity_changed (ModestFolderView *view,
+                    gboolean activity,
+                    ModestFolderWindow *folder_window)
+{
+       g_return_if_fail (MODEST_IS_FOLDER_WINDOW (folder_window));
+
+       update_progress_hint (folder_window);
+}
+
+
+static void
+update_window_title (ModestFolderWindow *self,
+                    TnyAccount *account)
+{
+       if (account) {
+               const gchar *name;
+               const gchar *mailbox;
+               gchar *title = NULL;
+               ModestFolderWindowPrivate *priv;
+
+               priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
+
+               mailbox = modest_folder_view_get_mailbox (MODEST_FOLDER_VIEW (priv->folder_view));
+               if (mailbox) {
+                       title = g_strdup (mailbox);
+               } else {
+                       name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+                       title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
+                                                                    name);
+               }
+               if (title) {
+                       gtk_window_set_title (GTK_WINDOW (self), title);
+                       g_free (title);
+               } else {
+                       gtk_window_set_title (GTK_WINDOW (self), _("mcen_ap_name"));
+               }
+       } else {
+               gtk_window_set_title (GTK_WINDOW (self), _("mcen_ap_name"));
+       }
+}
+
+static void
+on_visible_account_changed (ModestFolderView *folder_view,
+                           const gchar *account_id,
+                           gpointer user_data)
+{
+       TnyAccount *account;
+
+       if (!account_id)
+               return;
+
+       account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
+                                                              MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
+                                                              account_id);
+
+       /* Update window title */
+       if (account) {
+               update_window_title (MODEST_FOLDER_WINDOW (user_data), account);
+               g_object_unref (account);
+       }
+}
+
+static void
+on_account_changed (TnyAccountStore *account_store,
+                   TnyAccount *account,
+                   gpointer user_data)
+{
+       const gchar *acc_id, *visible;
+       ModestFolderWindowPrivate *priv;
+
+       if (!TNY_IS_STORE_ACCOUNT (account))
+               return;
+
+       priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (user_data);
+
+       acc_id = tny_account_get_id (account);
+       visible = modest_folder_view_get_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (priv->folder_view));
+
+       /* Update title if the visible account is the one that have just changed */
+       if (acc_id && visible && !g_utf8_collate (acc_id, visible))
+               update_window_title (MODEST_FOLDER_WINDOW (user_data), account);
+}