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 them here */
96 GSList *store_accounts;
97 GSList *transport_accounts;
100 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
101 MODEST_TYPE_TNY_ACCOUNT_STORE, \
102 ModestTnyAccountStorePrivate))
105 static GObjectClass *parent_class = NULL;
107 static guint signals[LAST_SIGNAL] = {0};
110 modest_tny_account_store_get_type (void)
112 static GType my_type = 0;
115 static const GTypeInfo my_info = {
116 sizeof(ModestTnyAccountStoreClass),
117 NULL, /* base init */
118 NULL, /* base finalize */
119 (GClassInitFunc) modest_tny_account_store_class_init,
120 NULL, /* class finalize */
121 NULL, /* class data */
122 sizeof(ModestTnyAccountStore),
124 (GInstanceInitFunc) modest_tny_account_store_instance_init,
128 static const GInterfaceInfo iface_info = {
129 (GInterfaceInitFunc) modest_tny_account_store_init,
130 NULL, /* interface_finalize */
131 NULL /* interface_data */
134 my_type = g_type_register_static (G_TYPE_OBJECT,
135 "ModestTnyAccountStore",
137 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
144 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
146 GObjectClass *gobject_class;
147 gobject_class = (GObjectClass*) klass;
149 parent_class = g_type_class_peek_parent (klass);
150 gobject_class->finalize = modest_tny_account_store_finalize;
152 g_type_class_add_private (gobject_class,
153 sizeof(ModestTnyAccountStorePrivate));
155 signals[ACCOUNT_UPDATE_SIGNAL] =
156 g_signal_new ("account_update",
157 G_TYPE_FROM_CLASS (gobject_class),
159 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
161 g_cclosure_marshal_VOID__STRING,
162 G_TYPE_NONE, 1, G_TYPE_STRING);
164 signals[PASSWORD_REQUESTED_SIGNAL] =
165 g_signal_new ("password_requested",
166 G_TYPE_FROM_CLASS (gobject_class),
168 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
170 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
171 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
177 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
179 ModestTnyAccountStorePrivate *priv =
180 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
182 priv->cache_dir = NULL;
183 priv->account_mgr = NULL;
184 priv->session = NULL;
187 /* An in-memory store of passwords,
188 * for passwords that are not remembered in the configuration,
189 * so they need to be asked for from the user once in each session:
191 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
198 account_list_free (GSList *accounts)
200 GSList *cursor = accounts;
203 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
204 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
205 modest_runtime_verify_object_last_ref(cursor->data,id);
207 g_object_unref (G_OBJECT(cursor->data));
208 cursor = cursor->next;
210 g_slist_free (accounts);
215 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
218 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
219 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
221 /* FIXME: make this more finegrained; changes do not really affect _all_
222 * accounts, and some do not affect tny accounts at all (such as 'last_update')
225 account_list_free (priv->store_accounts);
226 priv->store_accounts = get_server_accounts (TNY_ACCOUNT_STORE(self), NULL, TNY_ACCOUNT_TYPE_STORE);
228 account_list_free (priv->transport_accounts);
229 priv->transport_accounts = get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
230 TNY_ACCOUNT_TYPE_TRANSPORT);
232 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
238 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
239 const gchar *key, gboolean server_account, gpointer user_data)
242 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
243 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
245 /* FIXME: make this more finegrained; changes do not really affect _all_
246 * accounts, and some do not affect tny accounts at all (such as 'last_update')
248 if (server_account) {
249 if (priv->store_accounts) {
250 account_list_free (priv->store_accounts);
251 priv->store_accounts =
252 get_server_accounts (TNY_ACCOUNT_STORE(self),
253 NULL, TNY_ACCOUNT_TYPE_STORE);
256 if (priv->transport_accounts) {
257 account_list_free (priv->transport_accounts);
258 priv->transport_accounts =
259 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
260 TNY_ACCOUNT_TYPE_TRANSPORT);
264 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
269 static ModestTnyAccountStore*
270 get_account_store_for_account (TnyAccount *account)
272 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
276 /* This callback will be called by Tinymail when it needs the password
277 * from the user, for instance if the password was not remembered.
278 * Note that TnyAccount here will be the server account. */
280 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
282 /* Initialize the output parameter: */
287 const TnyAccountStore *account_store;
288 ModestTnyAccountStore *self;
289 ModestTnyAccountStorePrivate *priv;
290 gchar *username = NULL;
293 gboolean already_asked;
295 key = tny_account_get_id (account);
296 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
298 self = MODEST_TNY_ACCOUNT_STORE (account_store);
299 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
301 /* This hash map stores passwords, including passwords that are not stored in gconf. */
302 /* is it in the hash? if it's already there, it must be wrong... */
303 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
304 * type-punned ptrs...*/
305 already_asked = g_hash_table_lookup_extended (priv->password_hash,
308 (gpointer*)&pwd_ptr);
310 /* if the password is not already there, try ModestConf */
311 if (!already_asked) {
312 pwd = modest_account_mgr_get_string (priv->account_mgr,
313 key, MODEST_ACCOUNT_PASSWORD, TRUE);
314 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
317 /* if it was already asked, it must have been wrong, so ask again */
318 if (already_asked || !pwd || strlen(pwd) == 0) {
319 /* we don't have it yet. Get the password from the user */
320 const gchar* account_id = tny_account_get_id (account);
321 gboolean remember = FALSE;
324 /* Note that we ignore the returned username here,
325 * because it is enough that it will be stored in gconf
326 * by the signal handler. */
327 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
328 account_id, /* server_account_name */
329 &username, &pwd, cancel, &remember);
333 modest_account_mgr_set_string (priv->account_mgr,key,
334 MODEST_ACCOUNT_PASSWORD,
336 /* We need to dup the string even knowing that
337 it's already a dup of the contents of an
338 entry, because it if it's wrong, then camel
340 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
342 g_hash_table_remove (priv->password_hash, key);
353 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
358 /* tinymail calls this if the connection failed due to an incorrect password.
359 * And it seems to call this for any general connection failure. */
361 forget_password (TnyAccount *account)
363 printf ("DEBUG: %s\n", __FUNCTION__);
364 ModestTnyAccountStore *self;
365 ModestTnyAccountStorePrivate *priv;
366 const TnyAccountStore *account_store;
370 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
371 self = MODEST_TNY_ACCOUNT_STORE (account_store);
372 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
373 key = tny_account_get_id (account);
375 /* Do not remove the key, this will allow us to detect that we
376 have already asked for it at least once */
377 pwd = g_hash_table_lookup (priv->password_hash, key);
379 memset (pwd, 0, strlen (pwd));
380 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
383 /* Remove from configuration system */
384 modest_account_mgr_unset (priv->account_mgr,
385 key, MODEST_ACCOUNT_PASSWORD, TRUE);
390 modest_tny_account_store_finalize (GObject *obj)
392 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
393 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
395 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
397 g_free (priv->cache_dir);
398 priv->cache_dir = NULL;
400 if (priv->password_hash) {
401 g_hash_table_destroy (priv->password_hash);
402 priv->password_hash = NULL;
405 if (priv->account_mgr) {
406 g_object_unref (G_OBJECT(priv->account_mgr));
407 priv->account_mgr = NULL;
411 g_object_unref (G_OBJECT(priv->device));
415 /* this includes the local folder */
416 account_list_free (priv->store_accounts);
417 priv->store_accounts = NULL;
419 account_list_free (priv->transport_accounts);
420 priv->transport_accounts = NULL;
423 camel_object_unref (CAMEL_OBJECT(priv->session));
424 priv->session = NULL;
427 G_OBJECT_CLASS(parent_class)->finalize (obj);
431 ModestTnyAccountStore*
432 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
435 ModestTnyAccountStorePrivate *priv;
438 g_return_val_if_fail (account_mgr, NULL);
439 g_return_val_if_fail (device, NULL);
441 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
442 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
444 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
445 priv->device = g_object_ref (device);
447 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
449 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
450 /* FIXME: unref this in the end? */
451 tny_session_camel_set_async_connecting (priv->session, TRUE);
453 /* force a cache fill... ugly */
454 list = TNY_LIST(tny_simple_list_new());
455 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
456 TNY_ACCOUNT_STORE_BOTH);
457 g_object_unref(list);
459 /* Connect signals */
460 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
461 G_CALLBACK (on_account_changed), obj);
462 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
463 G_CALLBACK (on_account_removed), obj);
465 return MODEST_TNY_ACCOUNT_STORE(obj);
470 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
472 ModestTnyAccountStorePrivate *priv;
473 GSList *accounts, *cursor;
475 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
476 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
480 tny_list_prepend (list, G_OBJECT(cursor->data));
481 cursor = cursor->next;
487 /* this function fills the TnyList, and also returns a GSList of the accounts,
488 * for caching purposes
491 get_server_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
493 ModestTnyAccountStorePrivate *priv = NULL;
494 GSList *account_names = NULL, *cursor = NULL;
495 GSList *accounts = NULL;
497 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
499 /* these account names, not server_account names */
500 account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
502 for (cursor = account_names; cursor; cursor = cursor->next) {
504 gchar *account_name = (gchar*)cursor->data;
506 /* we get the server_accounts for enabled accounts */
507 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
508 TnyAccount *tny_account =
509 modest_tny_account_new_from_account (priv->account_mgr,
515 g_object_set_data (G_OBJECT(tny_account), "account_store",
518 tny_list_prepend (list, G_OBJECT(tny_account));
520 accounts = g_slist_append (accounts, tny_account); /* cache it */
522 g_printerr ("modest: failed to create account for %s\n",
525 g_free (account_name);
527 g_slist_free (account_names);
529 /* also, add the local folder pseudo-account */
530 if (type == TNY_ACCOUNT_TYPE_STORE) {
531 TnyAccount *tny_account =
532 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
534 tny_list_prepend (list, G_OBJECT(tny_account));
535 accounts = g_slist_append (accounts, tny_account); /* cache it */
542 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
543 TnyGetAccountsRequestType request_type)
545 ModestTnyAccountStorePrivate *priv;
547 g_return_if_fail (self);
548 g_return_if_fail (TNY_IS_LIST(list));
550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
552 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
553 modest_tny_account_store_get_accounts (self, list,
554 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
555 modest_tny_account_store_get_accounts (self, list,
556 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
560 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
561 if (!priv->store_accounts)
562 priv->store_accounts =
563 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
565 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
567 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
568 if (!priv->transport_accounts)
569 priv->transport_accounts =
570 get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
572 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
574 g_return_if_reached (); /* incorrect req type */
579 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
581 ModestTnyAccountStorePrivate *priv;
582 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
584 if (!priv->cache_dir)
585 priv->cache_dir = g_build_filename (g_get_home_dir(),
586 MODEST_DIR, MODEST_CACHE_DIR, NULL);
587 return priv->cache_dir;
592 * callers need to unref
595 modest_tny_account_store_get_device (TnyAccountStore *self)
597 ModestTnyAccountStorePrivate *priv;
599 g_return_val_if_fail (self, NULL);
601 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
604 return g_object_ref (G_OBJECT(priv->device));
611 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
613 TnyAccount *account = NULL;
614 ModestTnyAccountStorePrivate *priv;
617 g_return_val_if_fail (self, NULL);
618 g_return_val_if_fail (url_string, NULL);
620 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
622 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
623 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
624 account = TNY_ACCOUNT(cursor->data);
630 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
631 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
632 account = TNY_ACCOUNT(cursor->data);
639 g_object_ref (G_OBJECT(account));
647 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
648 gboolean question, const GError *error)
650 g_return_val_if_fail (error, FALSE);
652 if ((error->domain != TNY_ACCOUNT_ERROR)
653 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
654 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
655 __FUNCTION__, error->domain, error->message);
659 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
662 /* const gchar *prompt = NULL; */
663 gchar *prompt = NULL;
664 switch (error->code) {
665 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
666 /* The tinymail camel implementation just sends us this for almost
667 * everything, so we have to guess at the cause.
668 * It could be a wrong password, or inability to resolve a hostname,
669 * or lack of network, or something entirely different: */
670 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
671 g_debug ("%s: Handling GError domain=%d, code=%d, message=%s",
672 __FUNCTION__, error->domain, error->code, error->message);
674 /* TODO: Remove the internal error message for the real release.
675 * This is just so the testers can give us more information: */
676 /* prompt = _("Modest account not yet fully configured."); */
677 prompt = g_strdup_printf(_("Modest account not yet fully configured. Error=%s"),
681 g_warning ("%s: Unhandled GError code: %d, message=%s",
682 __FUNCTION__, error->code, error->message);
690 #ifdef MODEST_PLATFORM_MAEMO
691 /* The Tinymail documentation says that we should show Yes and No buttons,
692 * when it is a question.
693 * Obviously, we need tinymail to use more specific error codes instead,
694 * so we know what buttons to show. */
695 GtkWidget *dialog = NULL;
697 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL,
700 dialog = GTK_WIDGET (hildon_note_new_information (NULL,
705 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
708 case TNY_ALERT_TYPE_INFO:
709 gtktype = GTK_MESSAGE_INFO;
711 case TNY_ALERT_TYPE_WARNING:
712 gtktype = GTK_MESSAGE_WARNING;
714 case TNY_ALERT_TYPE_ERROR:
716 gtktype = GTK_MESSAGE_ERROR;
720 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
721 gtktype, GTK_BUTTONS_YES_NO, prompt);
722 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
724 gboolean retval = TRUE;
725 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
727 retval = (response == GTK_RESPONSE_YES) ||
728 (response == GTK_RESPONSE_OK);
731 gtk_widget_destroy (dialog);
733 /* TODO: Don't free this when we no longer strdup the message for testers. */
737 printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval);
743 modest_tny_account_store_init (gpointer g, gpointer iface_data)
745 TnyAccountStoreIface *klass;
747 g_return_if_fail (g);
749 klass = (TnyAccountStoreIface *)g;
751 klass->get_accounts_func =
752 modest_tny_account_store_get_accounts;
753 klass->get_cache_dir_func =
754 modest_tny_account_store_get_cache_dir;
755 klass->get_device_func =
756 modest_tny_account_store_get_device;
758 modest_tny_account_store_alert;
759 klass->find_account_func =
760 modest_tny_account_store_find_account_by_url;
764 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
765 ModestTnyGetPassFunc func)
767 /* not implemented, we use signals */
768 g_printerr ("modest: set_get_pass_func not implemented\n");
772 modest_tny_account_store_get_session (TnyAccountStore *self)
774 g_return_val_if_fail (self, NULL);
775 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
780 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
782 TnyAccount *account = NULL;
783 ModestTnyAccountStorePrivate *priv;
786 g_return_val_if_fail (self, NULL);
787 g_return_val_if_fail (id, NULL);
789 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
792 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
793 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
794 if (acc_id && strcmp (acc_id, id) == 0) {
795 account = TNY_ACCOUNT(cursor->data);
800 /* if we already found something, no need to search the transport accounts */
801 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
802 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
803 if (acc_id && strcmp (acc_id, id) == 0) {
804 account = TNY_ACCOUNT(cursor->data);
810 g_object_ref (G_OBJECT(account));
816 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
817 const gchar *account_name,
820 TnyAccount *account = NULL;
822 ModestTnyAccountStorePrivate *priv;
824 g_return_val_if_fail (self, NULL);
825 g_return_val_if_fail (account_name, NULL);
826 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
829 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
831 /* Special case for the local account */
832 if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
833 id = g_strdup (MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
835 ModestAccountData *account_data;
837 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
839 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
843 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
844 id = g_strdup (account_data->store_account->account_name);
845 else if (account_data->transport_account)
846 id = g_strdup (account_data->transport_account->account_name);
848 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
852 g_printerr ("modest: could not get an id for account %s\n",
855 account = modest_tny_account_store_get_tny_account_by_id (self, id);
858 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
859 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
860 account_name, id ? id : "<none>");
866 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
867 const gchar *account_name)
869 /* Get the current connection: */
870 TnyDevice *device = modest_runtime_get_device ();
872 if (!tny_device_is_online (device))
875 #ifdef MODEST_PLATFORM_MAEMO
876 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
877 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
878 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
882 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
886 const gchar *connection_name = con_ic_iap_get_name (connection);
887 if (!connection_name)
890 /* Get the connection-specific transport acccount, if any: */
891 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
892 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
893 account_name, connection_name);
895 if (!server_account_name)
896 return NULL; /* No connection-specific SMTP server was specified for this connection. */
898 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
899 g_free (server_account_name);
901 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
902 g_object_unref (connection);
906 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
907 #endif /* MODEST_PLATFORM_MAEMO */
912 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
913 const gchar *account_name)
915 /* Get the connection-specific transport acccount, if any: */
916 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
918 /* If there is no connection-specific transport account (the common case),
919 * just get the regular transport account: */
921 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
922 TNY_ACCOUNT_TYPE_TRANSPORT);