* modest-tny-account-store:
[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         GMutex             *store_lock; 
75         gchar              *cache_dir;
76         GHashTable         *password_hash;
77         TnyDevice          *device;
78         TnyPlatformFactory *platform_fact;
79         TnySessionCamel    *tny_session_camel;
80 };
81
82 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
83                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
84                                                       ModestTnyAccountStorePrivate))
85 /* globals */
86 static GObjectClass *parent_class = NULL;
87
88 static guint signals[LAST_SIGNAL] = {0};
89
90 GType
91 modest_tny_account_store_get_type (void)
92 {
93         static GType my_type = 0;
94
95         if (!my_type) {
96                 static const GTypeInfo my_info = {
97                         sizeof(ModestTnyAccountStoreClass),
98                         NULL,           /* base init */
99                         NULL,           /* base finalize */
100                         (GClassInitFunc) modest_tny_account_store_class_init,
101                         NULL,           /* class finalize */
102                         NULL,           /* class data */
103                         sizeof(ModestTnyAccountStore),
104                         0,              /* n_preallocs */
105                         (GInstanceInitFunc) modest_tny_account_store_instance_init,
106                         NULL
107                 };
108
109                 static const GInterfaceInfo iface_info = {
110                         (GInterfaceInitFunc) modest_tny_account_store_init,
111                         NULL,         /* interface_finalize */
112                         NULL          /* interface_data */
113                 };
114                 /* hack hack */
115                 my_type = g_type_register_static (G_TYPE_OBJECT,
116                                                   "ModestTnyAccountStore",
117                                                   &my_info, 0);
118                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
119                                              &iface_info);
120         }
121         return my_type;
122 }
123
124 static void
125 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
126 {
127         GObjectClass *gobject_class;
128         gobject_class = (GObjectClass*) klass;
129
130         parent_class            = g_type_class_peek_parent (klass);
131         gobject_class->finalize = modest_tny_account_store_finalize;
132
133         g_type_class_add_private (gobject_class,
134                                   sizeof(ModestTnyAccountStorePrivate));
135
136         signals[PASSWORD_REQUESTED_SIGNAL] =
137                 g_signal_new ("password_requested",
138                               G_TYPE_FROM_CLASS (gobject_class),
139                               G_SIGNAL_RUN_FIRST,
140                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
141                               NULL, NULL,
142                               modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
143                               G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
144         
145         signals[ACCOUNT_UPDATE_SIGNAL] =
146                 g_signal_new ("account_update",
147                               G_TYPE_FROM_CLASS (gobject_class),
148                               G_SIGNAL_RUN_FIRST,
149                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
150                               NULL, NULL,
151                               g_cclosure_marshal_VOID__STRING,
152                               G_TYPE_NONE, 1, G_TYPE_STRING);
153         
154 }
155
156 static void
157 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
158 {
159         ModestTnyAccountStorePrivate *priv =
160                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
161
162         priv->cache_dir              = NULL;
163         priv->platform_fact          = NULL;
164         priv->tny_session_camel      = NULL;
165         priv->device                 = NULL;
166         
167         priv->password_hash          = g_hash_table_new_full (g_str_hash, g_str_equal,
168                                                               g_free, g_free);
169 }
170
171
172 static void
173 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
174                     gpointer user_data)
175 {
176         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
177
178         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
179                        account);
180         
181 }
182
183
184 static void
185 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
186                     const gchar *key, gpointer user_data)
187 {
188         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
189         
190         g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
191                        account);
192 }
193
194
195 static ModestTnyAccountStore*
196 get_account_store_for_account (TnyAccount *account)
197 {
198         return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
199 }
200
201
202
203 static void
204 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
205 {
206         g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
207 }
208
209 static gchar*
210 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
211 {
212         const gchar *key;
213         ModestAccountMgr *account_mgr;
214         const TnyAccountStore *account_store;
215         ModestTnyAccountStore *self;
216         ModestTnyAccountStorePrivate *priv;
217         gchar *pwd = NULL;
218         gboolean already_asked;
219                 
220         key           = tny_account_get_id (account);
221         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
222
223         self = MODEST_TNY_ACCOUNT_STORE (account_store);
224         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
225         
226         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
227                 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
228         
229         /* is it in the hash? if it's already there, it must be wrong... */
230         already_asked = g_hash_table_lookup_extended (priv->password_hash,
231                                                       key, NULL, (gpointer *) &pwd);
232
233         /* if the password is not already there, try ModestConf */
234         if (!already_asked) {
235                 pwd  = modest_account_mgr_get_string (account_mgr,
236                                                       key, MODEST_ACCOUNT_PASSWORD,
237                                                       TRUE, NULL);
238                 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
239         }
240
241         /* if it was already asked, it must have been wrong, so ask again */
242         if (already_asked || !pwd || strlen(pwd) == 0) {
243
244                 /* we don't have it yet. we emit a signal to get the password somewhere */
245                 const gchar* name = tny_account_get_name (account);
246                 gboolean remember;
247                 pwd = NULL;
248
249                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
250                                name, &pwd, cancel, &remember);
251
252                 if (!*cancel) {
253                         if (remember)
254                                 modest_account_mgr_set_string (account_mgr,
255                                                                key, MODEST_ACCOUNT_PASSWORD,
256                                                                pwd,
257                                                                TRUE, NULL);
258                         /* We need to dup the string even knowing that
259                            it's already a dup of the contents of an
260                            entry, because it if it's wrong, then camel
261                            will free it */
262                         g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
263                 } else {
264                         g_hash_table_remove (priv->password_hash, key);
265                         g_free (pwd);
266                         pwd = NULL;
267                 }
268         } else
269                 *cancel = FALSE;
270
271         return pwd;
272 }
273
274
275 static void
276 forget_password (TnyAccount *account) {
277
278         ModestTnyAccountStore *self;
279         ModestTnyAccountStorePrivate *priv;
280         const TnyAccountStore *account_store;
281         gchar *pwd;
282         const gchar *key;
283         ModestAccountMgr *account_mgr;
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         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
299                 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
300
301         /* Remove from configuration system */
302         modest_account_mgr_unset (account_mgr,
303                                   key, MODEST_ACCOUNT_PASSWORD,
304                                   TRUE, NULL);
305 }
306
307
308
309 /* instantiate the correct tny account subclass */
310 static TnyAccount*
311 tny_account_for_proto (ModestProtocol proto) 
312 {
313         ModestProtocolType type;        
314         TnyAccount *tny_account = NULL;
315         
316         type  = modest_protocol_info_get_protocol_type (proto);
317
318         if (type == MODEST_PROTOCOL_TYPE_TRANSPORT) 
319                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
320         else if (proto == MODEST_PROTOCOL_STORE_POP)
321                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
322         else if (proto == MODEST_PROTOCOL_STORE_IMAP)
323                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
324         else
325                 g_return_val_if_reached (NULL);
326         
327         if (tny_account)
328                 tny_account_set_proto (tny_account,
329                                        modest_protocol_info_get_protocol_name(proto));
330         else
331                 g_printerr ("modest: could not get tny account for %d\n",
332                             proto);    
333         return tny_account;
334 }
335
336
337 /* create a tnyaccount for the server account connected to the account with name 'key'
338  */
339 static TnyAccount*
340 get_tny_account_from_server_account (ModestTnyAccountStore *self,
341                                      ModestServerAccountData *account_data,
342                                      ModestProtocolType modest_type)
343 {
344         TnyAccount *tny_account;
345         ModestTnyAccountStorePrivate *priv;
346                 
347         g_return_val_if_fail (self, NULL);
348         g_return_val_if_fail (account_data, NULL);
349
350         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
351         
352         /* proto */
353         tny_account = tny_account_for_proto (account_data->proto);
354         if (!tny_account) {
355                 g_printerr ("modest: could not create tny account for '%s'\n",
356                             account_data->account_name);
357                 return NULL;
358         }
359         
360         set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
361         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),  /* session */
362                                        priv->tny_session_camel);        
363         tny_account_set_id            (tny_account, account_data->account_name); /* id */
364
365         if (account_data->hostname)
366                 tny_account_set_hostname (tny_account, account_data->hostname);
367
368         if (account_data->username) 
369                 tny_account_set_user (tny_account, account_data->username);
370
371         tny_account_set_pass_func (tny_account, get_password);
372         tny_account_set_forget_pass_func (tny_account, forget_password);
373
374         return tny_account;
375 }
376
377
378
379 static void
380 modest_tny_account_store_finalize (GObject *obj)
381 {
382         ModestTnyAccountStore *self        = MODEST_TNY_ACCOUNT_STORE(obj);
383         ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
384
385         if (priv->tny_session_camel) {
386                 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
387                 priv->tny_session_camel = NULL;
388         }
389
390         if (priv->store_lock)
391                 g_mutex_free (priv->store_lock);
392
393         g_free (priv->cache_dir);
394         priv->cache_dir = NULL;
395
396         if (priv->device) {
397                 g_object_unref (priv->device);
398                 priv->device = NULL;
399         }
400         
401         if (priv->password_hash) {
402                 g_hash_table_destroy (priv->password_hash);
403                 priv->password_hash = NULL;
404         }
405         
406         G_OBJECT_CLASS(parent_class)->finalize (obj);
407 }
408
409
410 ModestTnyAccountStore*
411 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
412
413         GObject *obj;
414         ModestTnyAccountStorePrivate *priv;
415         TnyPlatformFactory *pfact;
416         
417         g_return_val_if_fail (account_mgr, NULL);
418
419         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
420         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
421
422         g_signal_connect (G_OBJECT(account_mgr), "account_changed",
423                                        G_CALLBACK (on_account_changed), obj);
424         g_signal_connect (G_OBJECT(account_mgr), "account_removed",
425                                        G_CALLBACK (on_account_removed), obj);
426         priv->store_lock = g_mutex_new ();
427
428         pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
429         if (!pfact) {
430                 g_printerr ("modest: cannot get platform factory instance\n");
431                 g_object_unref (obj);
432                 return NULL;
433         } else
434                 priv->platform_fact = pfact;
435         
436         priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
437         if (!priv->tny_session_camel) {
438                 g_printerr ("modest: cannot create TnySessionCamel instance\n");
439                 g_object_unref (obj);
440                 return NULL;
441         }
442
443         tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
444
445         return MODEST_TNY_ACCOUNT_STORE(obj);
446 }
447
448
449 static gboolean
450 add_account  (TnyAccountStore *self, TnyAccount *account) {
451
452         ModestTnyAccountStore *account_store;
453         ModestTnyAccountStorePrivate *priv;
454         ModestAccountMgr *account_mgr;  
455         const gchar *account_name;
456         const gchar *hostname, *username, *proto;
457
458         g_return_val_if_fail (self, FALSE);
459         g_return_val_if_fail (account, FALSE);
460
461         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
462         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
463         account_mgr    = modest_tny_platform_factory_get_account_mgr_instance
464                             (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
465         
466         
467         account_name   = tny_account_get_id(account);
468         if (!account_name) {
469                 g_printerr ("modest: failed to retrieve account name\n");
470                 return FALSE;
471         }
472
473         hostname =  tny_account_get_hostname(account);
474         username =  tny_account_get_user(account);
475         proto    =  tny_account_get_proto(account);
476
477         return modest_account_mgr_add_server_account (account_mgr,
478                                                       account_name,
479                                                       hostname, username, NULL,
480                                                       modest_protocol_info_get_protocol(proto));
481 }
482
483
484 static void
485 modest_tny_account_store_add_store_account  (TnyAccountStore *self,
486                                              TnyStoreAccount *account)
487 {
488         ModestTnyAccountStorePrivate *priv;
489
490         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
491         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
492                                        priv->tny_session_camel);
493         
494         if (!add_account (self, TNY_ACCOUNT(account)))
495                 g_printerr ("modest: failed to add store account\n");
496 }
497
498
499 static void
500 modest_tny_account_store_add_transport_account  (TnyAccountStore *self,
501                                                  TnyTransportAccount *account)
502 {
503         ModestTnyAccountStorePrivate *priv;
504         
505         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
506         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
507                                        priv->tny_session_camel);        
508         if (!add_account (self, TNY_ACCOUNT(account)))
509                 g_printerr ("modest: failed to add transport account\n");
510 }
511
512
513 static TnyAccount*
514 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
515                               TnyGetAccountsRequestType type) 
516 {
517         TnyAccount *tny_account = NULL;
518         ModestServerAccountData *server_account;
519
520         if (type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS && account_data->store_account)
521                 server_account = account_data->store_account;
522         else if (type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS && account_data->transport_account)
523                 server_account = account_data->transport_account;
524         else
525                 g_return_val_if_reached (NULL);
526         
527         if (!server_account) {
528                 g_printerr ("modest: no %s account defined for '%s'\n",
529                             type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS ? "store" : "transport",
530                             account_data->display_name);
531                 return NULL;
532         }
533         
534         tny_account = get_tny_account_from_server_account (self, server_account, type);
535         if (!tny_account) { 
536                 g_printerr ("modest: failed to create tny account for %s\n",
537                             account_data->account_name);
538                 return NULL;
539         }
540         
541         if (account_data->display_name)
542                 tny_account_set_name (tny_account, account_data->display_name); 
543
544         return tny_account;
545 }
546
547
548 static void
549 modest_tny_account_store_get_accounts  (TnyAccountStore *iface, TnyList *list,
550                                         TnyGetAccountsRequestType type)
551 {
552         ModestTnyAccountStore        *self;
553         ModestTnyAccountStorePrivate *priv;
554         GSList                       *accounts, *cursor;
555         ModestAccountMgr             *account_mgr; 
556         
557         g_return_if_fail (iface);
558         g_return_if_fail (TNY_IS_LIST(list));
559
560         self        = MODEST_TNY_ACCOUNT_STORE(iface);
561         priv        = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
562         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
563                 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
564         
565         if (type == TNY_ACCOUNT_STORE_BOTH) {
566                 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
567                 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
568         }
569         
570         accounts = modest_account_mgr_account_names (account_mgr, NULL); 
571         for (cursor = accounts; cursor; cursor = cursor->next) {
572                 TnyAccount *tny_account;
573                 ModestAccountData *account_data =
574                         modest_account_mgr_get_account_data (account_mgr, 
575                                                              (gchar*)cursor->data);
576                 if (account_data && account_data->enabled) {
577                         tny_account = get_tny_account_from_account (self, account_data, type);
578                         if (tny_account)
579                                 tny_list_prepend (list, G_OBJECT(tny_account));
580                 }
581                 g_free (cursor->data);
582                 modest_account_mgr_free_account_data (account_mgr, account_data);
583         }
584         g_slist_free (accounts);
585         tny_session_camel_set_account_store (priv->tny_session_camel, iface);
586 }
587
588
589 /*
590  * the cache dir will be ~/.modest/cache
591  * might want to change this in a simple #define...
592  */
593 static const gchar*
594 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
595 {
596         ModestTnyAccountStorePrivate *priv;
597         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
598         
599         if (!priv->cache_dir)
600                 priv->cache_dir = g_build_filename (g_get_home_dir(), ".modest",
601                                                     "cache", NULL);
602         return priv->cache_dir;
603 }
604
605
606 /*
607  * callers need to unref
608  */
609 static TnyDevice*
610 modest_tny_account_store_get_device (TnyAccountStore *self)
611 {
612         ModestTnyAccountStorePrivate *priv;
613
614         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
615
616         if (!priv->device) 
617                 priv->device = tny_platform_factory_new_device (priv->platform_fact);
618         
619         return g_object_ref (G_OBJECT(priv->device));
620 }
621
622
623
624 static gboolean
625 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
626                                 const gchar *prompt)
627 {
628         g_printerr ("modest: alert [%d]: %s", type, prompt);
629         return TRUE;
630 }
631
632
633 static void
634 modest_tny_account_store_init (gpointer g, gpointer iface_data)
635 {
636         TnyAccountStoreIface *klass;
637
638         g_return_if_fail (g);
639
640         klass = (TnyAccountStoreIface *)g;
641
642         klass->get_accounts_func =
643                 modest_tny_account_store_get_accounts;
644         klass->add_transport_account_func =
645                 modest_tny_account_store_add_transport_account;
646         klass->add_store_account_func =
647                 modest_tny_account_store_add_store_account;
648         klass->get_cache_dir_func =
649                 modest_tny_account_store_get_cache_dir;
650         klass->get_device_func =
651                 modest_tny_account_store_get_device;
652         klass->alert_func =
653                 modest_tny_account_store_alert;
654 }
655
656 void
657 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
658                                             ModestTnyGetPassFunc func)
659 {
660         g_warning (__FUNCTION__);
661         return; /* not implemented, we use signals */
662 }
663
664
665 TnySessionCamel*
666 tny_account_store_get_session    (TnyAccountStore *self)
667 {
668         g_return_val_if_fail (self, NULL);      
669         return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;
670 }