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