* Added support to get_folders call in the local folder account to filter by query
authorSergio Villar Senin <svillar@igalia.com>
Thu, 12 Jul 2007 14:22:11 +0000 (14:22 +0000)
committerSergio Villar Senin <svillar@igalia.com>
Thu, 12 Jul 2007 14:22:11 +0000 (14:22 +0000)
* Removed get_folders_async from the local folder account because it makes no sense to redefine it to just call the parent
* Redefined the create_folder method in the local folder account in order not to allow to create folders with the same name than the extra folders
* Removed some invalid string comparisons in the guess_folder_type_from_name
* Now Modest do not allow to create a folder called outbox, nor to move another one with that name to the local folders account

pmo-trunk-r2728

src/modest-mail-operation.c
src/modest-tny-folder.c
src/modest-tny-local-folders-account.c
src/modest-tny-local-folders-account.h

index aa51380..db115d4 100644 (file)
@@ -51,6 +51,7 @@
 #include "modest-text-utils.h"
 #include "modest-tny-msg.h"
 #include "modest-tny-folder.h"
 #include "modest-text-utils.h"
 #include "modest-tny-msg.h"
 #include "modest-tny-folder.h"
+#include "modest-tny-account-store.h"
 #include "modest-tny-platform-factory.h"
 #include "modest-marshal.h"
 #include "modest-error.h"
 #include "modest-tny-platform-factory.h"
 #include "modest-marshal.h"
 #include "modest-error.h"
@@ -1660,6 +1661,47 @@ transfer_folder_cb (TnyFolder *folder,
        g_slice_free   (XFerMsgAsyncHelper, helper);
 }
 
        g_slice_free   (XFerMsgAsyncHelper, helper);
 }
 
+/**
+ *
+ * This function checks if the new name is a valid name for our local
+ * folders account. The new name could not be the same than then name
+ * of any of the mandatory local folders
+ *
+ * We can not rely on tinymail because tinymail does not check the
+ * name of the virtual folders that the account could have in the case
+ * that we're doing a rename (because it directly calls Camel which
+ * knows nothing about our virtual folders). 
+ *
+ * In the case of an actual copy/move (i.e. move/copy a folder between
+ * accounts) tinymail uses the tny_folder_store_create_account which
+ * is reimplemented by our ModestTnyLocalFoldersAccount that indeed
+ * checks the new name of the folder, so this call in that case
+ * wouldn't be needed. *But* NOTE that if tinymail changes its
+ * implementation (if folder transfers within the same account is no
+ * longer implemented as a rename) this call will allow Modest to work
+ * perfectly
+ *
+ * If the new name is not valid, this function will set the status to
+ * failed and will set also an error in the mail operation
+ */
+static gboolean
+new_name_valid_if_local_account (ModestMailOperationPrivate *priv,
+                                TnyFolderStore *into,
+                                const gchar *new_name)
+{
+       if (TNY_IS_ACCOUNT (into) && 
+           modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (into)) &&
+           modest_tny_local_folders_account_extra_folder_exists (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (into),
+                                                                 new_name)) {
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+               g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+                            MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
+                            _("FIXME: folder name already in use"));
+               return FALSE;
+       } else
+               return TRUE;
+}
+
 void
 modest_mail_operation_xfer_folder (ModestMailOperation *self,
                                   TnyFolder *folder,
 void
 modest_mail_operation_xfer_folder (ModestMailOperation *self,
                                   TnyFolder *folder,
@@ -1708,23 +1750,31 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self,
                /* Notify the queue */
                modest_mail_operation_notify_end (self);
        } else {
                /* Notify the queue */
                modest_mail_operation_notify_end (self);
        } else {
-               /* Create the helper */
-               helper = g_slice_new0 (XFerMsgAsyncHelper);
-               helper->mail_op = g_object_ref(self);
-               helper->dest_folder = NULL;
-               helper->headers = NULL;
-               helper->user_callback = user_callback;
-               helper->user_data = user_data;
-               
-               /* Move/Copy folder */          
-               tny_folder_copy_async (folder,
-                                      parent,
-                                      tny_folder_get_name (folder),
-                                      delete_original,
-                                      transfer_folder_cb,
-                                      transfer_folder_status_cb,
-                                      helper);
-/*                                    self); */
+
+
+               /* Check that the new folder name is not used by any
+                  special local folder */
+               if (new_name_valid_if_local_account (priv, parent, 
+                                                    tny_folder_get_name (folder))) {
+                       /* Create the helper */
+                       helper = g_slice_new0 (XFerMsgAsyncHelper);
+                       helper->mail_op = g_object_ref(self);
+                       helper->dest_folder = NULL;
+                       helper->headers = NULL;
+                       helper->user_callback = user_callback;
+                       helper->user_data = user_data;
+                       
+                       /* Move/Copy folder */          
+                       tny_folder_copy_async (folder,
+                                              parent,
+                                              tny_folder_get_name (folder),
+                                              delete_original,
+                                              transfer_folder_cb,
+                                              transfer_folder_status_cb,
+                                              helper);
+               } else {
+                       modest_mail_operation_notify_end (self);
+               }
        }
 }
 
        }
 }
 
