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>
61 /* 'private'/'protected' functions */
62 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
63 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
64 static void modest_tny_account_store_finalize (GObject *obj);
66 /* implementations for tny-account-store-iface */
67 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
68 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
73 ACCOUNT_UPDATE_SIGNAL,
74 PASSWORD_REQUESTED_SIGNAL,
78 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
79 struct _ModestTnyAccountStorePrivate {
81 GHashTable *password_hash;
82 ModestAccountMgr *account_mgr;
83 TnySessionCamel *session;
86 /* we cache them here */
87 GSList *store_accounts;
88 GSList *transport_accounts;
91 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
92 MODEST_TYPE_TNY_ACCOUNT_STORE, \
93 ModestTnyAccountStorePrivate))
96 static GObjectClass *parent_class = NULL;
98 static guint signals[LAST_SIGNAL] = {0};
101 modest_tny_account_store_get_type (void)
103 static GType my_type = 0;
106 static const GTypeInfo my_info = {
107 sizeof(ModestTnyAccountStoreClass),
108 NULL, /* base init */
109 NULL, /* base finalize */
110 (GClassInitFunc) modest_tny_account_store_class_init,
111 NULL, /* class finalize */
112 NULL, /* class data */
113 sizeof(ModestTnyAccountStore),
115 (GInstanceInitFunc) modest_tny_account_store_instance_init,
119 static const GInterfaceInfo iface_info = {
120 (GInterfaceInitFunc) modest_tny_account_store_init,
121 NULL, /* interface_finalize */
122 NULL /* interface_data */
125 my_type = g_type_register_static (G_TYPE_OBJECT,
126 "ModestTnyAccountStore",
128 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
135 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
137 GObjectClass *gobject_class;
138 gobject_class = (GObjectClass*) klass;
140 parent_class = g_type_class_peek_parent (klass);
141 gobject_class->finalize = modest_tny_account_store_finalize;
143 g_type_class_add_private (gobject_class,
144 sizeof(ModestTnyAccountStorePrivate));
146 signals[ACCOUNT_UPDATE_SIGNAL] =
147 g_signal_new ("account_update",
148 G_TYPE_FROM_CLASS (gobject_class),
150 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
152 g_cclosure_marshal_VOID__STRING,
153 G_TYPE_NONE, 1, G_TYPE_STRING);
155 signals[PASSWORD_REQUESTED_SIGNAL] =
156 g_signal_new ("password_requested",
157 G_TYPE_FROM_CLASS (gobject_class),
159 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
161 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
162 G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
168 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
170 ModestTnyAccountStorePrivate *priv =
171 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
173 priv->cache_dir = NULL;
174 priv->account_mgr = NULL;
175 priv->session = NULL;
178 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
185 account_list_free (GSList *accounts)
187 GSList *cursor = accounts;
190 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
191 const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
192 modest_runtime_verify_object_last_ref(cursor->data,id);
194 g_object_unref (G_OBJECT(cursor->data));
195 cursor = cursor->next;
197 g_slist_free (accounts);
202 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
205 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
206 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
208 /* FIXME: make this more finegrained; changes do not really affect _all_
209 * accounts, and some do not affect tny accounts at all (such as 'last_update')
213 account_list_free (priv->store_accounts);
214 priv->store_accounts = NULL;
216 account_list_free (priv->transport_accounts);
217 priv->transport_accounts = NULL;
219 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
225 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
226 const gchar *key, gpointer user_data)
228 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
229 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
231 /* FIXME: make this more finegrained; changes do not really affect _all_
232 * accounts, and some do not affect tny accounts at all (such as 'last_update')
234 if (priv->store_accounts) {
235 account_list_free (priv->store_accounts);
236 priv->store_accounts = NULL;
239 if (priv->transport_accounts) {
240 account_list_free (priv->transport_accounts);
241 priv->transport_accounts = NULL;
244 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
249 static ModestTnyAccountStore*
250 get_account_store_for_account (TnyAccount *account)
252 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
256 /* This callback will be called by Tinymail when it needs the password.
257 * Note that TnyAccount here will be the server account. */
259 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
262 const TnyAccountStore *account_store;
263 ModestTnyAccountStore *self;
264 ModestTnyAccountStorePrivate *priv;
265 gchar *username = NULL;
268 gboolean already_asked;
270 key = tny_account_get_id (account);
271 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
273 self = MODEST_TNY_ACCOUNT_STORE (account_store);
274 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
276 /* TODO: What is this hash for? */
277 /* is it in the hash? if it's already there, it must be wrong... */
278 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
279 * type-punned ptrs...*/
280 already_asked = g_hash_table_lookup_extended (priv->password_hash,
283 (gpointer*)&pwd_ptr);
285 /* if the password is not already there, try ModestConf */
286 if (!already_asked) {
287 pwd = modest_account_mgr_get_string (priv->account_mgr,
288 key, MODEST_ACCOUNT_PASSWORD, TRUE);
289 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
292 /* if it was already asked, it must have been wrong, so ask again */
293 if (already_asked || !pwd || strlen(pwd) == 0) {
294 /* we don't have it yet. Get the password from the user */
295 const gchar* account_id = tny_account_get_id (account);
296 gboolean remember = FALSE;
299 /* Note that we ignore the returned username here,
300 * because it is enough that it will be stored in gconf
301 * by the signal handler. */
302 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
303 account_id, /* server_account_name */
304 &username, &pwd, cancel, &remember);
308 modest_account_mgr_set_string (priv->account_mgr,key,
309 MODEST_ACCOUNT_PASSWORD,
311 /* We need to dup the string even knowing that
312 it's already a dup of the contents of an
313 entry, because it if it's wrong, then camel
315 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
317 g_hash_table_remove (priv->password_hash, key);
329 /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
336 forget_password (TnyAccount *account)
338 ModestTnyAccountStore *self;
339 ModestTnyAccountStorePrivate *priv;
340 const TnyAccountStore *account_store;
344 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
345 self = MODEST_TNY_ACCOUNT_STORE (account_store);
346 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
347 key = tny_account_get_id (account);
349 /* Do not remove the key, this will allow us to detect that we
350 have already asked for it at least once */
351 pwd = g_hash_table_lookup (priv->password_hash, key);
353 memset (pwd, 0, strlen (pwd));
354 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
357 /* Remove from configuration system */
358 modest_account_mgr_unset (priv->account_mgr,
359 key, MODEST_ACCOUNT_PASSWORD, TRUE);
364 modest_tny_account_store_finalize (GObject *obj)
366 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
367 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
369 //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
371 g_free (priv->cache_dir);
372 priv->cache_dir = NULL;
374 if (priv->password_hash) {
375 g_hash_table_destroy (priv->password_hash);
376 priv->password_hash = NULL;
379 if (priv->account_mgr) {
380 g_object_unref (G_OBJECT(priv->account_mgr));
381 priv->account_mgr = NULL;
385 g_object_unref (G_OBJECT(priv->device));
389 /* this includes the local folder */
390 account_list_free (priv->store_accounts);
391 priv->store_accounts = NULL;
393 account_list_free (priv->transport_accounts);
394 priv->transport_accounts = NULL;
397 camel_object_unref (CAMEL_OBJECT(priv->session));
398 priv->session = NULL;
401 G_OBJECT_CLASS(parent_class)->finalize (obj);
405 ModestTnyAccountStore*
406 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
409 ModestTnyAccountStorePrivate *priv;
412 g_return_val_if_fail (account_mgr, NULL);
413 g_return_val_if_fail (device, NULL);
415 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
416 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
418 priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
419 priv->device = g_object_ref (device);
421 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
423 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
424 /* FIXME: unref this in the end? */
425 tny_session_camel_set_async_connecting (priv->session, TRUE);
427 /* force a cache fill... ugly */
428 list = TNY_LIST(tny_simple_list_new());
429 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
430 TNY_ACCOUNT_STORE_BOTH);
431 g_object_unref(list);
433 /* Connect signals */
434 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
435 G_CALLBACK (on_account_changed), obj);
436 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
437 G_CALLBACK (on_account_removed), obj);
439 return MODEST_TNY_ACCOUNT_STORE(obj);
444 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
446 ModestTnyAccountStorePrivate *priv;
447 GSList *accounts, *cursor;
449 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
450 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
454 tny_list_prepend (list, G_OBJECT(cursor->data));
455 cursor = cursor->next;
461 /* this function fills the TnyList, and also returns a GSList of the accounts,
462 * for caching purposes
465 get_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
467 ModestTnyAccountStorePrivate *priv = NULL;
468 GSList *account_names = NULL, *cursor = NULL;
469 GSList *accounts = NULL;
471 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
473 account_names = modest_account_mgr_account_names (priv->account_mgr,
474 TRUE /* including disabled accounts */);
476 for (cursor = account_names; cursor; cursor = cursor->next) {
478 gchar *account_name = (gchar*)cursor->data;
480 /* only return enabled accounts */
481 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
482 TnyAccount *tny_account =
483 modest_tny_account_new_from_account (priv->account_mgr,
489 g_object_set_data (G_OBJECT(tny_account), "account_store",
491 tny_list_prepend (list, G_OBJECT(tny_account));
492 accounts = g_slist_append (accounts, tny_account); /* cache it */
494 g_printerr ("modest: failed to create account for %s\n",
497 g_free (account_name);
499 g_slist_free (account_names);
501 /* also, add the local folder pseudo-account */
502 if (type == TNY_ACCOUNT_TYPE_STORE) {
503 TnyAccount *tny_account =
504 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
505 tny_list_prepend (list, G_OBJECT(tny_account));
506 accounts = g_slist_append (accounts, tny_account); /* cache it */
513 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
514 TnyGetAccountsRequestType request_type)
516 ModestTnyAccountStorePrivate *priv;
518 g_return_if_fail (self);
519 g_return_if_fail (TNY_IS_LIST(list));
521 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
523 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
524 modest_tny_account_store_get_accounts (self, list,
525 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
526 modest_tny_account_store_get_accounts (self, list,
527 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
531 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
533 if (!priv->store_accounts)
534 priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
536 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
538 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
540 if (!priv->transport_accounts)
541 priv->transport_accounts =
542 get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
544 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
546 g_return_if_reached (); /* incorrect req type */
551 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
553 ModestTnyAccountStorePrivate *priv;
554 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
556 if (!priv->cache_dir)
557 priv->cache_dir = g_build_filename (g_get_home_dir(),
558 MODEST_DIR, MODEST_CACHE_DIR, NULL);
559 return priv->cache_dir;
564 * callers need to unref
567 modest_tny_account_store_get_device (TnyAccountStore *self)
569 ModestTnyAccountStorePrivate *priv;
571 g_return_val_if_fail (self, NULL);
573 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
576 return g_object_ref (G_OBJECT(priv->device));
583 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
585 TnyAccount *account = NULL;
586 ModestTnyAccountStorePrivate *priv;
589 g_return_val_if_fail (self, NULL);
590 g_return_val_if_fail (url_string, NULL);
592 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
594 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
595 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
596 account = TNY_ACCOUNT(cursor->data);
602 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
603 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
604 account = TNY_ACCOUNT(cursor->data);
611 g_object_ref (G_OBJECT(account));
619 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
622 g_return_val_if_fail (error, FALSE);
623 g_return_val_if_fail (error->domain == TNY_ACCOUNT_ERROR, FALSE);
625 /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
627 GtkMessageType gtktype;
628 gboolean retval = FALSE;
633 case TNY_ALERT_TYPE_INFO:
634 gtktype = GTK_MESSAGE_INFO;
636 case TNY_ALERT_TYPE_WARNING:
637 gtktype = GTK_MESSAGE_WARNING;
639 case TNY_ALERT_TYPE_ERROR:
641 gtktype = GTK_MESSAGE_ERROR;
645 const gchar *prompt = NULL;
648 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
649 prompt = _("Modest account not yet fully configured");
652 g_warning ("%s: Unhandled GError code.", __FUNCTION__);
660 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
661 gtktype, GTK_BUTTONS_YES_NO, prompt);
663 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
666 gtk_widget_destroy (dialog);
673 modest_tny_account_store_init (gpointer g, gpointer iface_data)
675 TnyAccountStoreIface *klass;
677 g_return_if_fail (g);
679 klass = (TnyAccountStoreIface *)g;
681 klass->get_accounts_func =
682 modest_tny_account_store_get_accounts;
683 klass->get_cache_dir_func =
684 modest_tny_account_store_get_cache_dir;
685 klass->get_device_func =
686 modest_tny_account_store_get_device;
688 modest_tny_account_store_alert;
689 klass->find_account_func =
690 modest_tny_account_store_find_account_by_url;
694 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
695 ModestTnyGetPassFunc func)
697 /* not implemented, we use signals */
698 g_printerr ("modest: set_get_pass_func not implemented\n");
702 tny_account_store_get_session (TnyAccountStore *self)
704 g_return_val_if_fail (self, NULL);
705 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
710 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
712 TnyAccount *account = NULL;
713 ModestTnyAccountStorePrivate *priv;
716 g_return_val_if_fail (self, NULL);
717 g_return_val_if_fail (id, NULL);
719 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
721 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
722 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
723 if (acc_id && strcmp (acc_id, id) == 0) {
724 account = TNY_ACCOUNT(cursor->data);
729 /* if we already found something, no need to search the transport accounts */
730 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
731 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
732 if (acc_id && strcmp (acc_id, id) == 0) {
733 account = TNY_ACCOUNT(cursor->data);
739 g_object_ref (G_OBJECT(account));
745 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
746 const gchar *account_name,
749 TnyAccount *account = NULL;
750 ModestAccountData *account_data;
751 const gchar *id = NULL;
752 ModestTnyAccountStorePrivate *priv;
754 g_return_val_if_fail (self, NULL);
755 g_return_val_if_fail (account_name, NULL);
756 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
759 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
761 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
763 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
767 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
768 id = account_data->store_account->account_name;
769 else if (account_data->transport_account)
770 id = account_data->transport_account->account_name;
773 g_printerr ("modest: could not get an id for account %s\n",
776 account = modest_tny_account_store_get_tny_account_by_id (self, id);
779 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
780 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
781 account_name, id ? id : "<none>");
783 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
787 static TnyAccount* get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self, const gchar *account_name)
789 /* Get the current connection: */
790 TnyDevice *device = modest_runtime_get_device ();
792 if (!tny_device_is_online (device))
795 #ifdef MODEST_PLATFORM_MAEMO
796 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
797 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
798 const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
802 ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
806 const gchar *connection_name = con_ic_iap_get_name (connection);
807 if (!connection_name)
810 /* Get the connection-specific transport acccount, if any: */
811 ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
812 gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager,
813 account_name, connection_name);
815 if (!server_account_name)
816 return NULL; /* No connection-specific SMTP server was specified for this connection. */
818 TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
819 g_free (server_account_name);
821 /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
822 g_object_unref (connection);
826 return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
827 #endif /* MODEST_PLATFORM_MAEMO */
831 TnyAccount* modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
832 const gchar *account_name)
834 /* Get the connection-specific transport acccount, if any: */
835 TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
837 /* If there is no connection-specific transport account (the common case),
838 * just get the regular transport account: */
840 account = modest_tny_account_store_get_tny_account_by_account (self, account_name,
841 TNY_ACCOUNT_TYPE_TRANSPORT);