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-platform.h>
31 #include <modest-tny-platform-factory.h>
32 #include <modest-tny-account.h>
33 #include <modest-tny-account-store.h>
34 #include <modest-tny-local-folders-account.h>
35 #include <modest-runtime.h>
36 #include <tny-simple-list.h>
37 #include <modest-tny-folder.h>
38 #include <modest-tny-outbox-account.h>
39 #include <modest-account-mgr-helpers.h>
40 #include <modest-init.h>
41 #include <tny-camel-transport-account.h>
42 #include <tny-camel-imap-store-account.h>
43 #include <tny-camel-pop-store-account.h>
44 #include <tny-folder-stats.h>
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <hildon-widgets/hildon-file-system-info.h>
49 #include <hildon/hildon-file-system-info.h>
53 modest_tny_account_get_special_folder (TnyAccount *account,
54 TnyFolderType special_type)
58 TnyFolder *special_folder = NULL;
61 g_return_val_if_fail (account, NULL);
62 g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
65 TnyAccount *local_account = NULL;
67 /* The accounts have already been instantiated by
68 * modest_tny_account_store_get_accounts(), which is the
69 * TnyAccountStore::get_accounts_func() implementation,
70 * so we just get them here.
73 /* Per-account outbox folders are each in their own on-disk directory: */
74 if (special_type == TNY_FOLDER_TYPE_OUTBOX) {
75 const gchar *modest_account_name =
76 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
77 g_assert (modest_account_name);
79 gchar *account_id = g_strdup_printf (
80 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
83 local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
84 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
87 g_printerr ("modest: %s: modest_tny_account_store_get_tny_account_by(ID) returned NULL for %s\n", __FUNCTION__, account_id);
93 /* Other local folders are all in one on-disk directory: */
94 local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
95 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
96 MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
100 g_printerr ("modest: cannot get local account\n");
104 folders = TNY_LIST (tny_simple_list_new ());
106 /* There is no need to do this _async, as these are local folders. */
107 /* TODO: However, this seems to fail sometimes when the network is busy,
108 * returning an empty list. murrayc. */
109 GError *error = NULL;
110 tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
111 folders, NULL, &error);
113 g_warning ("%s: tny_folder_store_get_folders() failed:\n error=%s\n",
114 __FUNCTION__, error->message);
117 if (tny_list_get_length (folders) == 0) {
118 gchar* url_string = tny_account_get_url_string (local_account);
119 g_printerr ("modest: %s: tny_folder_store_get_folders() returned an empty list for account with URL '%s'\n",
120 __FUNCTION__, url_string);
124 iter = tny_list_create_iterator (folders);
126 while (!tny_iterator_is_done (iter)) {
128 TNY_FOLDER (tny_iterator_get_current (iter));
129 if (modest_tny_folder_get_local_folder_type (folder) == special_type) {
130 special_folder = folder;
134 g_object_unref (G_OBJECT(folder));
135 tny_iterator_next (iter);
138 g_object_unref (G_OBJECT (folders));
139 g_object_unref (G_OBJECT (iter));
140 g_object_unref (G_OBJECT (local_account));
143 if (!special_folder) {
144 g_warning ("%s: Returning NULL.", __FUNCTION__);
148 return special_folder;
152 on_connection_status_changed (TnyAccount *account, TnyConnectionStatus status, gpointer user_data)
154 printf ("DEBUG: %s: status=%d\n", __FUNCTION__, status);
156 if (status == TNY_CONNECTION_STATUS_DISCONNECTED) {
157 /* A tinymail network operation failed, and tinymail then noticed that
158 * the account is offline, because our TnyDevice is offline,
159 * because libconic says we are offline.
160 * So ask the user to go online again.
162 * Note that this signal will not be emitted if the account was offline
163 * when the network operation was first attempted. For those cases,
164 * the application must do its own explicit checks.
166 * We make sure that this UI is shown in the main thread, to avoid races,
167 * because tinymail does not guarantee that this signal handler will be called
168 * in the main thread.
170 /* TODO: Commented out (and setting offline instead), because this causes hangs, probably related to
171 * our use of mainloops:
172 * modest_platform_connect_and_wait (NULL);
174 TnyDevice *device = modest_runtime_get_device ();
175 tny_device_force_offline (device);
176 } else if (status == TNY_CONNECTION_STATUS_CONNECTED_BROKEN) {
177 printf ("DEBUG: %s: Connection broken. Forcing TnyDevice offline.\n",
180 /* Something went wrong during some network operation.
181 * Stop trying to use the network now,
182 * by forcing accounts into offline mode:
184 * When libconic reconnects, it will set the device back online again,
185 * regardless of it being forced offline before.
187 /* TODO: Find out when this is falsely being emitted. */
188 printf (" DEBUG: %s: Not forcing offline because tinymail is sometimes reporting false connection breaks.\n",
191 TnyDevice *device = modest_runtime_get_device ();
192 tny_device_force_offline (device);
199 /* These seem to be listed in
200 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-store.c
202 #define MODEST_ACCOUNT_OPTION_SSL "use_ssl"
203 #define MODEST_ACCOUNT_OPTION_SSL_NEVER "never"
204 /* This is a tinymail camel-lite specific option,
205 * roughly equivalent to "always" in regular camel,
206 * which is appropriate for a generic "SSL" connection option: */
207 #define MODEST_ACCOUNT_OPTION_SSL_WRAPPED "wrapped"
208 /* Not used in our UI so far: */
209 #define MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE "when-possible"
210 /* This is a tinymailcamel-lite specific option that is not in regular camel. */
211 #define MODEST_ACCOUNT_OPTION_SSL_TLS "tls"
213 /* These seem to be listed in
214 * libtinymail-camel/camel-lite/camel/providers/imap/camel-imap-provider.c
216 #define MODEST_ACCOUNT_OPTION_USE_LSUB "use_lsub" /* Show only subscribed folders */
217 #define MODEST_ACCOUNT_OPTION_CHECK_ALL "check_all" /* Check for new messages in all folders */
220 /* Posssible values for tny_account_set_secure_auth_mech().
221 * These might be camel-specific.
222 * Really, tinymail should use an enum.
223 * camel_sasl_authtype() seems to list some possible values.
226 /* Note that evolution does not offer these for IMAP: */
227 #define MODEST_ACCOUNT_AUTH_PLAIN "PLAIN"
228 #define MODEST_ACCOUNT_AUTH_ANONYMOUS "ANONYMOUS"
230 /* Caeml's IMAP uses NULL instead for "Password".
231 * Also, not that Evolution offers "Password" for IMAP, but "Login" for SMTP.*/
232 #define MODEST_ACCOUNT_AUTH_PASSWORD "LOGIN"
233 #define MODEST_ACCOUNT_AUTH_CRAMMD5 "CRAM-MD5"
238 * modest_tny_account_new_from_server_account:
239 * @account_mgr: a valid account mgr instance
240 * @session: A valid TnySessionCamel instance.
241 * @account_data: the server account for which to create a corresponding tny account
242 * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT)
244 * get a tnyaccount corresponding to the server_accounts (store or transport) for this account.
245 * NOTE: this function does not set the camel session or the get/forget password functions
247 * Returns: a new TnyAccount or NULL in case of error.
250 modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr,
251 TnySessionCamel *session,
252 ModestServerAccountData *account_data)
256 g_return_val_if_fail (account_mgr, NULL);
257 g_return_val_if_fail (session, NULL);
258 g_return_val_if_fail (account_data, NULL);
261 if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
262 g_printerr ("modest: '%s' does not provide a protocol\n",
263 account_data->account_name);
267 TnyAccount *tny_account = NULL;
269 switch (account_data->proto) {
270 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
271 case MODEST_PROTOCOL_TRANSPORT_SMTP:
272 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
273 case MODEST_PROTOCOL_STORE_POP:
274 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
275 case MODEST_PROTOCOL_STORE_IMAP:
276 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
277 case MODEST_PROTOCOL_STORE_MAILDIR:
278 case MODEST_PROTOCOL_STORE_MBOX:
279 /* Note that this is not where we create the special local folders account.
280 * That happens in modest_tny_account_new_for_local_folders() instead.
282 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
284 g_return_val_if_reached (NULL);
287 g_printerr ("modest: could not create tny account for '%s'\n",
288 account_data->account_name);
291 tny_account_set_id (tny_account, account_data->account_name);
293 /* This must be set quite early, or other set() functions will fail. */
294 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (tny_account), session);
296 /* Handle connection requests:
297 * This (badly-named) signal will be called when we try to use an offline account. */
298 g_signal_connect (G_OBJECT (tny_account), "connection-status-changed",
299 G_CALLBACK (on_connection_status_changed), NULL);
302 const gchar* proto_name =
303 modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
304 tny_account_set_proto (tny_account, proto_name);
307 /* mbox and maildir accounts use a URI instead of the rest:
308 * Note that this is not where we create the special local folders account.
309 * We do that in modest_tny_account_new_for_local_folders() instead. */
310 if (account_data->uri) {
311 tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
312 /* g_message ("DEBUG: %s: local account-url:\n %s", __FUNCTION__, account_data->uri); */
315 /* Set camel-specific options: */
317 /* Enable secure connection settings: */
318 /* printf("DEBUG: %s: security=%d\n", __FUNCTION__, account_data->security); */
319 const gchar* option_security = NULL;
320 switch (account_data->security) {
321 case MODEST_PROTOCOL_CONNECTION_NORMAL:
322 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_NEVER;
324 case MODEST_PROTOCOL_CONNECTION_SSL:
325 /* Apparently, use of "IMAPS" (specified in our UI spec), implies
326 * use of the "wrapped" option: */
327 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_WRAPPED;
329 case MODEST_PROTOCOL_CONNECTION_TLS:
330 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_TLS;
332 case MODEST_PROTOCOL_CONNECTION_TLS_OP:
333 /* This is not actually in our UI: */
334 option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_WHEN_POSSIBLE;
341 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
344 /* Secure authentication: */
345 /* printf("DEBUG: %s: secure-auth=%d\n", __FUNCTION__, account_data->secure_auth); */
346 const gchar* auth_mech_name = NULL;
347 switch (account_data->secure_auth) {
348 case MODEST_PROTOCOL_AUTH_NONE:
349 /* IMAP and POP need at least a password,
350 * which camel uses if we specify NULL.
351 * This setting should never happen anyway. */
352 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP ||
353 account_data->proto == MODEST_PROTOCOL_STORE_POP)
354 auth_mech_name = NULL;
355 else if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP)
356 auth_mech_name = MODEST_ACCOUNT_AUTH_ANONYMOUS;
358 auth_mech_name = MODEST_ACCOUNT_AUTH_PLAIN;
361 case MODEST_PROTOCOL_AUTH_PASSWORD:
362 /* Camel use a password for IMAP or POP if we specify NULL,
363 * For IMAP, at least it will report an error if we use "Password", "Login" or "Plain".
364 * (POP is know to report an error for Login too. Probably Password and Plain too.) */
365 if (account_data->proto == MODEST_PROTOCOL_STORE_IMAP)
366 auth_mech_name = NULL;
367 else if (account_data->proto == MODEST_PROTOCOL_STORE_POP)
368 auth_mech_name = NULL;
370 auth_mech_name = MODEST_ACCOUNT_AUTH_PASSWORD;
373 case MODEST_PROTOCOL_AUTH_CRAMMD5:
374 auth_mech_name = MODEST_ACCOUNT_AUTH_CRAMMD5;
378 g_warning ("%s: Unhandled secure authentication setting %d for "
379 "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
380 account_data->account_name, account_data->hostname);
385 tny_account_set_secure_auth_mech (tny_account, auth_mech_name);
387 if (modest_protocol_info_protocol_is_store(account_data->proto) &&
388 (account_data->proto == MODEST_PROTOCOL_STORE_IMAP) ) {
389 /* Other connection options, needed for IMAP. */
390 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
391 MODEST_ACCOUNT_OPTION_USE_LSUB);
392 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
393 MODEST_ACCOUNT_OPTION_CHECK_ALL);
396 if (account_data->username)
397 tny_account_set_user (tny_account, account_data->username);
398 if (account_data->hostname)
399 tny_account_set_hostname (tny_account, account_data->hostname);
402 if (account_data->port)
403 tny_account_set_port (tny_account, account_data->port);
406 /* FIXME: for debugging.
407 * Let's keep this because it is very useful for debugging. */
408 url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
410 printf ("DEBUG %s:\n account-url: %s\n", __FUNCTION__, url);
413 /***********************/
419 modest_tny_account_new_from_server_account_name (ModestAccountMgr *account_mgr,
420 TnySessionCamel *session,
421 const gchar *server_account_name)
423 ModestServerAccountData *account_data =
424 modest_account_mgr_get_server_account_data (account_mgr,
425 server_account_name);
429 TnyAccount *result = modest_tny_account_new_from_server_account (
430 account_mgr, session, account_data);
432 modest_account_mgr_free_server_account_data (account_mgr, account_data);
438 /* we need these dummy functions, or tinymail will complain */
440 get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
445 forget_pass_dummy (TnyAccount *account)
447 /* intentionally left blank */
451 modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name,
453 TnySessionCamel *session,
454 TnyGetPassFunc get_pass_func,
455 TnyForgetPassFunc forget_pass_func)
457 TnyAccount *tny_account = NULL;
458 ModestAccountData *account_data = NULL;
459 ModestServerAccountData *server_data = NULL;
461 g_return_val_if_fail (account_mgr, NULL);
462 g_return_val_if_fail (account_name, NULL);
463 g_return_val_if_fail (session, NULL);
465 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
467 g_printerr ("modest: %s: cannot get account data for account %s\n", __FUNCTION__, account_name);
471 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
472 server_data = account_data->store_account;
473 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
474 server_data = account_data->transport_account;
476 g_printerr ("modest: no %s account defined for '%s'\n",
477 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
478 account_data->display_name);
479 modest_account_mgr_free_account_data (account_mgr, account_data);
483 tny_account = modest_tny_account_new_from_server_account (account_mgr, session, server_data);
485 g_printerr ("modest: failed to create tny account for %s (%s)\n",
486 account_data->account_name, server_data->account_name);
487 modest_account_mgr_free_account_data (account_mgr, account_data);
491 tny_account_set_forget_pass_func (tny_account,
492 forget_pass_func ? forget_pass_func : forget_pass_dummy);
493 tny_account_set_pass_func (tny_account,
494 get_pass_func ? get_pass_func: get_pass_dummy);
497 /* This name is what shows up in the folder view -- so for some POP/IMAP/... server
498 * account, we set its name to the account of which it is part. */
500 if (account_data->display_name)
501 tny_account_set_name (tny_account, account_data->display_name);
503 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, account_name);
505 modest_account_mgr_free_account_data (account_mgr, account_data);
508 TnyAccountStore *astore = (TnyAccountStore *) modest_runtime_get_account_store ();
510 TnyDevice *device = tny_account_store_get_device (astore);
512 g_object_set_data (G_OBJECT(tny_account), "account_store", (gpointer)astore);
513 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (tny_account),
514 tny_device_is_online (device), &err);
516 g_print ("%s: tny_camel_account_set_online() failed: %s\n", __FUNCTION__, err->message);
519 g_object_unref (device);
528 TnyStoreAccount *account;
530 ModestTnyAccountGetMmcAccountNameCallback callback;
532 } GetMmcAccountNameData;
537 /* Gets the memory card name: */
539 on_modest_file_system_info(HildonFileSystemInfoHandle *handle,
540 HildonFileSystemInfo *info,
541 const GError *error, gpointer data)
543 GetMmcAccountNameData *callback_data = (GetMmcAccountNameData*)data;
546 g_warning ("%s: error=%s", __FUNCTION__, error->message);
549 TnyAccount *account = TNY_ACCOUNT (callback_data->account);
551 const gchar *previous_display_name = NULL;
553 const gchar *display_name = NULL;
554 if (!error && info) {
555 display_name = hildon_file_system_info_get_display_name(info);
556 previous_display_name = tny_account_get_name (account);
559 /* printf ("DEBUG: %s: display name=%s\n", __FUNCTION__, display_name); */
560 tny_account_set_name (account, display_name);
562 /* Inform the application that the name is now ready: */
563 if (callback_data->callback)
564 (*(callback_data->callback)) (callback_data->account,
565 callback_data->user_data);
567 g_object_unref (callback_data->account);
568 g_slice_free (GetMmcAccountNameData, callback_data);
571 void modest_tny_account_get_mmc_account_name (TnyStoreAccount* self, ModestTnyAccountGetMmcAccountNameCallback callback, gpointer user_data)
573 /* Just use the hard-coded path for the single memory card,
574 * rather than try to figure out the path to the specific card by
575 * looking at the maildir URI:
577 const gchar *uri_real = MODEST_MCC1_VOLUMEPATH_URI;
580 gchar* uri = tny_account_get_url_string (TNY_ACCOUNT (self));
584 TODO: This gets the name of the folder, but we want the name of the volume.
585 gchar *uri_real = NULL;
586 const gchar* prefix = "maildir://localhost/";
587 if ((strstr (uri, prefix) == uri) && (strlen(uri) > strlen(prefix)) )
588 uri_real = g_strconcat ("file:///", uri + strlen (prefix), NULL);
592 //This is freed in the callback:
593 GetMmcAccountNameData * callback_data = g_slice_new0(GetMmcAccountNameData);
594 callback_data->account = self;
595 g_object_ref (callback_data->account); /* Unrefed when we destroy the struct. */
596 callback_data->callback = callback;
597 callback_data->user_data = user_data;
599 /* TODO: gnome_vfs_volume_get_display_name() does not return
600 * the same string. But why not? Why does hildon needs its own
603 /* printf ("DEBUG: %s Calling hildon_file_system_info_async_new() with URI=%s\n", __FUNCTION__, uri_real); */
604 hildon_file_system_info_async_new(uri_real,
605 on_modest_file_system_info, callback_data /* user_data */);
607 /* g_free (uri_real); */
616 modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session,
617 const gchar* location_filepath)
621 /* Make sure that the directories exist: */
622 modest_init_local_folders (location_filepath);
624 TnyStoreAccount *tny_account;
626 gchar *maildir, *url_string;
628 g_return_val_if_fail (account_mgr, NULL);
629 g_return_val_if_fail (session, NULL);
632 if (!location_filepath) {
633 /* A NULL filepath means that this is the special local-folders maildir
635 tny_account = TNY_STORE_ACCOUNT (modest_tny_local_folders_account_new ());
638 /* Else, for instance, a per-account outbox maildir account: */
639 tny_account = TNY_STORE_ACCOUNT (tny_camel_store_account_new ());
643 g_printerr ("modest: %s: cannot create account for local folders. filepath=%s",
644 __FUNCTION__, location_filepath);
647 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
649 /* This path contains directories for each local folder.
650 * We have created them so that TnyCamelStoreAccount can find them
651 * and report a folder for each directory: */
652 maildir = modest_local_folder_info_get_maildir_path (location_filepath);
653 url = camel_url_new ("maildir:", NULL);
654 camel_url_set_path (url, maildir);
655 /* Needed by tinymail's DBC assertions */
656 camel_url_set_host (url, "localhost");
657 url_string = camel_url_to_string (url, 0);
659 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
660 /* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
662 /* TODO: Use a more generic way of identifying memory card paths,
663 * and of marking accounts as memory card accounts, maybe
664 * via a derived TnyCamelStoreAccount ? */
665 const gboolean is_mmc =
667 (strcmp (location_filepath, MODEST_MCC1_VOLUMEPATH) == 0);
669 /* The name of memory card locations will be updated asynchronously.
670 * This is just a default: */
671 const gchar *name = is_mmc ? _("Memory Card") :
672 MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME;
673 tny_account_set_name (TNY_ACCOUNT(tny_account), name);
675 /* Get the correct display name for memory cards, asynchronously: */
676 if (location_filepath) {
677 GError *error = NULL;
678 gchar *uri = g_filename_to_uri(location_filepath, NULL, &error);
680 g_warning ("%s: g_filename_to_uri(%s) failed: %s", __FUNCTION__,
681 location_filepath, error->message);
682 g_error_free (error);
685 /* Get the account name asynchronously:
686 * This might not happen soon enough, so some UI code might
687 * need to call this again, specifying a callback.
689 modest_tny_account_get_mmc_account_name (tny_account, NULL, NULL);
697 const gchar* id = is_mmc ? MODEST_MMC_ACCOUNT_ID :
698 MODEST_LOCAL_FOLDERS_ACCOUNT_ID;
699 tny_account_set_id (TNY_ACCOUNT(tny_account), id);
701 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
702 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
704 modest_tny_account_set_parent_modest_account_name_for_server_account (
705 TNY_ACCOUNT (tny_account), id);
707 camel_url_free (url);
711 return TNY_ACCOUNT(tny_account);
716 modest_tny_account_new_for_per_account_local_outbox_folder (ModestAccountMgr *account_mgr,
717 const gchar* account_name,
718 TnySessionCamel *session)
720 g_return_val_if_fail (account_mgr, NULL);
721 g_return_val_if_fail (account_name, NULL);
722 g_return_val_if_fail (session, NULL);
724 /* Notice that we create a ModestTnyOutboxAccount here,
725 * instead of just a TnyCamelStoreAccount,
726 * so that we can later identify this as a special account for internal use only.
728 TnyStoreAccount *tny_account = TNY_STORE_ACCOUNT (modest_tny_outbox_account_new ());
730 g_printerr ("modest: cannot create account for per-account local outbox folder.");
734 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
736 /* Make sure that the paths exists on-disk so that TnyCamelStoreAccount can
737 * find it to create a TnyFolder for it: */
738 gchar *folder_dir = modest_per_account_local_outbox_folder_info_get_maildir_path_to_outbox_folder (account_name);
739 modest_init_one_local_folder(folder_dir);
743 /* This path should contain just one directory - "outbox": */
745 modest_per_account_local_outbox_folder_info_get_maildir_path (account_name);
747 CamelURL *url = camel_url_new ("maildir:", NULL);
748 camel_url_set_path (url, maildir);
751 /* Needed by tinymail's DBC assertions */
752 camel_url_set_host (url, "localhost");
753 gchar *url_string = camel_url_to_string (url, 0);
754 camel_url_free (url);
756 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
757 /* printf("DEBUG: %s:\n url=%s\n", __FUNCTION__, url_string); */
760 /* This text should never been seen,
761 * because the per-account outbox accounts are not seen directly by the user.
762 * Their folders are merged and shown as one folder. */
763 tny_account_set_name (TNY_ACCOUNT(tny_account), "Per-Account Outbox");
765 gchar *account_id = g_strdup_printf (
766 MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s",
768 tny_account_set_id (TNY_ACCOUNT(tny_account), account_id);
771 tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy);
772 tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy);
774 /* Make this think that it belongs to the modest local-folders parent account: */
775 modest_tny_account_set_parent_modest_account_name_for_server_account (
776 TNY_ACCOUNT (tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
778 return TNY_ACCOUNT(tny_account);
783 typedef gint (*TnyStatsFunc) (TnyFolderStats *stats);
785 typedef struct _RecurseFoldersHelper {
786 TnyStatsFunc function;
789 } RecurseFoldersHelper;
792 recurse_folders (TnyFolderStore *store,
793 TnyFolderStoreQuery *query,
794 RecurseFoldersHelper *helper)
797 TnyList *folders = tny_simple_list_new ();
799 tny_folder_store_get_folders (store, folders, query, NULL);
800 iter = tny_list_create_iterator (folders);
802 helper->folders += tny_list_get_length (folders);
804 while (!tny_iterator_is_done (iter)) {
805 TnyFolderStats *stats;
808 folder = TNY_FOLDER (tny_iterator_get_current (iter));
809 stats = tny_folder_get_stats (folder);
812 /* initially, we sometimes get -1 from tinymail; ignore that */
813 if (helper->function && helper->function (stats) > 0)
814 helper->sum += helper->function (stats);
816 if (TNY_IS_FOLDER_STORE (folder)) {
817 recurse_folders (TNY_FOLDER_STORE (folder), query, helper);
819 g_object_unref (stats);
821 g_object_unref (folder);
822 tny_iterator_next (iter);
824 g_object_unref (G_OBJECT (iter));
825 g_object_unref (G_OBJECT (folders));
829 modest_tny_folder_store_get_folder_count (TnyFolderStore *self)
831 RecurseFoldersHelper *helper;
834 g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
837 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
838 helper->function = NULL;
842 recurse_folders (self, NULL, helper);
844 retval = helper->folders;
852 modest_tny_folder_store_get_message_count (TnyFolderStore *self)
854 RecurseFoldersHelper *helper;
857 g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
860 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
861 helper->function = (TnyStatsFunc) tny_folder_stats_get_all_count;
864 recurse_folders (self, NULL, helper);
866 retval = helper->sum;
874 modest_tny_folder_store_get_local_size (TnyFolderStore *self)
876 RecurseFoldersHelper *helper;
879 g_return_val_if_fail (TNY_IS_FOLDER_STORE (self), -1);
882 helper = g_malloc0 (sizeof (RecurseFoldersHelper));
883 helper->function = (TnyStatsFunc) tny_folder_stats_get_local_size;
886 recurse_folders (self, NULL, helper);
888 retval = helper->sum;
895 const gchar* modest_tny_account_get_parent_modest_account_name_for_server_account (TnyAccount *self)
897 return (const gchar *)g_object_get_data (G_OBJECT (self), "modest_account");
900 void modest_tny_account_set_parent_modest_account_name_for_server_account (TnyAccount *self, const gchar* parent_modest_acount_name)
902 g_object_set_data_full (G_OBJECT(self), "modest_account",
903 (gpointer) g_strdup (parent_modest_acount_name), g_free);