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