Now GetUnreadMessages dbus method tells also the number of unread messages
[modest] / tests / check_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 #include <string.h>
32
33 #include <tny-list.h>
34 #include <tny-iterator.h>
35 #include <tny-simple-list.h>
36 #include <tny-account-store.h>
37 #include <tny-store-account.h>
38 #include <tny-folder.h>
39 #include <tny-folder-store.h>
40 #include <modest-tny-platform-factory.h>
41 #include <modest-runtime.h>
42
43 #include <modest-account-mgr.h>
44 #include <modest-mail-operation.h>
45 #include <modest-init.h>
46
47 static gchar *cachedir=NULL;
48 static gboolean move=FALSE;
49 static const gchar *src_name = NULL;
50 static const gchar *dst_name = NULL;
51
52 static void
53 find_folders (TnyFolderStore *store, TnyFolderStoreQuery *query,
54               TnyFolder **folder_src, TnyFolder **folder_dst)
55 {
56         TnyIterator *iter;
57         TnyList *folders;
58
59         if ((*folder_src != NULL) && (*folder_dst != NULL))
60                 return;
61
62         folders = tny_simple_list_new ();
63         tny_folder_store_get_folders (store, folders, query, FALSE /*no poke*/, NULL);
64         iter = tny_list_create_iterator (folders);
65
66         while (!tny_iterator_is_done (iter) && (!*folder_src || !*folder_dst))
67         {
68                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
69                 if (folder) {
70                         const gchar *folder_name = NULL;
71
72                         folder_name = tny_folder_get_name (TNY_FOLDER (folder));
73
74                         if (strcmp (folder_name, src_name) == 0)
75                             *folder_src = g_object_ref (folder);
76                 
77                         if (!strcmp (folder_name, dst_name))
78                             *folder_dst = g_object_ref (folder);
79
80                         find_folders (folder, query, folder_src, folder_dst);
81             
82                         g_object_unref (G_OBJECT (folder));
83                 }
84
85                 tny_iterator_next (iter);           
86         }
87
88          g_object_unref (G_OBJECT (iter));
89          g_object_unref (G_OBJECT (folders));
90 }
91
92 static const GOptionEntry options[] = {
93                 { "from",  'f', 0, G_OPTION_ARG_STRING, &src_name,
94                   "Source folder", NULL},
95                 { "to", 't', 0, G_OPTION_ARG_STRING, &dst_name,
96                   "Destination folder", NULL},
97                 { "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
98                   "Cache directory", NULL },
99                 { "move", 'm', 0, G_OPTION_ARG_NONE, &move,
100                   "Move the messages instead of copy them", NULL },
101                 { NULL }
102 };
103
104 int 
105 main (int argc, char **argv)
106 {
107         GOptionContext *context;
108         TnyList *accounts, *src_headers;
109         TnyStoreAccount *account;
110         TnyIterator *iter;
111         TnyFolder *folder_src = NULL, *folder_dst = NULL;
112         ModestAccountMgr *acc_mgr = NULL;
113         ModestMailOperation *mail_op = NULL;
114         ModestTnyAccountStore *account_store = NULL;
115         guint src_num_headers = 0, dst_num_headers = 0;
116         GError *err;
117     
118         g_type_init ();
119         g_thread_init (NULL);
120         
121         context = g_option_context_new ("Test");
122         g_option_context_add_main_entries (context, options, "Modest");
123         if (!g_option_context_parse (context, &argc, &argv, &err)) {
124                 g_printerr ("Error in command line parameter(s): '%s', exiting\n",
125                             err ? err->message : "");
126                 return 0;
127         }
128         g_option_context_free (context);
129
130         if (!modest_init (argc, argv)) {
131                 return 0;
132         }
133
134         acc_mgr = modest_runtime_get_account_mgr ();
135         account_store = modest_runtime_get_account_store();
136
137         if (cachedir)
138                 g_print ("Using %s as cache directory\n", cachedir);
139
140         if (!src_name || !dst_name) {
141                 g_printerr ("Error in command line parameter(s), specify source and target folders\n"); 
142                 return 0;
143         }
144
145         /* Get accounts */
146         accounts = tny_simple_list_new ();
147
148         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(account_store),
149                                         accounts,
150                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
151     
152         iter = tny_list_create_iterator (accounts);
153         account = (TnyStoreAccount*) tny_iterator_get_current (iter);
154
155         g_object_unref (G_OBJECT (iter));
156         g_object_unref (G_OBJECT (accounts));
157
158         /* Find the two folders */
159         find_folders (TNY_FOLDER_STORE (account), NULL,
160                       &folder_src, &folder_dst);
161
162         if (!folder_src || !folder_dst)
163                 goto cleanup;
164
165         /* Refresh folders */
166         tny_folder_refresh (folder_src, NULL);
167         src_num_headers = tny_folder_get_all_count (folder_src);
168
169         tny_folder_refresh (folder_dst, NULL);
170         dst_num_headers = tny_folder_get_all_count (folder_dst);
171
172         /* Get all the headers of the source & target folder */
173         src_headers = tny_simple_list_new ();
174         tny_folder_get_headers (folder_src, src_headers, TRUE, NULL);
175
176         mail_op = modest_mail_operation_new (NULL);
177                 
178         modest_mail_operation_xfer_folder (mail_op, 
179                                            folder_src, 
180                                            TNY_FOLDER_STORE (folder_dst), move,
181                                            NULL, NULL);
182         
183         g_object_unref (G_OBJECT (src_headers));
184         g_object_unref (G_OBJECT (mail_op));
185
186  cleanup:
187         g_object_unref (account);
188     
189         return 0;
190 }