* src/widgets/modest-msg-view.c:
[modest] / src / modest-tny-local-folders-account.c
index aa3948e..2efb499 100644 (file)
  */
 
 
+#include <string.h>
+#include <stdio.h>
 #include <config.h>
 #include <glib/gi18n.h>
-
+#include <tny-error.h>
 #include <modest-tny-local-folders-account.h>
 #include <modest-tny-outbox-account.h>
 #include <modest-tny-folder.h>
+#include <tny-camel-folder.h>
 #include <tny-merge-folder.h>
 #include <tny-simple-list.h>
 
-#include <string.h>
-#include <stdio.h>
-
 G_DEFINE_TYPE (ModestTnyLocalFoldersAccount, 
        modest_tny_local_folders_account, 
        TNY_TYPE_CAMEL_STORE_ACCOUNT);
@@ -54,6 +54,15 @@ struct _ModestTnyLocalFoldersAccountPrivate
        GSList *list_extra_folders;
 };
 
+static void         get_folders    (TnyFolderStore *self, 
+                                   TnyList *list, 
+                                   TnyFolderStoreQuery *query, 
+                                   GError **err);
+
+static TnyFolder*   create_folder  (TnyFolderStore *self, 
+                                   const gchar *name, 
+                                   GError **err);
+
 static void
 modest_tny_local_folders_account_dispose (GObject *object)
 {
@@ -96,12 +105,6 @@ modest_tny_local_folders_account_finalize (GObject *object)
 }
 
 static void
-get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err);
-
-static void 
-get_folders_async (TnyFolderStore *self, TnyList *list, TnyGetFoldersCallback callback, TnyFolderStoreQuery *query, TnyStatusCallback status_callback, gpointer user_data);
-
-static void
 modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
@@ -113,7 +116,7 @@ modest_tny_local_folders_account_class_init (ModestTnyLocalFoldersAccountClass *
          
        /* Override virtual functions from the parent class: */
        TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->get_folders_func = get_folders;
-       TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->get_folders_async_func = get_folders_async;
+       TNY_CAMEL_STORE_ACCOUNT_CLASS(klass)->create_folder_func = create_folder;
 }
 
 static void
@@ -145,6 +148,55 @@ modest_tny_local_folders_account_add_extra_folder (ModestTnyLocalFoldersAccount
        g_object_ref (folder);
 }
 
+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))
+       {
+               TnyList *items = tny_folder_store_query_get_items (query);
+               TnyIterator *iterator;
+               iterator = tny_list_create_iterator (items);
+
+               while (!tny_iterator_is_done (iterator))
+               {
+                       TnyFolderStoreQueryItem *item = (TnyFolderStoreQueryItem*) tny_iterator_get_current (iterator);
+                       if (item) {
+                               TnyFolderStoreQueryOption options = tny_folder_store_query_item_get_options (item);
+                               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_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_ID)
+                                 if (regexec (regex, tny_folder_get_id (folder), 0, NULL, 0) == 0)
+                                       retval = TRUE;
+
+                               g_object_unref (G_OBJECT (item));
+                       }
+
+                       tny_iterator_next (iterator);
+               }
+                
+               g_object_unref (G_OBJECT (iterator));    
+               g_object_unref (G_OBJECT (items));
+       } else
+               retval = TRUE;
+
+       return retval;
+}
+
+
 static void
 get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GError **err)
 {
@@ -156,27 +208,18 @@ get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GE
                MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
        parent_class->get_folders_func (self, list, query, err);
        
-       /* Add our extra folders: */
+       /* Add our extra folders only if it passes the query */
        GSList *iter = priv->list_extra_folders;
-       while (iter)
-       {
+       while (iter) {
                TnyFolder *folder = TNY_FOLDER (iter->data);
-               if (folder)
-                       tny_list_append (list, G_OBJECT (folder));
+
+               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 
-get_folders_async (TnyFolderStore *self, TnyList *list, TnyGetFoldersCallback callback, TnyFolderStoreQuery *query, TnyStatusCallback status_callback, gpointer user_data)
-{
-       /* Call the base class implementation: */
-       TnyCamelStoreAccountClass *parent_class = g_type_class_peek_parent (
-               MODEST_TNY_LOCAL_FOLDERS_ACCOUNT_GET_CLASS (self));
-       parent_class->get_folders_async_func (self, list, callback, query, status_callback, user_data);
-}
-
 static void
 add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge_folder)
 {
@@ -199,7 +242,7 @@ add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge
                         * 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);
+                               const TnyFolderType type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
                                if (type == TNY_FOLDER_TYPE_OUTBOX) {
                                        add = FALSE;
                                }
@@ -224,7 +267,10 @@ void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalF
        
        /* 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());
+       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)
@@ -251,4 +297,53 @@ void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalF
        merged_outbox = NULL;
 }
 
+gboolean
+modest_tny_local_folders_account_extra_folder_exists (ModestTnyLocalFoldersAccount *self,
+                                                     const gchar *name)
+{
+       ModestTnyLocalFoldersAccountPrivate *priv;
+       GSList *iter;
+       gboolean found;
+       gchar *down_name;
+       
+       /* Check that we're not trying to create/rename any folder
+          with the same name that our extra folders */
+       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);
+       }
+       g_free (down_name);
+
+       return found;
+}
+
+static TnyFolder*
+create_folder (TnyFolderStore *self, 
+              const gchar *name, 
+              GError **err)
+{
+       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_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;
+       }
 
+       /* Call the base class implementation: */
+       return parent_class->create_folder_func (self, name, err);
+}