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