* updated test case, code cleanup, documentation fixes
[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-account-store.h>
38 #include <tny-camel-transport-account.h>
39 #include <tny-camel-store-account.h>
40 #include <modest-marshal.h>
41 #include <glib/gi18n.h>
42 #include "modest-account-mgr.h"
43 #include "modest-tny-account-store.h"
44 #include "modest-tny-platform-factory.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
232         /* if the password is not already there, try ModestConf */
233 /*      if (!already_asked)  */
234                 pwd  = modest_account_mgr_get_string (priv->account_mgr,
235                                                       key, MODEST_ACCOUNT_PASSWORD,
236                                                       TRUE, NULL);
237
238 /*      /\* if it was already asked, it must have been wrong, so ask again *\/ */
239 /*      if (!already_asked || !pwd || strlen(pwd) == 0) { */
240
241 /*              /\* we don't have it yet. we emit a signal to get the password somewhere *\/ */
242 /*              const gchar* name = tny_account_get_name (account); */
243 /*              *cancel = TRUE; */
244 /*              pwd     = NULL; */
245 /*              g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, */
246 /*                             name, &pwd, cancel); */
247 /*              if (!*cancel) /\* remember the password *\/ */
248 /*                      modest_account_mgr_set_string (priv->account_mgr, */
249 /*                                                     key, MODEST_ACCOUNT_PASSWORD, */
250 /*                                                     pwd, TRUE, NULL); */
251 /*      } else */
252 /*              *cancel = FALSE; */
253
254 /*      g_hash_table_insert (priv->password_hash, key, pwd); */
255
256                 /* FIXME: pwd needs to be freed somehow */
257         return pwd; 
258 }
259
260
261 static void
262 forget_password (TnyAccount *account) {
263
264         ModestTnyAccountStore *self;
265         ModestTnyAccountStorePrivate *priv;
266         const TnyAccountStore *account_store;
267
268         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
269         self = MODEST_TNY_ACCOUNT_STORE (account_store);
270         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
271 }
272
273
274
275 /* create a tnyaccount for the server account connected to the account with name 'key'
276  */
277 static TnyAccount*
278 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account, 
279                        const gchar *server_account, ModestProtocolType modest_type)
280 {
281         TnyAccount *tny_account;
282         ModestTnyAccountStorePrivate *priv;
283         gchar *val;
284         GSList *options = NULL;
285         GError *error = NULL;
286         
287         g_return_val_if_fail (self, NULL);
288         g_return_val_if_fail (account, NULL);
289         g_return_val_if_fail (server_account, NULL);
290
291         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
292
293         /* is it a store or a transport? */
294         if  (modest_type == MODEST_PROTOCOL_TYPE_STORE) 
295                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
296         else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
297                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
298         else
299                 g_assert_not_reached ();
300
301         if (!tny_account) {
302                 g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
303                             account, server_account);
304                 return NULL;
305         }
306         
307         set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
308
309         /* session */
310         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
311                                        priv->tny_session_camel);
312         
313         /* id */
314         tny_account_set_id   (tny_account, server_account);
315
316         /* name */
317         val = modest_account_mgr_get_string (priv->account_mgr, account,
318                                              MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
319         if (val) {
320                 tny_account_set_name (tny_account, val);
321                 g_free (val);
322         } else {
323                 g_printerr ("modest: display name not defined for '%s:%s'\n", 
324                             account, server_account);
325                 g_object_unref (G_OBJECT(tny_account));
326                 return NULL;
327         }
328
329         /* proto */
330         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
331                                              MODEST_ACCOUNT_PROTO, TRUE, NULL);
332         if (val) {
333                 tny_account_set_proto (tny_account, val);
334                 g_free (val);
335         } else {
336                 g_printerr ("modest: protocol not defined for '%s:%s'\n", 
337                             account, server_account);
338                 g_object_unref (G_OBJECT(tny_account));
339                 return NULL;
340         }
341
342         /* Options */
343         options = modest_account_mgr_get_list (priv->account_mgr,
344                                                tny_account_get_id (tny_account),
345                                                MODEST_ACCOUNT_OPTIONS,
346                                                MODEST_CONF_VALUE_STRING,
347                                                TRUE,
348                                                &error);
349         
350         if (error) {
351                 g_warning ("Error retrieving account %s options: %s",
352                            tny_account_get_id (tny_account), error->message);
353                 g_error_free (error);
354         } else {
355                 GSList *tmp = options;
356                 while (options) {
357                         tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
358                         g_free (options->data);
359                         options = g_slist_next (options);
360                 }
361                 g_slist_free (tmp);
362         }
363
364         /* hostname */
365         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
366                                              MODEST_ACCOUNT_HOSTNAME, TRUE,
367                                              NULL);
368         if (val) {
369                 tny_account_set_hostname (tny_account, val);
370                 g_free (val);
371         }
372
373         /* username */
374         val = modest_account_mgr_get_string (priv->account_mgr, server_account,
375                                              MODEST_ACCOUNT_USERNAME, TRUE,
376                                              NULL);
377         if (val) {
378                 tny_account_set_user (tny_account, val);
379                 g_free (val);
380         }
381
382         tny_account_set_pass_func (tny_account, get_password);
383         tny_account_set_forget_pass_func (tny_account, forget_password);
384
385         return tny_account;
386 }
387
388
389
390 static void
391 modest_tny_account_store_finalize (GObject *obj)
392 {
393         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
394         ModestTnyAccountStorePrivate *priv =
395                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
396
397         if (priv->account_mgr) {
398                 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
399                                              priv->sig1);
400                 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
401                                              priv->sig2);
402                 g_object_unref (G_OBJECT(priv->account_mgr));
403                 priv->account_mgr = NULL;
404         }
405
406         if (priv->tny_session_camel) {
407                 // FIXME: how to kill a camel
408                 priv->tny_session_camel = NULL;
409         }
410
411         if (priv->device) {
412                 g_object_unref (G_OBJECT(priv->device));
413                 priv->device = NULL;
414         }
415
416         if (priv->store_lock)
417                 g_mutex_free (priv->store_lock);
418
419         g_free (priv->cache_dir);
420         priv->cache_dir = NULL;
421
422         if (priv->password_hash) {
423                 g_hash_table_destroy (priv->password_hash);
424                 priv->password_hash = NULL;
425         }
426
427         G_OBJECT_CLASS(parent_class)->finalize (obj);
428 }
429
430
431 ModestTnyAccountStore*
432 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
433
434         GObject *obj;
435         ModestTnyAccountStorePrivate *priv;
436         TnyPlatformFactory *pfact;
437         
438         g_return_val_if_fail (account_mgr, NULL);
439
440         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
441
442         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
443
444         g_object_ref(G_OBJECT(account_mgr));
445         priv->account_mgr = account_mgr;
446
447         priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
448                                        G_CALLBACK (on_account_changed), obj);
449         priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
450                                        G_CALLBACK (on_account_removed), obj);
451         
452         priv->store_lock = g_mutex_new ();
453
454         pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
455         if (!pfact) {
456                 g_printerr ("modest: cannot create platform factory\n");
457                 g_object_unref (obj);
458                 return NULL;
459         }
460         
461         priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
462         if (!priv->device) {
463                 g_printerr ("modest: cannot create device instance\n");
464                 g_object_unref (obj);
465                 return NULL;
466         }
467         tny_device_force_online (priv->device);
468         
469         priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
470
471         if (!priv->tny_session_camel) {
472                 g_printerr ("modest: cannot create TnySessionCamel instance\n");
473                 g_object_unref (obj);
474                 return NULL;
475         }
476
477         return MODEST_TNY_ACCOUNT_STORE(obj);
478 }
479
480
481 static gboolean
482 add_account  (TnyAccountStore *self, TnyAccount *account) {
483
484         ModestTnyAccountStore *account_store;
485         ModestTnyAccountStorePrivate *priv;
486
487         const gchar *account_name;
488         const gchar *hostname, *username, *proto;
489
490         g_return_val_if_fail (self, FALSE);
491         g_return_val_if_fail (account, FALSE);
492
493         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
494         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
495
496         account_name   = tny_account_get_id(account);
497         if (!account_name) {
498                 g_printerr ("modest: failed to retrieve account name\n");
499                 return FALSE;
500         }
501
502         hostname =  tny_account_get_hostname(account);
503         username =  tny_account_get_user(account);
504         proto    =  tny_account_get_proto(account);
505
506         return modest_account_mgr_add_server_account (priv->account_mgr,
507                                                       account_name,
508                                                       hostname, username, NULL,
509                                                       proto);
510 }
511
512
513 static void
514 modest_tny_account_store_add_store_account  (TnyAccountStore *self,
515                                              TnyStoreAccount *account)
516 {
517         ModestTnyAccountStorePrivate *priv;
518
519         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
520         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
521                                        priv->tny_session_camel);
522         
523         if (!add_account (self, TNY_ACCOUNT(account)))
524                 g_printerr ("modest: failed to add store account\n");
525 }
526
527
528 static void
529 modest_tny_account_store_add_transport_account  (TnyAccountStore *self,
530                                                  TnyTransportAccount *account)
531 {
532         ModestTnyAccountStorePrivate *priv;
533         
534         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
535         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
536                                        priv->tny_session_camel);
537         
538         if (!add_account (self, TNY_ACCOUNT(account)))
539                 g_printerr ("modest: failed to add transport account\n");
540 }
541
542
543 static gchar*
544 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
545                                 ModestProtocolType modest_type)
546 {
547         ModestTnyAccountStorePrivate *priv;
548         gchar *server;
549         gchar *key;
550
551         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
552
553         if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
554                 key = MODEST_ACCOUNT_STORE_ACCOUNT;
555         else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
556                 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
557         else
558                 g_assert_not_reached();
559         
560         server = modest_account_mgr_get_string (priv->account_mgr,
561                                                 account_name,
562                                                 key, FALSE, NULL);
563         if (!server)
564                 return NULL;
565         
566         if (!modest_account_mgr_account_exists (priv->account_mgr,
567                                                 server, TRUE, NULL)) {
568                 g_free (server);
569                 return NULL;
570         }
571         return server;
572 }
573
574 static void
575 modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
576                                         TnyList *list,
577                                         TnyGetAccountsRequestType type)
578 {
579         ModestTnyAccountStore        *self;
580         ModestTnyAccountStorePrivate *priv;
581         GSList                       *accounts, *cursor;
582         ModestProtocolType            modest_type;
583         
584         g_return_if_fail (iface);
585
586         self = MODEST_TNY_ACCOUNT_STORE(iface);
587         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
588
589         switch (type) {
590         case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
591                 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
592                 break;
593         case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
594                 modest_type = MODEST_PROTOCOL_TYPE_STORE;
595                 break;
596         case TNY_ACCOUNT_STORE_BOTH:
597                 modest_type = MODEST_PROTOCOL_TYPE_ANY;
598                 break;
599         default:
600                 g_assert_not_reached ();
601         }
602
603         cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL); 
604
605         while (cursor) {
606                 gchar       *account_name;
607                 gchar       *server_account;
608                 TnyAccount  *account;
609                 gboolean     is_server_account;
610
611                 account_name      = (gchar*)cursor->data;
612                 account           = NULL;
613                 is_server_account = FALSE;
614                 
615                 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) { 
616                         g_free (account_name); 
617                         cursor = cursor->next;
618                         continue;
619                 } 
620                 
621                 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
622                         server_account = get_server_account_for_account (self, account_name,
623                                                                          MODEST_PROTOCOL_TYPE_TRANSPORT);
624                         if (server_account) {
625                                 account = tny_account_from_name (self, account_name, 
626                                                                        server_account,
627                                                                        MODEST_PROTOCOL_TYPE_TRANSPORT);
628                                 is_server_account = TRUE;
629                         }
630
631                         if (!account)
632                                 g_printerr ("modest: no transport account for '%s'\n",
633                                             account_name);
634                         else
635                                 tny_list_prepend (list, G_OBJECT(account));
636                 
637                         g_free (server_account);
638                 }
639                 
640                 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
641                         server_account = get_server_account_for_account (self, account_name,
642                                                                          MODEST_PROTOCOL_TYPE_STORE);
643                         if (server_account) {
644                                 account = tny_account_from_name (self, account_name, 
645                                                                        server_account,
646                                                                        MODEST_PROTOCOL_TYPE_STORE);
647                                 is_server_account = TRUE;
648                         }
649
650                         if (!account)
651                                 g_printerr ("modest: no store account for '%s'\n",
652                                             account_name);
653                         else
654                                 tny_list_prepend (list, G_OBJECT(account));
655                         g_free (server_account);
656                 }
657
658                 g_free (account_name);
659                 cursor = cursor->next;
660         }
661
662         g_slist_free (accounts);
663
664         tny_session_camel_set_account_store (priv->tny_session_camel, iface);
665 }
666
667
668 /*
669  * the cache dir will be ~/.modest/cache
670  * might want to change this in a simple #define...
671  */
672 static const gchar*
673 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
674 {
675         ModestTnyAccountStorePrivate *priv;
676         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
677         
678         if (!priv->cache_dir)
679                 priv->cache_dir = g_build_filename (g_get_home_dir(),
680                                                     ".modest",
681                                                     "cache",
682                                                     NULL);
683         return priv->cache_dir;
684 }
685
686
687 /*
688  * callers need to unref
689  */
690 static TnyDevice*
691 modest_tny_account_store_get_device (TnyAccountStore *self)
692 {
693         ModestTnyAccountStorePrivate *priv;
694
695         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
696
697         return g_object_ref (G_OBJECT(priv->device));
698 }
699
700
701
702 static gboolean
703 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
704                                 const gchar *prompt)
705 {
706         g_printerr ("modest: alert [%d]: %s",
707                     type, prompt);
708
709         return TRUE;
710 }
711
712
713 static void
714 modest_tny_account_store_init (gpointer g, gpointer iface_data)
715 {
716         TnyAccountStoreIface *klass;
717
718         g_return_if_fail (g);
719
720         klass = (TnyAccountStoreIface *)g;
721
722         klass->get_accounts_func =
723                 modest_tny_account_store_get_accounts;
724         klass->add_transport_account_func =
725                 modest_tny_account_store_add_transport_account;
726         klass->add_store_account_func =
727                 modest_tny_account_store_add_store_account;
728         klass->get_cache_dir_func =
729                 modest_tny_account_store_get_cache_dir;
730         klass->get_device_func =
731                 modest_tny_account_store_get_device;
732         klass->alert_func =
733                 modest_tny_account_store_alert;
734 }
735
736 void
737 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
738                                             ModestTnyGetPassFunc func)
739 {
740         g_warning (__FUNCTION__);
741         return; /* not implemented, we use signals */
742 }
743
744
745 TnySessionCamel*
746 tny_account_store_get_session    (TnyAccountStore *self)
747 {
748         g_return_val_if_fail (self, NULL);
749         
750         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;
751 }