2007-05-11 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
92 /* These seem to be listed in 
93  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c 
94  */
95 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
96 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
97 #define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
98 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
99
100 /* These seem to be listed in 
101  * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c 
102  */
103 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
104 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
105
106 /* TODO: Find how to specify the authentication method with camel.
107  * This is just made up stuff - I have no idea if its even possible via options:
108 #define MODEST_ACCOUNT_OPTION_AUTH "auth"
109 #define MODEST_ACCOUNT_OPTION_AUTH_PLAIN "PLAIN"
110 #define MODEST_ACCOUNT_OPTION_AUTH_PASSWORD "PASSWORD"
111 #define MODEST_ACCOUNT_OPTION_AUTH_CRAMMD5 "CRAMMD5"
112 */
113
114 /**
115  * modest_tny_account_new_from_server_account:
116  * @account_mgr: a valid account mgr instance
117  * @account_name: the server account name for which to create a corresponding tny account
118  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
119  * 
120  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
121  * NOTE: this function does not set the camel session or the get/forget password functions
122  * 
123  * Returns: a new TnyAccount or NULL in case of error.
124  */
125 static TnyAccount*
126 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
127                                             ModestServerAccountData *account_data)
128 {
129         TnyAccount *tny_account;
130                 
131         g_return_val_if_fail (account_mgr, NULL);
132         g_return_val_if_fail (account_data, NULL);
133
134         /* sanity checks */
135         if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
136                 g_printerr ("modest: '%s' does not provide a protocol\n",
137                             account_data->account_name);
138                 return NULL;
139         }
140
141         switch (account_data->proto) {
142         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
143         case MODEST_PROTOCOL_TRANSPORT_SMTP:
144                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
145         case MODEST_PROTOCOL_STORE_POP:
146                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
147         case MODEST_PROTOCOL_STORE_IMAP:
148                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
149         case MODEST_PROTOCOL_STORE_MAILDIR:
150         case MODEST_PROTOCOL_STORE_MBOX:
151                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
152         default:
153                 g_return_val_if_reached (NULL);
154         }
155         if (!tny_account) {
156                 g_printerr ("modest: could not create tny account for '%s'\n",
157                             account_data->account_name);
158                 return NULL;
159         }
160         tny_account_set_id (tny_account, account_data->account_name);
161
162         /* Proto */
163         tny_account_set_proto (tny_account,
164                                modest_protocol_info_get_protocol_name(account_data->proto));
165                                
166         if (account_data->uri) 
167                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
168         else {
169                 /* Set camel-specific options: */
170                 
171                 /* To enable security settings: */
172                 const gchar* option_security = NULL;
173                 switch (account_data->security) {
174                 case MODEST_PROTOCOL_SECURITY_NONE:
175                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_NEVER;
176                         break;
177                 case MODEST_PROTOCOL_SECURITY_SSL:
178                 case MODEST_PROTOCOL_SECURITY_TLS:
179                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
180                         break;
181                 case MODEST_PROTOCOL_SECURITY_TLS_OP:
182                         option_security = MODEST_ACCOUNT_OPTION_SSL "= " MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
183                         break;
184                 default:
185                         break;
186                 }
187                 
188                 if(option_security) {
189                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
190                                                       option_security);
191                 }
192                 
193                 /* To enable secure-auth settings: */
194                 /* The UI spec says:
195                  *  # If secure authentication is unchecked, allow sending username and password also as plain text.
196                  *  # If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc
197                  */
198                 /* TODO: Find how to specify the authentication method with camel.
199                  * This is just made up stuff - I have no idea if its even possible via options:
200                 const gchar* option_secure_auth = NULL;
201                 switch (account_data->secure_auth) {
202                 case MODEST_PROTOCOL_AUTH_NONE:
203                         option_secure_auth = MODEST_ACCOUNT_OPTION_AUTH "= " MODEST_ACCOUNT_OPTION_AUTH_PLAIN;
204                         break;
205                 case MODEST_PROTOCOL_AUTH_PASSWORD:
206                         option_secure_auth = MODEST_ACCOUNT_OPTION_AUTH "= " MODEST_ACCOUNT_OPTION_AUTH_PASSWORD;
207                         break;
208                 case MODEST_PROTOCOL_AUTH_CRAMMD5:
209                         option_secure_auth = MODEST_ACCOUNT_OPTION_AUTH "= " MODEST_ACCOUNT_OPTION_AUTH_CRAMMD5;
210                 default:
211                         break;
212                 }
213                 
214                 if(option_secure_auth) {
215                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
216                                                       option_secure_auth);
217                 }
218                 */
219                 
220                 if (account_data->proto == MODEST_PROTOCOL_TYPE_STORE) {
221                         /* Other connection options. Some options are only valid for IMAP
222                            accounts but it's OK for just now since POP is still not
223                            supported */
224                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
225                                                                       MODEST_ACCOUNT_OPTION_USE_LSUB);
226                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
227                                                                       MODEST_ACCOUNT_OPTION_CHECK_ALL);
228                 }
229                 
230                 if (account_data->username) 
231                         tny_account_set_user (tny_account, account_data->username);
232                 if (account_data->hostname)
233                         tny_account_set_hostname (tny_account, account_data->hostname);
234                  
235                 /* Set the port: */
236                 if (account_data->port)
237                         tny_account_set_port (tny_account, account_data->port);
238         }
239         
240         
241         return tny_account;
242 }
243
244
245 /* we need these dummy functions, or tinymail will complain */
246 static gchar*
247 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
248 {
249         return NULL;
250 }
251 static void
252 forget_pass_dummy (TnyAccount *account)
253 {
254         /* intentionally left blank */
255 }
256
257 TnyAccount*
258 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
259                                      TnyAccountType type,
260                                      TnySessionCamel *session,
261                                      TnyGetPassFunc get_pass_func,
262                                      TnyForgetPassFunc forget_pass_func) 
263 {
264         TnyAccount *tny_account = NULL;
265         ModestAccountData *account_data = NULL;
266         ModestServerAccountData *server_data = NULL;
267
268         g_return_val_if_fail (account_mgr, NULL);
269         g_return_val_if_fail (account_name, NULL);
270
271         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
272         if (!account_data) {
273                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
274                 return NULL;
275         }
276
277         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
278                 server_data = account_data->store_account;
279         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
280                 server_data = account_data->transport_account;
281         if (!server_data) {
282                 g_printerr ("modest: no %s account defined for '%s'\n",
283                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
284                             account_data->display_name);
285                 modest_account_mgr_free_account_data (account_mgr, account_data);
286                 return NULL;
287         }
288         
289         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
290         if (!tny_account) { 
291                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
292                             account_data->account_name, server_data->account_name);
293                 modest_account_mgr_free_account_data (account_mgr, account_data);
294                 return NULL;
295         }
296         
297         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
298         tny_account_set_forget_pass_func (tny_account,
299                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
300         tny_account_set_pass_func (tny_account,
301                                    get_pass_func ? get_pass_func: get_pass_dummy);
302
303         /* this name is what shows up in the folder view -- so for some POP/IMAP/... server
304          * account, we set its name to the acount of which it is part */
305         if (account_data->display_name)
306                 tny_account_set_name (tny_account, account_data->display_name); 
307
308         g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
309                                 (gpointer*) g_strdup (account_name), g_free);
310         
311         modest_account_mgr_free_account_data (account_mgr, account_data);
312         return tny_account;
313 }
314
315
316 TnyAccount*
317 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
318 {
319         TnyStoreAccount *tny_account;
320         CamelURL *url;
321         gchar *maildir, *url_string;
322
323         g_return_val_if_fail (account_mgr, NULL);
324         
325         tny_account = tny_camel_store_account_new ();
326         if (!tny_account) {
327                 g_printerr ("modest: cannot create account for local folders");
328                 return NULL;
329         }
330         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
331         
332         maildir = modest_local_folder_info_get_maildir_path ();
333         url = camel_url_new ("maildir:", NULL);
334         camel_url_set_path (url, maildir);
335         /* Needed by tinymail's DBC assertions */
336         camel_url_set_host (url, "localhost");
337         url_string = camel_url_to_string (url, 0);
338         
339         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
340
341         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
342         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); 
343         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
344         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
345         
346         g_object_set_data (G_OBJECT(tny_account), "modest_account",
347                            (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
348         
349         camel_url_free (url);
350         g_free (maildir);
351         g_free (url_string);
352
353         return TNY_ACCOUNT(tny_account);
354 }
355
356 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
357
358 typedef struct _RecurseFoldersHelper {
359         TnyStatsFunc function;
360         guint sum;
361         guint folders;
362 } RecurseFoldersHelper;
363
364 static void
365 recurse_folders (TnyFolderStore *store, 
366                  TnyFolderStoreQuery *query, 
367                  RecurseFoldersHelper *helper)
368 {
369         TnyIterator *iter;
370         TnyList *folders = tny_simple_list_new ();
371
372         tny_folder_store_get_folders (store, folders, query, NULL);
373         iter = tny_list_create_iterator (folders);
374
375         helper->folders += tny_list_get_length (folders);
376
377         while (!tny_iterator_is_done (iter))
378         {
379                 TnyFolderStats *stats;
380                 TnyFolder *folder;
381
382                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
383                 stats = tny_folder_get_stats (folder);
384
385                 if (helper->function)
386                         helper->sum += helper->function (stats);
387
388                 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
389             
390                 g_object_unref (folder);
391                 g_object_unref (stats);
392                 tny_iterator_next (iter);
393         }
394          g_object_unref (G_OBJECT (iter));
395          g_object_unref (G_OBJECT (folders));
396 }
397
398 gint 
399 modest_tny_account_get_folder_count (TnyAccount *self)
400 {
401         RecurseFoldersHelper *helper;
402         gint retval;
403
404         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
405
406         /* Create helper */
407         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
408         helper->function = NULL;
409         helper->sum = 0;
410         helper->folders = 0;
411
412         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
413
414         retval = helper->folders;
415
416         g_free (helper);
417
418         return retval;
419 }
420
421 gint
422 modest_tny_account_get_message_count (TnyAccount *self)
423 {
424         RecurseFoldersHelper *helper;
425         gint retval;
426
427         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
428
429         /* Create helper */
430         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
431         helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
432         helper->sum = 0;
433
434         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
435
436         retval = helper->sum;
437
438         g_free (helper);
439
440         return retval;
441 }
442
443 gint 
444 modest_tny_account_get_local_size (TnyAccount *self)
445 {
446         RecurseFoldersHelper *helper;
447         gint retval;
448
449         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
450
451         /* Create helper */
452         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
453         helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
454         helper->sum = 0;
455
456         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
457
458         retval = helper->sum;
459
460         g_free (helper);
461
462         return retval;
463 }