* all:
[modest] / src / widgets / modest-account-view.c
index eb3e0f3..cfaa5e6 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <glib/gi18n.h>
 #include "modest-account-view.h"
-/* include other impl specific header files */
+
+#include <modest-account-mgr.h>
+#include <modest-account-mgr-helpers.h>
+
+#include <gtk/gtkcellrenderertoggle.h>
+#include <gtk/gtkcellrenderertext.h>
+#include <gtk/gtktreeselection.h>
+#include <gtk/gtkliststore.h>
 
 /* 'private'/'protected' functions */
 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
@@ -55,6 +63,8 @@ enum {
 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
 struct _ModestAccountViewPrivate {
        ModestAccountMgr *account_mgr;
+       gulong sig1, sig2;
+       
 };
 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                  MODEST_TYPE_ACCOUNT_VIEW, \
@@ -80,6 +90,7 @@ modest_account_view_get_type (void)
                        sizeof(ModestAccountView),
                        1,              /* n_preallocs */
                        (GInstanceInitFunc) modest_account_view_init,
+                       NULL
                };
                my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
                                                  "ModestAccountView",
@@ -98,13 +109,6 @@ modest_account_view_class_init (ModestAccountViewClass *klass)
        gobject_class->finalize = modest_account_view_finalize;
 
        g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
-
-       /* signal definitions go here, e.g.: */
-/*     signals[MY_SIGNAL_1] = */
-/*             g_signal_new ("my_signal_1",....); */
-/*     signals[MY_SIGNAL_2] = */
-/*             g_signal_new ("my_signal_2",....); */
-/*     etc. */
 }
 
 static void
@@ -123,11 +127,18 @@ modest_account_view_finalize (GObject *obj)
        ModestAccountViewPrivate *priv;
 
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
-       
+
+       g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
+                                    priv->sig1);
+       g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
+                                    priv->sig2);
+
        if (priv->account_mgr) {
                g_object_unref (G_OBJECT(priv->account_mgr));
                priv->account_mgr = NULL; 
        }
+       
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
 
@@ -143,36 +154,41 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
 
        cursor = account_names =
                modest_account_mgr_account_names (account_mgr, NULL);
-       
+
        while (cursor) {
                gchar    *proto = NULL;
-               gchar    *store, *account_name;
+               gchar    *store, *account_name, *display_name;
                gboolean enabled;
 
                account_name = (gchar*)cursor->data;
                
-               store        = modest_account_mgr_get_string (account_mgr,
+               display_name = modest_account_mgr_get_string (account_mgr,
                                                              account_name,
-                                                             MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                             MODEST_ACCOUNT_DISPLAY_NAME,
                                                              FALSE, NULL);
-               if (store) {
-                       proto = modest_account_mgr_get_string (account_mgr,
-                                                              store,
-                                                              MODEST_ACCOUNT_PROTO,
-                                                              TRUE, NULL);
-                       g_free(store);
-               }
-               
-               enabled = modest_account_mgr_account_get_enabled (account_mgr,
-                                                                 account_name,
-                                                                 FALSE);
-               gtk_list_store_insert_with_values (
-                       model, NULL, 0,
-                       ENABLED_COLUMN, enabled,
-                       NAME_COLUMN,  account_name,
-                       PROTO_COLUMN, proto,
-                       -1);
+               /* don't display accounts without stores */
+               if (display_name) {
+                       store = modest_account_mgr_get_string (account_mgr,
+                                                              account_name,
+                                                              MODEST_ACCOUNT_STORE_ACCOUNT,
+                                                              FALSE, NULL);
+                       if (store) {
+                               proto = modest_account_mgr_get_string (account_mgr,
+                                                                      store,
+                                                                      MODEST_ACCOUNT_PROTO,
+                                                                      TRUE, NULL);
+                               g_free(store);
+                       }
                
+                       enabled = modest_account_mgr_get_enabled (account_mgr, account_name);
+                       gtk_list_store_insert_with_values (
+                               model, NULL, 0,
+                               ENABLED_COLUMN, enabled,
+                               NAME_COLUMN,  display_name,
+                               PROTO_COLUMN, proto,
+                               -1);
+               }
+               g_free (display_name);
                g_free (account_name);
                g_free (proto);
                
@@ -186,10 +202,7 @@ static void
 on_account_changed (ModestAccountMgr *account_mgr,
                    const gchar* account, const gchar* key,
                    gboolean server_account, ModestAccountView *self)
-{
-       ModestAccountViewPrivate *priv;
-       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
-       
+{      
        update_account_view (account_mgr, self);
 }
 
@@ -197,9 +210,9 @@ on_account_changed (ModestAccountMgr *account_mgr,
 static void
 on_account_removed (ModestAccountMgr *account_mgr,
                    const gchar* account, gboolean server_account,
-                   gpointer user_data)
+                   ModestAccountView *self)
 {
-       on_account_changed (account_mgr, account, NULL, server_account, user_data);
+       on_account_changed (account_mgr, account, NULL, server_account, self);
 }
 
 
@@ -223,23 +236,19 @@ on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
                return;
        }
        
-       gtk_tree_model_get (model, &iter,
-                           ENABLED_COLUMN, &enabled,
+       gtk_tree_model_get (model, &iter, ENABLED_COLUMN, &enabled,
                            NAME_COLUMN, &account_name,
                            -1);
        
        /* toggle enabled / disabled */
-       modest_account_mgr_account_set_enabled (priv->account_mgr, account_name,
-                                               FALSE, !enabled);
+       modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
        g_free (account_name);
 }
 
-
 static void
 init_view (ModestAccountView *self)
 {
        ModestAccountViewPrivate *priv;
-       GtkTreeSelection *sel;
        GtkCellRenderer *renderer;
        GtkListStore *model;
        
@@ -274,14 +283,12 @@ init_view (ModestAccountView *self)
                                             gtk_cell_renderer_text_new (),
                                             "text", PROTO_COLUMN, NULL));
 
-       sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
-       
-       g_signal_connect (G_OBJECT(priv->account_mgr),
-                         "account_removed",
-                         G_CALLBACK(on_account_removed), self);
-       g_signal_connect (G_OBJECT(priv->account_mgr),
-                         "account_changed",
-                         G_CALLBACK(on_account_changed), self);
+       priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),
+                                      "account_removed",
+                                      G_CALLBACK(on_account_removed), self);
+       priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr),
+                                      "account_changed",
+                                      G_CALLBACK(on_account_changed), self);
 }
 
 
@@ -291,17 +298,37 @@ modest_account_view_new (ModestAccountMgr *account_mgr)
 {
        GObject *obj;
        ModestAccountViewPrivate *priv;
-
+       
        g_return_val_if_fail (account_mgr, NULL);
        
        obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
-
+       
        g_object_ref (G_OBJECT(account_mgr));
        priv->account_mgr = account_mgr;
 
        init_view (MODEST_ACCOUNT_VIEW (obj));
+       update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
        
        return MODEST_ACCOUNT_VIEW(obj);
 }
 
+const gchar *
+modest_account_view_get_selected_account (ModestAccountView *self)
+{
+       const gchar *account_name = NULL;
+       GtkTreeSelection *sel;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
+
+       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
+       if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
+               gtk_tree_model_get (model, &iter,
+                                   NAME_COLUMN, &account_name,
+                                   -1);
+       }
+
+       return account_name;
+}