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