e8fd54d3bcf0950df6bf815f974e33fd8f91ddcd
[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 #endif
60
61 /* 'private'/'protected' functions */
62 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
63 //static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
64 static void modest_tny_account_store_finalize     (GObject *obj);
65
66 /* implementations for tny-account-store-iface */
67 static void    modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
68 static void    modest_tny_account_store_init          (gpointer g, gpointer iface_data);
69
70
71 /* list my signals */
72 enum {
73         ACCOUNT_UPDATE_SIGNAL,
74         PASSWORD_REQUESTED_SIGNAL,
75         LAST_SIGNAL
76 };
77
78 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
79 struct _ModestTnyAccountStorePrivate {
80         gchar              *cache_dir;  
81         GHashTable         *password_hash;
82         ModestAccountMgr   *account_mgr;
83         TnySessionCamel    *session;
84         TnyDevice          *device;
85         
86         /* we cache them here */
87         GSList             *store_accounts;
88         GSList             *transport_accounts;
89 };
90
91 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
92                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
93                                                       ModestTnyAccountStorePrivate))
94
95 /* globals */
96 static GObjectClass *parent_class = NULL;
97
98 static guint signals[LAST_SIGNAL] = {0};
99
100 GType
101 modest_tny_account_store_get_type (void)
102 {
103         static GType my_type = 0;
104
105         if (!my_type) {
106                 static const GTypeInfo my_info = {
107                         sizeof(ModestTnyAccountStoreClass),
108                         NULL,           /* base init */
109                         NULL,           /* base finalize */
110                         (GClassInitFunc) modest_tny_account_store_class_init,
111                         NULL,           /* class finalize */
112                         NULL,           /* class data */
113                         sizeof(ModestTnyAccountStore),
114                         0,              /* n_preallocs */
115                         (GInstanceInitFunc) modest_tny_account_store_instance_init,
116                         NULL
117                 };
118
119                 static const GInterfaceInfo iface_info = {
120                         (GInterfaceInitFunc) modest_tny_account_store_init,
121                         NULL,         /* interface_finalize */
122                         NULL          /* interface_data */
123                 };
124                 /* hack hack */
125                 my_type = g_type_register_static (G_TYPE_OBJECT,
126                                                   "ModestTnyAccountStore",
127                                                   &my_info, 0);
128                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
129                                              &iface_info);
130         }
131         return my_type;
132 }
133
134 static void
135 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
136 {
137         GObjectClass *gobject_class;
138         gobject_class = (GObjectClass*) klass;
139
140         parent_class            = g_type_class_peek_parent (klass);
141         gobject_class->finalize = modest_tny_account_store_finalize;
142
143         g_type_class_add_private (gobject_class,
144                                   sizeof(ModestTnyAccountStorePrivate));
145
146          signals[ACCOUNT_UPDATE_SIGNAL] =
147                 g_signal_new ("account_update",
148                               G_TYPE_FROM_CLASS (gobject_class),
149                               G_SIGNAL_RUN_FIRST,
150                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
151                               NULL, NULL,
152                               g_cclosure_marshal_VOID__STRING,
153                               G_TYPE_NONE, 1, G_TYPE_STRING);
154          
155          signals[PASSWORD_REQUESTED_SIGNAL] =
156                  g_signal_new ("password_requested",
157                                G_TYPE_FROM_CLASS (gobject_class),
158                                G_SIGNAL_RUN_FIRST,
159                                G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
160                                NULL, NULL,
161                                modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
162                                G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER,
163                                G_TYPE_POINTER);
164 }
165
166
167 static void
168 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
169 {
170         ModestTnyAccountStorePrivate *priv =
171                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
172
173         priv->cache_dir              = NULL;
174         priv->account_mgr            = NULL;
175         priv->session                = NULL;
176         priv->device                 = NULL;
177         
178         priv->password_hash          = g_hash_table_new_full (g_str_hash, g_str_equal,
179                                                               g_free, g_free);
180 }
181
182
183
184 static void
185 account_list_free (GSList *accounts)
186 {
187         GSList *cursor = accounts;
188
189         while (cursor) {
190                 if (G_IS_OBJECT(cursor->data)) { /* check twice... */
191                         const gchar *id = tny_account_get_id(TNY_ACCOUNT(cursor->data));
192                         modest_runtime_verify_object_last_ref(cursor->data,id);
193                 }                       
194                 g_object_unref (G_OBJECT(cursor->data));
195                 cursor = cursor->next;
196         }
197         g_slist_free (accounts);
198 }
199
200
201 static void
202 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
203                     gpointer user_data)
204 {
205         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(user_data);
206         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
207
208         /* FIXME: make this more finegrained; changes do not really affect _all_
209          * accounts, and some do not affect tny accounts at all (such as 'last_update')
210          */
211         
212         
213         account_list_free (priv->store_accounts);
214         priv->store_accounts = NULL;
215         
216         account_list_free (priv->transport_accounts);
217         priv->transport_accounts = NULL;
218         
219         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
220                        account);
221 }
222
223
224 static void
225 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
226                     const gchar *key, gpointer user_data)
227 {
228         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
229         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
230         
231         /* FIXME: make this more finegrained; changes do not really affect _all_
232          * accounts, and some do not affect tny accounts at all (such as 'last_update')
233          */
234         if (priv->store_accounts) {
235                 account_list_free (priv->store_accounts);
236                 priv->store_accounts = NULL;
237         }
238         
239         if (priv->transport_accounts) {
240                 account_list_free (priv->transport_accounts);
241                 priv->transport_accounts = NULL;
242         }
243
244         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
245                        account);
246 }
247
248
249 static ModestTnyAccountStore*
250 get_account_store_for_account (TnyAccount *account)
251 {
252         return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
253                                                            "account_store"));
254 }
255
256 static gchar*
257 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
258 {
259         /* printf("DEBUG: %s\n", __FUNCTION__); */
260         
261         const gchar *key;
262         const TnyAccountStore *account_store;
263         ModestTnyAccountStore *self;
264         ModestTnyAccountStorePrivate *priv;
265         gchar *pwd = NULL;
266         gpointer pwd_ptr;
267         gboolean already_asked;
268         
269         key           = tny_account_get_id (account);
270         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
271         
272         self = MODEST_TNY_ACCOUNT_STORE (account_store);
273         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
274         
275         /* is it in the hash? if it's already there, it must be wrong... */
276         pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
277                                    * type-punned ptrs...*/
278         already_asked = g_hash_table_lookup_extended (priv->password_hash,
279                                                       key,
280                                                       NULL,
281                                                       (gpointer*)&pwd_ptr);
282
283         /* if the password is not already there, try ModestConf */
284         if (!already_asked) {
285                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
286                                                       key, MODEST_ACCOUNT_PASSWORD,TRUE);
287                 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
288         }
289
290         /* if it was already asked, it must have been wrong, so ask again */
291         if (already_asked || !pwd || strlen(pwd) == 0) {
292
293                 /* we don't have it yet. Get the password from the user */
294                 const gchar* name = tny_account_get_name (account);
295                 gboolean remember = FALSE;
296                 pwd = NULL;
297                 
298                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
299                                name, &pwd, cancel, &remember);
300                 
301                 if (!*cancel) {
302                         if (remember)
303                                 modest_account_mgr_set_string (priv->account_mgr,key,
304                                                                MODEST_ACCOUNT_PASSWORD,
305                                                                pwd, TRUE);
306                         /* We need to dup the string even knowing that
307                            it's already a dup of the contents of an
308                            entry, because it if it's wrong, then camel
309                            will free it */
310                         g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
311                 } else {
312                         g_hash_table_remove (priv->password_hash, key);
313                         g_free (pwd);
314                         pwd = NULL;
315                 }
316         } else
317                 *cancel = FALSE;
318  
319     /* printf("  DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
320         
321         return pwd;
322 }
323
324
325 static void
326 forget_password (TnyAccount *account)
327 {
328         ModestTnyAccountStore *self;
329         ModestTnyAccountStorePrivate *priv;
330         const TnyAccountStore *account_store;
331         gchar *pwd;
332         const gchar *key;
333         
334         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
335         self = MODEST_TNY_ACCOUNT_STORE (account_store);
336         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
337         key  = tny_account_get_id (account);
338
339         /* Do not remove the key, this will allow us to detect that we
340            have already asked for it at least once */
341         pwd = g_hash_table_lookup (priv->password_hash, key);
342         if (pwd) {
343                 memset (pwd, 0, strlen (pwd));
344                 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
345         }
346
347         /* Remove from configuration system */
348         modest_account_mgr_unset (priv->account_mgr,
349                                   key, MODEST_ACCOUNT_PASSWORD, TRUE);
350 }
351
352
353 static void
354 modest_tny_account_store_finalize (GObject *obj)
355 {
356         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
357         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
358         
359         //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
360
361         g_free (priv->cache_dir);
362         priv->cache_dir = NULL;
363         
364         if (priv->password_hash) {
365                 g_hash_table_destroy (priv->password_hash);
366                 priv->password_hash = NULL;
367         }
368
369         if (priv->account_mgr) {
370                 g_object_unref (G_OBJECT(priv->account_mgr));
371                 priv->account_mgr = NULL;
372         }
373
374         if (priv->device) {
375                 g_object_unref (G_OBJECT(priv->device));
376                 priv->device = NULL;
377         }
378
379         /* this includes the local folder */
380         account_list_free (priv->store_accounts);
381         priv->store_accounts = NULL;
382         
383         account_list_free (priv->transport_accounts);
384         priv->transport_accounts = NULL;
385
386         if (priv->session) {
387                 camel_object_unref (CAMEL_OBJECT(priv->session));
388                 priv->session = NULL;
389         }
390         
391         G_OBJECT_CLASS(parent_class)->finalize (obj);
392 }
393
394
395 ModestTnyAccountStore*
396 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
397
398         GObject *obj;
399         ModestTnyAccountStorePrivate *priv;
400         TnyList *list; 
401         
402         g_return_val_if_fail (account_mgr, NULL);
403         g_return_val_if_fail (device, NULL);
404
405         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
406         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
407
408         priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
409         priv->device = g_object_ref (device);
410         
411         priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
412         
413         tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
414         /* FIXME: unref this in the end? */
415         tny_session_camel_set_async_connecting (priv->session, TRUE);
416         
417         /* force a cache fill... ugly */
418         list = TNY_LIST(tny_simple_list_new());
419         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
420                                         TNY_ACCOUNT_STORE_BOTH);
421         g_object_unref(list);
422         
423         /* Connect signals */
424         g_signal_connect (G_OBJECT(account_mgr), "account_changed",
425                                        G_CALLBACK (on_account_changed), obj);
426         g_signal_connect (G_OBJECT(account_mgr), "account_removed",
427                                        G_CALLBACK (on_account_removed), obj);
428
429         return MODEST_TNY_ACCOUNT_STORE(obj);
430 }
431
432
433 static void
434 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
435 {
436         ModestTnyAccountStorePrivate *priv;
437         GSList                       *accounts, *cursor;
438         
439         priv     = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
440         accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
441
442         cursor = accounts;
443         while (cursor) {
444                 tny_list_prepend (list, G_OBJECT(cursor->data));
445                 cursor = cursor->next;
446         }
447 }
448
449
450
451 /* this function fills the TnyList, and also returns a GSList of the accounts,
452  * for caching purposes
453  */
454 static GSList*
455 get_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
456 {
457         ModestTnyAccountStorePrivate *priv = NULL;
458         GSList                       *account_names = NULL, *cursor = NULL;
459         GSList                       *accounts = NULL;
460         
461         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
462  
463         account_names = modest_account_mgr_account_names (priv->account_mgr, 
464                 TRUE /* including disabled accounts */);
465         
466         for (cursor = account_names; cursor; cursor = cursor->next) {
467                 
468                 gchar *account_name = (gchar*)cursor->data;
469                 
470                 /* only return enabled accounts */
471                 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
472                         TnyAccount *tny_account = 
473                                 modest_tny_account_new_from_account (priv->account_mgr,
474                                                                      account_name,
475                                                                      type, priv->session,
476                                                                      get_password,
477                                                                      forget_password);
478                         if (tny_account) {
479                                 g_object_set_data (G_OBJECT(tny_account), "account_store",
480                                                    (gpointer)self);
481                                 tny_list_prepend (list, G_OBJECT(tny_account));
482                                 accounts = g_slist_append (accounts, tny_account); /* cache it */
483                         } else
484                                 g_printerr ("modest: failed to create account for %s\n",
485                                             account_name);
486                 }
487                 g_free (account_name);
488         }
489         g_slist_free (account_names);
490
491         /* also, add the local folder pseudo-account */
492         if (type == TNY_ACCOUNT_TYPE_STORE) {
493                 TnyAccount *tny_account =
494                         modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
495                 tny_list_prepend (list, G_OBJECT(tny_account));
496                 accounts = g_slist_append (accounts, tny_account); /* cache it */
497         }       
498         return accounts;
499 }       
500
501
502 static void
503 modest_tny_account_store_get_accounts  (TnyAccountStore *self, TnyList *list,
504                                         TnyGetAccountsRequestType request_type)
505 {
506         ModestTnyAccountStorePrivate *priv;
507         
508         g_return_if_fail (self);
509         g_return_if_fail (TNY_IS_LIST(list));
510         
511         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
512         
513         if (request_type == TNY_ACCOUNT_STORE_BOTH) {
514                 modest_tny_account_store_get_accounts (self, list,
515                                                        TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
516                 modest_tny_account_store_get_accounts (self, list,
517                                                        TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
518                 return;
519         }
520         
521         if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS)  {
522                 
523                 if (!priv->store_accounts)
524                         priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
525                 else
526                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
527
528         } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
529
530                 if (!priv->transport_accounts)
531                         priv->transport_accounts =
532                                 get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
533                 else
534                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
535         } else
536                 g_return_if_reached (); /* incorrect req type */
537 }
538
539
540 static const gchar*
541 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
542 {
543         ModestTnyAccountStorePrivate *priv;
544         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
545         
546         if (!priv->cache_dir)
547                 priv->cache_dir = g_build_filename (g_get_home_dir(), 
548                                                     MODEST_DIR, MODEST_CACHE_DIR, NULL);
549         return priv->cache_dir;
550 }
551
552
553 /*
554  * callers need to unref
555  */
556 static TnyDevice*
557 modest_tny_account_store_get_device (TnyAccountStore *self)
558 {
559         ModestTnyAccountStorePrivate *priv;
560
561         g_return_val_if_fail (self, NULL);
562         
563         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
564
565         if (priv->device)
566                 return g_object_ref (G_OBJECT(priv->device));
567         else
568                 return NULL;
569 }
570
571
572 static TnyAccount*
573 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
574 {
575         TnyAccount *account = NULL;
576         ModestTnyAccountStorePrivate *priv;     
577         GSList *cursor;
578         
579         g_return_val_if_fail (self, NULL);
580         g_return_val_if_fail (url_string, NULL);
581         
582         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
583
584         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
585                 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
586                         account = TNY_ACCOUNT(cursor->data);
587                         break;
588                 }
589         }
590
591         if (!account) {
592                 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
593                         if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
594                                 account = TNY_ACCOUNT(cursor->data);
595                                 break;
596                         }
597                 }
598         }
599
600         if (account)
601                 g_object_ref (G_OBJECT(account));
602
603         return account;
604 }
605
606
607
608 static gboolean
609 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
610                                 const GError *error)
611 {
612         g_return_val_if_fail (error, FALSE);
613         
614         printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message);
615         
616         GtkMessageType gtktype;
617         gboolean retval = FALSE;
618         GtkWidget *dialog;
619
620         switch (type)
621         {
622                 case TNY_ALERT_TYPE_INFO:
623                 gtktype = GTK_MESSAGE_INFO;
624                 break;
625                 case TNY_ALERT_TYPE_WARNING:
626                 gtktype = GTK_MESSAGE_WARNING;
627                 break;
628                 case TNY_ALERT_TYPE_ERROR:
629                 default:
630                 gtktype = GTK_MESSAGE_ERROR;
631                 break;
632         }
633         
634         const gchar *prompt = NULL;
635         switch (error->code)
636         {
637                 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
638                         /* Use a Logical ID: */
639                         prompt = _("Modest account not yet fully configured");
640                         break;
641                 default:
642                         g_warning ("%s: Unhandled GError code.", __FUNCTION__);
643                         prompt = NULL;
644                 break;
645         }
646         
647         if (!prompt)
648                 return FALSE;
649
650         dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
651                 gtktype, GTK_BUTTONS_YES_NO, prompt);
652
653         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
654                 retval = TRUE;
655
656         gtk_widget_destroy (dialog);
657
658         return retval;
659 }
660
661
662 static void
663 modest_tny_account_store_init (gpointer g, gpointer iface_data)
664 {
665         TnyAccountStoreIface *klass;
666
667         g_return_if_fail (g);
668
669         klass = (TnyAccountStoreIface *)g;
670
671         klass->get_accounts_func =
672                 modest_tny_account_store_get_accounts;
673         klass->get_cache_dir_func =
674                 modest_tny_account_store_get_cache_dir;
675         klass->get_device_func =
676                 modest_tny_account_store_get_device;
677         klass->alert_func =
678                 modest_tny_account_store_alert;
679         klass->find_account_func =
680                 modest_tny_account_store_find_account_by_url;
681 }
682
683 void
684 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
685                                             ModestTnyGetPassFunc func)
686 {
687         /* not implemented, we use signals */
688         g_printerr ("modest: set_get_pass_func not implemented\n");
689 }
690
691 TnySessionCamel*
692 tny_account_store_get_session  (TnyAccountStore *self)
693 {
694         g_return_val_if_fail (self, NULL);
695         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
696 }
697
698
699 TnyAccount*
700 modest_tny_account_store_get_tny_account_by_id  (ModestTnyAccountStore *self, const gchar *id)
701 {
702         TnyAccount *account = NULL;
703         ModestTnyAccountStorePrivate *priv;     
704         GSList *cursor;
705
706         g_return_val_if_fail (self, NULL);
707         g_return_val_if_fail (id, NULL);
708         
709         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
710
711         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
712                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
713                 if (acc_id && strcmp (acc_id, id) == 0) { 
714                         account = TNY_ACCOUNT(cursor->data);
715                         break;
716                 }
717         }
718
719         /* if we already found something, no need to search the transport accounts */
720         for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
721                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
722                 if (acc_id && strcmp (acc_id, id) == 0) {
723                         account = TNY_ACCOUNT(cursor->data);
724                         break;
725                 }
726         }
727
728         if (account)
729                 g_object_ref (G_OBJECT(account));
730         
731         return account;
732 }
733
734 TnyAccount*
735 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
736                                                      const gchar *account_name,
737                                                      TnyAccountType type)
738 {
739         TnyAccount *account = NULL;
740         ModestAccountData *account_data;
741         const gchar *id = NULL;
742         ModestTnyAccountStorePrivate *priv;     
743
744         g_return_val_if_fail (self, NULL);
745         g_return_val_if_fail (account_name, NULL);
746         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
747                               NULL);
748         
749         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
750         
751         account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
752         if (!account_data) {
753                 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
754                 return NULL;
755         }
756
757         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
758                 id = account_data->store_account->account_name;
759         else if (account_data->transport_account)
760                 id = account_data->transport_account->account_name;
761
762         if (!id)
763                 g_printerr ("modest: could not get an id for account %s\n",
764                             account_name);
765         else    
766                 account = modest_tny_account_store_get_tny_account_by_id (self, id);
767
768         if (!account)
769                 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
770                             type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
771                             account_name, id ? id : "<none>");
772
773         modest_account_mgr_free_account_data (priv->account_mgr, account_data);
774         return account; 
775 }
776
777 static TnyAccount* get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self, const gchar *account_name)
778 {
779         /* Get the current connection: */
780         TnyDevice *device = modest_runtime_get_device ();
781         
782         if (!tny_device_is_online (device))
783                 return NULL;
784
785 #ifdef MODEST_PLATFORM_MAEMO
786         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
787         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);    
788         const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
789         if (!iap_id)
790                 return NULL;
791                 
792         ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
793         if (!connection)
794                 return NULL;
795                 
796         const gchar *connection_name = con_ic_iap_get_name (connection);
797         if (!connection_name)
798                 return NULL;
799         
800         /*  Get the connection-specific transport acccount, if any: */
801         ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
802         gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager, 
803                 account_name, connection_name);
804                 
805         if (!server_account_name)
806                 return NULL; /* No connection-specific SMTP server was specified for this connection. */
807                 
808         TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
809         g_free (server_account_name);   
810
811         /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
812         g_object_unref (connection);
813         
814         return account;
815 #else
816         return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
817 #endif /* MODEST_PLATFORM_MAEMO */
818 }
819
820                                                                  
821 TnyAccount* modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
822                                                                  const gchar *account_name)
823 {
824         /*  Get the connection-specific transport acccount, if any: */
825         TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
826                         
827         /* If there is no connection-specific transport account (the common case), 
828          * just get the regular transport account: */
829         if (!account) {         
830                 account = modest_tny_account_store_get_tny_account_by_account (self, account_name, 
831                                                      TNY_ACCOUNT_TYPE_TRANSPORT);
832         }
833                              
834         return account;
835 }