0952eb4cc4c288c65193738cffcb7f7140686b88
[modest] / src / modest-tny-account-store.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <string.h>
31 #include <glib/gi18n.h>
32
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>
43
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>
51 #include <widgets/modest-window-mgr.h>
52 #include <modest-account-settings-dialog.h>
53
54
55 #include "modest-tny-account-store.h"
56 #include "modest-tny-platform-factory.h"
57 #include <tny-gtk-lockable.h>
58 #include <camel/camel.h>
59
60 #ifdef MODEST_PLATFORM_MAEMO
61 #include <tny-maemo-conic-device.h>
62 #ifdef MODEST_HILDON_VERSION_0
63 #include <hildon-widgets/hildon-note.h>
64 #include <hildon-widgets/hildon-banner.h>
65 #else
66 #include <hildon/hildon-note.h>
67 #include <hildon/hildon-banner.h>
68 #endif
69 #endif
70
71 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
72
73 /* 'private'/'protected' functions */
74 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
75 //static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
76 static void modest_tny_account_store_finalize     (GObject *obj);
77
78 /* implementations for tny-account-store-iface */
79 static void    modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
80 static void    modest_tny_account_store_init          (gpointer g, gpointer iface_data);
81
82
83 static void
84 get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type);
85
86
87 /* list my signals */
88 enum {
89         ACCOUNT_UPDATE_SIGNAL,
90         PASSWORD_REQUESTED_SIGNAL,
91         LAST_SIGNAL
92 };
93
94 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
95 struct _ModestTnyAccountStorePrivate {
96         gchar              *cache_dir;  
97         GHashTable         *password_hash;
98         ModestAccountMgr   *account_mgr;
99         TnySessionCamel    *session;
100         TnyDevice          *device;
101         
102         /* We cache the lists of accounts here.
103          * They are created in our get_accounts_func() implementation. */
104         GSList             *store_accounts;
105         GSList             *transport_accounts;
106         
107         /* This is also contained in store_accounts,
108          * but we cached it temporarily separately, 
109          * because we create this while creating the transport accounts, 
110          * but return it when requesting the store accounts: 
111          */
112         GSList             *store_accounts_outboxes;
113 };
114
115 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
116                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
117                                                       ModestTnyAccountStorePrivate))
118
119 /* globals */
120 static GObjectClass *parent_class = NULL;
121
122 static guint signals[LAST_SIGNAL] = {0};
123
124 GType
125 modest_tny_account_store_get_type (void)
126 {
127         static GType my_type = 0;
128
129         if (!my_type) {
130                 static const GTypeInfo my_info = {
131                         sizeof(ModestTnyAccountStoreClass),
132                         NULL,           /* base init */
133                         NULL,           /* base finalize */
134                         (GClassInitFunc) modest_tny_account_store_class_init,
135                         NULL,           /* class finalize */
136                         NULL,           /* class data */
137                         sizeof(ModestTnyAccountStore),
138                         0,              /* n_preallocs */
139                         (GInstanceInitFunc) modest_tny_account_store_instance_init,
140                         NULL
141                 };
142
143                 static const GInterfaceInfo iface_info = {
144                         (GInterfaceInitFunc) modest_tny_account_store_init,
145                         NULL,         /* interface_finalize */
146                         NULL          /* interface_data */
147                 };
148                 /* hack hack */
149                 my_type = g_type_register_static (G_TYPE_OBJECT,
150                                                   "ModestTnyAccountStore",
151                                                   &my_info, 0);
152                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
153                                              &iface_info);
154         }
155         return my_type;
156 }
157
158 static void
159 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
160 {
161         GObjectClass *gobject_class;
162         gobject_class = (GObjectClass*) klass;
163
164         parent_class            = g_type_class_peek_parent (klass);
165         gobject_class->finalize = modest_tny_account_store_finalize;
166
167         g_type_class_add_private (gobject_class,
168                                   sizeof(ModestTnyAccountStorePrivate));
169
170          signals[ACCOUNT_UPDATE_SIGNAL] =
171                 g_signal_new ("account_update",
172                               G_TYPE_FROM_CLASS (gobject_class),
173                               G_SIGNAL_RUN_FIRST,
174                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
175                               NULL, NULL,
176                               g_cclosure_marshal_VOID__STRING,
177                               G_TYPE_NONE, 1, G_TYPE_STRING);
178          
179          signals[PASSWORD_REQUESTED_SIGNAL] =
180                  g_signal_new ("password_requested",
181                                G_TYPE_FROM_CLASS (gobject_class),
182                                G_SIGNAL_RUN_FIRST,
183                                G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
184                                NULL, NULL,
185                                modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
186                                G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
187                                G_TYPE_POINTER);
188 }
189
190
191      
192 static void
193 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor, 
194         GnomeVFSVolume *volume, gpointer user_data);
195
196 static void
197 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor, 
198         GnomeVFSVolume *volume, gpointer user_data);
199
200 static void
201 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
202 {
203         ModestTnyAccountStorePrivate *priv =
204                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
205
206         priv->cache_dir              = NULL;
207         priv->account_mgr            = NULL;
208         priv->session                = NULL;
209         priv->device                 = NULL;
210         
211         /* An in-memory store of passwords, 
212          * for passwords that are not remembered in the configuration,
213          * so they need to be asked for from the user once in each session:
214          */
215         priv->password_hash          = g_hash_table_new_full (g_str_hash, g_str_equal,
216                                                               g_free, g_free);
217                                                               
218         /* Respond to volume mounts and unmounts, such 
219          * as the insertion/removal of the memory card: */
220         GnomeVFSVolumeMonitor* monitor = 
221                 gnome_vfs_get_volume_monitor();
222         g_signal_connect (G_OBJECT(monitor), "volume-mounted",
223                           G_CALLBACK(on_vfs_volume_mounted),
224                           obj);
225         g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
226                           G_CALLBACK(on_vfs_volume_unmounted),
227                           obj);
228 }
229
230 static void
231 account_list_free (GSList *accounts)
232 {
233         GSList *cursor = accounts;
234
235         while (cursor) {
236                 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
237                         const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
238                         modest_runtime_verify_object_last_ref(cursor->data,id);
239                 }                       
240                 g_object_unref (G_OBJECT(cursor->data));
241                 cursor = cursor->next;
242         }
243         g_slist_free (accounts);
244 }
245
246 static void
247 recreate_all_accounts (ModestTnyAccountStore *self)
248 {
249         ModestTnyAccountStorePrivate *priv = 
250                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
251         
252         if (priv->store_accounts_outboxes) {
253                 account_list_free (priv->store_accounts_outboxes);
254                 priv->store_accounts_outboxes = NULL;
255         }
256                         
257         if (priv->store_accounts) {
258                 account_list_free (priv->store_accounts);
259                 priv->store_accounts = NULL;
260                 get_server_accounts (TNY_ACCOUNT_STORE(self),
261                                              NULL, TNY_ACCOUNT_TYPE_STORE);
262         }
263         
264         if (priv->transport_accounts) {
265                 account_list_free (priv->transport_accounts);
266                 priv->transport_accounts = NULL;
267                 get_server_accounts (TNY_ACCOUNT_STORE(self), NULL,
268                                              TNY_ACCOUNT_TYPE_TRANSPORT);
269         }
270 }
271
272 static void
273 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor, 
274         GnomeVFSVolume *volume, gpointer user_data)
275 {
276         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
277         
278         /* Check whether this was the external MMC1 card: */
279         gchar *uri = gnome_vfs_volume_get_activation_uri (volume);      
280         if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
281                 printf ("DEBUG: %s: MMC1 card mounted.\n", __FUNCTION__);
282                 
283                 /* TODO: Just add an account and emit (and respond to) 
284                  * TnyAccountStore::accountinserted signal?
285                  */
286                 recreate_all_accounts (self);
287                 
288                 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
289                                NULL);
290         }
291         
292         g_free (uri);
293 }
294
295 static void
296 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor, 
297         GnomeVFSVolume *volume, gpointer user_data)
298 {
299         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
300         
301         /* Check whether this was the external MMC1 card: */
302         gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
303         if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
304                 printf ("DEBUG: %s: MMC1 card unmounted.\n", __FUNCTION__);
305                 
306                 /* TODO: Just add an account and emit (and respond to) 
307                  * TnyAccountStore::accountinserted signal?
308                  */
309                 recreate_all_accounts (self);
310                 
311                 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
312                                NULL);
313         }
314         
315         g_free (uri);
316 }
317
318 static void
319 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
320                     gpointer user_data)
321 {
322         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
323         
324         /* FIXME: make this more finegrained; changes do not really affect _all_
325          * accounts, and some do not affect tny accounts at all (such as 'last_update')
326          */
327         if (server_account)
328                 recreate_all_accounts (self);
329         
330         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
331                        account);
332 }
333
334 static void
335 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
336                     const gchar *key, gboolean server_account, gpointer user_data)
337
338 {
339         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
340         
341         /* Ignore the change if it's a change in the last_updated value */
342         if (g_str_has_suffix (key, MODEST_ACCOUNT_LAST_UPDATED))
343                 return;
344
345         /* FIXME: make this more finegrained; changes do not really affect _all_
346          * accounts, and some do not affect tny accounts at all (such as 'last_update')
347          */
348         if (server_account)
349                 recreate_all_accounts (self);
350
351         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
352                        account);
353 }
354
355
356 static ModestTnyAccountStore*
357 get_account_store_for_account (TnyAccount *account)
358 {
359         return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
360                                                            "account_store"));
361 }
362
363 /* This callback will be called by Tinymail when it needs the password
364  * from the user, for instance if the password was not remembered.
365  * Note that TnyAccount here will be the server account. */
366 static gchar*
367 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
368 {
369         /* Initialize the output parameter: */
370         if (cancel)
371           *cancel = FALSE;
372           
373         const gchar *key;
374         const TnyAccountStore *account_store;
375         ModestTnyAccountStore *self;
376         ModestTnyAccountStorePrivate *priv;
377         gchar *username = NULL;
378         gchar *pwd = NULL;
379         gpointer pwd_ptr;
380         gboolean already_asked;
381         
382         key           = tny_account_get_id (account);
383         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
384         
385         self = MODEST_TNY_ACCOUNT_STORE (account_store);
386         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
387         
388         /* This hash map stores passwords, including passwords that are not stored in gconf. */
389         /* is it in the hash? if it's already there, it must be wrong... */
390         pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
391                                    * type-punned ptrs...*/
392         already_asked = g_hash_table_lookup_extended (priv->password_hash,
393                                                       key,
394                                                       NULL,
395                                                       (gpointer*)&pwd_ptr);
396
397         /* if the password is not already there, try ModestConf */
398         if (!already_asked) {
399                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
400                                                       key, MODEST_ACCOUNT_PASSWORD, TRUE);
401                 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
402         }
403
404         /* if it was already asked, it must have been wrong, so ask again */
405         /* TODO: However, when we supply a wrong password to tinymail, 
406          * it seems to (at least sometimes) call our alert_func() instead of 
407          * asking for the password again.
408          */
409         if (already_asked || !pwd || strlen(pwd) == 0) {
410                 /* we don't have it yet. Get the password from the user */
411                 const gchar* account_id = tny_account_get_id (account);
412                 gboolean remember = FALSE;
413                 pwd = NULL;
414                 
415                 /* Note that we ignore the returned username here,
416                  * because it is enough that it will be stored in gconf 
417                  * by the signal handler. */
418                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
419                                account_id, /* server_account_name */
420                                &username, &pwd, cancel, &remember);
421                 
422                 if (!*cancel) {
423                         if (remember)
424                                 modest_account_mgr_set_string (priv->account_mgr,key,
425                                                                MODEST_ACCOUNT_PASSWORD,
426                                                                pwd, TRUE);
427                         /* We need to dup the string even knowing that
428                            it's already a dup of the contents of an
429                            entry, because it if it's wrong, then camel
430                            will free it */
431                         g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
432                 } else {
433                         g_hash_table_remove (priv->password_hash, key);
434                         
435                         g_free (pwd);
436                         pwd = NULL;
437                 }
438
439                 g_free (username);
440                 username = NULL;
441         } else
442                 *cancel = FALSE;
443  
444     /* printf("  DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
445         
446         return pwd;
447 }
448
449 /* tinymail calls this if the connection failed due to an incorrect password.
450  * And it seems to call this for any general connection failure. */
451 static void
452 forget_password (TnyAccount *account)
453 {
454         printf ("DEBUG: %s\n", __FUNCTION__);
455         ModestTnyAccountStore *self;
456         ModestTnyAccountStorePrivate *priv;
457         const TnyAccountStore *account_store;
458         gchar *pwd;
459         const gchar *key;
460         
461         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
462         self = MODEST_TNY_ACCOUNT_STORE (account_store);
463         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
464         key  = tny_account_get_id (account);
465
466         /* Do not remove the key, this will allow us to detect that we
467            have already asked for it at least once */
468         pwd = g_hash_table_lookup (priv->password_hash, key);
469         if (pwd) {
470                 memset (pwd, 0, strlen (pwd));
471                 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
472         }
473
474         /* Remove from configuration system */
475         modest_account_mgr_unset (priv->account_mgr,
476                                   key, MODEST_ACCOUNT_PASSWORD, TRUE);
477 }
478
479
480 static void
481 modest_tny_account_store_finalize (GObject *obj)
482 {
483         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
484         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
485         
486         //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
487
488         g_free (priv->cache_dir);
489         priv->cache_dir = NULL;
490         
491         if (priv->password_hash) {
492                 g_hash_table_destroy (priv->password_hash);
493                 priv->password_hash = NULL;
494         }
495
496         if (priv->account_mgr) {
497                 g_object_unref (G_OBJECT(priv->account_mgr));
498                 priv->account_mgr = NULL;
499         }
500
501         if (priv->device) {
502                 g_object_unref (G_OBJECT(priv->device));
503                 priv->device = NULL;
504         }
505
506         /* this includes the local folder */
507         account_list_free (priv->store_accounts);
508         priv->store_accounts = NULL;
509         
510         account_list_free (priv->transport_accounts);
511         priv->transport_accounts = NULL;
512
513         if (priv->session) {
514                 camel_object_unref (CAMEL_OBJECT(priv->session));
515                 priv->session = NULL;
516         }
517         
518         G_OBJECT_CLASS(parent_class)->finalize (obj);
519 }
520
521
522 ModestTnyAccountStore*
523 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
524
525         GObject *obj;
526         ModestTnyAccountStorePrivate *priv;
527         TnyList *list; 
528         
529         g_return_val_if_fail (account_mgr, NULL);
530         g_return_val_if_fail (device, NULL);
531
532         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
533         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
534
535         priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
536         priv->device = g_object_ref (device);
537         
538         priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
539         
540         tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
541         /* FIXME: unref this in the end? */
542         tny_session_camel_set_async_connecting (priv->session, TRUE);
543         
544         /* force a cache fill... ugly */
545         list = TNY_LIST(tny_simple_list_new());
546         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
547                                         TNY_ACCOUNT_STORE_BOTH);
548         g_object_unref(list);
549         
550         /* Connect signals */
551         g_signal_connect (G_OBJECT(account_mgr), "account_changed",
552                                        G_CALLBACK (on_account_changed), obj);
553         g_signal_connect (G_OBJECT(account_mgr), "account_removed",
554                                        G_CALLBACK (on_account_removed), obj);
555
556         return MODEST_TNY_ACCOUNT_STORE(obj);
557 }
558
559 /** Fill the TnyList from the appropriate cached GSList of accounts. */
560 static void
561 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
562 {
563         ModestTnyAccountStorePrivate *priv;
564         GSList                       *accounts, *cursor;
565         
566         priv     = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
567         accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
568
569         cursor = accounts;
570         while (cursor) {
571                 if (cursor->data) {
572                         GObject *object = G_OBJECT(cursor->data);
573                         tny_list_prepend (list, object);
574                 }
575                         
576                 cursor = cursor->next;
577         }
578 }
579
580 static void
581 create_per_account_local_outbox_folders (TnyAccountStore *self)
582 {
583         g_return_if_fail (self);
584         
585         ModestTnyAccountStorePrivate *priv = 
586                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
587         
588         /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
589         
590         GSList *accounts = NULL;
591         
592         GSList *account_names  = modest_account_mgr_account_names (priv->account_mgr, 
593                 TRUE /* including disabled accounts */);
594         
595         GSList *iter = NULL;
596         for (iter = account_names; iter; iter = g_slist_next (iter)) {
597                 
598                 const gchar* account_name = (const gchar*)iter->data;
599                 
600                 /* Create a per-account local outbox folder (a _store_ account) 
601                  * for each _transport_ account: */
602                 TnyAccount *tny_account_outbox =
603                         modest_tny_account_new_for_per_account_local_outbox_folder (
604                                 priv->account_mgr, account_name, priv->session);
605                                 
606                 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
607         };
608         
609         g_slist_free (account_names);
610         
611         priv->store_accounts_outboxes = accounts;
612 }
613
614 /* This function fills the TnyList, and also stores a GSList of the accounts,
615  * for caching purposes. It creates the TnyAccount objects if necessary.
616  * The @list parameter may be NULL, if you just want to fill the cache.
617  */
618 static void
619 get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
620 {
621         g_return_if_fail (self);
622         
623         /* printf ("DEBUG: %s: list=%p, type=%d\n", __FUNCTION__, list, type); */
624                 
625         ModestTnyAccountStorePrivate *priv = 
626                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
627                 
628         /* Do nothing if the accounts are already cached: */
629         if (type == TNY_ACCOUNT_TYPE_STORE) {
630                 if (priv->store_accounts)
631                         return;
632         } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
633                 if (priv->transport_accounts)
634                         return;
635         }
636         
637         GSList                       *account_names = NULL, *cursor = NULL;
638         GSList                       *accounts = NULL;
639
640         /* These are account names, not server_account names */
641         account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
642                 
643         for (cursor = account_names; cursor; cursor = cursor->next) {
644                 
645                 gchar *account_name = (gchar*)cursor->data;
646                 
647                 /* we get the server_accounts for enabled accounts */
648                 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
649                                 
650                         /* Add the account: */
651                         TnyAccount *tny_account = 
652                                 modest_tny_account_new_from_account (priv->account_mgr,
653                                                                      account_name,
654                                                                      type, priv->session,
655                                                                      get_password,
656                                                                      forget_password);
657                         if (tny_account) {
658                                 g_object_set_data (G_OBJECT(tny_account), "account_store",
659                                                    (gpointer)self);
660                                 if (list)
661                                         tny_list_prepend (list, G_OBJECT(tny_account));
662                                 
663                                 accounts = g_slist_append (accounts, tny_account); /* cache it */               
664                         } else
665                                 g_printerr ("modest: failed to create account for %s\n",
666                                             account_name);
667                         }
668         }
669         
670         if (type == TNY_ACCOUNT_TYPE_STORE) {
671                 /* Also add the local folder pseudo-account: */
672                 TnyAccount *tny_account =
673                         modest_tny_account_new_for_local_folders (priv->account_mgr, 
674                                 priv->session, NULL);
675                 if (list)
676                         tny_list_prepend (list, G_OBJECT(tny_account));
677                 accounts = g_slist_append (accounts, tny_account); /* cache it */
678                 
679                 
680                 /* Also add the Memory card account if it is mounted: */
681                 gboolean mmc_is_mounted = FALSE;
682                 GnomeVFSVolumeMonitor* monitor = 
683                         gnome_vfs_get_volume_monitor();
684                 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
685                 GList *iter = list_volumes;
686                 while (iter) {
687                         GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
688                         if (volume) {
689                                 if (!mmc_is_mounted) {
690                                         gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
691                                         if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
692                                                 mmc_is_mounted = TRUE;
693                                         }
694                                         g_free (uri);
695                                 }
696                                 
697                                 gnome_vfs_volume_unref(volume);
698                         }
699                         
700                         iter = g_list_next (iter);
701                 }
702                 g_list_free (list_volumes);
703                 
704                 if (mmc_is_mounted) {
705                         TnyAccount *tny_account =
706                                 modest_tny_account_new_for_local_folders (priv->account_mgr, 
707                                         priv->session, MODEST_MCC1_VOLUMEPATH);
708                         if (list)
709                                 tny_list_prepend (list, G_OBJECT(tny_account));
710                         accounts = g_slist_append (accounts, tny_account); /* cache it */
711                 }
712         }
713
714         /* And add the connection-specific transport accounts, if any.
715          * Note that these server account instances might never be used 
716          * if their connections are never active: */
717         /* Look at each modest account: */
718         if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
719                 GSList *iter_account_names = account_names;
720                 while (iter_account_names) {
721                         const gchar* account_name = (const gchar*)(iter_account_names->data);
722                         GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
723                                 account_name, 
724                                 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
725                                 MODEST_CONF_VALUE_STRING, FALSE);
726                                 
727                         /* Look at each connection-specific transport account for the 
728                          * modest account: */
729                         GSList *iter = list_specifics;
730                         while (iter) {
731                                 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
732                                 iter = g_slist_next (iter);
733                                 if (iter) {
734                                         const gchar* transport_account_name = (const gchar*)(iter->data);
735                                         if (transport_account_name) {
736                                                 TnyAccount * tny_account = NULL;
737                                                 /* Add the account: */
738                                                 tny_account = modest_tny_account_new_from_server_account_name (
739                                                         priv->account_mgr, transport_account_name);
740                                                 if (tny_account) {
741                                                         g_object_set_data (G_OBJECT(tny_account), "account_store",
742                                                                            (gpointer)self);
743                                                         if (list)
744                                                                 tny_list_prepend (list, G_OBJECT(tny_account));
745                                                         
746                                                         accounts = g_slist_append (accounts, tny_account); /* cache it */               
747                                                 } else
748                                                         g_printerr ("modest: failed to create smtp-specific account for %s\n",
749                                                                     transport_account_name);
750                                         }
751                                 }
752                                 
753                                 iter = g_slist_next (iter);
754                         }
755                         
756                         iter_account_names = g_slist_next (iter_account_names);
757                 }               
758         }
759         
760         g_slist_free (account_names);
761         account_names = NULL;
762         
763         /* TODO: Delete the strings in the GSList */
764         
765         
766         /* We also create a per-account local outbox folder (a _store_ account) 
767          * for each _transport_ account. */
768         if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
769                 /* Now would be a good time to create the per-account local outbox folder 
770                  * _store_ accounts corresponding to each transport account: */
771                 if (!priv->store_accounts_outboxes) {
772                         create_per_account_local_outbox_folders (self);
773                 }
774         }
775         
776         /* But we only return the per-account local outbox folder when 
777          * _store_ accounts are requested. */
778         if (type == TNY_ACCOUNT_TYPE_STORE) {
779                 /* Create them if necessary, 
780                  * (which also requires creating the transport accounts, 
781                  * if necessary.) */
782                 if (!priv->store_accounts_outboxes) {
783                         create_per_account_local_outbox_folders (self);
784                 }
785         
786                 /* Add them to the TnyList: */
787                 if (priv->store_accounts_outboxes) {
788                         GSList *iter = NULL;
789                         for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
790                                 TnyAccount *outbox_account = (TnyAccount*)iter->data;
791                                 if (list && outbox_account)
792                                         tny_list_prepend (list,  G_OBJECT(outbox_account));
793                                         
794                                 g_object_ref (outbox_account);
795                                 accounts = g_slist_append (accounts, outbox_account);
796                         }
797                         
798                         account_list_free (priv->store_accounts_outboxes);
799                         priv->store_accounts_outboxes = NULL;
800                 }
801         }
802                 
803         if (type == TNY_ACCOUNT_TYPE_STORE) {
804                         /* Store the cache: */
805                         priv->store_accounts = accounts;
806         } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
807                         /* Store the cache: */
808                         priv->transport_accounts = accounts;
809         }
810 }       
811
812
813 static void
814 modest_tny_account_store_get_accounts  (TnyAccountStore *self, TnyList *list,
815                                         TnyGetAccountsRequestType request_type)
816 {
817         ModestTnyAccountStorePrivate *priv;
818         
819         g_return_if_fail (self);
820         g_return_if_fail (TNY_IS_LIST(list));
821         
822         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
823         
824         if (request_type == TNY_ACCOUNT_STORE_BOTH) {
825                 modest_tny_account_store_get_accounts (self, list,
826                                                        TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
827                 modest_tny_account_store_get_accounts (self, list,
828                                                        TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
829                 return;
830         }
831         
832         if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS)  {
833                 if (!priv->store_accounts)
834                         get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
835                 else
836                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
837                 
838         } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
839                 if (!priv->transport_accounts)
840                         get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
841                 else
842                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
843         } else
844                 g_return_if_reached (); /* incorrect req type */
845 }
846
847
848 static const gchar*
849 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
850 {
851         ModestTnyAccountStorePrivate *priv;
852         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
853         
854         if (!priv->cache_dir)
855                 priv->cache_dir = g_build_filename (g_get_home_dir(), 
856                                                     MODEST_DIR, MODEST_CACHE_DIR, NULL);
857         return priv->cache_dir;
858 }
859
860
861 /*
862  * callers need to unref
863  */
864 static TnyDevice*
865 modest_tny_account_store_get_device (TnyAccountStore *self)
866 {
867         ModestTnyAccountStorePrivate *priv;
868
869         g_return_val_if_fail (self, NULL);
870         
871         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
872
873         if (priv->device)
874                 return g_object_ref (G_OBJECT(priv->device));
875         else
876                 return NULL;
877 }
878
879
880 static TnyAccount*
881 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
882 {
883         TnyAccount *account = NULL;
884         ModestTnyAccountStorePrivate *priv;     
885         GSList *cursor;
886         
887         g_return_val_if_fail (self, NULL);
888         g_return_val_if_fail (url_string, NULL);
889         
890         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
891
892         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
893                 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
894                         account = TNY_ACCOUNT(cursor->data);
895                         break;
896                 }
897         }
898
899         if (!account) {
900                 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
901                         if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
902                                 account = TNY_ACCOUNT(cursor->data);
903                                 break;
904                         }
905                 }
906         }
907
908         if (account)
909                 g_object_ref (G_OBJECT(account));
910
911         return account;
912 }
913
914
915
916 static gboolean
917 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
918                                 gboolean question, const GError *error)
919 {
920         g_return_val_if_fail (error, FALSE);
921
922         if ((error->domain != TNY_ACCOUNT_ERROR) 
923                 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
924                 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s", 
925                         __FUNCTION__, error->domain, error->message); 
926                 return FALSE;
927         }
928         
929         /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
930         
931
932         /* const gchar *prompt = NULL; */
933         gchar *prompt = NULL;
934         switch (error->code) {
935                 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
936                 /* The tinymail camel implementation just sends us this for almost 
937                  * everything, so we have to guess at the cause.
938                  * It could be a wrong password, or inability to resolve a hostname, 
939                  * or lack of network, or something entirely different: */
940                 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT: 
941                     g_debug ("%s: Handling GError domain=%d, code=%d, message=%s", 
942                                 __FUNCTION__, error->domain, error->code, error->message);
943                         
944                         /* TODO: Remove the internal error message for the real release.
945                          * This is just so the testers can give us more information: */
946                         /* prompt = _("Modest account not yet fully configured."); */
947                         prompt = g_strdup_printf(_("Modest account not yet fully configured. Error=%s"), 
948                                 error->message);
949                                 
950                         /* TODO: If we can ever determine that the problem is a wrong password:
951                          * In this case, the UI spec wants us to show a banner, and then 
952                          * open the Account Settings dialog. */
953                         /* Note: Sometimes, the get_password() function seems to be called again 
954                          * when a password is wrong, but sometimes this alert_func is called. */
955                         #if 0
956                         GtkWidget *parent_widget = 
957                                 GTK_WIDGET (
958                                         modest_window_mgr_get_main_window (
959                                                 modest_runtime_get_window_mgr ()));
960                         
961                         hildon_banner_show_information (
962                                 parent_widget,
963                                 NULL /* icon name */,
964                                 _("mcen_ib_username_pw_incorrect") );
965                                 
966                         /* Show the Account Settings window: */
967                         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
968                         /* TODO: Get the account somehow. Maybe tinymail should send it with the signal. */
969                         const gchar* modest_account_name = 
970                                 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
971                         g_assert (modest_account_name);
972                         modest_account_settings_dialog_set_account_name (dialog, 
973                                 modest_account_name);
974                         
975                         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
976                         gtk_dialog_run (GTK_DIALOG (dialog));
977                         gtk_widget_destroy (GTK_WIDGET (dialog));
978                         #endif
979                          
980                         break;
981                         
982                 //TODO: We have started receiving errors of 
983                 //domain=TNY_ACCOUNT_ERROR, code=TNY_ACCOUNT_ERROR_TRY_CONNECT, messagae="Canceled".
984                 //If this is really a result of us cancelling our own operation then 
985                 //a) this probably shouldn't be an error, and
986                 //b) should have its own error code.
987                 
988                 default:
989                         g_warning ("%s: Unhandled GError code: %d, message=%s", 
990                                 __FUNCTION__, error->code, error->message);
991                         prompt = NULL;
992                 break;
993         }
994         
995         if (!prompt)
996                 return FALSE;
997
998 #ifdef MODEST_PLATFORM_MAEMO
999         /* The Tinymail documentation says that we should show Yes and No buttons, 
1000          * when it is a question.
1001          * Obviously, we need tinymail to use more specific error codes instead,
1002          * so we know what buttons to show. */
1003          GtkWidget *dialog = NULL;
1004          if (question) {
1005                 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL, 
1006                         prompt));
1007          } else {
1008                 dialog = GTK_WIDGET (hildon_note_new_information (NULL, 
1009                         prompt));
1010          }
1011 #else
1012
1013         GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1014         switch (type)
1015         {
1016                 case TNY_ALERT_TYPE_INFO:
1017                 gtktype = GTK_MESSAGE_INFO;
1018                 break;
1019                 case TNY_ALERT_TYPE_WARNING:
1020                 gtktype = GTK_MESSAGE_WARNING;
1021                 break;
1022                 case TNY_ALERT_TYPE_ERROR:
1023                 default:
1024                 gtktype = GTK_MESSAGE_ERROR;
1025                 break;
1026         }
1027         
1028         GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
1029                 gtktype, GTK_BUTTONS_YES_NO, prompt);
1030 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1031
1032         gboolean retval = TRUE;
1033         const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1034         if (question) {
1035                 retval = (response == GTK_RESPONSE_YES) ||
1036                                  (response == GTK_RESPONSE_OK); 
1037         }
1038
1039         gtk_widget_destroy (dialog);
1040         
1041         /* TODO: Don't free this when we no longer strdup the message for testers. */
1042         g_free (prompt);
1043
1044
1045         /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1046         return retval;
1047 }
1048
1049
1050 static void
1051 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1052 {
1053         TnyAccountStoreIface *klass;
1054
1055         g_return_if_fail (g);
1056
1057         klass = (TnyAccountStoreIface *)g;
1058
1059         klass->get_accounts_func =
1060                 modest_tny_account_store_get_accounts;
1061         klass->get_cache_dir_func =
1062                 modest_tny_account_store_get_cache_dir;
1063         klass->get_device_func =
1064                 modest_tny_account_store_get_device;
1065         klass->alert_func =
1066                 modest_tny_account_store_alert;
1067         klass->find_account_func =
1068                 modest_tny_account_store_find_account_by_url;
1069 }
1070
1071 void
1072 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1073                                             ModestTnyGetPassFunc func)
1074 {
1075         /* not implemented, we use signals */
1076         g_printerr ("modest: set_get_pass_func not implemented\n");
1077 }
1078
1079 TnySessionCamel*
1080 modest_tny_account_store_get_session  (TnyAccountStore *self)
1081 {
1082         g_return_val_if_fail (self, NULL);
1083         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1084 }
1085
1086
1087 TnyAccount*
1088 modest_tny_account_store_get_tny_account_by_id  (ModestTnyAccountStore *self, const gchar *id)
1089 {
1090         TnyAccount *account = NULL;
1091         ModestTnyAccountStorePrivate *priv;     
1092         GSList *cursor;
1093
1094         g_return_val_if_fail (self, NULL);
1095         g_return_val_if_fail (id, NULL);
1096         
1097         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1098
1099
1100         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1101                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1102                 if (acc_id && strcmp (acc_id, id) == 0) {
1103                         account = TNY_ACCOUNT(cursor->data);
1104                         break;
1105                 }
1106         }
1107                 
1108         /* if we already found something, no need to search the transport accounts */
1109         for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1110                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1111                 if (acc_id && strcmp (acc_id, id) == 0) {
1112                         account = TNY_ACCOUNT(cursor->data);
1113                         break;
1114                 }
1115         }
1116
1117         if (account)
1118                 g_object_ref (G_OBJECT(account));
1119         
1120         return account;
1121 }
1122
1123 TnyAccount*
1124 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
1125                                                      const gchar *account_name,
1126                                                      TnyAccountType type)
1127 {
1128         TnyAccount *account = NULL;
1129         gchar *id = NULL;
1130         ModestTnyAccountStorePrivate *priv;     
1131
1132         g_return_val_if_fail (self, NULL);
1133         g_return_val_if_fail (account_name, NULL);
1134         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1135                               NULL);
1136         
1137         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1138
1139         /* Special case for the local account */
1140         if (!strcmp (account_name, MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID) && 
1141             type == TNY_ACCOUNT_TYPE_STORE) {
1142                 id = g_strdup (MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID);
1143         } else {
1144                 ModestAccountData *account_data;
1145
1146                 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
1147                 if (!account_data) {
1148                         g_printerr ("modest: %s: cannot get account data for account '%s'\n", __FUNCTION__, account_name);
1149                         return NULL;
1150                 }
1151
1152                 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1153                         id = g_strdup (account_data->store_account->account_name);
1154                 else if (account_data->transport_account)
1155                         id = g_strdup (account_data->transport_account->account_name);
1156
1157                 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1158         }
1159
1160         if (!id)
1161                 g_printerr ("modest: could not get an id for account %s\n",
1162                             account_name);
1163         else    
1164                 account = modest_tny_account_store_get_tny_account_by_id (self, id);
1165
1166         if (!account)
1167                 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1168                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1169                             account_name, id ? id : "<none>");
1170
1171         return account; 
1172 }
1173
1174 static TnyAccount*
1175 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1176                                                          const gchar *account_name)
1177 {
1178         /* Get the current connection: */
1179         TnyDevice *device = modest_runtime_get_device ();
1180         
1181         if (!tny_device_is_online (device))
1182                 return NULL;
1183
1184 #ifdef MODEST_PLATFORM_MAEMO
1185         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1186         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);    
1187         const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1188         /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1189         if (!iap_id)
1190                 return NULL;
1191                 
1192         ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1193         if (!connection)
1194                 return NULL;
1195                 
1196         const gchar *connection_name = con_ic_iap_get_name (connection);
1197         /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1198         if (!connection_name)
1199                 return NULL;
1200         
1201         /*  Get the connection-specific transport acccount, if any: */
1202         ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1203         gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager, 
1204                 account_name, connection_name);
1205
1206         /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1207         if (!server_account_name) {
1208                 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1209         }
1210                 
1211         TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
1212
1213         /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1214         g_free (server_account_name);   
1215
1216         /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1217         g_object_unref (connection);
1218         
1219         return account;
1220 #else
1221         return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1222 #endif /* MODEST_PLATFORM_MAEMO */
1223 }
1224
1225                                                                  
1226 TnyAccount*
1227 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1228                                                                     const gchar *account_name)
1229 {
1230         /*  Get the connection-specific transport acccount, if any: */
1231         TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1232                         
1233         /* If there is no connection-specific transport account (the common case), 
1234          * just get the regular transport account: */
1235         if (!account) {
1236                 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1237                 account = modest_tny_account_store_get_tny_account_by_account (self, account_name, 
1238                                                      TNY_ACCOUNT_TYPE_TRANSPORT);
1239         }
1240                              
1241         return account;
1242 }
1243
1244 gboolean modest_tny_folder_store_is_virtual_local_folders (TnyFolderStore *self)
1245 {
1246         /* We should make this more sophisticated if we ever use ModestTnySimpleFolderStore 
1247          * for anything else. */
1248         return MODEST_IS_TNY_SIMPLE_FOLDER_STORE (self);
1249 }