* listen to updates in the account store, and refresh
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 11 Jul 2006 16:43:48 +0000 (16:43 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 11 Jul 2006 16:43:48 +0000 (16:43 +0000)
pmo-trunk-r364

src/modest-tny-folder-tree-view.c
src/modest-tny-folder-tree-view.h

index 01e18c8..119f944 100644 (file)
@@ -27,9 +27,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-
-/* modest-tny-folder-tree-view.c */
-
 #include <glib/gi18n.h>
 #include <string.h>
 
@@ -69,9 +66,10 @@ struct _ModestTnyFolderTreeViewPrivate {
        TnyMsgFolderIface *cur_folder;
        gboolean view_is_empty;
 };
-#define MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
-                                                         MODEST_TYPE_TNY_FOLDER_TREE_VIEW, \
-                                                         ModestTnyFolderTreeViewPrivate))
+#define MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(o)                     \
+       (G_TYPE_INSTANCE_GET_PRIVATE((o),                               \
+                                    MODEST_TYPE_TNY_FOLDER_TREE_VIEW,  \
+                                    ModestTnyFolderTreeViewPrivate))
 /* globals */
 static GObjectClass *parent_class = NULL;
 
@@ -119,7 +117,8 @@ modest_tny_folder_tree_view_class_init (ModestTnyFolderTreeViewClass *klass)
                g_signal_new ("folder_selected",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_FIRST,
-                             G_STRUCT_OFFSET (ModestTnyFolderTreeViewClass,folder_selected),
+                             G_STRUCT_OFFSET (ModestTnyFolderTreeViewClass,
+                                              folder_selected),
                              NULL, NULL,
                              g_cclosure_marshal_VOID__POINTER,
                              G_TYPE_NONE, 1, G_TYPE_POINTER); 
@@ -172,25 +171,34 @@ guess_folder_type (const gchar* name)
        type = TNY_MSG_FOLDER_TYPE_NORMAL;
        folder = g_utf8_strdown (name, strlen(name));
 
-       if (strcmp (folder, "inbox") == 0 || strcmp (folder, _("inbox")) == 0)
-           type = TNY_MSG_FOLDER_TYPE_INBOX;
-       else if (strcmp (folder, "outbox") == 0 || strcmp (folder, _("outbox")) == 0)
+       if (strcmp (folder, "inbox") == 0 ||
+           strcmp (folder, _("inbox")) == 0)
+               type = TNY_MSG_FOLDER_TYPE_INBOX;
+       else if (strcmp (folder, "outbox") == 0 ||
+                strcmp (folder, _("outbox")) == 0)
                type = TNY_MSG_FOLDER_TYPE_OUTBOX;
-       else if (g_str_has_prefix(folder, "junk") || g_str_has_prefix(folder, _("junk")))
+       else if (g_str_has_prefix(folder, "junk") ||
+                g_str_has_prefix(folder, _("junk")))
                type = TNY_MSG_FOLDER_TYPE_JUNK;
-       else if (g_str_has_prefix(folder, "trash") || g_str_has_prefix(folder, _("trash")))
+       else if (g_str_has_prefix(folder, "trash") ||
+                g_str_has_prefix(folder, _("trash")))
                type = TNY_MSG_FOLDER_TYPE_JUNK;
-       else if (g_str_has_prefix(folder, "sent") || g_str_has_prefix(folder, _("sent")))
+       else if (g_str_has_prefix(folder, "sent") ||
+                g_str_has_prefix(folder, _("sent")))
                type = TNY_MSG_FOLDER_TYPE_SENT;
 
        /* these are not *really* TNY_ types */
-       else if (g_str_has_prefix(folder, "draft") || g_str_has_prefix(folder, _("draft")))
+       else if (g_str_has_prefix(folder, "draft") ||
+                g_str_has_prefix(folder, _("draft")))
                type = TNY_MSG_FOLDER_TYPE_DRAFTS;
-       else if (g_str_has_prefix(folder, "notes") || g_str_has_prefix(folder, _("notes")))
+       else if (g_str_has_prefix(folder, "notes") ||
+                g_str_has_prefix(folder, _("notes")))
                type = TNY_MSG_FOLDER_TYPE_NOTES;
-       else if (g_str_has_prefix(folder, "contacts") || g_str_has_prefix(folder, _("contacts")))
+       else if (g_str_has_prefix(folder, "contacts") ||
+                g_str_has_prefix(folder, _("contacts")))
                type = TNY_MSG_FOLDER_TYPE_CONTACTS;
-       else if (g_str_has_prefix(folder, "calendar") || g_str_has_prefix(folder, _("calendar")))
+       else if (g_str_has_prefix(folder, "calendar") ||
+                g_str_has_prefix(folder, _("calendar")))
                type = TNY_MSG_FOLDER_TYPE_CALENDAR;
        
        g_free (folder);
@@ -246,7 +254,6 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        case TNY_MSG_FOLDER_TYPE_CONTACTS:
                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_CONTACTS);
                 break;
-               
        case TNY_MSG_FOLDER_TYPE_NORMAL:
         default:
                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_NORMAL);
