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