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