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>
52 #include "modest-tny-account-store.h"
53 #include "modest-tny-platform-factory.h"
54 #include <tny-gtk-lockable.h>
55 #include <camel/camel.h>
57 #ifdef MODEST_PLATFORM_MAEMO
58 #include <tny-maemo-conic-device.h>
59 #ifdef MODEST_HILDON_VERSION_0
60 #include <hildon-widgets/hildon-note.h>
62 #include <hildon/hildon-note.h>
66 /* 'private'/'protected' functions */
67 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
68 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
69 static void modest_tny_account_store_finalize (GObject *obj);
71 /* implementations for tny-account-store-iface */
72 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
73 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
77 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type);
82 ACCOUNT_UPDATE_SIGNAL,
83 PASSWORD_REQUESTED_SIGNAL,
87 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
88 struct _ModestTnyAccountStorePrivate {
90 GHashTable *password_hash;
91 ModestAccountMgr *account_mgr;
92 TnySessionCamel *session;
95 /* We cache the lists of accounts here.
96 * They are created in our get_accounts_func() implementation. */
97 GSList *store_accounts;
98 GSList *transport_accounts;
100 /* This is also contained in store_accounts,
101 * but we cached it temporarily separately,
102 * because we create this while creating the transport accounts,
103 * but return it when requesting the store accounts:
105 GSList *store_accounts_outboxes;
108 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
109 MODEST_TYPE_TNY_ACCOUNT_STORE, \
110 ModestTnyAccountStorePrivate))
113 static GObjectClass *parent_class = NULL;
115 static guint signals[LAST_SIGNAL] = {0};
118 modest_tny_account_store_get_type (void)
120 static GType my_type = 0;
123 static const GTypeInfo my_info = {
124 sizeof(ModestTnyAccountStoreClass),
125 NULL, /* base init */
126 NULL, /* base finalize */
127 (GClassInitFunc) modest_tny_account_store_class_init,
128 NULL, /* class finalize */
129 NULL, /* class data */
130 sizeof(ModestTnyAccountStore),
132 (GInstanceInitFunc) modest_tny_account_store_instance_init,
136 static const GInterfaceInfo iface_info = {
137 (GInterfaceInitFunc) modest_tny_account_store_init,
138 NULL, /* interface_finalize */
139 NULL /* interface_data */
142 my_type = g_type_register_static (G_TYPE_OBJECT,
143 "ModestTnyAccountStore",
145 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
152 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
154 GObjectClass *gobject_class;
155 gobject_class = (GObjectClass*) klass;
157 parent_class = g_type_class_peek_parent (klass);
158 gobject_class->finalize = modest_tny_account_store_finalize;
160 g_type_class_add_private (gobject_class,
161 sizeof(ModestTnyAccountStorePrivate));
163 signals[ACCOUNT_UPDATE_SIGNAL] =
164 g_signal_new ("account_update",
165 G_TYPE_FROM_CLASS (gobject_class),
167 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
169 g_cclosure_marshal_VOID__STRING,
170 G_TYPE_NONE, 1, G_TYPE_STRING);
172 signals[PASSWORD_REQUESTED_SIGNAL] =
173 g_signal_new ("password_requested",
174 G_TYPE_FROM_CLASS (gobject_class),
176 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
178 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
179 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
185 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
187 ModestTnyAccountStorePrivate *priv =
188 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
190 priv->cache_dir = NULL;
191 priv->account_mgr = NULL;
192 priv->session = NULL;
195 /* An in-memory store of passwords,
196 * for passwords that are not remembered in the configuration,
197 * so they need to be asked for from the user once in each session:
199 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
206 account_list_free (GSList *accounts)
208 GSList *cursor = accounts;
211 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
212 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
213 modest_runtime_verify_object_last_ref(cursor->data,id);
215 g_object_unref (G_OBJECT(cursor->data));
216 cursor = cursor->next;
218 g_slist_free (accounts);
223 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
226 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
227 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
229 /* FIXME: make this more finegrained; changes do not really affect _all_
230 * accounts, and some do not affect tny accounts at all (such as 'last_update')
233 account_list_free (priv->store_accounts);
234 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL, TNY_ACCOUNT_TYPE_STORE);
236 account_list_free (priv->transport_accounts);
237 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
238 TNY_ACCOUNT_TYPE_TRANSPORT);
240 /* TODO: Ref these when we add them? */
241 g_slist_free (priv->store_accounts_outboxes);
242 priv->store_accounts_outboxes = NULL;
244 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
250 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
251 const gchar *key, gboolean server_account, gpointer user_data)
254 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
255 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
257 /* Ignore the change if it's a change in the last_updated value */
258 if (g_str_has_suffix (key, MODEST_ACCOUNT_LAST_UPDATED))
261 /* FIXME: make this more finegrained; changes do not really affect _all_
262 * accounts, and some do not affect tny accounts at all (such as 'last_update')
264 if (server_account) {
265 if (priv->store_accounts) {
266 account_list_free (priv->store_accounts);
267 priv->store_accounts = NULL;
268 get_server_accounts (TNY_ACCOUNT_STORE(self),
269 NULL, TNY_ACCOUNT_TYPE_STORE);
272 if (priv->transport_accounts) {
273 account_list_free (priv->transport_accounts);
274 priv->transport_accounts = NULL;
275 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
276 TNY_ACCOUNT_TYPE_TRANSPORT);
280 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
285 static ModestTnyAccountStore*
286 get_account_store_for_account (TnyAccount *account)
288 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
292 /* This callback will be called by Tinymail when it needs the password
293 * from the user, for instance if the password was not remembered.
294 * Note that TnyAccount here will be the server account. */
296 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
298 /* Initialize the output parameter: */
303 const TnyAccountStore *account_store;
304 ModestTnyAccountStore *self;
305 ModestTnyAccountStorePrivate *priv;
306 gchar *username = NULL;
309 gboolean already_asked;
311 key = tny_account_get_id (account);
312 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
314 self = MODEST_TNY_ACCOUNT_STORE (account_store);
315 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
317 /* This hash map stores passwords, including passwords that are not stored in gconf. */
318 /* is it in the hash? if it's already there, it must be wrong... */
319 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
320 * type-punned ptrs...*/
321 already_asked = g_hash_table_lookup_extended (priv->password_hash,
324 (gpointer*)&pwd_ptr);
326 /* if the password is not already there, try ModestConf */
327 if (!already_asked) {
328 pwd = modest_account_mgr_get_string (priv->account_mgr,
329 key, MODEST_ACCOUNT_PASSWORD, TRUE);
330 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
333 /* if it was already asked, it must have been wrong, so ask again */
334 if (already_asked || !pwd || strlen(pwd) == 0) {
335 /* we don't have it yet. Get the password from the user */
336 const gchar* account_id = tny_account_get_id (account);
337 gboolean remember = FALSE;
340 /* Note that we ignore the returned username here,
341 * because it is enough that it will be stored in gconf
342 * by the signal handler. */
343 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
344 account_id, /* server_account_name */
345 &username, &pwd, cancel, &remember);
349 modest_account_mgr_set_string (priv->account_mgr,key,
350 MODEST_ACCOUNT_PASSWORD,
352 /* We need to dup the string even knowing that
353 it's already a dup of the contents of an
354 entry, because it if it's wrong, then camel
356 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
358 g_hash_table_remove (priv->password_hash, key);
369 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
374 /* tinymail calls this if the connection failed due to an incorrect password.
375 * And it seems to call this for any general connection failure. */
377 forget_password (TnyAccount *account)
379 printf ("DEBUG: %s\n", __FUNCTION__);
380 ModestTnyAccountStore *self;
381 ModestTnyAccountStorePrivate *priv;
382 const TnyAccountStore *account_store;
386 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
387 self = MODEST_TNY_ACCOUNT_STORE (account_store);
388 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
389 key = tny_account_get_id (account);
391 /* Do not remove the key, this will allow us to detect that we
392 have already asked for it at least once */
393 pwd = g_hash_table_lookup (priv->password_hash, key);
395 memset (pwd, 0, strlen (pwd));
396 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
399 /* Remove from configuration system */
400 modest_account_mgr_unset (priv->account_mgr,
401 key, MODEST_ACCOUNT_PASSWORD, TRUE);
406 modest_tny_account_store_finalize (GObject *obj)
408 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
409 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
411 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
413 g_free (priv->cache_dir);
414 priv->cache_dir = NULL;
416 if (priv->password_hash) {
417 g_hash_table_destroy (priv->password_hash);
418 priv->password_hash = NULL;
421 if (priv->account_mgr) {
422 g_object_unref (G_OBJECT(priv->account_mgr));
423 priv->account_mgr = NULL;
427 g_object_unref (G_OBJECT(priv->device));
431 /* this includes the local folder */
432 account_list_free (priv->store_accounts);
433 priv->store_accounts = NULL;
435 account_list_free (priv->transport_accounts);
436 priv->transport_accounts = NULL;
439 camel_object_unref (CAMEL_OBJECT(priv->session));
440 priv->session = NULL;
443 G_OBJECT_CLASS(parent_class)->finalize (obj);
447 ModestTnyAccountStore*
448 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
451 ModestTnyAccountStorePrivate *priv;
454 g_return_val_if_fail (account_mgr, NULL);
455 g_return_val_if_fail (device, NULL);
457 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
458 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
460 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
461 priv->device = g_object_ref (device);
463 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
465 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
466 /* FIXME: unref this in the end? */
467 tny_session_camel_set_async_connecting (priv->session, TRUE);
469 /* force a cache fill... ugly */
470 list = TNY_LIST(tny_simple_list_new());
471 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
472 TNY_ACCOUNT_STORE_BOTH);
473 g_object_unref(list);
475 /* Connect signals */
476 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
477 G_CALLBACK (on_account_changed), obj);
478 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
479 G_CALLBACK (on_account_removed), obj);
481 return MODEST_TNY_ACCOUNT_STORE(obj);
484 /** Fill the TnyList from the appropriate cached GSList of accounts. */
486 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
488 ModestTnyAccountStorePrivate *priv;
489 GSList *accounts, *cursor;
491 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
492 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
496 tny_list_prepend (list, G_OBJECT(cursor->data));
497 cursor = cursor->next;
502 create_per_account_local_outbox_folders (TnyAccountStore *self)
504 g_return_if_fail (self);
506 ModestTnyAccountStorePrivate *priv =
507 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
509 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
511 GSList *accounts = NULL;
513 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
514 TRUE /* including disabled accounts */);
517 for (iter = account_names; iter; iter = g_slist_next (iter)) {
519 const gchar* account_name = (const gchar*)iter->data;
521 /* Create a per-account local outbox folder (a _store_ account)
522 * for each _transport_ account: */
523 TnyAccount *tny_account_outbox =
524 modest_tny_account_new_for_per_account_local_outbox_folder (
525 priv->account_mgr, account_name, priv->session);
527 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
530 g_slist_free (account_names);
532 priv->store_accounts_outboxes = accounts;
535 /* This function fills the TnyList, and also stores a GSList of the accounts,
536 * for caching purposes. It creates the TnyAccount objects if necessary.
537 * The @list parameter may be NULL, if you just want to fill the cache.
540 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
542 g_return_if_fail (self);
544 /* printf ("DEBUG: %s: list=%p, type=%d\n", __FUNCTION__, list, type); */
546 ModestTnyAccountStorePrivate *priv =
547 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
549 /* Do nothing if the accounts are already cached: */
550 if (type == TNY_ACCOUNT_TYPE_STORE) {
551 if (priv->store_accounts)
553 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
554 if (priv->transport_accounts)
558 GSList *account_names = NULL, *cursor = NULL;
559 GSList *accounts = NULL;
561 /* These are account names, not server_account names */
562 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
564 for (cursor = account_names; cursor; cursor = cursor->next) {
566 gchar *account_name = (gchar*)cursor->data;
568 /* we get the server_accounts for enabled accounts */
569 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
571 /* Add the account: */
572 TnyAccount *tny_account =
573 modest_tny_account_new_from_account (priv->account_mgr,
579 g_object_set_data (G_OBJECT(tny_account), "account_store",
582 tny_list_prepend (list, G_OBJECT(tny_account));
584 accounts = g_slist_append (accounts, tny_account); /* cache it */
586 g_printerr ("modest: failed to create account for %s\n",
591 if (type == TNY_ACCOUNT_TYPE_STORE) {
592 /* Also add the local folder pseudo-account: */
593 TnyAccount *tny_account =
594 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
596 tny_list_prepend (list, G_OBJECT(tny_account));
597 accounts = g_slist_append (accounts, tny_account); /* cache it */
600 /* And add the connection-specific transport accounts, if any.
601 * Note that these server account instances might never be used
602 * if their connections are never active: */
603 /* Look at each modest account: */
604 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
605 GSList *iter_account_names = account_names;
606 while (iter_account_names) {
607 const gchar* account_name = (const gchar*)(iter_account_names->data);
608 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
610 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
611 MODEST_CONF_VALUE_STRING, FALSE);
613 /* Look at each connection-specific transport account for the
615 GSList *iter = list_specifics;
617 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
618 iter = g_slist_next (iter);
620 const gchar* transport_account_name = (const gchar*)(iter->data);
621 if (transport_account_name) {
622 TnyAccount * tny_account = NULL;
623 /* Add the account: */
624 tny_account = modest_tny_account_new_from_server_account_name (
625 priv->account_mgr, transport_account_name);
627 g_object_set_data (G_OBJECT(tny_account), "account_store",
630 tny_list_prepend (list, G_OBJECT(tny_account));
632 accounts = g_slist_append (accounts, tny_account); /* cache it */
634 g_printerr ("modest: failed to create smtp-specific account for %s\n",
635 transport_account_name);
639 iter = g_slist_next (iter);
642 iter_account_names = g_slist_next (iter_account_names);
646 g_slist_free (account_names);
647 account_names = NULL;
649 /* TODO: Delete the strings in the GSList */
652 /* We also create a per-account local outbox folder (a _store_ account)
653 * for each _transport_ account. */
654 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
655 /* Now would be a good time to create the per-account local outbox folder
656 * _store_ accounts corresponding to each transport account: */
657 if (!priv->store_accounts_outboxes) {
658 create_per_account_local_outbox_folders (self);
662 /* But we only return the per-account local outbox folder when
663 * _store_ accounts are requested. */
664 if (type == TNY_ACCOUNT_TYPE_STORE) {
665 /* Create them if necessary,
666 * (which also requires creating the transport accounts,
668 if (!priv->store_accounts_outboxes) {
669 create_per_account_local_outbox_folders (self);
672 /* Add them to the TnyList: */
673 if (priv->store_accounts_outboxes) {
675 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
676 TnyAccount *outbox_account = (TnyAccount*)iter->data;
677 if (list && outbox_account)
678 tny_list_prepend (list, G_OBJECT(outbox_account));
680 accounts = g_slist_append (accounts, outbox_account);
685 if (type == TNY_ACCOUNT_TYPE_STORE) {
686 /* Store the cache: */
687 priv->store_accounts = accounts;
688 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
689 /* Store the cache: */
690 priv->transport_accounts = accounts;
696 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
697 TnyGetAccountsRequestType request_type)
699 ModestTnyAccountStorePrivate *priv;
701 g_return_if_fail (self);
702 g_return_if_fail (TNY_IS_LIST(list));
704 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
706 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
707 modest_tny_account_store_get_accounts (self, list,
708 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
709 modest_tny_account_store_get_accounts (self, list,
710 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
714 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
715 if (!priv->store_accounts)
716 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
718 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
720 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
721 if (!priv->transport_accounts)
722 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
724 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
726 g_return_if_reached (); /* incorrect req type */
731 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
733 ModestTnyAccountStorePrivate *priv;
734 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
736 if (!priv->cache_dir)
737 priv->cache_dir = g_build_filename (g_get_home_dir(),
738 MODEST_DIR, MODEST_CACHE_DIR, NULL);
739 return priv->cache_dir;
744 * callers need to unref
747 modest_tny_account_store_get_device (TnyAccountStore *self)
749 ModestTnyAccountStorePrivate *priv;
751 g_return_val_if_fail (self, NULL);
753 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
756 return g_object_ref (G_OBJECT(priv->device));
763 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
765 TnyAccount *account = NULL;
766 ModestTnyAccountStorePrivate *priv;
769 g_return_val_if_fail (self, NULL);
770 g_return_val_if_fail (url_string, NULL);
772 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
774 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
775 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
776 account = TNY_ACCOUNT(cursor->data);
782 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
783 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
784 account = TNY_ACCOUNT(cursor->data);
791 g_object_ref (G_OBJECT(account));
799 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
800 gboolean question, const GError *error)
802 g_return_val_if_fail (error, FALSE);
804 if ((error->domain != TNY_ACCOUNT_ERROR)
805 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
806 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
807 __FUNCTION__, error->domain, error->message);
811 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
814 /* const gchar *prompt = NULL; */
815 gchar *prompt = NULL;
816 switch (error->code) {
817 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
818 /* The tinymail camel implementation just sends us this for almost
819 * everything, so we have to guess at the cause.
820 * It could be a wrong password, or inability to resolve a hostname,
821 * or lack of network, or something entirely different: */
822 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
823 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
824 __FUNCTION__, error->domain, error->code, error->message);
826 /* TODO: Remove the internal error message for the real release.
827 * This is just so the testers can give us more information: */
828 /* prompt = _("Modest account not yet fully configured."); */
829 prompt = g_strdup_printf(_("Modest account not yet fully configured. Error=%s"),
833 g_warning ("%s: Unhandled GError code: %d, message=%s",
834 __FUNCTION__, error->code, error->message);
842 #ifdef MODEST_PLATFORM_MAEMO
843 /* The Tinymail documentation says that we should show Yes and No buttons,
844 * when it is a question.
845 * Obviously, we need tinymail to use more specific error codes instead,
846 * so we know what buttons to show. */
847 GtkWidget *dialog = NULL;
849 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
852 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
857 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
860 case TNY_ALERT_TYPE_INFO:
861 gtktype = GTK_MESSAGE_INFO;
863 case TNY_ALERT_TYPE_WARNING:
864 gtktype = GTK_MESSAGE_WARNING;
866 case TNY_ALERT_TYPE_ERROR:
868 gtktype = GTK_MESSAGE_ERROR;
872 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
873 gtktype, GTK_BUTTONS_YES_NO, prompt);
874 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
876 gboolean retval = TRUE;
877 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
879 retval = (response == GTK_RESPONSE_YES) ||
880 (response == GTK_RESPONSE_OK);
883 gtk_widget_destroy (dialog);
885 /* TODO: Don't free this when we no longer strdup the message for testers. */
889 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
895 modest_tny_account_store_init (gpointer g, gpointer iface_data)
897 TnyAccountStoreIface *klass;
899 g_return_if_fail (g);
901 klass = (TnyAccountStoreIface *)g;
903 klass->get_accounts_func =
904 modest_tny_account_store_get_accounts;
905 klass->get_cache_dir_func =
906 modest_tny_account_store_get_cache_dir;
907 klass->get_device_func =
908 modest_tny_account_store_get_device;
910 modest_tny_account_store_alert;
911 klass->find_account_func =
912 modest_tny_account_store_find_account_by_url;
916 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
917 ModestTnyGetPassFunc func)
919 /* not implemented, we use signals */
920 g_printerr ("modest: set_get_pass_func not implemented\n");
924 modest_tny_account_store_get_session (TnyAccountStore *self)
926 g_return_val_if_fail (self, NULL);
927 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
932 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
934 TnyAccount *account = NULL;
935 ModestTnyAccountStorePrivate *priv;
938 g_return_val_if_fail (self, NULL);
939 g_return_val_if_fail (id, NULL);
941 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
944 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
945 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
946 if (acc_id && strcmp (acc_id, id) == 0) {
947 account = TNY_ACCOUNT(cursor->data);
952 /* if we already found something, no need to search the transport accounts */
953 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
954 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
955 if (acc_id && strcmp (acc_id, id) == 0) {
956 account = TNY_ACCOUNT(cursor->data);
962 g_object_ref (G_OBJECT(account));
968 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
969 const gchar *account_name,
972 TnyAccount *account = NULL;
974 ModestTnyAccountStorePrivate *priv;
976 g_return_val_if_fail (self, NULL);
977 g_return_val_if_fail (account_name, NULL);
978 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
981 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
983 /* Special case for the local account */
984 if (!strcmp (account_name, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) &&
985 type == TNY_ACCOUNT_TYPE_STORE) {
986 id = g_strdup (MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
988 ModestAccountData *account_data;
990 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
992 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__, account_name);
996 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
997 id = g_strdup (account_data->store_account->account_name);
998 else if (account_data->transport_account)
999 id = g_strdup (account_data->transport_account->account_name);
1001 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1005 g_printerr ("modest: could not get an id for account %s\n",
1008 account = modest_tny_account_store_get_tny_account_by_id (self, id);
1011 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1012 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
1013 account_name, id ? id : "<none>");
1019 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1020 const gchar *account_name)
1022 /* Get the current connection: */
1023 TnyDevice *device = modest_runtime_get_device ();
1025 if (!tny_device_is_online (device))
1028 #ifdef MODEST_PLATFORM_MAEMO
1029 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1030 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1031 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1032 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1036 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1040 const gchar *connection_name = con_ic_iap_get_name (connection);
1041 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1042 if (!connection_name)
1045 /* Get the connection-specific transport acccount, if any: */
1046 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1047 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1048 account_name, connection_name);
1050 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1051 if (!server_account_name) {
1052 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1055 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
1057 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1058 g_free (server_account_name);
1060 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1061 g_object_unref (connection);
1065 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1066 #endif /* MODEST_PLATFORM_MAEMO */
1071 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1072 const gchar *account_name)
1074 /* Get the connection-specific transport acccount, if any: */
1075 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1077 /* If there is no connection-specific transport account (the common case),
1078 * just get the regular transport account: */
1080 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1081 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
1082 TNY_ACCOUNT_TYPE_TRANSPORT);
1088 gboolean modest_tny_folder_store_is_virtual_local_folders (TnyFolderStore *self)
1090 /* We should make this more sophisticated if we ever use ModestTnySimpleFolderStore
1091 * for anything else. */
1092 return MODEST_IS_TNY_SIMPLE_FOLDER_STORE (self);