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 /* FIXME: make this more finegrained; changes do not really affect _all_
258 * accounts, and some do not affect tny accounts at all (such as 'last_update')
260 if (server_account) {
261 if (priv->store_accounts) {
262 account_list_free (priv->store_accounts);
263 priv->store_accounts = NULL;
264 get_server_accounts (TNY_ACCOUNT_STORE(self),
265 NULL, TNY_ACCOUNT_TYPE_STORE);
268 if (priv->transport_accounts) {
269 account_list_free (priv->transport_accounts);
270 priv->transport_accounts = NULL;
271 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
272 TNY_ACCOUNT_TYPE_TRANSPORT);
276 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
281 static ModestTnyAccountStore*
282 get_account_store_for_account (TnyAccount *account)
284 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
288 /* This callback will be called by Tinymail when it needs the password
289 * from the user, for instance if the password was not remembered.
290 * Note that TnyAccount here will be the server account. */
292 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
294 /* Initialize the output parameter: */
299 const TnyAccountStore *account_store;
300 ModestTnyAccountStore *self;
301 ModestTnyAccountStorePrivate *priv;
302 gchar *username = NULL;
305 gboolean already_asked;
307 key = tny_account_get_id (account);
308 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
310 self = MODEST_TNY_ACCOUNT_STORE (account_store);
311 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
313 /* This hash map stores passwords, including passwords that are not stored in gconf. */
314 /* is it in the hash? if it's already there, it must be wrong... */
315 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
316 * type-punned ptrs...*/
317 already_asked = g_hash_table_lookup_extended (priv->password_hash,
320 (gpointer*)&pwd_ptr);
322 /* if the password is not already there, try ModestConf */
323 if (!already_asked) {
324 pwd = modest_account_mgr_get_string (priv->account_mgr,
325 key, MODEST_ACCOUNT_PASSWORD, TRUE);
326 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
329 /* if it was already asked, it must have been wrong, so ask again */
330 if (already_asked || !pwd || strlen(pwd) == 0) {
331 /* we don't have it yet. Get the password from the user */
332 const gchar* account_id = tny_account_get_id (account);
333 gboolean remember = FALSE;
336 /* Note that we ignore the returned username here,
337 * because it is enough that it will be stored in gconf
338 * by the signal handler. */
339 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
340 account_id, /* server_account_name */
341 &username, &pwd, cancel, &remember);
345 modest_account_mgr_set_string (priv->account_mgr,key,
346 MODEST_ACCOUNT_PASSWORD,
348 /* We need to dup the string even knowing that
349 it's already a dup of the contents of an
350 entry, because it if it's wrong, then camel
352 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
354 g_hash_table_remove (priv->password_hash, key);
365 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
370 /* tinymail calls this if the connection failed due to an incorrect password.
371 * And it seems to call this for any general connection failure. */
373 forget_password (TnyAccount *account)
375 printf ("DEBUG: %s\n", __FUNCTION__);
376 ModestTnyAccountStore *self;
377 ModestTnyAccountStorePrivate *priv;
378 const TnyAccountStore *account_store;
382 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
383 self = MODEST_TNY_ACCOUNT_STORE (account_store);
384 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
385 key = tny_account_get_id (account);
387 /* Do not remove the key, this will allow us to detect that we
388 have already asked for it at least once */
389 pwd = g_hash_table_lookup (priv->password_hash, key);
391 memset (pwd, 0, strlen (pwd));
392 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
395 /* Remove from configuration system */
396 modest_account_mgr_unset (priv->account_mgr,
397 key, MODEST_ACCOUNT_PASSWORD, TRUE);
402 modest_tny_account_store_finalize (GObject *obj)
404 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
405 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
407 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
409 g_free (priv->cache_dir);
410 priv->cache_dir = NULL;
412 if (priv->password_hash) {
413 g_hash_table_destroy (priv->password_hash);
414 priv->password_hash = NULL;
417 if (priv->account_mgr) {
418 g_object_unref (G_OBJECT(priv->account_mgr));
419 priv->account_mgr = NULL;
423 g_object_unref (G_OBJECT(priv->device));
427 /* this includes the local folder */
428 account_list_free (priv->store_accounts);
429 priv->store_accounts = NULL;
431 account_list_free (priv->transport_accounts);
432 priv->transport_accounts = NULL;
435 camel_object_unref (CAMEL_OBJECT(priv->session));
436 priv->session = NULL;
439 G_OBJECT_CLASS(parent_class)->finalize (obj);
443 ModestTnyAccountStore*
444 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
447 ModestTnyAccountStorePrivate *priv;
450 g_return_val_if_fail (account_mgr, NULL);
451 g_return_val_if_fail (device, NULL);
453 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
454 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
456 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
457 priv->device = g_object_ref (device);
459 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
461 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
462 /* FIXME: unref this in the end? */
463 tny_session_camel_set_async_connecting (priv->session, TRUE);
465 /* force a cache fill... ugly */
466 list = TNY_LIST(tny_simple_list_new());
467 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
468 TNY_ACCOUNT_STORE_BOTH);
469 g_object_unref(list);
471 /* Connect signals */
472 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
473 G_CALLBACK (on_account_changed), obj);
474 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
475 G_CALLBACK (on_account_removed), obj);
477 return MODEST_TNY_ACCOUNT_STORE(obj);
480 /** Fill the TnyList from the appropriate cached GSList of accounts. */
482 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
484 ModestTnyAccountStorePrivate *priv;
485 GSList *accounts, *cursor;
487 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
488 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
492 tny_list_prepend (list, G_OBJECT(cursor->data));
493 cursor = cursor->next;
498 create_per_account_local_outbox_folders (TnyAccountStore *self)
500 g_return_if_fail (self);
502 ModestTnyAccountStorePrivate *priv =
503 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
505 /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
507 GSList *accounts = NULL;
509 GSList *account_names = modest_account_mgr_account_names (priv->account_mgr,
510 TRUE /* including disabled accounts */);
513 for (iter = account_names; iter; iter = g_slist_next (iter)) {
515 const gchar* account_name = (const gchar*)iter->data;
517 /* Create a per-account local outbox folder (a _store_ account)
518 * for each _transport_ account: */
519 TnyAccount *tny_account_outbox =
520 modest_tny_account_new_for_per_account_local_outbox_folder (
521 priv->account_mgr, account_name, priv->session);
523 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
526 g_slist_free (account_names);
528 priv->store_accounts_outboxes = accounts;
531 /* This function fills the TnyList, and also stores a GSList of the accounts,
532 * for caching purposes. It creates the TnyAccount objects if necessary.
533 * The @list parameter may be NULL, if you just want to fill the cache.
536 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
538 g_return_if_fail (self);
540 /* printf ("DEBUG: %s: list=%p, type=%d\n", __FUNCTION__, list, type); */
542 ModestTnyAccountStorePrivate *priv =
543 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
545 /* Do nothing if the accounts are already cached: */
546 if (type == TNY_ACCOUNT_TYPE_STORE) {
547 if (priv->store_accounts)
549 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
550 if (priv->transport_accounts)
554 GSList *account_names = NULL, *cursor = NULL;
555 GSList *accounts = NULL;
557 /* These are account names, not server_account names */
558 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
560 for (cursor = account_names; cursor; cursor = cursor->next) {
562 gchar *account_name = (gchar*)cursor->data;
564 /* we get the server_accounts for enabled accounts */
565 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
567 /* Add the account: */
568 TnyAccount *tny_account =
569 modest_tny_account_new_from_account (priv->account_mgr,
575 g_object_set_data (G_OBJECT(tny_account), "account_store",
578 tny_list_prepend (list, G_OBJECT(tny_account));
580 accounts = g_slist_append (accounts, tny_account); /* cache it */
582 g_printerr ("modest: failed to create account for %s\n",
587 if (type == TNY_ACCOUNT_TYPE_STORE) {
588 /* Also add the local folder pseudo-account: */
589 TnyAccount *tny_account =
590 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
592 tny_list_prepend (list, G_OBJECT(tny_account));
593 accounts = g_slist_append (accounts, tny_account); /* cache it */
596 /* And add the connection-specific transport accounts, if any.
597 * Note that these server account instances might never be used
598 * if their connections are never active: */
599 /* Look at each modest account: */
600 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
601 GSList *iter_account_names = account_names;
602 while (iter_account_names) {
603 const gchar* account_name = (const gchar*)(iter_account_names->data);
604 GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
606 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
607 MODEST_CONF_VALUE_STRING, FALSE);
609 /* Look at each connection-specific transport account for the
611 GSList *iter = list_specifics;
613 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
614 iter = g_slist_next (iter);
616 const gchar* transport_account_name = (const gchar*)(iter->data);
617 if (transport_account_name) {
618 TnyAccount * tny_account = NULL;
619 /* Add the account: */
620 tny_account = modest_tny_account_new_from_server_account_name (
621 priv->account_mgr, transport_account_name);
623 g_object_set_data (G_OBJECT(tny_account), "account_store",
626 tny_list_prepend (list, G_OBJECT(tny_account));
628 accounts = g_slist_append (accounts, tny_account); /* cache it */
630 g_printerr ("modest: failed to create smtp-specific account for %s\n",
631 transport_account_name);
635 iter = g_slist_next (iter);
638 iter_account_names = g_slist_next (iter_account_names);
642 g_slist_free (account_names);
643 account_names = NULL;
645 /* TODO: Delete the strings in the GSList */
648 /* We also create a per-account local outbox folder (a _store_ account)
649 * for each _transport_ account. */
650 if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
651 /* Now would be a good time to create the per-account local outbox folder
652 * _store_ accounts corresponding to each transport account: */
653 if (!priv->store_accounts_outboxes) {
654 create_per_account_local_outbox_folders (self);
658 /* But we only return the per-account local outbox folder when
659 * _store_ accounts are requested. */
660 if (type == TNY_ACCOUNT_TYPE_STORE) {
661 /* Create them if necessary,
662 * (which also requires creating the transport accounts,
664 if (!priv->store_accounts_outboxes) {
665 create_per_account_local_outbox_folders (self);
668 /* Add them to the TnyList: */
669 if (priv->store_accounts_outboxes) {
671 for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
672 TnyAccount *outbox_account = (TnyAccount*)iter->data;
673 if (list && outbox_account)
674 tny_list_prepend (list, G_OBJECT(outbox_account));
676 accounts = g_slist_append (accounts, outbox_account);
681 if (type == TNY_ACCOUNT_TYPE_STORE) {
682 /* Store the cache: */
683 priv->store_accounts = accounts;
684 } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
685 /* Store the cache: */
686 priv->transport_accounts = accounts;
692 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
693 TnyGetAccountsRequestType request_type)
695 ModestTnyAccountStorePrivate *priv;
697 g_return_if_fail (self);
698 g_return_if_fail (TNY_IS_LIST(list));
700 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
702 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
703 modest_tny_account_store_get_accounts (self, list,
704 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
705 modest_tny_account_store_get_accounts (self, list,
706 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
710 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
711 if (!priv->store_accounts)
712 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
714 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
716 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
717 if (!priv->transport_accounts)
718 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
720 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
722 g_return_if_reached (); /* incorrect req type */
727 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
729 ModestTnyAccountStorePrivate *priv;
730 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
732 if (!priv->cache_dir)
733 priv->cache_dir = g_build_filename (g_get_home_dir(),
734 MODEST_DIR, MODEST_CACHE_DIR, NULL);
735 return priv->cache_dir;
740 * callers need to unref
743 modest_tny_account_store_get_device (TnyAccountStore *self)
745 ModestTnyAccountStorePrivate *priv;
747 g_return_val_if_fail (self, NULL);
749 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
752 return g_object_ref (G_OBJECT(priv->device));
759 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
761 TnyAccount *account = NULL;
762 ModestTnyAccountStorePrivate *priv;
765 g_return_val_if_fail (self, NULL);
766 g_return_val_if_fail (url_string, NULL);
768 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
770 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
771 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
772 account = TNY_ACCOUNT(cursor->data);
778 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
779 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
780 account = TNY_ACCOUNT(cursor->data);
787 g_object_ref (G_OBJECT(account));
795 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
796 gboolean question, const GError *error)
798 g_return_val_if_fail (error, FALSE);
800 if ((error->domain != TNY_ACCOUNT_ERROR)
801 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
802 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
803 __FUNCTION__, error->domain, error->message);
807 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
810 /* const gchar *prompt = NULL; */
811 gchar *prompt = NULL;
812 switch (error->code) {
813 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
814 /* The tinymail camel implementation just sends us this for almost
815 * everything, so we have to guess at the cause.
816 * It could be a wrong password, or inability to resolve a hostname,
817 * or lack of network, or something entirely different: */
818 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
819 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
820 __FUNCTION__, error->domain, error->code, error->message);
822 /* TODO: Remove the internal error message for the real release.
823 * This is just so the testers can give us more information: */
824 /* prompt = _("Modest account not yet fully configured."); */
825 prompt = g_strdup_printf(_("Modest account not yet fully configured. Error=%s"),
829 g_warning ("%s: Unhandled GError code: %d, message=%s",
830 __FUNCTION__, error->code, error->message);
838 #ifdef MODEST_PLATFORM_MAEMO
839 /* The Tinymail documentation says that we should show Yes and No buttons,
840 * when it is a question.
841 * Obviously, we need tinymail to use more specific error codes instead,
842 * so we know what buttons to show. */
843 GtkWidget *dialog = NULL;
845 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
848 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
853 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
856 case TNY_ALERT_TYPE_INFO:
857 gtktype = GTK_MESSAGE_INFO;
859 case TNY_ALERT_TYPE_WARNING:
860 gtktype = GTK_MESSAGE_WARNING;
862 case TNY_ALERT_TYPE_ERROR:
864 gtktype = GTK_MESSAGE_ERROR;
868 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
869 gtktype, GTK_BUTTONS_YES_NO, prompt);
870 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
872 gboolean retval = TRUE;
873 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
875 retval = (response == GTK_RESPONSE_YES) ||
876 (response == GTK_RESPONSE_OK);
879 gtk_widget_destroy (dialog);
881 /* TODO: Don't free this when we no longer strdup the message for testers. */
885 /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
891 modest_tny_account_store_init (gpointer g, gpointer iface_data)
893 TnyAccountStoreIface *klass;
895 g_return_if_fail (g);
897 klass = (TnyAccountStoreIface *)g;
899 klass->get_accounts_func =
900 modest_tny_account_store_get_accounts;
901 klass->get_cache_dir_func =
902 modest_tny_account_store_get_cache_dir;
903 klass->get_device_func =
904 modest_tny_account_store_get_device;
906 modest_tny_account_store_alert;
907 klass->find_account_func =
908 modest_tny_account_store_find_account_by_url;
912 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
913 ModestTnyGetPassFunc func)
915 /* not implemented, we use signals */
916 g_printerr ("modest: set_get_pass_func not implemented\n");
920 modest_tny_account_store_get_session (TnyAccountStore *self)
922 g_return_val_if_fail (self, NULL);
923 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
928 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
930 TnyAccount *account = NULL;
931 ModestTnyAccountStorePrivate *priv;
934 g_return_val_if_fail (self, NULL);
935 g_return_val_if_fail (id, NULL);
937 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
940 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
941 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
942 if (acc_id && strcmp (acc_id, id) == 0) {
943 account = TNY_ACCOUNT(cursor->data);
948 /* if we already found something, no need to search the transport accounts */
949 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
950 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
951 if (acc_id && strcmp (acc_id, id) == 0) {
952 account = TNY_ACCOUNT(cursor->data);
958 g_object_ref (G_OBJECT(account));
964 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
965 const gchar *account_name,
968 TnyAccount *account = NULL;
970 ModestTnyAccountStorePrivate *priv;
972 g_return_val_if_fail (self, NULL);
973 g_return_val_if_fail (account_name, NULL);
974 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
977 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
979 /* Special case for the local account */
980 if (!strcmp (account_name, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) &&
981 type == TNY_ACCOUNT_TYPE_STORE) {
982 id = g_strdup (MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
984 ModestAccountData *account_data;
986 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
988 g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__, account_name);
992 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
993 id = g_strdup (account_data->store_account->account_name);
994 else if (account_data->transport_account)
995 id = g_strdup (account_data->transport_account->account_name);
997 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1001 g_printerr ("modest: could not get an id for account %s\n",
1004 account = modest_tny_account_store_get_tny_account_by_id (self, id);
1007 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1008 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
1009 account_name, id ? id : "<none>");
1015 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1016 const gchar *account_name)
1018 /* Get the current connection: */
1019 TnyDevice *device = modest_runtime_get_device ();
1021 if (!tny_device_is_online (device))
1024 #ifdef MODEST_PLATFORM_MAEMO
1025 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1026 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
1027 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1028 /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1032 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1036 const gchar *connection_name = con_ic_iap_get_name (connection);
1037 /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1038 if (!connection_name)
1041 /* Get the connection-specific transport acccount, if any: */
1042 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1043 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
1044 account_name, connection_name);
1046 /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1047 if (!server_account_name) {
1048 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1051 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
1053 /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1054 g_free (server_account_name);
1056 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1057 g_object_unref (connection);
1061 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1062 #endif /* MODEST_PLATFORM_MAEMO */
1067 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1068 const gchar *account_name)
1070 /* Get the connection-specific transport acccount, if any: */
1071 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1073 /* If there is no connection-specific transport account (the common case),
1074 * just get the regular transport account: */
1076 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1077 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
1078 TNY_ACCOUNT_TYPE_TRANSPORT);
1084 gboolean modest_tny_folder_store_is_virtual_local_folders (TnyFolderStore *self)
1086 /* We should make this more sophisticated if we ever use ModestTnySimpleFolderStore
1087 * for anything else. */
1088 return MODEST_IS_TNY_SIMPLE_FOLDER_STORE (self);