Re: Move and copy messages
- From: Sergio Villar Senin <svillar igalia com>
- To: tinymail-devel-list gnome org
- Subject: Re: Move and copy messages
- Date: Fri, 17 Nov 2006 11:55:32 +0100
Philip Van Hoof wrote:
> On Wed, 2006-11-15 at 18:36 +0100, Sergio Villar Senin wrote:
>> What do you think about that? Will a patch with this be interesting?
>
> Looking forward to your patch
Hi all,
find attached the patch to allow tinymail to transfer messages between
folders. With transfer I mean both copy and move. I've also included a
new functional test to check if it works fine or not. The test transfers
all the messages of the source folder to the destination one. A typical
call would be:
msg-transfer -f 10 -t children -o -m
begin:
10: name of the source folder (I'm using tinymail's test account)
children: name of the destination folder
-o: do it online
-m: move (default is copy)
I'd like people to review it. Comments? Opinions?
Br
Index: libtinymail-camel/tny-camel-folder.c
===================================================================
--- libtinymail-camel/tny-camel-folder.c (revision 1178)
+++ libtinymail-camel/tny-camel-folder.c (working copy)
@@ -946,6 +946,91 @@
return priv->folder_name;
}
+static void
+tny_camel_folder_transfer_msgs (TnyFolder *folder_src,
+ TnyList *headers,
+ TnyFolder *folder_dst,
+ gboolean delete_originals)
+{
+ TNY_CAMEL_FOLDER_GET_CLASS (folder_src)->transfer_msgs_func (folder_src, headers, folder_dst, delete_originals);
+}
+
+static void
+tny_camel_folder_transfer_msgs_default (TnyFolder *folder_src,
+ TnyList *headers,
+ TnyFolder *folder_dst,
+ gboolean delete_originals)
+{
+ TnyCamelFolderPriv *priv_src, *priv_dst;
+ TnyIterator *iter;
+ CamelException *ex;
+ CamelFolder *cfol_src, *cfol_dst;
+ GPtrArray *uids, *transferred_uids = NULL;
+ guint list_length;
+
+ g_assert (TNY_IS_LIST (headers));
+ g_assert (TNY_IS_FOLDER (folder_src));
+ g_assert (TNY_IS_FOLDER (folder_dst));
+
+ list_length = tny_list_get_length (headers);
+ if (list_length < 1) return;
+
+
+ /* Get privates */
+ priv_src = TNY_CAMEL_FOLDER_GET_PRIVATE (folder_src);
+ priv_dst = TNY_CAMEL_FOLDER_GET_PRIVATE (folder_dst);
+
+ g_mutex_lock (priv_src->folder_lock);
+ g_mutex_lock (priv_dst->folder_lock);
+
+ /* Get camel folders */
+ cfol_src = _tny_camel_folder_get_camel_folder (TNY_CAMEL_FOLDER (folder_src));
+ cfol_dst = _tny_camel_folder_get_camel_folder (TNY_CAMEL_FOLDER (folder_dst));
+
+ /* Create uids */
+ uids = g_ptr_array_sized_new (list_length);
+ iter = tny_list_create_iterator (headers);
+ while (!tny_iterator_is_done (iter)) {
+ TnyHeader *header;
+
+ header = TNY_HEADER (tny_iterator_get_current (iter));
+ g_ptr_array_add (uids, (gpointer) tny_header_get_uid (header));
+ tny_iterator_next (iter);
+ }
+
+ /* Initialize exception */
+ ex = camel_exception_new ();
+ camel_exception_init (ex);
+
+ camel_folder_transfer_messages_to (cfol_src,
+ uids,
+ cfol_dst,
+ &transferred_uids,
+ delete_originals,
+ ex);
+
+ if (camel_exception_is_set (ex)) {
+ g_warning ("Transfering messages failed: %s\n",
+ camel_exception_get_description (ex));
+ } else {
+ if (delete_originals)
+ camel_folder_sync (cfol_src, TRUE, ex);
+
+ if (camel_exception_is_set (ex))
+ g_warning ("Expunging messages failed: %s\n",
+ camel_exception_get_description (ex));
+ }
+ camel_exception_free (ex);
+
+ /* Frees */
+ if (transferred_uids) g_ptr_array_free (transferred_uids, FALSE);
+ g_ptr_array_free (uids, FALSE);
+
+ g_mutex_unlock (priv_dst->folder_lock);
+ g_mutex_unlock (priv_src->folder_lock);
+}
+
+
void
_tny_camel_folder_set_id (TnyCamelFolder *self, const gchar *id)
{
@@ -1493,6 +1578,7 @@
klass->remove_msg_func = tny_camel_folder_remove_msg;
klass->expunge_func = tny_camel_folder_expunge;
klass->add_msg_func = tny_camel_folder_add_msg;
+ klass->transfer_msgs_func = tny_camel_folder_transfer_msgs;
return;
}
@@ -1536,6 +1622,7 @@
class->remove_msg_func = tny_camel_folder_remove_msg_default;
class->add_msg_func = tny_camel_folder_add_msg_default;
class->expunge_func = tny_camel_folder_expunge_default;
+ class->transfer_msgs_func = tny_camel_folder_transfer_msgs_default;
class->get_folders_async_func = tny_camel_folder_get_folders_async_default;
class->get_folders_func = tny_camel_folder_get_folders_default;
Index: libtinymail-camel/tny-camel-folder.h
===================================================================
--- libtinymail-camel/tny-camel-folder.h (revision 1178)
+++ libtinymail-camel/tny-camel-folder.h (working copy)
@@ -67,6 +67,7 @@
gboolean (*is_subscribed_func) (TnyFolder *self);
void (*refresh_async_func) (TnyFolder *self, TnyRefreshFolderCallback callback, TnyRefreshFolderStatusCallback status_callback, gpointer user_data);
void (*refresh_func) (TnyFolder *self);
+ void (*transfer_msgs_func) (TnyFolder *folder_src, TnyList *headers, TnyFolder *folder_dst, gboolean delete_originals);
void (*get_folders_async_func) (TnyFolderStore *self, TnyList *list, TnyGetFoldersCallback callback, TnyFolderStoreQuery *query, gpointer user_data);
void (*get_folders_func) (TnyFolderStore *self, TnyList *list, TnyFolderStoreQuery *query);
Index: tests/functional/Makefile.am
===================================================================
--- tests/functional/Makefile.am (revision 1178)
+++ tests/functional/Makefile.am (working copy)
@@ -15,7 +15,7 @@
INCLUDES += -DMOZEMBED
endif
-bin_PROGRAMS = folder-lister folder-lister-async
+bin_PROGRAMS = folder-lister folder-lister-async msg-transfer
folder_lister_SOURCES = folder-lister.c
folder_lister_LDADD = \
@@ -35,3 +35,11 @@
$(top_builddir)/libtinymail-camel/libtinymail-camel-$(API_VERSION).la \
$(top_builddir)/tests/shared/libtestsshared.a
+msg_transfer_SOURCES = msg-transfer.c
+msg_transfer_LDADD = \
+ $(TINYMAIL_LIBS) $(LIBTINYMAIL_GNOME_DESKTOP_LIBS) \
+ $(top_builddir)/libtinymail/libtinymail-$(API_VERSION).la \
+ $(top_builddir)/libtinymailui/libtinymailui-$(API_VERSION).la \
+ $(top_builddir)/libtinymailui-gtk/libtinymailui-gtk-$(API_VERSION).la \
+ $(top_builddir)/libtinymail-camel/libtinymail-camel-$(API_VERSION).la \
+ $(top_builddir)/tests/shared/libtestsshared.a
\ No newline at end of file
Index: tests/functional/msg-transfer.c
===================================================================
--- tests/functional/msg-transfer.c (revision 0)
+++ tests/functional/msg-transfer.c (revision 0)
@@ -0,0 +1,180 @@
+/* tinymail - Tiny Mail
+ * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof gnome org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with self program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <glib.h>
+
+#include <tny-list.h>
+#include <tny-iterator.h>
+#include <tny-simple-list.h>
+#include <tny-account-store.h>
+#include <tny-store-account.h>
+#include <tny-folder.h>
+#include <tny-folder-store.h>
+#include <tny-folder-store-query.h>
+
+#include <account-store.h>
+
+static gchar *cachedir=NULL;
+static gboolean online=FALSE;
+static gboolean move=FALSE;
+static const gchar *src_name = NULL;
+static const gchar *dst_name = NULL;
+
+static void
+find_folders (TnyFolderStore *store, TnyFolderStoreQuery *query,
+ TnyFolder **folder_src, TnyFolder **folder_dst)
+{
+ TnyIterator *iter;
+ TnyList *folders;
+
+ if ((*folder_src != NULL) && (*folder_dst != NULL))
+ return;
+
+ folders = tny_simple_list_new ();
+ tny_folder_store_get_folders (store, folders, query);
+ iter = tny_list_create_iterator (folders);
+
+ while (!tny_iterator_is_done (iter) && (!*folder_src || !*folder_dst))
+ {
+ TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
+ gint i=0;
+ const gchar *folder_name = NULL;
+
+ folder_name = tny_folder_get_name (TNY_FOLDER (folder));
+
+ if (!strcmp (folder_name, src_name))
+ *folder_src = g_object_ref (folder);
+
+ if (!strcmp (folder_name, dst_name))
+ *folder_dst = g_object_ref (folder);
+
+ find_folders (folder, query, folder_src, folder_dst);
+
+ g_object_unref (G_OBJECT (folder));
+
+ tny_iterator_next (iter);
+ }
+
+ g_object_unref (G_OBJECT (iter));
+ g_object_unref (G_OBJECT (folders));
+}
+
+static const GOptionEntry options[] = {
+ { "from", 'f', 0, G_OPTION_ARG_STRING, &src_name,
+ "Source folder", NULL},
+ { "to", 't', 0, G_OPTION_ARG_STRING, &dst_name,
+ "Destination folder", NULL},
+ { "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
+ "Cache directory", NULL },
+ { "online", 'o', 0, G_OPTION_ARG_NONE, &online,
+ "Online or offline", NULL },
+ { "move", 'm', 0, G_OPTION_ARG_NONE, &move,
+ "Move the messages instead of copy them", NULL },
+ { NULL }
+};
+
+int
+main (int argc, char **argv)
+{
+ GOptionContext *context;
+ TnyAccountStore *account_store;
+ TnyList *accounts, *src_headers, *dst_headers;
+ TnyFolderStoreQuery *query;
+ TnyStoreAccount *account;
+ TnyIterator *iter;
+ TnyFolder *folder_src = NULL, *folder_dst = NULL;
+ guint src_num_headers = 0, dst_num_headers = 0;
+ GError *err;
+
+ g_type_init ();
+
+ context = g_option_context_new ("- The tinymail functional tester");
+ g_option_context_add_main_entries (context, options, "tinymail");
+ if (!g_option_context_parse (context, &argc, &argv, &err)) {
+ g_printerr ("Error in command line parameter(s): '%s', exiting\n",
+ err ? err->message : "");
+ return 1;
+ }
+ g_option_context_free (context);
+
+ account_store = tny_test_account_store_new (online, cachedir);
+
+ if (cachedir)
+ g_print ("Using %s as cache directory\n", cachedir);
+
+ if (!src_name || !dst_name) {
+ g_printerr ("Error in command line parameter(s), specify source and target folders\n");
+ return 1;
+ }
+
+ /* Get accounts */
+ accounts = tny_simple_list_new ();
+
+ tny_account_store_get_accounts (account_store, accounts,
+ TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
+
+ iter = tny_list_create_iterator (accounts);
+ account = (TnyStoreAccount*) tny_iterator_get_current (iter);
+
+ g_object_unref (G_OBJECT (iter));
+ g_object_unref (G_OBJECT (accounts));
+
+ query = tny_folder_store_query_new ();
+ tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
+
+ /* Find the two folders */
+ find_folders (TNY_FOLDER_STORE (account), query,
+ &folder_src, &folder_dst);
+
+ if (!folder_src || !folder_dst)
+ goto cleanup;
+
+ /* Refresh folders */
+ tny_folder_refresh (folder_src);
+ src_num_headers = tny_folder_get_all_count (folder_src);
+
+ tny_folder_refresh (folder_dst);
+ dst_num_headers = tny_folder_get_all_count (folder_dst);
+
+ /* Get all the headers of the source & target folder */
+ src_headers = tny_simple_list_new ();
+ tny_folder_get_headers (folder_src, src_headers, TRUE);
+
+ g_printf ("%s %d messages from %s to %s\n",
+ move ? "Moving" : "Copying",
+ src_num_headers,
+ tny_folder_get_name (folder_src),
+ tny_folder_get_name (folder_dst));
+
+ /* Copy/move messages */
+ tny_folder_transfer_msgs (folder_src, src_headers, folder_dst, move);
+
+ /* Check that all the messages have been transferred */
+ tny_folder_refresh (folder_dst);
+ g_printf ("Transferred %d of %d messages\n",
+ tny_folder_get_all_count (folder_dst) - dst_num_headers,
+ src_num_headers);
+
+ g_object_unref (G_OBJECT (src_headers));
+
+ cleanup:
+ g_object_unref (account);
+ g_object_unref (query);
+
+ return 0;
+}
Index: libtinymail/tny-folder.c
===================================================================
--- libtinymail/tny-folder.c (revision 1178)
+++ libtinymail/tny-folder.c (working copy)
@@ -346,8 +346,31 @@
return TNY_FOLDER_GET_IFACE (self)->get_account_func (self);
}
+/**
+ * tny_folder_transfer_msgs:
+ * @folder_src: the TnyFolder where the headers are stored
+ * @header_list: a list of TnyHeader objects
+ * @folder_dst: the TnyFolder where the msgs will be transfered
+ * @delete_originals: if TRUE then move msgs, else copy them
+ *
+ * Transfers messages from a folder to another. They could be moved or
+ * just copied depending on the value of the delete_originals argument
+ **/
+void
+tny_folder_transfer_msgs (TnyFolder *folder_src,
+ TnyList *headers,
+ TnyFolder *folder_dst,
+ gboolean delete_originals)
+{
+#ifdef DEBUG
+ if (!TNY_FOLDER_GET_IFACE (folder_src)->transfer_msgs_func)
+ g_critical ("You must implement tny_folder_transfer_msgs\n");
+#endif
+ return TNY_FOLDER_GET_IFACE (folder_src)->transfer_msgs_func (folder_src, headers, folder_dst, delete_originals);
+}
+
/**
* tny_folder_get_msg:
* @self: a TnyFolder object
@@ -488,8 +511,6 @@
return;
}
-
-
/**
* tny_folder_get_folder_type:
* @self: a TnyFolder object
Index: libtinymail/tny-folder.h
===================================================================
--- libtinymail/tny-folder.h (revision 1178)
+++ libtinymail/tny-folder.h (working copy)
@@ -93,6 +93,7 @@
gboolean (*is_subscribed_func) (TnyFolder *self);
void (*refresh_async_func) (TnyFolder *self, TnyRefreshFolderCallback callback, TnyRefreshFolderStatusCallback status_callback, gpointer user_data);
void (*refresh_func) (TnyFolder *self);
+ void (*transfer_msgs_func) (TnyFolder *folder_src, TnyList *header_list, TnyFolder *folder_dst, gboolean delete_originals);
};
GType tny_folder_get_type (void);
@@ -115,6 +116,7 @@
gboolean tny_folder_is_subscribed (TnyFolder *self);
void tny_folder_refresh_async (TnyFolder *self, TnyRefreshFolderCallback callback, TnyRefreshFolderStatusCallback status_callback, gpointer user_data);
void tny_folder_refresh (TnyFolder *self);
+void tny_folder_transfer_msgs (TnyFolder *folder_src, TnyList *header_list, TnyFolder *folder_dst, gboolean delete_originals);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]