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);
77 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
78 static void modest_tny_account_store_finalize (GObject *obj);
80 /* implementations for tny-account-store-iface */
81 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
82 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
85 static void get_server_accounts (TnyAccountStore *self,
91 ACCOUNT_UPDATE_SIGNAL,
92 PASSWORD_REQUESTED_SIGNAL,
96 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
97 struct _ModestTnyAccountStorePrivate {
99 GHashTable *password_hash;
100 GHashTable *account_settings_dialog_hash;
101 ModestAccountMgr *account_mgr;
102 TnySessionCamel *session;
105 /* We cache the lists of accounts here.
106 * They are created in our get_accounts_func() implementation. */
107 GSList *store_accounts;
108 GSList *transport_accounts;
110 /* This is also contained in store_accounts,
111 * but we cached it temporarily separately,
112 * because we create this while creating the transport accounts,
113 * but return it when requesting the store accounts:
115 GSList *store_accounts_outboxes;
118 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
119 MODEST_TYPE_TNY_ACCOUNT_STORE, \
120 ModestTnyAccountStorePrivate))
123 static GObjectClass *parent_class = NULL;
125 static guint signals[LAST_SIGNAL] = {0};
128 modest_tny_account_store_get_type (void)
130 static GType my_type = 0;
133 static const GTypeInfo my_info = {
134 sizeof(ModestTnyAccountStoreClass),
135 NULL, /* base init */
136 NULL, /* base finalize */
137 (GClassInitFunc) modest_tny_account_store_class_init,
138 NULL, /* class finalize */
139 NULL, /* class data */
140 sizeof(ModestTnyAccountStore),
142 (GInstanceInitFunc) modest_tny_account_store_instance_init,
146 static const GInterfaceInfo iface_info = {
147 (GInterfaceInitFunc) modest_tny_account_store_init,
148 NULL, /* interface_finalize */
149 NULL /* interface_data */
152 my_type = g_type_register_static (G_TYPE_OBJECT,
153 "ModestTnyAccountStore",
155 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
162 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
164 GObjectClass *gobject_class;
165 gobject_class = (GObjectClass*) klass;
167 parent_class = g_type_class_peek_parent (klass);
168 gobject_class->finalize = modest_tny_account_store_finalize;
170 g_type_class_add_private (gobject_class,
171 sizeof(ModestTnyAccountStorePrivate));
173 signals[ACCOUNT_UPDATE_SIGNAL] =
174 g_signal_new ("account_update",
175 G_TYPE_FROM_CLASS (gobject_class),
177 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
179 g_cclosure_marshal_VOID__STRING,
180 G_TYPE_NONE, 1, G_TYPE_STRING);
182 signals[PASSWORD_REQUESTED_SIGNAL] =
183 g_signal_new ("password_requested",
184 G_TYPE_FROM_CLASS (gobject_class),
186 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
188 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
189 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
196 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
197 GnomeVFSVolume *volume, gpointer user_data);
200 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
201 GnomeVFSVolume *volume, gpointer user_data);
204 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
206 ModestTnyAccountStorePrivate *priv =
207 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
209 priv->cache_dir = NULL;
210 priv->account_mgr = NULL;
211 priv->session = NULL;
214 /* An in-memory store of passwords,
215 * for passwords that are not remembered in the configuration,
216 * so they need to be asked for from the user once in each session:
218 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
221 /* A hash-map of modest account names to dialog pointers,
222 * so we can avoid showing the account settings twice for the same modest account: */
223 priv->account_settings_dialog_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
226 /* Respond to volume mounts and unmounts, such
227 * as the insertion/removal of the memory card: */
228 GnomeVFSVolumeMonitor* monitor =
229 gnome_vfs_get_volume_monitor();
230 g_signal_connect (G_OBJECT(monitor), "volume-mounted",
231 G_CALLBACK(on_vfs_volume_mounted),
233 g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
234 G_CALLBACK(on_vfs_volume_unmounted),
239 account_list_free (GSList *accounts)
241 GSList *cursor = accounts;
244 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
245 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
246 modest_runtime_verify_object_last_ref(cursor->data,id);
248 g_object_unref (G_OBJECT(cursor->data));
249 cursor = cursor->next;
251 g_slist_free (accounts);
256 /* disconnect the list of TnyAccounts */
258 account_list_disconnect (GSList *accounts)
260 GSList *cursor = accounts;
263 if (TNY_IS_CAMEL_ACCOUNT(cursor->data)) /* check twice... */
264 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(cursor->data), FALSE, NULL);
265 cursor = g_slist_next (cursor);
272 recreate_all_accounts (ModestTnyAccountStore *self)
274 ModestTnyAccountStorePrivate *priv =
275 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
277 if (priv->store_accounts_outboxes) {
278 account_list_free (priv->store_accounts_outboxes);
279 priv->store_accounts_outboxes = NULL;
282 if (priv->store_accounts) {
283 account_list_free (priv->store_accounts);
284 priv->store_accounts = NULL;
285 get_server_accounts (TNY_ACCOUNT_STORE(self),
286 NULL, TNY_ACCOUNT_TYPE_STORE);
289 if (priv->transport_accounts) {
290 account_list_free (priv->transport_accounts);
291 priv->transport_accounts = NULL;
292 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
293 TNY_ACCOUNT_TYPE_TRANSPORT);
298 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
299 GnomeVFSVolume *volume, gpointer user_data)
301 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
303 /* Check whether this was the external MMC1 card: */
304 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
305 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
306 printf ("DEBUG: %s: MMC1 card mounted.\n", __FUNCTION__);
308 /* TODO: Just add an account and emit (and respond to)
309 * TnyAccountStore::accountinserted signal?
311 recreate_all_accounts (self);
313 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
321 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
322 GnomeVFSVolume *volume, gpointer user_data)
324 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
326 /* Check whether this was the external MMC1 card: */
327 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
328 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
329 printf ("DEBUG: %s: MMC1 card unmounted.\n", __FUNCTION__);
331 /* TODO: Just add an account and emit (and respond to)
332 * TnyAccountStore::accountinserted signal?
334 recreate_all_accounts (self);
336 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
344 on_account_removed (ModestAccountMgr *acc_mgr,
345 const gchar *account,
348 TnyAccount *store_account = NULL, *transport_account = NULL;
349 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
351 /* Get the server and the transport account */
353 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
355 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
357 /* Clear the cache */
358 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
360 /* Notify the observers */
361 g_signal_emit (G_OBJECT (self),
362 tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNT_REMOVED],
364 g_signal_emit (G_OBJECT (self),
365 tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNT_REMOVED],
366 0, transport_account);
369 g_object_unref (store_account);
370 g_object_unref (transport_account);
374 * modest_tny_account_store_forget_password_in_memory
375 * @self: a TnyAccountStore instance
376 * @account: A server account.
378 * Forget any password stored in memory for this account.
379 * For instance, this should be called when the user has changed the password in the account settings.
382 modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, const gchar * server_account_name)
384 /* printf ("DEBUG: %s\n", __FUNCTION__); */
385 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
387 if (server_account_name && priv->password_hash) {
388 g_hash_table_remove (priv->password_hash, server_account_name);
393 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
394 const GSList *keys, gboolean server_account, gpointer user_data)
397 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
400 printf ("DEBUG: %s\n", __FUNCTION__);
401 const GSList *iter = keys;
402 for (iter = keys; iter; iter = g_slist_next (iter)) {
403 printf (" DEBUG: %s: key=%s\n", __FUNCTION__, (const gchar*)iter->data);
407 /* Ignore the change if it's a change in the last_updated value */
408 if (g_slist_length ((GSList *)keys) == 1 &&
409 g_str_has_suffix ((const gchar *) keys->data, MODEST_ACCOUNT_LAST_UPDATED)) {
413 /* FIXME: make this more finegrained; changes do not really affect _all_
416 recreate_all_accounts (self);
418 /* TODO: This doesn't actually work, because
419 * a) The account name is not sent correctly per key:
420 * b) We should test the end of the key, not the whole keym
421 * c) We don't seem to be getting all keys here.
422 * Instead, we just forget the password for all accounts when we create them, for now.
425 /* If a password has changed, then forget the previously cached password for this account: */
426 if (server_account && keys && g_slist_find_custom ((GSList *)keys, MODEST_ACCOUNT_PASSWORD, (GCompareFunc)strcmp)) {
427 printf ("DEBUG: %s: Forgetting cached password for account ID=%s\n", __FUNCTION__, account);
428 modest_tny_account_store_forget_password_in_memory (self, account);
432 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
437 static ModestTnyAccountStore*
438 get_account_store_for_account (TnyAccount *account)
440 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
445 void on_account_settings_hide (GtkWidget *widget, gpointer user_data)
447 TnyAccount *account = (TnyAccount*)user_data;
449 /* This is easier than using a struct for the user_data: */
450 ModestTnyAccountStore *self = modest_runtime_get_account_store();
451 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
453 const gchar *modest_account_name =
454 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
455 if (modest_account_name)
456 g_hash_table_remove (priv->account_settings_dialog_hash, modest_account_name);
460 gboolean on_idle_wrong_password (gpointer user_data)
462 TnyAccount *account = (TnyAccount*)user_data;
463 /* This is easier than using a struct for the user_data: */
464 ModestTnyAccountStore *self = modest_runtime_get_account_store();
465 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
467 const gchar *modest_account_name =
468 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
469 if (!modest_account_name) {
470 g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n",
473 g_object_unref (account);
478 /* Check whether this window is already open,
479 * for instance because of a previous get_password() call:
481 gpointer dialog_as_gpointer = NULL;
482 gboolean found = FALSE;
483 if (priv->account_settings_dialog_hash) {
484 found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
485 modest_account_name, NULL, (gpointer*)&dialog_as_gpointer);
487 ModestAccountSettingsDialog *dialog = dialog_as_gpointer;
489 ModestWindow *main_window =
490 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
492 gdk_threads_enter ();
493 gboolean created_dialog = FALSE;
494 if (!found || !dialog) {
495 dialog = modest_account_settings_dialog_new ();
496 modest_account_settings_dialog_set_account_name (dialog, modest_account_name);
497 modest_account_settings_dialog_switch_to_user_info (dialog);
499 g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (modest_account_name), dialog);
501 created_dialog = TRUE;
504 /* Show an explanatory temporary banner: */
505 hildon_banner_show_information (
506 GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
508 if (created_dialog) {
509 /* Forget it when it closes: */
510 g_signal_connect_object (G_OBJECT (dialog), "hide", G_CALLBACK (on_account_settings_hide),
513 /* Show it and delete it when it closes: */
514 modest_maemo_show_dialog_and_forget (GTK_WINDOW (main_window), GTK_DIALOG (dialog));
517 /* Just show it instead of showing it and deleting it when it closes,
518 * though it is probably open already: */
519 gtk_window_present (GTK_WINDOW (dialog));
522 g_object_unref (account);
523 gdk_threads_leave ();
525 return FALSE; /* Dont' call this again. */
531 ModestTnyAccountStore* account_store;
532 const gchar* server_account_id;
537 } IdlePasswordRequest;
540 on_idle_request_password (gpointer user_data)
544 IdlePasswordRequest* info = (IdlePasswordRequest*)user_data;
545 g_signal_emit (G_OBJECT(info->account_store), signals[PASSWORD_REQUESTED_SIGNAL], 0,
546 info->server_account_id, /* server_account_name */
547 info->username, info->password, info->cancel, info->remember);
550 g_main_loop_quit (info->loop);
554 return FALSE; /* Don't call again. */
558 request_password_in_main_loop_and_wait (ModestTnyAccountStore *account_store,
559 const gchar* server_account_id,
565 IdlePasswordRequest *data = g_slice_new0 (IdlePasswordRequest);
566 data->account_store = account_store;
567 data->server_account_id = server_account_id;
568 data->username = username;
569 data->password = password;
570 data->cancel = cancel;
571 data->remember = remember;
573 data->loop = g_main_loop_new (NULL, FALSE /* not running */);
575 /* Cause the function to be run in an idle-handler, which is always
576 * in the main thread:
578 g_idle_add (&on_idle_request_password, data);
580 /* This main loop will run until the idle handler has stopped it: */
581 printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
583 g_main_loop_run (data->loop);
585 printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
586 printf ("DEBUG: %s: Finished\n", __FUNCTION__);
587 g_main_loop_unref (data->loop);
589 g_slice_free (IdlePasswordRequest, data);
592 /* This callback will be called by Tinymail when it needs the password
593 * from the user or the account settings.
594 * It can also call forget_password() before calling this,
595 * so that we clear wrong passwords out of our account settings.
596 * Note that TnyAccount here will be the server account. */
598 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
600 /* TODO: Settting cancel to FALSE does not actually cancel everything.
601 * We still get multiple requests afterwards, so we end up showing the
602 * same dialogs repeatedly.
605 printf ("DEBUG: modest: %s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
607 g_return_val_if_fail (account, NULL);
609 const TnyAccountStore *account_store = NULL;
610 ModestTnyAccountStore *self = NULL;
611 ModestTnyAccountStorePrivate *priv;
612 gchar *username = NULL;
614 gpointer pwd_ptr = NULL;
615 gboolean already_asked = FALSE;
617 /* Initialize the output parameter: */
621 const gchar *server_account_name = tny_account_get_id (account);
622 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
624 if (!server_account_name || !account_store) {
625 g_warning ("modest: %s: could not retrieve account_store for account %s",
626 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
633 self = MODEST_TNY_ACCOUNT_STORE (account_store);
634 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
636 /* This hash map stores passwords, including passwords that are not stored in gconf. */
637 /* Is it in the hash? if it's already there, it must be wrong... */
638 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
639 * type-punned ptrs...*/
640 already_asked = priv->password_hash &&
641 g_hash_table_lookup_extended (priv->password_hash,
644 (gpointer*)&pwd_ptr);
646 printf ("DEBUG: modest: %s: Already asked = %d\n", __FUNCTION__, already_asked);
648 /* If the password is not already there, try ModestConf */
649 if (!already_asked) {
650 pwd = modest_server_account_get_password (priv->account_mgr,
651 server_account_name);
652 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
655 /* If it was already asked, it must have been wrong, so ask again */
656 if (already_asked || !pwd || strlen(pwd) == 0) {
657 /* As per the UI spec, if no password was set in the account settings,
658 * ask for it now. But if the password is wrong in the account settings,
659 * then show a banner and the account settings dialog so it can be corrected:
661 const gboolean settings_have_password =
662 modest_server_account_get_has_password (priv->account_mgr, server_account_name);
663 printf ("DEBUG: modest: %s: settings_have_password=%d\n", __FUNCTION__, settings_have_password);
664 if (settings_have_password) {
667 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
668 /* We show it in the main loop, because this function might no tbe in the main loop. */
669 g_object_ref (account); /* unrefed in the idle handler. */
670 g_idle_add (on_idle_wrong_password, account);
678 /* we don't have it yet. Get the password from the user */
679 const gchar* account_id = tny_account_get_id (account);
680 gboolean remember = FALSE;
683 request_password_in_main_loop_and_wait (self, account_id,
684 &username, &pwd, cancel, &remember);
687 /* The password will be returned as the result,
688 * but we need to tell tinymail about the username too: */
689 tny_account_set_user (account, username);
691 /* Do not save the password in gconf,
692 * because the UI spec says "The password will never be saved in the account": */
695 printf ("%s: Storing username=%s, password=%s\n",
696 __FUNCTION__, username, pwd);
697 modest_server_account_set_username (priv->account_mgr, server_account_name,
699 modest_server_account_set_password (priv->account_mgr, server_account_name,
704 /* We need to dup the string even knowing that
705 it's already a dup of the contents of an
706 entry, because it if it's wrong, then camel
708 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
710 g_hash_table_remove (priv->password_hash, server_account_name);
721 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
726 /* tinymail calls this if the connection failed due to an incorrect password.
727 * And it seems to call this for any general connection failure. */
729 forget_password (TnyAccount *account)
731 printf ("DEBUG: %s\n", __FUNCTION__);
732 ModestTnyAccountStore *self;
733 ModestTnyAccountStorePrivate *priv;
734 const TnyAccountStore *account_store;
738 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
739 self = MODEST_TNY_ACCOUNT_STORE (account_store);
740 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
741 key = tny_account_get_id (account);
743 /* Do not remove the key, this will allow us to detect that we
744 have already asked for it at least once */
745 pwd = g_hash_table_lookup (priv->password_hash, key);
747 memset (pwd, 0, strlen (pwd));
748 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
751 /* Remove from configuration system */
753 modest_account_mgr_unset (priv->account_mgr,
754 key, MODEST_ACCOUNT_PASSWORD, TRUE);
759 destroy_password_hashtable (ModestTnyAccountStore *self)
761 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
763 g_free (priv->cache_dir);
764 priv->cache_dir = NULL;
766 if (priv->password_hash) {
767 g_hash_table_destroy (priv->password_hash);
768 priv->password_hash = NULL;
773 modest_tny_account_store_finalize (GObject *obj)
775 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
776 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
778 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
780 g_free (priv->cache_dir);
781 priv->cache_dir = NULL;
783 if (priv->password_hash) {
784 g_hash_table_destroy (priv->password_hash);
785 priv->password_hash = NULL;
788 destroy_password_hashtable (self);
790 if (priv->account_mgr) {
791 g_object_unref (G_OBJECT(priv->account_mgr));
792 priv->account_mgr = NULL;
796 g_object_unref (G_OBJECT(priv->device));
800 /* disconnect all accounts when we are destroyed */
801 g_debug ("modest: disconnecting all store accounts");
802 account_list_disconnect (priv->store_accounts);
803 g_debug ("modest: disconnecting all transport accounts");
804 account_list_disconnect (priv->transport_accounts);
806 /* this includes the local folder */
807 account_list_free (priv->store_accounts);
808 priv->store_accounts = NULL;
810 account_list_free (priv->transport_accounts);
811 priv->transport_accounts = NULL;
814 camel_object_unref (CAMEL_OBJECT(priv->session));
815 priv->session = NULL;
818 G_OBJECT_CLASS(parent_class)->finalize (obj);
822 ModestTnyAccountStore*
823 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
826 ModestTnyAccountStorePrivate *priv;
829 g_return_val_if_fail (account_mgr, NULL);
830 g_return_val_if_fail (device, NULL);
832 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
833 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
835 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
836 priv->device = g_object_ref (device);
838 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
839 if (!priv->session) {
840 g_warning ("failed to get TnySessionCamel");
844 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
845 tny_session_camel_set_async_connecting (priv->session, TRUE);
847 /* Connect signals */
848 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
849 G_CALLBACK (on_account_changed), obj);
850 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
851 G_CALLBACK (on_account_removed), obj);
853 return MODEST_TNY_ACCOUNT_STORE(obj);
856 /** Fill the TnyList from the appropriate cached GSList of accounts. */
858 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
860 ModestTnyAccountStorePrivate *priv;
861 GSList *accounts, *cursor;
863 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
864 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
869 GObject *object = G_OBJECT(cursor->data);
870 tny_list_prepend (list, object);
873 cursor = cursor->next;
878 create_per_account_local_outbox_folders (TnyAccountStore *self)
880 g_return_if_fail (self);
882 ModestTnyAccountStorePrivate *priv =
883 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
885 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
887 GSList *accounts = NULL;
889 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
890 TRUE /* including disabled accounts */);
893 for (iter = account_names; iter; iter = g_slist_next (iter)) {
895 const gchar* account_name = (const gchar*)iter->data;
897 /* Create a per-account local outbox folder (a _store_ account)
898 * for each _transport_ account: */
899 TnyAccount *tny_account_outbox =
900 modest_tny_account_new_for_per_account_local_outbox_folder (
901 priv->account_mgr, account_name, priv->session);
903 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
906 modest_account_mgr_free_account_names (account_names);
907 account_names = NULL;
909 priv->store_accounts_outboxes = accounts;
912 /* This function fills the TnyList, and also stores a GSList of the accounts,
913 * for caching purposes. It creates the TnyAccount objects if necessary.
914 * The @list parameter may be NULL, if you just want to fill the cache.
917 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
919 g_return_if_fail (self);
921 ModestTnyAccountStorePrivate *priv =
922 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
924 /* Do nothing if the accounts are already cached: */
925 if (type == TNY_ACCOUNT_TYPE_STORE) {
926 if (priv->store_accounts)
928 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
929 if (priv->transport_accounts)
933 GSList *account_names = NULL, *cursor = NULL;
934 GSList *accounts = NULL;
936 /* These are account names, not server_account names */
937 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
939 for (cursor = account_names; cursor; cursor = cursor->next) {
941 gchar *account_name = (gchar*)cursor->data;
943 /* we get the server_accounts for enabled accounts */
944 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
946 /* Add the account: */
947 TnyAccount *tny_account =
948 modest_tny_account_new_from_account (priv->account_mgr,
954 /* Forget any cached password for the account,
955 * so that we use a new account if any.
956 * TODO: Really we should do this in a more precise way in
957 * on_account_changed().
959 modest_tny_account_store_forget_password_in_memory (
960 MODEST_TNY_ACCOUNT_STORE (self),
961 tny_account_get_id (tny_account));
963 g_object_set_data (G_OBJECT(tny_account), "account_store",
966 tny_list_prepend (list, G_OBJECT(tny_account));
968 accounts = g_slist_append (accounts, tny_account); /* cache it */
970 g_printerr ("modest: failed to create account for %s\n",
975 if (type == TNY_ACCOUNT_TYPE_STORE) {
976 /* Also add the Memory card account if it is mounted: */
977 gboolean mmc_is_mounted = FALSE;
978 GnomeVFSVolumeMonitor* monitor =
979 gnome_vfs_get_volume_monitor();
980 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
981 GList *iter = list_volumes;
983 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
985 if (!mmc_is_mounted) {
986 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
987 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
988 mmc_is_mounted = TRUE;
993 gnome_vfs_volume_unref(volume);
996 iter = g_list_next (iter);
998 g_list_free (list_volumes);
1000 if (mmc_is_mounted) {
1001 TnyAccount *tny_account =
1002 modest_tny_account_new_for_local_folders (priv->account_mgr,
1003 priv->session, MODEST_MCC1_VOLUMEPATH);
1005 tny_list_prepend (list, G_OBJECT(tny_account));
1006 accounts = g_slist_append (accounts, tny_account); /* cache it */
1010 /* And add the connection-specific transport accounts, if any.
1011 * Note that these server account instances might never be used
1012 * if their connections are never active: */
1013 /* Look at each modest account: */
1014 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
1015 GSList *iter_account_names = account_names;
1016 while (iter_account_names) {
1017 const gchar* account_name = (const gchar*)(iter_account_names->data);
1018 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
1020 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
1021 MODEST_CONF_VALUE_STRING, FALSE);
1023 /* Look at each connection-specific transport account for the
1024 * modest account: */
1025 GSList *iter = list_specifics;
1027 /* The list alternates between the connection name and the transport name: */
1028 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
1029 iter = g_slist_next (iter);
1031 const gchar* transport_account_name = (const gchar*)(iter->data);
1032 if (transport_account_name) {
1033 TnyAccount * tny_account = NULL;
1034 /* Add the account: */
1035 tny_account = modest_tny_account_new_from_server_account_name (
1036 priv->account_mgr, priv->session, transport_account_name);
1038 modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, account_name);
1039 g_object_set_data (G_OBJECT(tny_account), "account_store",
1042 tny_list_prepend (list, G_OBJECT(tny_account));
1044 accounts = g_slist_append (accounts, tny_account); /* cache it */
1046 g_printerr ("modest: failed to create smtp-specific account for %s\n",
1047 transport_account_name);
1051 iter = g_slist_next (iter);
1054 iter_account_names = g_slist_next (iter_account_names);
1058 /* free the account_names */
1059 modest_account_mgr_free_account_names (account_names);
1060 account_names = NULL;
1062 /* We also create a per-account local outbox folder (a _store_ account)
1063 * for each _transport_ account. */
1064 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
1065 /* Now would be a good time to create the per-account local outbox folder
1066 * _store_ accounts corresponding to each transport account: */
1067 if (!priv->store_accounts_outboxes) {
1068 create_per_account_local_outbox_folders (self);
1072 /* But we only return the per-account local outbox folder when
1073 * _store_ accounts are requested. */
1074 if (type == TNY_ACCOUNT_TYPE_STORE) {
1075 /* Create them if necessary,
1076 * (which also requires creating the transport accounts,
1078 if (!priv->store_accounts_outboxes) {
1079 create_per_account_local_outbox_folders (self);
1082 /* Also add the local folder pseudo-account: */
1083 TnyAccount *tny_account =
1084 modest_tny_account_new_for_local_folders (priv->account_mgr,
1085 priv->session, NULL);
1087 /* Add them to the TnyList: */
1088 if (priv->store_accounts_outboxes) {
1089 GSList *iter = NULL;
1090 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
1091 TnyAccount *outbox_account = (TnyAccount*)iter->data;
1092 if (list && outbox_account)
1093 tny_list_prepend (list, G_OBJECT(outbox_account));
1095 g_object_ref (outbox_account);
1096 accounts = g_slist_append (accounts, outbox_account);
1100 /* Add a merged folder, merging all the per-account outbox folders: */
1101 modest_tny_local_folders_account_add_merged_outbox_folders (
1102 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (tny_account), priv->store_accounts_outboxes);
1104 if (priv->store_accounts_outboxes) {
1105 /* We have finished with this temporary list, so free it: */
1106 account_list_free (priv->store_accounts_outboxes);
1107 priv->store_accounts_outboxes = NULL;
1111 tny_list_prepend (list, G_OBJECT(tny_account));
1112 accounts = g_slist_append (accounts, tny_account); /* cache it */
1115 if (type == TNY_ACCOUNT_TYPE_STORE) {
1116 /* Store the cache: */
1117 priv->store_accounts = accounts;
1118 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
1119 /* Store the cache: */
1120 priv->transport_accounts = accounts;
1126 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
1127 TnyGetAccountsRequestType request_type)
1129 ModestTnyAccountStorePrivate *priv;
1131 g_return_if_fail (self);
1132 g_return_if_fail (TNY_IS_LIST(list));
1134 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1136 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
1137 modest_tny_account_store_get_accounts (self, list,
1138 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1139 modest_tny_account_store_get_accounts (self, list,
1140 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
1142 tny_session_camel_set_initialized (priv->session);
1147 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
1148 if (!priv->store_accounts)
1149 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1151 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1153 tny_session_camel_set_initialized (priv->session);
1155 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
1156 if (!priv->transport_accounts)
1157 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1159 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1161 tny_session_camel_set_initialized (priv->session);
1163 g_return_if_reached (); /* incorrect req type */
1168 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
1170 ModestTnyAccountStorePrivate *priv;
1171 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1173 if (!priv->cache_dir)
1174 priv->cache_dir = g_build_filename (g_get_home_dir(),
1175 MODEST_DIR, MODEST_CACHE_DIR, NULL);
1176 return priv->cache_dir;
1181 * callers need to unref
1184 modest_tny_account_store_get_device (TnyAccountStore *self)
1186 ModestTnyAccountStorePrivate *priv;
1188 g_return_val_if_fail (self, NULL);
1190 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1193 return g_object_ref (G_OBJECT(priv->device));
1200 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1202 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1203 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1210 modest_tny_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type,
1211 gboolean question, const GError *error)
1213 g_return_val_if_fail (error, FALSE);
1215 if ((error->domain != TNY_ACCOUNT_ERROR)
1216 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
1217 g_warning("modest: %s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
1218 __FUNCTION__, error->domain, error->message);
1223 printf("DEBUG: %s: GError code: %d, message=%s\n",
1224 __FUNCTION__, error->code, error->message);
1226 /* Get the server name: */
1227 const gchar* server_name = NULL;
1228 if (account && TNY_IS_ACCOUNT (account)) {
1229 server_name = tny_account_get_hostname (account);
1230 printf ("modest: %s: account name = %s, server_name=%s\n", __FUNCTION__,
1231 tny_account_get_id (account), server_name);
1235 server_name = _("Unknown Server");
1237 ModestTransportStoreProtocol proto = MODEST_PROTOCOL_STORE_POP; /* Arbitrary default. */
1239 const gchar *proto_name = tny_account_get_proto (account);
1241 proto = modest_protocol_info_get_transport_store_protocol (proto_name);
1243 g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__,
1244 tny_account_get_id (account));
1248 /* const gchar *prompt = NULL; */
1249 gchar *prompt = NULL;
1250 switch (error->code) {
1251 case TNY_ACCOUNT_STORE_ERROR_CANCEL_ALERT:
1252 case TNY_ACCOUNT_ERROR_TRY_CONNECT_USER_CANCEL:
1253 /* Don't show waste the user's time by showing him a dialog telling
1254 * him that he has just cancelled something: */
1255 g_debug ("%s: Handling GError domain=%d, code=%d (cancelled) without showing a dialog, message=%s",
1256 __FUNCTION__, error->domain, error->code, error->message);
1260 case TNY_ACCOUNT_ERROR_TRY_CONNECT_HOST_LOOKUP_FAILED:
1261 /* TODO: Show the appropriate message, depending on whether it's POP or IMAP: */
1262 g_debug ("%s: Handling GError domain=%d, code=%d (lookup failed), message=%s",
1263 __FUNCTION__, error->domain, error->code, error->message);
1266 case MODEST_PROTOCOL_STORE_POP:
1267 prompt = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), server_name);
1269 case MODEST_PROTOCOL_STORE_IMAP:
1270 prompt = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), server_name);
1272 case MODEST_PROTOCOL_TRANSPORT_SMTP:
1273 default: /* Arbitrary default. */
1274 prompt = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
1279 prompt = g_strdup_printf(
1280 _("Incorrect Account Settings:\n Host lookup failed.%s"),
1285 case TNY_ACCOUNT_ERROR_TRY_CONNECT_AUTHENTICATION_NOT_SUPPORTED:
1286 g_debug ("%s: Handling GError domain=%d, code=%d (authentication not supported), message=%s",
1287 __FUNCTION__, error->domain, error->code, error->message);
1288 /* TODO: This needs a logical ID for the string: */
1289 prompt = g_strdup_printf(
1290 _("Incorrect Account Settings:\nThe secure authentication method is not supported.\n%s"),
1294 case TNY_ACCOUNT_ERROR_TRY_CONNECT_CERTIFICATE:
1295 g_debug ("%s: Handling GError domain=%d, code=%d (certificatae), message=%s",
1296 __FUNCTION__, error->domain, error->code, error->message);
1297 prompt = g_strdup_printf(
1298 _("Certificate Problem:\n%s"),
1302 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
1303 /* The tinymail camel implementation just sends us this for almost
1304 * everything, so we have to guess at the cause.
1305 * It could be a wrong password, or inability to resolve a hostname,
1306 * or lack of network, or incorrect authentication method, or something entirely different: */
1307 /* TODO: Fix camel to provide specific error codes, and then use the
1308 * specific dialog messages from Chapter 12 of the UI spec.
1310 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
1311 /* This debug output is useful. Please keep it uncommented until
1312 * we have fixed the problems in this function: */
1313 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
1314 __FUNCTION__, error->domain, error->code, error->message);
1316 /* TODO: Remove the internal error message for the real release.
1317 * This is just so the testers can give us more information: */
1318 /* prompt = _("Modest account not yet fully configured."); */
1319 prompt = g_strdup_printf(
1320 "%s\n (Internal error message, often very misleading):\n%s",
1321 _("Incorrect Account Settings"),
1324 /* Note: If the password was wrong then get_password() would be called again,
1325 * instead of this vfunc being called. */
1330 g_warning ("%s: Unhandled GError code: %d, message=%s",
1331 __FUNCTION__, error->code, error->message);
1339 ModestWindow *main_window =
1340 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
1341 gboolean retval = TRUE;
1343 /* The Tinymail documentation says that we should show Yes and No buttons,
1344 * when it is a question.
1345 * Obviously, we need tinymail to use more specific error codes instead,
1346 * so we know what buttons to show. */
1348 /* TODO: Do this in the main context: */
1349 GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_window),
1351 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1353 retval = (response == GTK_RESPONSE_YES) ||
1354 (response == GTK_RESPONSE_OK);
1357 gtk_widget_destroy (dialog);
1360 /* Just show the error text and use the default response: */
1361 modest_maemo_show_information_note_in_main_context_and_forget (GTK_WINDOW (main_window),
1365 /* TODO: Don't free this when we no longer strdup the message for testers. */
1369 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1375 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1377 TnyAccountStoreIface *klass;
1379 g_return_if_fail (g);
1381 klass = (TnyAccountStoreIface *)g;
1383 klass->get_accounts_func =
1384 modest_tny_account_store_get_accounts;
1385 klass->get_cache_dir_func =
1386 modest_tny_account_store_get_cache_dir;
1387 klass->get_device_func =
1388 modest_tny_account_store_get_device;
1390 modest_tny_account_store_alert;
1391 klass->find_account_func =
1392 modest_tny_account_store_find_account_by_url;
1396 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1397 ModestTnyGetPassFunc func)
1399 /* not implemented, we use signals */
1400 g_printerr ("modest: set_get_pass_func not implemented\n");
1404 modest_tny_account_store_get_session (TnyAccountStore *self)
1406 g_return_val_if_fail (self, NULL);
1407 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1413 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1414 ModestTnyAccountStoreQueryType type,
1417 TnyAccount *account = NULL;
1418 ModestTnyAccountStorePrivate *priv;
1420 const gchar *val = NULL;
1424 g_return_val_if_fail (self, NULL);
1425 g_return_val_if_fail (str, NULL);
1427 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1429 /* fill the caches, as that may not have happened yet */
1430 list = TNY_LIST(tny_simple_list_new());
1431 modest_tny_account_store_get_accounts (TNY_ACCOUNT_STORE(self),
1432 list, TNY_ACCOUNT_STORE_BOTH);
1433 g_object_unref (list);
1437 /* Search in store accounts */
1438 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1440 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1441 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1443 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1444 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1448 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1449 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), str)) {
1450 account = TNY_ACCOUNT (cursor->data);
1453 if (strcmp (val, str) == 0) {
1454 account = TNY_ACCOUNT(cursor->data);
1460 /* if we already found something, no need to search the transport accounts */
1461 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1463 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1464 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1466 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1467 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1471 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1472 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), val)) {
1473 account = TNY_ACCOUNT (cursor->data);
1476 if (strcmp (val, str) == 0) {
1477 account = TNY_ACCOUNT(cursor->data);
1484 g_object_ref (G_OBJECT(account));
1486 /* Warn if nothing was found. This is generally unusual. */
1488 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1489 g_warning("%s: Failed to find account with ID=%s\n", __FUNCTION__, str);
1491 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1492 g_warning("%s: Failed to find account with URL=%s\n", __FUNCTION__, str);
1501 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1502 const gchar *account_name,
1503 TnyAccountType type)
1505 ModestTnyAccountStorePrivate *priv = NULL;
1506 TnyAccount *account = NULL;
1507 GSList *account_list = NULL;
1508 gboolean found = FALSE;
1510 g_return_val_if_fail (self, NULL);
1511 g_return_val_if_fail (account_name, NULL);
1512 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE ||
1513 type == TNY_ACCOUNT_TYPE_TRANSPORT,
1516 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1518 account_list = (type == TNY_ACCOUNT_TYPE_STORE) ?
1519 priv->store_accounts :
1520 priv->transport_accounts;
1522 /* Look for the server account */
1523 while (account_list && !found) {
1524 const gchar *modest_acc_name;
1526 account = TNY_ACCOUNT (account_list->data);;
1528 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
1530 if (!strcmp (account_name, modest_acc_name))
1533 account_list = g_slist_next (account_list);
1537 g_printerr ("modest: could not get tny %s account for %s\n",
1538 (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport",
1541 /* Pick a reference */
1542 g_object_ref (account);
1549 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1550 const gchar *account_name)
1552 /* Get the current connection: */
1553 TnyDevice *device = modest_runtime_get_device ();
1555 if (!tny_device_is_online (device))
1558 g_return_val_if_fail (self, NULL);
1559 g_return_val_if_fail (account_name, NULL);
1562 #ifdef MODEST_PLATFORM_MAEMO
1563 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1564 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1565 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1566 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1570 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1574 const gchar *connection_name = con_ic_iap_get_name (connection);
1575 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1576 if (!connection_name)
1579 /* Get the connection-specific transport acccount, if any: */
1580 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1581 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1582 account_name, connection_name);
1584 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1585 if (!server_account_name) {
1586 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1589 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1590 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1591 server_account_name);
1593 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1594 g_free (server_account_name);
1596 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1597 g_object_unref (connection);
1601 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1602 #endif /* MODEST_PLATFORM_MAEMO */
1607 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1608 const gchar *account_name)
1610 g_return_val_if_fail (self, NULL);
1611 g_return_val_if_fail (account_name, NULL);
1613 if (!account_name || !self)
1616 /* Get the connection-specific transport acccount, if any: */
1617 TnyAccount *account =
1618 get_smtp_specific_transport_account_for_open_connection (self, account_name);
1620 /* If there is no connection-specific transport account (the common case),
1621 * just get the regular transport account: */
1623 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1625 /* The special local folders don't have transport accounts. */
1626 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1629 account = modest_tny_account_store_get_server_account (self, account_name,
1630 TNY_ACCOUNT_TYPE_TRANSPORT);
1637 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1639 /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
1640 * for anything else. */
1641 return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1646 modest_tny_account_is_memory_card_account (TnyAccount *self)
1651 const gchar* account_id = tny_account_get_id (self);
1655 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
1659 modest_tny_account_store_get_local_folders_account (TnyAccountStore *self)
1661 TnyAccount *account = NULL;
1662 ModestTnyAccountStorePrivate *priv;
1665 g_return_val_if_fail (self, NULL);
1667 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1669 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1670 TnyAccount *this_account = TNY_ACCOUNT(cursor->data);
1671 if (modest_tny_account_is_virtual_local_folders (this_account)) {
1672 account = this_account;
1678 g_object_ref (G_OBJECT(account));