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