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);
78 ACCOUNT_UPDATE_SIGNAL,
79 PASSWORD_REQUESTED_SIGNAL,
83 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
84 struct _ModestTnyAccountStorePrivate {
86 GHashTable *password_hash;
87 ModestAccountMgr *account_mgr;
88 TnySessionCamel *session;
91 /* we cache them here */
92 GSList *store_accounts;
93 GSList *transport_accounts;
96 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
97 MODEST_TYPE_TNY_ACCOUNT_STORE, \
98 ModestTnyAccountStorePrivate))
101 static GObjectClass *parent_class = NULL;
103 static guint signals[LAST_SIGNAL] = {0};
106 modest_tny_account_store_get_type (void)
108 static GType my_type = 0;
111 static const GTypeInfo my_info = {
112 sizeof(ModestTnyAccountStoreClass),
113 NULL, /* base init */
114 NULL, /* base finalize */
115 (GClassInitFunc) modest_tny_account_store_class_init,
116 NULL, /* class finalize */
117 NULL, /* class data */
118 sizeof(ModestTnyAccountStore),
120 (GInstanceInitFunc) modest_tny_account_store_instance_init,
124 static const GInterfaceInfo iface_info = {
125 (GInterfaceInitFunc) modest_tny_account_store_init,
126 NULL, /* interface_finalize */
127 NULL /* interface_data */
130 my_type = g_type_register_static (G_TYPE_OBJECT,
131 "ModestTnyAccountStore",
133 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
140 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
142 GObjectClass *gobject_class;
143 gobject_class = (GObjectClass*) klass;
145 parent_class = g_type_class_peek_parent (klass);
146 gobject_class->finalize = modest_tny_account_store_finalize;
148 g_type_class_add_private (gobject_class,
149 sizeof(ModestTnyAccountStorePrivate));
151 signals[ACCOUNT_UPDATE_SIGNAL] =
152 g_signal_new ("account_update",
153 G_TYPE_FROM_CLASS (gobject_class),
155 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
157 g_cclosure_marshal_VOID__STRING,
158 G_TYPE_NONE, 1, G_TYPE_STRING);
160 signals[PASSWORD_REQUESTED_SIGNAL] =
161 g_signal_new ("password_requested",
162 G_TYPE_FROM_CLASS (gobject_class),
164 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
166 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
167 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
173 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
175 ModestTnyAccountStorePrivate *priv =
176 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
178 priv->cache_dir = NULL;
179 priv->account_mgr = NULL;
180 priv->session = NULL;
183 /* An in-memory store of passwords,
184 * for passwords that are not remembered in the configuration,
185 * so they need to be asked for from the user once in each session:
187 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
194 account_list_free (GSList *accounts)
196 GSList *cursor = accounts;
199 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
200 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
201 modest_runtime_verify_object_last_ref(cursor->data,id);
203 g_object_unref (G_OBJECT(cursor->data));
204 cursor = cursor->next;
206 g_slist_free (accounts);
211 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
214 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
215 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
217 /* FIXME: make this more finegrained; changes do not really affect _all_
218 * accounts, and some do not affect tny accounts at all (such as 'last_update')
222 account_list_free (priv->store_accounts);
223 priv->store_accounts = NULL;
225 account_list_free (priv->transport_accounts);
226 priv->transport_accounts = NULL;
228 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
234 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
235 const gchar *key, gboolean server_account, gpointer user_data)
238 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
239 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
241 /* FIXME: make this more finegrained; changes do not really affect _all_
242 * accounts, and some do not affect tny accounts at all (such as 'last_update')
244 if (priv->store_accounts) {
245 account_list_free (priv->store_accounts);
246 priv->store_accounts = NULL;
249 if (priv->transport_accounts) {
250 account_list_free (priv->transport_accounts);
251 priv->transport_accounts = NULL;
254 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
259 static ModestTnyAccountStore*
260 get_account_store_for_account (TnyAccount *account)
262 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
266 /* This callback will be called by Tinymail when it needs the password
267 * from the user, for instance if the password was not remembered.
268 * Note that TnyAccount here will be the server account. */
270 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
273 const TnyAccountStore *account_store;
274 ModestTnyAccountStore *self;
275 ModestTnyAccountStorePrivate *priv;
276 gchar *username = NULL;
279 gboolean already_asked;
281 key = tny_account_get_id (account);
282 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
284 self = MODEST_TNY_ACCOUNT_STORE (account_store);
285 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
287 /* This hash map stores passwords, including passwords that are not stored in gconf. */
288 /* is it in the hash? if it's already there, it must be wrong... */
289 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
290 * type-punned ptrs...*/
291 already_asked = g_hash_table_lookup_extended (priv->password_hash,
294 (gpointer*)&pwd_ptr);
296 /* if the password is not already there, try ModestConf */
297 if (!already_asked) {
298 pwd = modest_account_mgr_get_string (priv->account_mgr,
299 key, MODEST_ACCOUNT_PASSWORD, TRUE);
300 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
303 /* if it was already asked, it must have been wrong, so ask again */
304 if (already_asked || !pwd || strlen(pwd) == 0) {
305 /* we don't have it yet. Get the password from the user */
306 const gchar* account_id = tny_account_get_id (account);
307 gboolean remember = FALSE;
310 /* Note that we ignore the returned username here,
311 * because it is enough that it will be stored in gconf
312 * by the signal handler. */
313 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
314 account_id, /* server_account_name */
315 &username, &pwd, cancel, &remember);
319 modest_account_mgr_set_string (priv->account_mgr,key,
320 MODEST_ACCOUNT_PASSWORD,
322 /* We need to dup the string even knowing that
323 it's already a dup of the contents of an
324 entry, because it if it's wrong, then camel
326 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
328 g_hash_table_remove (priv->password_hash, key);
339 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
346 forget_password (TnyAccount *account)
348 ModestTnyAccountStore *self;
349 ModestTnyAccountStorePrivate *priv;
350 const TnyAccountStore *account_store;
354 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
355 self = MODEST_TNY_ACCOUNT_STORE (account_store);
356 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
357 key = tny_account_get_id (account);
359 /* Do not remove the key, this will allow us to detect that we
360 have already asked for it at least once */
361 pwd = g_hash_table_lookup (priv->password_hash, key);
363 memset (pwd, 0, strlen (pwd));
364 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
367 /* Remove from configuration system */
368 modest_account_mgr_unset (priv->account_mgr,
369 key, MODEST_ACCOUNT_PASSWORD, TRUE);
374 modest_tny_account_store_finalize (GObject *obj)
376 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
377 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
379 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
381 g_free (priv->cache_dir);
382 priv->cache_dir = NULL;
384 if (priv->password_hash) {
385 g_hash_table_destroy (priv->password_hash);
386 priv->password_hash = NULL;
389 if (priv->account_mgr) {
390 g_object_unref (G_OBJECT(priv->account_mgr));
391 priv->account_mgr = NULL;
395 g_object_unref (G_OBJECT(priv->device));
399 /* this includes the local folder */
400 account_list_free (priv->store_accounts);
401 priv->store_accounts = NULL;
403 account_list_free (priv->transport_accounts);
404 priv->transport_accounts = NULL;
407 camel_object_unref (CAMEL_OBJECT(priv->session));
408 priv->session = NULL;
411 G_OBJECT_CLASS(parent_class)->finalize (obj);
415 ModestTnyAccountStore*
416 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
419 ModestTnyAccountStorePrivate *priv;
422 g_return_val_if_fail (account_mgr, NULL);
423 g_return_val_if_fail (device, NULL);
425 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
426 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
428 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
429 priv->device = g_object_ref (device);
431 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
433 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
434 /* FIXME: unref this in the end? */
435 tny_session_camel_set_async_connecting (priv->session, TRUE);
437 /* force a cache fill... ugly */
438 list = TNY_LIST(tny_simple_list_new());
439 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
440 TNY_ACCOUNT_STORE_BOTH);
441 g_object_unref(list);
443 /* Connect signals */
444 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
445 G_CALLBACK (on_account_changed), obj);
446 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
447 G_CALLBACK (on_account_removed), obj);
449 return MODEST_TNY_ACCOUNT_STORE(obj);
454 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
456 ModestTnyAccountStorePrivate *priv;
457 GSList *accounts, *cursor;
459 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
460 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
464 tny_list_prepend (list, G_OBJECT(cursor->data));
465 cursor = cursor->next;
471 /* this function fills the TnyList, and also returns a GSList of the accounts,
472 * for caching purposes
475 get_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
477 ModestTnyAccountStorePrivate *priv = NULL;
478 GSList *account_names = NULL, *cursor = NULL;
479 GSList *accounts = NULL;
481 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
483 account_names = modest_account_mgr_account_names (priv->account_mgr,
484 TRUE /* including disabled accounts */);
486 for (cursor = account_names; cursor; cursor = cursor->next) {
488 gchar *account_name = (gchar*)cursor->data;
490 /* only return enabled accounts */
491 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
492 TnyAccount *tny_account =
493 modest_tny_account_new_from_account (priv->account_mgr,
499 g_object_set_data (G_OBJECT(tny_account), "account_store",
501 tny_list_prepend (list, G_OBJECT(tny_account));
502 accounts = g_slist_append (accounts, tny_account); /* cache it */
504 g_printerr ("modest: failed to create account for %s\n",
507 g_free (account_name);
509 g_slist_free (account_names);
511 /* also, add the local folder pseudo-account */
512 if (type == TNY_ACCOUNT_TYPE_STORE) {
513 TnyAccount *tny_account =
514 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
515 tny_list_prepend (list, G_OBJECT(tny_account));
516 accounts = g_slist_append (accounts, tny_account); /* cache it */
523 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
524 TnyGetAccountsRequestType request_type)
526 ModestTnyAccountStorePrivate *priv;
528 g_return_if_fail (self);
529 g_return_if_fail (TNY_IS_LIST(list));
531 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
533 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
534 modest_tny_account_store_get_accounts (self, list,
535 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
536 modest_tny_account_store_get_accounts (self, list,
537 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
541 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
543 if (!priv->store_accounts)
544 priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
546 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
548 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
550 if (!priv->transport_accounts)
551 priv->transport_accounts =
552 get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
554 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
556 g_return_if_reached (); /* incorrect req type */
561 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
563 ModestTnyAccountStorePrivate *priv;
564 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
566 if (!priv->cache_dir)
567 priv->cache_dir = g_build_filename (g_get_home_dir(),
568 MODEST_DIR, MODEST_CACHE_DIR, NULL);
569 return priv->cache_dir;
574 * callers need to unref
577 modest_tny_account_store_get_device (TnyAccountStore *self)
579 ModestTnyAccountStorePrivate *priv;
581 g_return_val_if_fail (self, NULL);
583 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
586 return g_object_ref (G_OBJECT(priv->device));
593 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
595 TnyAccount *account = NULL;
596 ModestTnyAccountStorePrivate *priv;
599 g_return_val_if_fail (self, NULL);
600 g_return_val_if_fail (url_string, NULL);
602 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
604 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
605 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
606 account = TNY_ACCOUNT(cursor->data);
612 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
613 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
614 account = TNY_ACCOUNT(cursor->data);
621 g_object_ref (G_OBJECT(account));
629 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
632 g_return_val_if_fail (error, FALSE);
634 if ((error->domain != TNY_ACCOUNT_ERROR)
635 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
636 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s",
637 __FUNCTION__, error->domain, error->message);
641 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
645 const gchar *prompt = NULL;
648 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
649 prompt = _("Modest account not yet fully configured");
651 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT:
652 g_warning("%s: TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT: message=%s",
653 __FUNCTION__, error->message);
654 prompt = _("Unknown Tinymail error (TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT)");
657 g_warning ("%s: Unhandled GError code: %d, message=%s",
658 __FUNCTION__, error->code, error->message);
666 gboolean retval = FALSE;
667 #ifdef MODEST_PLATFORM_MAEMO
668 /* The Tinymail documentation says that we should show Yes and No buttons,
669 * but these never seem to be questions: */
670 GtkWidget *dialog = GTK_WIDGET (hildon_note_new_information (NULL, prompt));
673 GtkMessageType gtktype = GTK_MESSAGE_ERROR;
676 case TNY_ALERT_TYPE_INFO:
677 gtktype = GTK_MESSAGE_INFO;
679 case TNY_ALERT_TYPE_WARNING:
680 gtktype = GTK_MESSAGE_WARNING;
682 case TNY_ALERT_TYPE_ERROR:
684 gtktype = GTK_MESSAGE_ERROR;
688 GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
689 gtktype, GTK_BUTTONS_YES_NO, prompt);
690 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
692 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
695 gtk_widget_destroy (dialog);
702 modest_tny_account_store_init (gpointer g, gpointer iface_data)
704 TnyAccountStoreIface *klass;
706 g_return_if_fail (g);
708 klass = (TnyAccountStoreIface *)g;
710 klass->get_accounts_func =
711 modest_tny_account_store_get_accounts;
712 klass->get_cache_dir_func =
713 modest_tny_account_store_get_cache_dir;
714 klass->get_device_func =
715 modest_tny_account_store_get_device;
717 modest_tny_account_store_alert;
718 klass->find_account_func =
719 modest_tny_account_store_find_account_by_url;
723 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
724 ModestTnyGetPassFunc func)
726 /* not implemented, we use signals */
727 g_printerr ("modest: set_get_pass_func not implemented\n");
731 tny_account_store_get_session (TnyAccountStore *self)
733 g_return_val_if_fail (self, NULL);
734 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
739 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
741 TnyAccount *account = NULL;
742 ModestTnyAccountStorePrivate *priv;
745 g_return_val_if_fail (self, NULL);
746 g_return_val_if_fail (id, NULL);
748 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
750 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
751 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
752 if (acc_id && strcmp (acc_id, id) == 0) {
753 account = TNY_ACCOUNT(cursor->data);
758 /* if we already found something, no need to search the transport accounts */
759 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
760 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
761 if (acc_id && strcmp (acc_id, id) == 0) {
762 account = TNY_ACCOUNT(cursor->data);
768 g_object_ref (G_OBJECT(account));
774 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
775 const gchar *account_name,
778 TnyAccount *account = NULL;
779 ModestAccountData *account_data;
780 const gchar *id = NULL;
781 ModestTnyAccountStorePrivate *priv;
783 g_return_val_if_fail (self, NULL);
784 g_return_val_if_fail (account_name, NULL);
785 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
788 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
790 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
792 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
796 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
797 id = account_data->store_account->account_name;
798 else if (account_data->transport_account)
799 id = account_data->transport_account->account_name;
802 g_printerr ("modest: could not get an id for account %s\n",
805 account = modest_tny_account_store_get_tny_account_by_id (self, id);
808 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
809 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
810 account_name, id ? id : "<none>");
812 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
816 static TnyAccount* get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self, const gchar *account_name)
818 /* Get the current connection: */
819 TnyDevice *device = modest_runtime_get_device ();
821 if (!tny_device_is_online (device))
824 #ifdef MODEST_PLATFORM_MAEMO
825 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
826 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
827 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
831 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
835 const gchar *connection_name = con_ic_iap_get_name (connection);
836 if (!connection_name)
839 /* Get the connection-specific transport acccount, if any: */
840 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
841 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
842 account_name, connection_name);
844 if (!server_account_name)
845 return NULL; /* No connection-specific SMTP server was specified for this connection. */
847 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
848 g_free (server_account_name);
850 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
851 g_object_unref (connection);
855 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
856 #endif /* MODEST_PLATFORM_MAEMO */
860 TnyAccount* modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
861 const gchar *account_name)
863 /* Get the connection-specific transport acccount, if any: */
864 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
866 /* If there is no connection-specific transport account (the common case),
867 * just get the regular transport account: */
869 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
870 TNY_ACCOUNT_TYPE_TRANSPORT);