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