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-account-mgr.h>
50 #include <modest-account-mgr-helpers.h>
51 #include <widgets/modest-window-mgr.h>
52 #include <modest-account-settings-dialog.h>
55 #include "modest-tny-account-store.h"
56 #include "modest-tny-platform-factory.h"
57 #include <tny-gtk-lockable.h>
58 #include <camel/camel.h>
60 #ifdef MODEST_PLATFORM_MAEMO
61 #include <tny-maemo-conic-device.h>
62 #ifdef MODEST_HILDON_VERSION_0
63 #include <hildon-widgets/hildon-note.h>
64 #include <hildon-widgets/hildon-banner.h>
66 #include <hildon/hildon-note.h>
67 #include <hildon/hildon-banner.h>
71 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
73 /* 'private'/'protected' functions */
74 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
75 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
76 static void modest_tny_account_store_finalize (GObject *obj);
78 /* implementations for tny-account-store-iface */
79 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
80 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
84 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type);
89 ACCOUNT_UPDATE_SIGNAL,
90 PASSWORD_REQUESTED_SIGNAL,
94 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
95 struct _ModestTnyAccountStorePrivate {
97 GHashTable *password_hash;
98 ModestAccountMgr *account_mgr;
99 TnySessionCamel *session;
102 /* We cache the lists of accounts here.
103 * They are created in our get_accounts_func() implementation. */
104 GSList *store_accounts;
105 GSList *transport_accounts;
107 /* This is also contained in store_accounts,
108 * but we cached it temporarily separately,
109 * because we create this while creating the transport accounts,
110 * but return it when requesting the store accounts:
112 GSList *store_accounts_outboxes;
115 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
116 MODEST_TYPE_TNY_ACCOUNT_STORE, \
117 ModestTnyAccountStorePrivate))
120 static GObjectClass *parent_class = NULL;
122 static guint signals[LAST_SIGNAL] = {0};
125 modest_tny_account_store_get_type (void)
127 static GType my_type = 0;
130 static const GTypeInfo my_info = {
131 sizeof(ModestTnyAccountStoreClass),
132 NULL, /* base init */
133 NULL, /* base finalize */
134 (GClassInitFunc) modest_tny_account_store_class_init,
135 NULL, /* class finalize */
136 NULL, /* class data */
137 sizeof(ModestTnyAccountStore),
139 (GInstanceInitFunc) modest_tny_account_store_instance_init,
143 static const GInterfaceInfo iface_info = {
144 (GInterfaceInitFunc) modest_tny_account_store_init,
145 NULL, /* interface_finalize */
146 NULL /* interface_data */
149 my_type = g_type_register_static (G_TYPE_OBJECT,
150 "ModestTnyAccountStore",
152 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
159 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
161 GObjectClass *gobject_class;
162 gobject_class = (GObjectClass*) klass;
164 parent_class = g_type_class_peek_parent (klass);
165 gobject_class->finalize = modest_tny_account_store_finalize;
167 g_type_class_add_private (gobject_class,
168 sizeof(ModestTnyAccountStorePrivate));
170 signals[ACCOUNT_UPDATE_SIGNAL] =
171 g_signal_new ("account_update",
172 G_TYPE_FROM_CLASS (gobject_class),
174 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
176 g_cclosure_marshal_VOID__STRING,
177 G_TYPE_NONE, 1, G_TYPE_STRING);
179 signals[PASSWORD_REQUESTED_SIGNAL] =
180 g_signal_new ("password_requested",
181 G_TYPE_FROM_CLASS (gobject_class),
183 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
185 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
186 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
193 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
194 GnomeVFSVolume *volume, gpointer user_data);
197 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
198 GnomeVFSVolume *volume, gpointer user_data);
201 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
203 ModestTnyAccountStorePrivate *priv =
204 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
206 priv->cache_dir = NULL;
207 priv->account_mgr = NULL;
208 priv->session = NULL;
211 /* An in-memory store of passwords,
212 * for passwords that are not remembered in the configuration,
213 * so they need to be asked for from the user once in each session:
215 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
218 /* Respond to volume mounts and unmounts, such
219 * as the insertion/removal of the memory card: */
220 GnomeVFSVolumeMonitor* monitor =
221 gnome_vfs_get_volume_monitor();
222 g_signal_connect (G_OBJECT(monitor), "volume-mounted",
223 G_CALLBACK(on_vfs_volume_mounted),
225 g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
226 G_CALLBACK(on_vfs_volume_unmounted),
231 account_list_free (GSList *accounts)
233 GSList *cursor = accounts;
236 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
237 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
238 modest_runtime_verify_object_last_ref(cursor->data,id);
240 g_object_unref (G_OBJECT(cursor->data));
241 cursor = cursor->next;
243 g_slist_free (accounts);
247 recreate_all_accounts (ModestTnyAccountStore *self)
249 ModestTnyAccountStorePrivate *priv =
250 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
252 if (priv->store_accounts_outboxes) {
253 account_list_free (priv->store_accounts_outboxes);
254 priv->store_accounts_outboxes = NULL;
257 if (priv->store_accounts) {
258 account_list_free (priv->store_accounts);
259 priv->store_accounts = NULL;
260 get_server_accounts (TNY_ACCOUNT_STORE(self),
261 NULL, TNY_ACCOUNT_TYPE_STORE);
264 if (priv->transport_accounts) {
265 account_list_free (priv->transport_accounts);
266 priv->transport_accounts = NULL;
267 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
268 TNY_ACCOUNT_TYPE_TRANSPORT);
273 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor,
274 GnomeVFSVolume *volume, gpointer user_data)
276 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
278 /* Check whether this was the external MMC1 card: */
279 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
280 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
281 printf ("DEBUG: %s: MMC1 card mounted.\n", __FUNCTION__);
283 /* TODO: Just add an account and emit (and respond to)
284 * TnyAccountStore::accountinserted signal?
286 recreate_all_accounts (self);
288 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
296 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
297 GnomeVFSVolume *volume, gpointer user_data)
299 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
301 /* Check whether this was the external MMC1 card: */
302 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
303 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
304 printf ("DEBUG: %s: MMC1 card unmounted.\n", __FUNCTION__);
306 /* TODO: Just add an account and emit (and respond to)
307 * TnyAccountStore::accountinserted signal?
309 recreate_all_accounts (self);
311 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
319 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
322 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
324 /* FIXME: make this more finegrained; changes do not really affect _all_
325 * accounts, and some do not affect tny accounts at all (such as 'last_update')
328 recreate_all_accounts (self);
330 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
335 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
336 const gchar *key, gboolean server_account, gpointer user_data)
339 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
341 /* Ignore the change if it's a change in the last_updated value */
342 if (g_str_has_suffix (key, MODEST_ACCOUNT_LAST_UPDATED))
345 /* FIXME: make this more finegrained; changes do not really affect _all_
346 * accounts, and some do not affect tny accounts at all (such as 'last_update')
349 recreate_all_accounts (self);
351 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
356 static ModestTnyAccountStore*
357 get_account_store_for_account (TnyAccount *account)
359 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
363 /* This callback will be called by Tinymail when it needs the password
364 * from the user, for instance if the password was not remembered.
365 * Note that TnyAccount here will be the server account. */
367 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
369 /* Initialize the output parameter: */
374 const TnyAccountStore *account_store;
375 ModestTnyAccountStore *self;
376 ModestTnyAccountStorePrivate *priv;
377 gchar *username = NULL;
380 gboolean already_asked;
382 key = tny_account_get_id (account);
383 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
385 self = MODEST_TNY_ACCOUNT_STORE (account_store);
386 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
388 /* This hash map stores passwords, including passwords that are not stored in gconf. */
389 /* is it in the hash? if it's already there, it must be wrong... */
390 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
391 * type-punned ptrs...*/
392 already_asked = g_hash_table_lookup_extended (priv->password_hash,
395 (gpointer*)&pwd_ptr);
397 /* if the password is not already there, try ModestConf */
398 if (!already_asked) {
399 pwd = modest_account_mgr_get_string (priv->account_mgr,
400 key, MODEST_ACCOUNT_PASSWORD, TRUE);
401 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
404 /* if it was already asked, it must have been wrong, so ask again */
405 /* TODO: However, when we supply a wrong password to tinymail,
406 * it seems to (at least sometimes) call our alert_func() instead of
407 * asking for the password again.
409 if (already_asked || !pwd || strlen(pwd) == 0) {
410 /* we don't have it yet. Get the password from the user */
411 const gchar* account_id = tny_account_get_id (account);
412 gboolean remember = FALSE;
415 /* Note that we ignore the returned username here,
416 * because it is enough that it will be stored in gconf
417 * by the signal handler. */
418 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
419 account_id, /* server_account_name */
420 &username, &pwd, cancel, &remember);
424 modest_account_mgr_set_string (priv->account_mgr,key,
425 MODEST_ACCOUNT_PASSWORD,
427 /* We need to dup the string even knowing that
428 it's already a dup of the contents of an
429 entry, because it if it's wrong, then camel
431 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
433 g_hash_table_remove (priv->password_hash, key);
444 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
449 /* tinymail calls this if the connection failed due to an incorrect password.
450 * And it seems to call this for any general connection failure. */
452 forget_password (TnyAccount *account)
454 printf ("DEBUG: %s\n", __FUNCTION__);
455 ModestTnyAccountStore *self;
456 ModestTnyAccountStorePrivate *priv;
457 const TnyAccountStore *account_store;
461 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
462 self = MODEST_TNY_ACCOUNT_STORE (account_store);
463 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
464 key = tny_account_get_id (account);
466 /* Do not remove the key, this will allow us to detect that we
467 have already asked for it at least once */
468 pwd = g_hash_table_lookup (priv->password_hash, key);
470 memset (pwd, 0, strlen (pwd));
471 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
474 /* Remove from configuration system */
475 modest_account_mgr_unset (priv->account_mgr,
476 key, MODEST_ACCOUNT_PASSWORD, TRUE);
481 modest_tny_account_store_finalize (GObject *obj)
483 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
484 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
486 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
488 g_free (priv->cache_dir);
489 priv->cache_dir = NULL;
491 if (priv->password_hash) {
492 g_hash_table_destroy (priv->password_hash);
493 priv->password_hash = NULL;
496 if (priv->account_mgr) {
497 g_object_unref (G_OBJECT(priv->account_mgr));
498 priv->account_mgr = NULL;
502 g_object_unref (G_OBJECT(priv->device));
506 /* this includes the local folder */
507 account_list_free (priv->store_accounts);
508 priv->store_accounts = NULL;
510 account_list_free (priv->transport_accounts);
511 priv->transport_accounts = NULL;
514 camel_object_unref (CAMEL_OBJECT(priv->session));
515 priv->session = NULL;
518 G_OBJECT_CLASS(parent_class)->finalize (obj);
522 ModestTnyAccountStore*
523 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
526 ModestTnyAccountStorePrivate *priv;
529 g_return_val_if_fail (account_mgr, NULL);
530 g_return_val_if_fail (device, NULL);
532 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
533 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
535 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
536 priv->device = g_object_ref (device);
538 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
540 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
541 /* FIXME: unref this in the end? */
542 tny_session_camel_set_async_connecting (priv->session, TRUE);
544 /* force a cache fill... ugly */
545 list = TNY_LIST(tny_simple_list_new());
546 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
547 TNY_ACCOUNT_STORE_BOTH);
548 g_object_unref(list);
550 /* Connect signals */
551 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
552 G_CALLBACK (on_account_changed), obj);
553 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
554 G_CALLBACK (on_account_removed), obj);
556 return MODEST_TNY_ACCOUNT_STORE(obj);
559 /** Fill the TnyList from the appropriate cached GSList of accounts. */
561 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
563 ModestTnyAccountStorePrivate *priv;
564 GSList *accounts, *cursor;
566 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
567 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
572 GObject *object = G_OBJECT(cursor->data);
573 tny_list_prepend (list, object);
576 cursor = cursor->next;
581 create_per_account_local_outbox_folders (TnyAccountStore *self)
583 g_return_if_fail (self);
585 ModestTnyAccountStorePrivate *priv =
586 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
588 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
590 GSList *accounts = NULL;
592 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
593 TRUE /* including disabled accounts */);
596 for (iter = account_names; iter; iter = g_slist_next (iter)) {
598 const gchar* account_name = (const gchar*)iter->data;
600 /* Create a per-account local outbox folder (a _store_ account)
601 * for each _transport_ account: */
602 TnyAccount *tny_account_outbox =
603 modest_tny_account_new_for_per_account_local_outbox_folder (
604 priv->account_mgr, account_name, priv->session);
606 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
609 g_slist_free (account_names);
611 priv->store_accounts_outboxes = accounts;
614 /* This function fills the TnyList, and also stores a GSList of the accounts,
615 * for caching purposes. It creates the TnyAccount objects if necessary.
616 * The @list parameter may be NULL, if you just want to fill the cache.
619 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
621 g_return_if_fail (self);
623 /* printf ("DEBUG: %s: list=%p, type=%d\n", __FUNCTION__, list, type); */
625 ModestTnyAccountStorePrivate *priv =
626 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
628 /* Do nothing if the accounts are already cached: */
629 if (type == TNY_ACCOUNT_TYPE_STORE) {
630 if (priv->store_accounts)
632 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
633 if (priv->transport_accounts)
637 GSList *account_names = NULL, *cursor = NULL;
638 GSList *accounts = NULL;
640 /* These are account names, not server_account names */
641 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
643 for (cursor = account_names; cursor; cursor = cursor->next) {
645 gchar *account_name = (gchar*)cursor->data;
647 /* we get the server_accounts for enabled accounts */
648 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
650 /* Add the account: */
651 TnyAccount *tny_account =
652 modest_tny_account_new_from_account (priv->account_mgr,
658 g_object_set_data (G_OBJECT(tny_account), "account_store",
661 tny_list_prepend (list, G_OBJECT(tny_account));
663 accounts = g_slist_append (accounts, tny_account); /* cache it */
665 g_printerr ("modest: failed to create account for %s\n",
670 if (type == TNY_ACCOUNT_TYPE_STORE) {
671 /* Also add the local folder pseudo-account: */
672 TnyAccount *tny_account =
673 modest_tny_account_new_for_local_folders (priv->account_mgr,
674 priv->session, NULL);
676 tny_list_prepend (list, G_OBJECT(tny_account));
677 accounts = g_slist_append (accounts, tny_account); /* cache it */
680 /* Also add the Memory card account if it is mounted: */
681 gboolean mmc_is_mounted = FALSE;
682 GnomeVFSVolumeMonitor* monitor =
683 gnome_vfs_get_volume_monitor();
684 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
685 GList *iter = list_volumes;
687 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
689 if (!mmc_is_mounted) {
690 gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
691 if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
692 mmc_is_mounted = TRUE;
697 gnome_vfs_volume_unref(volume);
700 iter = g_list_next (iter);
702 g_list_free (list_volumes);
704 if (mmc_is_mounted) {
705 TnyAccount *tny_account =
706 modest_tny_account_new_for_local_folders (priv->account_mgr,
707 priv->session, MODEST_MCC1_VOLUMEPATH);
709 tny_list_prepend (list, G_OBJECT(tny_account));
710 accounts = g_slist_append (accounts, tny_account); /* cache it */
714 /* And add the connection-specific transport accounts, if any.
715 * Note that these server account instances might never be used
716 * if their connections are never active: */
717 /* Look at each modest account: */
718 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
719 GSList *iter_account_names = account_names;
720 while (iter_account_names) {
721 const gchar* account_name = (const gchar*)(iter_account_names->data);
722 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
724 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
725 MODEST_CONF_VALUE_STRING, FALSE);
727 /* Look at each connection-specific transport account for the
729 GSList *iter = list_specifics;
731 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
732 iter = g_slist_next (iter);
734 const gchar* transport_account_name = (const gchar*)(iter->data);
735 if (transport_account_name) {
736 TnyAccount * tny_account = NULL;
737 /* Add the account: */
738 tny_account = modest_tny_account_new_from_server_account_name (
739 priv->account_mgr, transport_account_name);
741 g_object_set_data (G_OBJECT(tny_account), "account_store",
744 tny_list_prepend (list, G_OBJECT(tny_account));
746 accounts = g_slist_append (accounts, tny_account); /* cache it */
748 g_printerr ("modest: failed to create smtp-specific account for %s\n",
749 transport_account_name);
753 iter = g_slist_next (iter);
756 iter_account_names = g_slist_next (iter_account_names);
760 g_slist_free (account_names);
761 account_names = NULL;
763 /* TODO: Delete the strings in the GSList */
766 /* We also create a per-account local outbox folder (a _store_ account)
767 * for each _transport_ account. */
768 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
769 /* Now would be a good time to create the per-account local outbox folder
770 * _store_ accounts corresponding to each transport account: */
771 if (!priv->store_accounts_outboxes) {
772 create_per_account_local_outbox_folders (self);
776 /* But we only return the per-account local outbox folder when
777 * _store_ accounts are requested. */
778 if (type == TNY_ACCOUNT_TYPE_STORE) {
779 /* Create them if necessary,
780 * (which also requires creating the transport accounts,
782 if (!priv->store_accounts_outboxes) {
783 create_per_account_local_outbox_folders (self);
786 /* Add them to the TnyList: */
787 if (priv->store_accounts_outboxes) {
789 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
790 TnyAccount *outbox_account = (TnyAccount*)iter->data;
791 if (list && outbox_account)
792 tny_list_prepend (list, G_OBJECT(outbox_account));
794 g_object_ref (outbox_account);
795 accounts = g_slist_append (accounts, outbox_account);
798 account_list_free (priv->store_accounts_outboxes);
799 priv->store_accounts_outboxes = NULL;
803 if (type == TNY_ACCOUNT_TYPE_STORE) {
804 /* Store the cache: */
805 priv->store_accounts = accounts;
806 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
807 /* Store the cache: */
808 priv->transport_accounts = accounts;
814 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
815 TnyGetAccountsRequestType request_type)
817 ModestTnyAccountStorePrivate *priv;
819 g_return_if_fail (self);
820 g_return_if_fail (TNY_IS_LIST(list));
822 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
824 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
825 modest_tny_account_store_get_accounts (self, list,
826 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
827 modest_tny_account_store_get_accounts (self, list,
828 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
832 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
833 if (!priv->store_accounts)
834 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
836 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
838 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
839 if (!priv->transport_accounts)
840 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
842 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
844 g_return_if_reached (); /* incorrect req type */
849 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
851 ModestTnyAccountStorePrivate *priv;
852 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
854 if (!priv->cache_dir)
855 priv->cache_dir = g_build_filename (g_get_home_dir(),
856 MODEST_DIR, MODEST_CACHE_DIR, NULL);
857 return priv->cache_dir;
862 * callers need to unref
865 modest_tny_account_store_get_device (TnyAccountStore *self)
867 ModestTnyAccountStorePrivate *priv;
869 g_return_val_if_fail (self, NULL);
871 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
874 return g_object_ref (G_OBJECT(priv->device));
881 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
883 TnyAccount *account = NULL;
884 ModestTnyAccountStorePrivate *priv;
887 g_return_val_if_fail (self, NULL);
888 g_return_val_if_fail (url_string, NULL);
890 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
892 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
893 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
894 account = TNY_ACCOUNT(cursor->data);
900 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
901 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
902 account = TNY_ACCOUNT(cursor->data);
909 g_object_ref (G_OBJECT(account));
917 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
918 gboolean question, const GError *error)
920 g_return_val_if_fail (error, FALSE);
922 if ((error->domain != TNY_ACCOUNT_ERROR)
923 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
924 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
925 __FUNCTION__, error->domain, error->message);
929 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
932 /* const gchar *prompt = NULL; */
933 gchar *prompt = NULL;
934 switch (error->code) {
935 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
936 /* The tinymail camel implementation just sends us this for almost
937 * everything, so we have to guess at the cause.
938 * It could be a wrong password, or inability to resolve a hostname,
939 * or lack of network, or something entirely different: */
940 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
941 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
942 __FUNCTION__, error->domain, error->code, error->message);
944 /* TODO: Remove the internal error message for the real release.
945 * This is just so the testers can give us more information: */
946 /* prompt = _("Modest account not yet fully configured."); */
947 prompt = g_strdup_printf(_("Modest account not yet fully configured. Error=%s"),
950 /* TODO: If we can ever determine that the problem is a wrong password:
951 * In this case, the UI spec wants us to show a banner, and then
952 * open the Account Settings dialog. */
953 /* Note: Sometimes, the get_password() function seems to be called again
954 * when a password is wrong, but sometimes this alert_func is called. */
956 GtkWidget *parent_widget =
958 modest_window_mgr_get_main_window (
959 modest_runtime_get_window_mgr ()));
961 hildon_banner_show_information (
963 NULL /* icon name */,
964 _("mcen_ib_username_pw_incorrect") );
966 /* Show the Account Settings window: */
967 ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
968 /* TODO: Get the account somehow. Maybe tinymail should send it with the signal. */
969 const gchar* modest_account_name =
970 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
971 g_assert (modest_account_name);
972 modest_account_settings_dialog_set_account_name (dialog,
973 modest_account_name);
975 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
976 gtk_dialog_run (GTK_DIALOG (dialog));
977 gtk_widget_destroy (GTK_WIDGET (dialog));
982 //TODO: We have started receiving errors of
983 //domain=TNY_ACCOUNT_ERROR, code=TNY_ACCOUNT_ERROR_TRY_CONNECT, messagae="Canceled".
984 //If this is really a result of us cancelling our own operation then
985 //a) this probably shouldn't be an error, and
986 //b) should have its own error code.
989 g_warning ("%s: Unhandled GError code: %d, message=%s",
990 __FUNCTION__, error->code, error->message);
998 #ifdef MODEST_PLATFORM_MAEMO
999 /* The Tinymail documentation says that we should show Yes and No buttons,
1000 * when it is a question.
1001 * Obviously, we need tinymail to use more specific error codes instead,
1002 * so we know what buttons to show. */
1003 GtkWidget *dialog = NULL;
1005 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
1008 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
1013 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1016 case TNY_ALERT_TYPE_INFO:
1017 gtktype = GTK_MESSAGE_INFO;
1019 case TNY_ALERT_TYPE_WARNING:
1020 gtktype = GTK_MESSAGE_WARNING;
1022 case TNY_ALERT_TYPE_ERROR:
1024 gtktype = GTK_MESSAGE_ERROR;
1028 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
1029 gtktype, GTK_BUTTONS_YES_NO, prompt);
1030 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1032 gboolean retval = TRUE;
1033 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1035 retval = (response == GTK_RESPONSE_YES) ||
1036 (response == GTK_RESPONSE_OK);
1039 gtk_widget_destroy (dialog);
1041 /* TODO: Don't free this when we no longer strdup the message for testers. */
1045 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1051 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1053 TnyAccountStoreIface *klass;
1055 g_return_if_fail (g);
1057 klass = (TnyAccountStoreIface *)g;
1059 klass->get_accounts_func =
1060 modest_tny_account_store_get_accounts;
1061 klass->get_cache_dir_func =
1062 modest_tny_account_store_get_cache_dir;
1063 klass->get_device_func =
1064 modest_tny_account_store_get_device;
1066 modest_tny_account_store_alert;
1067 klass->find_account_func =
1068 modest_tny_account_store_find_account_by_url;
1072 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1073 ModestTnyGetPassFunc func)
1075 /* not implemented, we use signals */
1076 g_printerr ("modest: set_get_pass_func not implemented\n");
1080 modest_tny_account_store_get_session (TnyAccountStore *self)
1082 g_return_val_if_fail (self, NULL);
1083 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1088 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
1090 TnyAccount *account = NULL;
1091 ModestTnyAccountStorePrivate *priv;
1094 g_return_val_if_fail (self, NULL);
1095 g_return_val_if_fail (id, NULL);
1097 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1100 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1101 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1102 if (acc_id && strcmp (acc_id, id) == 0) {
1103 account = TNY_ACCOUNT(cursor->data);
1108 /* if we already found something, no need to search the transport accounts */
1109 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1110 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1111 if (acc_id && strcmp (acc_id, id) == 0) {
1112 account = TNY_ACCOUNT(cursor->data);
1118 g_object_ref (G_OBJECT(account));
1124 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
1125 const gchar *account_name,
1126 TnyAccountType type)
1128 TnyAccount *account = NULL;
1130 ModestTnyAccountStorePrivate *priv;
1132 g_return_val_if_fail (self, NULL);
1133 g_return_val_if_fail (account_name, NULL);
1134 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1137 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1139 /* Special case for the local account */
1140 if (!strcmp (account_name, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) &&
1141 type == TNY_ACCOUNT_TYPE_STORE) {
1142 id = g_strdup (MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
1144 ModestAccountData *account_data;
1146 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
1147 if (!account_data) {
1148 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__, account_name);
1152 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1153 id = g_strdup (account_data->store_account->account_name);
1154 else if (account_data->transport_account)
1155 id = g_strdup (account_data->transport_account->account_name);
1157 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1161 g_printerr ("modest: could not get an id for account %s\n",
1164 account = modest_tny_account_store_get_tny_account_by_id (self, id);
1167 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1168 type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1169 account_name, id ? id : "<none>");
1175 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1176 const gchar *account_name)
1178 /* Get the current connection: */
1179 TnyDevice *device = modest_runtime_get_device ();
1181 if (!tny_device_is_online (device))
1184 #ifdef MODEST_PLATFORM_MAEMO
1185 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1186 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1187 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1188 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1192 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1196 const gchar *connection_name = con_ic_iap_get_name (connection);
1197 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1198 if (!connection_name)
1201 /* Get the connection-specific transport acccount, if any: */
1202 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1203 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1204 account_name, connection_name);
1206 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1207 if (!server_account_name) {
1208 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1211 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
1213 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1214 g_free (server_account_name);
1216 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1217 g_object_unref (connection);
1221 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1222 #endif /* MODEST_PLATFORM_MAEMO */
1227 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1228 const gchar *account_name)
1230 /* Get the connection-specific transport acccount, if any: */
1231 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1233 /* If there is no connection-specific transport account (the common case),
1234 * just get the regular transport account: */
1236 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1237 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
1238 TNY_ACCOUNT_TYPE_TRANSPORT);
1244 gboolean modest_tny_folder_store_is_virtual_local_folders (TnyFolderStore *self)
1246 /* We should make this more sophisticated if we ever use ModestTnySimpleFolderStore
1247 * for anything else. */
1248 return MODEST_IS_TNY_SIMPLE_FOLDER_STORE (self);