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>
56 #include "modest-tny-account-store.h"
57 #include "modest-tny-platform-factory.h"
58 #include <tny-gtk-lockable.h>
59 #include <camel/camel.h>
61 #ifdef MODEST_PLATFORM_MAEMO
62 #include <tny-maemo-conic-device.h>
63 #ifdef MODEST_HAVE_HILDON0_WIDGETS
64 #include <hildon-widgets/hildon-note.h>
65 #include <hildon-widgets/hildon-banner.h>
67 #include <hildon/hildon-note.h>
68 #include <hildon/hildon-banner.h>
72 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
74 /* 'private'/'protected' functions */
75 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
76 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
77 static void modest_tny_account_store_finalize (GObject *obj);
79 /* implementations for tny-account-store-iface */
80 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
81 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
84 static void get_server_accounts (TnyAccountStore *self,
90 ACCOUNT_UPDATE_SIGNAL,
91 PASSWORD_REQUESTED_SIGNAL,
95 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
96 struct _ModestTnyAccountStorePrivate {
98 GHashTable *password_hash;
99 ModestAccountMgr *account_mgr;
100 TnySessionCamel *session;
103 /* We cache the lists of accounts here.
104 * They are created in our get_accounts_func() implementation. */
105 GSList *store_accounts;
106 GSList *transport_accounts;
108 /* This is also contained in store_accounts,
109 * but we cached it temporarily separately,
110 * because we create this while creating the transport accounts,
111 * but return it when requesting the store accounts:
113 GSList *store_accounts_outboxes;
116 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
117 MODEST_TYPE_TNY_ACCOUNT_STORE, \
118 ModestTnyAccountStorePrivate))
121 static GObjectClass *parent_class = NULL;
123 static guint signals[LAST_SIGNAL] = {0};
126 modest_tny_account_store_get_type (void)
128 static GType my_type = 0;
131 static const GTypeInfo my_info = {
132 sizeof(ModestTnyAccountStoreClass),
133 NULL, /* base init */
134 NULL, /* base finalize */
135 (GClassInitFunc) modest_tny_account_store_class_init,
136 NULL, /* class finalize */
137 NULL, /* class data */
138 sizeof(ModestTnyAccountStore),
140 (GInstanceInitFunc) modest_tny_account_store_instance_init,
144 static const GInterfaceInfo iface_info = {
145 (GInterfaceInitFunc) modest_tny_account_store_init,
146 NULL, /* interface_finalize */
147 NULL /* interface_data */
150 my_type = g_type_register_static (G_TYPE_OBJECT,
151 "ModestTnyAccountStore",
153 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
160 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
162 GObjectClass *gobject_class;
163 gobject_class = (GObjectClass*) klass;
165 parent_class = g_type_class_peek_parent (klass);
166 gobject_class->finalize = modest_tny_account_store_finalize;
168 g_type_class_add_private (gobject_class,
169 sizeof(ModestTnyAccountStorePrivate));
171 signals[ACCOUNT_UPDATE_SIGNAL] =
172 g_signal_new ("account_update",
173 G_TYPE_FROM_CLASS (gobject_class),
175 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
177 g_cclosure_marshal_VOID__STRING,
178 G_TYPE_NONE, 1, G_TYPE_STRING);
180 signals[PASSWORD_REQUESTED_SIGNAL] =
181 g_signal_new ("password_requested",
182 G_TYPE_FROM_CLASS (gobject_class),
184 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
186 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
187 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
194 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
195 GnomeVFSVolume *volume, gpointer user_data);
198 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
199 GnomeVFSVolume *volume, gpointer user_data);
202 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
204 ModestTnyAccountStorePrivate *priv =
205 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
207 priv->cache_dir = NULL;
208 priv->account_mgr = NULL;
209 priv->session = NULL;
212 /* An in-memory store of passwords,
213 * for passwords that are not remembered in the configuration,
214 * so they need to be asked for from the user once in each session:
216 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
219 /* Respond to volume mounts and unmounts, such
220 * as the insertion/removal of the memory card: */
221 GnomeVFSVolumeMonitor* monitor =
222 gnome_vfs_get_volume_monitor();
223 g_signal_connect (G_OBJECT(monitor), "volume-mounted",
224 G_CALLBACK(on_vfs_volume_mounted),
226 g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
227 G_CALLBACK(on_vfs_volume_unmounted),
232 account_list_free (GSList *accounts)
234 GSList *cursor = accounts;
237 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
238 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
239 modest_runtime_verify_object_last_ref(cursor->data,id);
241 g_object_unref (G_OBJECT(cursor->data));
242 cursor = cursor->next;
244 g_slist_free (accounts);
249 /* disconnect the list of TnyAccounts */
251 account_list_disconnect (GSList *accounts)
253 GSList *cursor = accounts;
255 g_printerr ("modest: DEBUG disconnecting all accounts\n");
257 if (TNY_IS_CAMEL_ACCOUNT(cursor->data)) /* check twice... */
258 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(cursor->data), FALSE, NULL);
259 cursor = g_slist_next (cursor);
266 recreate_all_accounts (ModestTnyAccountStore *self)
268 ModestTnyAccountStorePrivate *priv =
269 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
271 if (priv->store_accounts_outboxes) {
272 account_list_free (priv->store_accounts_outboxes);
273 priv->store_accounts_outboxes = NULL;
276 if (priv->store_accounts) {
277 account_list_free (priv->store_accounts);
278 priv->store_accounts = NULL;
279 get_server_accounts (TNY_ACCOUNT_STORE(self),
280 NULL, TNY_ACCOUNT_TYPE_STORE);
283 if (priv->transport_accounts) {
284 account_list_free (priv->transport_accounts);
285 priv->transport_accounts = NULL;
286 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
287 TNY_ACCOUNT_TYPE_TRANSPORT);
292 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
293 GnomeVFSVolume *volume, gpointer user_data)
295 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
297 /* Check whether this was the external MMC1 card: */
298 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
299 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
300 printf ("DEBUG: %s: MMC1 card mounted.\n", __FUNCTION__);
302 /* TODO: Just add an account and emit (and respond to)
303 * TnyAccountStore::accountinserted signal?
305 recreate_all_accounts (self);
307 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
315 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
316 GnomeVFSVolume *volume, gpointer user_data)
318 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
320 /* Check whether this was the external MMC1 card: */
321 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
322 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
323 printf ("DEBUG: %s: MMC1 card unmounted.\n", __FUNCTION__);
325 /* TODO: Just add an account and emit (and respond to)
326 * TnyAccountStore::accountinserted signal?
328 recreate_all_accounts (self);
330 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
338 on_account_removed (ModestAccountMgr *acc_mgr,
339 const gchar *account,
340 gboolean server_account,
343 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
344 TnyAccount *store_account;
346 /* Clear the account cache */
347 store_account = modest_tny_account_store_get_tny_account_by (self,
348 MODEST_TNY_ACCOUNT_STORE_QUERY_NAME,
351 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
353 g_signal_emit (G_OBJECT (self),
354 tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNT_REMOVED],
357 g_object_unref (store_account);
359 g_printerr ("modest: cannot find server account for %s", account);
361 /* FIXME: make this more finegrained; changes do not
362 * really affect _all_ accounts, and some do not
363 * affect tny accounts at all (such as 'last_update')
365 recreate_all_accounts (self);
367 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
372 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
373 const GSList *keys, gboolean server_account, gpointer user_data)
376 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
378 /* Ignore the change if it's a change in the last_updated value */
379 if (g_slist_length ((GSList *)keys) == 1 &&
380 g_str_has_suffix ((const gchar *) keys->data, MODEST_ACCOUNT_LAST_UPDATED))
383 /* FIXME: make this more finegrained; changes do not really affect _all_
386 recreate_all_accounts (self);
388 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
393 static ModestTnyAccountStore*
394 get_account_store_for_account (TnyAccount *account)
396 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
400 /* This callback will be called by Tinymail when it needs the password
401 * from the user, for instance if the password was not remembered.
402 * It also calls forget_password() before calling this,
403 * so that we clear wrong passwords out of our account settings.
404 * Note that TnyAccount here will be the server account. */
406 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
408 /* Initialize the output parameter: */
410 g_return_val_if_fail (account, NULL);
413 const TnyAccountStore *account_store;
414 ModestTnyAccountStore *self;
415 ModestTnyAccountStorePrivate *priv;
416 gchar *username = NULL;
419 gboolean already_asked;
424 key = tny_account_get_id (account);
425 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
427 if (!key || !account_store) {
428 g_warning ("BUG: could not retrieve account_store for account %s",
429 key ? key : "<NULL>");
436 self = MODEST_TNY_ACCOUNT_STORE (account_store);
437 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
439 /* This hash map stores passwords, including passwords that are not stored in gconf. */
440 /* is it in the hash? if it's already there, it must be wrong... */
441 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
442 * type-punned ptrs...*/
443 already_asked = priv->password_hash &&
444 g_hash_table_lookup_extended (priv->password_hash,
447 (gpointer*)&pwd_ptr);
449 /* if the password is not already there, try ModestConf */
450 if (!already_asked) {
451 pwd = modest_account_mgr_get_string (priv->account_mgr,
452 key, MODEST_ACCOUNT_PASSWORD, TRUE);
453 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
456 /* if it was already asked, it must have been wrong, so ask again */
457 /* TODO: However, when we supply a wrong password to tinymail,
458 * it seems to (at least sometimes) call our alert_func() instead of
459 * asking for the password again.
461 if (already_asked || !pwd || strlen(pwd) == 0) {
462 /* we don't have it yet. Get the password from the user */
463 const gchar* account_id = tny_account_get_id (account);
464 gboolean remember = FALSE;
467 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
468 account_id, /* server_account_name */
469 &username, &pwd, cancel, &remember);
472 /* The password will be returned as the result,
473 * but we need to tell tinymail about the username too: */
474 tny_account_set_user (account, username);
477 printf ("%s: Storing username=%s, password=%s\n",
478 __FUNCTION__, username, pwd);
479 modest_account_mgr_set_string (priv->account_mgr,key,
480 MODEST_ACCOUNT_USERNAME,
482 modest_account_mgr_set_string (priv->account_mgr,key,
483 MODEST_ACCOUNT_PASSWORD,
486 /* We need to dup the string even knowing that
487 it's already a dup of the contents of an
488 entry, because it if it's wrong, then camel
490 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
492 g_hash_table_remove (priv->password_hash, key);
503 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
508 /* tinymail calls this if the connection failed due to an incorrect password.
509 * And it seems to call this for any general connection failure. */
511 forget_password (TnyAccount *account)
513 printf ("DEBUG: %s\n", __FUNCTION__);
514 ModestTnyAccountStore *self;
515 ModestTnyAccountStorePrivate *priv;
516 const TnyAccountStore *account_store;
520 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
521 self = MODEST_TNY_ACCOUNT_STORE (account_store);
522 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
523 key = tny_account_get_id (account);
525 /* Do not remove the key, this will allow us to detect that we
526 have already asked for it at least once */
527 pwd = g_hash_table_lookup (priv->password_hash, key);
529 memset (pwd, 0, strlen (pwd));
530 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
533 /* Remove from configuration system */
534 modest_account_mgr_unset (priv->account_mgr,
535 key, MODEST_ACCOUNT_PASSWORD, TRUE);
540 modest_tny_account_store_finalize (GObject *obj)
542 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
543 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
545 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
547 g_free (priv->cache_dir);
548 priv->cache_dir = NULL;
550 if (priv->password_hash) {
551 g_hash_table_destroy (priv->password_hash);
552 priv->password_hash = NULL;
555 if (priv->account_mgr) {
556 g_object_unref (G_OBJECT(priv->account_mgr));
557 priv->account_mgr = NULL;
561 g_object_unref (G_OBJECT(priv->device));
565 /* disconnect all accounts when we are destroyed */
566 account_list_disconnect (priv->store_accounts);
567 account_list_disconnect (priv->transport_accounts);
569 /* this includes the local folder */
570 account_list_free (priv->store_accounts);
571 priv->store_accounts = NULL;
573 account_list_free (priv->transport_accounts);
574 priv->transport_accounts = NULL;
577 camel_object_unref (CAMEL_OBJECT(priv->session));
578 priv->session = NULL;
581 G_OBJECT_CLASS(parent_class)->finalize (obj);
585 ModestTnyAccountStore*
586 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
589 ModestTnyAccountStorePrivate *priv;
592 g_return_val_if_fail (account_mgr, NULL);
593 g_return_val_if_fail (device, NULL);
595 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
596 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
598 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
599 priv->device = g_object_ref (device);
601 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
603 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
604 /* FIXME: unref this in the end? */
605 tny_session_camel_set_async_connecting (priv->session, TRUE);
607 /* force a cache fill... ugly */
608 /* list = TNY_LIST(tny_simple_list_new()); */
609 /* tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list, */
610 /* TNY_ACCOUNT_STORE_BOTH); */
611 /* g_object_unref(list); */
613 /* Connect signals */
614 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
615 G_CALLBACK (on_account_changed), obj);
616 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
617 G_CALLBACK (on_account_removed), obj);
619 return MODEST_TNY_ACCOUNT_STORE(obj);
622 /** Fill the TnyList from the appropriate cached GSList of accounts. */
624 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
626 ModestTnyAccountStorePrivate *priv;
627 GSList *accounts, *cursor;
629 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
630 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
635 GObject *object = G_OBJECT(cursor->data);
636 tny_list_prepend (list, object);
639 cursor = cursor->next;
644 create_per_account_local_outbox_folders (TnyAccountStore *self)
646 g_return_if_fail (self);
648 ModestTnyAccountStorePrivate *priv =
649 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
651 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
653 GSList *accounts = NULL;
655 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
656 TRUE /* including disabled accounts */);
659 for (iter = account_names; iter; iter = g_slist_next (iter)) {
661 const gchar* account_name = (const gchar*)iter->data;
663 /* Create a per-account local outbox folder (a _store_ account)
664 * for each _transport_ account: */
665 TnyAccount *tny_account_outbox =
666 modest_tny_account_new_for_per_account_local_outbox_folder (
667 priv->account_mgr, account_name, priv->session);
669 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
672 modest_account_mgr_free_account_names (account_names);
673 account_names = NULL;
675 priv->store_accounts_outboxes = accounts;
678 /* This function fills the TnyList, and also stores a GSList of the accounts,
679 * for caching purposes. It creates the TnyAccount objects if necessary.
680 * The @list parameter may be NULL, if you just want to fill the cache.
683 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
685 g_return_if_fail (self);
687 ModestTnyAccountStorePrivate *priv =
688 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
690 /* Do nothing if the accounts are already cached: */
691 if (type == TNY_ACCOUNT_TYPE_STORE) {
692 if (priv->store_accounts)
694 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
695 if (priv->transport_accounts)
699 GSList *account_names = NULL, *cursor = NULL;
700 GSList *accounts = NULL;
702 /* These are account names, not server_account names */
703 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
705 for (cursor = account_names; cursor; cursor = cursor->next) {
707 gchar *account_name = (gchar*)cursor->data;
709 /* we get the server_accounts for enabled accounts */
710 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
712 /* Add the account: */
713 TnyAccount *tny_account =
714 modest_tny_account_new_from_account (priv->account_mgr,
720 g_object_set_data (G_OBJECT(tny_account), "account_store",
723 tny_list_prepend (list, G_OBJECT(tny_account));
725 accounts = g_slist_append (accounts, tny_account); /* cache it */
727 g_printerr ("modest: failed to create account for %s\n",
732 if (type == TNY_ACCOUNT_TYPE_STORE) {
733 /* Also add the Memory card account if it is mounted: */
734 gboolean mmc_is_mounted = FALSE;
735 GnomeVFSVolumeMonitor* monitor =
736 gnome_vfs_get_volume_monitor();
737 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
738 GList *iter = list_volumes;
740 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
742 if (!mmc_is_mounted) {
743 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
744 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
745 mmc_is_mounted = TRUE;
750 gnome_vfs_volume_unref(volume);
753 iter = g_list_next (iter);
755 g_list_free (list_volumes);
757 if (mmc_is_mounted) {
758 TnyAccount *tny_account =
759 modest_tny_account_new_for_local_folders (priv->account_mgr,
760 priv->session, MODEST_MCC1_VOLUMEPATH);
762 tny_list_prepend (list, G_OBJECT(tny_account));
763 accounts = g_slist_append (accounts, tny_account); /* cache it */
767 /* And add the connection-specific transport accounts, if any.
768 * Note that these server account instances might never be used
769 * if their connections are never active: */
770 /* Look at each modest account: */
771 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
772 GSList *iter_account_names = account_names;
773 while (iter_account_names) {
774 const gchar* account_name = (const gchar*)(iter_account_names->data);
775 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
777 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
778 MODEST_CONF_VALUE_STRING, FALSE);
780 /* Look at each connection-specific transport account for the
782 GSList *iter = list_specifics;
784 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
785 iter = g_slist_next (iter);
787 const gchar* transport_account_name = (const gchar*)(iter->data);
788 if (transport_account_name) {
789 TnyAccount * tny_account = NULL;
790 /* Add the account: */
791 tny_account = modest_tny_account_new_from_server_account_name (
792 priv->account_mgr, transport_account_name);
794 g_object_set_data (G_OBJECT(tny_account), "account_store",
797 tny_list_prepend (list, G_OBJECT(tny_account));
799 accounts = g_slist_append (accounts, tny_account); /* cache it */
801 g_printerr ("modest: failed to create smtp-specific account for %s\n",
802 transport_account_name);
806 iter = g_slist_next (iter);
809 iter_account_names = g_slist_next (iter_account_names);
813 /* free the account_names */
814 modest_account_mgr_free_account_names (account_names);
815 account_names = NULL;
817 /* We also create a per-account local outbox folder (a _store_ account)
818 * for each _transport_ account. */
819 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
820 /* Now would be a good time to create the per-account local outbox folder
821 * _store_ accounts corresponding to each transport account: */
822 if (!priv->store_accounts_outboxes) {
823 create_per_account_local_outbox_folders (self);
827 /* But we only return the per-account local outbox folder when
828 * _store_ accounts are requested. */
829 if (type == TNY_ACCOUNT_TYPE_STORE) {
830 /* Create them if necessary,
831 * (which also requires creating the transport accounts,
833 if (!priv->store_accounts_outboxes) {
834 create_per_account_local_outbox_folders (self);
837 /* Also add the local folder pseudo-account: */
838 TnyAccount *tny_account =
839 modest_tny_account_new_for_local_folders (priv->account_mgr,
840 priv->session, NULL);
842 /* Add them to the TnyList: */
843 if (priv->store_accounts_outboxes) {
845 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
846 TnyAccount *outbox_account = (TnyAccount*)iter->data;
847 if (list && outbox_account)
848 tny_list_prepend (list, G_OBJECT(outbox_account));
850 g_object_ref (outbox_account);
851 accounts = g_slist_append (accounts, outbox_account);
855 /* Add a merged folder, merging all the per-account outbox folders: */
856 modest_tny_local_folders_account_add_merged_outbox_folders (
857 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (tny_account), priv->store_accounts_outboxes);
859 if (priv->store_accounts_outboxes) {
860 /* We have finished with this temporary list, so free it: */
861 account_list_free (priv->store_accounts_outboxes);
862 priv->store_accounts_outboxes = NULL;
866 tny_list_prepend (list, G_OBJECT(tny_account));
867 accounts = g_slist_append (accounts, tny_account); /* cache it */
870 if (type == TNY_ACCOUNT_TYPE_STORE) {
871 /* Store the cache: */
872 priv->store_accounts = accounts;
873 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
874 /* Store the cache: */
875 priv->transport_accounts = accounts;
881 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
882 TnyGetAccountsRequestType request_type)
884 ModestTnyAccountStorePrivate *priv;
886 g_return_if_fail (self);
887 g_return_if_fail (TNY_IS_LIST(list));
889 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
891 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
892 modest_tny_account_store_get_accounts (self, list,
893 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
894 modest_tny_account_store_get_accounts (self, list,
895 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
899 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
900 if (!priv->store_accounts)
901 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
903 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
905 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
906 if (!priv->transport_accounts)
907 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
909 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
911 g_return_if_reached (); /* incorrect req type */
916 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
918 ModestTnyAccountStorePrivate *priv;
919 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
921 if (!priv->cache_dir)
922 priv->cache_dir = g_build_filename (g_get_home_dir(),
923 MODEST_DIR, MODEST_CACHE_DIR, NULL);
924 return priv->cache_dir;
929 * callers need to unref
932 modest_tny_account_store_get_device (TnyAccountStore *self)
934 ModestTnyAccountStorePrivate *priv;
936 g_return_val_if_fail (self, NULL);
938 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
941 return g_object_ref (G_OBJECT(priv->device));
948 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
950 return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self),
951 MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
958 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
959 gboolean question, const GError *error)
961 /* TODO: It would be nice to know what account caused this error. */
963 g_return_val_if_fail (error, FALSE);
965 if ((error->domain != TNY_ACCOUNT_ERROR)
966 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
967 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
968 __FUNCTION__, error->domain, error->message);
972 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
975 /* const gchar *prompt = NULL; */
976 gchar *prompt = NULL;
977 switch (error->code) {
978 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
979 /* The tinymail camel implementation just sends us this for almost
980 * everything, so we have to guess at the cause.
981 * It could be a wrong password, or inability to resolve a hostname,
982 * or lack of network, or incorrect authentication method, or something entirely different: */
983 /* TODO: Fix camel to provide specific error codes, and then use the
984 * specific dialog messages from Chapter 12 of the UI spec.
986 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
987 /* This debug output is useful. Please keep it uncommented until
988 * we have fixed the problems in this function: */
989 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
990 __FUNCTION__, error->domain, error->code, error->message);
992 /* TODO: Remove the internal error message for the real release.
993 * This is just so the testers can give us more information: */
994 /* prompt = _("Modest account not yet fully configured."); */
995 prompt = g_strdup_printf(
996 "%s\n (Internal error message, often very misleading):\n%s",
997 _("Incorrect Account Settings"),
1000 /* TODO: If we can ever determine that the problem is a wrong password:
1001 * In this case, the UI spec wants us to show a banner, and then
1002 * open the Account Settings dialog. */
1003 /* Note: Sometimes, the get_password() function seems to be called again
1004 * when a password is wrong, but sometimes this alert_func is called. */
1006 GtkWidget *parent_widget =
1008 modest_window_mgr_get_main_window (
1009 modest_runtime_get_window_mgr ()));
1011 hildon_banner_show_information (
1013 NULL /* icon name */,
1014 _("mcen_ib_username_pw_incorrect") );
1016 /* Show the Account Settings window: */
1017 ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
1018 /* TODO: Get the account somehow. Maybe tinymail should send it with the signal. */
1019 const gchar* modest_account_name =
1020 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
1021 g_assert (modest_account_name);
1022 modest_account_settings_dialog_set_account_name (dialog,
1023 modest_account_name);
1025 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
1026 gtk_dialog_run (GTK_DIALOG (dialog));
1027 gtk_widget_destroy (GTK_WIDGET (dialog));
1032 //TODO: We have started receiving errors of
1033 //domain=TNY_ACCOUNT_ERROR, code=TNY_ACCOUNT_ERROR_TRY_CONNECT, message="Canceled".
1034 //If this is really a result of us cancelling our own operation then
1035 //a) this probably shouldn't be an error, and
1036 //b) should have its own error code.
1039 g_warning ("%s: Unhandled GError code: %d, message=%s",
1040 __FUNCTION__, error->code, error->message);
1048 #ifdef MODEST_PLATFORM_MAEMO
1049 /* The Tinymail documentation says that we should show Yes and No buttons,
1050 * when it is a question.
1051 * Obviously, we need tinymail to use more specific error codes instead,
1052 * so we know what buttons to show. */
1053 GtkWidget *dialog = NULL;
1055 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
1058 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
1063 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1066 case TNY_ALERT_TYPE_INFO:
1067 gtktype = GTK_MESSAGE_INFO;
1069 case TNY_ALERT_TYPE_WARNING:
1070 gtktype = GTK_MESSAGE_WARNING;
1072 case TNY_ALERT_TYPE_ERROR:
1074 gtktype = GTK_MESSAGE_ERROR;
1078 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
1079 gtktype, GTK_BUTTONS_YES_NO, prompt);
1080 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1082 gboolean retval = TRUE;
1083 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1085 retval = (response == GTK_RESPONSE_YES) ||
1086 (response == GTK_RESPONSE_OK);
1089 gtk_widget_destroy (dialog);
1091 /* TODO: Don't free this when we no longer strdup the message for testers. */
1095 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1101 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1103 TnyAccountStoreIface *klass;
1105 g_return_if_fail (g);
1107 klass = (TnyAccountStoreIface *)g;
1109 klass->get_accounts_func =
1110 modest_tny_account_store_get_accounts;
1111 klass->get_cache_dir_func =
1112 modest_tny_account_store_get_cache_dir;
1113 klass->get_device_func =
1114 modest_tny_account_store_get_device;
1116 modest_tny_account_store_alert;
1117 klass->find_account_func =
1118 modest_tny_account_store_find_account_by_url;
1122 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1123 ModestTnyGetPassFunc func)
1125 /* not implemented, we use signals */
1126 g_printerr ("modest: set_get_pass_func not implemented\n");
1130 modest_tny_account_store_get_session (TnyAccountStore *self)
1132 g_return_val_if_fail (self, NULL);
1133 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1139 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self,
1140 ModestTnyAccountStoreQueryType type,
1143 TnyAccount *account = NULL;
1144 ModestTnyAccountStorePrivate *priv;
1146 const gchar *val = NULL;
1150 g_return_val_if_fail (self, NULL);
1151 g_return_val_if_fail (str, NULL);
1153 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1155 /* fill the caches, as that may not have happened yet */
1156 list = TNY_LIST(tny_simple_list_new());
1157 modest_tny_account_store_get_accounts (TNY_ACCOUNT_STORE(self),
1158 list, TNY_ACCOUNT_STORE_BOTH);
1159 g_object_unref (list);
1163 /* Search in store accounts */
1164 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1166 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1167 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1169 case MODEST_TNY_ACCOUNT_STORE_QUERY_NAME:
1170 val = modest_tny_account_get_parent_modest_account_name_for_server_account (TNY_ACCOUNT(cursor->data));
1172 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1173 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1177 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1178 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), str)) {
1179 account = TNY_ACCOUNT (cursor->data);
1182 if (strcmp (val, str) == 0) {
1183 account = TNY_ACCOUNT(cursor->data);
1189 /* if we already found something, no need to search the transport accounts */
1190 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1192 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1193 val = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1195 case MODEST_TNY_ACCOUNT_STORE_QUERY_NAME:
1196 val = tny_account_get_name (TNY_ACCOUNT(cursor->data));
1198 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1199 val = tny_account_get_url_string (TNY_ACCOUNT(cursor->data));
1203 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL &&
1204 tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), val)) {
1205 account = TNY_ACCOUNT (cursor->data);
1208 if (strcmp (val, str) == 0) {
1209 account = TNY_ACCOUNT(cursor->data);
1216 g_object_ref (G_OBJECT(account));
1222 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1223 const gchar *account_name,
1224 TnyAccountType type)
1226 TnyAccount *account = NULL;
1228 ModestTnyAccountStorePrivate *priv;
1230 g_return_val_if_fail (self, NULL);
1231 g_return_val_if_fail (account_name, NULL);
1232 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1235 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1237 /* Special case for the local account */
1238 if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1239 if(type == TNY_ACCOUNT_TYPE_STORE)
1240 id = g_strdup (MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
1242 /* The local folders modest account has no transport server account. */
1246 ModestAccountData *account_data;
1247 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
1248 if (!account_data) {
1249 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__,
1254 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1255 id = g_strdup (account_data->store_account->account_name);
1256 else if (account_data->transport_account)
1257 id = g_strdup (account_data->transport_account->account_name);
1259 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1263 g_printerr ("modest: could not get an id for account %s\n",
1266 account = modest_tny_account_store_get_tny_account_by (self, MODEST_TNY_ACCOUNT_STORE_QUERY_ID, id);
1269 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1270 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1271 account_name, id ? id : "<none>");
1277 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1278 const gchar *account_name)
1280 /* Get the current connection: */
1281 TnyDevice *device = modest_runtime_get_device ();
1283 if (!tny_device_is_online (device))
1286 #ifdef MODEST_PLATFORM_MAEMO
1287 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1288 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1289 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1290 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1294 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1298 const gchar *connection_name = con_ic_iap_get_name (connection);
1299 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1300 if (!connection_name)
1303 /* Get the connection-specific transport acccount, if any: */
1304 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1305 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1306 account_name, connection_name);
1308 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1309 if (!server_account_name) {
1310 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1313 TnyAccount* account = modest_tny_account_store_get_tny_account_by (self,
1314 MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
1315 server_account_name);
1317 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1318 g_free (server_account_name);
1320 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1321 g_object_unref (connection);
1325 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1326 #endif /* MODEST_PLATFORM_MAEMO */
1331 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1332 const gchar *account_name)
1334 /* Get the connection-specific transport acccount, if any: */
1335 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1337 /* If there is no connection-specific transport account (the common case),
1338 * just get the regular transport account: */
1340 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1342 /* The special local folders don't have transport accounts. */
1343 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1346 account = modest_tny_account_store_get_server_account (self, account_name,
1347 TNY_ACCOUNT_TYPE_TRANSPORT);
1354 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1356 /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount
1357 * for anything else. */
1358 return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1363 modest_tny_account_is_memory_card_account (TnyAccount *self)
1368 const gchar* account_id = tny_account_get_id (self);
1372 return (strcmp (account_id, MODEST_MMC_ACCOUNT_ID) == 0);
1376 modest_tny_account_store_get_local_folders_account (TnyAccountStore *self)
1378 TnyAccount *account = NULL;
1379 ModestTnyAccountStorePrivate *priv;
1382 g_return_val_if_fail (self, NULL);
1384 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1386 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1387 TnyAccount *this_account = TNY_ACCOUNT(cursor->data);
1388 if (modest_tny_account_is_virtual_local_folders (this_account)) {
1389 account = this_account;
1395 g_object_ref (G_OBJECT(account));