* Fixes NB#65015 the Outbox folder is no longer missing in the folder view when...
[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 #include <maemo/modest-maemo-utils.h>
55
56
57 #include "modest-tny-account-store.h"
58 #include "modest-tny-platform-factory.h"
59 #include <tny-gtk-lockable.h>
60 #include <camel/camel.h>
61
62 #ifdef MODEST_PLATFORM_MAEMO
63 #include <tny-maemo-conic-device.h>
64 #ifdef MODEST_HAVE_HILDON0_WIDGETS
65 #include <hildon-widgets/hildon-note.h>
66 #include <hildon-widgets/hildon-banner.h>
67 #else
68 #include <hildon/hildon-note.h>
69 #include <hildon/hildon-banner.h>
70 #endif
71 #endif
72
73 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
74
75 /* 'private'/'protected' functions */
76 static void    modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
77
78 static void    modest_tny_account_store_finalize     (GObject *obj);
79
80 static void    modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
81
82 static void    modest_tny_account_store_init          (gpointer g, gpointer iface_data);
83
84 static void    modest_tny_account_store_base_init     (gpointer g_class);
85
86 static void    on_account_inserted         (ModestAccountMgr *acc_mgr, 
87                                             const gchar *account,
88                                             gpointer user_data);
89
90 static void    add_existing_accounts       (ModestTnyAccountStore *self);
91
92 static void    insert_account              (ModestTnyAccountStore *self,
93                                             const gchar *account,
94                                             gboolean notify);
95
96 static void    on_account_removed          (ModestAccountMgr *acc_mgr, 
97                                             const gchar *account,
98                                             gpointer user_data);
99
100 static gchar*  get_password                (TnyAccount *account, 
101                                             const gchar * prompt_not_used, 
102                                             gboolean *cancel);
103
104 static void    forget_password             (TnyAccount *account);
105
106 static void    on_vfs_volume_mounted       (GnomeVFSVolumeMonitor *volume_monitor, 
107                                             GnomeVFSVolume *volume, 
108                                             gpointer user_data);
109
110 static void    on_vfs_volume_unmounted     (GnomeVFSVolumeMonitor *volume_monitor, 
111                                             GnomeVFSVolume *volume, 
112                                             gpointer user_data);
113
114 static void    modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, 
115                                                                    const gchar *server_account_name);
116
117 static void    add_connection_specific_transport_accounts         (ModestTnyAccountStore *self);
118
119 /* list my signals */
120 enum {
121         ACCOUNT_CHANGED_SIGNAL,
122         ACCOUNT_INSERTED_SIGNAL,
123         ACCOUNT_REMOVED_SIGNAL,
124
125         PASSWORD_REQUESTED_SIGNAL,
126         LAST_SIGNAL
127 };
128
129 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
130 struct _ModestTnyAccountStorePrivate {
131         gchar              *cache_dir;  
132         GHashTable         *password_hash;
133         GHashTable         *account_settings_dialog_hash;
134         ModestAccountMgr   *account_mgr;
135         TnySessionCamel    *session;
136         TnyDevice          *device;
137
138         gulong acc_inserted_handler;
139         gulong acc_changed_handler;
140         gulong acc_removed_handler;
141         gulong volume_mounted_handler;
142         gulong volume_unmounted_handler;
143         
144         /* We cache the lists of accounts here */
145         TnyList             *store_accounts;
146         TnyList             *transport_accounts;
147         TnyList             *store_accounts_outboxes;
148
149         /* Matches transport accounts and outbox folder */
150         GHashTable          *outbox_of_transport;
151 };
152
153 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
154                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
155                                                       ModestTnyAccountStorePrivate))
156
157 /* globals */
158 static GObjectClass *parent_class = NULL;
159
160 static guint signals[LAST_SIGNAL] = {0};
161
162 GType
163 modest_tny_account_store_get_type (void)
164 {
165         static GType my_type = 0;
166
167         if (!my_type) {
168                 static const GTypeInfo my_info = {
169                         sizeof(ModestTnyAccountStoreClass),
170                         modest_tny_account_store_base_init,     /* base init */
171                         NULL,           /* base finalize */
172                         (GClassInitFunc) modest_tny_account_store_class_init,
173                         NULL,           /* class finalize */
174                         NULL,           /* class data */
175                         sizeof(ModestTnyAccountStore),
176                         0,              /* n_preallocs */
177                         (GInstanceInitFunc) modest_tny_account_store_instance_init,
178                         NULL
179                 };
180
181                 static const GInterfaceInfo iface_info = {
182                         (GInterfaceInitFunc) modest_tny_account_store_init,
183                         NULL,         /* interface_finalize */
184                         NULL          /* interface_data */
185                 };
186                 /* hack hack */
187                 my_type = g_type_register_static (G_TYPE_OBJECT,
188                                                   "ModestTnyAccountStore",
189                                                   &my_info, 0);
190                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
191                                              &iface_info);
192         }
193         return my_type;
194 }
195
196
197 static void
198 modest_tny_account_store_base_init (gpointer g_class)
199 {
200         static gboolean tny_account_store_initialized = FALSE;
201
202         if (!tny_account_store_initialized) {
203
204                 signals[ACCOUNT_CHANGED_SIGNAL] =
205                         g_signal_new ("account_changed",
206                                       MODEST_TYPE_TNY_ACCOUNT_STORE,
207                                       G_SIGNAL_RUN_FIRST,
208                                       G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_changed),
209                                       NULL, NULL,
210                                       g_cclosure_marshal_VOID__OBJECT,
211                                       G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
212
213                 signals[ACCOUNT_INSERTED_SIGNAL] =
214                         g_signal_new ("account_inserted",
215                                       MODEST_TYPE_TNY_ACCOUNT_STORE,
216                                       G_SIGNAL_RUN_FIRST,
217                                       G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_inserted),
218                                       NULL, NULL,
219                                       g_cclosure_marshal_VOID__OBJECT,
220                                       G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
221                 
222                 signals[ACCOUNT_REMOVED_SIGNAL] =
223                         g_signal_new ("account_removed",
224                                       MODEST_TYPE_TNY_ACCOUNT_STORE,
225                                       G_SIGNAL_RUN_FIRST,
226                                       G_STRUCT_OFFSET (ModestTnyAccountStoreClass, account_removed),
227                                       NULL, NULL,
228                                       g_cclosure_marshal_VOID__OBJECT,
229                                       G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT);
230                 
231 /*              signals[TNY_ACCOUNT_STORE_CONNECTING_FINISHED] = */
232 /*                      g_signal_new ("connecting_finished", */
233 /*                                    TNY_TYPE_ACCOUNT_STORE, */
234 /*                                    G_SIGNAL_RUN_FIRST, */
235 /*                                    G_STRUCT_OFFSET (TnyAccountStoreIface, connecting_finished), */
236 /*                                    NULL, NULL, */
237 /*                                    g_cclosure_marshal_VOID__VOID,  */
238 /*                                    G_TYPE_NONE, 0); */
239
240                 signals[PASSWORD_REQUESTED_SIGNAL] =
241                         g_signal_new ("password_requested",
242                                       MODEST_TYPE_TNY_ACCOUNT_STORE,
243                                       G_SIGNAL_RUN_FIRST,
244                                       G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
245                                       NULL, NULL,
246                                       modest_marshal_VOID__STRING_POINTER_POINTER_POINTER_POINTER,
247                                       G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER,
248                                       G_TYPE_POINTER);          
249
250                 tny_account_store_initialized = TRUE;
251         }
252 }
253
254
255 static void
256 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
257 {
258         GObjectClass *gobject_class;
259         gobject_class = (GObjectClass*) klass;
260
261         parent_class            = g_type_class_peek_parent (klass);
262         gobject_class->finalize = modest_tny_account_store_finalize;
263
264         g_type_class_add_private (gobject_class,
265                                   sizeof(ModestTnyAccountStorePrivate));
266 }
267      
268 static void
269 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
270 {
271         GnomeVFSVolumeMonitor* monitor = NULL;
272         ModestTnyAccountStorePrivate *priv;
273
274         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
275
276         priv->cache_dir              = NULL;
277         priv->account_mgr            = NULL;
278         priv->session                = NULL;
279         priv->device                 = NULL;
280         
281         priv->outbox_of_transport = 
282                 g_hash_table_new_full (g_direct_hash,
283                                        g_direct_equal,
284                                        NULL,
285                                        NULL);
286
287         /* An in-memory store of passwords, 
288          * for passwords that are not remembered in the configuration,
289          * so they need to be asked for from the user once in each session:
290          */
291         priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
292                                                      g_free, g_free);
293                                                              
294         /* A hash-map of modest account names to dialog pointers,
295          * so we can avoid showing the account settings twice for the same modest account: */                                 
296         priv->account_settings_dialog_hash = g_hash_table_new_full (g_str_hash, g_str_equal, 
297                                                                     g_free, NULL);
298         
299         /* Respond to volume mounts and unmounts, such 
300          * as the insertion/removal of the memory card: */
301         /* This is a singleton, so it does not need to be unrefed. */
302         monitor = gnome_vfs_get_volume_monitor();
303
304         priv->volume_mounted_handler = g_signal_connect (G_OBJECT(monitor), 
305                                                          "volume-mounted",
306                                                          G_CALLBACK(on_vfs_volume_mounted),
307                                                          obj);
308
309         priv->volume_unmounted_handler = g_signal_connect (G_OBJECT(monitor), "volume-unmounted",
310                                                            G_CALLBACK(on_vfs_volume_unmounted),
311                                                            obj);
312 }
313
314 /* disconnect the list of TnyAccounts */
315 static void
316 foreach_account_disconnect (gpointer data, 
317                             gpointer user_data)
318 {
319         tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(data), FALSE, NULL);
320 }
321
322
323 static void
324 foreach_account_append_to_list (gpointer data, 
325                                 gpointer user_data)
326 {
327         TnyList *list;
328
329         list = TNY_LIST (user_data);
330         tny_list_append (list, G_OBJECT (data));
331 }
332
333 /********************************************************************/
334 /*           Control the state of the MMC local account             */
335 /********************************************************************/
336
337 /** Only call this if the memory card is really mounted.
338  */ 
339 static void
340 add_mmc_account(ModestTnyAccountStore *self, gboolean emit_insert_signal)
341 {
342         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
343         g_return_if_fail (priv->session);
344         
345         TnyAccount *mmc_account = modest_tny_account_new_for_local_folders (priv->account_mgr, 
346                                                                         priv->session, 
347                                                                         MODEST_MCC1_VOLUMEPATH);
348
349         /* Add to the list of store accounts */
350         tny_list_append (priv->store_accounts, G_OBJECT (mmc_account));
351
352         if (emit_insert_signal) {
353                 g_signal_emit (G_OBJECT (self), 
354                                signals [ACCOUNT_INSERTED_SIGNAL],
355                                0, mmc_account);
356         }
357
358         /* Free */
359         g_object_unref (mmc_account);
360 }
361
362 static void
363 on_vfs_volume_mounted(GnomeVFSVolumeMonitor *volume_monitor, 
364                       GnomeVFSVolume *volume, 
365                       gpointer user_data)
366 {
367         ModestTnyAccountStore *self;
368         ModestTnyAccountStorePrivate *priv;
369  
370         gchar *uri = NULL;
371
372         self = MODEST_TNY_ACCOUNT_STORE(user_data);
373         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
374         
375         /* Check whether this was the external MMC1 card: */
376         uri = gnome_vfs_volume_get_activation_uri (volume);
377
378         if (uri && (!strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI))) {
379                 add_mmc_account (self, TRUE /* emit the insert signal. */);
380         }
381         
382         g_free (uri);
383 }
384
385 static void
386 on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor, 
387                         GnomeVFSVolume *volume, 
388                         gpointer user_data)
389 {
390         ModestTnyAccountStore *self;
391         ModestTnyAccountStorePrivate *priv;
392         gchar *uri = NULL;
393
394         self = MODEST_TNY_ACCOUNT_STORE(user_data);
395         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
396         
397         /* Check whether this was the external MMC1 card: */
398         uri = gnome_vfs_volume_get_activation_uri (volume);
399         if (uri && (strcmp (uri, MODEST_MCC1_VOLUMEPATH_URI) == 0)) {
400                 TnyAccount *mmc_account = NULL;
401                 gboolean found = FALSE;
402                 TnyIterator *iter = NULL;
403
404                 iter = tny_list_create_iterator (priv->store_accounts);
405                 while (!tny_iterator_is_done (iter) && !found) {
406                         TnyAccount *account;
407
408                         account = TNY_ACCOUNT (tny_iterator_get_current (iter));
409                         if (modest_tny_account_is_memory_card_account (account)) {
410                                 found = TRUE;
411                                 mmc_account = g_object_ref (account);
412                         }
413                         g_object_unref (account);
414                         tny_iterator_next (iter);
415                 }
416                 g_object_unref (iter);
417
418                 if (found) {
419                         /* Remove from the list */
420                         tny_list_remove (priv->store_accounts, G_OBJECT (mmc_account));
421                        
422                         /* Notify observers */
423                         g_signal_emit (G_OBJECT (self),
424                                        signals [ACCOUNT_REMOVED_SIGNAL],
425                                        0, mmc_account);
426
427                         g_object_unref (mmc_account);
428                 } else {
429                         g_warning ("%s: there was no store account for the unmounted MMC",
430                                    __FUNCTION__);
431                 }
432         }
433         g_free (uri);
434 }
435
436 /**
437  * modest_tny_account_store_forget_password_in_memory
438  * @self: a TnyAccountStore instance
439  * @account: A server account.
440  * 
441  * Forget any password stored in memory for this account.
442  * For instance, this should be called when the user has changed the password in the account settings.
443  */
444 static void
445 modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, const gchar * server_account_name)
446 {
447         /* printf ("DEBUG: %s\n", __FUNCTION__); */
448         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
449
450         if (server_account_name && priv->password_hash) {
451                 g_hash_table_remove (priv->password_hash, server_account_name);
452         }
453 }
454
455
456 static gboolean
457 update_tny_account_for_account (ModestTnyAccountStore *self, ModestAccountMgr *acc_mgr,
458                                 const gchar *account_name, TnyAccountType type)
459 {
460         ModestTnyAccountStorePrivate *priv;
461         TnyList* account_list;
462         gboolean found = FALSE;
463         TnyIterator *iter = NULL;
464
465         g_return_val_if_fail (self, FALSE);
466         g_return_val_if_fail (account_name, FALSE);
467         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || 
468                               type == TNY_ACCOUNT_TYPE_TRANSPORT,
469                               FALSE);
470
471         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
472         account_list = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
473         
474         iter = tny_list_create_iterator (account_list);
475         while (!tny_iterator_is_done (iter) && !found) {
476                 TnyAccount *tny_account;
477                 tny_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
478                 if (tny_account) {
479                         const gchar* parent_name =
480                                 modest_tny_account_get_parent_modest_account_name_for_server_account (tny_account);
481                         if (parent_name && strcmp (parent_name, account_name) == 0) {
482                                 found = TRUE;
483                                 modest_tny_account_update_from_account (tny_account, acc_mgr, account_name, type);
484                                 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_CHANGED_SIGNAL], 0, tny_account);
485                         }
486                         g_object_unref (tny_account);
487                 }
488                 tny_iterator_next (iter);
489         }
490
491         if (iter)
492                 g_object_unref (iter);
493         
494         return found;
495 }
496
497
498 static void
499 on_account_changed (ModestAccountMgr *acc_mgr, 
500                     const gchar *account_name, 
501                     gpointer user_data)
502 {
503         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
504
505         g_debug ("DEBUG: modest: %s\n", __FUNCTION__);
506
507         /* Ignore the change if it's a change in the last_updated value */
508 //      if (key && g_str_has_suffix ((const gchar *) key, MODEST_ACCOUNT_LAST_UPDATED))
509 //              return;
510         
511         if (!update_tny_account_for_account (self, acc_mgr, account_name, TNY_ACCOUNT_TYPE_STORE))
512                 g_warning ("%s: failed to update store account for %s", __FUNCTION__, account_name);
513         if (!update_tny_account_for_account (self, acc_mgr, account_name, TNY_ACCOUNT_TYPE_TRANSPORT))
514                 g_warning ("%s: failed to update transport account for %s", __FUNCTION__, account_name);
515 }
516
517 static void 
518 on_account_settings_hide (GtkWidget *widget, gpointer user_data)
519 {
520         TnyAccount *account = (TnyAccount*)user_data;
521         
522         /* This is easier than using a struct for the user_data: */
523         ModestTnyAccountStore *self = modest_runtime_get_account_store();
524         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
525         
526         const gchar *modest_account_name = 
527                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
528         if (modest_account_name)
529                 g_hash_table_remove (priv->account_settings_dialog_hash, modest_account_name);
530 }
531
532 static void 
533 show_password_warning_only ()
534 {
535         ModestWindow *main_window = 
536                                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
537                                 
538         /* Show an explanatory temporary banner: */
539         hildon_banner_show_information ( 
540                 GTK_WIDGET(main_window), NULL, _("mcen_ib_username_pw_incorrect"));
541 }
542                 
543 static void 
544 show_wrong_password_dialog (TnyAccount *account)
545
546         /* This is easier than using a struct for the user_data: */
547         ModestTnyAccountStore *self = modest_runtime_get_account_store();
548         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
549         
550         const gchar *modest_account_name = 
551                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
552         if (!modest_account_name) {
553                 g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n", 
554                         __FUNCTION__);
555         }
556         
557         /* Check whether this window is already open,
558          * for instance because of a previous get_password() call: 
559          */
560         gpointer dialog_as_gpointer = NULL;
561         gboolean found = FALSE;
562         if (priv->account_settings_dialog_hash) {
563                 found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
564                         modest_account_name, NULL, (gpointer*)&dialog_as_gpointer);
565         }
566         ModestAccountSettingsDialog *dialog = dialog_as_gpointer;
567                                         
568         ModestWindow *main_window = 
569                                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
570
571         gboolean created_dialog = FALSE;
572         if (!found || !dialog) {
573                 dialog = modest_account_settings_dialog_new ();
574                 modest_account_settings_dialog_set_account_name (dialog, modest_account_name);
575                 modest_account_settings_dialog_switch_to_user_info (dialog);
576                 
577                 g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (modest_account_name), dialog);
578                 
579                 created_dialog = TRUE;
580         }
581         
582         /* Show an explanatory temporary banner: */
583         hildon_banner_show_information ( 
584                 GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
585                 
586         if (created_dialog) {
587                 /* Forget it when it closes: */
588                 g_signal_connect_object (G_OBJECT (dialog), "hide", G_CALLBACK (on_account_settings_hide), 
589                         account, 0);
590                         
591                 /* Show it and delete it when it closes: */
592                 modest_maemo_show_dialog_and_forget (GTK_WINDOW (main_window), GTK_DIALOG (dialog));
593         }
594         else {
595                 /* Just show it instead of showing it and deleting it when it closes,
596                  * though it is probably open already: */
597                 gtk_window_present (GTK_WINDOW (dialog));
598         }
599 }
600
601
602
603 static void
604 request_password_and_wait (ModestTnyAccountStore *account_store, 
605                                          const gchar* server_account_id,
606                                          gchar **username,
607                                          gchar **password,
608                                          gboolean *cancel, 
609                                          gboolean *remember)
610 {
611                         g_signal_emit (G_OBJECT(account_store), signals[PASSWORD_REQUESTED_SIGNAL], 0,
612                                server_account_id, /* server_account_name */
613                                username, password, cancel, remember);
614 }
615
616 /* This callback will be called by Tinymail when it needs the password
617  * from the user or the account settings.
618  * It can also call forget_password() before calling this,
619  * so that we clear wrong passwords out of our account settings.
620  * Note that TnyAccount here will be the server account. */
621 static gchar*
622 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
623 {
624         /* TODO: Settting cancel to FALSE does not actually cancel everything.
625          * We still get multiple requests afterwards, so we end up showing the 
626          * same dialogs repeatedly.
627          */
628          
629         printf ("DEBUG: modest: %s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
630           
631         g_return_val_if_fail (account, NULL);
632           
633         const TnyAccountStore *account_store = NULL;
634         ModestTnyAccountStore *self = NULL;
635         ModestTnyAccountStorePrivate *priv;
636         gchar *username = NULL;
637         gchar *pwd = NULL;
638         gpointer pwd_ptr = NULL;
639         gboolean already_asked = FALSE;
640
641         /* Initialize the output parameter: */
642         if (cancel)
643                 *cancel = FALSE;
644                 
645         const gchar *server_account_name = tny_account_get_id (account);
646         account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
647                                                              "account_store"));
648
649         if (!server_account_name || !account_store) {
650                 g_warning ("modest: %s: could not retrieve account_store for account %s",
651                            __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
652                 if (cancel)
653                         *cancel = TRUE;
654                 
655                 return NULL;
656         }
657
658         self = MODEST_TNY_ACCOUNT_STORE (account_store);
659         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
660         
661         /* This hash map stores passwords, including passwords that are not stored in gconf. */
662         /* Is it in the hash? if it's already there, it must be wrong... */
663         pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
664                                    * type-punned ptrs...*/
665         already_asked = priv->password_hash && 
666                                 g_hash_table_lookup_extended (priv->password_hash,
667                                                       server_account_name,
668                                                       NULL,
669                                                       (gpointer*)&pwd_ptr);
670                                                       
671         printf ("DEBUG: modest: %s: Already asked = %d\n", __FUNCTION__, already_asked);
672
673         /* If the password is not already there, try ModestConf */
674         if (!already_asked) {
675                 pwd  = modest_server_account_get_password (priv->account_mgr,
676                                                       server_account_name);
677                 g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup (pwd));
678         }
679
680         /* If it was already asked, it must have been wrong, so ask again */
681         if (already_asked || !pwd || strlen(pwd) == 0) {
682                 /* As per the UI spec, if no password was set in the account settings, 
683                  * ask for it now. But if the password is wrong in the account settings, 
684                  * then show a banner and the account settings dialog so it can be corrected:
685                  */
686                 const gboolean settings_have_password = 
687                         modest_server_account_get_has_password (priv->account_mgr, server_account_name);
688                 printf ("DEBUG: modest: %s: settings_have_password=%d\n", __FUNCTION__, settings_have_password);
689                 if (settings_have_password) {
690                         /* The password must be wrong, so show the account settings dialog so it can be corrected: */
691                         show_wrong_password_dialog (account);
692                         
693                         if (cancel)
694                                 *cancel = TRUE;
695                                 
696                         return NULL;
697                 }
698         
699                 /* we don't have it yet. Get the password from the user */
700                 const gchar* account_id = tny_account_get_id (account);
701                 gboolean remember = FALSE;
702                 pwd = NULL;
703                 
704                 if (already_asked) {
705                         /* Show an info banner, before we show the protected password dialog: */
706                         show_password_warning_only();
707                 }
708                 
709                 request_password_and_wait (self, account_id, 
710                                &username, &pwd, cancel, &remember);
711                 
712                 if (!*cancel) {
713                         /* The password will be returned as the result,
714                          * but we need to tell tinymail about the username too: */
715                         tny_account_set_user (account, username);
716                         
717                         /* Do not save the password in gconf, 
718                          * because the UI spec says "The password will never be saved in the account": */
719                         /*
720                         if (remember) {
721                                 printf ("%s: Storing username=%s, password=%s\n", 
722                                         __FUNCTION__, username, pwd);
723                                 modest_server_account_set_username (priv->account_mgr, server_account_name,
724                                                                username);
725                                 modest_server_account_set_password (priv->account_mgr, server_account_name,
726                                                                pwd);
727                         }
728                         */
729
730                         /* We need to dup the string even knowing that
731                            it's already a dup of the contents of an
732                            entry, because it if it's wrong, then camel
733                            will free it */
734                         g_hash_table_insert (priv->password_hash, g_strdup (server_account_name), g_strdup(pwd));
735                 } else {
736                         g_hash_table_remove (priv->password_hash, server_account_name);
737                         
738                         g_free (pwd);
739                         pwd = NULL;
740                 }
741
742                 g_free (username);
743                 username = NULL;
744         } else
745                 *cancel = FALSE;
746  
747     /* printf("  DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
748         
749         return pwd;
750 }
751
752 /* tinymail calls this if the connection failed due to an incorrect password.
753  * And it seems to call this for any general connection failure. */
754 static void
755 forget_password (TnyAccount *account)
756 {
757         printf ("DEBUG: %s\n", __FUNCTION__);
758         ModestTnyAccountStore *self;
759         ModestTnyAccountStorePrivate *priv;
760         const TnyAccountStore *account_store;
761         gchar *pwd;
762         const gchar *key;
763         
764         account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
765                                                              "account_store"));
766         self = MODEST_TNY_ACCOUNT_STORE (account_store);
767         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
768         key  = tny_account_get_id (account);
769
770         /* Do not remove the key, this will allow us to detect that we
771            have already asked for it at least once */
772         pwd = g_hash_table_lookup (priv->password_hash, key);
773         if (pwd) {
774                 memset (pwd, 0, strlen (pwd));
775                 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
776         }
777
778         /* Remove from configuration system */
779         /*
780         modest_account_mgr_unset (priv->account_mgr,
781                                   key, MODEST_ACCOUNT_PASSWORD, TRUE);
782         */
783 }
784
785 static void
786 modest_tny_account_store_finalize (GObject *obj)
787 {
788         GnomeVFSVolumeMonitor *volume_monitor;
789         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
790         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
791
792         g_free (priv->cache_dir);
793         priv->cache_dir = NULL;
794         
795         if (priv->password_hash) {
796                 g_hash_table_destroy (priv->password_hash);
797                 priv->password_hash = NULL;
798         }
799
800         if (priv->account_settings_dialog_hash) {
801                 g_hash_table_destroy (priv->account_settings_dialog_hash);
802                 priv->account_settings_dialog_hash = NULL;
803         }
804
805         if (priv->outbox_of_transport) {
806                 g_hash_table_destroy (priv->outbox_of_transport);
807                 priv->outbox_of_transport = NULL;
808         }
809
810
811         /* Disconnect VFS signals */
812         volume_monitor = gnome_vfs_get_volume_monitor ();
813         if (g_signal_handler_is_connected (volume_monitor, 
814                                            priv->volume_mounted_handler))
815                 g_signal_handler_disconnect (volume_monitor, 
816                                              priv->volume_mounted_handler);
817         if (g_signal_handler_is_connected (volume_monitor, 
818                                            priv->volume_unmounted_handler))
819                 g_signal_handler_disconnect (volume_monitor, 
820                                              priv->volume_unmounted_handler);
821
822         if (priv->account_mgr) {
823                 /* Disconnect signals */
824                 if (g_signal_handler_is_connected (priv->account_mgr, 
825                                                    priv->acc_inserted_handler))
826                         g_signal_handler_disconnect (priv->account_mgr, 
827                                                      priv->acc_inserted_handler);
828                 if (g_signal_handler_is_connected (priv->account_mgr, 
829                                                    priv->acc_changed_handler))
830                         g_signal_handler_disconnect (priv->account_mgr, 
831                                                      priv->acc_changed_handler);
832                 if (g_signal_handler_is_connected (priv->account_mgr, 
833                                                    priv->acc_removed_handler))
834                         g_signal_handler_disconnect (priv->account_mgr, 
835                                                      priv->acc_removed_handler);
836
837                 g_object_unref (G_OBJECT(priv->account_mgr));
838                 priv->account_mgr = NULL;
839         }
840
841         if (priv->device) {
842                 g_object_unref (G_OBJECT(priv->device));
843                 priv->device = NULL;
844         }
845
846         /* Destroy all accounts. Disconnect all accounts before they are destroyed */
847         if (priv->store_accounts) {
848                 tny_list_foreach (priv->store_accounts, foreach_account_disconnect, NULL);
849                 g_object_unref (priv->store_accounts);
850                 priv->store_accounts = NULL;
851         }
852
853         if (priv->transport_accounts) {
854                 tny_list_foreach (priv->transport_accounts, foreach_account_disconnect, NULL);
855                 g_object_unref (priv->transport_accounts);
856                 priv->transport_accounts = NULL;
857         }
858
859         if (priv->store_accounts_outboxes) {
860                 g_object_unref (priv->store_accounts_outboxes);
861                 priv->store_accounts_outboxes = NULL;
862         }
863                 
864         if (priv->session) {
865                 camel_object_unref (CAMEL_OBJECT(priv->session));
866                 priv->session = NULL;
867         }
868         
869         G_OBJECT_CLASS(parent_class)->finalize (obj);
870 }
871
872 gboolean volume_path_is_mounted (const gchar* path)
873 {
874         g_return_val_if_fail (path, FALSE);
875
876         gboolean result = FALSE;
877         gchar * path_as_uri = g_filename_to_uri (path, NULL, NULL);
878         g_return_val_if_fail (path_as_uri, FALSE);
879
880         /* Get the monitor singleton: */
881         GnomeVFSVolumeMonitor *monitor = gnome_vfs_get_volume_monitor();
882
883         /* This seems like a simpler way to do this, but it returns a   
884          * GnomeVFSVolume even if the drive is not mounted: */
885         /*
886         GnomeVFSVolume *volume = gnome_vfs_volume_monitor_get_volume_for_path (monitor, 
887                 MODEST_MCC1_VOLUMEPATH);
888         if (volume) {
889                 gnome_vfs_volume_unref(volume);
890         }
891         */
892
893         /* Get the mounted volumes from the monitor: */
894         GList *list = gnome_vfs_volume_monitor_get_mounted_volumes (monitor);
895         GList *iter = list;
896         for (iter = list; iter; iter = g_list_next (iter)) {
897                 GnomeVFSVolume *volume = (GnomeVFSVolume*)iter->data;
898                 if (volume) {
899                         /*
900                         char *display_name = 
901                                 gnome_vfs_volume_get_display_name (volume);
902                         printf ("volume display name=%s\n", display_name);
903                         g_free (display_name);
904                         */
905                         
906                         char *uri = 
907                                 gnome_vfs_volume_get_activation_uri (volume);
908                         /* printf ("  uri=%s\n", uri); */
909                         if (uri && (strcmp (uri, path_as_uri) == 0))
910                                 result = TRUE;
911
912                         g_free (uri);
913
914                         gnome_vfs_volume_unref (volume);
915                 }
916         }
917
918         g_list_free (list);
919
920         g_free (path_as_uri);
921
922         return result;
923 }
924
925 ModestTnyAccountStore*
926 modest_tny_account_store_new (ModestAccountMgr *account_mgr, 
927                               TnyDevice *device) 
928 {
929         GObject *obj;
930         ModestTnyAccountStorePrivate *priv;
931         TnyAccount *local_account = NULL;
932         
933         g_return_val_if_fail (account_mgr, NULL);
934         g_return_val_if_fail (device, NULL);
935
936         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
937         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
938
939         priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
940         priv->device = g_object_ref (device);
941         
942         priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
943         if (!priv->session) {
944                 g_warning ("failed to get TnySessionCamel");
945                 return NULL;
946         }
947
948         /* Set the ui locker */ 
949         tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
950                 
951         /* Connect signals */
952         priv->acc_inserted_handler = g_signal_connect (G_OBJECT(account_mgr), "account_inserted",
953                                                       G_CALLBACK (on_account_inserted), obj);
954         priv->acc_changed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
955                                                       G_CALLBACK (on_account_changed), obj);
956         priv->acc_removed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
957                                                       G_CALLBACK (on_account_removed), obj);
958
959         /* Create the lists of accounts */
960         priv->store_accounts = tny_simple_list_new ();
961         priv->transport_accounts = tny_simple_list_new ();
962         priv->store_accounts_outboxes = tny_simple_list_new ();
963
964         /* Create the local folders account */
965         local_account = 
966                 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session, NULL);
967         tny_list_append (priv->store_accounts, G_OBJECT(local_account));
968         g_object_unref (local_account); 
969
970         /* Add the other remote accounts. Do this after adding the
971            local account, because we need to add our outboxes to the
972            global OUTBOX hosted in the local account */
973         add_existing_accounts (MODEST_TNY_ACCOUNT_STORE (obj));
974         
975         
976         /* Create the memory card account if the card is mounted: */
977         
978         /* This is a singleton, so it does not need to be unrefed. */
979         if (volume_path_is_mounted (MODEST_MCC1_VOLUMEPATH)) {
980                 /* It is mounted: */
981                 add_mmc_account (MODEST_TNY_ACCOUNT_STORE (obj), FALSE /* don't emit the insert signal. */); 
982         }
983         
984         return MODEST_TNY_ACCOUNT_STORE(obj);
985 }
986
987 static void
988 modest_tny_account_store_get_accounts  (TnyAccountStore *self, 
989                                         TnyList *list,
990                                         TnyGetAccountsRequestType request_type)
991 {
992         ModestTnyAccountStorePrivate *priv;
993         
994         g_return_if_fail (self);
995         g_return_if_fail (TNY_IS_LIST(list));
996         
997         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
998         
999         switch (request_type) {
1000         case TNY_ACCOUNT_STORE_BOTH:
1001                 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
1002                 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
1003                 break;
1004         case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
1005                 tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
1006                 break;
1007         case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
1008                 tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
1009                 break;
1010         default:
1011                 g_return_if_reached ();
1012         }
1013
1014         /* Initialize session. Why do we need this ??? */
1015         tny_session_camel_set_initialized (priv->session);
1016 }
1017
1018
1019 static const gchar*
1020 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
1021 {
1022         ModestTnyAccountStorePrivate *priv;
1023         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1024         
1025         if (!priv->cache_dir)
1026                 priv->cache_dir = g_build_filename (g_get_home_dir(), 
1027                                                     MODEST_DIR, MODEST_CACHE_DIR, NULL);
1028         return priv->cache_dir;
1029 }
1030
1031
1032 /*
1033  * callers need to unref
1034  */
1035 static TnyDevice*
1036 modest_tny_account_store_get_device (TnyAccountStore *self)
1037 {
1038         ModestTnyAccountStorePrivate *priv;
1039
1040         g_return_val_if_fail (self, NULL);
1041         
1042         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1043
1044         if (priv->device)
1045                 return g_object_ref (G_OBJECT(priv->device));
1046         else
1047                 return NULL;
1048 }
1049
1050
1051 static TnyAccount*
1052 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
1053 {
1054         return modest_tny_account_store_get_tny_account_by (MODEST_TNY_ACCOUNT_STORE (self), 
1055                                                             MODEST_TNY_ACCOUNT_STORE_QUERY_URL,
1056                                                             url_string);
1057 }
1058
1059
1060
1061 static gboolean
1062 modest_tny_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type,
1063                                 gboolean question, const GError *error)
1064 {
1065         g_return_val_if_fail (error, FALSE);
1066
1067         if ((error->domain != TNY_ACCOUNT_ERROR) 
1068                 && (error->domain != TNY_ACCOUNT_STORE_ERROR)) {
1069                 g_warning("modest: %s: Unexpected error domain: != TNY_ACCOUNT_ERROR: %d, message=%s", 
1070                         __FUNCTION__, error->domain, error->message); 
1071                         
1072                 return FALSE;
1073         }
1074         
1075         printf("DEBUG: %s: GError code: %d, message=%s\n", 
1076                                 __FUNCTION__, error->code, error->message);
1077         
1078         /* Get the server name: */
1079         const gchar* server_name = NULL;
1080         if (account && TNY_IS_ACCOUNT (account)) {
1081                 server_name = tny_account_get_hostname (account);
1082                 printf ("modest: %s: account name = %s, server_name=%s\n", __FUNCTION__, 
1083                         tny_account_get_id (account), server_name);
1084         }
1085         
1086         if (!server_name)
1087                 server_name = _("Unknown Server");      
1088                 
1089         ModestTransportStoreProtocol proto = MODEST_PROTOCOL_STORE_POP; /* Arbitrary default. */
1090         if (account) {
1091                 const gchar *proto_name = tny_account_get_proto (account);
1092                 if (proto_name)
1093                         proto = modest_protocol_info_get_transport_store_protocol (proto_name);
1094                 else {
1095                         g_warning("modest: %s: account with id=%s has no proto.\n", __FUNCTION__, 
1096                                 tny_account_get_id (account));
1097                 }
1098         }
1099                 
1100         /* const gchar *prompt = NULL; */
1101         gchar *prompt = NULL;
1102         switch (error->code) {
1103                 case TNY_ACCOUNT_STORE_ERROR_CANCEL_ALERT:
1104                 case TNY_ACCOUNT_ERROR_TRY_CONNECT_USER_CANCEL:
1105                         /* Don't show waste the user's time by showing him a dialog telling 
1106                          * him that he has just cancelled something: */
1107                         g_debug ("%s: Handling GError domain=%d, code=%d (cancelled) without showing a dialog, message=%s", 
1108                                 __FUNCTION__, error->domain, error->code, error->message);
1109                         prompt = NULL;
1110                         break;
1111                         
1112                 case TNY_ACCOUNT_ERROR_TRY_CONNECT_HOST_LOOKUP_FAILED:
1113                 case TNY_ACCOUNT_ERROR_TRY_CONNECT_SERVICE_UNAVAILABLE:
1114                         /* TODO: Show the appropriate message, depending on whether it's POP or IMAP: */
1115                         g_debug ("%s: Handling GError domain=%d, code=%d (lookup failed), message=%s", 
1116                                 __FUNCTION__, error->domain, error->code, error->message);
1117                                 
1118                         switch (proto) {
1119                                 case MODEST_PROTOCOL_STORE_POP:
1120                                         prompt = g_strdup_printf (_("emev_ni_ui_pop3_msg_connect_error"), server_name);
1121                                         break;
1122                                 case MODEST_PROTOCOL_STORE_IMAP:
1123                                         prompt = g_strdup_printf (_("emev_ni_ui_imap_connect_server_error"), server_name);
1124                                         break;
1125                                 case MODEST_PROTOCOL_TRANSPORT_SMTP:
1126                                 default: /* Arbitrary default. */
1127                                         prompt = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
1128                                         break;
1129                         }
1130         
1131                         /*
1132                         prompt = g_strdup_printf(
1133                                 _("Incorrect Account Settings:\n Host lookup failed.%s"), 
1134                                 error->message);
1135                         */
1136                         break;
1137                         
1138                 case TNY_ACCOUNT_ERROR_TRY_CONNECT_AUTHENTICATION_NOT_SUPPORTED:
1139                         g_debug ("%s: Handling GError domain=%d, code=%d (authentication not supported), message=%s", 
1140                                 __FUNCTION__, error->domain, error->code, error->message);
1141                         /*
1142                         A more helpful error message than what the UI spec wants
1143                         prompt = g_strdup_printf(
1144                                 _("Incorrect Account Settings:\nThe secure authentication method is not supported.\n%s"), 
1145                                 error->message);
1146                         */
1147
1148                         /* This is "Secure connection failed", even though the logical ID has _certificate_ in the name: */
1149                         prompt = g_strdup (_("mail_ni_ssl_certificate_error")); 
1150                         
1151                         break;
1152                         
1153                 case TNY_ACCOUNT_ERROR_TRY_CONNECT_CERTIFICATE:
1154                         g_debug ("%s: Handling GError domain=%d, code=%d (certificatae), message=%s", 
1155                                 __FUNCTION__, error->domain, error->code, error->message);
1156
1157                         /* TODO: This needs a logical ID and/or some specified way to ask the different certificate questions: */
1158                         prompt = g_strdup_printf(
1159                                 _("Certificate Problem:\n%s"), 
1160                                 error->message);
1161                         break;
1162                 
1163                 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
1164                 /* The tinymail camel implementation just sends us this for almost 
1165                  * everything, so we have to guess at the cause.
1166                  * It could be a wrong password, or inability to resolve a hostname, 
1167                  * or lack of network, or incorrect authentication method, or something entirely different: */
1168                 /* TODO: Fix camel to provide specific error codes, and then use the 
1169                  * specific dialog messages from Chapter 12 of the UI spec.
1170                  */
1171                 case TNY_ACCOUNT_STORE_ERROR_UNKNOWN_ALERT: 
1172                         /* This debug output is useful. Please keep it uncommented until 
1173                          * we have fixed the problems in this function: */
1174                         g_debug ("%s: Handling GError domain=%d, code=%d, message=%s", 
1175                                 __FUNCTION__, error->domain, error->code, error->message);
1176                         
1177                         /* TODO: Remove the internal error message for the real release.
1178                          * This is just so the testers can give us more information: */
1179                         /* However, I haven't seen this for a few weeks, so maybe the users 
1180                          * will never see it. murrayc. */
1181                         /* prompt = _("Modest account not yet fully configured."); */
1182                         prompt = g_strdup_printf(
1183                                 "%s\n (Internal error message, often very misleading):\n%s", 
1184                                 _("Incorrect Account Settings"), 
1185                                 error->message);
1186                                 
1187                         /* Note: If the password was wrong then get_password() would be called again,
1188                          * instead of this vfunc being called. */
1189                          
1190                         break;
1191                 
1192                 default:
1193                         g_warning ("%s: Unhandled GError code: %d, message=%s", 
1194                                 __FUNCTION__, error->code, error->message);
1195                         prompt = NULL;
1196                 break;
1197         }
1198         
1199         if (!prompt)
1200                 return FALSE;
1201
1202         ModestWindow *main_window = 
1203                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
1204         gboolean retval = TRUE;
1205         if (question) {
1206                 /* The Tinymail documentation says that we should show Yes and No buttons, 
1207                  * when it is a question.
1208                  * Obviously, we need tinymail to use more specific error codes instead,
1209                  * so we know what buttons to show. */
1210         
1211                 GtkWidget *dialog = GTK_WIDGET (hildon_note_new_confirmation (GTK_WINDOW (main_window), 
1212                         prompt));
1213                 const int response = gtk_dialog_run (GTK_DIALOG (dialog));
1214                 if (question) {
1215                         retval = (response == GTK_RESPONSE_YES) ||
1216                                          (response == GTK_RESPONSE_OK);
1217                 }
1218         
1219                 gtk_widget_destroy (dialog);
1220         
1221          } else {
1222                 /* Just show the error text and use the default response: */
1223                 modest_maemo_show_information_note_and_forget(GTK_WINDOW (main_window), 
1224                         prompt);
1225          }
1226         
1227         /* TODO: Don't free this when we no longer strdup the message for testers. */
1228         g_free (prompt);
1229
1230
1231         /* printf("DEBUG: %s: returning %d\n", __FUNCTION__, retval); */
1232         return retval;
1233 }
1234
1235
1236 static void
1237 modest_tny_account_store_init (gpointer g, gpointer iface_data)
1238 {
1239         TnyAccountStoreIface *klass;
1240
1241         g_return_if_fail (g);
1242
1243         klass = (TnyAccountStoreIface *)g;
1244
1245         klass->get_accounts_func =
1246                 modest_tny_account_store_get_accounts;
1247         klass->get_cache_dir_func =
1248                 modest_tny_account_store_get_cache_dir;
1249         klass->get_device_func =
1250                 modest_tny_account_store_get_device;
1251         klass->alert_func =
1252                 modest_tny_account_store_alert;
1253         klass->find_account_func =
1254                 modest_tny_account_store_find_account_by_url;
1255 }
1256
1257 void
1258 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
1259                                             ModestTnyGetPassFunc func)
1260 {
1261         /* not implemented, we use signals */
1262         g_printerr ("modest: set_get_pass_func not implemented\n");
1263 }
1264
1265 TnySessionCamel*
1266 modest_tny_account_store_get_session  (TnyAccountStore *self)
1267 {
1268         g_return_val_if_fail (self, NULL);
1269         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
1270 }
1271
1272 static TnyAccount*
1273 get_tny_account_by (TnyList *accounts,
1274                     ModestTnyAccountStoreQueryType type,
1275                     const gchar *str)
1276 {
1277         TnyIterator *iter = NULL;
1278         gboolean found = FALSE;
1279         TnyAccount *retval = NULL;
1280
1281         iter = tny_list_create_iterator (accounts);
1282         while (!tny_iterator_is_done (iter) && !found) {
1283                 TnyAccount *tmp_account = NULL;
1284                 const gchar *val = NULL;
1285
1286                 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1287                 switch (type) {
1288                 case MODEST_TNY_ACCOUNT_STORE_QUERY_ID:
1289                         val = tny_account_get_id (tmp_account);
1290                         break;
1291                 case MODEST_TNY_ACCOUNT_STORE_QUERY_URL:
1292                         val = tny_account_get_url_string (tmp_account);
1293                         break;
1294                 }
1295                 
1296                 if (type == MODEST_TNY_ACCOUNT_STORE_QUERY_URL && 
1297                     tny_account_matches_url_string (tmp_account, str)) {
1298                         retval = g_object_ref (tmp_account);
1299                         found = TRUE;
1300                 } else {
1301                         if (strcmp (val, str) == 0) {
1302                                 retval = g_object_ref (tmp_account);
1303                                 found = TRUE;
1304                         }
1305                 }
1306                 g_object_unref (tmp_account);
1307                 tny_iterator_next (iter);
1308         }
1309         g_object_unref (iter);
1310
1311         return retval;
1312 }
1313
1314 TnyAccount*
1315 modest_tny_account_store_get_tny_account_by (ModestTnyAccountStore *self, 
1316                                              ModestTnyAccountStoreQueryType type,
1317                                              const gchar *str)
1318 {
1319         TnyAccount *account = NULL;
1320         ModestTnyAccountStorePrivate *priv;     
1321         
1322         g_return_val_if_fail (self, NULL);
1323         g_return_val_if_fail (str, NULL);
1324         
1325         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1326         
1327         /* Search in store accounts */
1328         account = get_tny_account_by (priv->store_accounts, type, str);
1329
1330         /* If we already found something, no need to search the transport accounts */
1331         if (!account) {
1332                 account = get_tny_account_by (priv->transport_accounts, type, str);
1333
1334                 /* If we already found something, no need to search the
1335                    per-account outbox accounts */
1336                 if (!account)
1337                         account = get_tny_account_by (priv->store_accounts_outboxes, type, str);
1338         }
1339
1340         /* Warn if nothing was found. This is generally unusual. */
1341         if (!account) {
1342                 g_warning("%s: Failed to find account with %s=%s\n", 
1343                           __FUNCTION__, 
1344                           (type == MODEST_TNY_ACCOUNT_STORE_QUERY_ID) ? "ID" : "URL",                     
1345                           str);
1346         }
1347
1348         /* Returns a new reference to the account if found */   
1349         return account;
1350 }
1351
1352 TnyAccount*
1353 modest_tny_account_store_get_server_account (ModestTnyAccountStore *self,
1354                                              const gchar *account_name,
1355                                              TnyAccountType type)
1356 {
1357         ModestTnyAccountStorePrivate *priv = NULL;
1358         TnyAccount *retval = NULL;
1359         TnyList *account_list = NULL;
1360         TnyIterator *iter = NULL;
1361         gboolean found;
1362
1363         g_return_val_if_fail (self, NULL);
1364         g_return_val_if_fail (account_name, NULL);
1365         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || 
1366                               type == TNY_ACCOUNT_TYPE_TRANSPORT,
1367                               NULL);
1368         
1369         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1370
1371         account_list = (type == TNY_ACCOUNT_TYPE_STORE) ? 
1372                 priv->store_accounts : 
1373                 priv->transport_accounts;
1374
1375         if (!account_list) {
1376                 g_printerr ("%s: No server accounts of type %s\n", __FUNCTION__, 
1377                         (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport");
1378                 return NULL;
1379         }
1380         
1381         /* Look for the server account */
1382         found = FALSE;
1383         iter = tny_list_create_iterator (account_list);
1384         while (!tny_iterator_is_done (iter) && !found) {
1385                 const gchar *modest_acc_name;
1386                 TnyAccount *tmp_account;
1387
1388                 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1389                 modest_acc_name = 
1390                         modest_tny_account_get_parent_modest_account_name_for_server_account (tmp_account);
1391                 
1392                 if (!strcmp (account_name, modest_acc_name)) {
1393                         found = TRUE;
1394                         retval = g_object_ref (tmp_account);
1395                 }
1396                 /* Free and continue */
1397                 g_object_unref (tmp_account);
1398                 tny_iterator_next (iter);
1399         }
1400
1401         if (!found) {
1402                 g_printerr ("modest: %s: could not get tny %s account for %s\n." \
1403                             "Number of server accounts of this type=%d\n", __FUNCTION__,
1404                             (type == TNY_ACCOUNT_TYPE_STORE) ? "store" : "transport",
1405                             account_name, tny_list_get_length (account_list));
1406         }
1407
1408         /* Returns a new reference */
1409         return retval;
1410 }
1411
1412 static TnyAccount*
1413 get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self,
1414         const gchar *account_name)
1415 {
1416         /* Get the current connection: */
1417         TnyDevice *device = modest_runtime_get_device ();
1418         
1419         if (!tny_device_is_online (device))
1420                 return NULL;
1421
1422         g_return_val_if_fail (self, NULL);
1423         
1424         
1425 #ifdef MODEST_PLATFORM_MAEMO
1426         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
1427         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);    
1428         const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
1429         /* printf ("DEBUG: %s: iap_id=%s\n", __FUNCTION__, iap_id); */
1430         if (!iap_id)
1431                 return NULL;
1432                 
1433         ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
1434         if (!connection)
1435                 return NULL;
1436                 
1437         const gchar *connection_name = con_ic_iap_get_name (connection);
1438         /* printf ("DEBUG: %s: connection_name=%s\n", __FUNCTION__, connection_name); */
1439         if (!connection_name)
1440                 return NULL;
1441         
1442         /*  Get the connection-specific transport acccount, if any: */
1443         ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
1444
1445         /* Check if this account has connection-specific SMTP enabled */
1446         if (!modest_account_mgr_get_use_connection_specific_smtp (account_manager, account_name)) {
1447                 return NULL;
1448         }
1449
1450         gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager, 
1451                 connection_name);
1452
1453         /* printf ("DEBUG: %s: server_account_name=%s\n", __FUNCTION__, server_account_name); */
1454         if (!server_account_name) {
1455                 return NULL; /* No connection-specific SMTP server was specified for this connection. */
1456         }
1457                 
1458         TnyAccount* account = modest_tny_account_store_get_tny_account_by (self, 
1459                                                                            MODEST_TNY_ACCOUNT_STORE_QUERY_ID, 
1460                                                                            server_account_name);
1461
1462         /* printf ("DEBUG: %s: account=%p\n", __FUNCTION__, account); */
1463         g_free (server_account_name);   
1464
1465         /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
1466         g_object_unref (connection);
1467         
1468         return account;
1469 #else
1470         return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
1471 #endif /* MODEST_PLATFORM_MAEMO */
1472 }
1473
1474                                                                  
1475 TnyAccount*
1476 modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
1477                                                                     const gchar *account_name)
1478 {
1479         g_return_val_if_fail (self, NULL);
1480         g_return_val_if_fail (account_name, NULL);
1481
1482         if (!account_name || !self)
1483                 return NULL;
1484         
1485         /*  Get the connection-specific transport acccount, if any: */
1486         /* Note: This gives us a reference: */
1487         TnyAccount *account =
1488                 get_smtp_specific_transport_account_for_open_connection (self, account_name);
1489                         
1490         /* If there is no connection-specific transport account (the common case), 
1491          * just get the regular transport account: */
1492         if (!account) {
1493                 /* The special local folders don't have transport accounts. */
1494                 if (strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) == 0)
1495                         account = NULL;
1496                 else {
1497                         /* Note: This gives us a reference: */
1498                         account = modest_tny_account_store_get_server_account (self, account_name, 
1499                                                      TNY_ACCOUNT_TYPE_TRANSPORT);
1500                 }
1501         }
1502                         
1503         /* returns a reference. */     
1504         return account;
1505 }
1506
1507 TnyAccount*
1508 modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
1509 {
1510         TnyAccount *account = NULL;
1511         ModestTnyAccountStorePrivate *priv;
1512         TnyIterator *iter;
1513         gboolean found;
1514
1515         g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
1516         
1517         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1518
1519         found = FALSE;
1520         iter = tny_list_create_iterator (priv->store_accounts);
1521         while (!tny_iterator_is_done (iter) && !found) {
1522                 TnyAccount *tmp_account;
1523
1524                 tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
1525                 if (modest_tny_account_is_virtual_local_folders (tmp_account)) {
1526                         account = g_object_ref (tmp_account);
1527                         found = TRUE;
1528                 }
1529                 g_object_unref (tmp_account);
1530                 tny_iterator_next (iter);
1531         }
1532         g_object_unref (iter);
1533
1534         /* Returns a new reference to the account */
1535         return account;
1536 }
1537
1538 /*********************************************************************************/
1539 static void
1540 add_existing_accounts (ModestTnyAccountStore *self)
1541 {
1542         GSList *account_names = NULL, *iter = NULL;
1543         ModestTnyAccountStorePrivate *priv = NULL;
1544         
1545         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1546
1547         /* These are account names, not server_account names */
1548         account_names = modest_account_mgr_account_names (priv->account_mgr, FALSE);
1549
1550         for (iter = account_names; iter != NULL; iter = g_slist_next (iter)) {
1551                 const gchar *account_name = (const gchar*) iter->data;
1552                 
1553                 /* Insert all enabled accounts without notifying */
1554                 if (modest_account_mgr_get_enabled (priv->account_mgr, account_name))
1555                         insert_account (self, account_name, FALSE);
1556         }
1557         modest_account_mgr_free_account_names (account_names);
1558 }
1559
1560 static TnyAccount*
1561 create_tny_account (ModestTnyAccountStore *self,
1562                     const gchar *name,
1563                     TnyAccountType type)
1564 {
1565         TnyAccount *account = NULL;
1566         ModestTnyAccountStorePrivate *priv = NULL;
1567         
1568         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1569
1570         account = modest_tny_account_new_from_account (priv->account_mgr,
1571                                                        name, type, 
1572                                                        priv->session,
1573                                                        get_password,
1574                                                        forget_password);
1575
1576         if (account) {
1577                 /* Forget any cached password for the account, so that
1578                    we use a new account if any */
1579                 modest_tny_account_store_forget_password_in_memory (self, 
1580                                                                     tny_account_get_id (account));
1581                 /* Set the account store */                             
1582                 g_object_set_data (G_OBJECT(account), "account_store", self);
1583         } else {
1584                 g_printerr ("modest: failed to create account for %s\n", name);
1585         }
1586
1587         return account;
1588 }
1589
1590 /*
1591  * This function will be used for both adding new accounts and for the
1592  * initialization. In the initialization we do not want to emit
1593  * signals so notify will be FALSE, in the case of account additions
1594  * we do want to notify the observers
1595  */
1596 static void
1597 insert_account (ModestTnyAccountStore *self,
1598                 const gchar *account,
1599                 gboolean notify)
1600 {
1601         ModestTnyAccountStorePrivate *priv = NULL;
1602         TnyAccount *store_account = NULL, *transport_account = NULL;
1603         
1604         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1605
1606         /* Get the server and the transport account */
1607         store_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_STORE);
1608         transport_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
1609
1610         /* Add to the list, and notify the observers */
1611         if (store_account) {
1612                 tny_list_append (priv->store_accounts, G_OBJECT (store_account));
1613                 if (notify)
1614                         g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
1615                 g_object_unref (store_account);
1616         }
1617
1618         /* Add to the list, and notify the observers */
1619         if (transport_account) {
1620                 TnyAccount *account_outbox = NULL, *local_account = NULL;
1621                 TnyFolder *per_account_outbox = NULL;
1622                 TnyList *folders;
1623                 TnyIterator *iter_folders;
1624
1625                 /* Add account to the list */
1626                 tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
1627
1628                 /* Add connection-specific transport accounts */
1629                 add_connection_specific_transport_accounts (self);
1630
1631                 /* Create per account local outbox */
1632                 account_outbox = 
1633                         modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr, 
1634                                                                                     account, 
1635                                                                                     priv->session);
1636                 tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
1637
1638                 /* Get the outbox folder */
1639                 folders = tny_simple_list_new ();
1640                 tny_folder_store_get_folders (TNY_FOLDER_STORE (account_outbox),
1641                                               folders, NULL, NULL);
1642                 if (tny_list_get_length (folders) != 1)
1643                         g_warning ("%s: there should be only one outbox folder, but found %d!",
1644                                    __FUNCTION__, tny_list_get_length (folders));
1645                 
1646                 iter_folders = tny_list_create_iterator (folders);
1647                 per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
1648                 g_object_unref (iter_folders);
1649                 g_object_unref (folders);
1650                 g_object_unref (account_outbox);
1651
1652                 /* Add the outbox of the new per-account-local-outbox
1653                    account to the global local merged OUTBOX of the
1654                    local folders account */
1655                 local_account = modest_tny_account_store_get_local_folders_account (MODEST_TNY_ACCOUNT_STORE (self));
1656                 modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account), 
1657                                                                        per_account_outbox);
1658                 /* Add the pair to the hash table */
1659                 g_hash_table_insert (priv->outbox_of_transport,
1660                                      transport_account,
1661                                      per_account_outbox);
1662
1663                 /* Notify that the local folders account chaned */
1664                 if (notify)
1665                         g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1666
1667                 g_object_unref (local_account);
1668                 g_object_unref (per_account_outbox);
1669
1670                 /* Notify the observers */
1671                 if (notify)
1672                         g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
1673                 g_object_unref (transport_account);
1674         }
1675 }
1676
1677 static void
1678 on_account_inserted (ModestAccountMgr *acc_mgr, 
1679                      const gchar *account,
1680                      gpointer user_data)
1681 {
1682         /* Insert the account and notify the observers */
1683         insert_account (MODEST_TNY_ACCOUNT_STORE (user_data), account, TRUE);
1684 }
1685
1686 static void
1687 on_account_removed (ModestAccountMgr *acc_mgr, 
1688                     const gchar *account,
1689                     gpointer user_data)
1690 {
1691         TnyAccount *store_account = NULL, *transport_account = NULL;
1692         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
1693         
1694         /* Get the server and the transport account */
1695         store_account = 
1696                 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
1697         transport_account = 
1698                 modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
1699
1700         /* If there was any problem creating the account, for example,
1701            with the configuration system this could not exist */
1702         if (store_account) {
1703                 /* Clear the cache */
1704                 tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
1705
1706                 /* Notify the observers */
1707                 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
1708                 g_object_unref (store_account);
1709         } else {
1710                 g_warning ("There is no store account for account %s\n", account);
1711         }
1712
1713         /* If there was any problem creating the account, for example,
1714            with the configuration system this could not exist */
1715         if (transport_account) {
1716                 TnyAccount *local_account = NULL;
1717                 TnyFolder *outbox = NULL;
1718                 ModestTnyAccountStorePrivate *priv = NULL;
1719         
1720                 /* Remove the OUTBOX of the account from the global outbox */
1721                 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1722                 outbox = g_hash_table_lookup (priv->outbox_of_transport, transport_account);
1723
1724                 if (TNY_IS_FOLDER (outbox)) {
1725                         local_account = modest_tny_account_store_get_local_folders_account (self);
1726                         modest_tny_local_folders_account_remove_folder_from_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
1727                                                                                     outbox);
1728                         g_hash_table_remove (priv->outbox_of_transport, transport_account);
1729
1730                         /* Notify the change in the local account */
1731                         g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
1732                         g_object_unref (local_account);
1733                 } else {
1734                         g_warning ("Removing a transport account that has no outbox");
1735                 }
1736
1737                 /* Notify the observers */
1738                 g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
1739                 g_object_unref (transport_account);
1740         } else {
1741                 g_warning ("There is no transport account for account %s\n", account);
1742         }
1743 }
1744
1745 static void
1746 add_connection_specific_transport_accounts (ModestTnyAccountStore *self)
1747 {
1748         ModestTnyAccountStorePrivate *priv = NULL;
1749         GSList *list_specifics = NULL, *iter = NULL;
1750
1751         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
1752
1753         ModestConf *conf = modest_runtime_get_conf ();
1754
1755         GError *err = NULL;
1756         list_specifics = modest_conf_get_list (conf,
1757                                                       MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
1758                                                       MODEST_CONF_VALUE_STRING, &err);
1759         if (err) {
1760                 g_printerr ("modest: %s: error getting list: %s\n.", __FUNCTION__, err->message);
1761                 g_error_free (err);
1762                 err = NULL;
1763         }
1764                                 
1765         /* Look at each connection-specific transport account for the 
1766          * modest account: */
1767         iter = list_specifics;
1768         while (iter) {
1769                 /* The list alternates between the connection name and the transport name: */
1770                 iter = g_slist_next (iter);
1771                 if (iter) {
1772                         const gchar* transport_account_name = (const gchar*) (iter->data);
1773                         if (transport_account_name) {
1774                                 TnyAccount * tny_account = NULL;
1775                                 /* Add the account: */
1776                                 tny_account = 
1777                                         modest_tny_account_new_from_server_account_name (priv->account_mgr, 
1778                                                                                          priv->session, 
1779                                                                                          transport_account_name);
1780                                 if (tny_account) {
1781                                         g_object_set_data (G_OBJECT(tny_account), 
1782                                                            "account_store", 
1783                                                            (gpointer)self);
1784
1785                                         tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
1786                                 } else
1787                                         g_printerr ("modest: failed to create smtp-specific account for %s\n",
1788                                                     transport_account_name);
1789                         }
1790                 }                               
1791                 iter = g_slist_next (iter);
1792         }
1793 }