2007-05-23 Christian Kellner <ckellner@openismus.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
107 /* Posssible values for tny_account_set_secure_auth_mech().
108  * These might be camel-specific.
109  * Really, tinymail should use an enum.
110  * camel_sasl_authtype() seems to list some possible values.
111  */
112  
113 /* Note that evolution does not offer these for IMAP: */
114 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
115 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
116
117 /* Caeml's IMAP uses NULL instead for "Password".
118  * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
119 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN" 
120 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
121
122
123 /**
124  * modest_tny_account_new_from_server_account:
125  * @account_mgr: a valid account mgr instance
126  * @account_name: the server account name for which to create a corresponding tny account
127  * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
128  * 
129  * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
130  * NOTE: this function does not set the camel session or the get/forget password functions
131  * 
132  * Returns: a new TnyAccount or NULL in case of error.
133  */
134 static TnyAccount*
135 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
136                                             ModestServerAccountData *account_data)
137 {       
138         gchar *url;
139
140         g_return_val_if_fail (account_mgr, NULL);
141         g_return_val_if_fail (account_data, NULL);
142
143         /* sanity checks */
144         if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
145                 g_printerr ("modest: '%s' does not provide a protocol\n",
146                             account_data->account_name);
147                 return NULL;
148         }
149
150         TnyAccount *tny_account = NULL;
151         
152         switch (account_data->proto) {
153         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
154         case MODEST_PROTOCOL_TRANSPORT_SMTP:
155                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
156         case MODEST_PROTOCOL_STORE_POP:
157                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
158         case MODEST_PROTOCOL_STORE_IMAP:
159                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
160         case MODEST_PROTOCOL_STORE_MAILDIR:
161         case MODEST_PROTOCOL_STORE_MBOX:
162                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
163         default:
164                 g_return_val_if_reached (NULL);
165         }
166         if (!tny_account) {
167                 g_printerr ("modest: could not create tny account for '%s'\n",
168                             account_data->account_name);
169                 return NULL;
170         }
171         tny_account_set_id (tny_account, account_data->account_name);
172
173         /* Proto */
174         const gchar* proto_name =
175                 modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
176         tny_account_set_proto (tny_account, proto_name);
177
178                
179         /* mbox and maildir accounts use a URI instead of the rest: */
180         if (account_data->uri)  {
181                 /* printf("DEBUG: %s: Using URI=%s\n", __FUNCTION__, account_data->uri); */
182                 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
183         }
184         else {
185                 /* Set camel-specific options: */
186                 
187                 /* Enable secure connection settings: */
188                 /* printf("DEBUG: %s: security=%d\n", __FUNCTION__, account_data->security); */
189                 const gchar* option_security = NULL;
190                 switch (account_data->security) {
191                 case MODEST_PROTOCOL_CONNECTION_NORMAL:
192                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_NEVER;
193                         break;
194                 case MODEST_PROTOCOL_CONNECTION_SSL:
195                 case MODEST_PROTOCOL_CONNECTION_TLS:
196                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
197                         break;
198                 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
199                         option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
200                         break;
201                 default:
202                         break;
203                 }
204                 
205                 if(option_security)
206                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
207                                                       option_security);
208                 
209                 /* Secure authentication: */
210                 /* printf("DEBUG: %s: secure-auth=%d\n", __FUNCTION__, account_data->secure_auth); */
211                 const gchar* auth_mech_name = NULL;
212                 switch (account_data->secure_auth) {
213                 case MODEST_PROTOCOL_AUTH_NONE:
214                         /* IMAP and POP need at least a password,
215                          * which camel uses if we specify NULL.
216                          * This setting should never happen anyway. */
217                         if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP ||
218                             account_data->proto == MODEST_PROTOCOL_STORE_POP)
219                                 auth_mech_name = NULL;
220                         else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
221                                 auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
222                         else
223                                 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
224                         break;
225                         
226                 case MODEST_PROTOCOL_AUTH_PASSWORD:
227                         /* Camel use a password for IMAP or POP if we specify NULL,
228                          * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
229                          * (POP is know to report an error for Login too. Probably Password and Plain too.) */
230                         if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
231                                 auth_mech_name = NULL;
232                         else if (account_data->proto == MODEST_PROTOCOL_STORE_POP)
233                                 auth_mech_name = NULL;
234                         else
235                                 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
236                         break;
237                         
238                 case MODEST_PROTOCOL_AUTH_CRAMMD5:
239                         auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
240                         break;
241                         
242                 default:
243                         g_warning ("%s: Unhandled secure authentication setting %d for "
244                                 "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
245                                    account_data->account_name, account_data->hostname);
246                         break;
247                 }
248                 
249                 if(auth_mech_name) 
250                         tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
251                 
252                 if (modest_protocol_info_protocol_is_store(account_data->proto) && 
253                         (account_data->proto == MODEST_PROTOCOL_STORE_IMAP) ) {
254                         /* Other connection options, needed for IMAP. */
255                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
256                                                       MODEST_ACCOUNT_OPTION_USE_LSUB);
257                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
258                                                       MODEST_ACCOUNT_OPTION_CHECK_ALL);
259                 }
260                 
261                 if (account_data->username) 
262                         tny_account_set_user (tny_account, account_data->username);
263                 if (account_data->hostname)
264                         tny_account_set_hostname (tny_account, account_data->hostname);
265                  
266                 /* Set the port: */
267                 if (account_data->port)
268                         tny_account_set_port (tny_account, account_data->port);
269         }
270
271         /* FIXME: for debugging */
272         url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
273         g_message ("modest: account-url: %s", url);
274         g_free (url);
275         /***********************/
276         
277         return tny_account;
278 }
279
280
281 /* we need these dummy functions, or tinymail will complain */
282 static gchar*
283 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
284 {
285         return NULL;
286 }
287 static void
288 forget_pass_dummy (TnyAccount *account)
289 {
290         /* intentionally left blank */
291 }
292
293 TnyAccount*
294 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
295                                      TnyAccountType type,
296                                      TnySessionCamel *session,
297                                      TnyGetPassFunc get_pass_func,
298                                      TnyForgetPassFunc forget_pass_func) 
299 {
300         TnyAccount *tny_account = NULL;
301         ModestAccountData *account_data = NULL;
302         ModestServerAccountData *server_data = NULL;
303
304         g_return_val_if_fail (account_mgr, NULL);
305         g_return_val_if_fail (account_name, NULL);
306
307         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
308         if (!account_data) {
309                 g_printerr ("modest: cannot get account data for account %s\n", account_name);
310                 return NULL;
311         }
312
313         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
314                 server_data = account_data->store_account;
315         else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
316                 server_data = account_data->transport_account;
317         if (!server_data) {
318                 g_printerr ("modest: no %s account defined for '%s'\n",
319                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
320                             account_data->display_name);
321                 modest_account_mgr_free_account_data (account_mgr, account_data);
322                 return NULL;
323         }
324         
325         tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
326         if (!tny_account) { 
327                 g_printerr ("modest: failed to create tny account for %s (%s)\n",
328                             account_data->account_name, server_data->account_name);
329                 modest_account_mgr_free_account_data (account_mgr, account_data);
330                 return NULL;
331         }
332         
333         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
334         tny_account_set_forget_pass_func (tny_account,
335                                           forget_pass_func ? forget_pass_func : forget_pass_dummy);
336         tny_account_set_pass_func (tny_account,
337                                    get_pass_func ? get_pass_func: get_pass_dummy);
338
339         /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
340          * account, we set its name to the account of which it is part. */
341         if (account_data->display_name)
342                 tny_account_set_name (tny_account, account_data->display_name); 
343
344         g_object_set_data_full (G_OBJECT(tny_account), "modest_account",
345                                 (gpointer*) g_strdup (account_name), g_free);
346         
347         modest_account_mgr_free_account_data (account_mgr, account_data);
348
349         return tny_account;
350 }
351
352
353 TnyAccount*
354 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
355 {
356         TnyStoreAccount *tny_account;
357         CamelURL *url;
358         gchar *maildir, *url_string;
359
360         g_return_val_if_fail (account_mgr, NULL);
361         
362         tny_account = tny_camel_store_account_new ();
363         if (!tny_account) {
364                 g_printerr ("modest: cannot create account for local folders");
365                 return NULL;
366         }
367         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
368         
369         maildir = modest_local_folder_info_get_maildir_path ();
370         url = camel_url_new ("maildir:", NULL);
371         camel_url_set_path (url, maildir);
372         /* Needed by tinymail's DBC assertions */
373         camel_url_set_host (url, "localhost");
374         url_string = camel_url_to_string (url, 0);
375         
376         tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
377
378         tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME); 
379         tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); 
380         tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
381         tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
382         
383         g_object_set_data (G_OBJECT(tny_account), "modest_account",
384                            (gpointer*)MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
385         
386         camel_url_free (url);
387         g_free (maildir);
388         g_free (url_string);
389
390         return TNY_ACCOUNT(tny_account);
391 }
392
393 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
394
395 typedef struct _RecurseFoldersHelper {
396         TnyStatsFunc function;
397         guint sum;
398         guint folders;
399 } RecurseFoldersHelper;
400
401 static void
402 recurse_folders (TnyFolderStore *store, 
403                  TnyFolderStoreQuery *query, 
404                  RecurseFoldersHelper *helper)
405 {
406         TnyIterator *iter;
407         TnyList *folders = tny_simple_list_new ();
408
409         tny_folder_store_get_folders (store, folders, query, NULL);
410         iter = tny_list_create_iterator (folders);
411
412         helper->folders += tny_list_get_length (folders);
413
414         while (!tny_iterator_is_done (iter)) {
415                 TnyFolderStats *stats;
416                 TnyFolder *folder;
417
418                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
419                 stats = tny_folder_get_stats (folder);
420
421                 /* initially, we sometimes get -1 from tinymail; ignore that */
422                 if (helper->function && helper->function (stats) > 0)
423                         helper->sum += helper->function (stats);
424
425                 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
426             
427                 g_object_unref (folder);
428                 g_object_unref (stats);
429                 tny_iterator_next (iter);
430         }
431          g_object_unref (G_OBJECT (iter));
432          g_object_unref (G_OBJECT (folders));
433 }
434
435 gint 
436 modest_tny_account_get_folder_count (TnyAccount *self)
437 {
438         RecurseFoldersHelper *helper;
439         gint retval;
440
441         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
442
443         /* Create helper */
444         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
445         helper->function = NULL;
446         helper->sum = 0;
447         helper->folders = 0;
448
449         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
450
451         retval = helper->folders;
452
453         g_free (helper);
454
455         return retval;
456 }
457
458 gint
459 modest_tny_account_get_message_count (TnyAccount *self)
460 {
461         RecurseFoldersHelper *helper;
462         gint retval;
463
464         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
465         
466         /* Create helper */
467         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
468         helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
469         helper->sum = 0;
470
471         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
472
473         retval = helper->sum;
474
475         g_free (helper);
476
477         return retval;
478 }
479
480 gint 
481 modest_tny_account_get_local_size (TnyAccount *self)
482 {
483         RecurseFoldersHelper *helper;
484         gint retval;
485
486         g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
487
488         /* Create helper */
489         helper = g_malloc0 (sizeof (RecurseFoldersHelper));
490         helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
491         helper->sum = 0;
492
493         recurse_folders (TNY_FOLDER_STORE (self), NULL, helper);
494
495         retval = helper->sum;
496
497         g_free (helper);
498
499         return retval;
500 }