@@ -1767,25 +1817,30 @@ modest_mail_operation_rename_folder (ModestMailOperation *self,
        } else {
                TnyFolderStore *into;
 
        } else {
                TnyFolderStore *into;
 
-               /* Create the helper */
-               helper = g_slice_new0 (XFerMsgAsyncHelper);
-               helper->mail_op = g_object_ref(self);
-               helper->dest_folder = NULL;
-               helper->headers = NULL;
-               helper->user_callback = NULL;
-               helper->user_data = NULL;
-
-               /* Rename. Camel handles folder subscription/unsubscription */
-               into = tny_folder_get_folder_store (folder);
-               tny_folder_copy_async (folder, into, name, TRUE,
-                                transfer_folder_cb,
-                                transfer_folder_status_cb,
-                                helper);
-/*                              self); */
-               if (into)
-                       g_object_unref (into);          
-       }
- }
+               into = tny_folder_get_folder_store (folder);    
+
+               /* Check that the new folder name is not used by any
+                  special local folder */
+               if (new_name_valid_if_local_account (priv, into, name)) {
+                       /* Create the helper */
+                       helper = g_slice_new0 (XFerMsgAsyncHelper);
+                       helper->mail_op = g_object_ref(self);
+                       helper->dest_folder = NULL;
+                       helper->headers = NULL;
+                       helper->user_callback = NULL;
+                       helper->user_data = NULL;
+               
+                       /* Rename. Camel handles folder subscription/unsubscription */
+                       tny_folder_copy_async (folder, into, name, TRUE,
+                                              transfer_folder_cb,
+                                              transfer_folder_status_cb,
+                                              helper);
+               } else {
+                       modest_mail_operation_notify_end (self);
+               }
+               g_object_unref (into);
+       }
+}
 
 /* ******************************************************************* */
 /* **************************  MSG  ACTIONS  ************************* */
 
 /* ******************************************************************* */
 /* **************************  MSG  ACTIONS  ************************* */
index 9a8b57d..588c1f0 100644 (file)
@@ -54,9 +54,7 @@ modest_tny_folder_guess_folder_type_from_name (const gchar* name)
 //         strcmp (folder, _("inbox")) == 0 ||
 //         strcmp (folder, _("mcen_me_folder_inbox")) == 0)
 //             type = TNY_FOLDER_TYPE_INBOX;
 //         strcmp (folder, _("inbox")) == 0 ||
 //         strcmp (folder, _("mcen_me_folder_inbox")) == 0)
 //             type = TNY_FOLDER_TYPE_INBOX;
-       if (strcmp (folder, "outbox") == 0 ||
-                strcmp (folder, _("outbox")) == 0 ||
-                strcmp (folder, _("mcen_me_folder_outbox")) == 0)
+       if (strcmp (folder, _("mcen_me_folder_outbox")) == 0)
                type = TNY_FOLDER_TYPE_OUTBOX;
 //     else if (g_str_has_prefix(folder, "junk") ||
 //              g_str_has_prefix(folder, _("junk")))
                type = TNY_FOLDER_TYPE_OUTBOX;
 //     else if (g_str_has_prefix(folder, "junk") ||
 //              g_str_has_prefix(folder, _("junk")))
