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