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