* Added some missing signal disconnections
[modest] / src / maemo / modest-main-window.c
index 7fc0726..f97cdbf 100644 (file)
 #define YSPACE 0
 
 /* 'private'/'protected' functions */
-static void modest_main_window_class_init    (ModestMainWindowClass *klass);
-static void modest_main_window_init          (ModestMainWindow *obj);
-static void modest_main_window_finalize      (GObject *obj);
+static void modest_main_window_class_init  (ModestMainWindowClass *klass);
+static void modest_main_window_init        (ModestMainWindow *obj);
+static void modest_main_window_finalize    (GObject *obj);
+
 static gboolean modest_main_window_window_state_event (GtkWidget *widget, 
                                                           GdkEventWindowState *event, 
                                                           gpointer userdata);
 
 static void connect_signals (ModestMainWindow *self);
 
-static void restore_settings (ModestMainWindow *self, gboolean do_folder_view_too);
+static void modest_main_window_disconnect_signals (ModestWindow *self);
+
+static void restore_settings (ModestMainWindow *self, 
+                             gboolean do_folder_view_too);
+
 static void save_state (ModestWindow *self);
 
 static void modest_main_window_show_toolbar   (ModestWindow *window,
@@ -96,14 +101,14 @@ static void modest_main_window_show_toolbar   (ModestWindow *window,
 static void cancel_progressbar (GtkToolButton *toolbutton,
                                ModestMainWindow *self);
 
-static void         on_queue_changed                     (ModestMailOperationQueue *queue,
-                                                         ModestMailOperation *mail_op,
-                                                         ModestMailOperationQueueNotification type,
-                                                         ModestMainWindow *self);
+static void on_queue_changed   (ModestMailOperationQueue *queue,
+                               ModestMailOperation *mail_op,
+                               ModestMailOperationQueueNotification type,
+                               ModestMainWindow *self);
 
 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
 
-static void on_account_update                 (TnyAccountStore *account_store, 
+static void account_number_changed            (TnyAccountStore *account_store, 
                                               const gchar *account_name,
                                               gpointer user_data);
 
@@ -113,7 +118,8 @@ static gboolean on_inner_widgets_key_pressed  (GtkWidget *widget,
 
 static void on_configuration_key_changed      (ModestConf* conf, 
                                               const gchar *key, 
-                                              ModestConfEvent event, 
+                                              ModestConfEvent event,
+                                              ModestConfNotificationId id,
                                               ModestMainWindow *self);
 
 static void set_toolbar_mode                  (ModestMainWindow *self, 
@@ -172,8 +178,8 @@ struct _ModestMainWindowPrivate {
        GtkWidget *empty_view;
 
        /* Progress observers */
-       GtkWidget        *progress_bar;
-       GSList           *progress_widgets;
+       GtkWidget   *progress_bar;
+       GSList      *progress_widgets;
 
        /* Tollbar items */
        GtkWidget   *progress_toolitem;
@@ -206,6 +212,8 @@ struct _ModestMainWindowPrivate {
        /* Signal handler UIDs */
        gint queue_changed_handler_uid; 
        GList *queue_err_signals;
+
+       ModestConfNotificationId notification_id;
 };
 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                 MODEST_TYPE_MAIN_WINDOW, \
@@ -299,6 +307,7 @@ modest_main_window_class_init (ModestMainWindowClass *klass)
        modest_window_class->save_state_func = save_state;
        modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
        modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
+       modest_window_class->disconnect_signals_func = modest_main_window_disconnect_signals;
 }
 
 static void
@@ -337,6 +346,10 @@ modest_main_window_finalize (GObject *obj)
 
        priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
 
+       /* Sanity check: shouldn't be needed, the window mgr should
+          call this function before */
+       modest_main_window_disconnect_signals (MODEST_WINDOW (obj));
+
        modest_main_window_cleanup_queue_error_signals ((ModestMainWindow *) obj);
 
        g_slist_free (priv->progress_widgets);
@@ -348,10 +361,11 @@ modest_main_window_finalize (GObject *obj)
                priv->progress_bar_timeout = 0;
        }
 
-       /* Disconnect signal handlers */
-       if (priv->queue_changed_handler_uid)
-               g_signal_handler_disconnect (modest_runtime_get_mail_operation_queue (),
-                                            priv->queue_changed_handler_uid);
+       if (priv->notification_id) {
+               modest_conf_forget_namespace (modest_runtime_get_conf (),
+                                             MODEST_CONF_NAMESPACE,
+                                             priv->notification_id);
+       }
 
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -626,6 +640,20 @@ _header_view_csm_menu_activated (GtkWidget *widget, gpointer user_data)
 }
 
 static void
+modest_main_window_disconnect_signals (ModestWindow *self)
+{      
+       ModestMainWindowPrivate *priv;
+       
+       priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
+
+       /* Disconnect signal handlers */
+       if (g_signal_handler_is_connected (modest_runtime_get_mail_operation_queue (),
+                                          priv->queue_changed_handler_uid))
+               g_signal_handler_disconnect (modest_runtime_get_mail_operation_queue (),
+                                            priv->queue_changed_handler_uid);
+}
+
+static void
 connect_signals (ModestMainWindow *self)
 {      
        ModestWindowPrivate *parent_priv;
@@ -673,7 +701,6 @@ connect_signals (ModestMainWindow *self)
                          self);
        
        /* window */
-/*     g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self); */
        g_signal_connect (G_OBJECT (self), "window-state-event",
                          G_CALLBACK (modest_main_window_window_state_event),
                          NULL);
@@ -684,13 +711,18 @@ connect_signals (ModestMainWindow *self)
                                  "queue-changed", G_CALLBACK (on_queue_changed), self);
 
        /* Track changes in the device name */
+       priv->notification_id =  modest_conf_listen_to_namespace (modest_runtime_get_conf (), 
+                                                                 MODEST_CONF_NAMESPACE);
        g_signal_connect (G_OBJECT(modest_runtime_get_conf ()),
                          "key_changed", G_CALLBACK (on_configuration_key_changed), 
                          self);
 
        /* Track account changes. We need to refresh the toolbar */
        g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
-                         "account_update", G_CALLBACK (on_account_update),
+                         "account_inserted", G_CALLBACK (account_number_changed),
+                         self);
+       g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
+                         "account_removed", G_CALLBACK (account_number_changed),
                          self);
 
        /* Account store */
