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