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.
31 #include <glib/gi18n.h>
33 #include <tny-error.h>
34 #include <tny-account.h>
35 #include <tny-account-store.h>
36 #include <tny-store-account.h>
37 #include <tny-transport-account.h>
38 #include <tny-simple-list.h>
39 #include <tny-account-store.h>
40 #include <tny-camel-transport-account.h>
41 #include <tny-camel-imap-store-account.h>
42 #include <tny-camel-pop-store-account.h>
43 #include "modest-text-utils.h"
44 #include <modest-runtime.h>
45 #include <modest-marshal.h>
46 #include <modest-protocol-registry.h>
47 #include <modest-local-folder-info.h>
48 #include "modest-account-protocol.h"
49 #include <modest-tny-account.h>
50 #include <modest-tny-local-folders-account.h>
51 #include <modest-account-mgr.h>
52 #include <modest-account-mgr-helpers.h>
53 #include <widgets/modest-window-mgr.h>
54 #include <modest-signal-mgr.h>
55 #include <modest-debug.h>
57 #include <modest-defs.h>
58 #include "modest-tny-account-store.h"
59 #include "modest-tny-platform-factory.h"
60 #include <tny-gtk-lockable.h>
61 #include <camel/camel.h>
62 #include <modest-platform.h>
63 #include "modest-ui-actions.h"
64 #include <widgets/modest-account-settings-dialog.h>
66 #ifdef MODEST_PLATFORM_MAEMO
67 #include <tny-maemo-conic-device.h>
68 #include <maemo/modest-maemo-utils.h>
71 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
73 /* 'private'/'protected' functions */
74 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
75 static void modest_tny_account_store_finalize (GObject *obj);
76 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
77 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
78 static void modest_tny_account_store_base_init (gpointer g_class);
80 static void on_account_inserted (ModestAccountMgr *acc_mgr,
84 static void add_existing_accounts (ModestTnyAccountStore *self);
86 static void insert_account (ModestTnyAccountStore *self,
90 static void on_account_removed (ModestAccountMgr *acc_mgr,
94 static gchar* get_password (TnyAccount *account,
95 const gchar * prompt_not_used,
98 static void forget_password (TnyAccount *account);
100 static void on_vfs_volume_mounted (GnomeVFSVolumeMonitor *volume_monitor,
101 GnomeVFSVolume *volume,
104 static void on_vfs_volume_unmounted (GnomeVFSVolumeMonitor *volume_monitor,
105 GnomeVFSVolume *volume,
108 static void forget_password_in_memory (ModestTnyAccountStore *self,
109 const gchar *server_account_name);
111 static void add_connection_specific_transport_accounts (ModestTnyAccountStore *self);
113 static void remove_connection_specific_transport_accounts (ModestTnyAccountStore *self);
115 static inline gboolean only_local_accounts (ModestTnyAccountStore *self);
117 /* list my signals */
119 ACCOUNT_CHANGED_SIGNAL,
120 ACCOUNT_INSERTED_SIGNAL,
121 ACCOUNT_REMOVED_SIGNAL,
123 PASSWORD_REQUESTED_SIGNAL,
127 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
128 struct _ModestTnyAccountStorePrivate {
130 GHashTable *password_hash;
131 ModestAccountMgr *account_mgr;
132 TnySessionCamel *session;
137 /* We cache the lists of accounts here */
138 TnyList *store_accounts;
139 TnyList *transport_accounts;
140 TnyList *store_accounts_outboxes;
142 /* Matches transport accounts and outbox folder */
143 GHashTable *outbox_of_transport;
145 /* is sending mail blocked? */
146 gboolean send_mail_blocked;
149 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
150 MODEST_TYPE_TNY_ACCOUNT_STORE, \
151 ModestTnyAccountStorePrivate))
154 static GObjectClass *parent_class = NULL;
156 static guint signals[LAST_SIGNAL] = {0};
159 modest_tny_account_store_get_type (void)
161 static GType my_type = 0;
164 static const GTypeInfo my_info = {
165 sizeof(ModestTnyAccountStoreClass),
166 modest_tny_account_store_base_init, /* base init */
167 NULL, /* base finalize */
168 (GClassInitFunc) modest_tny_account_store_class_init,
169 NULL, /* class finalize */
170 NULL, /* class data */
171 sizeof(ModestTnyAccountStore),
173 (GInstanceInitFunc) modest_tny_account_store_instance_init,
177 static const GInterfaceInfo iface_info = {
178 (GInterfaceInitFunc) modest_tny_account_store_init,
179 NULL, /* interface_finalize */
180 NULL /* interface_data */
183 my_type = g_type_register_static (G_TYPE_OBJECT,
184 "ModestTnyAccountStore",
186 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
194 modest_tny_account_store_base_init (gpointer g_class)
196 static gboolean tny_account_store_initialized = FALSE;
198 if (!tny_account_store_initialized) {
200 signals[ACCOUNT_CHANGED_SIGNAL] =
201 g_signal_new ("account_changed",
202 MODEST_TYPE_TNY_ACCOUNT_STORE,
204 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_changed),
206 g_cclosure_marshal_VOID__OBJECT,
207 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
209 signals[ACCOUNT_INSERTED_SIGNAL] =
210 g_signal_new ("account_inserted",
211 MODEST_TYPE_TNY_ACCOUNT_STORE,
213 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_inserted),
215 g_cclosure_marshal_VOID__OBJECT,
216 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
218 signals[ACCOUNT_REMOVED_SIGNAL] =
219 g_signal_new ("account_removed",
220 MODEST_TYPE_TNY_ACCOUNT_STORE,
222 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_removed),
224 g_cclosure_marshal_VOID__OBJECT,
225 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
227 signals[PASSWORD_REQUESTED_SIGNAL] =
228 g_signal_new ("password_requested",
229 MODEST_TYPE_TNY_ACCOUNT_STORE,
231 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
233 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
234 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
237 tny_account_store_initialized = TRUE;
243 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
245 GObjectClass *gobject_class;
246 gobject_class = (GObjectClass*) klass;
248 parent_class = g_type_class_peek_parent (klass);
249 gobject_class->finalize = modest_tny_account_store_finalize;
251 g_type_class_add_private (gobject_class,
252 sizeof(ModestTnyAccountStorePrivate));
256 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
258 GnomeVFSVolumeMonitor* monitor = NULL;
259 ModestTnyAccountStorePrivate *priv;
261 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
263 priv->cache_dir = NULL;
264 priv->account_mgr = NULL;
265 priv->session = NULL;
267 priv->sighandlers = NULL;
268 priv->send_mail_blocked = FALSE;
270 priv->outbox_of_transport = g_hash_table_new_full (g_direct_hash,
275 /* An in-memory store of passwords,
276 * for passwords that are not remembered in the configuration,
277 * so they need to be asked for from the user once in each session:
279 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
282 /* Respond to volume mounts and unmounts, such
283 * as the insertion/removal of the memory card: */
284 /* This is a singleton, so it does not need to be unrefed. */
285 monitor = gnome_vfs_get_volume_monitor();
287 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
290 G_CALLBACK(on_vfs_volume_mounted),
292 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
293 G_OBJECT(monitor), "volume-unmounted",
294 G_CALLBACK(on_vfs_volume_unmounted),
298 /* disconnect the list of TnyAccounts */
300 account_verify_last_ref (TnyAccount *account, const gchar *str)
304 g_return_if_fail (account && TNY_IS_ACCOUNT(account));
306 txt = g_strdup_printf ("%s: %s", str ? str : "?", tny_account_get_name(account));
307 MODEST_DEBUG_VERIFY_OBJECT_LAST_REF(G_OBJECT(account),txt);
312 foreach_account_append_to_list (gpointer data,
317 list = TNY_LIST (user_data);
318 tny_list_append (list, G_OBJECT (data));
321 /********************************************************************/
322 /* Control the state of the MMC local account */
323 /********************************************************************/
325 /** Only call this if the memory card is really mounted.
328 add_mmc_account(ModestTnyAccountStore *self, gboolean emit_insert_signal)
330 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
331 g_return_if_fail (priv->session);
333 TnyAccount *mmc_account = modest_tny_account_new_for_local_folders (priv->account_mgr,
335 g_getenv (MODEST_MMC1_VOLUMEPATH_ENV));
337 /* Add to the list of store accounts */
338 tny_list_append (priv->store_accounts, G_OBJECT (mmc_account));
340 if (emit_insert_signal) {
341 g_signal_emit (G_OBJECT (self),
342 signals [ACCOUNT_INSERTED_SIGNAL],
347 g_object_unref (mmc_account);
351 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
352 GnomeVFSVolume *volume,
355 ModestTnyAccountStore *self;
356 ModestTnyAccountStorePrivate *priv;
357 gchar *volume_path_uri;
361 self = MODEST_TNY_ACCOUNT_STORE(user_data);
362 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
364 /* Check whether this was the external MMC1 card: */
365 uri = gnome_vfs_volume_get_activation_uri (volume);
367 volume_path_uri = g_strconcat (MODEST_MMC1_VOLUMEPATH_URI_PREFIX,
368 g_getenv (MODEST_MMC1_VOLUMEPATH_ENV),
370 if (uri && (!strcmp (uri, volume_path_uri))) {
371 add_mmc_account (self, TRUE /* emit the insert signal. */);
374 g_free (volume_path_uri);
379 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
380 GnomeVFSVolume *volume,
383 ModestTnyAccountStore *self;
384 ModestTnyAccountStorePrivate *priv;
386 gchar *volume_path_uri;
388 self = MODEST_TNY_ACCOUNT_STORE(user_data);
389 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
391 /* Check whether this was the external MMC1 card: */
392 uri = gnome_vfs_volume_get_activation_uri (volume);
393 volume_path_uri = g_strconcat (MODEST_MMC1_VOLUMEPATH_URI_PREFIX,
394 g_getenv (MODEST_MMC1_VOLUMEPATH_ENV),
396 if (uri && (strcmp (uri, volume_path_uri) == 0)) {
397 TnyAccount *mmc_account = NULL;
398 gboolean found = FALSE;
399 TnyIterator *iter = NULL;
401 iter = tny_list_create_iterator (priv->store_accounts);
402 while (!tny_iterator_is_done (iter) && !found) {
405 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
406 if (modest_tny_account_is_memory_card_account (account)) {
408 mmc_account = g_object_ref (account);
410 g_object_unref (account);
411 tny_iterator_next (iter);
413 g_object_unref (iter);
416 /* Remove from the list */
417 tny_list_remove (priv->store_accounts, G_OBJECT (mmc_account));
419 /* Notify observers */
420 g_signal_emit (G_OBJECT (self),
421 signals [ACCOUNT_REMOVED_SIGNAL],
424 g_object_unref (mmc_account);
426 g_warning ("%s: there was no store account for the unmounted MMC",
430 g_free (volume_path_uri);
435 * forget_password_in_memory
436 * @self: a TnyAccountStore instance
437 * @account: A server account.
439 * Forget any password stored in memory for this account.
440 * For instance, this should be called when the user has changed the password in the account settings.
443 forget_password_in_memory (ModestTnyAccountStore *self,
444 const gchar * server_account_name)
446 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
448 if (server_account_name && priv->password_hash) {
449 g_hash_table_remove (priv->password_hash, server_account_name);
454 on_account_changed (ModestAccountMgr *acc_mgr,
455 const gchar *account_name,
456 TnyAccountType account_type,
459 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
460 ModestTnyAccountStorePrivate *priv;
461 TnyList* account_list;
462 gboolean found = FALSE;
463 TnyIterator *iter = NULL;
465 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
466 account_list = (account_type == TNY_ACCOUNT_TYPE_STORE ?
467 priv->store_accounts :
468 priv->transport_accounts);
470 iter = tny_list_create_iterator (account_list);
471 while (!tny_iterator_is_done (iter) && !found) {
472 TnyAccount *tny_account;
473 tny_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
475 if (!strcmp (tny_account_get_id (tny_account), account_name)) {
477 modest_tny_account_update_from_account (tny_account, get_password, forget_password);
478 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, tny_account);
480 g_object_unref (tny_account);
482 tny_iterator_next (iter);
484 g_object_unref (iter);
488 show_password_warning_only (const gchar *msg)
490 /* Show an explanatory temporary banner: */
491 if (modest_window_mgr_get_num_windows (modest_runtime_get_window_mgr ()))
492 modest_platform_information_banner (NULL, NULL, msg);
496 show_wrong_password_dialog (TnyAccount *account)
498 if (g_object_get_data (G_OBJECT (account), "connection_specific") != NULL) {
499 modest_ui_actions_on_smtp_servers (NULL, NULL);
501 ModestAccountProtocol *proto;
502 ModestProtocolType proto_type;
503 const gchar *modest_account_name;
504 modest_account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
507 proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
508 modest_account_name);
509 proto = (ModestAccountProtocol *)
510 modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
513 /* Create and show the dialog */
514 if (proto && MODEST_IS_ACCOUNT_PROTOCOL (proto)) {
515 ModestAccountSettingsDialog *dialog =
516 modest_account_protocol_get_account_settings_dialog (proto, modest_account_name);
517 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog), NULL);
518 gtk_widget_show (GTK_WIDGET (dialog));
521 /* Show an explanatory temporary banner: */
522 modest_platform_information_banner (NULL, NULL, _("mcen_ib_username_pw_incorrect"));
525 /* This callback will be called by Tinymail when it needs the password
526 * from the user or the account settings.
527 * It can also call forget_password() before calling this,
528 * so that we clear wrong passwords out of our account settings.
529 * Note that TnyAccount here will be the server account. */
531 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
533 ModestTnyAccountStore *self = NULL;
534 ModestTnyAccountStorePrivate *priv;
535 gchar *username = NULL;
537 gpointer pwd_ptr = NULL;
538 gboolean already_asked = FALSE;
539 const gchar *server_account_name;
542 g_return_val_if_fail (account, NULL);
545 g_debug ("%s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
548 /* Get a reference to myself */
549 self = MODEST_TNY_ACCOUNT_STORE (g_object_get_data (G_OBJECT(account), "account_store"));
550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
552 /* Ensure that we still have this account. It could happen
553 that a set_online was requested *before* removing an
554 account, and due to tinymail emits the get_password
555 function using a g_idle the account could be actually
556 removed *before* this function was really called */
557 url_string = tny_account_get_url_string (account);
559 TnyAccount *tmp_account;
561 tmp_account = tny_account_store_find_account (TNY_ACCOUNT_STORE (self),
569 g_object_unref (tmp_account);
572 server_account_name = tny_account_get_id (account);
573 if (!server_account_name || !self) {
574 g_warning ("modest: %s: could not retrieve account_store for account %s",
575 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
582 /* This hash map stores passwords, including passwords that are not stored in gconf. */
583 /* Is it in the hash? if it's already there, it must be wrong... */
584 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
585 * type-punned ptrs...*/
586 already_asked = priv->password_hash &&
587 g_hash_table_lookup_extended (priv->password_hash,
590 (gpointer*)&pwd_ptr);
592 g_debug ("%s: Already asked = %d\n", __FUNCTION__, already_asked);
595 /* If the password is not already there, try ModestConf */
596 if (!already_asked) {
597 pwd = modest_account_mgr_get_server_account_password (priv->account_mgr,
598 server_account_name);
599 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
602 /* If it was already asked, it must have been wrong, so ask again */
603 if (already_asked || !pwd || strlen(pwd) == 0) {
604 gboolean settings_have_password;
605 ModestProtocolType protocol_type;
607 /* As per the UI spec, if no password was set in the account settings,
608 * ask for it now. But if the password is wrong in the account settings,
609 * then show a banner and the account settings dialog so it can be corrected:
611 settings_have_password =
612 modest_account_mgr_get_server_account_has_password (priv->account_mgr, server_account_name);
614 protocol_type = modest_tny_account_get_protocol_type (account);
616 /* Show an error and after that ask for a password */
617 if (modest_protocol_registry_protocol_type_has_tag(modest_runtime_get_protocol_registry (),
618 protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
619 gchar *username = NULL, *msg = NULL;
620 username = modest_account_mgr_get_server_account_username (priv->account_mgr,
621 server_account_name);
622 if (!username || strlen(username) == 0) {
623 msg = g_strdup_printf (_("emev_ni_ui_smtp_userid_invalid"),
624 tny_account_get_name (account),
625 tny_account_get_hostname (account));
628 password = modest_account_mgr_get_server_account_password (priv->account_mgr,
629 server_account_name);
632 msg = g_strdup (_("mcen_ib_username_pw_incorrect"));
633 else if (!password || strlen(password) == 0)
634 msg = g_strdup_printf (_("emev_ni_ui_smtp_passwd_invalid"),
635 tny_account_get_name (account),
636 tny_account_get_hostname (account));
638 msg = g_strdup_printf (_("emev_ni_ui_smtp_authentication_fail_error"),
639 tny_account_get_hostname (account));
644 modest_platform_run_information_dialog (NULL, msg, TRUE);
651 if (settings_have_password) {
652 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
653 show_wrong_password_dialog (account);
661 /* we don't have it yet. Get the password from the user */
662 const gchar* account_id = tny_account_get_id (account);
663 gboolean remember = FALSE;
668 gboolean username_known =
669 modest_account_mgr_get_server_account_username_has_succeeded(priv->account_mgr,
670 server_account_name);
671 /* If the login has ever succeeded then show a specific message */
673 msg = _CS ("ecdg_ib_set_password_incorrect");
675 msg = _("mcen_ib_username_pw_incorrect");
676 show_password_warning_only (msg);
679 /* Request password */
680 g_signal_emit (G_OBJECT (self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
681 account_id, /* server_account_name */
682 &username, &pwd, cancel, &remember);
686 /* The password will be returned as the result,
687 * but we need to tell tinymail about the username too: */
689 /* WARNING: I disabled setting username as this can cause locks. Anyway,
690 * as now we have the password dialog username entry always dimmed
691 * this shouldn't be a problem */
694 /* tny_account_set_user (account, username); */
696 /* Do not save the password in gconf, because
697 * the UI spec says "The password will never
698 * be saved in the account": */
700 /* We need to dup the string even knowing that
701 it's already a dup of the contents of an
702 entry, because it if it's wrong, then camel
704 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
706 g_hash_table_remove (priv->password_hash, server_account_name);
721 modest_tny_account_store_forget_already_asked (ModestTnyAccountStore *self, TnyAccount *account)
723 g_return_if_fail (account);
725 ModestTnyAccountStorePrivate *priv;
727 gpointer pwd_ptr = NULL;
728 gboolean already_asked = FALSE;
730 const gchar *server_account_name = tny_account_get_id (account);
732 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
734 /* This hash map stores passwords, including passwords that are not stored in gconf. */
735 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
736 * type-punned ptrs...*/
737 already_asked = priv->password_hash &&
738 g_hash_table_lookup_extended (priv->password_hash,
741 (gpointer*)&pwd_ptr);
744 g_hash_table_remove (priv->password_hash, server_account_name);
752 /* tinymail calls this if the connection failed due to an incorrect password.
753 * And it seems to call this for any general connection failure. */
755 forget_password (TnyAccount *account)
757 ModestTnyAccountStore *self;
758 ModestTnyAccountStorePrivate *priv;
759 const TnyAccountStore *account_store;
763 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
765 self = MODEST_TNY_ACCOUNT_STORE (account_store);
766 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
767 key = tny_account_get_id (account);
769 /* Do not remove the key, this will allow us to detect that we
770 have already asked for it at least once */
771 pwd = g_hash_table_lookup (priv->password_hash, key);
773 memset (pwd, 0, strlen (pwd));
774 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
777 /* Remove from configuration system */
779 modest_account_mgr_unset (priv->account_mgr,
780 key, MODEST_ACCOUNT_PASSWORD, TRUE);
785 modest_tny_account_store_finalize (GObject *obj)
787 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
788 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
790 g_free (priv->cache_dir);
791 priv->cache_dir = NULL;
793 if (priv->password_hash) {
794 g_hash_table_destroy (priv->password_hash);
795 priv->password_hash = NULL;
798 if (priv->outbox_of_transport) {
799 g_hash_table_destroy (priv->outbox_of_transport);
800 priv->outbox_of_transport = NULL;
803 modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
804 priv->sighandlers = NULL;
806 if (priv->account_mgr) {
807 g_object_unref (G_OBJECT(priv->account_mgr));
808 priv->account_mgr = NULL;
812 g_object_unref (G_OBJECT(priv->device));
816 /* Destroy all accounts. Disconnect all accounts before they are destroyed */
817 if (priv->store_accounts) {
818 tny_list_foreach (priv->store_accounts, (GFunc)account_verify_last_ref, "store");
819 g_object_unref (priv->store_accounts);
820 priv->store_accounts = NULL;
823 if (priv->transport_accounts) {
824 tny_list_foreach (priv->transport_accounts, (GFunc)account_verify_last_ref, "transport");
825 g_object_unref (priv->transport_accounts);
826 priv->transport_accounts = NULL;
829 if (priv->store_accounts_outboxes) {
830 g_object_unref (priv->store_accounts_outboxes);
831 priv->store_accounts_outboxes = NULL;
835 camel_object_unref (CAMEL_OBJECT(priv->session));
836 priv->session = NULL;
839 G_OBJECT_CLASS(parent_class)->finalize (obj);
843 volume_path_is_mounted (const gchar* path)
845 g_return_val_if_fail (path, FALSE);
847 gboolean result = FALSE;
848 gchar * path_as_uri = g_filename_to_uri (path, NULL, NULL);
849 g_return_val_if_fail (path_as_uri, FALSE);
851 /* Get the monitor singleton: */
852 GnomeVFSVolumeMonitor *monitor = gnome_vfs_get_volume_monitor();
854 /* This seems like a simpler way to do this, but it returns a
855 * GnomeVFSVolume even if the drive is not mounted: */
857 GnomeVFSVolume *volume = gnome_vfs_volume_monitor_get_volume_for_path (monitor,
858 g_getenv (MODEST_MMC1_VOLUMEPATH_ENV));
860 gnome_vfs_volume_unref(volume);
864 /* Get the mounted volumes from the monitor: */
865 GList *list = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
867 for (iter = list; iter; iter = g_list_next (iter)) {
868 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
872 gnome_vfs_volume_get_display_name (volume);
873 printf ("volume display name=%s\n", display_name);
874 g_free (display_name);
878 gnome_vfs_volume_get_activation_uri (volume);
879 /* printf (" uri=%s\n", uri); */
880 if (uri && (strcmp (uri, path_as_uri) == 0))
885 gnome_vfs_volume_unref (volume);
891 g_free (path_as_uri);
896 ModestTnyAccountStore*
897 modest_tny_account_store_new (ModestAccountMgr *account_mgr,
901 ModestTnyAccountStorePrivate *priv;
902 TnyAccount *local_account = NULL;
903 TnyLockable *lockable;
905 g_return_val_if_fail (account_mgr, NULL);
906 g_return_val_if_fail (device, NULL);
908 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
909 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
911 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
912 priv->device = g_object_ref (device);
914 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
915 if (!priv->session) {
916 g_warning ("failed to get TnySessionCamel");
920 /* Set the ui locker */
921 lockable = tny_gtk_lockable_new ();
922 tny_session_camel_set_ui_locker (priv->session, lockable);
923 g_object_unref (lockable);
925 /* Connect signals */
926 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
927 G_OBJECT(account_mgr), "account_inserted",
928 G_CALLBACK (on_account_inserted), obj);
929 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
930 G_OBJECT(account_mgr), "account_changed",
931 G_CALLBACK (on_account_changed), obj);
932 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
933 G_OBJECT(account_mgr), "account_removed",
934 G_CALLBACK (on_account_removed), obj);
936 /* Create the lists of accounts */
937 priv->store_accounts = tny_simple_list_new ();
938 priv->transport_accounts = tny_simple_list_new ();
939 priv->store_accounts_outboxes = tny_simple_list_new ();
941 /* Create the local folders account */
943 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session, NULL);
944 tny_list_append (priv->store_accounts, G_OBJECT(local_account));
945 g_object_unref (local_account);
947 /* Add the other remote accounts. Do this after adding the
948 local account, because we need to add our outboxes to the
949 global OUTBOX hosted in the local account */
950 add_existing_accounts (MODEST_TNY_ACCOUNT_STORE (obj));
952 /* Add connection-specific transport accounts if there are any
953 accounts available */
954 if (!only_local_accounts (MODEST_TNY_ACCOUNT_STORE(obj)))
955 add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE(obj));
957 /* This is a singleton, so it does not need to be unrefed. */
958 if (volume_path_is_mounted (g_getenv (MODEST_MMC1_VOLUMEPATH_ENV))) {
960 add_mmc_account (MODEST_TNY_ACCOUNT_STORE (obj), FALSE /* don't emit the insert signal. */);
963 return MODEST_TNY_ACCOUNT_STORE(obj);
967 modest_tny_account_store_get_accounts (TnyAccountStore *self,
969 TnyGetAccountsRequestType request_type)
971 ModestTnyAccountStorePrivate *priv;
973 g_return_if_fail (self);
974 g_return_if_fail (TNY_IS_LIST(list));
976 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
978 switch (request_type) {
979 case TNY_ACCOUNT_STORE_BOTH:
980 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
981 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
983 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
984 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
986 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
987 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
990 g_return_if_reached ();
993 /* Initialize session. Why do we need this ??? */
994 tny_session_camel_set_initialized (priv->session);
999 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
1001 ModestTnyAccountStorePrivate *priv;
1002 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1004 if (!priv->cache_dir)
1005 priv->cache_dir = g_build_filename (g_get_home_dir(),
1006 MODEST_DIR, MODEST_CACHE_DIR, NULL);
1007 return priv->cache_dir;
1012 * callers need to unref
1015 modest_tny_account_store_get_device (TnyAccountStore *self)
1017 ModestTnyAccountStorePrivate *priv;
1019 g_return_val_if_fail (self, NULL);
1021 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1024 return g_object_ref (G_OBJECT(priv->device));
1031 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1033 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1034 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1041 modest_tny_account_store_alert (TnyAccountStore *self,
1042 TnyAccount *account,
1047 ModestProtocolType protocol_type = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1048 ModestProtocol *protocol = NULL;
1049 const gchar* server_name = "";
1050 gchar *prompt = NULL;
1051 gboolean retval = TRUE;
1053 /* NOTE: account may be NULL in some cases */
1054 g_return_val_if_fail (error, FALSE);
1056 /* Get the server name: */
1058 server_name = tny_account_get_hostname (account);
1059 protocol_type = modest_tny_account_get_protocol_type (account);
1060 if (protocol_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID){
1061 g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__,
1062 tny_account_get_id (account));
1065 protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
1069 switch (error->code) {
1070 case TNY_SYSTEM_ERROR_CANCEL:
1071 /* Don't show waste the user's time by showing him a dialog telling
1072 * him that he has just cancelled something: */
1075 case TNY_SERVICE_ERROR_PROTOCOL:
1076 /* Like a BAD from IMAP (protocol error) */
1077 case TNY_SERVICE_ERROR_LOST_CONNECTION:
1078 /* Lost the connection with the service */
1079 case TNY_SERVICE_ERROR_UNAVAILABLE:
1080 /* You must be working online for this operation */
1081 case TNY_SERVICE_ERROR_CONNECT:
1083 prompt = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_CONNECT_ERROR, server_name);
1086 g_return_val_if_reached (FALSE);
1090 case TNY_SERVICE_ERROR_AUTHENTICATE:
1091 /* It seems that there's no better error to show with
1092 * POP and IMAP because TNY_SERVICE_ERROR_AUTHENTICATE
1093 * may appear if there's a timeout during auth */
1095 prompt = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_AUTH_ERROR, server_name);
1098 g_return_val_if_reached (FALSE);
1102 case TNY_SERVICE_ERROR_CERTIFICATE:
1103 /* We'll show the proper dialog later */
1106 case TNY_SYSTEM_ERROR_MEMORY:
1107 /* Can't allocate memory for this operation */
1109 case TNY_SERVICE_ERROR_UNKNOWN:
1112 g_debug ("Unexpected error %d", error->code);
1113 g_return_val_if_reached (FALSE);
1117 if (error->code == TNY_SERVICE_ERROR_CERTIFICATE)
1118 retval = modest_platform_run_certificate_confirmation_dialog (server_name,
1120 else if (error->code == TNY_SERVICE_ERROR_AUTHENTICATE ||
1121 error->code == TNY_SERVICE_ERROR_CONNECT) {
1123 modest_platform_run_information_dialog (NULL, prompt, TRUE);
1125 /* Show the account dialog if it was wrong */
1126 if (error->code == TNY_SERVICE_ERROR_AUTHENTICATE)
1127 show_wrong_password_dialog (account);
1132 g_debug ("%s: error code %d (%s", __FUNCTION__, error->code, error->message);
1142 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1144 TnyAccountStoreIface *klass;
1146 g_return_if_fail (g);
1148 klass = (TnyAccountStoreIface *)g;
1150 klass->get_accounts =
1151 modest_tny_account_store_get_accounts;
1152 klass->get_cache_dir =
1153 modest_tny_account_store_get_cache_dir;
1155 modest_tny_account_store_get_device;
1157 modest_tny_account_store_alert;
1158 klass->find_account =
1159 modest_tny_account_store_find_account_by_url;
1163 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1164 ModestTnyGetPassFunc func)
1166 /* not implemented, we use signals */
1167 g_printerr ("modest: set_get_pass_func not implemented\n");
1171 modest_tny_account_store_get_session (TnyAccountStore *self)
1173 g_return_val_if_fail (self, NULL);
1174 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1178 get_tny_account_by (TnyList *accounts,
1179 ModestTnyAccountStoreQueryType type,
1182 TnyIterator *iter = NULL;
1183 gboolean found = FALSE;
1184 TnyAccount *retval = NULL;
1186 g_return_val_if_fail (TNY_IS_LIST(accounts), NULL);
1188 if (tny_list_get_length(accounts) == 0) {
1189 g_warning ("%s: account list is empty", __FUNCTION__);
1193 iter = tny_list_create_iterator (accounts);
1194 while (!tny_iterator_is_done (iter) && !found) {
1195 TnyAccount *tmp_account = NULL;
1196 const gchar *val = NULL;
1198 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1199 if (!TNY_IS_ACCOUNT(tmp_account)) {
1200 g_warning ("%s: not a valid account", __FUNCTION__);
1206 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1207 val = tny_account_get_id (tmp_account);
1209 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1210 val = tny_account_get_url_string (tmp_account);
1214 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1215 tny_account_matches_url_string (tmp_account, str)) {
1216 retval = g_object_ref (tmp_account);
1219 if (val && str && strcmp (val, str) == 0) {
1220 retval = g_object_ref (tmp_account);
1224 g_object_unref (tmp_account);
1225 tny_iterator_next (iter);
1227 g_object_unref (iter);
1233 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1234 ModestTnyAccountStoreQueryType type,
1237 TnyAccount *account = NULL;
1238 ModestTnyAccountStorePrivate *priv;
1240 g_return_val_if_fail (self, NULL);
1241 g_return_val_if_fail (str, NULL);
1243 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1245 /* Search in store accounts */
1246 account = get_tny_account_by (priv->store_accounts, type, str);
1248 /* If we already found something, no need to search the transport accounts */
1250 account = get_tny_account_by (priv->transport_accounts, type, str);
1252 /* If we already found something, no need to search the
1253 per-account outbox accounts */
1255 account = get_tny_account_by (priv->store_accounts_outboxes, type, str);
1258 /* Warn if nothing was found. This is generally unusual. */
1260 g_warning("%s: Failed to find account with %s=%s\n",
1262 (type == MODEST_TNY_ACCOUNT_STORE_QUERY_ID) ? "ID" : "URL",
1266 /* Returns a new reference to the account if found */
1272 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1273 const gchar *account_name,
1274 TnyAccountType type)
1276 ModestTnyAccountStorePrivate *priv = NULL;
1277 TnyAccount *retval = NULL;
1278 TnyList *account_list = NULL;
1279 TnyIterator *iter = NULL;
1282 g_return_val_if_fail (self, NULL);
1283 g_return_val_if_fail (account_name, NULL);
1284 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE ||
1285 type == TNY_ACCOUNT_TYPE_TRANSPORT,
1288 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1290 account_list = (type == TNY_ACCOUNT_TYPE_STORE) ?
1291 priv->store_accounts :
1292 priv->transport_accounts;
1294 if (!account_list) {
1295 g_printerr ("%s: No server accounts of type %s\n", __FUNCTION__,
1296 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport");
1300 /* Look for the server account */
1302 iter = tny_list_create_iterator (account_list);
1303 while (!tny_iterator_is_done (iter) && !found) {
1304 const gchar *modest_acc_name;
1305 TnyAccount *tmp_account;
1307 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1309 modest_tny_account_get_parent_modest_account_name_for_server_account (tmp_account);
1311 if (account_name && modest_acc_name && !strcmp (account_name, modest_acc_name)) {
1313 retval = g_object_ref (tmp_account);
1315 /* Free and continue */
1316 g_object_unref (tmp_account);
1317 tny_iterator_next (iter);
1319 g_object_unref (iter);
1322 g_printerr ("modest: %s: could not get tny %s account for %s\n." \
1323 "Number of server accounts of this type=%d\n", __FUNCTION__,
1324 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport",
1325 account_name, tny_list_get_length (account_list));
1328 /* Returns a new reference */
1333 modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (
1334 ModestTnyAccountStore *self, const gchar *account_name)
1338 g_return_val_if_fail (self && MODEST_IS_TNY_ACCOUNT_STORE(self), NULL);
1339 g_return_val_if_fail (account_name, NULL);
1341 /* Get the current connection: */
1342 device = modest_runtime_get_device ();
1345 g_warning ("%s: could not get device", __FUNCTION__);
1349 if (!tny_device_is_online (device))
1352 #ifdef MODEST_HAVE_CONIC
1353 g_return_val_if_fail (TNY_IS_MAEMO_CONIC_DEVICE (device), NULL);
1355 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1356 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1357 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1361 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1365 const gchar *connection_id = con_ic_iap_get_id (connection);
1366 /* printf ("DEBUG: %s: connection_id=%s\n", __FUNCTION__, connection_id); */
1370 /* Get the connection-specific transport acccount, if any: */
1371 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1373 /* Check if this account has connection-specific SMTP enabled */
1374 if (!modest_account_mgr_get_use_connection_specific_smtp (account_manager, account_name)) {
1378 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1381 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1382 if (!server_account_name) {
1383 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1386 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1387 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1388 server_account_name);
1390 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1391 g_free (server_account_name);
1393 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1394 g_object_unref (connection);
1398 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1399 #endif /* MODEST_HAVE_CONIC */
1404 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1405 const gchar *account_name)
1407 g_return_val_if_fail (self, NULL);
1408 g_return_val_if_fail (account_name, NULL);
1410 if (!account_name || !self)
1413 /* Get the connection-specific transport acccount, if any: */
1414 /* Note: This gives us a reference: */
1415 TnyAccount *account =
1416 modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (self, account_name);
1418 /* If there is no connection-specific transport account (the common case),
1419 * just get the regular transport account: */
1421 /* The special local folders don't have transport accounts. */
1422 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1425 /* Note: This gives us a reference: */
1426 account = modest_tny_account_store_get_server_account (self, account_name,
1427 TNY_ACCOUNT_TYPE_TRANSPORT);
1431 /* returns a reference. */
1436 modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
1438 TnyAccount *account = NULL;
1439 ModestTnyAccountStorePrivate *priv;
1443 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1445 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1448 iter = tny_list_create_iterator (priv->store_accounts);
1449 while (!tny_iterator_is_done (iter) && !found) {
1450 TnyAccount *tmp_account;
1452 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1453 if (modest_tny_account_is_virtual_local_folders (tmp_account)) {
1454 account = g_object_ref (tmp_account);
1457 g_object_unref (tmp_account);
1458 tny_iterator_next (iter);
1460 g_object_unref (iter);
1462 /* Returns a new reference to the account */
1467 modest_tny_account_store_get_mmc_folders_account (ModestTnyAccountStore *self)
1469 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1472 return modest_tny_account_store_get_tny_account_by (self, MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1473 MODEST_MMC_ACCOUNT_ID);
1477 /*********************************************************************************/
1479 add_existing_accounts (ModestTnyAccountStore *self)
1481 GSList *account_names = NULL, *iter = NULL;
1482 ModestTnyAccountStorePrivate *priv = NULL;
1484 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1486 /* These are account names, not server_account names */
1487 account_names = modest_account_mgr_account_names (priv->account_mgr, FALSE);
1489 for (iter = account_names; iter != NULL; iter = g_slist_next (iter)) {
1490 const gchar *account_name = (const gchar*) iter->data;
1492 /* Insert all enabled accounts without notifying */
1493 if (modest_account_mgr_get_enabled (priv->account_mgr, account_name))
1494 insert_account (self, account_name, FALSE);
1496 modest_account_mgr_free_account_names (account_names);
1500 create_tny_account (ModestTnyAccountStore *self,
1502 TnyAccountType type,
1505 TnyAccount *account = NULL;
1506 ModestTnyAccountStorePrivate *priv = NULL;
1508 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1510 account = modest_tny_account_new_from_account (priv->account_mgr,
1517 /* Forget any cached password for the account, so that
1518 we use a new account if any */
1519 forget_password_in_memory (self, tny_account_get_id (account));
1521 /* Set the account store */
1522 g_object_set_data (G_OBJECT(account), "account_store", self);
1524 g_printerr ("modest: failed to create account for %s\n", name);
1530 typedef struct _AddOutboxInfo {
1531 ModestTnyAccountStore *account_store;
1532 TnyAccount *transport_account;
1536 add_outbox_from_transport_account_to_global_outbox_get_folders_cb (TnyFolderStore *folder_store,
1542 TnyIterator *iter_folders;
1543 TnyFolder *per_account_outbox;
1544 TnyAccount *local_account = NULL;
1545 AddOutboxInfo *info = (AddOutboxInfo *) userdata;
1546 ModestTnyAccountStorePrivate *priv = NULL;
1547 ModestTnyAccountStore *self;
1549 self = MODEST_TNY_ACCOUNT_STORE (info->account_store);
1550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1552 /* Note that this could happen if there is not enough space
1553 available on disk, then the outbox folder could not be
1555 if (tny_list_get_length (list) != 1) {
1556 g_warning ("%s: could not create outbox folder (%d folders found)", __FUNCTION__,
1557 tny_list_get_length (list));
1561 iter_folders = tny_list_create_iterator (list);
1562 per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
1563 g_object_unref (iter_folders);
1564 g_object_unref (list);
1566 /* Add the outbox of the new per-account-local-outbox account
1567 to the global local merged OUTBOX of the local folders
1569 local_account = modest_tny_account_store_get_local_folders_account (info->account_store);
1570 modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1571 per_account_outbox);
1572 /* Add the pair to the hash table */
1573 g_hash_table_insert (priv->outbox_of_transport,
1574 info->transport_account,
1575 per_account_outbox);
1577 /* Notify that the local account changed */
1578 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1579 g_object_unref (local_account);
1580 g_object_unref (per_account_outbox);
1583 g_object_unref (info->transport_account);
1584 g_slice_free (AddOutboxInfo, info);
1589 add_outbox_from_transport_account_to_global_outbox (ModestTnyAccountStore *self,
1590 const gchar *account_name,
1591 TnyAccount *transport_account)
1593 TnyList *folders = NULL;
1594 TnyAccount *account_outbox = NULL;
1595 ModestTnyAccountStorePrivate *priv = NULL;
1596 AddOutboxInfo *info;
1598 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1600 /* Create per account local outbox */
1602 modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr,
1606 if (!G_IS_OBJECT (account_outbox)) {
1607 g_warning ("%s: could not create per account local outbox folder", __FUNCTION__);
1611 tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
1613 /* Get the outbox folder */
1614 folders = tny_simple_list_new ();
1615 info = g_slice_new0 (AddOutboxInfo);
1616 info->account_store = self;
1617 info->transport_account = g_object_ref (transport_account);
1618 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account_outbox), folders, NULL, FALSE,
1619 add_outbox_from_transport_account_to_global_outbox_get_folders_cb, NULL, (gpointer) info);
1620 g_object_unref (account_outbox);
1624 * This function will be used for both adding new accounts and for the
1625 * initialization. In the initialization we do not want to emit
1626 * signals so notify will be FALSE, in the case of account additions
1627 * we do want to notify the observers
1630 insert_account (ModestTnyAccountStore *self,
1631 const gchar *account,
1634 ModestTnyAccountStorePrivate *priv = NULL;
1635 TnyAccount *store_account = NULL, *transport_account = NULL;
1637 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1639 /* Get the server and the transport account */
1640 store_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_STORE, notify);
1641 if (!store_account || !TNY_IS_ACCOUNT(store_account)) {
1642 g_warning ("%s: failed to create store account", __FUNCTION__);
1646 transport_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT, notify);
1647 if (!transport_account || !TNY_IS_ACCOUNT(transport_account)) {
1648 g_warning ("%s: failed to create transport account", __FUNCTION__);
1649 g_object_unref (store_account);
1653 /* Add accounts to the lists */
1654 tny_list_append (priv->store_accounts, G_OBJECT (store_account));
1655 tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
1657 /* Create a new pseudo-account with an outbox for this
1658 transport account and add it to the global outbox
1659 in the local account */
1660 add_outbox_from_transport_account_to_global_outbox (self, account, transport_account);
1662 /* Notify the observers. We do it after everything is
1665 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
1666 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
1670 g_object_unref (store_account);
1671 g_object_unref (transport_account);
1674 static inline gboolean
1675 only_local_accounts (ModestTnyAccountStore *self)
1677 return (modest_tny_account_store_get_num_remote_accounts (self) > 0) ? FALSE : TRUE;
1681 on_account_inserted (ModestAccountMgr *acc_mgr,
1682 const gchar *account,
1685 gboolean add_specific;
1687 add_specific = only_local_accounts (MODEST_TNY_ACCOUNT_STORE (user_data));
1689 /* Insert the account and notify the observers */
1690 insert_account (MODEST_TNY_ACCOUNT_STORE (user_data), account, TRUE);
1692 /* If it's the first remote account then add the connection
1693 specific SMTP servers as well */
1695 add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE (user_data));
1699 /* This is the callback of the tny_camel_account_set_online called in
1700 on_account_removed to disconnect the account */
1702 on_account_disconnect_when_removing (TnyCamelAccount *account,
1707 ModestTnyAccountStore *self;
1708 ModestTnyAccountStorePrivate *priv;
1710 self = MODEST_TNY_ACCOUNT_STORE (user_data);
1711 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1713 /* Cancel all pending operations */
1714 tny_account_cancel (TNY_ACCOUNT (account));
1716 /* Unref the extra reference added by get_server_account */
1717 g_object_unref (account);
1719 /* Clear the cache if it's an store account */
1720 if (TNY_IS_STORE_ACCOUNT (account)) {
1721 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (account));
1722 } else if (TNY_IS_TRANSPORT_ACCOUNT (account)) {
1723 ModestTnySendQueue* send_queue;
1724 send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (account), FALSE);
1725 if (TNY_IS_SEND_QUEUE (send_queue)) {
1726 if (modest_tny_send_queue_sending_in_progress (send_queue))
1727 tny_send_queue_cancel (TNY_SEND_QUEUE (send_queue),
1728 TNY_SEND_QUEUE_CANCEL_ACTION_REMOVE,
1730 modest_runtime_remove_send_queue (TNY_TRANSPORT_ACCOUNT (account));
1736 * We use this one for both removing "normal" and "connection
1737 * specific" transport accounts
1740 remove_transport_account (ModestTnyAccountStore *self,
1741 TnyTransportAccount *transport_account)
1743 ModestTnyAccountStorePrivate *priv;
1744 TnyFolder *outbox = NULL;
1746 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1748 /* Remove it from the list of accounts and notify the
1749 observers. Do not need to wait for account
1751 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
1752 tny_list_remove (priv->transport_accounts, (GObject *) transport_account);
1754 /* Remove the OUTBOX of the account from the global outbox */
1755 outbox = g_hash_table_lookup (priv->outbox_of_transport, transport_account);
1757 if (TNY_IS_FOLDER (outbox)) {
1758 TnyAccount *local_account = NULL;
1759 TnyAccount *outbox_account = tny_folder_get_account (outbox);
1761 if (outbox_account) {
1762 tny_list_remove (priv->store_accounts_outboxes, G_OBJECT (outbox_account));
1763 /* Remove existing emails to send */
1764 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (outbox_account));
1765 g_object_unref (outbox_account);
1768 local_account = modest_tny_account_store_get_local_folders_account (self);
1769 modest_tny_local_folders_account_remove_folder_from_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1772 g_hash_table_remove (priv->outbox_of_transport, transport_account);
1774 /* Notify the change in the local account */
1775 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1776 g_object_unref (local_account);
1778 g_warning ("Removing a transport account that has no outbox");
1781 /* Cancel all pending operations */
1782 tny_account_cancel (TNY_ACCOUNT (transport_account));
1784 /* Disconnect and notify the observers. The callback will free the reference */
1785 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account), FALSE,
1786 on_account_disconnect_when_removing, self);
1790 images_cache_remove_filter (TnyStreamCache *self, const gchar *id, const gchar *account_name)
1792 gchar *account_name_with_separator;
1794 if (account_name == NULL || account_name[0] == '\0')
1797 if (id == NULL || id[0] == '\0')
1800 account_name_with_separator = g_strconcat (account_name, "__", NULL);
1802 result = (g_str_has_prefix (id, account_name));
1803 g_free (account_name_with_separator);
1809 on_account_removed (ModestAccountMgr *acc_mgr,
1810 const gchar *account,
1813 TnyAccount *store_account = NULL, *transport_account = NULL;
1814 ModestTnyAccountStore *self;
1815 ModestTnyAccountStorePrivate *priv;
1816 TnyStreamCache *stream_cache;
1818 self = MODEST_TNY_ACCOUNT_STORE (user_data);
1819 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1821 /* Get the server and the transport account */
1823 modest_tny_account_store_get_server_account (self, account,
1824 TNY_ACCOUNT_TYPE_STORE);
1826 modest_tny_account_store_get_server_account (self, account,
1827 TNY_ACCOUNT_TYPE_TRANSPORT);
1829 /* If there was any problem creating the account, for example,
1830 with the configuration system this could not exist */
1831 if (TNY_IS_STORE_ACCOUNT(store_account)) {
1832 /* Forget any cached password for the account */
1833 forget_password_in_memory (self, tny_account_get_id (store_account));
1835 /* Remove it from the list of accounts and notify the
1836 observers. Do not need to wait for account
1838 tny_list_remove (priv->store_accounts, (GObject *) store_account);
1839 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
1841 /* Cancel all pending operations */
1842 tny_account_cancel (TNY_ACCOUNT (store_account));
1844 /* Disconnect before deleting the cache, because the
1845 disconnection will rewrite the cache to the
1847 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account), FALSE,
1848 on_account_disconnect_when_removing, self);
1850 g_warning ("%s: no store account for account %s\n",
1851 __FUNCTION__, account);
1854 /* If there was any problem creating the account, for example,
1855 with the configuration system this could not exist */
1856 if (TNY_IS_TRANSPORT_ACCOUNT(transport_account)) {
1858 /* Forget any cached password for the account */
1859 forget_password_in_memory (self, tny_account_get_id (transport_account));
1861 /* Remove transport account. It'll free the reference
1862 added by get_server_account */
1863 remove_transport_account (self, TNY_TRANSPORT_ACCOUNT (transport_account));
1865 g_warning ("%s: no transport account for account %s\n",
1866 __FUNCTION__, account);
1869 /* Remove cached images */
1870 stream_cache = modest_runtime_get_images_cache ();
1871 tny_stream_cache_remove (stream_cache, (TnyStreamCacheRemoveFilter) images_cache_remove_filter, (gpointer) account);
1873 /* If there are no more user accounts then delete the
1874 transport specific SMTP servers */
1875 if (only_local_accounts (self))
1876 remove_connection_specific_transport_accounts (self);
1879 TnyTransportAccount *
1880 modest_tny_account_store_new_connection_specific_transport_account (ModestTnyAccountStore *self,
1883 ModestTnyAccountStorePrivate *priv = NULL;
1884 TnyAccount * tny_account = NULL;
1886 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1888 /* Add the account: */
1890 modest_tny_account_new_from_server_account_name (priv->account_mgr,
1896 g_object_set_data (G_OBJECT(tny_account),
1899 g_object_set_data (G_OBJECT(tny_account),
1900 "connection_specific",
1901 GINT_TO_POINTER (TRUE));
1903 tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
1904 add_outbox_from_transport_account_to_global_outbox (self,
1909 g_printerr ("modest: failed to create smtp-specific account for %s\n",
1912 return TNY_TRANSPORT_ACCOUNT (tny_account);
1916 foreach_free_string(gpointer data,
1923 add_connection_specific_transport_accounts (ModestTnyAccountStore *self)
1925 ModestTnyAccountStorePrivate *priv = NULL;
1926 GSList *list_specifics = NULL, *iter = NULL;
1929 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1931 list_specifics = modest_conf_get_list (modest_runtime_get_conf (),
1932 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
1933 MODEST_CONF_VALUE_STRING, &err);
1936 g_return_if_reached ();
1940 /* Look at each connection-specific transport account for the
1941 * modest account: */
1942 iter = list_specifics;
1944 /* The list alternates between the connection name and the transport name: */
1945 iter = g_slist_next (iter);
1947 const gchar* transport_account_name = (const gchar*) (iter->data);
1948 TnyTransportAccount * account = NULL;
1949 account = modest_tny_account_store_new_connection_specific_transport_account (
1950 self, transport_account_name);
1952 g_object_unref (account);
1954 iter = g_slist_next (iter);
1958 g_slist_foreach (list_specifics, foreach_free_string, NULL);
1959 g_slist_free (list_specifics);
1963 remove_connection_specific_transport_accounts (ModestTnyAccountStore *self)
1965 ModestTnyAccountStorePrivate *priv = NULL;
1966 GSList *list_specifics = NULL, *iter = NULL;
1969 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1972 list_specifics = modest_conf_get_list (modest_runtime_get_conf (),
1973 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
1974 MODEST_CONF_VALUE_STRING, &err);
1977 g_return_if_reached ();
1981 /* Look at each connection-specific transport account for the
1982 * modest account: */
1983 iter = list_specifics;
1985 /* The list alternates between the connection name and the transport name: */
1986 iter = g_slist_next (iter);
1988 const gchar* transport_account_name = (const gchar*) (iter->data);
1989 TnyAccount * account;
1990 account = modest_tny_account_store_get_server_account (self,
1991 transport_account_name,
1992 TNY_ACCOUNT_TYPE_TRANSPORT);
1994 /* the call will free the reference */
1996 remove_transport_account (self, TNY_TRANSPORT_ACCOUNT (account));
1998 iter = g_slist_next (iter);
2002 g_slist_foreach (list_specifics, foreach_free_string, NULL);
2003 g_slist_free (list_specifics);
2008 modest_tny_account_store_find_msg_in_outboxes (ModestTnyAccountStore *self,
2010 TnyAccount **ac_out)
2012 TnyIterator *acc_iter;
2013 ModestTnyAccountStorePrivate *priv;
2015 TnyAccount *msg_account = NULL;
2017 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
2018 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2020 acc_iter = tny_list_create_iterator (priv->store_accounts_outboxes);
2021 while (!msg && !tny_iterator_is_done (acc_iter)) {
2022 TnyList *folders = tny_simple_list_new ();
2023 TnyAccount *account = TNY_ACCOUNT (tny_iterator_get_current (acc_iter));
2024 TnyIterator *folders_iter = NULL;
2026 tny_folder_store_get_folders (TNY_FOLDER_STORE (account), folders, NULL, FALSE, NULL);
2027 folders_iter = tny_list_create_iterator (folders);
2029 while (msg == NULL && !tny_iterator_is_done (folders_iter)) {
2030 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (folders_iter));
2031 msg = tny_folder_find_msg (folder, uri, NULL);
2034 msg_account = g_object_ref (account);
2036 g_object_unref (folder);
2037 tny_iterator_next (folders_iter);
2039 g_object_unref (folders_iter);
2041 g_object_unref (folders);
2042 g_object_unref (account);
2043 tny_iterator_next (acc_iter);
2046 g_object_unref (acc_iter);
2049 *ac_out = msg_account;
2054 TnyTransportAccount *
2055 modest_tny_account_store_get_transport_account_from_outbox_header(ModestTnyAccountStore *self, TnyHeader *header)
2057 TnyIterator *acc_iter;
2058 ModestTnyAccountStorePrivate *priv;
2059 TnyTransportAccount *header_acc = NULL;
2062 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
2063 g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
2064 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2065 msg_id = modest_tny_send_queue_get_msg_id (header);
2066 acc_iter = tny_list_create_iterator (priv->transport_accounts);
2067 while (!header_acc && !tny_iterator_is_done (acc_iter)) {
2068 TnyTransportAccount *account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current (acc_iter));
2069 ModestTnySendQueue *send_queue;
2070 ModestTnySendQueueStatus status;
2071 send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account), TRUE);
2072 if (TNY_IS_SEND_QUEUE (send_queue)) {
2073 status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
2074 if (status != MODEST_TNY_SEND_QUEUE_UNKNOWN)
2075 header_acc = g_object_ref(account);
2077 g_object_unref (account);
2078 tny_iterator_next (acc_iter);
2080 g_object_unref(acc_iter);
2088 ModestTnyAccountStore *account_store;
2089 ModestTnyAccountStoreShutdownCallback callback;
2095 account_shutdown_callback (TnyCamelAccount *account, gboolean canceled, GError *err, gpointer userdata)
2097 ShutdownOpData *op_data = (ShutdownOpData *) userdata;
2099 if (op_data->pending == 0) {
2100 if (op_data->callback)
2101 op_data->callback (op_data->account_store, op_data->userdata);
2102 g_object_unref (op_data->account_store);
2105 g_object_unref (op_data->account_store);
2110 account_shutdown (TnyAccount *account, ShutdownOpData *op_data)
2112 g_return_if_fail (account && TNY_IS_ACCOUNT(account));
2114 if (TNY_IS_STORE_ACCOUNT (account) &&
2115 !modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
2118 /* Disconnect account */
2119 if (tny_account_get_connection_status (account) == TNY_CONNECTION_STATUS_CONNECTED) {
2120 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(account), FALSE,
2121 account_shutdown_callback, op_data);
2127 g_object_unref (op_data->account_store);
2132 modest_tny_account_store_shutdown (ModestTnyAccountStore *self,
2133 ModestTnyAccountStoreShutdownCallback callback,
2136 gint i, num_accounts;
2137 ShutdownOpData *op_data;
2138 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2140 /* Get references */
2142 if (priv->store_accounts)
2143 num_accounts += tny_list_get_length (priv->store_accounts);
2144 if (priv->transport_accounts) {
2145 num_accounts += tny_list_get_length (priv->transport_accounts);
2147 for (i = 0 ; i < num_accounts ; i++)
2148 g_object_ref (self);
2150 /* Create the helper object */
2151 op_data = g_new0 (ShutdownOpData, 1);
2152 op_data->callback = callback;
2153 op_data->userdata = userdata;
2154 op_data->pending = num_accounts;
2155 op_data->account_store = self;
2157 /* Destroy all accounts. Disconnect all accounts before they are destroyed */
2158 if (priv->store_accounts) {
2159 tny_list_foreach (priv->store_accounts, (GFunc)account_shutdown, op_data);
2162 if (priv->transport_accounts) {
2163 tny_list_foreach (priv->transport_accounts, (GFunc)account_shutdown, op_data);
2166 if (op_data->pending == 0) {
2167 if (op_data->callback)
2168 op_data->callback (op_data->account_store, op_data->userdata);
2174 modest_tny_account_store_is_send_mail_blocked (ModestTnyAccountStore *self)
2176 ModestTnyAccountStorePrivate *priv;
2178 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
2180 return priv->send_mail_blocked;
2184 modest_tny_account_store_set_send_mail_blocked (ModestTnyAccountStore *self,
2187 ModestTnyAccountStorePrivate *priv;
2189 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
2191 priv->send_mail_blocked = blocked;
2195 count_remote_accounts (gpointer data, gpointer user_data)
2197 TnyFolderStore *account = TNY_FOLDER_STORE (data);
2198 gint *count = (gint *) user_data;
2200 if (modest_tny_folder_store_is_remote (account))
2205 modest_tny_account_store_get_num_remote_accounts (ModestTnyAccountStore *self)
2207 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2210 /* Count remote accounts */
2211 tny_list_foreach (priv->store_accounts, (GFunc) count_remote_accounts, &count);