1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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-tny-outbox-account.h>
37 #include <modest-account-mgr-helpers.h>
38 #include <modest-init.h>
39 #include <tny-camel-transport-account.h>
40 #include <tny-camel-imap-store-account.h>
41 #include <tny-camel-pop-store-account.h>
42 #include <tny-folder-stats.h>
46 modest_tny_account_get_special_folder (TnyAccount *account,
47 TnyFolderType special_type)
51 TnyFolder *special_folder = NULL;
54 g_return_val_if_fail (account, NULL);
55 g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
58 TnyAccount *local_account = NULL;
60 /* The accounts have already been instantiated by
61 * modest_tny_account_store_get_accounts(), which is the
62 * TnyAccountStore::get_accounts_func() implementation,
63 * so we just get them here.
66 /* Per-account outbox folders are each in their own on-disk directory: */
67 if (special_type == TNY_FOLDER_TYPE_OUTBOX) {
68 gchar *account_id = g_strdup_printf (
69 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
70 tny_account_get_id (account));
72 local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
76 /* Other local folders are all in one on-disk directory: */
77 local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(),
78 MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
82 g_printerr ("modest: cannot get local account\n");
86 folders = TNY_LIST (tny_simple_list_new ());
88 /* no need to do this _async, as these are local folders */
89 tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
91 iter = tny_list_create_iterator (folders);
93 while (!tny_iterator_is_done (iter)) {
95 TNY_FOLDER (tny_iterator_get_current (iter));
96 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
97 special_folder = folder;
101 g_object_unref (G_OBJECT(folder));
102 tny_iterator_next (iter);
105 g_object_unref (G_OBJECT (folders));
106 g_object_unref (G_OBJECT (iter));
107 g_object_unref (G_OBJECT (local_account));
109 return special_folder;
114 /* These seem to be listed in
115 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c
117 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
118 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
119 #define MODEST_ACCOUNT_OPTION_SSL_ALWAYS "always"
120 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
122 /* These seem to be listed in
123 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c
125 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
126 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
129 /* Posssible values for tny_account_set_secure_auth_mech().
130 * These might be camel-specific.
131 * Really, tinymail should use an enum.
132 * camel_sasl_authtype() seems to list some possible values.
135 /* Note that evolution does not offer these for IMAP: */
136 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
137 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
139 /* Caeml's IMAP uses NULL instead for "Password".
140 * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
141 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN"
142 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
146 * modest_tny_account_new_from_server_account:
147 * @account_mgr: a valid account mgr instance
148 * @account_name: the server account name for which to create a corresponding tny account
149 * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
151 * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
152 * NOTE: this function does not set the camel session or the get/forget password functions
154 * Returns: a new TnyAccount or NULL in case of error.
157 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
158 ModestServerAccountData *account_data)
162 g_return_val_if_fail (account_mgr, NULL);
163 g_return_val_if_fail (account_data, NULL);
166 if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
167 g_printerr ("modest: '%s' does not provide a protocol\n",
168 account_data->account_name);
172 TnyAccount *tny_account = NULL;
174 switch (account_data->proto) {
175 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
176 case MODEST_PROTOCOL_TRANSPORT_SMTP:
177 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
178 case MODEST_PROTOCOL_STORE_POP:
179 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
180 case MODEST_PROTOCOL_STORE_IMAP:
181 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
182 case MODEST_PROTOCOL_STORE_MAILDIR:
183 case MODEST_PROTOCOL_STORE_MBOX:
184 /* Note that this is not where we create the special local folders account.
185 * That happens in modest_tny_account_new_for_local_folders() instead.
187 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
189 g_return_val_if_reached (NULL);
192 g_printerr ("modest: could not create tny account for '%s'\n",
193 account_data->account_name);
196 tny_account_set_id (tny_account, account_data->account_name);
199 const gchar* proto_name =
200 modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
201 tny_account_set_proto (tny_account, proto_name);
204 /* mbox and maildir accounts use a URI instead of the rest:
205 * Note that this is not where we create the special local folders account.
206 * We do that in modest_tny_account_new_for_local_folders() instead. */
207 if (account_data->uri) {
208 /* printf("DEBUG: %s: Using URI=%s\n", __FUNCTION__, account_data->uri); */
209 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
210 g_message ("%s: local account-url: %s", __FUNCTION__, account_data->uri);
213 /* Set camel-specific options: */
215 /* Enable secure connection settings: */
216 /* printf("DEBUG: %s: security=%d\n", __FUNCTION__, account_data->security); */
217 const gchar* option_security = NULL;
218 switch (account_data->security) {
219 case MODEST_PROTOCOL_CONNECTION_NORMAL:
220 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_NEVER;
222 case MODEST_PROTOCOL_CONNECTION_SSL:
223 case MODEST_PROTOCOL_CONNECTION_TLS:
224 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_ALWAYS;;
226 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
227 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
234 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
237 /* Secure authentication: */
238 /* printf("DEBUG: %s: secure-auth=%d\n", __FUNCTION__, account_data->secure_auth); */
239 const gchar* auth_mech_name = NULL;
240 switch (account_data->secure_auth) {
241 case MODEST_PROTOCOL_AUTH_NONE:
242 /* IMAP and POP need at least a password,
243 * which camel uses if we specify NULL.
244 * This setting should never happen anyway. */
245 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP ||
246 account_data->proto == MODEST_PROTOCOL_STORE_POP)
247 auth_mech_name = NULL;
248 else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
249 auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
251 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
254 case MODEST_PROTOCOL_AUTH_PASSWORD:
255 /* Camel use a password for IMAP or POP if we specify NULL,
256 * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
257 * (POP is know to report an error for Login too. Probably Password and Plain too.) */
258 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
259 auth_mech_name = NULL;
260 else if (account_data->proto == MODEST_PROTOCOL_STORE_POP)
261 auth_mech_name = NULL;
263 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
266 case MODEST_PROTOCOL_AUTH_CRAMMD5:
267 auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
271 g_warning ("%s: Unhandled secure authentication setting %d for "
272 "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
273 account_data->account_name, account_data->hostname);
278 tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
280 if (modest_protocol_info_protocol_is_store(account_data->proto) &&
281 (account_data->proto == MODEST_PROTOCOL_STORE_IMAP) ) {
282 /* Other connection options, needed for IMAP. */
283 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
284 MODEST_ACCOUNT_OPTION_USE_LSUB);
285 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
286 MODEST_ACCOUNT_OPTION_CHECK_ALL);
289 if (account_data->username)
290 tny_account_set_user (tny_account, account_data->username);
291 if (account_data->hostname)
292 tny_account_set_hostname (tny_account, account_data->hostname);
295 if (account_data->port)
296 tny_account_set_port (tny_account, account_data->port);
299 /* FIXME: for debugging */
300 url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
301 g_message ("modest: account-url: %s", url);
303 /***********************/
309 /* we need these dummy functions, or tinymail will complain */
311 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
316 forget_pass_dummy (TnyAccount *account)
318 /* intentionally left blank */
322 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
324 TnySessionCamel *session,
325 TnyGetPassFunc get_pass_func,
326 TnyForgetPassFunc forget_pass_func)
328 TnyAccount *tny_account = NULL;
329 ModestAccountData *account_data = NULL;
330 ModestServerAccountData *server_data = NULL;
332 g_return_val_if_fail (account_mgr, NULL);
333 g_return_val_if_fail (account_name, NULL);
335 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
337 g_printerr ("modest: cannot get account data for account %s\n", account_name);
341 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
342 server_data = account_data->store_account;
343 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
344 server_data = account_data->transport_account;
346 g_printerr ("modest: no %s account defined for '%s'\n",
347 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
348 account_data->display_name);
349 modest_account_mgr_free_account_data (account_mgr, account_data);
353 tny_account = modest_tny_account_new_from_server_account (account_mgr, server_data);
355 g_printerr ("modest: failed to create tny account for %s (%s)\n",
356 account_data->account_name, server_data->account_name);
357 modest_account_mgr_free_account_data (account_mgr, account_data);
361 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
362 tny_account_set_forget_pass_func (tny_account,
363 forget_pass_func ? forget_pass_func : forget_pass_dummy);
364 tny_account_set_pass_func (tny_account,
365 get_pass_func ? get_pass_func: get_pass_dummy);
367 /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
368 * account, we set its name to the account of which it is part. */
369 if (account_data->display_name)
370 tny_account_set_name (tny_account, account_data->display_name);
372 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, account_name);
374 modest_account_mgr_free_account_data (account_mgr, account_data);
381 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session)
383 TnyStoreAccount *tny_account;
385 gchar *maildir, *url_string;
387 g_return_val_if_fail (account_mgr, NULL);
389 tny_account = tny_camel_store_account_new ();
391 g_printerr ("modest: cannot create account for local folders");
394 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
396 /* This path contains directories for each local folder.
397 * We have created them so that TnyCamelStoreAccount can find them
398 * and report a folder for each directory: */
399 maildir = modest_local_folder_info_get_maildir_path ();
400 url = camel_url_new ("maildir:", NULL);
401 camel_url_set_path (url, maildir);
402 /* Needed by tinymail's DBC assertions */
403 camel_url_set_host (url, "localhost");
404 url_string = camel_url_to_string (url, 0);
406 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
407 printf("DEBUG: %s: local folders url=%s\n", __FUNCTION__, url_string);
409 tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
410 tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
411 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
412 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
414 modest_tny_account_set_parent_modest_account_name_for_server_account (
415 TNY_ACCOUNT (tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
417 camel_url_free (url);
421 return TNY_ACCOUNT(tny_account);
426 modest_tny_account_new_for_per_account_local_outbox_folder (ModestAccountMgr *account_mgr, TnyAccount *account, TnySessionCamel *session)
428 g_return_val_if_fail (account_mgr, NULL);
429 g_return_val_if_fail (account, NULL);
430 g_return_val_if_fail (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_TRANSPORT, NULL);
432 /* Notice that we create a ModestTnyOutboxAccount here,
433 * instead of just a TnyCamelStoreAccount,
434 * so that we can later identify this as a special account for internal use only.
436 TnyStoreAccount *tny_account = TNY_STORE_ACCOUNT (modest_tny_outbox_account_new ());
438 g_printerr ("modest: cannot create account for per-account local outbox folder.");
442 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
444 /* Make sure that the paths exists on-disk so that TnyCamelStoreAccount can
445 * find it to create a TnyFolder for it: */
446 gchar *folder_dir = modest_per_account_local_outbox_folder_info_get_maildir_path_to_outbox_folder (account);
447 modest_init_one_local_folder(folder_dir);
451 /* This path should contain just one directory - "outbox": */
453 modest_per_account_local_outbox_folder_info_get_maildir_path (account);
455 CamelURL *url = camel_url_new ("maildir:", NULL);
456 camel_url_set_path (url, maildir);
459 /* Needed by tinymail's DBC assertions */
460 camel_url_set_host (url, "localhost");
461 gchar *url_string = camel_url_to_string (url, 0);
462 camel_url_free (url);
464 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
465 printf("DEBUG: %s: local outbox folder account url=%s\n", __FUNCTION__, url_string);
468 /* This text should never been seen,
469 * because the per-account outbox accounts are not seen directly by the user.
470 * Their folders are merged and shown as one folder. */
471 tny_account_set_name (TNY_ACCOUNT(tny_account), "Per-Account Outbox");
473 gchar *account_id = g_strdup_printf (
474 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
475 tny_account_get_id (account));
476 tny_account_set_id (TNY_ACCOUNT(tny_account), account_id);
479 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
480 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
482 /* Make this think that it belongs to the modest local-folders parent account: */
483 modest_tny_account_set_parent_modest_account_name_for_server_account (
484 TNY_ACCOUNT (tny_account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
486 return TNY_ACCOUNT(tny_account);
491 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
493 typedef struct _RecurseFoldersHelper {
494 TnyStatsFunc function;
497 } RecurseFoldersHelper;
500 recurse_folders (TnyFolderStore *store,
501 TnyFolderStoreQuery *query,
502 RecurseFoldersHelper *helper)
505 TnyList *folders = tny_simple_list_new ();
507 tny_folder_store_get_folders (store, folders, query, NULL);
508 iter = tny_list_create_iterator (folders);
510 helper->folders += tny_list_get_length (folders);
512 while (!tny_iterator_is_done (iter)) {
513 TnyFolderStats *stats;
516 folder = TNY_FOLDER (tny_iterator_get_current (iter));
517 stats = tny_folder_get_stats (folder);
519 /* initially, we sometimes get -1 from tinymail; ignore that */
520 if (helper->function && helper->function (stats) > 0)
521 helper->sum += helper->function (stats);
523 if (TNY_IS_FOLDER_STORE (folder)) {
524 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
527 g_object_unref (folder);
528 g_object_unref (stats);
529 tny_iterator_next (iter);
531 g_object_unref (G_OBJECT (iter));
532 g_object_unref (G_OBJECT (folders));
536 modest_tny_folder_store_get_folder_count (TnyFolderStore *self)
538 RecurseFoldersHelper *helper;
541 g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
544 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
545 helper->function = NULL;
549 recurse_folders (self, NULL, helper);
551 retval = helper->folders;
559 modest_tny_folder_store_get_message_count (TnyFolderStore *self)
561 RecurseFoldersHelper *helper;
564 g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
567 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
568 helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
571 recurse_folders (self, NULL, helper);
573 retval = helper->sum;
581 modest_tny_folder_store_get_local_size (TnyFolderStore *self)
583 RecurseFoldersHelper *helper;
586 g_return_val_if_fail (TNY_IS_ACCOUNT (self), -1);
589 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
590 helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
593 recurse_folders (self, NULL, helper);
595 retval = helper->sum;
602 const gchar* modest_tny_account_get_parent_modest_account_name_for_server_account (TnyAccount *self)
604 return (const gchar *)g_object_get_data (G_OBJECT (self), "modest_account");
607 void modest_tny_account_set_parent_modest_account_name_for_server_account (TnyAccount *self, const gchar* parent_modest_acount_name)
609 g_object_set_data_full (G_OBJECT(self), "modest_account",
610 (gpointer*) g_strdup (parent_modest_acount_name), g_free);