* A lot of changes in the documentation, now it's generated correctly
[modest] / tests / folder-transfer.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib.h>
31
32 #include <tny-list.h>
33 #include <tny-iterator.h>
34 #include <tny-simple-list.h>
35 #include <tny-account-store.h>
36 #include <tny-store-account.h>
37 #include <tny-folder.h>
38 #include <tny-folder-store.h>
39 #include <tny-platform-factory.h>
40 #include "modest-account-mgr.h"
41 #include "modest-mail-operation.h"
42
43 static gchar *cachedir=NULL;
44 static gboolean move=FALSE;
45 static const gchar *src_name = NULL;
46 static const gchar *dst_name = NULL;
47
48 static void
49 find_folders (TnyFolderStore *store, TnyFolderStoreQuery *query,
50               TnyFolder **folder_src, TnyFolder **folder_dst)
51 {
52         TnyIterator *iter;
53         TnyList *folders;
54
55         if ((*folder_src != NULL) && (*folder_dst != NULL))
56                 return;
57
58         folders = tny_simple_list_new ();
59         tny_folder_store_get_folders (store, folders, query);
60         iter = tny_list_create_iterator (folders);
61
62         while (!tny_iterator_is_done (iter) && (!*folder_src || !*folder_dst))
63         {
64                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
65                 gint i=0;
66                 const gchar *folder_name = NULL;
67
68                 folder_name = tny_folder_get_name (TNY_FOLDER (folder));
69
70                 if (!strcmp (folder_name, src_name))
71                     *folder_src = g_object_ref (folder);
72
73                 if (!strcmp (folder_name, dst_name))
74                     *folder_dst = g_object_ref (folder);
75
76                 find_folders (folder, query, folder_src, folder_dst);
77             
78                 g_object_unref (G_OBJECT (folder));
79
80                 tny_iterator_next (iter);           
81         }
82
83          g_object_unref (G_OBJECT (iter));
84          g_object_unref (G_OBJECT (folders));
85 }
86
87 static const GOptionEntry options[] = {
88                 { "from",  'f', 0, G_OPTION_ARG_STRING, &src_name,
89                   "Source folder", NULL},
90                 { "to", 't', 0, G_OPTION_ARG_STRING, &dst_name,
91                   "Destination folder", NULL},
92                 { "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
93                   "Cache directory", NULL },
94                 { "move", 'm', 0, G_OPTION_ARG_NONE, &move,
95                   "Move the messages instead of copy them", NULL },
96                 { NULL }
97 };
98
99 int 
100 main (int argc, char **argv)
101 {
102         GOptionContext *context;
103         TnyList *accounts, *src_headers, *dst_headers;
104         TnyStoreAccount *account;
105         TnyIterator *iter;
106         TnyFolder *folder_src = NULL, *folder_dst = NULL;
107         TnyPlatformFactory *fact = NULL;
108         ModestAccountMgr *acc_mgr = NULL;
109         ModestMailOperation *mail_op = NULL;
110         TnyAccountStore *account_store = NULL;
111         guint src_num_headers = 0, dst_num_headers = 0;
112         GError *err;
113     
114         g_type_init ();
115
116         context = g_option_context_new ("Test");
117         g_option_context_add_main_entries (context, options, "Modest");
118         if (!g_option_context_parse (context, &argc, &argv, &err)) {
119                 g_printerr ("Error in command line parameter(s): '%s', exiting\n",
120                             err ? err->message : "");
121                 return 1;
122         }
123         g_option_context_free (context);
124
125         fact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance ());
126         acc_mgr = MODEST_ACCOUNT_MGR (modest_tny_platform_factory_get_modest_account_mgr_instance (fact));
127         account_store = tny_platform_factory_new_account_store (fact);  
128
129         if (cachedir)
130                 g_print ("Using %s as cache directory\n", cachedir);
131
132         if (!src_name || !dst_name) {
133                 g_printerr ("Error in command line parameter(s), specify source and target folders\n"); 
134                 return 1;
135         }
136
137         /* Get accounts */
138         accounts = tny_simple_list_new ();
139
140         tny_account_store_get_accounts (account_store, accounts,
141               TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
142     
143         iter = tny_list_create_iterator (accounts);
144         account = (TnyStoreAccount*) tny_iterator_get_current (iter);
145
146         g_object_unref (G_OBJECT (iter));
147         g_object_unref (G_OBJECT (accounts));
148
149         /* Find the two folders */
150         find_folders (TNY_FOLDER_STORE (account), NULL,
151                       &folder_src, &folder_dst);
152
153         if (!folder_src || !folder_dst)
154                 goto cleanup;
155
156         /* Refresh folders */
157         tny_folder_refresh (folder_src);
158         src_num_headers = tny_folder_get_all_count (folder_src);
159
160         tny_folder_refresh (folder_dst);
161         dst_num_headers = tny_folder_get_all_count (folder_dst);
162
163         /* Get all the headers of the source & target folder */
164         src_headers = tny_simple_list_new ();
165         tny_folder_get_headers (folder_src, src_headers, TRUE);
166
167         mail_op = modest_mail_operation_new ();
168                 
169         if (move)
170                 modest_mail_operation_move_folder (mail_op, 
171                                                    folder_src, 
172                                                    TNY_FOLDER_STORE (folder_dst));
173         else
174                 modest_mail_operation_copy_folder (mail_op, 
175                                                    folder_src, 
176                                                    TNY_FOLDER_STORE (folder_dst));
177         
178         g_object_unref (G_OBJECT (src_headers));
179         g_object_unref (G_OBJECT (mail_op));
180
181  cleanup:
182         g_object_unref (account);
183     
184         return 0;
185 }