2007-05-10 Murray Cumming <murrayc@murrayc.com>
[modest] / src / modest-tny-account.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 <modest-tny-platform-factory.h>
31 #include <modest-tny-account.h>
32 #include <modest-tny-account-store.h>
33 #include <modest-runtime.h>
34 #include <tny-simple-list.h>
35 #include <modest-tny-folder.h>
36 #include <modest-account-mgr-helpers.h>
37 #include <tny-camel-transport-account.h>
38 #include <tny-camel-imap-store-account.h>
39 #include <tny-camel-pop-store-account.h>
40 #include <tny-folder-stats.h>
41
42 /* for now, ignore the account ===> the special folders are the same,
43  * local folders for all accounts
44  * this might change, ie, IMAP might have server-side sent-items
45  */
46 TnyFolder *
47 modest_tny_account_get_special_folder (TnyAccount *account,
48                                        TnyFolderType special_type)
49 {
50         TnyList *folders;
51         TnyIterator *iter;
52         TnyFolder *special_folder = NULL;
53         TnyAccount *local_account;
54         
55         g_return_val_if_fail (account, NULL);
56         g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
57                               NULL);
58         
59         local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
60                                                                         MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
61         if (!local_account) {
62                 g_printerr ("modest: cannot get local account\n");
63                 return NULL;
64         }
65
66         folders = TNY_LIST (tny_simple_list_new ());
67
68         /* no need to do this _async, as these are local folders */
69         tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
70                                       folders, NULL, NULL);
71         iter = tny_list_create_iterator (folders);
72
73         while (!tny_iterator_is_done (iter)) {
74                 TnyFolder *folder =
75                         TNY_FOLDER (tny_iterator_get_current (iter));
76                 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
77                         special_folder = folder;
78                         break;
79                 }
80                 g_object_unref (G_OBJECT(folder));
81                 tny_iterator_next (iter);
82         }
83         g_object_unref (G_OBJECT (folders));
84         g_object_unref (G_OBJECT (iter));
85         g_object_unref (G_OBJECT (local_account));
86
87         return special_folder;
88 }
89
90 /* Camel options: */
91 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
92 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
93 #define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
94 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
95 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub"
96 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all"
97
98 /**
99  * modest_tny_account_new_from_server_account:
100  * @account_mgr: a valid account mgr instance
101  * @account_name: the server account name for which to create a corresponding tny account
102  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
103  * 
104  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
105  * NOTE: this function does not set the camel session or the get/forget password functions
106  * 
107  * Returns: a new TnyAccount or NULL in case of error.
108  */
109 static TnyAccount*
110 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
111                                             ModestServerAccountData *account_data)
112 {
113         TnyAccount *tny_account;
114                 
115         g_return_val_if_fail (account_mgr, NULL);
116         g_return_val_if_fail (account_data, NULL);
117
118         /* sanity checks */
119         if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
120                 g_printerr ("modest: '%s' does not provide a protocol\n",
121                             account_data->account_name);
122                 return NULL;
123         }
124
125         switch (account_data->proto) {
126         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
127         case MODEST_PROTOCOL_TRANSPORT_SMTP:
128                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
129         case MODEST_PROTOCOL_STORE_POP:
130                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
131         case MODEST_PROTOCOL_STORE_IMAP:
132                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
133         case MODEST_PROTOCOL_STORE_MAILDIR:
134         case MODEST_PROTOCOL_STORE_MBOX:
135                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
136         default:
137                 g_return_val_if_reached (NULL);
138         }
139         if (!tny_account) {
140                 g_printerr ("modest: could not create tny account for '%s'\n",
141                             account_data->account_name);
142                 return NULL;
143         }
144         tny_account_set_id (tny_account, account_data->account_name);
145
146         /* Proto */
147         tny_account_set_proto (tny_account,
148                                modest_protocol_info_get_protocol_name(account_data->proto));
149         if (account_data->uri) 
150                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
151         else {
152                 const gchar* option_security = NULL;
153                 switch (account_data->security) {
154                 case MODEST_PROTOCOL_SECURITY_NONE:
155                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER;
156                         break;
157                 case MODEST_PROTOCOL_SECURITY_SSL:
158                 case MODEST_PROTOCOL_SECURITY_TLS:
159                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
160                         break;
161                 case MODEST_PROTOCOL_SECURITY_TLS_OP:
162                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
163                         break;
164                 default:
165                         break;
166                 }
167                 
168                 if(option_security) {
169                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
170                                                       option_security);
171                 }
172                 
173                 if (account_data->proto == MODEST_PROTOCOL_TYPE_STORE) {
174                         /* Connection options. Some options are only valid for IMAP
175                            accounts but it's OK for just now since POP is still not
176                            supported */
177                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
178                                                                       MODEST_ACCOUNT_OPTION_USE_LSUB);
179                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
180                                                                       MODEST_ACCOUNT_OPTION_CHECK_ALL);
181                 }
182                 
183                 if (account_data->username) 
184                         tny_account_set_user (tny_account, account_data->username);
185                 if (account_data->hostname)
186                         tny_account_set_hostname (tny_account, account_data->hostname);
187         }
188         return tny_account;
189 }
190
191
192 /* we need these dummy functions, or tinymail will complain */
193 static gchar*
194 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
195 {
196         return NULL;
197 }
198 static void
199 forget_pass_dummy (TnyAccount *account)
200 {
201         /* intentionally left blank */
202 }
203
204 TnyAccount*
205 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
206                                      TnyAccountType type,
207                                      TnySessionCamel *session,
208                                      TnyGetPassFunc get_pass_func,
209                                      TnyForgetPassFunc forget_pass_func) 
210 {
211         TnyAccount *tny_account = NULL;
212         ModestAccountData *account_data = NULL;
213         ModestServerAccountData *server_data = NULL;
214
215         g_return_val_if_fail (account_mgr, NULL);
216         g_return_val_if_fail (account_name, NULL);
217
218         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
219         if (!account_data) {
220                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
221                 return NULL;
222         }
223
224         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
225                 server_data = account_data->store_account;
226         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
227                 server_data = account_data->transport_account;
228         if (!server_data) {
229                 g_printerr ("modest: no %s account defined for '%s'\n",
230                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
231                             account_data->display_name);
232                 modest_account_mgr_free_account_data (account_mgr, account_data);
233                 return NULL;
234         }
235         
236         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
237         if (!tny_account) { 
238                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
239                             account_data->account_name, server_data->account_name);
240                 modest_account_mgr_free_account_data (account_mgr, account_data);
241                 return NULL;
242         }
243         
244         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
245         tny_account_set_forget_pass_func (tny_account,
246                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
247         tny_account_set_pass_func (tny_account,
248                                    get_pass_func ? get_pass_func: get_pass_dummy);
249
250         /* this name is what shows up in the folder view -- so for some POP/IMAP/... server
251          * account, we set its name to the acount of which it is part */
252         if (account_data->display_name)
253                 tny_account_set_name (tny_account, account_data->display_name); 
254
255         g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
256                                 (gpointer*) g_strdup (account_name), g_free);
257         
258         modest_account_mgr_free_account_data (account_mgr, account_data);
259         return tny_account;
260 }
261
262
263 TnyAccount*
264 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
265 {
266         TnyStoreAccount *tny_account;
267         CamelURL *url;
268         gchar *maildir, *url_string;
269
270         g_return_val_if_fail (account_mgr, NULL);
271         
272         tny_account = tny_camel_store_account_new ();
273         if (!tny_account) {
274                 g_printerr ("modest: cannot create account for local folders");
275                 return NULL;
276         }
277         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
278         
279         maildir = modest_local_folder_info_get_maildir_path ();
280         url = camel_url_new ("maildir:", NULL);
281         camel_url_set_path (url, maildir);
282         /* Needed by tinymail's DBC assertions */
283         camel_url_set_host (url, "localhost");
284         url_string = camel_url_to_string (url, 0);
285         
286         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
287
288         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
289         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); 
290         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
291         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
292         
293         g_object_set_data (G_OBJECT(tny_account), "modest_account",
294                            (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
295         
296         camel_url_free (url);
297         g_free (maildir);
298         g_free (url_string);
299
300         return TNY_ACCOUNT(tny_account);
301 }
302
303 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
304
305 typedef struct _RecurseFoldersHelper {
306         TnyStatsFunc function;
307         guint sum;
308         guint folders;
309 } RecurseFoldersHelper;
310
311 static void
312 recurse_folders (TnyFolderStore *store, 
313                  TnyFolderStoreQuery *query, 
314                  RecurseFoldersHelper *helper)
315 {
316         TnyIterator *iter;
317         TnyList *folders = tny_simple_list_new ();
318
319         tny_folder_store_get_folders (store, folders, query, NULL);
320         iter = tny_list_create_iterator (folders);
321
322         helper->folders += tny_list_get_length (folders);
323
324         while (!tny_iterator_is_done (iter))
325         {
326                 TnyFolderStats *stats;
327                 TnyFolder *folder;
328
329                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
330                 stats = tny_folder_get_stats (folder);
331
332                 if (helper->function)
333                         helper->sum += helper->function (stats);
334
335                 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
336             
337                 g_object_unref (folder);
338                 g_object_unref (stats);
339                 tny_iterator_next (iter);
340         }
341          g_object_unref (G_OBJECT (iter));
342          g_object_unref (G_OBJECT (folders));
343 }
344
345 gint 
346 modest_tny_account_get_folder_count (TnyAccount *self)
347 {
348         RecurseFoldersHelper *helper;
349         gint retval;
350
351         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
352
353         /* Create helper */
354         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
355         helper->function = NULL;
356         helper->sum = 0;
357         helper->folders = 0;
358
359         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
360
361         retval = helper->folders;
362
363         g_free (helper);
364
365         return retval;
366 }
367
368 gint
369 modest_tny_account_get_message_count (TnyAccount *self)
370 {
371         RecurseFoldersHelper *helper;
372         gint retval;
373
374         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
375
376         /* Create helper */
377         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
378         helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
379         helper->sum = 0;
380
381         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
382
383         retval = helper->sum;
384
385         g_free (helper);
386
387         return retval;
388 }
389
390 gint 
391 modest_tny_account_get_local_size (TnyAccount *self)
392 {
393         RecurseFoldersHelper *helper;
394         gint retval;
395
396         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
397
398         /* Create helper */
399         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
400         helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
401         helper->sum = 0;
402
403         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
404
405         retval = helper->sum;
406
407         g_free (helper);
408
409         return retval;
410 }