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