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