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>
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 void connection_status_changed (TnyAccount *account,
116 TnyConnectionStatus status,
119 static gboolean only_local_accounts (ModestTnyAccountStore *self);
121 /* list my signals */
123 ACCOUNT_CHANGED_SIGNAL,
124 ACCOUNT_INSERTED_SIGNAL,
125 ACCOUNT_REMOVED_SIGNAL,
127 PASSWORD_REQUESTED_SIGNAL,
131 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
132 struct _ModestTnyAccountStorePrivate {
134 GHashTable *password_hash;
135 ModestAccountMgr *account_mgr;
136 TnySessionCamel *session;
141 /* We cache the lists of accounts here */
142 TnyList *store_accounts;
143 TnyList *transport_accounts;
144 TnyList *store_accounts_outboxes;
146 /* Matches transport accounts and outbox folder */
147 GHashTable *outbox_of_transport;
150 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
151 MODEST_TYPE_TNY_ACCOUNT_STORE, \
152 ModestTnyAccountStorePrivate))
155 static GObjectClass *parent_class = NULL;
157 static guint signals[LAST_SIGNAL] = {0};
160 modest_tny_account_store_get_type (void)
162 static GType my_type = 0;
165 static const GTypeInfo my_info = {
166 sizeof(ModestTnyAccountStoreClass),
167 modest_tny_account_store_base_init, /* base init */
168 NULL, /* base finalize */
169 (GClassInitFunc) modest_tny_account_store_class_init,
170 NULL, /* class finalize */
171 NULL, /* class data */
172 sizeof(ModestTnyAccountStore),
174 (GInstanceInitFunc) modest_tny_account_store_instance_init,
178 static const GInterfaceInfo iface_info = {
179 (GInterfaceInitFunc) modest_tny_account_store_init,
180 NULL, /* interface_finalize */
181 NULL /* interface_data */
184 my_type = g_type_register_static (G_TYPE_OBJECT,
185 "ModestTnyAccountStore",
187 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
195 modest_tny_account_store_base_init (gpointer g_class)
197 static gboolean tny_account_store_initialized = FALSE;
199 if (!tny_account_store_initialized) {
201 signals[ACCOUNT_CHANGED_SIGNAL] =
202 g_signal_new ("account_changed",
203 MODEST_TYPE_TNY_ACCOUNT_STORE,
205 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_changed),
207 g_cclosure_marshal_VOID__OBJECT,
208 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
210 signals[ACCOUNT_INSERTED_SIGNAL] =
211 g_signal_new ("account_inserted",
212 MODEST_TYPE_TNY_ACCOUNT_STORE,
214 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_inserted),
216 g_cclosure_marshal_VOID__OBJECT,
217 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
219 signals[ACCOUNT_REMOVED_SIGNAL] =
220 g_signal_new ("account_removed",
221 MODEST_TYPE_TNY_ACCOUNT_STORE,
223 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_removed),
225 g_cclosure_marshal_VOID__OBJECT,
226 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
228 signals[PASSWORD_REQUESTED_SIGNAL] =
229 g_signal_new ("password_requested",
230 MODEST_TYPE_TNY_ACCOUNT_STORE,
232 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
234 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
235 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
238 tny_account_store_initialized = TRUE;
244 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
246 GObjectClass *gobject_class;
247 gobject_class = (GObjectClass*) klass;
249 parent_class = g_type_class_peek_parent (klass);
250 gobject_class->finalize = modest_tny_account_store_finalize;
252 g_type_class_add_private (gobject_class,
253 sizeof(ModestTnyAccountStorePrivate));
257 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
259 GnomeVFSVolumeMonitor* monitor = NULL;
260 ModestTnyAccountStorePrivate *priv;
262 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
264 priv->cache_dir = NULL;
265 priv->account_mgr = NULL;
266 priv->session = NULL;
268 priv->sighandlers = NULL;
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 MODEST_MCC1_VOLUMEPATH);
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;
360 self = MODEST_TNY_ACCOUNT_STORE(user_data);
361 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
363 /* Check whether this was the external MMC1 card: */
364 uri = gnome_vfs_volume_get_activation_uri (volume);
366 if (uri && (!strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI))) {
367 add_mmc_account (self, TRUE /* emit the insert signal. */);
374 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
375 GnomeVFSVolume *volume,
378 ModestTnyAccountStore *self;
379 ModestTnyAccountStorePrivate *priv;
382 self = MODEST_TNY_ACCOUNT_STORE(user_data);
383 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
385 /* Check whether this was the external MMC1 card: */
386 uri = gnome_vfs_volume_get_activation_uri (volume);
387 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
388 TnyAccount *mmc_account = NULL;
389 gboolean found = FALSE;
390 TnyIterator *iter = NULL;
392 iter = tny_list_create_iterator (priv->store_accounts);
393 while (!tny_iterator_is_done (iter) && !found) {
396 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
397 if (modest_tny_account_is_memory_card_account (account)) {
399 mmc_account = g_object_ref (account);
401 g_object_unref (account);
402 tny_iterator_next (iter);
404 g_object_unref (iter);
407 /* Remove from the list */
408 tny_list_remove (priv->store_accounts, G_OBJECT (mmc_account));
410 /* Notify observers */
411 g_signal_emit (G_OBJECT (self),
412 signals [ACCOUNT_REMOVED_SIGNAL],
415 g_object_unref (mmc_account);
417 g_warning ("%s: there was no store account for the unmounted MMC",
425 * forget_password_in_memory
426 * @self: a TnyAccountStore instance
427 * @account: A server account.
429 * Forget any password stored in memory for this account.
430 * For instance, this should be called when the user has changed the password in the account settings.
433 forget_password_in_memory (ModestTnyAccountStore *self,
434 const gchar * server_account_name)
436 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
438 if (server_account_name && priv->password_hash) {
439 g_hash_table_remove (priv->password_hash, server_account_name);
444 on_account_changed (ModestAccountMgr *acc_mgr,
445 const gchar *account_name,
446 TnyAccountType account_type,
449 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
450 ModestTnyAccountStorePrivate *priv;
451 TnyList* account_list;
452 gboolean found = FALSE;
453 TnyIterator *iter = NULL;
455 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
456 account_list = (account_type == TNY_ACCOUNT_TYPE_STORE ?
457 priv->store_accounts :
458 priv->transport_accounts);
460 iter = tny_list_create_iterator (account_list);
461 while (!tny_iterator_is_done (iter) && !found) {
462 TnyAccount *tny_account;
463 tny_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
465 if (!strcmp (tny_account_get_id (tny_account), account_name)) {
467 modest_tny_account_update_from_account (tny_account, get_password, forget_password);
468 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, tny_account);
470 g_object_unref (tny_account);
472 tny_iterator_next (iter);
476 g_object_unref (iter);
480 show_password_warning_only (const gchar *msg)
482 ModestWindow *main_window =
483 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE); /* don't create */
485 /* Show an explanatory temporary banner: */
487 modest_platform_information_banner (NULL, NULL, msg);
491 show_wrong_password_dialog (TnyAccount *account)
493 if (g_object_get_data (G_OBJECT (account), "connection_specific") != NULL) {
494 modest_ui_actions_on_smtp_servers (NULL, NULL);
496 ModestAccountProtocol *proto;
497 ModestProtocolType proto_type;
498 const gchar *modest_account_name;
499 modest_account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
502 proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
503 modest_account_name);
504 proto = (ModestAccountProtocol *)
505 modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
508 /* Create and show the dialog */
509 if (proto && MODEST_IS_ACCOUNT_PROTOCOL (proto)) {
510 ModestAccountSettingsDialog *dialog =
511 modest_account_protocol_get_account_settings_dialog (proto, modest_account_name);
512 gtk_widget_show (GTK_WIDGET (dialog));
515 /* Show an explanatory temporary banner: */
516 modest_platform_information_banner (NULL, NULL, _("mcen_ib_username_pw_incorrect"));
519 /* This callback will be called by Tinymail when it needs the password
520 * from the user or the account settings.
521 * It can also call forget_password() before calling this,
522 * so that we clear wrong passwords out of our account settings.
523 * Note that TnyAccount here will be the server account. */
525 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
527 ModestTnyAccountStore *self = NULL;
528 ModestTnyAccountStorePrivate *priv;
529 gchar *username = NULL;
531 gpointer pwd_ptr = NULL;
532 gboolean already_asked = FALSE;
533 const gchar *server_account_name;
536 g_return_val_if_fail (account, NULL);
539 g_debug ("%s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
542 /* Get a reference to myself */
543 self = MODEST_TNY_ACCOUNT_STORE (g_object_get_data (G_OBJECT(account), "account_store"));
544 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
546 /* Ensure that we still have this account. It could happen
547 that a set_online was requested *before* removing an
548 account, and due to tinymail emits the get_password
549 function using a g_idle the account could be actually
550 removed *before* this function was really called */
551 url_string = tny_account_get_url_string (account);
553 TnyAccount *tmp_account;
555 tmp_account = tny_account_store_find_account (TNY_ACCOUNT_STORE (self),
563 g_object_unref (tmp_account);
566 server_account_name = tny_account_get_id (account);
567 if (!server_account_name || !self) {
568 g_warning ("modest: %s: could not retrieve account_store for account %s",
569 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
576 /* This hash map stores passwords, including passwords that are not stored in gconf. */
577 /* Is it in the hash? if it's already there, it must be wrong... */
578 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
579 * type-punned ptrs...*/
580 already_asked = priv->password_hash &&
581 g_hash_table_lookup_extended (priv->password_hash,
584 (gpointer*)&pwd_ptr);
586 g_debug ("%s: Already asked = %d\n", __FUNCTION__, already_asked);
589 /* If the password is not already there, try ModestConf */
590 if (!already_asked) {
591 pwd = modest_account_mgr_get_server_account_password (priv->account_mgr,
592 server_account_name);
593 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
596 /* If it was already asked, it must have been wrong, so ask again */
597 if (already_asked || !pwd || strlen(pwd) == 0) {
598 gboolean settings_have_password;
599 ModestProtocolType protocol_type;
601 /* As per the UI spec, if no password was set in the account settings,
602 * ask for it now. But if the password is wrong in the account settings,
603 * then show a banner and the account settings dialog so it can be corrected:
605 settings_have_password =
606 modest_account_mgr_get_server_account_has_password (priv->account_mgr, server_account_name);
608 protocol_type = modest_tny_account_get_protocol_type (account);
610 /* Show an error and after that ask for a password */
611 if (modest_protocol_registry_protocol_type_has_tag(modest_runtime_get_protocol_registry (),
612 protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
613 gchar *username = NULL, *msg = NULL;
614 username = modest_account_mgr_get_server_account_username (priv->account_mgr,
615 server_account_name);
616 if (!username || strlen(username) == 0) {
617 msg = g_strdup_printf (_("emev_ni_ui_smtp_userid_invalid"),
618 tny_account_get_name (account),
619 tny_account_get_hostname (account));
622 password = modest_account_mgr_get_server_account_password (priv->account_mgr,
623 server_account_name);
624 if (!password || strlen(password) == 0)
625 msg = g_strdup_printf (_("emev_ni_ui_smtp_passwd_invalid"),
626 tny_account_get_name (account),
627 tny_account_get_hostname (account));
629 msg = g_strdup_printf (_("emev_ni_ui_smtp_authentication_fail_error"),
630 tny_account_get_hostname (account));
635 modest_platform_run_information_dialog (NULL, msg, TRUE);
642 if (settings_have_password) {
643 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
644 show_wrong_password_dialog (account);
652 /* we don't have it yet. Get the password from the user */
653 const gchar* account_id = tny_account_get_id (account);
654 gboolean remember = FALSE;
659 gboolean username_known =
660 modest_account_mgr_get_server_account_username_has_succeeded(priv->account_mgr,
661 server_account_name);
662 /* If the login has ever succeeded then show a specific message */
664 msg = dgettext ("hildon-common-strings", "ecdg_ib_set_password_incorrect");
666 msg = _("mcen_ib_username_pw_incorrect");
667 show_password_warning_only (msg);
670 /* Request password */
671 g_signal_emit (G_OBJECT (self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
672 account_id, /* server_account_name */
673 &username, &pwd, cancel, &remember);
677 /* The password will be returned as the result,
678 * but we need to tell tinymail about the username too: */
680 /* WARNING: I disabled setting username as this can cause locks. Anyway,
681 * as now we have the password dialog username entry always dimmed
682 * this shouldn't be a problem */
685 /* tny_account_set_user (account, username); */
687 /* Do not save the password in gconf, because
688 * the UI spec says "The password will never
689 * be saved in the account": */
691 /* We need to dup the string even knowing that
692 it's already a dup of the contents of an
693 entry, because it if it's wrong, then camel
695 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
697 g_hash_table_remove (priv->password_hash, server_account_name);
712 modest_tny_account_store_forget_already_asked (ModestTnyAccountStore *self, TnyAccount *account)
714 g_return_if_fail (account);
716 ModestTnyAccountStorePrivate *priv;
718 gpointer pwd_ptr = NULL;
719 gboolean already_asked = FALSE;
721 const gchar *server_account_name = tny_account_get_id (account);
723 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
725 /* This hash map stores passwords, including passwords that are not stored in gconf. */
726 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
727 * type-punned ptrs...*/
728 already_asked = priv->password_hash &&
729 g_hash_table_lookup_extended (priv->password_hash,
732 (gpointer*)&pwd_ptr);
735 g_hash_table_remove (priv->password_hash, server_account_name);
743 /* tinymail calls this if the connection failed due to an incorrect password.
744 * And it seems to call this for any general connection failure. */
746 forget_password (TnyAccount *account)
748 ModestTnyAccountStore *self;
749 ModestTnyAccountStorePrivate *priv;
750 const TnyAccountStore *account_store;
754 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
756 self = MODEST_TNY_ACCOUNT_STORE (account_store);
757 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
758 key = tny_account_get_id (account);
760 /* Do not remove the key, this will allow us to detect that we
761 have already asked for it at least once */
762 pwd = g_hash_table_lookup (priv->password_hash, key);
764 memset (pwd, 0, strlen (pwd));
765 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
768 /* Remove from configuration system */
770 modest_account_mgr_unset (priv->account_mgr,
771 key, MODEST_ACCOUNT_PASSWORD, TRUE);
776 modest_tny_account_store_finalize (GObject *obj)
778 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
779 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
781 g_free (priv->cache_dir);
782 priv->cache_dir = NULL;
784 if (priv->password_hash) {
785 g_hash_table_destroy (priv->password_hash);
786 priv->password_hash = NULL;
789 if (priv->outbox_of_transport) {
790 g_hash_table_destroy (priv->outbox_of_transport);
791 priv->outbox_of_transport = NULL;
794 modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
795 priv->sighandlers = NULL;
797 if (priv->account_mgr) {
798 g_object_unref (G_OBJECT(priv->account_mgr));
799 priv->account_mgr = NULL;
803 g_object_unref (G_OBJECT(priv->device));
807 /* Destroy all accounts. Disconnect all accounts before they are destroyed */
808 if (priv->store_accounts) {
809 tny_list_foreach (priv->store_accounts, (GFunc)account_verify_last_ref, "store");
810 g_object_unref (priv->store_accounts);
811 priv->store_accounts = NULL;
814 if (priv->transport_accounts) {
815 tny_list_foreach (priv->transport_accounts, (GFunc)account_verify_last_ref, "transport");
816 g_object_unref (priv->transport_accounts);
817 priv->transport_accounts = NULL;
820 if (priv->store_accounts_outboxes) {
821 g_object_unref (priv->store_accounts_outboxes);
822 priv->store_accounts_outboxes = NULL;
826 camel_object_unref (CAMEL_OBJECT(priv->session));
827 priv->session = NULL;
830 G_OBJECT_CLASS(parent_class)->finalize (obj);
834 volume_path_is_mounted (const gchar* path)
836 g_return_val_if_fail (path, FALSE);
838 gboolean result = FALSE;
839 gchar * path_as_uri = g_filename_to_uri (path, NULL, NULL);
840 g_return_val_if_fail (path_as_uri, FALSE);
842 /* Get the monitor singleton: */
843 GnomeVFSVolumeMonitor *monitor = gnome_vfs_get_volume_monitor();
845 /* This seems like a simpler way to do this, but it returns a
846 * GnomeVFSVolume even if the drive is not mounted: */
848 GnomeVFSVolume *volume = gnome_vfs_volume_monitor_get_volume_for_path (monitor,
849 MODEST_MCC1_VOLUMEPATH);
851 gnome_vfs_volume_unref(volume);
855 /* Get the mounted volumes from the monitor: */
856 GList *list = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
858 for (iter = list; iter; iter = g_list_next (iter)) {
859 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
863 gnome_vfs_volume_get_display_name (volume);
864 printf ("volume display name=%s\n", display_name);
865 g_free (display_name);
869 gnome_vfs_volume_get_activation_uri (volume);
870 /* printf (" uri=%s\n", uri); */
871 if (uri && (strcmp (uri, path_as_uri) == 0))
876 gnome_vfs_volume_unref (volume);
882 g_free (path_as_uri);
887 ModestTnyAccountStore*
888 modest_tny_account_store_new (ModestAccountMgr *account_mgr,
892 ModestTnyAccountStorePrivate *priv;
893 TnyAccount *local_account = NULL;
894 TnyLockable *lockable;
896 g_return_val_if_fail (account_mgr, NULL);
897 g_return_val_if_fail (device, NULL);
899 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
900 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
902 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
903 priv->device = g_object_ref (device);
905 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
906 if (!priv->session) {
907 g_warning ("failed to get TnySessionCamel");
911 /* Set the ui locker */
912 lockable = tny_gtk_lockable_new ();
913 tny_session_camel_set_ui_locker (priv->session, lockable);
914 g_object_unref (lockable);
916 /* Connect signals */
917 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
918 G_OBJECT(account_mgr), "account_inserted",
919 G_CALLBACK (on_account_inserted), obj);
920 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
921 G_OBJECT(account_mgr), "account_changed",
922 G_CALLBACK (on_account_changed), obj);
923 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
924 G_OBJECT(account_mgr), "account_removed",
925 G_CALLBACK (on_account_removed), obj);
927 /* Create the lists of accounts */
928 priv->store_accounts = tny_simple_list_new ();
929 priv->transport_accounts = tny_simple_list_new ();
930 priv->store_accounts_outboxes = tny_simple_list_new ();
932 /* Create the local folders account */
934 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session, NULL);
935 tny_list_append (priv->store_accounts, G_OBJECT(local_account));
936 g_object_unref (local_account);
938 /* Add the other remote accounts. Do this after adding the
939 local account, because we need to add our outboxes to the
940 global OUTBOX hosted in the local account */
941 add_existing_accounts (MODEST_TNY_ACCOUNT_STORE (obj));
943 /* Add connection-specific transport accounts if there are any
944 accounts available */
945 if (!only_local_accounts (MODEST_TNY_ACCOUNT_STORE(obj)))
946 add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE(obj));
948 /* This is a singleton, so it does not need to be unrefed. */
949 if (volume_path_is_mounted (MODEST_MCC1_VOLUMEPATH)) {
951 add_mmc_account (MODEST_TNY_ACCOUNT_STORE (obj), FALSE /* don't emit the insert signal. */);
954 return MODEST_TNY_ACCOUNT_STORE(obj);
958 modest_tny_account_store_get_accounts (TnyAccountStore *self,
960 TnyGetAccountsRequestType request_type)
962 ModestTnyAccountStorePrivate *priv;
964 g_return_if_fail (self);
965 g_return_if_fail (TNY_IS_LIST(list));
967 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
969 switch (request_type) {
970 case TNY_ACCOUNT_STORE_BOTH:
971 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
972 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
974 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
975 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
977 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
978 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
981 g_return_if_reached ();
984 /* Initialize session. Why do we need this ??? */
985 tny_session_camel_set_initialized (priv->session);
990 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
992 ModestTnyAccountStorePrivate *priv;
993 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
995 if (!priv->cache_dir)
996 priv->cache_dir = g_build_filename (g_get_home_dir(),
997 MODEST_DIR, MODEST_CACHE_DIR, NULL);
998 return priv->cache_dir;
1003 * callers need to unref
1006 modest_tny_account_store_get_device (TnyAccountStore *self)
1008 ModestTnyAccountStorePrivate *priv;
1010 g_return_val_if_fail (self, NULL);
1012 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1015 return g_object_ref (G_OBJECT(priv->device));
1022 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1024 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1025 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1032 modest_tny_account_store_alert (TnyAccountStore *self,
1033 TnyAccount *account,
1038 ModestProtocolType protocol_type = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1039 ModestProtocol *protocol = NULL;
1040 const gchar* server_name = "";
1041 gchar *prompt = NULL;
1042 gboolean retval = TRUE;
1044 /* NOTE: account may be NULL in some cases */
1045 g_return_val_if_fail (error, FALSE);
1047 /* Get the server name: */
1049 server_name = tny_account_get_hostname (account);
1050 protocol_type = modest_tny_account_get_protocol_type (account);
1051 if (protocol_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID){
1052 g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__,
1053 tny_account_get_id (account));
1056 protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
1060 switch (error->code) {
1061 case TNY_SYSTEM_ERROR_CANCEL:
1062 /* Don't show waste the user's time by showing him a dialog telling
1063 * him that he has just cancelled something: */
1066 case TNY_SERVICE_ERROR_PROTOCOL:
1067 /* Like a BAD from IMAP (protocol error) */
1068 case TNY_SERVICE_ERROR_LOST_CONNECTION:
1069 /* Lost the connection with the service */
1070 case TNY_SERVICE_ERROR_UNAVAILABLE:
1071 /* You must be working online for this operation */
1072 case TNY_SERVICE_ERROR_CONNECT:
1074 prompt = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_CONNECT_ERROR, server_name);
1077 g_return_val_if_reached (FALSE);
1081 case TNY_SERVICE_ERROR_AUTHENTICATE:
1082 /* It seems that there's no better error to show with
1083 * POP and IMAP because TNY_SERVICE_ERROR_AUTHENTICATE
1084 * may appear if there's a timeout during auth */
1086 prompt = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_AUTH_ERROR, server_name);
1089 g_return_val_if_reached (FALSE);
1093 case TNY_SERVICE_ERROR_CERTIFICATE:
1094 /* We'll show the proper dialog later */
1097 case TNY_SYSTEM_ERROR_MEMORY:
1098 /* Can't allocate memory for this operation */
1100 case TNY_SERVICE_ERROR_UNKNOWN:
1103 g_debug ("Unexpected error %d", error->code);
1104 g_return_val_if_reached (FALSE);
1108 if (error->code == TNY_SERVICE_ERROR_CERTIFICATE)
1109 retval = modest_platform_run_certificate_confirmation_dialog (server_name,
1111 else if (error->code == TNY_SERVICE_ERROR_AUTHENTICATE) {
1112 modest_platform_run_information_dialog (NULL, prompt, TRUE);
1114 /* Show the account dialog if it was wrong */
1115 if (error->code == TNY_SERVICE_ERROR_CONNECT ||
1116 error->code == TNY_SERVICE_ERROR_AUTHENTICATE)
1117 show_wrong_password_dialog (account);
1122 g_debug ("%s: error code %d (%s", __FUNCTION__, error->code, error->message);
1132 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1134 TnyAccountStoreIface *klass;
1136 g_return_if_fail (g);
1138 klass = (TnyAccountStoreIface *)g;
1140 klass->get_accounts =
1141 modest_tny_account_store_get_accounts;
1142 klass->get_cache_dir =
1143 modest_tny_account_store_get_cache_dir;
1145 modest_tny_account_store_get_device;
1147 modest_tny_account_store_alert;
1148 klass->find_account =
1149 modest_tny_account_store_find_account_by_url;
1153 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1154 ModestTnyGetPassFunc func)
1156 /* not implemented, we use signals */
1157 g_printerr ("modest: set_get_pass_func not implemented\n");
1161 modest_tny_account_store_get_session (TnyAccountStore *self)
1163 g_return_val_if_fail (self, NULL);
1164 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1168 get_tny_account_by (TnyList *accounts,
1169 ModestTnyAccountStoreQueryType type,
1172 TnyIterator *iter = NULL;
1173 gboolean found = FALSE;
1174 TnyAccount *retval = NULL;
1176 g_return_val_if_fail (TNY_IS_LIST(accounts), NULL);
1178 if (tny_list_get_length(accounts) == 0) {
1179 g_warning ("%s: account list is empty", __FUNCTION__);
1183 iter = tny_list_create_iterator (accounts);
1184 while (!tny_iterator_is_done (iter) && !found) {
1185 TnyAccount *tmp_account = NULL;
1186 const gchar *val = NULL;
1188 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1189 if (!TNY_IS_ACCOUNT(tmp_account)) {
1190 g_warning ("%s: not a valid account", __FUNCTION__);
1196 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1197 val = tny_account_get_id (tmp_account);
1199 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1200 val = tny_account_get_url_string (tmp_account);
1204 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1205 tny_account_matches_url_string (tmp_account, str)) {
1206 retval = g_object_ref (tmp_account);
1209 if (val && str && strcmp (val, str) == 0) {
1210 retval = g_object_ref (tmp_account);
1214 g_object_unref (tmp_account);
1215 tny_iterator_next (iter);
1217 g_object_unref (iter);
1223 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1224 ModestTnyAccountStoreQueryType type,
1227 TnyAccount *account = NULL;
1228 ModestTnyAccountStorePrivate *priv;
1230 g_return_val_if_fail (self, NULL);
1231 g_return_val_if_fail (str, NULL);
1233 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1235 /* Search in store accounts */
1236 account = get_tny_account_by (priv->store_accounts, type, str);
1238 /* If we already found something, no need to search the transport accounts */
1240 account = get_tny_account_by (priv->transport_accounts, type, str);
1242 /* If we already found something, no need to search the
1243 per-account outbox accounts */
1245 account = get_tny_account_by (priv->store_accounts_outboxes, type, str);
1248 /* Warn if nothing was found. This is generally unusual. */
1250 g_warning("%s: Failed to find account with %s=%s\n",
1252 (type == MODEST_TNY_ACCOUNT_STORE_QUERY_ID) ? "ID" : "URL",
1256 /* Returns a new reference to the account if found */
1262 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1263 const gchar *account_name,
1264 TnyAccountType type)
1266 ModestTnyAccountStorePrivate *priv = NULL;
1267 TnyAccount *retval = NULL;
1268 TnyList *account_list = NULL;
1269 TnyIterator *iter = NULL;
1272 g_return_val_if_fail (self, NULL);
1273 g_return_val_if_fail (account_name, NULL);
1274 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE ||
1275 type == TNY_ACCOUNT_TYPE_TRANSPORT,
1278 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1280 account_list = (type == TNY_ACCOUNT_TYPE_STORE) ?
1281 priv->store_accounts :
1282 priv->transport_accounts;
1284 if (!account_list) {
1285 g_printerr ("%s: No server accounts of type %s\n", __FUNCTION__,
1286 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport");
1290 /* Look for the server account */
1292 iter = tny_list_create_iterator (account_list);
1293 while (!tny_iterator_is_done (iter) && !found) {
1294 const gchar *modest_acc_name;
1295 TnyAccount *tmp_account;
1297 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1299 modest_tny_account_get_parent_modest_account_name_for_server_account (tmp_account);
1301 if (account_name && modest_acc_name && !strcmp (account_name, modest_acc_name)) {
1303 retval = g_object_ref (tmp_account);
1305 /* Free and continue */
1306 g_object_unref (tmp_account);
1307 tny_iterator_next (iter);
1309 g_object_unref (iter);
1312 g_printerr ("modest: %s: could not get tny %s account for %s\n." \
1313 "Number of server accounts of this type=%d\n", __FUNCTION__,
1314 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport",
1315 account_name, tny_list_get_length (account_list));
1318 /* Returns a new reference */
1323 modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (
1324 ModestTnyAccountStore *self, const gchar *account_name)
1328 g_return_val_if_fail (self && MODEST_IS_TNY_ACCOUNT_STORE(self), NULL);
1329 g_return_val_if_fail (account_name, NULL);
1331 /* Get the current connection: */
1332 device = modest_runtime_get_device ();
1335 g_warning ("%s: could not get device", __FUNCTION__);
1339 if (!tny_device_is_online (device))
1342 #ifdef MODEST_HAVE_CONIC
1343 g_return_val_if_fail (TNY_IS_MAEMO_CONIC_DEVICE (device), NULL);
1345 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1346 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1347 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1351 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1355 const gchar *connection_id = con_ic_iap_get_id (connection);
1356 /* printf ("DEBUG: %s: connection_id=%s\n", __FUNCTION__, connection_id); */
1360 /* Get the connection-specific transport acccount, if any: */
1361 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1363 /* Check if this account has connection-specific SMTP enabled */
1364 if (!modest_account_mgr_get_use_connection_specific_smtp (account_manager, account_name)) {
1368 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1371 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1372 if (!server_account_name) {
1373 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1376 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1377 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1378 server_account_name);
1380 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1381 g_free (server_account_name);
1383 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1384 g_object_unref (connection);
1388 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1389 #endif /* MODEST_HAVE_CONIC */
1394 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1395 const gchar *account_name)
1397 g_return_val_if_fail (self, NULL);
1398 g_return_val_if_fail (account_name, NULL);
1400 if (!account_name || !self)
1403 /* Get the connection-specific transport acccount, if any: */
1404 /* Note: This gives us a reference: */
1405 TnyAccount *account =
1406 modest_tny_account_store_get_smtp_specific_transport_account_for_open_connection (self, account_name);
1408 /* If there is no connection-specific transport account (the common case),
1409 * just get the regular transport account: */
1411 /* The special local folders don't have transport accounts. */
1412 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1415 /* Note: This gives us a reference: */
1416 account = modest_tny_account_store_get_server_account (self, account_name,
1417 TNY_ACCOUNT_TYPE_TRANSPORT);
1421 /* returns a reference. */
1426 modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
1428 TnyAccount *account = NULL;
1429 ModestTnyAccountStorePrivate *priv;
1433 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1435 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1438 iter = tny_list_create_iterator (priv->store_accounts);
1439 while (!tny_iterator_is_done (iter) && !found) {
1440 TnyAccount *tmp_account;
1442 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1443 if (modest_tny_account_is_virtual_local_folders (tmp_account)) {
1444 account = g_object_ref (tmp_account);
1447 g_object_unref (tmp_account);
1448 tny_iterator_next (iter);
1450 g_object_unref (iter);
1452 /* Returns a new reference to the account */
1457 modest_tny_account_store_get_mmc_folders_account (ModestTnyAccountStore *self)
1459 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1462 return modest_tny_account_store_get_tny_account_by (self, MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1463 MODEST_MMC_ACCOUNT_ID);
1467 /*********************************************************************************/
1469 add_existing_accounts (ModestTnyAccountStore *self)
1471 GSList *account_names = NULL, *iter = NULL;
1472 ModestTnyAccountStorePrivate *priv = NULL;
1474 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1476 /* These are account names, not server_account names */
1477 account_names = modest_account_mgr_account_names (priv->account_mgr, FALSE);
1479 for (iter = account_names; iter != NULL; iter = g_slist_next (iter)) {
1480 const gchar *account_name = (const gchar*) iter->data;
1482 /* Insert all enabled accounts without notifying */
1483 if (modest_account_mgr_get_enabled (priv->account_mgr, account_name))
1484 insert_account (self, account_name, FALSE);
1486 modest_account_mgr_free_account_names (account_names);
1490 connection_status_changed (TnyAccount *account,
1491 TnyConnectionStatus status,
1494 /* We do this here and not in the connection policy because we
1495 don't want to do it for every account, just for the
1496 accounts that are interactively added when modest is
1498 if (status == TNY_CONNECTION_STATUS_CONNECTED) {
1499 const gchar *account_name;
1500 ModestWindow *main_window;
1501 ModestTnyAccountStorePrivate *priv = NULL;
1503 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (data);
1505 /* Remove this handler */
1506 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1508 "connection_status_changed");
1510 /* Perform a send receive */
1511 account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
1512 main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
1513 modest_ui_actions_do_send_receive (account_name, FALSE, FALSE, TRUE, main_window);
1518 create_tny_account (ModestTnyAccountStore *self,
1520 TnyAccountType type,
1523 TnyAccount *account = NULL;
1524 ModestTnyAccountStorePrivate *priv = NULL;
1526 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1528 account = modest_tny_account_new_from_account (priv->account_mgr,
1535 /* Forget any cached password for the account, so that
1536 we use a new account if any */
1537 forget_password_in_memory (self, tny_account_get_id (account));
1539 /* Install a signal handler that will refresh the
1540 account the first time it becomes online. Do this
1541 only if we're adding a new account while the
1542 program is running (we do not want to do this
1544 if (type == TNY_ACCOUNT_TYPE_STORE && notify)
1545 priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
1547 "connection_status_changed",
1548 G_CALLBACK (connection_status_changed),
1551 /* Set the account store */
1552 g_object_set_data (G_OBJECT(account), "account_store", self);
1554 g_printerr ("modest: failed to create account for %s\n", name);
1560 typedef struct _AddOutboxInfo {
1561 ModestTnyAccountStore *account_store;
1562 TnyAccount *transport_account;
1566 add_outbox_from_transport_account_to_global_outbox_get_folders_cb (TnyFolderStore *folder_store,
1572 TnyIterator *iter_folders;
1573 TnyFolder *per_account_outbox;
1574 TnyAccount *local_account = NULL;
1575 AddOutboxInfo *info = (AddOutboxInfo *) userdata;
1576 ModestTnyAccountStorePrivate *priv = NULL;
1577 ModestTnyAccountStore *self;
1579 self = MODEST_TNY_ACCOUNT_STORE (info->account_store);
1580 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1582 /* Note that this could happen if there is not enough space
1583 available on disk, then the outbox folder could not be
1585 if (tny_list_get_length (list) != 1) {
1586 g_warning ("%s: could not create outbox folder (%d folders found)", __FUNCTION__,
1587 tny_list_get_length (list));
1591 iter_folders = tny_list_create_iterator (list);
1592 per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
1593 g_object_unref (iter_folders);
1594 g_object_unref (list);
1596 /* Add the outbox of the new per-account-local-outbox account
1597 to the global local merged OUTBOX of the local folders
1599 local_account = modest_tny_account_store_get_local_folders_account (info->account_store);
1600 modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1601 per_account_outbox);
1602 /* Add the pair to the hash table */
1603 g_hash_table_insert (priv->outbox_of_transport,
1604 info->transport_account,
1605 per_account_outbox);
1607 /* Notify that the local account changed */
1608 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1609 g_object_unref (local_account);
1610 g_object_unref (per_account_outbox);
1613 g_object_unref (info->transport_account);
1614 g_slice_free (AddOutboxInfo, info);
1619 add_outbox_from_transport_account_to_global_outbox (ModestTnyAccountStore *self,
1620 const gchar *account_name,
1621 TnyAccount *transport_account)
1623 TnyList *folders = NULL;
1624 TnyAccount *account_outbox = NULL;
1625 ModestTnyAccountStorePrivate *priv = NULL;
1626 AddOutboxInfo *info;
1628 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1630 /* Create per account local outbox */
1632 modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr,
1636 if (!G_IS_OBJECT (account_outbox)) {
1637 g_warning ("%s: could not create per account local outbox folder", __FUNCTION__);
1641 tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
1643 /* Get the outbox folder */
1644 folders = tny_simple_list_new ();
1645 info = g_slice_new0 (AddOutboxInfo);
1646 info->account_store = self;
1647 info->transport_account = g_object_ref (transport_account);
1648 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account_outbox), folders, NULL,
1649 add_outbox_from_transport_account_to_global_outbox_get_folders_cb, NULL, (gpointer) info);
1650 g_object_unref (account_outbox);
1654 * This function will be used for both adding new accounts and for the
1655 * initialization. In the initialization we do not want to emit
1656 * signals so notify will be FALSE, in the case of account additions
1657 * we do want to notify the observers
1660 insert_account (ModestTnyAccountStore *self,
1661 const gchar *account,
1664 ModestTnyAccountStorePrivate *priv = NULL;
1665 TnyAccount *store_account = NULL, *transport_account = NULL;
1667 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1669 /* Get the server and the transport account */
1670 store_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_STORE, notify);
1671 if (!store_account || !TNY_IS_ACCOUNT(store_account)) {
1672 g_warning ("%s: failed to create store account", __FUNCTION__);
1676 transport_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT, notify);
1677 if (!transport_account || !TNY_IS_ACCOUNT(transport_account)) {
1678 g_warning ("%s: failed to create transport account", __FUNCTION__);
1679 g_object_unref (store_account);
1683 /* Add accounts to the lists */
1684 tny_list_append (priv->store_accounts, G_OBJECT (store_account));
1685 tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
1687 /* Create a new pseudo-account with an outbox for this
1688 transport account and add it to the global outbox
1689 in the local account */
1690 add_outbox_from_transport_account_to_global_outbox (self, account, transport_account);
1692 /* Notify the observers. We do it after everything is
1695 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
1696 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
1700 g_object_unref (store_account);
1701 g_object_unref (transport_account);
1705 only_local_accounts (ModestTnyAccountStore *self)
1707 ModestTnyAccountStorePrivate *priv = NULL;
1708 gboolean only_local = TRUE;
1711 /* Check if this is the first remote account we add */
1712 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1713 iter = tny_list_create_iterator (priv->store_accounts);
1715 while (!tny_iterator_is_done (iter) && only_local) {
1716 TnyAccount *account = (TnyAccount *) tny_iterator_get_current (iter);
1717 if (modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
1719 g_object_unref (account);
1720 tny_iterator_next (iter);
1722 g_object_unref (iter);
1728 on_account_inserted (ModestAccountMgr *acc_mgr,
1729 const gchar *account,
1732 gboolean add_specific;
1734 add_specific = only_local_accounts (MODEST_TNY_ACCOUNT_STORE (user_data));
1736 /* Insert the account and notify the observers */
1737 insert_account (MODEST_TNY_ACCOUNT_STORE (user_data), account, TRUE);
1739 /* If it's the first remote account then add the connection
1740 specific SMTP servers as well */
1742 add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE (user_data));
1746 /* This is the callback of the tny_camel_account_set_online called in
1747 on_account_removed to disconnect the account */
1749 on_account_disconnect_when_removing (TnyCamelAccount *account,
1754 ModestTnyAccountStore *self;
1755 ModestTnyAccountStorePrivate *priv;
1757 self = MODEST_TNY_ACCOUNT_STORE (user_data);
1758 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1760 /* Remove the connection-status-changed handler if it's still there */
1761 if (modest_signal_mgr_is_connected (priv->sighandlers,
1763 "connection_status_changed")) {
1764 priv->sighandlers = modest_signal_mgr_disconnect (priv->sighandlers,
1766 "connection_status_changed");
1769 /* Cancel all pending operations */
1770 tny_account_cancel (TNY_ACCOUNT (account));
1772 /* Unref the extra reference added by get_server_account */
1773 g_object_unref (account);
1775 /* Clear the cache if it's an store account */
1776 if (TNY_IS_STORE_ACCOUNT (account)) {
1777 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (account));
1778 } else if (TNY_IS_TRANSPORT_ACCOUNT (account)) {
1779 ModestTnySendQueue* send_queue;
1780 send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (account), FALSE);
1781 if (TNY_IS_SEND_QUEUE (send_queue)) {
1782 if (modest_tny_send_queue_sending_in_progress (send_queue))
1783 tny_send_queue_cancel (TNY_SEND_QUEUE (send_queue),
1784 TNY_SEND_QUEUE_CANCEL_ACTION_REMOVE,
1786 modest_runtime_remove_send_queue (TNY_TRANSPORT_ACCOUNT (account));
1792 * We use this one for both removing "normal" and "connection
1793 * specific" transport accounts
1796 remove_transport_account (ModestTnyAccountStore *self,
1797 TnyTransportAccount *transport_account)
1799 ModestTnyAccountStorePrivate *priv;
1800 TnyFolder *outbox = NULL;
1802 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1804 /* Remove it from the list of accounts and notify the
1805 observers. Do not need to wait for account
1807 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
1808 tny_list_remove (priv->transport_accounts, (GObject *) transport_account);
1810 /* Remove the OUTBOX of the account from the global outbox */
1811 outbox = g_hash_table_lookup (priv->outbox_of_transport, transport_account);
1813 if (TNY_IS_FOLDER (outbox)) {
1814 TnyAccount *local_account = NULL;
1815 TnyAccount *outbox_account = tny_folder_get_account (outbox);
1817 if (outbox_account) {
1818 tny_list_remove (priv->store_accounts_outboxes, G_OBJECT (outbox_account));
1819 /* Remove existing emails to send */
1820 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (outbox_account));
1821 g_object_unref (outbox_account);
1824 local_account = modest_tny_account_store_get_local_folders_account (self);
1825 modest_tny_local_folders_account_remove_folder_from_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1828 g_hash_table_remove (priv->outbox_of_transport, transport_account);
1830 /* Notify the change in the local account */
1831 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1832 g_object_unref (local_account);
1834 g_warning ("Removing a transport account that has no outbox");
1837 /* Cancel all pending operations */
1838 tny_account_cancel (TNY_ACCOUNT (transport_account));
1840 /* Disconnect and notify the observers. The callback will free the reference */
1841 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account), FALSE,
1842 on_account_disconnect_when_removing, self);
1846 images_cache_remove_filter (TnyStreamCache *self, const gchar *id, const gchar *account_name)
1848 gchar *account_name_with_separator;
1850 if (account_name == NULL || account_name[0] == '\0')
1853 if (id == NULL || id[0] == '\0')
1856 account_name_with_separator = g_strconcat (account_name, "__", NULL);
1858 result = (g_str_has_prefix (id, account_name));
1859 g_free (account_name_with_separator);
1865 on_account_removed (ModestAccountMgr *acc_mgr,
1866 const gchar *account,
1869 TnyAccount *store_account = NULL, *transport_account = NULL;
1870 ModestTnyAccountStore *self;
1871 ModestTnyAccountStorePrivate *priv;
1872 TnyStreamCache *stream_cache;
1874 self = MODEST_TNY_ACCOUNT_STORE (user_data);
1875 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
1877 /* Get the server and the transport account */
1879 modest_tny_account_store_get_server_account (self, account,
1880 TNY_ACCOUNT_TYPE_STORE);
1882 modest_tny_account_store_get_server_account (self, account,
1883 TNY_ACCOUNT_TYPE_TRANSPORT);
1885 /* If there was any problem creating the account, for example,
1886 with the configuration system this could not exist */
1887 if (TNY_IS_STORE_ACCOUNT(store_account)) {
1888 /* Forget any cached password for the account */
1889 forget_password_in_memory (self, tny_account_get_id (store_account));
1891 /* Remove it from the list of accounts and notify the
1892 observers. Do not need to wait for account
1894 tny_list_remove (priv->store_accounts, (GObject *) store_account);
1895 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
1897 /* Cancel all pending operations */
1898 tny_account_cancel (TNY_ACCOUNT (store_account));
1900 /* Disconnect before deleting the cache, because the
1901 disconnection will rewrite the cache to the
1903 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account), FALSE,
1904 on_account_disconnect_when_removing, self);
1906 g_warning ("%s: no store account for account %s\n",
1907 __FUNCTION__, account);
1910 /* If there was any problem creating the account, for example,
1911 with the configuration system this could not exist */
1912 if (TNY_IS_TRANSPORT_ACCOUNT(transport_account)) {
1914 /* Forget any cached password for the account */
1915 forget_password_in_memory (self, tny_account_get_id (transport_account));
1917 /* Remove transport account. It'll free the reference
1918 added by get_server_account */
1919 remove_transport_account (self, TNY_TRANSPORT_ACCOUNT (transport_account));
1921 g_warning ("%s: no transport account for account %s\n",
1922 __FUNCTION__, account);
1925 /* Remove cached images */
1926 stream_cache = modest_runtime_get_images_cache ();
1927 tny_stream_cache_remove (stream_cache, (TnyStreamCacheRemoveFilter) images_cache_remove_filter, (gpointer) account);
1929 /* If there are no more user accounts then delete the
1930 transport specific SMTP servers */
1931 if (only_local_accounts (self))
1932 remove_connection_specific_transport_accounts (self);
1935 TnyTransportAccount *
1936 modest_tny_account_store_new_connection_specific_transport_account (ModestTnyAccountStore *self,
1939 ModestTnyAccountStorePrivate *priv = NULL;
1940 TnyAccount * tny_account = NULL;
1942 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1944 /* Add the account: */
1946 modest_tny_account_new_from_server_account_name (priv->account_mgr,
1952 g_object_set_data (G_OBJECT(tny_account),
1955 g_object_set_data (G_OBJECT(tny_account),
1956 "connection_specific",
1957 GINT_TO_POINTER (TRUE));
1959 tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
1960 add_outbox_from_transport_account_to_global_outbox (self,
1965 g_printerr ("modest: failed to create smtp-specific account for %s\n",
1968 return TNY_TRANSPORT_ACCOUNT (tny_account);
1973 add_connection_specific_transport_accounts (ModestTnyAccountStore *self)
1975 ModestTnyAccountStorePrivate *priv = NULL;
1976 GSList *list_specifics = NULL, *iter = NULL;
1979 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1981 list_specifics = modest_conf_get_list (modest_runtime_get_conf (),
1982 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
1983 MODEST_CONF_VALUE_STRING, &err);
1986 g_return_if_reached ();
1989 /* Look at each connection-specific transport account for the
1990 * modest account: */
1991 iter = list_specifics;
1993 /* The list alternates between the connection name and the transport name: */
1994 iter = g_slist_next (iter);
1996 const gchar* transport_account_name = (const gchar*) (iter->data);
1997 TnyTransportAccount * account = NULL;
1998 account = modest_tny_account_store_new_connection_specific_transport_account (
1999 self, transport_account_name);
2001 g_object_unref (account);
2003 iter = g_slist_next (iter);
2008 remove_connection_specific_transport_accounts (ModestTnyAccountStore *self)
2010 ModestTnyAccountStorePrivate *priv = NULL;
2011 GSList *list_specifics = NULL, *iter = NULL;
2014 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
2017 list_specifics = modest_conf_get_list (modest_runtime_get_conf (),
2018 MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
2019 MODEST_CONF_VALUE_STRING, &err);
2022 g_return_if_reached ();
2025 /* Look at each connection-specific transport account for the
2026 * modest account: */
2027 iter = list_specifics;
2029 /* The list alternates between the connection name and the transport name: */
2030 iter = g_slist_next (iter);
2032 const gchar* transport_account_name = (const gchar*) (iter->data);
2033 TnyAccount * account;
2034 account = modest_tny_account_store_get_server_account (self,
2035 transport_account_name,
2036 TNY_ACCOUNT_TYPE_TRANSPORT);
2038 /* the call will free the reference */
2040 remove_transport_account (self, TNY_TRANSPORT_ACCOUNT (account));
2042 iter = g_slist_next (iter);
2048 modest_tny_account_store_find_msg_in_outboxes (ModestTnyAccountStore *self,
2050 TnyAccount **ac_out)
2052 TnyIterator *acc_iter;
2053 ModestTnyAccountStorePrivate *priv;
2055 TnyAccount *msg_account = NULL;
2057 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
2058 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2060 acc_iter = tny_list_create_iterator (priv->store_accounts_outboxes);
2061 while (!msg && !tny_iterator_is_done (acc_iter)) {
2062 TnyList *folders = tny_simple_list_new ();
2063 TnyAccount *account = TNY_ACCOUNT (tny_iterator_get_current (acc_iter));
2064 TnyIterator *folders_iter = NULL;
2066 tny_folder_store_get_folders (TNY_FOLDER_STORE (account), folders, NULL, NULL);
2067 folders_iter = tny_list_create_iterator (folders);
2069 while (msg == NULL && !tny_iterator_is_done (folders_iter)) {
2070 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (folders_iter));
2071 msg = tny_folder_find_msg (folder, uri, NULL);
2074 msg_account = g_object_ref (account);
2076 g_object_unref (folder);
2077 tny_iterator_next (folders_iter);
2079 g_object_unref (folders_iter);
2081 g_object_unref (folders);
2082 g_object_unref (account);
2083 tny_iterator_next (acc_iter);
2086 g_object_unref (acc_iter);
2089 *ac_out = msg_account;
2094 TnyTransportAccount *
2095 modest_tny_account_store_get_transport_account_from_outbox_header(ModestTnyAccountStore *self, TnyHeader *header)
2097 TnyIterator *acc_iter;
2098 ModestTnyAccountStorePrivate *priv;
2099 TnyTransportAccount *header_acc = NULL;
2102 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
2103 g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
2104 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2105 msg_id = modest_tny_send_queue_get_msg_id (header);
2106 acc_iter = tny_list_create_iterator (priv->transport_accounts);
2107 while (!header_acc && !tny_iterator_is_done (acc_iter)) {
2108 TnyTransportAccount *account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current (acc_iter));
2109 ModestTnySendQueue *send_queue;
2110 ModestTnySendQueueStatus status;
2111 send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account), TRUE);
2112 if (TNY_IS_SEND_QUEUE (send_queue)) {
2113 status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
2114 if (status != MODEST_TNY_SEND_QUEUE_UNKNOWN)
2115 header_acc = g_object_ref(account);
2117 g_object_unref (account);
2118 tny_iterator_next (acc_iter);
2120 g_object_unref(acc_iter);
2128 ModestTnyAccountStore *account_store;
2129 ModestTnyAccountStoreShutdownCallback callback;
2135 account_shutdown_callback (TnyCamelAccount *account, gboolean canceled, GError *err, gpointer userdata)
2137 ShutdownOpData *op_data = (ShutdownOpData *) userdata;
2139 if (op_data->pending == 0) {
2140 if (op_data->callback)
2141 op_data->callback (op_data->account_store, op_data->userdata);
2142 g_object_unref (op_data->account_store);
2145 g_object_unref (op_data->account_store);
2150 account_shutdown (TnyAccount *account, ShutdownOpData *op_data)
2152 g_return_if_fail (account && TNY_IS_ACCOUNT(account));
2154 if (TNY_IS_STORE_ACCOUNT (account) &&
2155 !modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
2158 /* Disconnect account */
2159 if (tny_account_get_connection_status (account) == TNY_CONNECTION_STATUS_CONNECTED) {
2160 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(account), FALSE,
2161 account_shutdown_callback, op_data);
2167 g_object_unref (op_data->account_store);
2172 modest_tny_account_store_shutdown (ModestTnyAccountStore *self,
2173 ModestTnyAccountStoreShutdownCallback callback,
2176 gint i, num_accounts;
2177 ShutdownOpData *op_data;
2178 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
2180 /* Get references */
2181 num_accounts = tny_list_get_length (priv->store_accounts) +
2182 tny_list_get_length (priv->transport_accounts);
2183 for (i = 0 ; i < num_accounts ; i++)
2184 g_object_ref (self);
2186 /* Create the helper object */
2187 op_data = g_new0 (ShutdownOpData, 1);
2188 op_data->callback = callback;
2189 op_data->userdata = userdata;
2190 op_data->pending = num_accounts;
2191 op_data->account_store = self;
2193 /* Destroy all accounts. Disconnect all accounts before they are destroyed */
2194 if (priv->store_accounts) {
2195 tny_list_foreach (priv->store_accounts, (GFunc)account_shutdown, op_data);
2198 if (priv->transport_accounts) {
2199 tny_list_foreach (priv->transport_accounts, (GFunc)account_shutdown, op_data);
2202 if (op_data->pending == 0) {
2203 if (op_data->callback)
2204 op_data->callback (op_data->account_store, op_data->userdata);