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 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
381 const GSList *keys, gboolean server_account, gpointer user_data)
384 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
386 /* Ignore the change if it's a change in the last_updated value */
387 if (g_slist_length ((GSList *)keys) == 1 &&
388 g_str_has_suffix ((const gchar *) keys->data, MODEST_ACCOUNT_LAST_UPDATED))
391 /* FIXME: make this more finegrained; changes do not really affect _all_
394 recreate_all_accounts (self);
396 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
401 static ModestTnyAccountStore*
402 get_account_store_for_account (TnyAccount *account)
404 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
409 void on_account_settings_hide (GtkWidget *widget, gpointer user_data)
411 TnyAccount *account = (TnyAccount*)user_data;
413 /* This is easier than using a struct for the user_data: */
414 ModestTnyAccountStore *self = modest_runtime_get_account_store();
415 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
417 const gchar *modest_account_name =
418 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
419 if (modest_account_name)
420 g_hash_table_remove (priv->account_settings_dialog_hash, modest_account_name);
424 gboolean on_idle_wrong_password (gpointer user_data)
426 TnyAccount *account = (TnyAccount*)user_data;
427 /* This is easier than using a struct for the user_data: */
428 ModestTnyAccountStore *self = modest_runtime_get_account_store();
429 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
431 const gchar *modest_account_name =
432 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
433 if (!modest_account_name) {
434 g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n",
437 g_object_unref (account);
442 /* Check whether this window is already open,
443 * for instance because of a previous get_password() call:
445 gpointer dialog_as_gpointer = NULL;
446 gboolean found = FALSE;
447 if (priv->account_settings_dialog_hash) {
448 found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
449 modest_account_name, NULL, (gpointer*)&dialog_as_gpointer);
452 ModestAccountSettingsDialog *dialog = dialog_as_gpointer;
454 ModestWindow *main_window =
455 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
457 gdk_threads_enter ();
458 gboolean created_dialog = FALSE;
459 if (!found || !dialog) {
460 dialog = modest_account_settings_dialog_new ();
461 modest_account_settings_dialog_set_account_name (dialog, modest_account_name);
462 modest_account_settings_dialog_switch_to_user_info (dialog);
464 g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (modest_account_name), dialog);
466 created_dialog = TRUE;
469 /* Show an explanatory temporary banner: */
470 hildon_banner_show_information (
471 GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
473 if (created_dialog) {
474 /* Forget it when it closes: */
475 g_signal_connect_object (G_OBJECT (dialog), "hide", G_CALLBACK (on_account_settings_hide),
478 /* Show it and delete it when it closes: */
479 modest_maemo_show_dialog_and_forget (GTK_WINDOW (main_window), GTK_DIALOG (dialog));
482 /* Just show it instead of showing it and deleting it when it closes,
483 * though it is probably open already: */
484 gtk_window_present (GTK_WINDOW (dialog));
487 g_object_unref (account);
488 gdk_threads_leave ();
490 return FALSE; /* Dont' call this again. */
493 /* This callback will be called by Tinymail when it needs the password
494 * from the user or the account settings.
495 * It can also call forget_password() before calling this,
496 * so that we clear wrong passwords out of our account settings.
497 * Note that TnyAccount here will be the server account. */
499 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
501 /* TODO: Settting cancel to FALSE does not actually cancel everything.
502 * We still get multiple requests afterwards, so we end up showing the
503 * same dialogs repeatedly.
506 printf ("DEBUG: modest: %s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
508 g_return_val_if_fail (account, NULL);
510 const TnyAccountStore *account_store = NULL;
511 ModestTnyAccountStore *self = NULL;
512 ModestTnyAccountStorePrivate *priv;
513 gchar *username = NULL;
515 gpointer pwd_ptr = NULL;
516 gboolean already_asked = FALSE;
518 /* Initialize the output parameter: */
522 const gchar *server_account_name = tny_account_get_id (account);
523 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
525 if (!server_account_name || !account_store) {
526 g_warning ("%s: could not retrieve account_store for account %s",
527 __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
534 self = MODEST_TNY_ACCOUNT_STORE (account_store);
535 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
537 /* This hash map stores passwords, including passwords that are not stored in gconf. */
538 /* Is it in the hash? if it's already there, it must be wrong... */
539 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
540 * type-punned ptrs...*/
541 already_asked = priv->password_hash &&
542 g_hash_table_lookup_extended (priv->password_hash,
545 (gpointer*)&pwd_ptr);
547 printf ("DEBUG: modest: %s: Already asked = %d\n", __FUNCTION__, already_asked);
549 /* If the password is not already there, try ModestConf */
550 if (!already_asked) {
551 pwd = modest_server_account_get_password (priv->account_mgr,
552 server_account_name);
553 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
556 /* If it was already asked, it must have been wrong, so ask again */
557 if (already_asked || !pwd || strlen(pwd) == 0) {
558 /* As per the UI spec, if no password was set in the account settings,
559 * ask for it now. But if the password is wrong in the account settings,
560 * then show a banner and the account settings dialog so it can be corrected:
562 const gboolean settings_have_password =
563 modest_server_account_get_has_password (priv->account_mgr, server_account_name);
564 printf ("DEBUG: modest: %s: settings_have_password=%d\n", __FUNCTION__, settings_have_password);
565 if (settings_have_password) {
568 /* The password must be wrong, so show the account settings dialog so it can be corrected: */
569 /* We show it in the main loop, because this function might no tbe in the main loop. */
570 g_object_ref (account); /* unrefed in the idle handler. */
571 g_idle_add (on_idle_wrong_password, account);
579 /* we don't have it yet. Get the password from the user */
580 const gchar* account_id = tny_account_get_id (account);
581 gboolean remember = FALSE;
584 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
585 account_id, /* server_account_name */
586 &username, &pwd, cancel, &remember);
589 /* The password will be returned as the result,
590 * but we need to tell tinymail about the username too: */
591 tny_account_set_user (account, username);
594 printf ("%s: Storing username=%s, password=%s\n",
595 __FUNCTION__, username, pwd);
596 modest_server_account_set_username (priv->account_mgr, server_account_name,
598 modest_server_account_set_password (priv->account_mgr, server_account_name,
601 /* We need to dup the string even knowing that
602 it's already a dup of the contents of an
603 entry, because it if it's wrong, then camel
605 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
607 g_hash_table_remove (priv->password_hash, server_account_name);
618 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
623 /* tinymail calls this if the connection failed due to an incorrect password.
624 * And it seems to call this for any general connection failure. */
626 forget_password (TnyAccount *account)
628 printf ("DEBUG: %s\n", __FUNCTION__);
629 ModestTnyAccountStore *self;
630 ModestTnyAccountStorePrivate *priv;
631 const TnyAccountStore *account_store;
635 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
636 self = MODEST_TNY_ACCOUNT_STORE (account_store);
637 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
638 key = tny_account_get_id (account);
640 /* Do not remove the key, this will allow us to detect that we
641 have already asked for it at least once */
642 pwd = g_hash_table_lookup (priv->password_hash, key);
644 memset (pwd, 0, strlen (pwd));
645 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
648 /* Remove from configuration system */
650 modest_account_mgr_unset (priv->account_mgr,
651 key, MODEST_ACCOUNT_PASSWORD, TRUE);
657 modest_tny_account_store_finalize (GObject *obj)
659 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
660 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
662 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
664 g_free (priv->cache_dir);
665 priv->cache_dir = NULL;
667 if (priv->password_hash) {
668 g_hash_table_destroy (priv->password_hash);
669 priv->password_hash = NULL;
672 if (priv->account_settings_dialog_hash) {
673 g_hash_table_destroy (priv->account_settings_dialog_hash);
674 priv->account_settings_dialog_hash = NULL;
677 if (priv->account_mgr) {
678 g_object_unref (G_OBJECT(priv->account_mgr));
679 priv->account_mgr = NULL;
683 g_object_unref (G_OBJECT(priv->device));
687 /* disconnect all accounts when we are destroyed */
688 g_debug ("modest: disconnecting all store accounts");
689 account_list_disconnect (priv->store_accounts);
690 g_debug ("modest: disconnecting all transport accounts");
691 account_list_disconnect (priv->transport_accounts);
693 /* this includes the local folder */
694 account_list_free (priv->store_accounts);
695 priv->store_accounts = NULL;
697 account_list_free (priv->transport_accounts);
698 priv->transport_accounts = NULL;
701 camel_object_unref (CAMEL_OBJECT(priv->session));
702 priv->session = NULL;
705 G_OBJECT_CLASS(parent_class)->finalize (obj);
709 ModestTnyAccountStore*
710 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
713 ModestTnyAccountStorePrivate *priv;
716 g_return_val_if_fail (account_mgr, NULL);
717 g_return_val_if_fail (device, NULL);
719 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
720 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
722 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
723 priv->device = g_object_ref (device);
725 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
726 if (!priv->session) {
727 g_warning ("failed to get TnySessionCamel");
731 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
732 tny_session_camel_set_async_connecting (priv->session, TRUE);
734 /* Connect signals */
735 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
736 G_CALLBACK (on_account_changed), obj);
737 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
738 G_CALLBACK (on_account_removed), obj);
740 return MODEST_TNY_ACCOUNT_STORE(obj);
743 /** Fill the TnyList from the appropriate cached GSList of accounts. */
745 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
747 ModestTnyAccountStorePrivate *priv;
748 GSList *accounts, *cursor;
750 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
751 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
756 GObject *object = G_OBJECT(cursor->data);
757 tny_list_prepend (list, object);
760 cursor = cursor->next;
765 create_per_account_local_outbox_folders (TnyAccountStore *self)
767 g_return_if_fail (self);
769 ModestTnyAccountStorePrivate *priv =
770 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
772 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
774 GSList *accounts = NULL;
776 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
777 TRUE /* including disabled accounts */);
780 for (iter = account_names; iter; iter = g_slist_next (iter)) {
782 const gchar* account_name = (const gchar*)iter->data;
784 /* Create a per-account local outbox folder (a _store_ account)
785 * for each _transport_ account: */
786 TnyAccount *tny_account_outbox =
787 modest_tny_account_new_for_per_account_local_outbox_folder (
788 priv->account_mgr, account_name, priv->session);
790 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
793 modest_account_mgr_free_account_names (account_names);
794 account_names = NULL;
796 priv->store_accounts_outboxes = accounts;
799 /* This function fills the TnyList, and also stores a GSList of the accounts,
800 * for caching purposes. It creates the TnyAccount objects if necessary.
801 * The @list parameter may be NULL, if you just want to fill the cache.
804 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
806 g_return_if_fail (self);
808 ModestTnyAccountStorePrivate *priv =
809 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
811 /* Do nothing if the accounts are already cached: */
812 if (type == TNY_ACCOUNT_TYPE_STORE) {
813 if (priv->store_accounts)
815 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
816 if (priv->transport_accounts)
820 GSList *account_names = NULL, *cursor = NULL;
821 GSList *accounts = NULL;
823 /* These are account names, not server_account names */
824 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
826 for (cursor = account_names; cursor; cursor = cursor->next) {
828 gchar *account_name = (gchar*)cursor->data;
830 /* we get the server_accounts for enabled accounts */
831 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
833 /* Add the account: */
834 TnyAccount *tny_account =
835 modest_tny_account_new_from_account (priv->account_mgr,
841 g_object_set_data (G_OBJECT(tny_account), "account_store",
844 tny_list_prepend (list, G_OBJECT(tny_account));
846 accounts = g_slist_append (accounts, tny_account); /* cache it */
848 g_printerr ("modest: failed to create account for %s\n",
853 if (type == TNY_ACCOUNT_TYPE_STORE) {
854 /* Also add the Memory card account if it is mounted: */
855 gboolean mmc_is_mounted = FALSE;
856 GnomeVFSVolumeMonitor* monitor =
857 gnome_vfs_get_volume_monitor();
858 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
859 GList *iter = list_volumes;
861 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
863 if (!mmc_is_mounted) {
864 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
865 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
866 mmc_is_mounted = TRUE;
871 gnome_vfs_volume_unref(volume);
874 iter = g_list_next (iter);
876 g_list_free (list_volumes);
878 if (mmc_is_mounted) {
879 TnyAccount *tny_account =
880 modest_tny_account_new_for_local_folders (priv->account_mgr,
881 priv->session, MODEST_MCC1_VOLUMEPATH);
883 tny_list_prepend (list, G_OBJECT(tny_account));
884 accounts = g_slist_append (accounts, tny_account); /* cache it */
888 /* And add the connection-specific transport accounts, if any.
889 * Note that these server account instances might never be used
890 * if their connections are never active: */
891 /* Look at each modest account: */
892 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
893 GSList *iter_account_names = account_names;
894 while (iter_account_names) {
895 const gchar* account_name = (const gchar*)(iter_account_names->data);
896 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
898 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
899 MODEST_CONF_VALUE_STRING, FALSE);
901 /* Look at each connection-specific transport account for the
903 GSList *iter = list_specifics;
905 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
906 iter = g_slist_next (iter);
908 const gchar* transport_account_name = (const gchar*)(iter->data);
909 if (transport_account_name) {
910 TnyAccount * tny_account = NULL;
911 /* Add the account: */
912 tny_account = modest_tny_account_new_from_server_account_name (
913 priv->account_mgr, priv->session, transport_account_name);
915 g_object_set_data (G_OBJECT(tny_account), "account_store",
918 tny_list_prepend (list, G_OBJECT(tny_account));
920 accounts = g_slist_append (accounts, tny_account); /* cache it */
922 g_printerr ("modest: failed to create smtp-specific account for %s\n",
923 transport_account_name);
927 iter = g_slist_next (iter);
930 iter_account_names = g_slist_next (iter_account_names);
934 /* free the account_names */
935 modest_account_mgr_free_account_names (account_names);
936 account_names = NULL;
938 /* We also create a per-account local outbox folder (a _store_ account)
939 * for each _transport_ account. */
940 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
941 /* Now would be a good time to create the per-account local outbox folder
942 * _store_ accounts corresponding to each transport account: */
943 if (!priv->store_accounts_outboxes) {
944 create_per_account_local_outbox_folders (self);
948 /* But we only return the per-account local outbox folder when
949 * _store_ accounts are requested. */
950 if (type == TNY_ACCOUNT_TYPE_STORE) {
951 /* Create them if necessary,
952 * (which also requires creating the transport accounts,
954 if (!priv->store_accounts_outboxes) {
955 create_per_account_local_outbox_folders (self);
958 /* Also add the local folder pseudo-account: */
959 TnyAccount *tny_account =
960 modest_tny_account_new_for_local_folders (priv->account_mgr,
961 priv->session, NULL);
963 /* Add them to the TnyList: */
964 if (priv->store_accounts_outboxes) {
966 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
967 TnyAccount *outbox_account = (TnyAccount*)iter->data;
968 if (list && outbox_account)
969 tny_list_prepend (list, G_OBJECT(outbox_account));
971 g_object_ref (outbox_account);
972 accounts = g_slist_append (accounts, outbox_account);
976 /* Add a merged folder, merging all the per-account outbox folders: */
977 modest_tny_local_folders_account_add_merged_outbox_folders (
978 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (tny_account), priv->store_accounts_outboxes);
980 if (priv->store_accounts_outboxes) {
981 /* We have finished with this temporary list, so free it: */
982 account_list_free (priv->store_accounts_outboxes);
983 priv->store_accounts_outboxes = NULL;
987 tny_list_prepend (list, G_OBJECT(tny_account));
988 accounts = g_slist_append (accounts, tny_account); /* cache it */
991 if (type == TNY_ACCOUNT_TYPE_STORE) {
992 /* Store the cache: */
993 priv->store_accounts = accounts;
994 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
995 /* Store the cache: */
996 priv->transport_accounts = accounts;
1002 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
1003 TnyGetAccountsRequestType request_type)
1005 ModestTnyAccountStorePrivate *priv;
1007 g_return_if_fail (self);
1008 g_return_if_fail (TNY_IS_LIST(list));
1010 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1012 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
1013 modest_tny_account_store_get_accounts (self, list,
1014 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
1015 modest_tny_account_store_get_accounts (self, list,
1016 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
1020 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
1021 if (!priv->store_accounts)
1022 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1024 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
1026 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
1027 if (!priv->transport_accounts)
1028 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1030 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
1032 g_return_if_reached (); /* incorrect req type */
1037 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
1039 ModestTnyAccountStorePrivate *priv;
1040 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1042 if (!priv->cache_dir)
1043 priv->cache_dir = g_build_filename (g_get_home_dir(),
1044 MODEST_DIR, MODEST_CACHE_DIR, NULL);
1045 return priv->cache_dir;
1050 * callers need to unref
1053 modest_tny_account_store_get_device (TnyAccountStore *self)
1055 ModestTnyAccountStorePrivate *priv;
1057 g_return_val_if_fail (self, NULL);
1059 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1062 return g_object_ref (G_OBJECT(priv->device));
1069 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1071 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
1072 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1079 modest_tny_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type,
1080 gboolean question, const GError *error)
1082 g_return_val_if_fail (error, FALSE);
1084 if ((error->domain != TNY_ACCOUNT_ERROR)
1085 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
1086 g_warning("modest: %s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
1087 __FUNCTION__, error->domain, error->message);
1092 printf("DEBUG: %s: GError code: %d, message=%s\n",
1093 __FUNCTION__, error->code, error->message);
1095 /* Get the server name: */
1096 const gchar* server_name = NULL;
1097 if (account && TNY_IS_ACCOUNT (account)) {
1098 server_name = tny_account_get_hostname (account);
1099 printf ("modest: %s: account name = %s, server_name=%s\n", __FUNCTION__,
1100 tny_account_get_id (account), server_name);
1104 server_name = _("Unknown Server");
1106 const ModestTransportStoreProtocol proto
1107 = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
1109 /* const gchar *prompt = NULL; */
1110 gchar *prompt = NULL;
1111 switch (error->code) {
1112 case TNY_ACCOUNT_STORE_ERROR_CANCEL_ALERT:
1113 case TNY_ACCOUNT_ERROR_TRY_CONNECT_USER_CANCEL:
1114 /* Don't show waste the user's time by showing him a dialog telling
1115 * him that he has just cancelled something: */
1116 g_debug ("%s: Handling GError domain=%d, code=%d (cancelled) without showing a dialog, message=%s",
1117 __FUNCTION__, error->domain, error->code, error->message);
1121 case TNY_ACCOUNT_ERROR_TRY_CONNECT_HOST_LOOKUP_FAILED:
1122 /* TODO: Show the appropriate message, depending on whether it's POP or IMAP: */
1123 g_debug ("%s: Handling GError domain=%d, code=%d (lookup failed), message=%s",
1124 __FUNCTION__, error->domain, error->code, error->message);
1127 case MODEST_PROTOCOL_STORE_POP:
1128 prompt = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), server_name);
1130 case MODEST_PROTOCOL_STORE_IMAP:
1131 prompt = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), server_name);
1133 case MODEST_PROTOCOL_TRANSPORT_SMTP:
1134 default: /* Arbitrary default. */
1135 prompt = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
1140 prompt = g_strdup_printf(
1141 _("Incorrect Account Settings:\n Host lookup failed.%s"),
1146 case TNY_ACCOUNT_ERROR_TRY_CONNECT_AUTHENTICATION_NOT_SUPPORTED:
1147 g_debug ("%s: Handling GError domain=%d, code=%d (authentication not supported), message=%s",
1148 __FUNCTION__, error->domain, error->code, error->message);
1149 /* TODO: This needs a logical ID for the string: */
1150 prompt = g_strdup_printf(
1151 _("Incorrect Account Settings:\nThe secure authentication method is not supported.\n%s"),
1155 case TNY_ACCOUNT_ERROR_TRY_CONNECT_CERTIFICATE:
1156 g_debug ("%s: Handling GError domain=%d, code=%d (certificatae), message=%s",
1157 __FUNCTION__, error->domain, error->code, error->message);
1158 prompt = g_strdup_printf(
1159 _("Certificate Problem:\n%s"),
1163 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
1164 /* The tinymail camel implementation just sends us this for almost
1165 * everything, so we have to guess at the cause.
1166 * It could be a wrong password, or inability to resolve a hostname,
1167 * or lack of network, or incorrect authentication method, or something entirely different: */
1168 /* TODO: Fix camel to provide specific error codes, and then use the
1169 * specific dialog messages from Chapter 12 of the UI spec.
1171 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
1172 /* This debug output is useful. Please keep it uncommented until
1173 * we have fixed the problems in this function: */
1174 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
1175 __FUNCTION__, error->domain, error->code, error->message);
1177 /* TODO: Remove the internal error message for the real release.
1178 * This is just so the testers can give us more information: */
1179 /* prompt = _("Modest account not yet fully configured."); */
1180 prompt = g_strdup_printf(
1181 "%s\n (Internal error message, often very misleading):\n%s",
1182 _("Incorrect Account Settings"),
1185 /* Note: If the password was wrong then get_password() would be called again,
1186 * instead of this vfunc being called. */
1191 g_warning ("%s: Unhandled GError code: %d, message=%s",
1192 __FUNCTION__, error->code, error->message);
1200 #ifdef MODEST_PLATFORM_MAEMO
1201 /* The Tinymail documentation says that we should show Yes and No buttons,
1202 * when it is a question.
1203 * Obviously, we need tinymail to use more specific error codes instead,
1204 * so we know what buttons to show. */
1205 GtkWidget *dialog = NULL;
1207 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
1210 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
1215 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1218 case TNY_ALERT_TYPE_INFO:
1219 gtktype = GTK_MESSAGE_INFO;
1221 case TNY_ALERT_TYPE_WARNING:
1222 gtktype = GTK_MESSAGE_WARNING;
1224 case TNY_ALERT_TYPE_ERROR:
1226 gtktype = GTK_MESSAGE_ERROR;
1230 GtkWidget *dialog = gtk_message_dialog_new (NULL, 0,
1231 gtktype, GTK_BUTTONS_YES_NO, prompt);
1232 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1234 gboolean retval = TRUE;
1235 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1237 retval = (response == GTK_RESPONSE_YES) ||
1238 (response == GTK_RESPONSE_OK);
1241 gtk_widget_destroy (dialog);
1243 /* TODO: Don't free this when we no longer strdup the message for testers. */
1247 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1253 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1255 TnyAccountStoreIface *klass;
1257 g_return_if_fail (g);
1259 klass = (TnyAccountStoreIface *)g;
1261 klass->get_accounts_func =
1262 modest_tny_account_store_get_accounts;
1263 klass->get_cache_dir_func =
1264 modest_tny_account_store_get_cache_dir;
1265 klass->get_device_func =
1266 modest_tny_account_store_get_device;
1268 modest_tny_account_store_alert;
1269 klass->find_account_func =
1270 modest_tny_account_store_find_account_by_url;
1274 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1275 ModestTnyGetPassFunc func)
1277 /* not implemented, we use signals */
1278 g_printerr ("modest: set_get_pass_func not implemented\n");
1282 modest_tny_account_store_get_session (TnyAccountStore *self)
1284 g_return_val_if_fail (self, NULL);
1285 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1291 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1292 ModestTnyAccountStoreQueryType type,
1295 TnyAccount *account = NULL;
1296 ModestTnyAccountStorePrivate *priv;
1298 const gchar *val = NULL;
1302 g_return_val_if_fail (self, NULL);
1303 g_return_val_if_fail (str, NULL);
1305 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1307 /* fill the caches, as that may not have happened yet */
1308 list = TNY_LIST(tny_simple_list_new());
1309 modest_tny_account_store_get_accounts (TNY_ACCOUNT_STORE(self),
1310 list, TNY_ACCOUNT_STORE_BOTH);
1311 g_object_unref (list);
1315 /* Search in store accounts */
1316 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1318 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1319 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1321 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1322 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1326 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1327 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), str)) {
1328 account = TNY_ACCOUNT (cursor->data);
1331 if (strcmp (val, str) == 0) {
1332 account = TNY_ACCOUNT(cursor->data);
1338 /* if we already found something, no need to search the transport accounts */
1339 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1341 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1342 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1344 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1345 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1349 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1350 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), val)) {
1351 account = TNY_ACCOUNT (cursor->data);
1354 if (strcmp (val, str) == 0) {
1355 account = TNY_ACCOUNT(cursor->data);
1362 g_object_ref (G_OBJECT(account));
1364 /* Warn if nothing was found. This is generally unusual. */
1366 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1367 g_warning("%s: Failed to find account with ID=%s\n", __FUNCTION__, str);
1369 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1370 g_warning("%s: Failed to find account with URL=%s\n", __FUNCTION__, str);
1379 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1380 const gchar *account_name,
1381 TnyAccountType type)
1383 TnyAccount *account = NULL;
1385 ModestTnyAccountStorePrivate *priv;
1387 g_return_val_if_fail (self, NULL);
1388 g_return_val_if_fail (account_name, NULL);
1389 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1392 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1394 /* Special case for the local account */
1395 if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1396 if(type == TNY_ACCOUNT_TYPE_STORE)
1397 id = g_strdup (MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
1399 /* The local folders modest account has no transport server account. */
1403 ModestAccountData *account_data;
1404 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
1405 if (!account_data) {
1406 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__,
1411 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1412 id = g_strdup (account_data->store_account->account_name);
1413 else if (type == TNY_ACCOUNT_TYPE_TRANSPORT && account_data->transport_account)
1414 id = g_strdup (account_data->transport_account->account_name);
1416 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1420 g_printerr ("modest: could not get an id for account %s\n",
1423 account = modest_tny_account_store_get_tny_account_by (self,
1424 MODEST_TNY_ACCOUNT_STORE_QUERY_ID, id);
1427 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1428 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1429 account_name, id ? id : "<none>");
1436 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1437 const gchar *account_name)
1439 /* Get the current connection: */
1440 TnyDevice *device = modest_runtime_get_device ();
1442 if (!tny_device_is_online (device))
1445 g_return_val_if_fail (self, NULL);
1446 g_return_val_if_fail (account_name, NULL);
1449 #ifdef MODEST_PLATFORM_MAEMO
1450 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1451 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1452 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1453 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1457 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1461 const gchar *connection_name = con_ic_iap_get_name (connection);
1462 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1463 if (!connection_name)
1466 /* Get the connection-specific transport acccount, if any: */
1467 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1468 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1469 account_name, connection_name);
1471 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1472 if (!server_account_name) {
1473 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1476 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1477 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1478 server_account_name);
1480 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1481 g_free (server_account_name);
1483 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1484 g_object_unref (connection);
1488 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1489 #endif /* MODEST_PLATFORM_MAEMO */
1494 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1495 const gchar *account_name)
1497 g_return_val_if_fail (self, NULL);
1498 g_return_val_if_fail (account_name, NULL);
1500 if (!account_name || !self)
1503 /* Get the connection-specific transport acccount, if any: */
1504 TnyAccount *account =
1505 get_smtp_specific_transport_account_for_open_connection (self, account_name);
1507 /* If there is no connection-specific transport account (the common case),
1508 * just get the regular transport account: */
1510 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1512 /* The special local folders don't have transport accounts. */
1513 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1516 account = modest_tny_account_store_get_server_account (self, account_name,
1517 TNY_ACCOUNT_TYPE_TRANSPORT);
1524 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1526 /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
1527 * for anything else. */
1528 return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1533 modest_tny_account_is_memory_card_account (TnyAccount *self)
1538 const gchar* account_id = tny_account_get_id (self);
1542 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
1546 modest_tny_account_store_get_local_folders_account (TnyAccountStore *self)
1548 TnyAccount *account = NULL;
1549 ModestTnyAccountStorePrivate *priv;
1552 g_return_val_if_fail (self, NULL);
1554 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1556 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1557 TnyAccount *this_account = TNY_ACCOUNT(cursor->data);
1558 if (modest_tny_account_is_virtual_local_folders (this_account)) {
1559 account = this_account;
1565 g_object_ref (G_OBJECT(account));