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