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