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