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