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