Patch to compile Modest with the most recent changes
[modest] / src / modest-tny-local-folders-account.c
index 5ec6b52..6b2b6a8 100644 (file)
@@ -39,6 +39,7 @@
 #include <tny-camel-folder.h>
 #include <tny-merge-folder.h>
 #include <tny-simple-list.h>
+#include <tny-gtk-lockable.h>
 
 G_DEFINE_TYPE (ModestTnyLocalFoldersAccount, 
        modest_tny_local_folders_account, 
@@ -51,7 +52,7 @@ typedef struct _ModestTnyLocalFoldersAccountPrivate ModestTnyLocalFoldersAccount
 
 struct _ModestTnyLocalFoldersAccountPrivate
 {
-       GSList *list_extra_folders;
+       TnyMergeFolder *outbox_folder;
 };
 
 static void         get_folders    (TnyFolderStore *self, 
@@ -64,44 +65,9 @@ static TnyFolder*   create_folder  (TnyFolderStore *self,
                                    GError **err);
 
 static void
-modest_tny_local_folders_account_dispose (GObject *object)
-{
-  if (G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->dispose)
-    G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->dispose (object);
-}
-
-
-static void
-modest_tny_local_folders_account_remove_all_extra_folders (ModestTnyLocalFoldersAccount *store)
-{
-       ModestTnyLocalFoldersAccountPrivate *priv = 
-               TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (store);
-               
-       GSList *iter = priv->list_extra_folders;
-       while (iter)
-       {
-               TnyFolder *folder = (TnyFolder*)iter->data;
-               if (folder) {
-                       g_object_unref (folder);
-                       iter->data = NULL;
-               }
-                       
-               iter = g_slist_next (iter);
-       }
-       
-       g_slist_free (priv->list_extra_folders);
-       priv->list_extra_folders = NULL;
-}
-
-static void
 modest_tny_local_folders_account_finalize (GObject *object)
 {
-       G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->finalize (object);
-  
-       ModestTnyLocalFoldersAccount *self = 
-               MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (object);
-               
-       modest_tny_local_folders_account_remove_all_extra_folders (self);
+       G_OBJECT_CLASS (modest_tny_local_folders_account_parent_class)->finalize (object);
 }
 
 static void
@@ -111,7 +77,6 @@ modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *
        
        g_type_class_add_private (klass, sizeof (ModestTnyLocalFoldersAccountPrivate));
        
-       object_class->dispose = modest_tny_local_folders_account_dispose;
        object_class->finalize = modest_tny_local_folders_account_finalize;
          
        /* Override virtual functions from the parent class: */
@@ -122,6 +87,7 @@ modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *
 static void
 modest_tny_local_folders_account_init (ModestTnyLocalFoldersAccount *self)
 {
+       /* Do nothing */
 }
 
 ModestTnyLocalFoldersAccount*
@@ -130,31 +96,15 @@ modest_tny_local_folders_account_new (void)
   return g_object_new (MODEST_TYPE_TNY_LOCAL_FOLDERS_ACCOUNT, NULL);
 }
 
-void
-modest_tny_local_folders_account_add_extra_folder (ModestTnyLocalFoldersAccount *store, 
-       TnyFolder *folder)
-{
-       ModestTnyLocalFoldersAccountPrivate *priv = 
-               TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (store);
-               
-       /* Check that it isn't already in the list: */
-       GSList *exists = g_slist_find (priv->list_extra_folders, folder);
-       if (exists)
-               return;
-               
-       /* Add it: */
-       /* The reference is released in finalize: */
-       priv->list_extra_folders = g_slist_append (priv->list_extra_folders, folder);
-       g_object_ref (folder);
-}
-
+/**********************************************************/
+/*          TnyCamelStoreAccount functions redefinitions  */
+/**********************************************************/
 static gboolean 
 modest_tny_local_folders_account_query_passes (TnyFolderStoreQuery *query, TnyFolder *folder)
 {
        gboolean retval = FALSE;
 
-       if (query && (tny_list_get_length (tny_folder_store_query_get_items (query)) > 0))
-       {
+       if (query && (tny_list_get_length (tny_folder_store_query_get_items (query)) > 0)) {
                TnyList *items = tny_folder_store_query_get_items (query);
                TnyIterator *iterator;
                iterator = tny_list_create_iterator (items);
@@ -162,30 +112,33 @@ modest_tny_local_folders_account_query_passes (TnyFolderStoreQuery *query, TnyFo
                while (!tny_iterator_is_done (iterator))
                {
                        TnyFolderStoreQueryItem *item = (TnyFolderStoreQueryItem*) tny_iterator_get_current (iterator);
-                       TnyFolderStoreQueryOption options = tny_folder_store_query_item_get_options (item);
-                       regex_t *regex = tny_folder_store_query_item_get_regex (item);
+                       if (item) {
+                               TnyFolderStoreQueryOption options = tny_folder_store_query_item_get_options (item);
+                               const regex_t *regex = tny_folder_store_query_item_get_regex (item);
 
-                       if ((options & TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED) &&
-                           tny_folder_is_subscribed (folder))
-                               retval = TRUE;
+                               if ((options & TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED) &&
+                                  tny_folder_is_subscribed (folder))
+                                       retval = TRUE;
 
-                       if ((options & TNY_FOLDER_STORE_QUERY_OPTION_UNSUBSCRIBED) &&
-                           !(tny_folder_is_subscribed (folder)))
-                               retval = TRUE;
+                               if ((options & TNY_FOLDER_STORE_QUERY_OPTION_UNSUBSCRIBED) &&
+                                   !(tny_folder_is_subscribed (folder)))
+                                       retval = TRUE;
 
-                       if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_NAME)
-                           if (regexec (regex, tny_folder_get_name (folder), 0, NULL, 0) == 0)
-                               retval = TRUE;
+                               if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_NAME)
+                                  if (regexec (regex, tny_folder_get_name (folder), 0, NULL, 0) == 0)
+                                       retval = TRUE;
 
-                       if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_ID)
-                           if (regexec (regex, tny_folder_get_id (folder), 0, NULL, 0) == 0)
-                               retval = TRUE;
+                               if (regex && options & TNY_FOLDER_STORE_QUERY_OPTION_MATCH_ON_ID)
+                                 if (regexec (regex, tny_folder_get_id (folder), 0, NULL, 0) == 0)
+                                       retval = TRUE;
+
+                               g_object_unref (G_OBJECT (item));
+                       }
 
-                       g_object_unref (G_OBJECT (item));
                        tny_iterator_next (iterator);
                }
                 
-               g_object_unref (G_OBJECT (iterator));    
+               g_object_unref (G_OBJECT (iterator));
                g_object_unref (G_OBJECT (items));
        } else
                retval = TRUE;
@@ -193,154 +146,127 @@ modest_tny_local_folders_account_query_passes (TnyFolderStoreQuery *query, TnyFo
        return retval;
 }
 
-
 static void
 get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err)
 {
-       ModestTnyLocalFoldersAccountPrivate *priv = 
-               TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
-               
+       TnyCamelStoreAccountClass *parent_class;
+       ModestTnyLocalFoldersAccountPrivate *priv;
+
        /* Call the base class implementation: */
-       TnyCamelStoreAccountClass *parent_class = g_type_class_peek_parent (
-               MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
+       parent_class = g_type_class_peek_parent (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
        parent_class->get_folders_func (self, list, query, err);
        
-       /* Add our extra folders only if it passes the query */
-       GSList *iter = priv->list_extra_folders;
-       while (iter) {
-               TnyFolder *folder = TNY_FOLDER (iter->data);
-
-               if (folder && modest_tny_local_folders_account_query_passes (query, folder))
-                       tny_list_prepend (list, G_OBJECT (folder));
-                       
-               iter = g_slist_next (iter);
-       }
-}
-
-static void
-add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge_folder)
-{
-       const gchar* account_id = tny_account_get_id (account);
-       const gboolean is_actual_local_folders_account = account_id && 
-               (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0);
-               
-       TnyList *list_outbox_folders = tny_simple_list_new ();
-       tny_folder_store_get_folders (TNY_FOLDER_STORE (account), 
-               list_outbox_folders, NULL, NULL);
-               
-       TnyIterator*  iter =  tny_list_create_iterator (list_outbox_folders);
-       while (!tny_iterator_is_done (iter))
-       {
-               TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
-               
-               if (folder) {
-                       gboolean add = TRUE;
-                       /* TODO: Do not add outboxes that are inside local-folders/, 
-                        * because these are just left-over from earlier Modest versions 
-                        * that put the outbox there: */
-                       if (is_actual_local_folders_account) {
-                               const TnyFolderType type = modest_tny_folder_get_local_folder_type (folder);
-                               if (type == TNY_FOLDER_TYPE_OUTBOX) {
-                                       add = FALSE;
-                               }
-                       }
-                       
-                       if (add)
-                               tny_merge_folder_add_folder (merge_folder, folder);
-                               
-                       g_object_unref (folder);        
-               }
+       /* Add our extra folder only if it passes the query */
+       priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
                
-               tny_iterator_next (iter);
-       }
-       
-       g_object_unref (list_outbox_folders);
+       if (priv->outbox_folder && 
+           modest_tny_local_folders_account_query_passes (query, TNY_FOLDER (priv->outbox_folder)))
+               tny_list_prepend (list, G_OBJECT (priv->outbox_folder));
 }
 
-void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalFoldersAccount *self, 
-       GSList *accounts)
+static TnyFolder*
+create_folder (TnyFolderStore *self, 
+              const gchar *name, 
+              GError **err)
 {
-       modest_tny_local_folders_account_remove_all_extra_folders (self);
-       
-       /* All per-account outbox folders are merged into one folders
-        * so that they appear as one outbox to the user: */
-       TnyMergeFolder *merged_outbox = TNY_MERGE_FOLDER (tny_merge_folder_new(_("mcen_me_folder_outbox")));
-       
-       /* Set type to outbox (NB#61580) */
-       tny_merge_folder_set_folder_type (merged_outbox, TNY_FOLDER_TYPE_OUTBOX);
-       
-       GSList *iter = accounts;
-       while (iter)
-       {
-               TnyAccount *account = TNY_ACCOUNT (iter->data);
-               if (account) {
-                       /* Add both outbox account and local-folders account folders
-                        * to our one combined account:
-                        */
-                       if (MODEST_IS_TNY_OUTBOX_ACCOUNT (account)) {
-                               /* Add the folder to the merged folder.
-                                * We will add it later to the virtual local-folders store: */
-                               add_account_folders_to_merged_folder (account, merged_outbox);
-                       }
-               }
-          
-               iter = g_slist_next (iter);
+       TnyCamelStoreAccountClass *parent_class;
+
+       parent_class = g_type_class_peek_parent (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
+
+       /* If the folder name is been used by our extra folders */
+       if (modest_tny_local_folders_account_folder_name_in_use (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (self), name)) {
+               g_set_error (err, TNY_SERVICE_ERROR, 
+                            TNY_SERVICE_ERROR_FOLDER_CREATE,
+                            "Folder name already in use");
+               return NULL;
        }
-       
-       /* Add the merged outbox folder to the virtual local-folders store: */
-       /* printf ("Debug: %s: adding merged outbox.\n", __FUNCTION__); */
-       modest_tny_local_folders_account_add_extra_folder (self, TNY_FOLDER(merged_outbox));
-       g_object_unref (merged_outbox);
-       merged_outbox = NULL;
+
+       /* Call the base class implementation: */
+       return parent_class->create_folder_func (self, name, err);
 }
 
+/*****************************/
+/*      Public methods       */ 
+/*****************************/
 gboolean
-modest_tny_local_folders_account_extra_folder_exists (ModestTnyLocalFoldersAccount *self,
-                                                     const gchar *name)
+modest_tny_local_folders_account_folder_name_in_use (ModestTnyLocalFoldersAccount *self,
+                                                    const gchar *name)
 {
        ModestTnyLocalFoldersAccountPrivate *priv;
-       GSList *iter;
-       gboolean found;
        gchar *down_name;
+       const gchar *type_name;
+       gboolean retval;
        
        /* Check that we're not trying to create/rename any folder
-          with the same name that our extra folders */
+          with the same name that our OUTBOX, DRAFT, SENT */
        priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
-       iter = priv->list_extra_folders;
-       found = FALSE;
        down_name = g_utf8_strdown (name, strlen (name));
-       while (iter && !found) {
-               TnyFolder *folder = TNY_FOLDER (iter->data);
-               const gchar *type_name;
-
-               type_name = modest_local_folder_info_get_type_name (tny_folder_get_folder_type (folder));
-               if (!strcmp (type_name, down_name))
-                       found = TRUE;
-               else
-                       iter = g_slist_next (iter);
+
+       type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_OUTBOX);
+       if (!strcmp (type_name, down_name)) {
+               retval = TRUE;
+       } else {
+               type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_DRAFTS);
+               if (!strcmp (type_name, down_name)) {
+                       retval = TRUE;
+               } else {
+                       type_name = modest_local_folder_info_get_type_name (TNY_FOLDER_TYPE_SENT);
+                       if (!strcmp (type_name, down_name)) {
+                               retval = TRUE;
+                       } else {
+                               retval = FALSE;
+                       }
+               }
        }
        g_free (down_name);
 
-       return found;
+       return retval;
 }
 
-static TnyFolder*
-create_folder (TnyFolderStore *self, 
-              const gchar *name, 
-              GError **err)
+void
+modest_tny_local_folders_account_add_folder_to_outbox (ModestTnyLocalFoldersAccount *self, 
+                                                      TnyFolder *per_account_outbox)
 {
-       TnyCamelStoreAccountClass *parent_class;
+       ModestTnyLocalFoldersAccountPrivate *priv;
 
-       parent_class = g_type_class_peek_parent (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
+       g_return_if_fail (MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self));
+       g_return_if_fail (TNY_IS_FOLDER (per_account_outbox));
 
-       /* If the folder name is been used by our extra folders */
-       if (modest_tny_local_folders_account_extra_folder_exists (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (self), name)) {
-               g_set_error (err, TNY_FOLDER_STORE_ERROR, 
-                            TNY_FOLDER_STORE_ERROR_CREATE_FOLDER,
-                            "Folder name already in use");
-               return NULL;
+       priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
+
+       /* Create on-demand */
+       if (!priv->outbox_folder) {
+               priv->outbox_folder = TNY_MERGE_FOLDER (tny_merge_folder_new_with_ui_locker (_("mcen_me_folder_outbox"), tny_gtk_lockable_new ()));
+       
+               /* Set type to outbox */
+               tny_merge_folder_set_folder_type (priv->outbox_folder, TNY_FOLDER_TYPE_OUTBOX);
        }
 
-       /* Call the base class implementation: */
-       return parent_class->create_folder_func (self, name, err);
+       /* Add outbox to the global OUTBOX folder */
+       tny_merge_folder_add_folder (priv->outbox_folder, per_account_outbox);
+}
+
+void 
+modest_tny_local_folders_account_remove_folder_from_outbox (ModestTnyLocalFoldersAccount *self, 
+                                                           TnyFolder *per_account_outbox)
+{
+       ModestTnyLocalFoldersAccountPrivate *priv;
+       TnyList *merged_folders = NULL;
+
+       g_return_if_fail (MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self));
+       g_return_if_fail (TNY_IS_FOLDER (per_account_outbox));
+
+       priv = TNY_LOCAL_FOLDERS_ACCOUNT_GET_PRIVATE (self);
+
+       /* Remove outbox from the global OUTBOX folder */
+       tny_merge_folder_remove_folder (priv->outbox_folder, per_account_outbox);
+
+       /* If there is no folder in the outbox the delete it */
+       merged_folders = tny_simple_list_new ();
+       tny_merge_folder_get_folders (priv->outbox_folder, merged_folders);
+       if (tny_list_get_length (merged_folders) == 0) {
+               g_object_unref (priv->outbox_folder);
+               priv->outbox_folder = NULL;
+       }
+       g_object_unref (merged_folders);
 }