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,
346 gboolean is_server_account,
349 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
350 TnyAccount *server_account;
352 /* Clear the account cache */
353 server_account = modest_tny_account_store_get_tny_account_by (self,
354 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
356 if (server_account) {
357 if (TNY_IS_STORE_ACCOUNT (server_account)) {
359 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (server_account));
361 g_signal_emit (G_OBJECT (self),
362 tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNT_REMOVED],
365 /* /\* FIXME: make this more finegrained; changes do not */
366 /* * really affect _all_ accounts, and some do not */
367 /* * affect tny accounts at all (such as 'last_update') */
369 /* recreate_all_accounts (self); */
371 /* g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, */
374 g_object_unref (server_account);
376 g_printerr ("modest: cannot find server account for %s", account);
380 * modest_tny_account_store_forget_password_in_memory
381 * @self: a TnyAccountStore instance
382 * @account: A server account.
384 * Forget any password stored in memory for this account.
385 * For instance, this should be called when the user has changed the password in the account settings.
388 modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, const gchar * server_account_name)
390 /* printf ("DEBUG: %s\n", __FUNCTION__); */
391 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
393 if (server_account_name && priv->password_hash) {
394 g_hash_table_remove (priv->password_hash, server_account_name);
399 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
400 const GSList *keys, gboolean server_account, gpointer user_data)
403 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
406 printf ("DEBUG: %s\n", __FUNCTION__);
407 const GSList *iter = keys;
408 for (iter = keys; iter; iter = g_slist_next (iter)) {
409 printf (" DEBUG: %s: key=%s\n", __FUNCTION__, (const gchar*)iter->data);
413 /* Ignore the change if it's a change in the last_updated value */
414 if (g_slist_length ((GSList *)keys) == 1 &&
415 g_str_has_suffix ((const gchar *) keys->data, MODEST_ACCOUNT_LAST_UPDATED)) {
419 /* FIXME: make this more finegrained; changes do not really affect _all_
422 recreate_all_accounts (self);
424 /* TODO: This doesn't actually work, because
425 * a) The account name is not sent correctly per key:
426 * b) We should test the end of the key, not the whole keym
427 * c) We don't seem to be getting all keys here.
428 * Instead, we just forget the password for all accounts when we create them, for now.
431 /* If a password has changed, then forget the previously cached password for this account: */
432 if (server_account && keys && g_slist_find_custom ((GSList *)keys, MODEST_ACCOUNT_PASSWORD, (GCompareFunc)strcmp)) {
433 printf ("DEBUG: %s: Forgetting cached password for account ID=%s\n", __FUNCTION__, account);
434 modest_tny_account_store_forget_password_in_memory (self, account);
438 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
443 static ModestTnyAccountStore*
444 get_account_store_for_account (TnyAccount *account)
446 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
451 void on_account_settings_hide (GtkWidget *widget, gpointer user_data)
453 TnyAccount *account = (TnyAccount*)user_data;
455 /* This is easier than using a struct for the user_data: */
456 ModestTnyAccountStore *self = modest_runtime_get_account_store();
457 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
459 const gchar *modest_account_name =
460 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
461 if (modest_account_name)
462 g_hash_table_remove (priv->account_settings_dialog_hash, modest_account_name);
466 gboolean on_idle_wrong_password (gpointer user_data)
468 TnyAccount *account = (TnyAccount*)user_data;
469 /* This is easier than using a struct for the user_data: */
470 ModestTnyAccountStore *self = modest_runtime_get_account_store();
471 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
473 const gchar *modest_account_name =
474 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
475 if (!modest_account_name) {
476 g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n",
479 g_object_unref (account);
484 /* Check whether this window is already open,
485 * for instance because of a previous get_password() call:
487 gpointer dialog_as_gpointer = NULL;
488 gboolean found = FALSE;
489 if (priv->account_settings_dialog_hash) {
490 found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
491 modest_account_name, NULL, (gpointer*)&dialog_as_gpointer);
493 ModestAccountSettingsDialog *dialog = dialog_as_gpointer;
495 ModestWindow *main_window =
496 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
498 gdk_threads_enter ();
499 gboolean created_dialog = FALSE;
500 if (!found || !dialog) {
501 dialog = modest_account_settings_dialog_new ();
502 modest_account_settings_dialog_set_account_name (dialog, modest_account_name);
503 modest_account_settings_dialog_switch_to_user_info (dialog);
505 g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (modest_account_name), dialog);
507 created_dialog = TRUE;
510 /* Show an explanatory temporary banner: */
511 hildon_banner_show_information (
512 GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
514 if (created_dialog) {
515 /* Forget it when it closes: */
516 g_signal_connect_object (G_OBJECT (dialog), "hide", G_CALLBACK (on_account_settings_hide),
519 /* Show it and delete it when it closes: */
520 modest_maemo_show_dialog_and_forget (GTK_WINDOW (main_window), GTK_DIALOG (dialog));
523 /* Just show it instead of showing it and deleting it when it closes,
524 * though it is probably open already: */
525 gtk_window_present (GTK_WINDOW (dialog));
528 g_object_unref (account);
529 gdk_threads_leave ();
531 return FALSE; /* Dont' call this again. */
534 /* This callback will be called by Tinymail when it needs the password
535 * from the user or the account settings.
536 * It can also call forget_password() before calling this,
537 * so that we clear wrong passwords out of our account settings.
538 * Note that TnyAccount here will be the server account. */
540 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
542 /* TODO: Settting cancel to FALSE does not actually cancel everything.
543 * We still get multiple requests afterwards, so we end up showing the
544 * same dialogs repeatedly.
547 printf ("DEBUG: modest: %s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
549 g_return_val_if_fail (account, NULL);
551 const TnyAccountStore *account_store = NULL;
552 ModestTnyAccountStore *self = NULL;
553 ModestTnyAccountStorePrivate *priv;
554 gchar *username = NULL;
556 gpointer pwd_ptr = NULL;
557 gboolean already_asked = FALSE;
559 /* Initialize the output parameter: */
563 const gchar *server_account_name = tny_account_get_id (account);
564 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
566 if (!server_account_name || !account_store) {
567 g_warning ("modest: %s: could not retrieve account_store for account %s",
568 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
575 self = MODEST_TNY_ACCOUNT_STORE (account_store);
576 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
578 /* This hash map stores passwords, including passwords that are not stored in gconf. */
579 /* Is it in the hash? if it's already there, it must be wrong... */
580 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
581 * type-punned ptrs...*/
582 already_asked = priv->password_hash &&
583 g_hash_table_lookup_extended (priv->password_hash,
586 (gpointer*)&pwd_ptr);
588 printf ("DEBUG: modest: %s: Already asked = %d\n", __FUNCTION__, already_asked);
590 /* If the password is not already there, try ModestConf */
591 if (!already_asked) {
592 pwd = modest_server_account_get_password (priv->account_mgr,
593 server_account_name);
594 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
597 /* If it was already asked, it must have been wrong, so ask again */
598 if (already_asked || !pwd || strlen(pwd) == 0) {
599 /* As per the UI spec, if no password was set in the account settings,
600 * ask for it now. But if the password is wrong in the account settings,
601 * then show a banner and the account settings dialog so it can be corrected:
603 const gboolean settings_have_password =
604 modest_server_account_get_has_password (priv->account_mgr, server_account_name);
605 printf ("DEBUG: modest: %s: settings_have_password=%d\n", __FUNCTION__, settings_have_password);
606 if (settings_have_password) {
609 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
610 /* We show it in the main loop, because this function might no tbe in the main loop. */
611 g_object_ref (account); /* unrefed in the idle handler. */
612 g_idle_add (on_idle_wrong_password, account);
620 /* we don't have it yet. Get the password from the user */
621 const gchar* account_id = tny_account_get_id (account);
622 gboolean remember = FALSE;
625 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
626 account_id, /* server_account_name */
627 &username, &pwd, cancel, &remember);
630 /* The password will be returned as the result,
631 * but we need to tell tinymail about the username too: */
632 tny_account_set_user (account, username);
635 printf ("%s: Storing username=%s, password=%s\n",
636 __FUNCTION__, username, pwd);
637 modest_server_account_set_username (priv->account_mgr, server_account_name,
639 modest_server_account_set_password (priv->account_mgr, server_account_name,
642 /* We need to dup the string even knowing that
643 it's already a dup of the contents of an
644 entry, because it if it's wrong, then camel
646 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
648 g_hash_table_remove (priv->password_hash, server_account_name);
659 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
664 /* tinymail calls this if the connection failed due to an incorrect password.
665 * And it seems to call this for any general connection failure. */
667 forget_password (TnyAccount *account)
669 printf ("DEBUG: %s\n", __FUNCTION__);
670 ModestTnyAccountStore *self;
671 ModestTnyAccountStorePrivate *priv;
672 const TnyAccountStore *account_store;
676 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
677 self = MODEST_TNY_ACCOUNT_STORE (account_store);
678 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
679 key = tny_account_get_id (account);
681 /* Do not remove the key, this will allow us to detect that we
682 have already asked for it at least once */
683 pwd = g_hash_table_lookup (priv->password_hash, key);
685 memset (pwd, 0, strlen (pwd));
686 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
689 /* Remove from configuration system */
691 modest_account_mgr_unset (priv->account_mgr,
692 key, MODEST_ACCOUNT_PASSWORD, TRUE);
697 destroy_password_hashtable (ModestTnyAccountStore *self)
699 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
701 g_free (priv->cache_dir);
702 priv->cache_dir = NULL;
704 if (priv->password_hash) {
705 g_hash_table_destroy (priv->password_hash);
706 priv->password_hash = NULL;
711 modest_tny_account_store_finalize (GObject *obj)
713 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
714 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
716 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
718 g_free (priv->cache_dir);
719 priv->cache_dir = NULL;
721 if (priv->password_hash) {
722 g_hash_table_destroy (priv->password_hash);
723 priv->password_hash = NULL;
726 destroy_password_hashtable (self);
728 if (priv->account_mgr) {
729 g_object_unref (G_OBJECT(priv->account_mgr));
730 priv->account_mgr = NULL;
734 g_object_unref (G_OBJECT(priv->device));
738 /* disconnect all accounts when we are destroyed */
739 g_debug ("modest: disconnecting all store accounts");
740 account_list_disconnect (priv->store_accounts);
741 g_debug ("modest: disconnecting all transport accounts");
742 account_list_disconnect (priv->transport_accounts);
744 /* this includes the local folder */
745 account_list_free (priv->store_accounts);
746 priv->store_accounts = NULL;
748 account_list_free (priv->transport_accounts);
749 priv->transport_accounts = NULL;
752 camel_object_unref (CAMEL_OBJECT(priv->session));
753 priv->session = NULL;
756 G_OBJECT_CLASS(parent_class)->finalize (obj);
760 ModestTnyAccountStore*
761 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
764 ModestTnyAccountStorePrivate *priv;
767 g_return_val_if_fail (account_mgr, NULL);
768 g_return_val_if_fail (device, NULL);
770 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
771 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
773 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
774 priv->device = g_object_ref (device);
776 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
777 if (!priv->session) {
778 g_warning ("failed to get TnySessionCamel");
782 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
783 tny_session_camel_set_async_connecting (priv->session, TRUE);
785 /* Connect signals */
786 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
787 G_CALLBACK (on_account_changed), obj);
788 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
789 G_CALLBACK (on_account_removed), obj);
791 return MODEST_TNY_ACCOUNT_STORE(obj);
794 /** Fill the TnyList from the appropriate cached GSList of accounts. */
796 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
798 ModestTnyAccountStorePrivate *priv;
799 GSList *accounts, *cursor;
801 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
802 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
807 GObject *object = G_OBJECT(cursor->data);
808 tny_list_prepend (list, object);
811 cursor = cursor->next;
816 create_per_account_local_outbox_folders (TnyAccountStore *self)
818 g_return_if_fail (self);
820 ModestTnyAccountStorePrivate *priv =
821 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
823 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
825 GSList *accounts = NULL;
827 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
828 TRUE /* including disabled accounts */);
831 for (iter = account_names; iter; iter = g_slist_next (iter)) {
833 const gchar* account_name = (const gchar*)iter->data;
835 /* Create a per-account local outbox folder (a _store_ account)
836 * for each _transport_ account: */
837 TnyAccount *tny_account_outbox =
838 modest_tny_account_new_for_per_account_local_outbox_folder (
839 priv->account_mgr, account_name, priv->session);
841 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
844 modest_account_mgr_free_account_names (account_names);
845 account_names = NULL;
847 priv->store_accounts_outboxes = accounts;
850 /* This function fills the TnyList, and also stores a GSList of the accounts,
851 * for caching purposes. It creates the TnyAccount objects if necessary.
852 * The @list parameter may be NULL, if you just want to fill the cache.
855 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
857 g_return_if_fail (self);
859 ModestTnyAccountStorePrivate *priv =
860 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
862 /* Do nothing if the accounts are already cached: */
863 if (type == TNY_ACCOUNT_TYPE_STORE) {
864 if (priv->store_accounts)
866 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
867 if (priv->transport_accounts)
871 GSList *account_names = NULL, *cursor = NULL;
872 GSList *accounts = NULL;
874 /* These are account names, not server_account names */
875 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
877 for (cursor = account_names; cursor; cursor = cursor->next) {
879 gchar *account_name = (gchar*)cursor->data;
881 /* we get the server_accounts for enabled accounts */
882 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
884 /* Add the account: */
885 TnyAccount *tny_account =
886 modest_tny_account_new_from_account (priv->account_mgr,
892 /* Forget any cached password for the account,
893 * so that we use a new account if any.
894 * TODO: Really we should do this in a more precise way in
895 * on_account_changed().
897 modest_tny_account_store_forget_password_in_memory (
898 MODEST_TNY_ACCOUNT_STORE (self),
899 tny_account_get_id (tny_account));
901 g_object_set_data (G_OBJECT(tny_account), "account_store",
904 tny_list_prepend (list, G_OBJECT(tny_account));
906 accounts = g_slist_append (accounts, tny_account); /* cache it */
908 g_printerr ("modest: failed to create account for %s\n",
913 if (type == TNY_ACCOUNT_TYPE_STORE) {
914 /* Also add the Memory card account if it is mounted: */
915 gboolean mmc_is_mounted = FALSE;
916 GnomeVFSVolumeMonitor* monitor =
917 gnome_vfs_get_volume_monitor();
918 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
919 GList *iter = list_volumes;
921 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
923 if (!mmc_is_mounted) {
924 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
925 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
926 mmc_is_mounted = TRUE;
931 gnome_vfs_volume_unref(volume);
934 iter = g_list_next (iter);
936 g_list_free (list_volumes);
938 if (mmc_is_mounted) {
939 TnyAccount *tny_account =
940 modest_tny_account_new_for_local_folders (priv->account_mgr,
941 priv->session, MODEST_MCC1_VOLUMEPATH);
943 tny_list_prepend (list, G_OBJECT(tny_account));
944 accounts = g_slist_append (accounts, tny_account); /* cache it */
948 /* And add the connection-specific transport accounts, if any.
949 * Note that these server account instances might never be used
950 * if their connections are never active: */
951 /* Look at each modest account: */
952 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
953 GSList *iter_account_names = account_names;
954 while (iter_account_names) {
955 const gchar* account_name = (const gchar*)(iter_account_names->data);
956 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
958 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
959 MODEST_CONF_VALUE_STRING, FALSE);
961 /* Look at each connection-specific transport account for the
963 GSList *iter = list_specifics;
965 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
966 iter = g_slist_next (iter);
968 const gchar* transport_account_name = (const gchar*)(iter->data);
969 if (transport_account_name) {
970 TnyAccount * tny_account = NULL;
971 /* Add the account: */
972 tny_account = modest_tny_account_new_from_server_account_name (
973 priv->account_mgr, priv->session, transport_account_name);
975 g_object_set_data (G_OBJECT(tny_account), "account_store",
978 tny_list_prepend (list, G_OBJECT(tny_account));
980 accounts = g_slist_append (accounts, tny_account); /* cache it */
982 g_printerr ("modest: failed to create smtp-specific account for %s\n",
983 transport_account_name);
987 iter = g_slist_next (iter);
990 iter_account_names = g_slist_next (iter_account_names);
994 /* free the account_names */
995 modest_account_mgr_free_account_names (account_names);
996 account_names = NULL;
998 /* We also create a per-account local outbox folder (a _store_ account)
999 * for each _transport_ account. */
1000 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
1001 /* Now would be a good time to create the per-account local outbox folder
1002 * _store_ accounts corresponding to each transport account: */
1003 if (!priv->store_accounts_outboxes) {
1004 create_per_account_local_outbox_folders (self);
1008 /* But we only return the per-account local outbox folder when
1009 * _store_ accounts are requested. */
1010 if (type == TNY_ACCOUNT_TYPE_STORE) {
1011 /* Create them if necessary,
1012 * (which also requires creating the transport accounts,
1014 if (!priv->store_accounts_outboxes) {
1015 create_per_account_local_outbox_folders (self);
1018 /* Also add the local folder pseudo-account: */
1019 TnyAccount *tny_account =
1020 modest_tny_account_new_for_local_folders (priv->account_mgr,
1021 priv->session, NULL);
1023 /* Add them to the TnyList: */
1024 if (priv->store_accounts_outboxes) {
1025 GSList *iter = NULL;
1026 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
1027 TnyAccount *outbox_account = (TnyAccount*)iter->data;
1028 if (list && outbox_account)
1029 tny_list_prepend (list, G_OBJECT(outbox_account));
1031 g_object_ref (outbox_account);
1032 accounts = g_slist_append (accounts, outbox_account);
1036 /* Add a merged folder, merging all the per-account outbox folders: */
1037 modest_tny_local_folders_account_add_merged_outbox_folders (
1038 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (tny_account), priv->store_accounts_outboxes);
1040 if (priv->store_accounts_outboxes) {
1041 /* We have finished with this temporary list, so free it: */
1042 account_list_free (priv->store_accounts_outboxes);
1043 priv->store_accounts_outboxes = NULL;
1047 tny_list_prepend (list, G_OBJECT(tny_account));
1048 accounts = g_slist_append (accounts, tny_account); /* cache it */
1051 if (type == TNY_ACCOUNT_TYPE_STORE) {
1052 /* Store the cache: */
1053 priv->store_accounts = accounts;
1054 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
1055 /* Store the cache: */
1056 priv->transport_accounts = accounts;
1062 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
1063 TnyGetAccountsRequestType request_type)
1065 ModestTnyAccountStorePrivate *priv;
1067 g_return_if_fail (self);
1068 g_return_if_fail (TNY_IS_LIST(list));
1070 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1072 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
1073 modest_tny_account_store_get_accounts (self, list,
1074 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1075 modest_tny_account_store_get_accounts (self, list,
1076 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
1078 tny_session_camel_set_initialized (priv->session);
1083 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
1084 if (!priv->store_accounts)
1085 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1087 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1089 tny_session_camel_set_initialized (priv->session);
1091 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
1092 if (!priv->transport_accounts)
1093 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1095 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1097 tny_session_camel_set_initialized (priv->session);
1099 g_return_if_reached (); /* incorrect req type */
1104 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
1106 ModestTnyAccountStorePrivate *priv;
1107 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1109 if (!priv->cache_dir)
1110 priv->cache_dir = g_build_filename (g_get_home_dir(),
1111 MODEST_DIR, MODEST_CACHE_DIR, NULL);
1112 return priv->cache_dir;
1117 * callers need to unref
1120 modest_tny_account_store_get_device (TnyAccountStore *self)
1122 ModestTnyAccountStorePrivate *priv;
1124 g_return_val_if_fail (self, NULL);
1126 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1129 return g_object_ref (G_OBJECT(priv->device));
1136 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1138 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1139 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1146 modest_tny_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type,
1147 gboolean question, const GError *error)
1149 g_return_val_if_fail (error, FALSE);
1151 if ((error->domain != TNY_ACCOUNT_ERROR)
1152 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
1153 g_warning("modest: %s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
1154 __FUNCTION__, error->domain, error->message);
1159 printf("DEBUG: %s: GError code: %d, message=%s\n",
1160 __FUNCTION__, error->code, error->message);
1162 /* Get the server name: */
1163 const gchar* server_name = NULL;
1164 if (account && TNY_IS_ACCOUNT (account)) {
1165 server_name = tny_account_get_hostname (account);
1166 printf ("modest: %s: account name = %s, server_name=%s\n", __FUNCTION__,
1167 tny_account_get_id (account), server_name);
1171 server_name = _("Unknown Server");
1173 ModestTransportStoreProtocol proto = MODEST_PROTOCOL_STORE_POP; /* Arbitrary default. */
1175 const gchar *proto_name = tny_account_get_proto (account);
1177 proto = modest_protocol_info_get_transport_store_protocol (proto_name);
1179 g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__,
1180 tny_account_get_id (account));
1184 /* const gchar *prompt = NULL; */
1185 gchar *prompt = NULL;
1186 switch (error->code) {
1187 case TNY_ACCOUNT_STORE_ERROR_CANCEL_ALERT:
1188 case TNY_ACCOUNT_ERROR_TRY_CONNECT_USER_CANCEL:
1189 /* Don't show waste the user's time by showing him a dialog telling
1190 * him that he has just cancelled something: */
1191 g_debug ("%s: Handling GError domain=%d, code=%d (cancelled) without showing a dialog, message=%s",
1192 __FUNCTION__, error->domain, error->code, error->message);
1196 case TNY_ACCOUNT_ERROR_TRY_CONNECT_HOST_LOOKUP_FAILED:
1197 /* TODO: Show the appropriate message, depending on whether it's POP or IMAP: */
1198 g_debug ("%s: Handling GError domain=%d, code=%d (lookup failed), message=%s",
1199 __FUNCTION__, error->domain, error->code, error->message);
1202 case MODEST_PROTOCOL_STORE_POP:
1203 prompt = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), server_name);
1205 case MODEST_PROTOCOL_STORE_IMAP:
1206 prompt = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), server_name);
1208 case MODEST_PROTOCOL_TRANSPORT_SMTP:
1209 default: /* Arbitrary default. */
1210 prompt = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
1215 prompt = g_strdup_printf(
1216 _("Incorrect Account Settings:\n Host lookup failed.%s"),
1221 case TNY_ACCOUNT_ERROR_TRY_CONNECT_AUTHENTICATION_NOT_SUPPORTED:
1222 g_debug ("%s: Handling GError domain=%d, code=%d (authentication not supported), message=%s",
1223 __FUNCTION__, error->domain, error->code, error->message);
1224 /* TODO: This needs a logical ID for the string: */
1225 prompt = g_strdup_printf(
1226 _("Incorrect Account Settings:\nThe secure authentication method is not supported.\n%s"),
1230 case TNY_ACCOUNT_ERROR_TRY_CONNECT_CERTIFICATE:
1231 g_debug ("%s: Handling GError domain=%d, code=%d (certificatae), message=%s",
1232 __FUNCTION__, error->domain, error->code, error->message);
1233 prompt = g_strdup_printf(
1234 _("Certificate Problem:\n%s"),
1238 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
1239 /* The tinymail camel implementation just sends us this for almost
1240 * everything, so we have to guess at the cause.
1241 * It could be a wrong password, or inability to resolve a hostname,
1242 * or lack of network, or incorrect authentication method, or something entirely different: */
1243 /* TODO: Fix camel to provide specific error codes, and then use the
1244 * specific dialog messages from Chapter 12 of the UI spec.
1246 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
1247 /* This debug output is useful. Please keep it uncommented until
1248 * we have fixed the problems in this function: */
1249 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
1250 __FUNCTION__, error->domain, error->code, error->message);
1252 /* TODO: Remove the internal error message for the real release.
1253 * This is just so the testers can give us more information: */
1254 /* prompt = _("Modest account not yet fully configured."); */
1255 prompt = g_strdup_printf(
1256 "%s\n (Internal error message, often very misleading):\n%s",
1257 _("Incorrect Account Settings"),
1260 /* Note: If the password was wrong then get_password() would be called again,
1261 * instead of this vfunc being called. */
1266 g_warning ("%s: Unhandled GError code: %d, message=%s",
1267 __FUNCTION__, error->code, error->message);
1275 #ifdef MODEST_PLATFORM_MAEMO
1276 /* The Tinymail documentation says that we should show Yes and No buttons,
1277 * when it is a question.
1278 * Obviously, we need tinymail to use more specific error codes instead,
1279 * so we know what buttons to show. */
1280 GtkWidget *dialog = NULL;
1282 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
1285 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
1290 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1293 case TNY_ALERT_TYPE_INFO:
1294 gtktype = GTK_MESSAGE_INFO;
1296 case TNY_ALERT_TYPE_WARNING:
1297 gtktype = GTK_MESSAGE_WARNING;
1299 case TNY_ALERT_TYPE_ERROR:
1301 gtktype = GTK_MESSAGE_ERROR;
1305 GtkWidget *dialog = gtk_message_dialog_new (NULL, 0,
1306 gtktype, GTK_BUTTONS_YES_NO, prompt);
1307 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1309 gboolean retval = TRUE;
1310 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1312 retval = (response == GTK_RESPONSE_YES) ||
1313 (response == GTK_RESPONSE_OK);
1316 gtk_widget_destroy (dialog);
1318 /* TODO: Don't free this when we no longer strdup the message for testers. */
1322 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1328 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1330 TnyAccountStoreIface *klass;
1332 g_return_if_fail (g);
1334 klass = (TnyAccountStoreIface *)g;
1336 klass->get_accounts_func =
1337 modest_tny_account_store_get_accounts;
1338 klass->get_cache_dir_func =
1339 modest_tny_account_store_get_cache_dir;
1340 klass->get_device_func =
1341 modest_tny_account_store_get_device;
1343 modest_tny_account_store_alert;
1344 klass->find_account_func =
1345 modest_tny_account_store_find_account_by_url;
1349 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1350 ModestTnyGetPassFunc func)
1352 /* not implemented, we use signals */
1353 g_printerr ("modest: set_get_pass_func not implemented\n");
1357 modest_tny_account_store_get_session (TnyAccountStore *self)
1359 g_return_val_if_fail (self, NULL);
1360 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1366 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1367 ModestTnyAccountStoreQueryType type,
1370 TnyAccount *account = NULL;
1371 ModestTnyAccountStorePrivate *priv;
1373 const gchar *val = NULL;
1377 g_return_val_if_fail (self, NULL);
1378 g_return_val_if_fail (str, NULL);
1380 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1382 /* fill the caches, as that may not have happened yet */
1383 list = TNY_LIST(tny_simple_list_new());
1384 modest_tny_account_store_get_accounts (TNY_ACCOUNT_STORE(self),
1385 list, TNY_ACCOUNT_STORE_BOTH);
1386 g_object_unref (list);
1390 /* Search in store accounts */
1391 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1393 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1394 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1396 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1397 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1401 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1402 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), str)) {
1403 account = TNY_ACCOUNT (cursor->data);
1406 if (strcmp (val, str) == 0) {
1407 account = TNY_ACCOUNT(cursor->data);
1413 /* if we already found something, no need to search the transport accounts */
1414 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1416 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1417 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1419 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1420 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1424 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1425 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), val)) {
1426 account = TNY_ACCOUNT (cursor->data);
1429 if (strcmp (val, str) == 0) {
1430 account = TNY_ACCOUNT(cursor->data);
1437 g_object_ref (G_OBJECT(account));
1439 /* Warn if nothing was found. This is generally unusual. */
1441 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1442 g_warning("%s: Failed to find account with ID=%s\n", __FUNCTION__, str);
1444 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1445 g_warning("%s: Failed to find account with URL=%s\n", __FUNCTION__, str);
1454 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1455 const gchar *account_name,
1456 TnyAccountType type)
1458 TnyAccount *account = NULL;
1460 ModestTnyAccountStorePrivate *priv;
1462 g_return_val_if_fail (self, NULL);
1463 g_return_val_if_fail (account_name, NULL);
1464 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1467 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1469 /* Special case for the local account */
1470 if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1471 if(type == TNY_ACCOUNT_TYPE_STORE)
1472 id = g_strdup (MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
1474 /* The local folders modest account has no transport server account. */
1478 ModestAccountData *account_data;
1479 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
1480 if (!account_data) {
1481 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__,
1486 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1487 id = g_strdup (account_data->store_account->account_name);
1488 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
1489 id = g_strdup (account_data->transport_account->account_name);
1491 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1495 g_printerr ("modest: could not get an id for account %s\n",
1498 account = modest_tny_account_store_get_tny_account_by (self,
1499 MODEST_TNY_ACCOUNT_STORE_QUERY_ID, id);
1502 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1503 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1504 account_name, id ? id : "<none>");
1511 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1512 const gchar *account_name)
1514 /* Get the current connection: */
1515 TnyDevice *device = modest_runtime_get_device ();
1517 if (!tny_device_is_online (device))
1520 g_return_val_if_fail (self, NULL);
1521 g_return_val_if_fail (account_name, NULL);
1524 #ifdef MODEST_PLATFORM_MAEMO
1525 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1526 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1527 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1528 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1532 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1536 const gchar *connection_name = con_ic_iap_get_name (connection);
1537 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1538 if (!connection_name)
1541 /* Get the connection-specific transport acccount, if any: */
1542 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1543 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1544 account_name, connection_name);
1546 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1547 if (!server_account_name) {
1548 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1551 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1552 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1553 server_account_name);
1555 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1556 g_free (server_account_name);
1558 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1559 g_object_unref (connection);
1563 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1564 #endif /* MODEST_PLATFORM_MAEMO */
1569 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1570 const gchar *account_name)
1572 g_return_val_if_fail (self, NULL);
1573 g_return_val_if_fail (account_name, NULL);
1575 if (!account_name || !self)
1578 /* Get the connection-specific transport acccount, if any: */
1579 TnyAccount *account =
1580 get_smtp_specific_transport_account_for_open_connection (self, account_name);
1582 /* If there is no connection-specific transport account (the common case),
1583 * just get the regular transport account: */
1585 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1587 /* The special local folders don't have transport accounts. */
1588 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1591 account = modest_tny_account_store_get_server_account (self, account_name,
1592 TNY_ACCOUNT_TYPE_TRANSPORT);
1599 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1601 /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
1602 * for anything else. */
1603 return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1608 modest_tny_account_is_memory_card_account (TnyAccount *self)
1613 const gchar* account_id = tny_account_get_id (self);
1617 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
1621 modest_tny_account_store_get_local_folders_account (TnyAccountStore *self)
1623 TnyAccount *account = NULL;
1624 ModestTnyAccountStorePrivate *priv;
1627 g_return_val_if_fail (self, NULL);
1629 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1631 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1632 TnyAccount *this_account = TNY_ACCOUNT(cursor->data);
1633 if (modest_tny_account_is_virtual_local_folders (this_account)) {
1634 account = this_account;
1640 g_object_ref (G_OBJECT(account));