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