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