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