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-account.h>
34 #include <tny-account-store.h>
35 #include <tny-store-account.h>
36 #include <tny-error.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-info.h>
47 #include <modest-local-folder-info.h>
48 #include <modest-tny-account.h>
49 #include <modest-tny-local-folders-account.h>
50 #include <modest-account-mgr.h>
51 #include <modest-account-mgr-helpers.h>
52 #include <widgets/modest-window-mgr.h>
53 #include <modest-account-settings-dialog.h>
54 #include <maemo/modest-maemo-utils.h>
57 #include "modest-tny-account-store.h"
58 #include "modest-tny-platform-factory.h"
59 #include <tny-gtk-lockable.h>
60 #include <camel/camel.h>
62 #ifdef MODEST_PLATFORM_MAEMO
63 #include <tny-maemo-conic-device.h>
64 #ifdef MODEST_HAVE_HILDON0_WIDGETS
65 #include <hildon-widgets/hildon-note.h>
66 #include <hildon-widgets/hildon-banner.h>
68 #include <hildon/hildon-note.h>
69 #include <hildon/hildon-banner.h>
73 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
75 /* 'private'/'protected' functions */
76 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
78 static void modest_tny_account_store_finalize (GObject *obj);
80 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
82 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
84 static void modest_tny_account_store_base_init (gpointer g_class);
86 static void on_account_inserted (ModestAccountMgr *acc_mgr,
90 static void add_existing_accounts (ModestTnyAccountStore *self);
92 static void insert_account (ModestTnyAccountStore *self,
96 static void on_account_removed (ModestAccountMgr *acc_mgr,
100 static gchar* get_password (TnyAccount *account,
101 const gchar * prompt_not_used,
104 static void forget_password (TnyAccount *account);
106 static void on_vfs_volume_mounted (GnomeVFSVolumeMonitor *volume_monitor,
107 GnomeVFSVolume *volume,
110 static void on_vfs_volume_unmounted (GnomeVFSVolumeMonitor *volume_monitor,
111 GnomeVFSVolume *volume,
114 static void modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self,
115 const gchar *server_account_name);
117 static void add_connection_specific_transport_accounts (ModestTnyAccountStore *self,
118 const gchar *account_name);
120 /* list my signals */
122 ACCOUNT_CHANGED_SIGNAL,
123 ACCOUNT_INSERTED_SIGNAL,
124 ACCOUNT_REMOVED_SIGNAL,
126 PASSWORD_REQUESTED_SIGNAL,
130 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
131 struct _ModestTnyAccountStorePrivate {
133 GHashTable *password_hash;
134 GHashTable *account_settings_dialog_hash;
135 ModestAccountMgr *account_mgr;
136 TnySessionCamel *session;
139 gulong acc_inserted_handler;
140 gulong acc_changed_handler;
141 gulong acc_removed_handler;
142 gulong volume_mounted_handler;
143 gulong volume_unmounted_handler;
145 /* We cache the lists of accounts here */
146 TnyList *store_accounts;
147 TnyList *transport_accounts;
148 TnyList *store_accounts_outboxes;
151 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
152 MODEST_TYPE_TNY_ACCOUNT_STORE, \
153 ModestTnyAccountStorePrivate))
156 static GObjectClass *parent_class = NULL;
158 static guint signals[LAST_SIGNAL] = {0};
161 modest_tny_account_store_get_type (void)
163 static GType my_type = 0;
166 static const GTypeInfo my_info = {
167 sizeof(ModestTnyAccountStoreClass),
168 modest_tny_account_store_base_init, /* base init */
169 NULL, /* base finalize */
170 (GClassInitFunc) modest_tny_account_store_class_init,
171 NULL, /* class finalize */
172 NULL, /* class data */
173 sizeof(ModestTnyAccountStore),
175 (GInstanceInitFunc) modest_tny_account_store_instance_init,
179 static const GInterfaceInfo iface_info = {
180 (GInterfaceInitFunc) modest_tny_account_store_init,
181 NULL, /* interface_finalize */
182 NULL /* interface_data */
185 my_type = g_type_register_static (G_TYPE_OBJECT,
186 "ModestTnyAccountStore",
188 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
196 modest_tny_account_store_base_init (gpointer g_class)
198 static gboolean tny_account_store_initialized = FALSE;
200 if (!tny_account_store_initialized) {
202 signals[ACCOUNT_CHANGED_SIGNAL] =
203 g_signal_new ("account_changed",
204 MODEST_TYPE_TNY_ACCOUNT_STORE,
206 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_changed),
208 g_cclosure_marshal_VOID__OBJECT,
209 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
211 signals[ACCOUNT_INSERTED_SIGNAL] =
212 g_signal_new ("account_inserted",
213 MODEST_TYPE_TNY_ACCOUNT_STORE,
215 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_inserted),
217 g_cclosure_marshal_VOID__OBJECT,
218 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
220 signals[ACCOUNT_REMOVED_SIGNAL] =
221 g_signal_new ("account_removed",
222 MODEST_TYPE_TNY_ACCOUNT_STORE,
224 G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_removed),
226 g_cclosure_marshal_VOID__OBJECT,
227 G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
229 /* signals[TNY_ACCOUNT_STORE_CONNECTING_FINISHED] = */
230 /* g_signal_new ("connecting_finished", */
231 /* TNY_TYPE_ACCOUNT_STORE, */
232 /* G_SIGNAL_RUN_FIRST, */
233 /* G_STRUCT_OFFSET (TnyAccountStoreIface, connecting_finished), */
235 /* g_cclosure_marshal_VOID__VOID, */
236 /* G_TYPE_NONE, 0); */
238 signals[PASSWORD_REQUESTED_SIGNAL] =
239 g_signal_new ("password_requested",
240 MODEST_TYPE_TNY_ACCOUNT_STORE,
242 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
244 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
245 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
248 tny_account_store_initialized = TRUE;
254 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
256 GObjectClass *gobject_class;
257 gobject_class = (GObjectClass*) klass;
259 parent_class = g_type_class_peek_parent (klass);
260 gobject_class->finalize = modest_tny_account_store_finalize;
262 g_type_class_add_private (gobject_class,
263 sizeof(ModestTnyAccountStorePrivate));
267 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
269 GnomeVFSVolumeMonitor* monitor = NULL;
270 ModestTnyAccountStorePrivate *priv;
272 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
274 priv->cache_dir = NULL;
275 priv->account_mgr = NULL;
276 priv->session = NULL;
279 /* An in-memory store of passwords,
280 * for passwords that are not remembered in the configuration,
281 * so they need to be asked for from the user once in each session:
283 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
286 /* A hash-map of modest account names to dialog pointers,
287 * so we can avoid showing the account settings twice for the same modest account: */
288 priv->account_settings_dialog_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
291 /* Respond to volume mounts and unmounts, such
292 * as the insertion/removal of the memory card: */
293 monitor = gnome_vfs_get_volume_monitor();
295 priv->volume_mounted_handler = g_signal_connect (G_OBJECT(monitor),
297 G_CALLBACK(on_vfs_volume_mounted),
300 priv->volume_unmounted_handler = g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
301 G_CALLBACK(on_vfs_volume_unmounted),
305 /* disconnect the list of TnyAccounts */
307 foreach_account_disconnect (gpointer data,
310 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(data), FALSE, NULL);
315 foreach_account_append_to_list (gpointer data,
320 list = TNY_LIST (user_data);
321 tny_list_append (list, G_OBJECT (data));
324 /********************************************************************/
325 /* Control the state of the MMC local account */
326 /********************************************************************/
328 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
329 GnomeVFSVolume *volume,
332 ModestTnyAccountStore *self;
333 ModestTnyAccountStorePrivate *priv;
337 self = MODEST_TNY_ACCOUNT_STORE(user_data);
338 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
340 /* Check whether this was the external MMC1 card: */
341 uri = gnome_vfs_volume_get_activation_uri (volume);
343 if (uri && (!strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI))) {
344 TnyAccount *mmc_account;
346 mmc_account = modest_tny_account_new_for_local_folders (priv->account_mgr,
348 MODEST_MCC1_VOLUMEPATH);
350 /* Add to the list of store accounts */
351 tny_list_append (priv->store_accounts, G_OBJECT (mmc_account));
353 g_signal_emit (G_OBJECT (self),
354 signals [ACCOUNT_INSERTED_SIGNAL],
357 g_object_unref (mmc_account);
363 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
364 GnomeVFSVolume *volume,
367 ModestTnyAccountStore *self;
368 ModestTnyAccountStorePrivate *priv;
371 self = MODEST_TNY_ACCOUNT_STORE(user_data);
372 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
374 /* Check whether this was the external MMC1 card: */
375 uri = gnome_vfs_volume_get_activation_uri (volume);
376 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
377 TnyAccount *mmc_account = NULL;
378 gboolean found = FALSE;
379 TnyIterator *iter = NULL;
381 iter = tny_list_create_iterator (priv->store_accounts);
382 while (!tny_iterator_is_done (iter) && !found) {
385 account = TNY_ACCOUNT (tny_iterator_get_current (iter));
386 if (modest_tny_account_is_memory_card_account (account)) {
388 mmc_account = g_object_ref (account);
390 g_object_unref (account);
391 tny_iterator_next (iter);
393 g_object_unref (iter);
396 /* Remove from the list */
397 tny_list_remove (priv->store_accounts, G_OBJECT (mmc_account));
399 /* Notify observers */
400 g_signal_emit (G_OBJECT (self),
401 signals [ACCOUNT_REMOVED_SIGNAL],
404 g_object_unref (mmc_account);
406 g_warning ("%s: there was no store account for the unmounted MMC",
414 * modest_tny_account_store_forget_password_in_memory
415 * @self: a TnyAccountStore instance
416 * @account: A server account.
418 * Forget any password stored in memory for this account.
419 * For instance, this should be called when the user has changed the password in the account settings.
422 modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, const gchar * server_account_name)
424 /* printf ("DEBUG: %s\n", __FUNCTION__); */
425 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
427 if (server_account_name && priv->password_hash) {
428 g_hash_table_remove (priv->password_hash, server_account_name);
434 update_tny_account_for_account (ModestTnyAccountStore *self, ModestAccountMgr *acc_mgr,
435 const gchar *account_name, TnyAccountType type)
437 ModestTnyAccountStorePrivate *priv;
438 TnyList* account_list;
439 gboolean found = FALSE;
440 TnyIterator *iter = NULL;
442 g_return_val_if_fail (self, FALSE);
443 g_return_val_if_fail (account_name, FALSE);
444 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE ||
445 type == TNY_ACCOUNT_TYPE_TRANSPORT,
448 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
449 account_list = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
451 iter = tny_list_create_iterator (account_list);
452 while (!tny_iterator_is_done (iter) && !found) {
453 TnyAccount *tny_account;
454 tny_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
456 const gchar* parent_name =
457 modest_tny_account_get_parent_modest_account_name_for_server_account (tny_account);
458 if (parent_name && strcmp (parent_name, account_name) == 0) {
460 modest_tny_account_update_from_account (tny_account, acc_mgr, account_name, type);
461 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, tny_account);
463 g_object_unref (tny_account);
465 tny_iterator_next (iter);
469 g_object_unref (iter);
476 on_account_changed (ModestAccountMgr *acc_mgr,
477 const gchar *account_name,
480 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
482 g_debug ("DEBUG: modest: %s\n", __FUNCTION__);
484 /* Ignore the change if it's a change in the last_updated value */
485 // if (key && g_str_has_suffix ((const gchar *) key, MODEST_ACCOUNT_LAST_UPDATED))
488 if (!update_tny_account_for_account (self, acc_mgr, account_name, TNY_ACCOUNT_TYPE_STORE))
489 g_warning ("%s: failed to update store account for %s", __FUNCTION__, account_name);
490 if (!update_tny_account_for_account (self, acc_mgr, account_name, TNY_ACCOUNT_TYPE_TRANSPORT))
491 g_warning ("%s: failed to update transport account for %s", __FUNCTION__, account_name);
495 on_account_settings_hide (GtkWidget *widget, gpointer user_data)
497 TnyAccount *account = (TnyAccount*)user_data;
499 /* This is easier than using a struct for the user_data: */
500 ModestTnyAccountStore *self = modest_runtime_get_account_store();
501 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
503 const gchar *modest_account_name =
504 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
505 if (modest_account_name)
506 g_hash_table_remove (priv->account_settings_dialog_hash, modest_account_name);
510 on_idle_wrong_password_warning_only (gpointer user_data)
514 ModestWindow *main_window =
515 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
517 /* Show an explanatory temporary banner: */
518 hildon_banner_show_information (
519 GTK_WIDGET(main_window), NULL, _("mcen_ib_username_pw_incorrect"));
523 return FALSE; /* Don't show again. */
527 on_idle_wrong_password (gpointer user_data)
529 TnyAccount *account = (TnyAccount*)user_data;
530 /* This is easier than using a struct for the user_data: */
531 ModestTnyAccountStore *self = modest_runtime_get_account_store();
532 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
534 const gchar *modest_account_name =
535 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
536 if (!modest_account_name) {
537 g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n",
540 g_object_unref (account);
545 /* Check whether this window is already open,
546 * for instance because of a previous get_password() call:
548 gpointer dialog_as_gpointer = NULL;
549 gboolean found = FALSE;
550 if (priv->account_settings_dialog_hash) {
551 found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
552 modest_account_name, NULL, (gpointer*)&dialog_as_gpointer);
554 ModestAccountSettingsDialog *dialog = dialog_as_gpointer;
556 ModestWindow *main_window =
557 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
559 gdk_threads_enter ();
560 gboolean created_dialog = FALSE;
561 if (!found || !dialog) {
562 dialog = modest_account_settings_dialog_new ();
563 modest_account_settings_dialog_set_account_name (dialog, modest_account_name);
564 modest_account_settings_dialog_switch_to_user_info (dialog);
566 g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (modest_account_name), dialog);
568 created_dialog = TRUE;
571 /* Show an explanatory temporary banner: */
572 hildon_banner_show_information (
573 GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
575 if (created_dialog) {
576 /* Forget it when it closes: */
577 g_signal_connect_object (G_OBJECT (dialog), "hide", G_CALLBACK (on_account_settings_hide),
580 /* Show it and delete it when it closes: */
581 modest_maemo_show_dialog_and_forget (GTK_WINDOW (main_window), GTK_DIALOG (dialog));
584 /* Just show it instead of showing it and deleting it when it closes,
585 * though it is probably open already: */
586 gtk_window_present (GTK_WINDOW (dialog));
589 g_object_unref (account);
590 gdk_threads_leave ();
592 return FALSE; /* Dont' call this again. */
598 ModestTnyAccountStore* account_store;
599 const gchar* server_account_id;
604 } IdlePasswordRequest;
607 on_idle_request_password (gpointer user_data)
611 IdlePasswordRequest* info = (IdlePasswordRequest*)user_data;
612 g_signal_emit (G_OBJECT(info->account_store), signals[PASSWORD_REQUESTED_SIGNAL], 0,
613 info->server_account_id, /* server_account_name */
614 info->username, info->password, info->cancel, info->remember);
617 g_main_loop_quit (info->loop);
621 return FALSE; /* Don't call again. */
625 request_password_in_main_loop_and_wait (ModestTnyAccountStore *account_store,
626 const gchar* server_account_id,
632 IdlePasswordRequest *data = g_slice_new0 (IdlePasswordRequest);
633 data->account_store = account_store;
634 data->server_account_id = server_account_id;
635 data->username = username;
636 data->password = password;
637 data->cancel = cancel;
638 data->remember = remember;
640 data->loop = g_main_loop_new (NULL, FALSE /* not running */);
642 /* Cause the function to be run in an idle-handler, which is always
643 * in the main thread:
645 g_idle_add (&on_idle_request_password, data);
647 /* This main loop will run until the idle handler has stopped it: */
648 printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
650 g_main_loop_run (data->loop);
652 printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
653 printf ("DEBUG: %s: Finished\n", __FUNCTION__);
654 g_main_loop_unref (data->loop);
656 g_slice_free (IdlePasswordRequest, data);
659 /* This callback will be called by Tinymail when it needs the password
660 * from the user or the account settings.
661 * It can also call forget_password() before calling this,
662 * so that we clear wrong passwords out of our account settings.
663 * Note that TnyAccount here will be the server account. */
665 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
667 /* TODO: Settting cancel to FALSE does not actually cancel everything.
668 * We still get multiple requests afterwards, so we end up showing the
669 * same dialogs repeatedly.
672 printf ("DEBUG: modest: %s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
674 g_return_val_if_fail (account, NULL);
676 const TnyAccountStore *account_store = NULL;
677 ModestTnyAccountStore *self = NULL;
678 ModestTnyAccountStorePrivate *priv;
679 gchar *username = NULL;
681 gpointer pwd_ptr = NULL;
682 gboolean already_asked = FALSE;
684 /* Initialize the output parameter: */
688 const gchar *server_account_name = tny_account_get_id (account);
689 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
692 if (!server_account_name || !account_store) {
693 g_warning ("modest: %s: could not retrieve account_store for account %s",
694 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
701 self = MODEST_TNY_ACCOUNT_STORE (account_store);
702 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
704 /* This hash map stores passwords, including passwords that are not stored in gconf. */
705 /* Is it in the hash? if it's already there, it must be wrong... */
706 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
707 * type-punned ptrs...*/
708 already_asked = priv->password_hash &&
709 g_hash_table_lookup_extended (priv->password_hash,
712 (gpointer*)&pwd_ptr);
714 printf ("DEBUG: modest: %s: Already asked = %d\n", __FUNCTION__, already_asked);
716 /* If the password is not already there, try ModestConf */
717 if (!already_asked) {
718 pwd = modest_server_account_get_password (priv->account_mgr,
719 server_account_name);
720 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
723 /* If it was already asked, it must have been wrong, so ask again */
724 if (already_asked || !pwd || strlen(pwd) == 0) {
725 /* As per the UI spec, if no password was set in the account settings,
726 * ask for it now. But if the password is wrong in the account settings,
727 * then show a banner and the account settings dialog so it can be corrected:
729 const gboolean settings_have_password =
730 modest_server_account_get_has_password (priv->account_mgr, server_account_name);
731 printf ("DEBUG: modest: %s: settings_have_password=%d\n", __FUNCTION__, settings_have_password);
732 if (settings_have_password) {
735 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
736 /* We show it in the main loop, because this function might not be in the main loop. */
737 g_object_ref (account); /* unrefed in the idle handler. */
738 g_idle_add (on_idle_wrong_password, account);
746 /* we don't have it yet. Get the password from the user */
747 const gchar* account_id = tny_account_get_id (account);
748 gboolean remember = FALSE;
752 /* Show an info banner, before we show the protected password dialog: */
753 g_idle_add (on_idle_wrong_password_warning_only, NULL);
756 request_password_in_main_loop_and_wait (self, account_id,
757 &username, &pwd, cancel, &remember);
760 /* The password will be returned as the result,
761 * but we need to tell tinymail about the username too: */
762 tny_account_set_user (account, username);
764 /* Do not save the password in gconf,
765 * because the UI spec says "The password will never be saved in the account": */
768 printf ("%s: Storing username=%s, password=%s\n",
769 __FUNCTION__, username, pwd);
770 modest_server_account_set_username (priv->account_mgr, server_account_name,
772 modest_server_account_set_password (priv->account_mgr, server_account_name,
777 /* We need to dup the string even knowing that
778 it's already a dup of the contents of an
779 entry, because it if it's wrong, then camel
781 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
783 g_hash_table_remove (priv->password_hash, server_account_name);
794 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
799 /* tinymail calls this if the connection failed due to an incorrect password.
800 * And it seems to call this for any general connection failure. */
802 forget_password (TnyAccount *account)
804 printf ("DEBUG: %s\n", __FUNCTION__);
805 ModestTnyAccountStore *self;
806 ModestTnyAccountStorePrivate *priv;
807 const TnyAccountStore *account_store;
811 account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
813 self = MODEST_TNY_ACCOUNT_STORE (account_store);
814 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
815 key = tny_account_get_id (account);
817 /* Do not remove the key, this will allow us to detect that we
818 have already asked for it at least once */
819 pwd = g_hash_table_lookup (priv->password_hash, key);
821 memset (pwd, 0, strlen (pwd));
822 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
825 /* Remove from configuration system */
827 modest_account_mgr_unset (priv->account_mgr,
828 key, MODEST_ACCOUNT_PASSWORD, TRUE);
833 modest_tny_account_store_finalize (GObject *obj)
835 GnomeVFSVolumeMonitor *volume_monitor;
836 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
837 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
839 g_free (priv->cache_dir);
840 priv->cache_dir = NULL;
842 if (priv->password_hash) {
843 g_hash_table_destroy (priv->password_hash);
844 priv->password_hash = NULL;
847 /* Disconnect VFS signals */
848 volume_monitor = gnome_vfs_get_volume_monitor ();
849 if (g_signal_handler_is_connected (volume_monitor,
850 priv->volume_mounted_handler))
851 g_signal_handler_disconnect (volume_monitor,
852 priv->volume_mounted_handler);
853 if (g_signal_handler_is_connected (volume_monitor,
854 priv->volume_unmounted_handler))
855 g_signal_handler_disconnect (volume_monitor,
856 priv->volume_unmounted_handler);
858 if (priv->account_mgr) {
859 /* Disconnect signals */
860 if (g_signal_handler_is_connected (priv->account_mgr,
861 priv->acc_inserted_handler))
862 g_signal_handler_disconnect (priv->account_mgr,
863 priv->acc_inserted_handler);
864 if (g_signal_handler_is_connected (priv->account_mgr,
865 priv->acc_changed_handler))
866 g_signal_handler_disconnect (priv->account_mgr,
867 priv->acc_changed_handler);
868 if (g_signal_handler_is_connected (priv->account_mgr,
869 priv->acc_removed_handler))
870 g_signal_handler_disconnect (priv->account_mgr,
871 priv->acc_removed_handler);
873 g_object_unref (G_OBJECT(priv->account_mgr));
874 priv->account_mgr = NULL;
878 g_object_unref (G_OBJECT(priv->device));
882 /* Destroy all accounts. Disconnect all accounts before they are destroyed */
883 if (priv->store_accounts) {
884 tny_list_foreach (priv->store_accounts, foreach_account_disconnect, NULL);
885 g_object_unref (priv->store_accounts);
886 priv->store_accounts = NULL;
889 if (priv->transport_accounts) {
890 tny_list_foreach (priv->transport_accounts, foreach_account_disconnect, NULL);
891 g_object_unref (priv->transport_accounts);
892 priv->transport_accounts = NULL;
895 if (priv->store_accounts_outboxes) {
896 g_object_unref (priv->store_accounts_outboxes);
897 priv->store_accounts_outboxes = NULL;
901 camel_object_unref (CAMEL_OBJECT(priv->session));
902 priv->session = NULL;
905 G_OBJECT_CLASS(parent_class)->finalize (obj);
909 ModestTnyAccountStore*
910 modest_tny_account_store_new (ModestAccountMgr *account_mgr,
914 ModestTnyAccountStorePrivate *priv;
915 TnyAccount *local_account = NULL;
917 g_return_val_if_fail (account_mgr, NULL);
918 g_return_val_if_fail (device, NULL);
920 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
921 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
923 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
924 priv->device = g_object_ref (device);
926 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
927 if (!priv->session) {
928 g_warning ("failed to get TnySessionCamel");
932 /* Set the ui locker */
933 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
935 /* Connect signals */
936 priv->acc_inserted_handler = g_signal_connect (G_OBJECT(account_mgr), "account_inserted",
937 G_CALLBACK (on_account_inserted), obj);
938 priv->acc_changed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
939 G_CALLBACK (on_account_changed), obj);
940 priv->acc_removed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
941 G_CALLBACK (on_account_removed), obj);
943 /* Create the lists of accounts */
944 priv->store_accounts = tny_simple_list_new ();
945 priv->transport_accounts = tny_simple_list_new ();
946 priv->store_accounts_outboxes = tny_simple_list_new ();
948 /* Create the local folders account */
950 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session, NULL);
951 tny_list_append (priv->store_accounts, G_OBJECT(local_account));
952 g_object_unref (local_account);
954 /* Add the other remote accounts. Do this before adding the
955 local account, because we need to add our outboxes to the
956 global OUTBOX hosted in the local account */
957 add_existing_accounts (MODEST_TNY_ACCOUNT_STORE (obj));
959 return MODEST_TNY_ACCOUNT_STORE(obj);
963 modest_tny_account_store_get_accounts (TnyAccountStore *self,
965 TnyGetAccountsRequestType request_type)
967 ModestTnyAccountStorePrivate *priv;
969 g_return_if_fail (self);
970 g_return_if_fail (TNY_IS_LIST(list));
972 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
974 switch (request_type) {
975 case TNY_ACCOUNT_STORE_BOTH:
976 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
977 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
979 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
980 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
982 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
983 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
986 g_return_if_reached ();
989 /* Initialize session. Why do we need this ??? */
990 tny_session_camel_set_initialized (priv->session);
995 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
997 ModestTnyAccountStorePrivate *priv;
998 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1000 if (!priv->cache_dir)
1001 priv->cache_dir = g_build_filename (g_get_home_dir(),
1002 MODEST_DIR, MODEST_CACHE_DIR, NULL);
1003 return priv->cache_dir;
1008 * callers need to unref
1011 modest_tny_account_store_get_device (TnyAccountStore *self)
1013 ModestTnyAccountStorePrivate *priv;
1015 g_return_val_if_fail (self, NULL);
1017 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1020 return g_object_ref (G_OBJECT(priv->device));
1027 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1029 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1030 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1037 modest_tny_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type,
1038 gboolean question, const GError *error)
1040 g_return_val_if_fail (error, FALSE);
1042 if ((error->domain != TNY_ACCOUNT_ERROR)
1043 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
1044 g_warning("modest: %s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
1045 __FUNCTION__, error->domain, error->message);
1050 printf("DEBUG: %s: GError code: %d, message=%s\n",
1051 __FUNCTION__, error->code, error->message);
1053 /* Get the server name: */
1054 const gchar* server_name = NULL;
1055 if (account && TNY_IS_ACCOUNT (account)) {
1056 server_name = tny_account_get_hostname (account);
1057 printf ("modest: %s: account name = %s, server_name=%s\n", __FUNCTION__,
1058 tny_account_get_id (account), server_name);
1062 server_name = _("Unknown Server");
1064 ModestTransportStoreProtocol proto = MODEST_PROTOCOL_STORE_POP; /* Arbitrary default. */
1066 const gchar *proto_name = tny_account_get_proto (account);
1068 proto = modest_protocol_info_get_transport_store_protocol (proto_name);
1070 g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__,
1071 tny_account_get_id (account));
1075 /* const gchar *prompt = NULL; */
1076 gchar *prompt = NULL;
1077 switch (error->code) {
1078 case TNY_ACCOUNT_STORE_ERROR_CANCEL_ALERT:
1079 case TNY_ACCOUNT_ERROR_TRY_CONNECT_USER_CANCEL:
1080 /* Don't show waste the user's time by showing him a dialog telling
1081 * him that he has just cancelled something: */
1082 g_debug ("%s: Handling GError domain=%d, code=%d (cancelled) without showing a dialog, message=%s",
1083 __FUNCTION__, error->domain, error->code, error->message);
1087 case TNY_ACCOUNT_ERROR_TRY_CONNECT_HOST_LOOKUP_FAILED:
1088 /* TODO: Show the appropriate message, depending on whether it's POP or IMAP: */
1089 g_debug ("%s: Handling GError domain=%d, code=%d (lookup failed), message=%s",
1090 __FUNCTION__, error->domain, error->code, error->message);
1093 case MODEST_PROTOCOL_STORE_POP:
1094 prompt = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), server_name);
1096 case MODEST_PROTOCOL_STORE_IMAP:
1097 prompt = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), server_name);
1099 case MODEST_PROTOCOL_TRANSPORT_SMTP:
1100 default: /* Arbitrary default. */
1101 prompt = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
1106 prompt = g_strdup_printf(
1107 _("Incorrect Account Settings:\n Host lookup failed.%s"),
1112 case TNY_ACCOUNT_ERROR_TRY_CONNECT_AUTHENTICATION_NOT_SUPPORTED:
1113 g_debug ("%s: Handling GError domain=%d, code=%d (authentication not supported), message=%s",
1114 __FUNCTION__, error->domain, error->code, error->message);
1115 /* TODO: This needs a logical ID for the string: */
1116 prompt = g_strdup_printf(
1117 _("Incorrect Account Settings:\nThe secure authentication method is not supported.\n%s"),
1121 case TNY_ACCOUNT_ERROR_TRY_CONNECT_CERTIFICATE:
1122 g_debug ("%s: Handling GError domain=%d, code=%d (certificatae), message=%s",
1123 __FUNCTION__, error->domain, error->code, error->message);
1124 prompt = g_strdup_printf(
1125 _("Certificate Problem:\n%s"),
1129 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
1130 /* The tinymail camel implementation just sends us this for almost
1131 * everything, so we have to guess at the cause.
1132 * It could be a wrong password, or inability to resolve a hostname,
1133 * or lack of network, or incorrect authentication method, or something entirely different: */
1134 /* TODO: Fix camel to provide specific error codes, and then use the
1135 * specific dialog messages from Chapter 12 of the UI spec.
1137 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
1138 /* This debug output is useful. Please keep it uncommented until
1139 * we have fixed the problems in this function: */
1140 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
1141 __FUNCTION__, error->domain, error->code, error->message);
1143 /* TODO: Remove the internal error message for the real release.
1144 * This is just so the testers can give us more information: */
1145 /* prompt = _("Modest account not yet fully configured."); */
1146 prompt = g_strdup_printf(
1147 "%s\n (Internal error message, often very misleading):\n%s",
1148 _("Incorrect Account Settings"),
1151 /* Note: If the password was wrong then get_password() would be called again,
1152 * instead of this vfunc being called. */
1157 g_warning ("%s: Unhandled GError code: %d, message=%s",
1158 __FUNCTION__, error->code, error->message);
1166 ModestWindow *main_window =
1167 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
1168 gboolean retval = TRUE;
1170 /* The Tinymail documentation says that we should show Yes and No buttons,
1171 * when it is a question.
1172 * Obviously, we need tinymail to use more specific error codes instead,
1173 * so we know what buttons to show. */
1175 /* TODO: Do this in the main context: */
1176 GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_window),
1178 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1180 retval = (response == GTK_RESPONSE_YES) ||
1181 (response == GTK_RESPONSE_OK);
1184 gtk_widget_destroy (dialog);
1187 /* Just show the error text and use the default response: */
1188 modest_maemo_show_information_note_in_main_context_and_forget (GTK_WINDOW (main_window),
1192 /* TODO: Don't free this when we no longer strdup the message for testers. */
1196 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1202 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1204 TnyAccountStoreIface *klass;
1206 g_return_if_fail (g);
1208 klass = (TnyAccountStoreIface *)g;
1210 klass->get_accounts_func =
1211 modest_tny_account_store_get_accounts;
1212 klass->get_cache_dir_func =
1213 modest_tny_account_store_get_cache_dir;
1214 klass->get_device_func =
1215 modest_tny_account_store_get_device;
1217 modest_tny_account_store_alert;
1218 klass->find_account_func =
1219 modest_tny_account_store_find_account_by_url;
1223 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1224 ModestTnyGetPassFunc func)
1226 /* not implemented, we use signals */
1227 g_printerr ("modest: set_get_pass_func not implemented\n");
1231 modest_tny_account_store_get_session (TnyAccountStore *self)
1233 g_return_val_if_fail (self, NULL);
1234 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1238 get_tny_account_by (TnyList *accounts,
1239 ModestTnyAccountStoreQueryType type,
1242 TnyIterator *iter = NULL;
1243 gboolean found = FALSE;
1244 TnyAccount *retval = NULL;
1246 iter = tny_list_create_iterator (accounts);
1247 while (!tny_iterator_is_done (iter) && !found) {
1248 TnyAccount *tmp_account = NULL;
1249 const gchar *val = NULL;
1251 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1253 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1254 val = tny_account_get_id (tmp_account);
1256 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1257 val = tny_account_get_url_string (tmp_account);
1261 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1262 tny_account_matches_url_string (tmp_account, str)) {
1263 retval = g_object_ref (tmp_account);
1266 if (strcmp (val, str) == 0) {
1267 retval = g_object_ref (tmp_account);
1271 g_object_unref (tmp_account);
1272 tny_iterator_next (iter);
1274 g_object_unref (iter);
1280 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1281 ModestTnyAccountStoreQueryType type,
1284 TnyAccount *account = NULL;
1285 ModestTnyAccountStorePrivate *priv;
1287 g_return_val_if_fail (self, NULL);
1288 g_return_val_if_fail (str, NULL);
1290 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1292 /* Search in store accounts */
1293 account = get_tny_account_by (priv->store_accounts, type, str);
1295 /* If we already found something, no need to search the transport accounts */
1297 account = get_tny_account_by (priv->transport_accounts, type, str);
1299 /* If we already found something, no need to search the
1300 per-account outbox accounts */
1302 account = get_tny_account_by (priv->store_accounts_outboxes, type, str);
1305 /* Warn if nothing was found. This is generally unusual. */
1307 g_warning("%s: Failed to find account with %s=%s\n",
1309 (type == MODEST_TNY_ACCOUNT_STORE_QUERY_ID) ? "ID" : "URL",
1313 /* Returns a new reference to the account if found */
1318 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1319 const gchar *account_name,
1320 TnyAccountType type)
1322 ModestTnyAccountStorePrivate *priv = NULL;
1323 TnyAccount *retval = NULL;
1324 TnyList *account_list = NULL;
1325 TnyIterator *iter = NULL;
1328 g_return_val_if_fail (self, NULL);
1329 g_return_val_if_fail (account_name, NULL);
1330 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE ||
1331 type == TNY_ACCOUNT_TYPE_TRANSPORT,
1334 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1336 account_list = (type == TNY_ACCOUNT_TYPE_STORE) ?
1337 priv->store_accounts :
1338 priv->transport_accounts;
1340 if (!account_list) {
1341 g_printerr ("%s: No server accounts of type %s\n", __FUNCTION__,
1342 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport");
1346 /* Look for the server account */
1348 iter = tny_list_create_iterator (account_list);
1349 while (!tny_iterator_is_done (iter) && !found) {
1350 const gchar *modest_acc_name;
1351 TnyAccount *tmp_account;
1353 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1355 modest_tny_account_get_parent_modest_account_name_for_server_account (tmp_account);
1357 if (!strcmp (account_name, modest_acc_name)) {
1359 retval = g_object_ref (tmp_account);
1361 /* Free and continue */
1362 g_object_unref (tmp_account);
1363 tny_iterator_next (iter);
1367 g_printerr ("modest: %s: could not get tny %s account for %s\n." \
1368 "Number of server accounts of this type=%d\n", __FUNCTION__,
1369 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport",
1370 account_name, tny_list_get_length (account_list));
1373 /* Returns a new reference */
1378 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1379 const gchar *account_name)
1381 /* Get the current connection: */
1382 TnyDevice *device = modest_runtime_get_device ();
1384 if (!tny_device_is_online (device))
1387 g_return_val_if_fail (self, NULL);
1388 g_return_val_if_fail (account_name, NULL);
1391 #ifdef MODEST_PLATFORM_MAEMO
1392 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1393 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1394 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1395 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1399 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1403 const gchar *connection_name = con_ic_iap_get_name (connection);
1404 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1405 if (!connection_name)
1408 /* Get the connection-specific transport acccount, if any: */
1409 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1410 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1411 account_name, connection_name);
1413 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1414 if (!server_account_name) {
1415 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1418 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1419 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1420 server_account_name);
1422 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1423 g_free (server_account_name);
1425 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1426 g_object_unref (connection);
1430 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1431 #endif /* MODEST_PLATFORM_MAEMO */
1436 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1437 const gchar *account_name)
1439 g_return_val_if_fail (self, NULL);
1440 g_return_val_if_fail (account_name, NULL);
1442 if (!account_name || !self)
1445 /* Get the connection-specific transport acccount, if any: */
1446 TnyAccount *account =
1447 get_smtp_specific_transport_account_for_open_connection (self, account_name);
1449 /* If there is no connection-specific transport account (the common case),
1450 * just get the regular transport account: */
1452 /* The special local folders don't have transport accounts. */
1453 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1456 account = modest_tny_account_store_get_server_account (self, account_name,
1457 TNY_ACCOUNT_TYPE_TRANSPORT);
1464 modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
1466 TnyAccount *account = NULL;
1467 ModestTnyAccountStorePrivate *priv;
1471 g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1473 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1476 iter = tny_list_create_iterator (priv->store_accounts);
1477 while (!tny_iterator_is_done (iter) && !found) {
1478 TnyAccount *tmp_account;
1480 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1481 if (modest_tny_account_is_virtual_local_folders (tmp_account)) {
1482 account = g_object_ref (tmp_account);
1485 g_object_unref (tmp_account);
1486 tny_iterator_next (iter);
1488 g_object_unref (iter);
1490 /* Returns a new reference to the account */
1494 /*********************************************************************************/
1496 add_existing_accounts (ModestTnyAccountStore *self)
1498 GSList *account_names = NULL, *iter = NULL;
1499 ModestTnyAccountStorePrivate *priv = NULL;
1501 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1503 /* These are account names, not server_account names */
1504 account_names = modest_account_mgr_account_names (priv->account_mgr, FALSE);
1506 for (iter = account_names; iter != NULL; iter = g_slist_next (iter)) {
1507 const gchar *account_name = (const gchar*) iter->data;
1509 /* Insert all enabled accounts without notifying */
1510 if (modest_account_mgr_get_enabled (priv->account_mgr, account_name))
1511 insert_account (self, account_name, FALSE);
1513 modest_account_mgr_free_account_names (account_names);
1517 create_tny_account (ModestTnyAccountStore *self,
1519 TnyAccountType type)
1521 TnyAccount *account = NULL;
1522 ModestTnyAccountStorePrivate *priv = NULL;
1524 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1526 account = modest_tny_account_new_from_account (priv->account_mgr,
1533 /* Forget any cached password for the account, so that
1534 we use a new account if any */
1535 modest_tny_account_store_forget_password_in_memory (self,
1536 tny_account_get_id (account));
1537 /* Set the account store */
1538 g_object_set_data (G_OBJECT(account), "account_store", self);
1540 g_printerr ("modest: failed to create account for %s\n", name);
1547 * This function will be used for both adding new accounts and for the
1548 * initialization. In the initialization we do not want to emit
1549 * signals so notify will be FALSE, in the case of account additions
1550 * we do want to notify the observers
1553 insert_account (ModestTnyAccountStore *self,
1554 const gchar *account,
1557 ModestTnyAccountStorePrivate *priv = NULL;
1558 TnyAccount *store_account = NULL, *transport_account = NULL;
1560 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1562 /* Get the server and the transport account */
1563 store_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_STORE);
1564 transport_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
1566 /* Add to the list, and notify the observers */
1567 if (store_account) {
1568 tny_list_append (priv->store_accounts, G_OBJECT (store_account));
1570 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
1571 g_object_unref (store_account);
1574 /* Add to the list, and notify the observers */
1575 if (transport_account) {
1576 TnyAccount *account_outbox = NULL, *local_account = NULL;
1577 TnyFolder *per_account_outbox = NULL;
1579 TnyIterator *iter_folders;
1581 /* Add account to the list */
1582 tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
1584 /* Add connection-specific transport accounts */
1585 add_connection_specific_transport_accounts (self, account);
1587 /* Create per account local outbox */
1589 modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr,
1592 tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
1594 /* Get the outbox folder */
1595 folders = tny_simple_list_new ();
1596 tny_folder_store_get_folders (TNY_FOLDER_STORE (account_outbox),
1597 folders, NULL, NULL);
1598 g_assert (tny_list_get_length (folders) == 1);
1600 iter_folders = tny_list_create_iterator (folders);
1601 per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
1602 g_object_unref (iter_folders);
1603 g_object_unref (account_outbox);
1605 /* Add the outbox of the new per-account-local-outbox
1606 account to the global local merged OUTBOX of the
1607 local folders account */
1608 local_account = modest_tny_account_store_get_local_folders_account (MODEST_TNY_ACCOUNT_STORE (self));
1609 modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1610 per_account_outbox);
1611 g_object_unref (local_account);
1612 g_object_unref (per_account_outbox);
1614 /* Notify the observers */
1616 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
1617 g_object_unref (transport_account);
1622 on_account_inserted (ModestAccountMgr *acc_mgr,
1623 const gchar *account,
1626 /* Insert the account and notify the observers */
1627 insert_account (MODEST_TNY_ACCOUNT_STORE (user_data), account, TRUE);
1631 on_account_removed (ModestAccountMgr *acc_mgr,
1632 const gchar *account,
1635 TnyAccount *store_account = NULL, *transport_account = NULL;
1636 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
1638 /* Get the server and the transport account */
1640 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
1642 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
1644 /* If there was any problem creating the account, for example,
1645 with the configuration system this could not exist */
1646 if (store_account) {
1647 /* Clear the cache */
1648 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
1650 /* Notify the observers */
1651 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
1652 g_object_unref (store_account);
1654 g_warning ("There is no store account for account %s\n", account);
1657 /* If there was any problem creating the account, for example,
1658 with the configuration system this could not exist */
1659 if (transport_account) {
1660 /* Notify the observers */
1661 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
1662 g_object_unref (transport_account);
1664 g_warning ("There is no transport account for account %s\n", account);
1669 add_connection_specific_transport_accounts (ModestTnyAccountStore *self,
1670 const gchar *account_name)
1672 ModestTnyAccountStorePrivate *priv = NULL;
1673 GSList *list_specifics = NULL, *iter = NULL;
1675 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1677 list_specifics = modest_account_mgr_get_list (priv->account_mgr,
1679 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
1680 MODEST_CONF_VALUE_STRING, FALSE);
1682 /* Look at each connection-specific transport account for the
1683 * modest account: */
1684 iter = list_specifics;
1686 /* The list alternates between the connection name and the transport name: */
1687 iter = g_slist_next (iter);
1689 const gchar* transport_account_name = (const gchar*) (iter->data);
1690 if (transport_account_name) {
1691 TnyAccount * tny_account = NULL;
1692 /* Add the account: */
1694 modest_tny_account_new_from_server_account_name (priv->account_mgr,
1696 transport_account_name);
1698 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account,
1700 g_object_set_data (G_OBJECT(tny_account),
1704 tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
1706 g_printerr ("modest: failed to create smtp-specific account for %s\n",
1707 transport_account_name);
1710 iter = g_slist_next (iter);