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