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