1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include <glib/gi18n.h>
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>
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>
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);
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);
67 ACCOUNT_UPDATE_SIGNAL,
68 PASSWORD_REQUESTED_SIGNAL,
72 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
73 struct _ModestTnyAccountStorePrivate {
75 GHashTable *password_hash;
77 ModestAccountMgr *account_mgr;
78 TnySessionCamel *session;
80 /* we cache them here */
81 GSList *store_accounts;
82 GSList *transport_accounts;
85 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
86 MODEST_TYPE_TNY_ACCOUNT_STORE, \
87 ModestTnyAccountStorePrivate))
90 static GObjectClass *parent_class = NULL;
92 static guint signals[LAST_SIGNAL] = {0};
95 modest_tny_account_store_get_type (void)
97 static GType my_type = 0;
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),
109 (GInstanceInitFunc) modest_tny_account_store_instance_init,
113 static const GInterfaceInfo iface_info = {
114 (GInterfaceInitFunc) modest_tny_account_store_init,
115 NULL, /* interface_finalize */
116 NULL /* interface_data */
119 my_type = g_type_register_static (G_TYPE_OBJECT,
120 "ModestTnyAccountStore",
122 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
129 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
131 GObjectClass *gobject_class;
132 gobject_class = (GObjectClass*) klass;
134 parent_class = g_type_class_peek_parent (klass);
135 gobject_class->finalize = modest_tny_account_store_finalize;
137 g_type_class_add_private (gobject_class,
138 sizeof(ModestTnyAccountStorePrivate));
140 signals[ACCOUNT_UPDATE_SIGNAL] =
141 g_signal_new ("account_update",
142 G_TYPE_FROM_CLASS (gobject_class),
144 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
146 g_cclosure_marshal_VOID__STRING,
147 G_TYPE_NONE, 1, G_TYPE_STRING);
149 signals[PASSWORD_REQUESTED_SIGNAL] =
150 g_signal_new ("password_requested",
151 G_TYPE_FROM_CLASS (gobject_class),
153 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
155 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
156 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER,
162 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
164 ModestTnyAccountStorePrivate *priv =
165 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
167 priv->cache_dir = NULL;
168 priv->account_mgr = NULL;
170 priv->session = NULL;
172 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
177 account_list_free (GSList *accounts)
179 GSList *cursor = accounts;
181 g_object_unref (G_OBJECT(cursor->data));
182 cursor = cursor->next;
184 g_slist_free (accounts);
189 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
192 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
193 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
195 /* FIXME: make this more finegrained; changes do not really affect _all_
196 * accounts, and some do not affect tny accounts at all (such as 'last_update')
198 account_list_free (priv->store_accounts);
199 priv->store_accounts = NULL;
201 account_list_free (priv->transport_accounts);
202 priv->transport_accounts = NULL;
204 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
210 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
211 const gchar *key, gpointer user_data)
213 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
214 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
216 /* FIXME: make this more finegrained; changes do not really affect _all_
217 * accounts, and some do not affect tny accounts at all (such as 'last_update')
219 account_list_free (priv->store_accounts);
220 priv->store_accounts = NULL;
222 account_list_free (priv->transport_accounts);
223 priv->transport_accounts = NULL;
226 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
231 static ModestTnyAccountStore*
232 get_account_store_for_account (TnyAccount *account)
234 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
239 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
242 const TnyAccountStore *account_store;
243 ModestTnyAccountStore *self;
244 ModestTnyAccountStorePrivate *priv;
247 gboolean already_asked;
249 key = tny_account_get_id (account);
250 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
252 self = MODEST_TNY_ACCOUNT_STORE (account_store);
253 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
255 /* is it in the hash? if it's already there, it must be wrong... */
256 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
257 * type-punned ptrs...*/
258 already_asked = g_hash_table_lookup_extended (priv->password_hash,
261 (gpointer*)&pwd_ptr);
263 /* if the password is not already there, try ModestConf */
264 if (!already_asked) {
265 pwd = modest_account_mgr_get_string (priv->account_mgr,
266 key, MODEST_ACCOUNT_PASSWORD,TRUE);
267 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
270 /* if it was already asked, it must have been wrong, so ask again */
271 if (already_asked || !pwd || strlen(pwd) == 0) {
273 /* we don't have it yet. Get the password from the user */
274 const gchar* name = tny_account_get_name (account);
275 gboolean remember = FALSE;
278 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
279 name, &pwd, cancel, &remember);
283 modest_account_mgr_set_string (priv->account_mgr,key,
284 MODEST_ACCOUNT_PASSWORD,
286 /* We need to dup the string even knowing that
287 it's already a dup of the contents of an
288 entry, because it if it's wrong, then camel
290 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
292 g_hash_table_remove (priv->password_hash, key);
304 forget_password (TnyAccount *account)
306 ModestTnyAccountStore *self;
307 ModestTnyAccountStorePrivate *priv;
308 const TnyAccountStore *account_store;
312 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
313 self = MODEST_TNY_ACCOUNT_STORE (account_store);
314 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
315 key = tny_account_get_id (account);
317 /* Do not remove the key, this will allow us to detect that we
318 have already asked for it at least once */
319 pwd = g_hash_table_lookup (priv->password_hash, key);
321 memset (pwd, 0, strlen (pwd));
322 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
325 /* Remove from configuration system */
326 modest_account_mgr_unset (priv->account_mgr,
327 key, MODEST_ACCOUNT_PASSWORD, TRUE);
332 modest_tny_account_store_finalize (GObject *obj)
334 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
335 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
337 g_free (priv->cache_dir);
338 priv->cache_dir = NULL;
340 if (priv->password_hash) {
341 g_hash_table_destroy (priv->password_hash);
342 priv->password_hash = NULL;
345 if (priv->account_mgr) {
346 g_object_unref (G_OBJECT(priv->account_mgr));
347 priv->account_mgr = NULL;
350 /* this includes the local folder */
351 account_list_free (priv->store_accounts);
352 priv->store_accounts = NULL;
354 account_list_free (priv->transport_accounts);
355 priv->transport_accounts = NULL;
358 camel_object_unref (CAMEL_OBJECT(priv->session));
359 priv->session = NULL;
362 /* the device should be unref'ed *AFTER* the session */
364 g_object_unref (G_OBJECT(priv->device));
368 G_OBJECT_CLASS(parent_class)->finalize (obj);
372 ModestTnyAccountStore*
373 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
376 ModestTnyAccountStorePrivate *priv;
379 g_return_val_if_fail (account_mgr, NULL);
381 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
382 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
384 priv->account_mgr = account_mgr;
385 g_object_ref (G_OBJECT(priv->account_mgr));
387 priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
389 tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ());
390 /* FIXME: unref this in the end? */
392 /* force a cache fill... ugly */
393 list = TNY_LIST(tny_simple_list_new());
394 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list,
395 TNY_ACCOUNT_STORE_BOTH);
396 g_object_unref(list);
398 /* Connect signals */
399 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
400 G_CALLBACK (on_account_changed), obj);
401 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
402 G_CALLBACK (on_account_removed), obj);
404 return MODEST_TNY_ACCOUNT_STORE(obj);
409 get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
411 ModestTnyAccountStorePrivate *priv;
412 GSList *accounts, *cursor;
414 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
415 accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts);
419 tny_list_prepend (list, G_OBJECT(cursor->data));
420 cursor = cursor->next;
424 /* this function fills the TnyList, and also returns a GSList of the accounts,
425 * for caching purposes
428 get_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type)
430 ModestTnyAccountStorePrivate *priv;
431 GSList *account_names, *cursor;
432 GSList *accounts = NULL;
434 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
436 account_names = modest_account_mgr_account_names (priv->account_mgr);
438 for (cursor = account_names; cursor; cursor = cursor->next) {
440 gchar *account_name = (gchar*)cursor->data;
442 /* only return enabled accounts */
443 if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) {
444 TnyAccount *tny_account =
445 modest_tny_account_new_from_account (priv->account_mgr, account_name,
446 type, priv->session, get_password,
448 if (tny_account) { /* something went wrong */
449 g_object_set_data (G_OBJECT(tny_account), "account_store", (gpointer)self);
450 tny_list_prepend (list, G_OBJECT(tny_account));
451 accounts = g_slist_append (accounts, tny_account); /* cache it */
453 g_printerr ("modest: failed to create account for %s\n", account_name);
455 g_free (account_name);
457 g_slist_free (account_names);
459 /* also, add the local folder pseudo-account */
460 if (type == TNY_ACCOUNT_TYPE_STORE) {
461 TnyAccount *tny_account =
462 modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session);
463 tny_list_prepend (list, G_OBJECT(tny_account));
464 accounts = g_slist_append (accounts, tny_account); /* cache it */
472 modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list,
473 TnyGetAccountsRequestType request_type)
475 ModestTnyAccountStorePrivate *priv;
477 g_return_if_fail (self);
478 g_return_if_fail (TNY_IS_LIST(list));
480 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
482 if (request_type == TNY_ACCOUNT_STORE_BOTH) {
483 modest_tny_account_store_get_accounts (self, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
484 modest_tny_account_store_get_accounts (self, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
488 if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) {
490 if (!priv->store_accounts)
491 priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
493 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE);
495 } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
497 if (!priv->transport_accounts)
498 priv->transport_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
500 get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT);
502 g_return_if_reached (); /* incorrect req type */
507 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
509 ModestTnyAccountStorePrivate *priv;
510 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
512 if (!priv->cache_dir)
513 priv->cache_dir = g_build_filename (g_get_home_dir(),
517 return priv->cache_dir;
522 * callers need to unref
525 modest_tny_account_store_get_device (TnyAccountStore *self)
527 ModestTnyAccountStorePrivate *priv;
529 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
532 priv->device = tny_platform_factory_new_device
533 (modest_tny_platform_factory_get_instance());
535 return g_object_ref (G_OBJECT(priv->device));
541 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
544 GtkMessageType gtktype;
545 gboolean retval = FALSE;
550 case TNY_ALERT_TYPE_INFO:
551 gtktype = GTK_MESSAGE_INFO;
553 case TNY_ALERT_TYPE_WARNING:
554 gtktype = GTK_MESSAGE_WARNING;
556 case TNY_ALERT_TYPE_ERROR:
558 gtktype = GTK_MESSAGE_ERROR;
562 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
563 gtktype, GTK_BUTTONS_YES_NO, prompt);
565 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
568 gtk_widget_destroy (dialog);
576 modest_tny_account_store_add_store_account (TnyAccountStore *self,
577 TnyStoreAccount *account)
579 /* we should not need this...*/
580 g_printerr ("modest: add_store_account_func not implemented\n");
585 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
586 TnyTransportAccount *account)
588 /* we should not need this...*/
589 g_printerr ("modest: add_transport_account_func not implemented\n");
595 modest_tny_account_store_init (gpointer g, gpointer iface_data)
597 TnyAccountStoreIface *klass;
599 g_return_if_fail (g);
601 klass = (TnyAccountStoreIface *)g;
603 klass->get_accounts_func =
604 modest_tny_account_store_get_accounts;
605 klass->add_transport_account_func =
606 modest_tny_account_store_add_transport_account;
607 klass->add_store_account_func =
608 modest_tny_account_store_add_store_account;
609 klass->get_cache_dir_func =
610 modest_tny_account_store_get_cache_dir;
611 klass->get_device_func =
612 modest_tny_account_store_get_device;
614 modest_tny_account_store_alert;
618 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
619 ModestTnyGetPassFunc func)
621 /* not implemented, we use signals */
622 g_printerr ("modest: set_get_pass_func not implemented\n");
626 tny_account_store_get_session (TnyAccountStore *self)
628 g_return_val_if_fail (self, NULL);
629 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session;
634 modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id)
636 TnyAccount *account = NULL;
637 ModestTnyAccountStorePrivate *priv;
640 g_return_val_if_fail (self, NULL);
641 g_return_val_if_fail (id, NULL);
643 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
645 for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) {
646 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
647 if (acc_id && strcmp (acc_id, id) == 0) {
648 account = TNY_ACCOUNT(cursor->data);
653 /* if we already found something, no need to search the transport accounts */
654 for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) {
655 const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data));
656 if (acc_id && strcmp (acc_id, id) == 0) {
657 account = TNY_ACCOUNT(cursor->data);
663 g_object_ref (G_OBJECT(account));
670 modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self,
671 const gchar *account_name,
674 TnyAccount *account = NULL;
675 ModestAccountData *account_data;
676 const gchar *id = NULL;
677 ModestTnyAccountStorePrivate *priv;
679 g_return_val_if_fail (self, NULL);
680 g_return_val_if_fail (account_name, NULL);
681 g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
684 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
686 account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name);
688 g_printerr ("modest: cannot get account data for account '%s'\n", account_name);
692 if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account)
693 id = account_data->store_account->account_name;
694 else if (account_data->transport_account)
695 id = account_data->transport_account->account_name;
698 account = modest_tny_account_store_get_tny_account_by_id (self, id);
700 g_printerr ("modest: could not get tny %s account for %s (id=%s)\n",
701 type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport",
702 account_name, id ? id : "<none>");
704 modest_account_mgr_free_account_data (priv->account_mgr, account_data);