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