@@ -64,13 +62,9 @@ modest_tny_folder_guess_folder_type_from_name (const gchar* name)
 //     else if (g_str_has_prefix(folder, "trash") ||
 //              g_str_has_prefix(folder, _("trash")))
 //             type = TNY_FOLDER_TYPE_TRASH;
 //     else if (g_str_has_prefix(folder, "trash") ||
 //              g_str_has_prefix(folder, _("trash")))
 //             type = TNY_FOLDER_TYPE_TRASH;
-       else if (g_str_has_prefix(folder, "sent") ||
-                g_str_has_prefix(folder, _("sent")) ||
-                strcmp (folder, _("mcen_me_folder_sent")) == 0)
+       else if (strcmp (folder, _("mcen_me_folder_sent")) == 0)
                type = TNY_FOLDER_TYPE_SENT;
                type = TNY_FOLDER_TYPE_SENT;
-       else if (g_str_has_prefix(folder, "draft") ||
-                g_str_has_prefix(folder, _("draft")) ||
-                strcmp (folder, _("mcen_me_folder_drafts")) == 0)
+       else if (strcmp (folder, _("mcen_me_folder_drafts")) == 0)
                type = TNY_FOLDER_TYPE_DRAFTS;
 //     else if (g_str_has_prefix(folder, "notes") ||
 //              g_str_has_prefix(folder, _("notes")))
                type = TNY_FOLDER_TYPE_DRAFTS;
 //     else if (g_str_has_prefix(folder, "notes") ||
 //              g_str_has_prefix(folder, _("notes")))
index 2f176f7..5ec6b52 100644 (file)
  */
 
 
  */
 
 
+#include <string.h>
+#include <stdio.h>
 #include <config.h>
 #include <glib/gi18n.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 <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 <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);
 G_DEFINE_TYPE (ModestTnyLocalFoldersAccount, 
        modest_tny_local_folders_account, 
        TNY_TYPE_CAMEL_STORE_ACCOUNT);
@@ -54,6 +54,15 @@ struct _ModestTnyLocalFoldersAccountPrivate
        GSList *list_extra_folders;
 };
 
        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)
 {
 static void
 modest_tny_local_folders_account_dispose (GObject *object)
 {
@@ -96,12 +105,6 @@ modest_tny_local_folders_account_finalize (GObject *object)
 }
 
 static void
 }
 
 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);
 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;
          
        /* 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
 }
 
 static void
@@ -214,15 +217,6 @@ get_folders (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query, GE
        }
 }
 
        }
 }
 
-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)
 {
 static void
 add_account_folders_to_merged_folder (TnyAccount *account, TnyMergeFolder* merge_folder)
 {
@@ -300,4 +294,53 @@ void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalF
        merged_outbox = NULL;
 }
 
        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);
+}
index 44a08f1..49b90de 100644 (file)
@@ -72,11 +72,14 @@ GType modest_tny_local_folders_account_get_type (void);
 
 ModestTnyLocalFoldersAccount* modest_tny_local_folders_account_new (void);
 
 
 ModestTnyLocalFoldersAccount* modest_tny_local_folders_account_new (void);
 
-void modest_tny_local_folders_account_add_extra_folder (ModestTnyLocalFoldersAccount *store, 
-       TnyFolder *folder);
+void      modest_tny_local_folders_account_add_extra_folder           (ModestTnyLocalFoldersAccount *store, 
+                                                                      TnyFolder *folder);
        
        
-void modest_tny_local_folders_account_add_merged_outbox_folders (ModestTnyLocalFoldersAccount *store, 
-       GSList *accounts);
+void      modest_tny_local_folders_account_add_merged_outbox_folders  (ModestTnyLocalFoldersAccount *store, 
+                                                                      GSList *accounts);
+
+gboolean  modest_tny_local_folders_account_extra_folder_exists        (ModestTnyLocalFoldersAccount *self,
+                                                                      const gchar *name);
 
 G_END_DECLS
 
 
 G_END_DECLS