@@ -342,8 +349,20 @@ modest_tny_folder_tree_view_set_account_store (TnySummaryWindowIface *self,
 
 
 
+static void
+on_accounts_update (TnyAccountStoreIface *account_store, const gchar *account,
+                   gpointer user_data)
+{
+       update_model_empty (MODEST_TNY_FOLDER_TREE_VIEW(user_data));
+       
+       if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(user_data), account_store))
+               g_printerr ("modest: failed to update model for changes in '%s'",
+                           account);
+}
+
+
 GtkWidget*
-modest_tny_folder_tree_view_new (TnyAccountStoreIface *iface)
+modest_tny_folder_tree_view_new (TnyAccountStoreIface *account_store)
 {
        GObject *self;
        ModestTnyFolderTreeViewPrivate *priv;
@@ -352,11 +371,14 @@ modest_tny_folder_tree_view_new (TnyAccountStoreIface *iface)
        self = G_OBJECT(g_object_new(MODEST_TYPE_TNY_FOLDER_TREE_VIEW, NULL));
        priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
 
-       g_return_val_if_fail (iface, NULL);
+       g_return_val_if_fail (account_store, NULL);
        
-       if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(self), iface))
-               g_warning ("failed or update model");
+       if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(self), account_store))
+               g_printerr ("modest: failed to update model");
 
+       g_signal_connect (G_OBJECT(account_store), "update_accounts",
+                         G_CALLBACK (on_accounts_update), self);
+       
        sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
        g_signal_connect (sel, "changed",
                          G_CALLBACK(selection_changed), self);
@@ -394,49 +416,35 @@ update_model_empty (ModestTnyFolderTreeView *self)
 
 
 static gboolean
-update_model (ModestTnyFolderTreeView *self, TnyAccountStoreIface *iface)
+update_model (ModestTnyFolderTreeView *self, TnyAccountStoreIface *account_store)
 {
-       const GList *accounts;
-       TnyAccountTreeModel *folder_model;
        ModestTnyFolderTreeViewPrivate *priv;
-               
-       g_return_val_if_fail (iface, FALSE);
-
-       priv =  MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
-       priv->view_is_empty = TRUE;
-
-       accounts = tny_account_store_iface_get_store_accounts (iface);
-       if (!accounts) {
-               g_warning ("no accounts have been defined yet");
-               return update_model_empty (self);
-       }
+       TnyListIface     *account_list;
+       GtkTreeModel     *model, *sortable;
        
-       folder_model = tny_account_tree_model_new ();
-       if (!folder_model) {
-               g_warning ("failed to get account tree model");
-               return update_model_empty (self);
-       }
+       g_return_val_if_fail (account_store, FALSE);
        
-       while (accounts) {
-               TnyStoreAccountIface *account =
-                       TNY_STORE_ACCOUNT_IFACE(accounts->data);
-               if (!account) {
-                       g_warning ("invalid account");
-                       g_object_unref (folder_model);
-                       return update_model_empty (self);
-               }
-               tny_account_tree_model_add (TNY_ACCOUNT_TREE_MODEL (folder_model),
-                                           account);
-               accounts = accounts->next;
-       }
+       priv =  MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
+
+       model        = GTK_TREE_MODEL(tny_account_tree_model_new ());
+       account_list = TNY_LIST_IFACE(model);
+
+       update_model_empty (self); /* cleanup */
+       priv->view_is_empty = TRUE;
        
-       gtk_tree_view_set_model (GTK_TREE_VIEW(self),
-                                GTK_TREE_MODEL(folder_model)); 
-       g_object_unref (G_OBJECT(folder_model));
+       tny_account_store_iface_get_accounts (account_store, account_list,
+                                             TNY_ACCOUNT_STORE_IFACE_STORE_ACCOUNTS);
+       if (!account_list) /* no store accounts found */ 
+               return TRUE;
        
-       priv->view_is_empty = FALSE; /* were not empty anymore! */
+       sortable = gtk_tree_model_sort_new_with_model (model);
+       gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
+
+       priv->view_is_empty = FALSE;    
+       g_object_unref (model);
+
        return TRUE;
-}
+} 
 
 
 void
@@ -458,8 +466,8 @@ selection_changed (GtkTreeSelection *sel, gpointer user_data)
                return;
        
        /* folder was _un_selected if true */
-       if (!gtk_tree_selection_get_selected (sel, &model, &iter))
-       {
+       if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
+               
                if (priv->cur_folder) 
                        tny_msg_folder_iface_expunge (priv->cur_folder);
                priv->cur_folder = NULL;
index b725d5b..2c99c6d 100644 (file)
@@ -27,9 +27,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-
-/* modest-tny-folder-tree-view.h */
-
 #ifndef __MODEST_TNY_FOLDER_TREE_VIEW_H__
 #define __MODEST_TNY_FOLDER_TREE_VIEW_H__
 
@@ -113,7 +110,7 @@ gboolean     modest_tny_folder_tree_view_is_empty    (ModestTnyFolderTreeView *s
  * Returns: TRUE on success, FALSE otherwise
  */
 gboolean     modest_tny_folder_tree_view_update_model(ModestTnyFolderTreeView *self, 
-                                         TnyAccountStoreIface *iface);
+                                                     TnyAccountStoreIface *iface);
 
 
 G_END_DECLS