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