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