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