@@ -1194,7 +1226,7 @@ modest_main_window_show_toolbar (ModestWindow *self,
                gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
 
                /* Set send & receive button tap and hold menu */
-               on_account_update (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
+               account_number_changed (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
                                   NULL, self);
        }
 
@@ -1218,9 +1250,9 @@ compare_display_names (ModestAccountData *a,
 }
 
 static void 
-on_account_update (TnyAccountStore *account_store, 
-                  const gchar *account_name,
-                  gpointer user_data)
+account_number_changed (TnyAccountStore *account_store, 
+                       const gchar *account_name,
+                       gpointer user_data)
 {
        GSList *account_names, *iter, *accounts;
        ModestMainWindow *self;
@@ -1432,7 +1464,7 @@ on_account_update (TnyAccountStore *account_store,
        for (i = 0; i < num_accounts; i++) {
                ModestAccountData *account_data = (ModestAccountData *) g_slist_nth_data (accounts, i);
 
-               if(account_data->account_name &&
+               if(account_data->account_name && default_account &&
                   strcmp (account_data->account_name, default_account) == 0) {
                        gchar *item_name = g_strconcat (account_data->account_name, "Menu", NULL);
 
@@ -1857,13 +1889,16 @@ modest_main_window_get_contents_style (ModestMainWindow *self)
 static void 
 on_configuration_key_changed (ModestConf* conf, 
                              const gchar *key, 
-                             ModestConfEvent event, 
+                             ModestConfEvent event,
+                             ModestConfNotificationId id, 
                              ModestMainWindow *self)
 {
        ModestMainWindowPrivate *priv;
        TnyAccount *account;
 
-       if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
+       if (!key || 
+           priv->notification_id != id ||
+           strcmp (key, MODEST_CONF_DEVICE_NAME))
                return;
 
        priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);