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