2007-05-08 Murray Cumming <murrayc@murrayc.com>
[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_POINTER,
162                                G_TYPE_NONE, 5, G_TYPE_STRING, G_TYPE_POINTER, 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 /* This callback will be called by Tinymail when it needs the password.
257  * Note that TnyAccount here will be the server account. */
258 static gchar*
259 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
260 {
261         const gchar *key;
262         const TnyAccountStore *account_store;
263         ModestTnyAccountStore *self;
264         ModestTnyAccountStorePrivate *priv;
265         gchar *username = NULL;
266         gchar *pwd = NULL;
267         gpointer pwd_ptr;
268         gboolean already_asked;
269         
270         key           = tny_account_get_id (account);
271         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
272         
273         self = MODEST_TNY_ACCOUNT_STORE (account_store);
274         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
275         
276         /* is it in the hash? if it's already there, it must be wrong... */
277         pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
278                                    * type-punned ptrs...*/
279         already_asked = g_hash_table_lookup_extended (priv->password_hash,
280                                                       key,
281                                                       NULL,
282                                                       (gpointer*)&pwd_ptr);
283
284         /* if the password is not already there, try ModestConf */
285         if (!already_asked) {
286                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
287                                                       key, MODEST_ACCOUNT_PASSWORD, TRUE);
288                 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
289         }
290
291         /* if it was already asked, it must have been wrong, so ask again */
292         if (already_asked || !pwd || strlen(pwd) == 0) {
293                 /* we don't have it yet. Get the password from the user */
294                 const gchar* account_id = tny_account_get_id (account);
295                 gboolean remember = FALSE;
296                 pwd = NULL;
297                 
298                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
299                                account_id, /* server_account_name */
300                                &username, &pwd, cancel, &remember);
301                 
302                 if (!*cancel) {
303                         if (remember)
304                                 modest_account_mgr_set_string (priv->account_mgr,key,
305                                                                MODEST_ACCOUNT_PASSWORD,
306                                                                pwd, TRUE);
307                         /* We need to dup the string even knowing that
308                            it's already a dup of the contents of an
309                            entry, because it if it's wrong, then camel
310                            will free it */
311                         g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
312                 } else {
313                         g_hash_table_remove (priv->password_hash, key);
314                 }
315
316                 g_free (username);
317                 username = NULL;
318                 
319                 g_free (pwd);
320                 pwd = NULL;
321                         
322         } else
323                 *cancel = FALSE;
324  
325     /* printf("  DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
326         
327         return pwd;
328 }
329
330
331 static void
332 forget_password (TnyAccount *account)
333 {
334         ModestTnyAccountStore *self;
335         ModestTnyAccountStorePrivate *priv;
336         const TnyAccountStore *account_store;
337         gchar *pwd;
338         const gchar *key;
339         
340         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
341         self = MODEST_TNY_ACCOUNT_STORE (account_store);
342         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
343         key  = tny_account_get_id (account);
344
345         /* Do not remove the key, this will allow us to detect that we
346            have already asked for it at least once */
347         pwd = g_hash_table_lookup (priv->password_hash, key);
348         if (pwd) {
349                 memset (pwd, 0, strlen (pwd));
350                 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
351         }
352
353         /* Remove from configuration system */
354         modest_account_mgr_unset (priv->account_mgr,
355                                   key, MODEST_ACCOUNT_PASSWORD, TRUE);
356 }
357
358
359 static void
360 modest_tny_account_store_finalize (GObject *obj)
361 {
362         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
363         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
364         
365         //gboolean debug = modest_runtime_get_debug_flags() & MODEST_RUNTIME_DEBUG_DEBUG_OBJECTS;
366
367         g_free (priv->cache_dir);
368         priv->cache_dir = NULL;
369         
370         if (priv->password_hash) {
371                 g_hash_table_destroy (priv->password_hash);
372                 priv->password_hash = NULL;
373         }
374
375         if (priv->account_mgr) {
376                 g_object_unref (G_OBJECT(priv->account_mgr));
377                 priv->account_mgr = NULL;
378         }
379
380         if (priv->device) {
381                 g_object_unref (G_OBJECT(priv->device));
382                 priv->device = NULL;
383         }
384
385         /* this includes the local folder */
386         account_list_free (priv->store_accounts);
387         priv->store_accounts = NULL;
388         
389         account_list_free (priv->transport_accounts);
390         priv->transport_accounts = NULL;
391
392         if (priv->session) {
393                 camel_object_unref (CAMEL_OBJECT(priv->session));
394                 priv->session = NULL;
395         }
396         
397         G_OBJECT_CLASS(parent_class)->finalize (obj);
398 }
399
400
401 ModestTnyAccountStore*
402 modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) {
403
404         GObject *obj;
405         ModestTnyAccountStorePrivate *priv;
406         TnyList *list; 
407         
408         g_return_val_if_fail (account_mgr, NULL);
409         g_return_val_if_fail (device, NULL);
410
411         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
412         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
413
414         priv->account_mgr = g_object_ref (G_OBJECT(account_mgr));
415         priv->device = g_object_ref (device);
416         
417         priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
418         
419         tny_session_camel_set_ui_locker (priv->session,  tny_gtk_lockable_new ());
420         /* FIXME: unref this in the end? */
421         tny_session_camel_set_async_connecting (priv->session, TRUE);
422         
423         /* force a cache fill... ugly */
424         list = TNY_LIST(tny_simple_list_new());
425         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
426                                         TNY_ACCOUNT_STORE_BOTH);
427         g_object_unref(list);
428         
429         /* Connect signals */
430         g_signal_connect (G_OBJECT(account_mgr), "account_changed",
431                                        G_CALLBACK (on_account_changed), obj);
432         g_signal_connect (G_OBJECT(account_mgr), "account_removed",
433                                        G_CALLBACK (on_account_removed), obj);
434
435         return MODEST_TNY_ACCOUNT_STORE(obj);
436 }
437
438
439 static void
440 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
441 {
442         ModestTnyAccountStorePrivate *priv;
443         GSList                       *accounts, *cursor;
444         
445         priv     = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
446         accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
447
448         cursor = accounts;
449         while (cursor) {
450                 tny_list_prepend (list, G_OBJECT(cursor->data));
451                 cursor = cursor->next;
452         }
453 }
454
455
456
457 /* this function fills the TnyList, and also returns a GSList of the accounts,
458  * for caching purposes
459  */
460 static GSList*
461 get_accounts  (TnyAccountStore *self, TnyList *list, TnyAccountType type)
462 {
463         ModestTnyAccountStorePrivate *priv = NULL;
464         GSList                       *account_names = NULL, *cursor = NULL;
465         GSList                       *accounts = NULL;
466         
467         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
468  
469         account_names = modest_account_mgr_account_names (priv->account_mgr, 
470                 TRUE /* including disabled accounts */);
471         
472         for (cursor = account_names; cursor; cursor = cursor->next) {
473                 
474                 gchar *account_name = (gchar*)cursor->data;
475                 
476                 /* only return enabled accounts */
477                 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
478                         TnyAccount *tny_account = 
479                                 modest_tny_account_new_from_account (priv->account_mgr,
480                                                                      account_name,
481                                                                      type, priv->session,
482                                                                      get_password,
483                                                                      forget_password);
484                         if (tny_account) {
485                                 g_object_set_data (G_OBJECT(tny_account), "account_store",
486                                                    (gpointer)self);
487                                 tny_list_prepend (list, G_OBJECT(tny_account));
488                                 accounts = g_slist_append (accounts, tny_account); /* cache it */
489                         } else
490                                 g_printerr ("modest: failed to create account for %s\n",
491                                             account_name);
492                 }
493                 g_free (account_name);
494         }
495         g_slist_free (account_names);
496
497         /* also, add the local folder pseudo-account */
498         if (type == TNY_ACCOUNT_TYPE_STORE) {
499                 TnyAccount *tny_account =
500                         modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
501                 tny_list_prepend (list, G_OBJECT(tny_account));
502                 accounts = g_slist_append (accounts, tny_account); /* cache it */
503         }       
504         return accounts;
505 }       
506
507
508 static void
509 modest_tny_account_store_get_accounts  (TnyAccountStore *self, TnyList *list,
510                                         TnyGetAccountsRequestType request_type)
511 {
512         ModestTnyAccountStorePrivate *priv;
513         
514         g_return_if_fail (self);
515         g_return_if_fail (TNY_IS_LIST(list));
516         
517         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
518         
519         if (request_type == TNY_ACCOUNT_STORE_BOTH) {
520                 modest_tny_account_store_get_accounts (self, list,
521                                                        TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
522                 modest_tny_account_store_get_accounts (self, list,
523                                                        TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
524                 return;
525         }
526         
527         if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS)  {
528                 
529                 if (!priv->store_accounts)
530                         priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
531                 else
532                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
533
534         } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
535
536                 if (!priv->transport_accounts)
537                         priv->transport_accounts =
538                                 get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
539                 else
540                         get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
541         } else
542                 g_return_if_reached (); /* incorrect req type */
543 }
544
545
546 static const gchar*
547 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
548 {
549         ModestTnyAccountStorePrivate *priv;
550         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
551         
552         if (!priv->cache_dir)
553                 priv->cache_dir = g_build_filename (g_get_home_dir(), 
554                                                     MODEST_DIR, MODEST_CACHE_DIR, NULL);
555         return priv->cache_dir;
556 }
557
558
559 /*
560  * callers need to unref
561  */
562 static TnyDevice*
563 modest_tny_account_store_get_device (TnyAccountStore *self)
564 {
565         ModestTnyAccountStorePrivate *priv;
566
567         g_return_val_if_fail (self, NULL);
568         
569         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
570
571         if (priv->device)
572                 return g_object_ref (G_OBJECT(priv->device));
573         else
574                 return NULL;
575 }
576
577
578 static TnyAccount*
579 modest_tny_account_store_find_account_by_url (TnyAccountStore *self, const gchar* url_string)
580 {
581         TnyAccount *account = NULL;
582         ModestTnyAccountStorePrivate *priv;     
583         GSList *cursor;
584         
585         g_return_val_if_fail (self, NULL);
586         g_return_val_if_fail (url_string, NULL);
587         
588         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
589
590         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
591                 if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
592                         account = TNY_ACCOUNT(cursor->data);
593                         break;
594                 }
595         }
596
597         if (!account) {
598                 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
599                         if (tny_account_matches_url_string (TNY_ACCOUNT(cursor->data), url_string)) {
600                                 account = TNY_ACCOUNT(cursor->data);
601                                 break;
602                         }
603                 }
604         }
605
606         if (account)
607                 g_object_ref (G_OBJECT(account));
608
609         return account;
610 }
611
612
613
614 static gboolean
615 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
616                                 const GError *error)
617 {
618         g_return_val_if_fail (error, FALSE);
619         g_return_val_if_fail (error->domain == TNY_ACCOUNT_ERROR, FALSE);
620         
621         printf("DEBUG: %s: error->message=%s\n", __FUNCTION__, error->message);
622         
623         GtkMessageType gtktype;
624         gboolean retval = FALSE;
625         GtkWidget *dialog;
626
627         switch (type)
628         {
629                 case TNY_ALERT_TYPE_INFO:
630                 gtktype = GTK_MESSAGE_INFO;
631                 break;
632                 case TNY_ALERT_TYPE_WARNING:
633                 gtktype = GTK_MESSAGE_WARNING;
634                 break;
635                 case TNY_ALERT_TYPE_ERROR:
636                 default:
637                 gtktype = GTK_MESSAGE_ERROR;
638                 break;
639         }
640         
641         const gchar *prompt = NULL;
642         switch (error->code)
643         {
644                 case TNY_ACCOUNT_ERROR_TRY_CONNECT:
645                         prompt = _("Modest account not yet fully configured");
646                         break;
647                 default:
648                         g_warning ("%s: Unhandled GError code.", __FUNCTION__);
649                         prompt = NULL;
650                 break;
651         }
652         
653         if (!prompt)
654                 return FALSE;
655
656         dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
657                 gtktype, GTK_BUTTONS_YES_NO, prompt);
658
659         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
660                 retval = TRUE;
661
662         gtk_widget_destroy (dialog);
663
664         return retval;
665 }
666
667
668 static void
669 modest_tny_account_store_init (gpointer g, gpointer iface_data)
670 {
671         TnyAccountStoreIface *klass;
672
673         g_return_if_fail (g);
674
675         klass = (TnyAccountStoreIface *)g;
676
677         klass->get_accounts_func =
678                 modest_tny_account_store_get_accounts;
679         klass->get_cache_dir_func =
680                 modest_tny_account_store_get_cache_dir;
681         klass->get_device_func =
682                 modest_tny_account_store_get_device;
683         klass->alert_func =
684                 modest_tny_account_store_alert;
685         klass->find_account_func =
686                 modest_tny_account_store_find_account_by_url;
687 }
688
689 void
690 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
691                                             ModestTnyGetPassFunc func)
692 {
693         /* not implemented, we use signals */
694         g_printerr ("modest: set_get_pass_func not implemented\n");
695 }
696
697 TnySessionCamel*
698 tny_account_store_get_session  (TnyAccountStore *self)
699 {
700         g_return_val_if_fail (self, NULL);
701         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
702 }
703
704
705 TnyAccount*
706 modest_tny_account_store_get_tny_account_by_id  (ModestTnyAccountStore *self, const gchar *id)
707 {
708         TnyAccount *account = NULL;
709         ModestTnyAccountStorePrivate *priv;     
710         GSList *cursor;
711
712         g_return_val_if_fail (self, NULL);
713         g_return_val_if_fail (id, NULL);
714         
715         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
716
717         for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
718                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
719                 if (acc_id && strcmp (acc_id, id) == 0) { 
720                         account = TNY_ACCOUNT(cursor->data);
721                         break;
722                 }
723         }
724
725         /* if we already found something, no need to search the transport accounts */
726         for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
727                 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
728                 if (acc_id && strcmp (acc_id, id) == 0) {
729                         account = TNY_ACCOUNT(cursor->data);
730                         break;
731                 }
732         }
733
734         if (account)
735                 g_object_ref (G_OBJECT(account));
736         
737         return account;
738 }
739
740 TnyAccount*
741 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
742                                                      const gchar *account_name,
743                                                      TnyAccountType type)
744 {
745         TnyAccount *account = NULL;
746         ModestAccountData *account_data;
747         const gchar *id = NULL;
748         ModestTnyAccountStorePrivate *priv;     
749
750         g_return_val_if_fail (self, NULL);
751         g_return_val_if_fail (account_name, NULL);
752         g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
753                               NULL);
754         
755         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
756         
757         account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
758         if (!account_data) {
759                 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
760                 return NULL;
761         }
762
763         if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
764                 id = account_data->store_account->account_name;
765         else if (account_data->transport_account)
766                 id = account_data->transport_account->account_name;
767
768         if (!id)
769                 g_printerr ("modest: could not get an id for account %s\n",
770                             account_name);
771         else    
772                 account = modest_tny_account_store_get_tny_account_by_id (self, id);
773
774         if (!account)
775                 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
776                             type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
777                             account_name, id ? id : "<none>");
778
779         modest_account_mgr_free_account_data (priv->account_mgr, account_data);
780         return account; 
781 }
782
783 static TnyAccount* get_smtp_specific_transport_account_for_open_connection (ModestTnyAccountStore *self, const gchar *account_name)
784 {
785         /* Get the current connection: */
786         TnyDevice *device = modest_runtime_get_device ();
787         
788         if (!tny_device_is_online (device))
789                 return NULL;
790
791 #ifdef MODEST_PLATFORM_MAEMO
792         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
793         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);    
794         const gchar* iap_id = tny_maemo_conic_device_get_current_iap_id (maemo_device);
795         if (!iap_id)
796                 return NULL;
797                 
798         ConIcIap* connection = tny_maemo_conic_device_get_iap (maemo_device, iap_id);
799         if (!connection)
800                 return NULL;
801                 
802         const gchar *connection_name = con_ic_iap_get_name (connection);
803         if (!connection_name)
804                 return NULL;
805         
806         /*  Get the connection-specific transport acccount, if any: */
807         ModestAccountMgr *account_manager = modest_runtime_get_account_mgr ();
808         gchar* server_account_name = modest_account_mgr_get_connection_specific_smtp (account_manager, 
809                 account_name, connection_name);
810                 
811         if (!server_account_name)
812                 return NULL; /* No connection-specific SMTP server was specified for this connection. */
813                 
814         TnyAccount* account = modest_tny_account_store_get_tny_account_by_id (self, server_account_name);
815         g_free (server_account_name);   
816
817         /* Unref the get()ed object, as required by the tny_maemo_conic_device_get_iap() documentation. */
818         g_object_unref (connection);
819         
820         return account;
821 #else
822         return NULL; /* TODO: Implement this for GNOME, instead of just Maemo? */
823 #endif /* MODEST_PLATFORM_MAEMO */
824 }
825
826                                                                  
827 TnyAccount* modest_tny_account_store_get_transport_account_for_open_connection (ModestTnyAccountStore *self,
828                                                                  const gchar *account_name)
829 {
830         /*  Get the connection-specific transport acccount, if any: */
831         TnyAccount *account = get_smtp_specific_transport_account_for_open_connection (self, account_name);
832                         
833         /* If there is no connection-specific transport account (the common case), 
834          * just get the regular transport account: */
835         if (!account) {         
836                 account = modest_tny_account_store_get_tny_account_by_account (self, account_name, 
837                                                      TNY_ACCOUNT_TYPE_TRANSPORT);
838         }
839                              
840         return account;
841 }