6d18963939dcac0d41bbe56546df6103139e0059
[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         } else
337                 g_printerr ("modest: cannot find server account for %s", account);
338         
339         /* FIXME: make this more finegrained; changes do not
340          * really affect _all_ accounts, and some do not
341          * affect tny accounts at all (such as 'last_update')
342          */
343         recreate_all_accounts (self);
344         
345         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
346                        account);
347 }
348
349 static void
350 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account,
351                     const GSList *keys, gboolean server_account, gpointer user_data)
352
353 {
354         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
355         
356         /* Ignore the change if it's a change in the last_updated value */
357         if (g_slist_length ((GSList *)keys) == 1 &&
358             g_str_has_suffix ((const gchar *) keys->data, MODEST_ACCOUNT_LAST_UPDATED))
359                 return;
360
361         /* FIXME: make this more finegrained; changes do not really affect _all_
362          * accounts, and some do not affect tny accounts at all (such as 'last_update')
363          */
364         if (server_account)
365                 recreate_all_accounts (self);
366
367         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
368                        account);
369 }
370
371
372 static ModestTnyAccountStore*
373 get_account_store_for_account (TnyAccount *account)
374 {
375         return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
376                                                            "account_store"));
377 }
378
379 /* This callback will be called by Tinymail when it needs the password
380  * from the user, for instance if the password was not remembered.
381  * Note that TnyAccount here will be the server account. */
382 static gchar*
383 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
384 {
385         /* Initialize the output parameter: */
386         if (cancel)
387           *cancel = FALSE;
388           
389         const gchar *key;
390         const TnyAccountStore *account_store;
391         ModestTnyAccountStore *self;
392         ModestTnyAccountStorePrivate *priv;
393         gchar *username = NULL;
394         gchar *pwd = NULL;
395         gpointer pwd_ptr;
396         gboolean already_asked;
397         
398         key           = tny_account_get_id (account);
399         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
400         
401         self = MODEST_TNY_ACCOUNT_STORE (account_store);
402         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
403         
404         /* This hash map stores passwords, including passwords that are not stored in gconf. */
405         /* is it in the hash? if it's already there, it must be wrong... */
406         pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
407                                    * type-punned ptrs...*/
408         already_asked = g_hash_table_lookup_extended (priv->password_hash,
409                                                       key,
410                                                       NULL,
411                                                       (gpointer*)&pwd_ptr);
412
413         /* if the password is not already there, try ModestConf */
414         if (!already_asked) {
415                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
416                                                       key, MODEST_ACCOUNT_PASSWORD, TRUE);
417                 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
418         }
419
420         /* if it was already asked, it must have been wrong, so ask again */
421         /* TODO: However, when we supply a wrong password to tinymail, 
422          * it seems to (at least sometimes) call our alert_func() instead of 
423          * asking for the password again.
424          */
425         if (already_asked || !pwd || strlen(pwd) == 0) {
426                 /* we don't have it yet. Get the password from the user */
427                 const gchar* account_id = tny_account_get_id (account);
428                 gboolean remember = FALSE;
429                 pwd = NULL;
430                 
431                 /* Note that we ignore the returned username here,
432                  * because it is enough that it will be stored in gconf 
433                  * by the signal handler. */
434                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
435                                account_id, /* server_account_name */
436                                &username, &pwd, cancel, &remember);
437                 
438                 if (!*cancel) {
439                         if (remember)
440                                 modest_account_mgr_set_string (priv->account_mgr,key,
441                                                                MODEST_ACCOUNT_PASSWORD,
442                                                                pwd, TRUE);
443                         /* We need to dup the string even knowing that
444                            it's already a dup of the contents of an
445                            entry, because it if it's wrong, then camel
446                            will free it */
447                         g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
448                 } else {
449                         g_hash_table_remove (priv->password_hash, key);
450                         
451                         g_free (pwd);
452                         pwd = NULL;
453                 }
454
455                 g_free (username);
456                 username = NULL;
457         } else
458                 *cancel = FALSE;
459  
460     /* printf("  DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
461         
462         return pwd;
463 }
464
465 /* tinymail calls this if the connection failed due to an incorrect password.
466  * And it seems to call this for any general connection failure. */
467 static void
468 forget_password (TnyAccount *account)
469 {
470         printf ("DEBUG: %s\n", __FUNCTION__);
471         ModestTnyAccountStore *self;
472         ModestTnyAccountStorePrivate *priv;
473         const TnyAccountStore *account_store;
474         gchar *pwd;
475         const gchar *key;
476         
477         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
478         self = MODEST_TNY_ACCOUNT_STORE (account_store);
479         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
480         key  = tny_account_get_id (account);
481
482         /* Do not remove the key, this will allow us to detect that we
483            have already asked for it at least once */
484         pwd = g_hash_table_lookup (priv->password_hash, key);
485         if (pwd) {
486                 memset (pwd, 0, strlen (pwd));
487                 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
488         }
489
490         /* Remove from configuration system */
491         modest_account_mgr_unset (priv->account_mgr,
492                                   key, MODEST_ACCOUNT_PASSWORD, TRUE);
493 }
494
495
496 static void
497 modest_tny_account_store_finalize (GObject *obj)
498 {
499         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
500         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
501         
502         //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
503
504         g_free (priv->cache_dir);
505         priv->cache_dir = NULL;
506         
507         if (priv->password_hash) {
508                 g_hash_table_destroy (priv->password_hash);
509                 priv->password_hash = NULL;
510         }
511
512         if (priv->account_mgr) {
513                 g_object_unref (G_OBJECT(priv->account_mgr));
514                 priv->account_mgr = NULL;
515         }
516
517         if (priv->device) {
518                 g_object_unref (G_OBJECT(priv->device));
519                 priv->device = NULL;
520         }
521
522         /* this includes the local folder */
523         account_list_free (priv->store_accounts);
524         priv->store_accounts = NULL;
525         
526         account_list_free (priv->transport_accounts);
527         priv->transport_accounts = NULL;
528
529         if (priv->session) {
530                 camel_object_unref (CAMEL_OBJECT(priv->session));
531                 priv->session = NULL;
532         }
533         
534         G_OBJECT_CLASS(parent_class)->finalize (obj);
535 }
536
537
538 ModestTnyAccountStore*
539 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
540
541         GObject *obj;
542         ModestTnyAccountStorePrivate *priv;
543         TnyList *list; 
544         
545         g_return_val_if_fail (account_mgr, NULL);
546         g_return_val_if_fail (device, NULL);
547
548         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
549         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
550
551         priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
552         priv->device = g_object_ref (device);
553         
554         priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
555         
556         tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
557         /* FIXME: unref this in the end? */
558         tny_session_camel_set_async_connecting (priv->session, TRUE);
559         
560         /* force a cache fill... ugly */
561         list = TNY_LIST(tny_simple_list_new());
562         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
563                                         TNY_ACCOUNT_STORE_BOTH);
564         g_object_unref(list);
565         
566         /* Connect signals */
567         g_signal_connect (G_OBJECT(account_mgr), "account_changed",
568                                        G_CALLBACK (on_account_changed), obj);
569         g_signal_connect (G_OBJECT(account_mgr), "account_removed",
570                                        G_CALLBACK (on_account_removed), obj);
571
572         return MODEST_TNY_ACCOUNT_STORE(obj);
573 }
574
575 /** Fill the TnyList from the appropriate cached GSList of accounts. */
576 static void
577 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
578 {
579         ModestTnyAccountStorePrivate *priv;
580         GSList                       *accounts, *cursor;
581         
582         priv     = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
583         accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
584
585         cursor = accounts;
586         while (cursor) {
587                 if (cursor->data) {
588                         GObject *object = G_OBJECT(cursor->data);
589                         tny_list_prepend (list, object);
590                 }
591                         
592                 cursor = cursor->next;
593         }
594 }
595
596 static void
597 create_per_account_local_outbox_folders (TnyAccountStore *self)
598 {
599         g_return_if_fail (self);
600         
601         ModestTnyAccountStorePrivate *priv = 
602                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
603         
604         /* printf("DEBUG: %s: priv->store_accounts_outboxes = %p\n", __FUNCTION__, priv->store_accounts_outboxes); */
605         
606         GSList *accounts = NULL;
607         
608         GSList *account_names  = modest_account_mgr_account_names (priv->account_mgr, 
609                 TRUE /* including disabled accounts */);
610         
611         GSList *iter = NULL;
612         for (iter = account_names; iter; iter = g_slist_next (iter)) {
613                 
614                 const gchar* account_name = (const gchar*)iter->data;
615                 
616                 /* Create a per-account local outbox folder (a _store_ account) 
617                  * for each _transport_ account: */
618                 TnyAccount *tny_account_outbox =
619                         modest_tny_account_new_for_per_account_local_outbox_folder (
620                                 priv->account_mgr, account_name, priv->session);
621                                 
622                 accounts = g_slist_append (accounts, tny_account_outbox); /* cache it */
623         };
624         
625         g_slist_free (account_names);
626         
627         priv->store_accounts_outboxes = accounts;
628 }
629
630 /* This function fills the TnyList, and also stores a GSList of the accounts,
631  * for caching purposes. It creates the TnyAccount objects if necessary.
632  * The @list parameter may be NULL, if you just want to fill the cache.
633  */
634 static void
635 get_server_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
636 {
637         g_return_if_fail (self);
638         
639         /* printf ("DEBUG: %s: list=%p, type=%d\n", __FUNCTION__, list, type); */
640                 
641         ModestTnyAccountStorePrivate *priv = 
642                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
643                 
644         /* Do nothing if the accounts are already cached: */
645         if (type == TNY_ACCOUNT_TYPE_STORE) {
646                 if (priv->store_accounts)
647                         return;
648         } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
649                 if (priv->transport_accounts)
650                         return;
651         }
652         
653         GSList                       *account_names = NULL, *cursor = NULL;
654         GSList                       *accounts = NULL;
655
656         /* These are account names, not server_account names */
657         account_names = modest_account_mgr_account_names (priv->account_mgr,FALSE);
658                 
659         for (cursor = account_names; cursor; cursor = cursor->next) {
660                 
661                 gchar *account_name = (gchar*)cursor->data;
662                 
663                 /* we get the server_accounts for enabled accounts */
664                 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
665                                 
666                         /* Add the account: */
667                         TnyAccount *tny_account = 
668                                 modest_tny_account_new_from_account (priv->account_mgr,
669                                                                      account_name,
670                                                                      type, priv->session,
671                                                                      get_password,
672                                                                      forget_password);
673                         if (tny_account) {
674                                 g_object_set_data (G_OBJECT(tny_account), "account_store",
675                                                    (gpointer)self);
676                                 if (list)
677                                         tny_list_prepend (list, G_OBJECT(tny_account));
678                                 
679                                 accounts = g_slist_append (accounts, tny_account); /* cache it */               
680                         } else
681                                 g_printerr ("modest: failed to create account for %s\n",
682                                             account_name);
683                         }
684         }
685         
686         if (type == TNY_ACCOUNT_TYPE_STORE) {           
687                 /* Also add the Memory card account if it is mounted: */
688                 gboolean mmc_is_mounted = FALSE;
689                 GnomeVFSVolumeMonitor* monitor = 
690                         gnome_vfs_get_volume_monitor();
691                 GList* list_volumes = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
692                 GList *iter = list_volumes;
693                 while (iter) {
694                         GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
695                         if (volume) {
696                                 if (!mmc_is_mounted) {
697                                         gchar *uri = gnome_vfs_volume_get_activation_uri (volume);
698                                         if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
699                                                 mmc_is_mounted = TRUE;
700                                         }
701                                         g_free (uri);
702                                 }
703                                 
704                                 gnome_vfs_volume_unref(volume);
705                         }
706                         
707                         iter = g_list_next (iter);
708                 }
709                 g_list_free (list_volumes);
710                 
711                 if (mmc_is_mounted) {
712                         TnyAccount *tny_account =
713                                 modest_tny_account_new_for_local_folders (priv->account_mgr, 
714                                         priv->session, MODEST_MCC1_VOLUMEPATH);
715                         if (list)
716                                 tny_list_prepend (list, G_OBJECT(tny_account));
717                         accounts = g_slist_append (accounts, tny_account); /* cache it */
718                 }
719         }
720
721         /* And add the connection-specific transport accounts, if any.
722          * Note that these server account instances might never be used 
723          * if their connections are never active: */
724         /* Look at each modest account: */
725         if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
726                 GSList *iter_account_names = account_names;
727                 while (iter_account_names) {
728                         const gchar* account_name = (const gchar*)(iter_account_names->data);
729                         GSList *list_specifics = modest_account_mgr_get_list (priv->account_mgr,
730                                 account_name, 
731                                 MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
732                                 MODEST_CONF_VALUE_STRING, FALSE);
733                                 
734                         /* Look at each connection-specific transport account for the 
735                          * modest account: */
736                         GSList *iter = list_specifics;
737                         while (iter) {
738                                 /* const gchar* this_connection_name = (const gchar*)(iter->data); */
739                                 iter = g_slist_next (iter);
740                                 if (iter) {
741                                         const gchar* transport_account_name = (const gchar*)(iter->data);
742                                         if (transport_account_name) {
743                                                 TnyAccount * tny_account = NULL;
744                                                 /* Add the account: */
745                                                 tny_account = modest_tny_account_new_from_server_account_name (
746                                                         priv->account_mgr, transport_account_name);
747                                                 if (tny_account) {
748                                                         g_object_set_data (G_OBJECT(tny_account), "account_store",
749                                                                            (gpointer)self);
750                                                         if (list)
751                                                                 tny_list_prepend (list, G_OBJECT(tny_account));
752                                                         
753                                                         accounts = g_slist_append (accounts, tny_account); /* cache it */               
754                                                 } else
755                                                         g_printerr ("modest: failed to create smtp-specific account for %s\n",
756                                                                     transport_account_name);
757                                         }
758                                 }
759                                 
760                                 iter = g_slist_next (iter);
761                         }
762                         
763                         iter_account_names = g_slist_next (iter_account_names);
764                 }               
765         }
766         
767         g_slist_free (account_names);
768         account_names = NULL;
769         
770         /* TODO: Delete the strings in the GSList */
771         
772         
773         /* We also create a per-account local outbox folder (a _store_ account) 
774          * for each _transport_ account. */
775         if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
776                 /* Now would be a good time to create the per-account local outbox folder 
777                  * _store_ accounts corresponding to each transport account: */
778                 if (!priv->store_accounts_outboxes) {
779                         create_per_account_local_outbox_folders (self);
780                 }
781         }
782         
783         /* But we only return the per-account local outbox folder when 
784          * _store_ accounts are requested. */
785         if (type == TNY_ACCOUNT_TYPE_STORE) {
786                 /* Create them if necessary, 
787                  * (which also requires creating the transport accounts, 
788                  * if necessary.) */
789                 if (!priv->store_accounts_outboxes) {
790                         create_per_account_local_outbox_folders (self);
791                 }
792         
793                 /* Also add the local folder pseudo-account: */
794                 TnyAccount *tny_account =
795                         modest_tny_account_new_for_local_folders (priv->account_mgr, 
796                                 priv->session, NULL);
797                                         
798                 /* Add them to the TnyList: */
799                 if (priv->store_accounts_outboxes) {
800                         GSList *iter = NULL;
801                         for (iter = priv->store_accounts_outboxes; iter; iter = g_slist_next (iter)) {
802                                 TnyAccount *outbox_account = (TnyAccount*)iter->data;
803                                 if (list && outbox_account)
804                                         tny_list_prepend (list,  G_OBJECT(outbox_account));
805                                         
806                                 g_object_ref (outbox_account);
807                                 accounts = g_slist_append (accounts, outbox_account);
808                         }
809                         
810                         /* Add a merged folder, merging all the per-account outbox folders: */
811                         modest_tny_local_folders_account_add_merged_outbox_folders (
812                                 MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (tny_account), priv->store_accounts_outboxes);
813         
814                         /* We have finished with this temporary list, so free it: */
815                         account_list_free (priv->store_accounts_outboxes);
816                         priv->store_accounts_outboxes = NULL;
817                 }
818                 
819                 if (list)
820                         tny_list_prepend (list, G_OBJECT(tny_account));
821                 accounts = g_slist_append (accounts, tny_account); /* cache it */       
822         }
823                 
824         if (type == TNY_ACCOUNT_TYPE_STORE) {
825                         /* Store the cache: */
826                         priv->store_accounts = accounts;
827         } else if (type == TNY_ACCOUNT_TYPE_TRANSPORT) {
828                         /* Store the cache: */
829                         priv->transport_accounts = accounts;
830         }
831 }       
832
833
834 static void
835 modest_tny_account_store_get_accounts  (TnyAccountStore *self, TnyList *list,
836                                         TnyGetAccountsRequestType request_type)
837 {
838         ModestTnyAccountStorePrivate *priv;
839         
840         g_return_if_fail (self);
841         g_return_if_fail (TNY_IS_LIST(list));
842         
843         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
844         
845         if (request_type == TNY_ACCOUNT_STORE_BOTH) {
846                 modest_tny_account_store_get_accounts (self, list,
847                                                        TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
848                 modest_tny_account_store_get_accounts (self, list,
849                                                        TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
850                 return;
851         }
852         
853         if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS)  {
854                 if (!priv->store_accounts)
855                         get_server_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
856                 else
857                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
858                 
859         } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
860                 if (!priv->transport_accounts)
861                         get_server_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
862                 else
863                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
864         } else
865                 g_return_if_reached (); /* incorrect req type */
866 }
867
868
869 static const gchar*
870 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
871 {
872         ModestTnyAccountStorePrivate *priv;
873         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
874         
875         if (!priv->cache_dir)
876                 priv->cache_dir = g_build_filename (g_get_home_dir(), 
877                                                     MODEST_DIR, MODEST_CACHE_DIR, NULL);
878         return priv->cache_dir;
879 }
880
881
882 /*
883  * callers need to unref
884  */
885 static TnyDevice*
886 modest_tny_account_store_get_device (TnyAccountStore *self)
887 {
888         ModestTnyAccountStorePrivate *priv;
889
890         g_return_val_if_fail (self, NULL);
891         
892         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
893
894         if (priv->device)
895                 return g_object_ref (G_OBJECT(priv->device));
896         else
897                 return NULL;
898 }
899
900
901 static TnyAccount*
902 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
903 {
904         TnyAccount *account = NULL;
905         ModestTnyAccountStorePrivate *priv;     
906         GSList *cursor;
907         
908         g_return_val_if_fail (self, NULL);
909         g_return_val_if_fail (url_string, NULL);
910         
911         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
912
913         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
914                 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
915                         account = TNY_ACCOUNT(cursor->data);
916                         break;
917                 }
918         }
919
920         if (!account) {
921                 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
922                         if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
923                                 account = TNY_ACCOUNT(cursor->data);
924                                 break;
925                         }
926                 }
927         }
928
929         if (account)
930                 g_object_ref (G_OBJECT(account));
931
932         return account;
933 }
934
935
936
937 static gboolean
938 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
939                                 gboolean question, const GError *error)
940 {
941         /* TODO: It would be nice to know what account caused this error. */
942         
943         g_return_val_if_fail (error, FALSE);
944
945         if ((error->domain != TNY_ACCOUNT_ERROR) 
946                 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
947                 g_warning("%s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s", 
948                         __FUNCTION__, error->domain, error->message); 
949                 return FALSE;
950         }
951         
952         /* printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message); */
953         
954
955         /* const gchar *prompt = NULL; */
956         gchar *prompt = NULL;
957         switch (error->code) {
958                 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
959                 /* The tinymail camel implementation just sends us this for almost 
960                  * everything, so we have to guess at the cause.
961                  * It could be a wrong password, or inability to resolve a hostname, 
962                  * or lack of network, or incorrect authentication method, or something entirely different: */
963                 /* TODO: Fix camel to provide specific error codes, and then use the 
964                  * specific dialog messages from Chapter 12 of the UI spec.
965                  */
966                 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT: 
967                     g_debug ("%s: Handling GError domain=%d, code=%d, message=%s", 
968                                 __FUNCTION__, error->domain, error->code, error->message);
969                         
970                         /* TODO: Remove the internal error message for the real release.
971                          * This is just so the testers can give us more information: */
972                         /* prompt = _("Modest account not yet fully configured."); */
973                         prompt = g_strdup_printf(
974                                 "%s\n (Internal error message, often very misleading):\n%s", 
975                                 _("Incorrect Account Settings"), 
976                                 error->message);
977                                 
978                         /* TODO: If we can ever determine that the problem is a wrong password:
979                          * In this case, the UI spec wants us to show a banner, and then 
980                          * open the Account Settings dialog. */
981                         /* Note: Sometimes, the get_password() function seems to be called again 
982                          * when a password is wrong, but sometimes this alert_func is called. */
983                         #if 0
984                         GtkWidget *parent_widget = 
985                                 GTK_WIDGET (
986                                         modest_window_mgr_get_main_window (
987                                                 modest_runtime_get_window_mgr ()));
988                         
989                         hildon_banner_show_information (
990                                 parent_widget,
991                                 NULL /* icon name */,
992                                 _("mcen_ib_username_pw_incorrect") );
993                                 
994                         /* Show the Account Settings window: */
995                         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
996                         /* TODO: Get the account somehow. Maybe tinymail should send it with the signal. */
997                         const gchar* modest_account_name = 
998                                 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
999                         g_assert (modest_account_name);
1000                         modest_account_settings_dialog_set_account_name (dialog, 
1001                                 modest_account_name);
1002                         
1003                         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
1004                         gtk_dialog_run (GTK_DIALOG (dialog));
1005                         gtk_widget_destroy (GTK_WIDGET (dialog));
1006                         #endif
1007                          
1008                         break;
1009                         
1010                 //TODO: We have started receiving errors of 
1011                 //domain=TNY_ACCOUNT_ERROR, code=TNY_ACCOUNT_ERROR_TRY_CONNECT, message="Canceled".
1012                 //If this is really a result of us cancelling our own operation then 
1013                 //a) this probably shouldn't be an error, and
1014                 //b) should have its own error code.
1015                 
1016                 default:
1017                         g_warning ("%s: Unhandled GError code: %d, message=%s", 
1018                                 __FUNCTION__, error->code, error->message);
1019                         prompt = NULL;
1020                 break;
1021         }
1022         
1023         if (!prompt)
1024                 return FALSE;
1025
1026 #ifdef MODEST_PLATFORM_MAEMO
1027         /* The Tinymail documentation says that we should show Yes and No buttons, 
1028          * when it is a question.
1029          * Obviously, we need tinymail to use more specific error codes instead,
1030          * so we know what buttons to show. */
1031          GtkWidget *dialog = NULL;
1032          if (question) {
1033                 dialog = GTK_WIDGET (hildon_note_new_confirmation (NULL, 
1034                         prompt));
1035          } else {
1036                 dialog = GTK_WIDGET (hildon_note_new_information (NULL, 
1037                         prompt));
1038          }
1039 #else
1040
1041         GtkMessageType gtktype = GTK_MESSAGE_ERROR;
1042         switch (type)
1043         {
1044                 case TNY_ALERT_TYPE_INFO:
1045                 gtktype = GTK_MESSAGE_INFO;
1046                 break;
1047                 case TNY_ALERT_TYPE_WARNING:
1048                 gtktype = GTK_MESSAGE_WARNING;
1049                 break;
1050                 case TNY_ALERT_TYPE_ERROR:
1051                 default:
1052                 gtktype = GTK_MESSAGE_ERROR;
1053                 break;
1054         }
1055         
1056         GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
1057                 gtktype, GTK_BUTTONS_YES_NO, prompt);
1058 #endif /* #ifdef MODEST_PLATFORM_MAEMO */
1059
1060         gboolean retval = TRUE;
1061         const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1062         if (question) {
1063                 retval = (response == GTK_RESPONSE_YES) ||
1064                                  (response == GTK_RESPONSE_OK); 
1065         }
1066
1067         gtk_widget_destroy (dialog);
1068         
1069         /* TODO: Don't free this when we no longer strdup the message for testers. */
1070         g_free (prompt);
1071
1072
1073         /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1074         return retval;
1075 }
1076
1077
1078 static void
1079 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1080 {
1081         TnyAccountStoreIface *klass;
1082
1083         g_return_if_fail (g);
1084
1085         klass = (TnyAccountStoreIface *)g;
1086
1087         klass->get_accounts_func =
1088                 modest_tny_account_store_get_accounts;
1089         klass->get_cache_dir_func =
1090                 modest_tny_account_store_get_cache_dir;
1091         klass->get_device_func =
1092                 modest_tny_account_store_get_device;
1093         klass->alert_func =
1094                 modest_tny_account_store_alert;
1095         klass->find_account_func =
1096                 modest_tny_account_store_find_account_by_url;
1097 }
1098
1099 void
1100 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1101                                             ModestTnyGetPassFunc func)
1102 {
1103         /* not implemented, we use signals */
1104         g_printerr ("modest: set_get_pass_func not implemented\n");
1105 }
1106
1107 TnySessionCamel*
1108 modest_tny_account_store_get_session  (TnyAccountStore *self)
1109 {
1110         g_return_val_if_fail (self, NULL);
1111         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1112 }
1113
1114
1115 TnyAccount*
1116 modest_tny_account_store_get_tny_account_by_id  (ModestTnyAccountStore *self, const gchar *id)
1117 {
1118         TnyAccount *account = NULL;
1119         ModestTnyAccountStorePrivate *priv;     
1120         GSList *cursor;
1121
1122         g_return_val_if_fail (self, NULL);
1123         g_return_val_if_fail (id, NULL);
1124         
1125         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1126
1127
1128         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1129                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1130                 if (acc_id && strcmp (acc_id, id) == 0) {
1131                         account = TNY_ACCOUNT(cursor->data);
1132                         break;
1133                 }
1134         }
1135                 
1136         /* if we already found something, no need to search the transport accounts */
1137         for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
1138                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
1139                 if (acc_id && strcmp (acc_id, id) == 0) {
1140                         account = TNY_ACCOUNT(cursor->data);
1141                         break;
1142                 }
1143         }
1144
1145         if (account)
1146                 g_object_ref (G_OBJECT(account));
1147         
1148         return account;
1149 }
1150
1151 TnyAccount*
1152 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1153                                                      const gchar *account_name,
1154                                                      TnyAccountType type)
1155 {
1156         TnyAccount *account = NULL;
1157         gchar *id = NULL;
1158         ModestTnyAccountStorePrivate *priv;     
1159
1160         g_return_val_if_fail (self, NULL);
1161         g_return_val_if_fail (account_name, NULL);
1162         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
1163                               NULL);
1164         
1165         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1166
1167         /* Special case for the local account */
1168         if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
1169                 if(type == TNY_ACCOUNT_TYPE_STORE)
1170                         id = g_strdup (MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
1171                 else {
1172                         /* The local folders modest account has no transport server account. */
1173                         return NULL;
1174                 }
1175         } else {
1176                 ModestAccountData *account_data;
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__,
1180                                     account_name);
1181                         return NULL;
1182                 }
1183
1184                 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
1185                         id = g_strdup (account_data->store_account->account_name);
1186                 else if (account_data->transport_account)
1187                         id = g_strdup (account_data->transport_account->account_name);
1188
1189                 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
1190         }
1191
1192         if (!id)
1193                 g_printerr ("modest: could not get an id for account %s\n",
1194                             account_name);
1195         else    
1196                 account = modest_tny_account_store_get_tny_account_by_id (self, id);
1197
1198         if (!account)
1199                 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
1200                             type == TNY_ACCOUNT_TYPE_STORE ? "store" : "transport",
1201                             account_name, id ? id : "<none>");
1202
1203         return account; 
1204 }
1205
1206 static TnyAccount*
1207 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1208                                                          const gchar *account_name)
1209 {
1210         /* Get the current connection: */
1211         TnyDevice *device = modest_runtime_get_device ();
1212         
1213         if (!tny_device_is_online (device))
1214                 return NULL;
1215
1216 #ifdef MODEST_PLATFORM_MAEMO
1217         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1218         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);    
1219         const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1220         /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1221         if (!iap_id)
1222                 return NULL;
1223                 
1224         ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1225         if (!connection)
1226                 return NULL;
1227                 
1228         const gchar *connection_name = con_ic_iap_get_name (connection);
1229         /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1230         if (!connection_name)
1231                 return NULL;
1232         
1233         /*  Get the connection-specific transport acccount, if any: */
1234         ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1235         gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager, 
1236                 account_name, connection_name);
1237
1238         /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1239         if (!server_account_name) {
1240                 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1241         }
1242                 
1243         TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
1244
1245         /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1246         g_free (server_account_name);   
1247
1248         /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1249         g_object_unref (connection);
1250         
1251         return account;
1252 #else
1253         return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1254 #endif /* MODEST_PLATFORM_MAEMO */
1255 }
1256
1257                                                                  
1258 TnyAccount*
1259 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1260                                                                     const gchar *account_name)
1261 {
1262         /*  Get the connection-specific transport acccount, if any: */
1263         TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
1264                         
1265         /* If there is no connection-specific transport account (the common case), 
1266          * just get the regular transport account: */
1267         if (!account) {
1268                 /* printf("DEBUG: %s: using regular transport account for account %s.\n", __FUNCTION__, account_name); */
1269
1270                 /* The special local folders don't have transport accounts. */
1271                 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1272                         account = NULL;
1273                 else
1274                         account = modest_tny_account_store_get_server_account (self, account_name, 
1275                                                      TNY_ACCOUNT_TYPE_TRANSPORT);
1276         }
1277                              
1278         return account;
1279 }
1280
1281 gboolean
1282 modest_tny_account_is_virtual_local_folders (TnyAccount *self)
1283 {
1284         /* We should make this more sophisticated if we ever use ModestTnyLocalFoldersAccount 
1285          * for anything else. */
1286         return MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (self);
1287 }
1288
1289 TnyAccount*
1290 modest_tny_account_store_get_local_folders_account (TnyAccountStore *self)
1291 {
1292         TnyAccount *account = NULL;
1293         ModestTnyAccountStorePrivate *priv;     
1294         GSList *cursor;
1295
1296         g_return_val_if_fail (self, NULL);
1297         
1298         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1299
1300         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
1301                 TnyAccount *this_account = TNY_ACCOUNT(cursor->data);
1302                 if (modest_tny_account_is_virtual_local_folders (this_account)) {
1303                                  account = this_account;
1304                                  break;
1305                 }
1306         }
1307
1308         if (account)
1309                 g_object_ref (G_OBJECT(account));
1310         
1311         return account;
1312 }