a42323c0a703fe1a021e40a8fc2f0e737dc32c76
[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
32 #include <tny-account.h>
33 #include <tny-account-store.h>
34 #include <tny-store-account.h>
35 #include <tny-transport-account.h>
36 #include <tny-device.h>
37 #include <tny-gnome-device.h>
38 #include <tny-account-store.h>
39 #include <tny-gnome-platform-factory.h> 
40 #include <tny-camel-transport-account.h>
41 #include <tny-camel-store-account.h>
42 #include <modest-marshal.h>
43 #include "modest-account-mgr.h"
44 #include "modest-tny-account-store.h"
45
46 /* 'private'/'protected' functions */
47 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
48 //static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
49 static void modest_tny_account_store_finalize     (GObject *obj);
50
51 /* implementations for tny-account-store-iface */
52 static void    modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
53
54 static void    modest_tny_account_store_init              (gpointer g, gpointer iface_data);
55 static void    modest_tny_account_store_add_store_account       (TnyAccountStore *self,
56                                                                  TnyStoreAccount *account);
57 static void    modest_tny_account_store_add_transport_account   (TnyAccountStore *self,
58                                                                  TnyTransportAccount *account);
59 static void    modest_tny_account_store_get_accounts            (TnyAccountStore *iface, TnyList *list,
60                                                                  TnyGetAccountsRequestType type);
61 /* list my signals */
62 enum {
63         PASSWORD_REQUESTED_SIGNAL,
64         ACCOUNT_UPDATE_SIGNAL,
65         LAST_SIGNAL
66 };
67
68 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
69 struct _ModestTnyAccountStorePrivate {
70
71         GMutex *store_lock;     
72         gchar *cache_dir;
73         gulong sig1, sig2;
74
75         GHashTable *password_hash;
76         
77         TnySessionCamel *tny_session_camel;
78         TnyDevice  *device;
79         
80         ModestAccountMgr *account_mgr;
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 /* globals */
87 static GObjectClass *parent_class = NULL;
88
89 static guint signals[LAST_SIGNAL] = {0};
90
91 GType
92 modest_tny_account_store_get_type (void)
93 {
94         static GType my_type = 0;
95
96         if (!my_type) {
97                 static const GTypeInfo my_info = {
98                         sizeof(ModestTnyAccountStoreClass),
99                         NULL,           /* base init */
100                         NULL,           /* base finalize */
101                         (GClassInitFunc) modest_tny_account_store_class_init,
102                         NULL,           /* class finalize */
103                         NULL,           /* class data */
104                         sizeof(ModestTnyAccountStore),
105                         0,              /* n_preallocs */
106                         (GInstanceInitFunc) modest_tny_account_store_instance_init,
107                         NULL
108                 };
109
110                 static const GInterfaceInfo iface_info = {
111                         (GInterfaceInitFunc) modest_tny_account_store_init,
112                         NULL,         /* interface_finalize */
113                         NULL          /* interface_data */
114                 };
115                 /* hack hack */
116                 my_type = g_type_register_static (G_TYPE_OBJECT,
117                                                   "ModestTnyAccountStore",
118                                                   &my_info, 0);
119                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
120                                              &iface_info);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_tny_account_store_finalize;
133
134         g_type_class_add_private (gobject_class,
135                                   sizeof(ModestTnyAccountStorePrivate));
136
137         signals[PASSWORD_REQUESTED_SIGNAL] =
138                 g_signal_new ("password_requested",
139                               G_TYPE_FROM_CLASS (gobject_class),
140                               G_SIGNAL_RUN_FIRST,
141                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
142                               NULL, NULL,
143                               modest_marshal_VOID__STRING_POINTER_POINTER,
144                               G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER);
145         
146         signals[ACCOUNT_UPDATE_SIGNAL] =
147                 g_signal_new ("account_update",
148                               G_TYPE_FROM_CLASS (gobject_class),
149                               G_SIGNAL_RUN_FIRST,
150                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
151                               NULL, NULL,
152                               g_cclosure_marshal_VOID__STRING,
153                               G_TYPE_NONE, 1, G_TYPE_STRING);
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->account_mgr            = NULL;
164         priv->device                 = NULL;
165         priv->cache_dir              = NULL;
166         priv->tny_session_camel      = NULL;
167
168         priv->password_hash          = g_hash_table_new_full (g_str_hash, g_str_equal,
169                                                               g_free, g_free);
170 }
171
172
173 static void
174 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
175                     gpointer user_data)
176 {
177         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
178
179         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
180                        account);
181         
182 }
183
184
185 static void
186 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
187                     const gchar *key, gpointer user_data)
188 {
189         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
190         
191         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
192                        account);
193 }
194
195
196 static ModestTnyAccountStore*
197 get_account_store_for_account (TnyAccount *account)
198 {
199         return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
200 }
201
202
203
204 static void
205 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
206 {
207         g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
208 }
209
210
211 static gchar*
212 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
213 {
214         const gchar *key;
215         const TnyAccountStore *account_store;
216         ModestTnyAccountStore *self;
217         ModestTnyAccountStorePrivate *priv;
218         gchar *pwd = NULL;
219         gboolean already_asked;
220         
221         g_return_val_if_fail (account, NULL);
222         
223         key           = tny_account_get_id (account);
224         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
225
226         self = MODEST_TNY_ACCOUNT_STORE (account_store);
227         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
228
229         /* is it in the hash? if it's already there, it must be wrong... */
230 //      already_asked = g_hash_table_lookup (priv->password_hash, key) != NULL;
231 /*      if (already_asked) */
232 /*              g_warning ("password already asked for or in config (%s)", */
233 /*                         key); */
234 /*      else */
235 /*              g_warning ("password not yet asked for or in config (%s)", */
236 /*                         key); */
237
238         /* if the password is not already there, try ModestConf */
239 //      if (!already_asked) 
240                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
241                                                       key, MODEST_ACCOUNT_PASSWORD,
242                                                       TRUE, NULL);
243
244         /* if it was already asked, it must have been wrong, so ask again */
245 /*      if (!already_asked ||  !pwd || strlen(pwd) == 0) { */
246 /*              g_warning ("emit signal to get pass for %s", key); */
247                 
248 /*              /\* we don't have it yet. we emit a signal to get the password somewhere *\/ */
249 /*              const gchar* name = tny_account_get_name (account); */
250 /*              *cancel = TRUE; */
251 /*              pwd     = NULL; */
252 /*              g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, */
253 /*                             name, &pwd, cancel); */
254 /*              if (!*cancel) /\* remember the password *\/ */
255 /*                      modest_account_mgr_set_string (priv->account_mgr, */
256 /*                                                     key, MODEST_ACCOUNT_PASSWORD, */
257 /*                                                     pwd, TRUE, NULL); */
258 /*      } else */
259 /*              *cancel = FALSE; */
260
261 //      g_hash_table_insert (priv->password_hash, (gpointer)key, (gpointer)pwd);
262         
263         return pwd; 
264 }
265
266
267 static void
268 forget_password (TnyAccount *account) {
269
270         ModestTnyAccountStore *self;
271         ModestTnyAccountStorePrivate *priv;
272         const TnyAccountStore *account_store;
273
274         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
275         self = MODEST_TNY_ACCOUNT_STORE (account_store);
276         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
277 }
278
279
280
281 /* create a tnyaccount for the server account connected to the account with name 'key'
282  */
283 static TnyAccount*
284 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account, 
285                        const gchar *server_account, ModestProtocolType modest_type)
286 {
287         TnyAccount *tny_account;
288         ModestTnyAccountStorePrivate *priv;
289         gchar *val;
290
291         g_return_val_if_fail (self, NULL);
292         g_return_val_if_fail (account, NULL);
293         g_return_val_if_fail (server_account, NULL);
294
295         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
296
297         /* is it a store or a transport? */
298         if  (modest_type == MODEST_PROTOCOL_TYPE_STORE) 
299                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
300         else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
301                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
302         else
303                 g_assert_not_reached ();
304
305         if (!tny_account) {
306                 g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
307                             account, server_account);
308                 return NULL;
309         }
310         
311         set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
312
313         /* session */
314         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
315                                        priv->tny_session_camel);
316         
317         /* id */
318         tny_account_set_id (tny_account, server_account);
319         tny_account_set_name (tny_account, account);
320
321         /* proto */
322         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
323                                              MODEST_ACCOUNT_PROTO, TRUE, NULL);
324         if (val) {
325                 tny_account_set_proto (tny_account, val);
326                 g_free (val);
327         } else {
328                 g_printerr ("modest: protocol not defined for '%s:%s'\n", 
329                             account, server_account);
330                 g_object_unref (G_OBJECT(tny_account));
331                 return NULL;
332         }
333
334         /* hostname */
335         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
336                                              MODEST_ACCOUNT_HOSTNAME, TRUE,
337                                              NULL);
338         if (val) {
339                 tny_account_set_hostname (tny_account, val);
340                 g_free (val);
341         }
342
343         /* username */
344         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
345                                              MODEST_ACCOUNT_USERNAME, TRUE,
346                                              NULL);
347         if (val) {
348                 tny_account_set_user (tny_account, val);
349                 g_free (val);
350         }
351
352         tny_account_set_pass_func (tny_account, get_password);
353         tny_account_set_forget_pass_func (tny_account, forget_password);
354
355         return tny_account;
356 }
357
358
359
360 static void
361 modest_tny_account_store_finalize (GObject *obj)
362 {
363         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
364         ModestTnyAccountStorePrivate *priv =
365                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
366
367         if (priv->account_mgr) {
368                 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
369                                              priv->sig1);
370                 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
371                                              priv->sig2);
372                 g_object_unref (G_OBJECT(priv->account_mgr));
373                 priv->account_mgr = NULL;
374         }
375
376         if (priv->tny_session_camel) {
377                 // FIXME: how to kill a camel
378                 priv->tny_session_camel = NULL;
379         }
380
381         if (priv->device) {
382                 g_object_unref (G_OBJECT(priv->device));
383                 priv->device = NULL;
384         }
385
386         if (priv->store_lock)
387                 g_mutex_free (priv->store_lock);
388
389         g_free (priv->cache_dir);
390         priv->cache_dir = NULL;
391
392         if (priv->password_hash) {
393                 g_hash_table_destroy (priv->password_hash);
394                 priv->password_hash = NULL;
395         }
396
397         G_OBJECT_CLASS(parent_class)->finalize (obj);
398 }
399
400
401 ModestTnyAccountStore*
402 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
403
404         GObject *obj;
405         ModestTnyAccountStorePrivate *priv;
406         TnyPlatformFactory *pfact;
407         
408         g_return_val_if_fail (account_mgr, NULL);
409
410         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
411
412         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
413
414         g_object_ref(G_OBJECT(account_mgr));
415         priv->account_mgr = account_mgr;
416
417         priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
418                                        G_CALLBACK (on_account_changed), obj);
419         priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
420                                        G_CALLBACK (on_account_removed), obj);
421         
422         priv->store_lock = g_mutex_new ();
423
424         /* FIXME: don't use GNOME */
425         pfact = TNY_PLATFORM_FACTORY (tny_gnome_platform_factory_get_instance());
426         if (!pfact) {
427                 g_printerr ("modest: cannot create platform factory\n");
428                 g_object_unref (obj);
429                 return NULL;
430         }
431         
432         priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
433         if (!priv->device) {
434                 g_printerr ("modest: cannot create device instance\n");
435                 g_object_unref (obj);
436                 return NULL;
437         }
438         tny_device_force_online (priv->device);
439         
440         priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
441
442         if (!priv->tny_session_camel) {
443                 g_printerr ("modest: cannot create TnySessionCamel instance\n");
444                 g_object_unref (obj);
445                 return NULL;
446         }
447
448         return MODEST_TNY_ACCOUNT_STORE(obj);
449 }
450
451
452 static gboolean
453 add_account  (TnyAccountStore *self, TnyAccount *account) {
454
455         ModestTnyAccountStore *account_store;
456         ModestTnyAccountStorePrivate *priv;
457
458         const gchar *account_name;
459         const gchar *hostname, *username, *proto;
460
461         g_return_val_if_fail (self, FALSE);
462         g_return_val_if_fail (account, FALSE);
463
464         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
465         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
466
467         account_name   = tny_account_get_id(account);
468         if (!account_name) {
469                 g_printerr ("modest: failed to retrieve account name\n");
470                 return FALSE;
471         }
472
473         hostname =  tny_account_get_hostname(account);
474         username =  tny_account_get_user(account);
475         proto    =  tny_account_get_proto(account);
476
477         return modest_account_mgr_add_server_account (priv->account_mgr,
478                                                       account_name,
479                                                       hostname, username, NULL,
480                                                       proto);
481 }
482
483
484 static void
485 modest_tny_account_store_add_store_account  (TnyAccountStore *self,
486                                              TnyStoreAccount *account)
487 {
488         ModestTnyAccountStorePrivate *priv;
489
490         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
491         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
492                                        priv->tny_session_camel);
493         
494         if (!add_account (self, TNY_ACCOUNT(account)))
495                 g_printerr ("modest: failed to add store account\n");
496 }
497
498
499 static void
500 modest_tny_account_store_add_transport_account  (TnyAccountStore *self,
501                                                  TnyTransportAccount *account)
502 {
503         ModestTnyAccountStorePrivate *priv;
504         
505         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
506         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
507                                        priv->tny_session_camel);
508         
509         if (!add_account (self, TNY_ACCOUNT(account)))
510                 g_printerr ("modest: failed to add transport account\n");
511 }
512
513
514 static gchar*
515 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
516                                 ModestProtocolType modest_type)
517 {
518         ModestTnyAccountStorePrivate *priv;
519         gchar *server;
520         gchar *key;
521
522         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
523
524         if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
525                 key = MODEST_ACCOUNT_STORE_ACCOUNT;
526         else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
527                 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
528         else
529                 g_assert_not_reached();
530         
531         server = modest_account_mgr_get_string (priv->account_mgr,
532                                                 account_name,
533                                                 key, FALSE, NULL);
534         if (!server)
535                 return NULL;
536         
537         if (!modest_account_mgr_account_exists (priv->account_mgr,
538                                                 server, TRUE, NULL)) {
539                 g_free (server);
540                 return NULL;
541         }
542         return server;
543 }
544
545 static void
546 modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
547                                         TnyList *list,
548                                         TnyGetAccountsRequestType type)
549 {
550         ModestTnyAccountStore        *self;
551         ModestTnyAccountStorePrivate *priv;
552         GSList                       *accounts, *cursor;
553         ModestProtocolType            modest_type;
554         
555         g_return_if_fail (iface);
556
557         self = MODEST_TNY_ACCOUNT_STORE(iface);
558         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
559
560         switch (type) {
561         case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
562                 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
563                 break;
564         case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
565                 modest_type = MODEST_PROTOCOL_TYPE_STORE;
566                 break;
567         case TNY_ACCOUNT_STORE_BOTH:
568                 modest_type = MODEST_PROTOCOL_TYPE_ANY;
569                 break;
570         default:
571                 g_assert_not_reached ();
572         }
573
574         cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL); 
575
576         while (cursor) {
577                 gchar           *account_name;
578                 gchar           *server_account;
579                 TnyAccount *account;
580
581                 account_name  =  (gchar*)cursor->data;
582                 account =  NULL;
583                 
584                 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) { 
585                         g_free (account_name); 
586                         cursor = cursor->next;
587                         continue;
588                 } 
589                 
590                 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
591                         server_account = get_server_account_for_account (self, account_name,
592                                                                          MODEST_PROTOCOL_TYPE_TRANSPORT);
593                         if (server_account)
594                                 account = tny_account_from_name (self, account_name, 
595                                                                        server_account,
596                                                                        MODEST_PROTOCOL_TYPE_TRANSPORT);
597                         if (!account)
598                                 g_printerr ("modest: no transport account for '%s'\n",
599                                             account_name);
600                         else
601                                 tny_list_prepend (list, G_OBJECT(account));
602                 
603                         g_free (server_account);
604                 }
605                 
606                 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
607                         server_account = get_server_account_for_account (self, account_name,
608                                                                          MODEST_PROTOCOL_TYPE_STORE);
609                         if (server_account)
610                                 account = tny_account_from_name (self, account_name, 
611                                                                        server_account,
612                                                                        MODEST_PROTOCOL_TYPE_STORE);
613                         if (!account)
614                                 g_printerr ("modest: no store account for '%s'\n",
615                                             account_name);
616                         else
617                                 tny_list_prepend (list, G_OBJECT(account));
618                         g_free (server_account);
619                 }
620
621                 g_free (account_name);
622                 cursor = cursor->next;
623         }
624
625         g_slist_free (accounts);
626
627         tny_session_camel_set_account_store (priv->tny_session_camel, iface);
628 /*      tny_session_camel_set_current_accounts (priv->tny_session_camel, */
629 /*                                              list); */
630 }
631
632
633 /*
634  * the cache dir will be ~/.modest/cache
635  * might want to change this in a simple #define...
636  */
637 static const gchar*
638 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
639 {
640         ModestTnyAccountStorePrivate *priv;
641         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
642         
643         if (!priv->cache_dir)
644                 priv->cache_dir = g_build_filename (g_get_home_dir(),
645                                                     ".modest",
646                                                     "cache",
647                                                     NULL);
648         return priv->cache_dir;
649 }
650
651
652 /*
653  * callers need to unref
654  */
655 static TnyDevice*
656 modest_tny_account_store_get_device (TnyAccountStore *self)
657 {
658         ModestTnyAccountStorePrivate *priv;
659
660         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
661
662         return g_object_ref (G_OBJECT(priv->device));
663 }
664
665
666
667 static gboolean
668 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
669                                 const gchar *prompt)
670 {
671         g_printerr ("modest: alert [%d]: %s",
672                     type, prompt);
673
674         return TRUE;
675 }
676
677
678 static void
679 modest_tny_account_store_init (gpointer g, gpointer iface_data)
680 {
681         TnyAccountStoreIface *klass;
682
683         g_return_if_fail (g);
684
685         klass = (TnyAccountStoreIface *)g;
686
687         klass->get_accounts_func =
688                 modest_tny_account_store_get_accounts;
689         klass->add_transport_account_func =
690                 modest_tny_account_store_add_transport_account;
691         klass->add_store_account_func =
692                 modest_tny_account_store_add_store_account;
693         klass->get_cache_dir_func =
694                 modest_tny_account_store_get_cache_dir;
695         klass->get_device_func =
696                 modest_tny_account_store_get_device;
697         klass->alert_func =
698                 modest_tny_account_store_alert;
699 }
700
701 void
702 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
703                                             ModestTnyGetPassFunc func)
704 {
705         g_warning (__FUNCTION__);
706         return; /* not implemented, we use signals */
707 }
708
709
710 TnySessionCamel*
711 tny_account_store_get_session    (TnyAccountStore *self)
712 {
713         g_return_val_if_fail (self, NULL);
714         
715         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;
716 }