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.
32 #include <tny-account.h>
33 #include <tny-account-store.h>
34 #include <tny-store-account.h>
35 #include <tny-transport-account.h>
36 #include <tny-device.h>
37 #include <tny-account-store.h>
38 #include <tny-camel-transport-account.h>
39 #include <tny-camel-store-account.h>
40 #include <modest-marshal.h>
41 #include <glib/gi18n.h>
42 #include "modest-account-mgr.h"
43 #include "modest-tny-account-store.h"
44 #include "modest-tny-platform-factory.h"
46 #include <camel/camel.h>
48 /* 'private'/'protected' functions */
49 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
50 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
51 static void modest_tny_account_store_finalize (GObject *obj);
53 /* implementations for tny-account-store-iface */
54 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
56 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
57 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
58 TnyStoreAccount *account);
59 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
60 TnyTransportAccount *account);
61 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
62 TnyGetAccountsRequestType type);
65 PASSWORD_REQUESTED_SIGNAL,
66 ACCOUNT_UPDATE_SIGNAL,
70 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
71 struct _ModestTnyAccountStorePrivate {
77 GHashTable *password_hash;
79 TnySessionCamel *tny_session_camel;
82 ModestAccountMgr *account_mgr;
85 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
86 MODEST_TYPE_TNY_ACCOUNT_STORE, \
87 ModestTnyAccountStorePrivate))
89 static GObjectClass *parent_class = NULL;
91 static guint signals[LAST_SIGNAL] = {0};
94 modest_tny_account_store_get_type (void)
96 static GType my_type = 0;
99 static const GTypeInfo my_info = {
100 sizeof(ModestTnyAccountStoreClass),
101 NULL, /* base init */
102 NULL, /* base finalize */
103 (GClassInitFunc) modest_tny_account_store_class_init,
104 NULL, /* class finalize */
105 NULL, /* class data */
106 sizeof(ModestTnyAccountStore),
108 (GInstanceInitFunc) modest_tny_account_store_instance_init,
112 static const GInterfaceInfo iface_info = {
113 (GInterfaceInitFunc) modest_tny_account_store_init,
114 NULL, /* interface_finalize */
115 NULL /* interface_data */
118 my_type = g_type_register_static (G_TYPE_OBJECT,
119 "ModestTnyAccountStore",
121 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
128 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
130 GObjectClass *gobject_class;
131 gobject_class = (GObjectClass*) klass;
133 parent_class = g_type_class_peek_parent (klass);
134 gobject_class->finalize = modest_tny_account_store_finalize;
136 g_type_class_add_private (gobject_class,
137 sizeof(ModestTnyAccountStorePrivate));
139 signals[PASSWORD_REQUESTED_SIGNAL] =
140 g_signal_new ("password_requested",
141 G_TYPE_FROM_CLASS (gobject_class),
143 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
145 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
146 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
148 signals[ACCOUNT_UPDATE_SIGNAL] =
149 g_signal_new ("account_update",
150 G_TYPE_FROM_CLASS (gobject_class),
152 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
154 g_cclosure_marshal_VOID__STRING,
155 G_TYPE_NONE, 1, G_TYPE_STRING);
160 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
162 ModestTnyAccountStorePrivate *priv =
163 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
165 priv->account_mgr = NULL;
167 priv->cache_dir = NULL;
168 priv->tny_session_camel = NULL;
170 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
176 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
179 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
181 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
188 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
189 const gchar *key, gpointer user_data)
191 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
193 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
198 static ModestTnyAccountStore*
199 get_account_store_for_account (TnyAccount *account)
201 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
207 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
209 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
213 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
216 const TnyAccountStore *account_store;
217 ModestTnyAccountStore *self;
218 ModestTnyAccountStorePrivate *priv;
220 gboolean already_asked;
222 gdk_threads_enter ();
224 key = tny_account_get_id (account);
225 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
227 self = MODEST_TNY_ACCOUNT_STORE (account_store);
228 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
230 /* is it in the hash? if it's already there, it must be wrong... */
231 already_asked = g_hash_table_lookup_extended (priv->password_hash,
232 key, NULL, (gpointer *) &pwd);
234 /* if the password is not already there, try ModestConf */
235 if (!already_asked) {
236 pwd = modest_account_mgr_get_string (priv->account_mgr,
237 key, MODEST_ACCOUNT_PASSWORD,
239 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
242 /* if it was already asked, it must have been wrong, so ask again */
243 if (already_asked || !pwd || strlen(pwd) == 0) {
245 /* we don't have it yet. we emit a signal to get the password somewhere */
246 const gchar* name = tny_account_get_name (account);
250 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
251 name, &pwd, cancel, &remember);
255 modest_account_mgr_set_string (priv->account_mgr,
256 key, MODEST_ACCOUNT_PASSWORD,
259 /* We need to dup the string even knowing that
260 it's already a dup of the contents of an
261 entry, because it if it's wrong, then camel
263 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
265 g_hash_table_remove (priv->password_hash, key);
272 gdk_threads_leave ();
279 forget_password (TnyAccount *account) {
281 ModestTnyAccountStore *self;
282 ModestTnyAccountStorePrivate *priv;
283 const TnyAccountStore *account_store;
287 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
288 self = MODEST_TNY_ACCOUNT_STORE (account_store);
289 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
290 key = tny_account_get_id (account);
292 /* Do not remove the key, this will allow us to detect that we
293 have already asked for it at least once */
294 pwd = g_hash_table_lookup (priv->password_hash, key);
296 memset (pwd, 0, strlen (pwd));
297 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
300 /* Remove from configuration system */
301 modest_account_mgr_unset (priv->account_mgr,
302 key, MODEST_ACCOUNT_PASSWORD,
306 /* create a tnyaccount for the server account connected to the account with name 'key'
309 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
310 const gchar *server_account, ModestProtocolType modest_type)
312 TnyAccount *tny_account;
313 ModestTnyAccountStorePrivate *priv;
315 GSList *options = NULL;
316 GError *error = NULL;
318 g_return_val_if_fail (self, NULL);
319 g_return_val_if_fail (account, NULL);
320 g_return_val_if_fail (server_account, NULL);
322 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
324 /* is it a store or a transport? */
325 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
326 tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
327 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
328 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
330 g_assert_not_reached ();
333 g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
334 account, server_account);
338 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
341 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
342 priv->tny_session_camel);
345 tny_account_set_id (tny_account, server_account);
348 val = modest_account_mgr_get_string (priv->account_mgr, account,
349 MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
351 tny_account_set_name (tny_account, val);
354 g_printerr ("modest: display name not defined for '%s:%s'\n",
355 account, server_account);
356 g_object_unref (G_OBJECT(tny_account));
361 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
362 MODEST_ACCOUNT_PROTO, TRUE, NULL);
364 tny_account_set_proto (tny_account, val);
367 g_printerr ("modest: protocol not defined for '%s:%s'\n",
368 account, server_account);
369 g_object_unref (G_OBJECT(tny_account));
374 options = modest_account_mgr_get_list (priv->account_mgr,
375 tny_account_get_id (tny_account),
376 MODEST_ACCOUNT_OPTIONS,
377 MODEST_CONF_VALUE_STRING,
382 g_warning ("Error retrieving account %s options: %s",
383 tny_account_get_id (tny_account), error->message);
384 g_error_free (error);
386 GSList *tmp = options;
388 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
389 g_free (options->data);
390 options = g_slist_next (options);
396 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
397 MODEST_ACCOUNT_HOSTNAME, TRUE,
400 tny_account_set_hostname (tny_account, val);
405 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
406 MODEST_ACCOUNT_USERNAME, TRUE,
409 tny_account_set_user (tny_account, val);
413 tny_account_set_pass_func (tny_account, get_password);
414 tny_account_set_forget_pass_func (tny_account, forget_password);
422 modest_tny_account_store_finalize (GObject *obj)
424 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
425 ModestTnyAccountStorePrivate *priv =
426 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
428 if (priv->account_mgr) {
429 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
431 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
433 g_object_unref (G_OBJECT(priv->account_mgr));
434 priv->account_mgr = NULL;
437 if (priv->tny_session_camel) {
438 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
439 priv->tny_session_camel = NULL;
443 g_object_unref (G_OBJECT(priv->device));
447 if (priv->store_lock)
448 g_mutex_free (priv->store_lock);
450 g_free (priv->cache_dir);
451 priv->cache_dir = NULL;
453 if (priv->password_hash) {
454 g_hash_table_destroy (priv->password_hash);
455 priv->password_hash = NULL;
458 G_OBJECT_CLASS(parent_class)->finalize (obj);
462 ModestTnyAccountStore*
463 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
466 ModestTnyAccountStorePrivate *priv;
467 TnyPlatformFactory *pfact;
469 g_return_val_if_fail (account_mgr, NULL);
471 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
473 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
475 g_object_ref(G_OBJECT(account_mgr));
476 priv->account_mgr = account_mgr;
478 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
479 G_CALLBACK (on_account_changed), obj);
480 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
481 G_CALLBACK (on_account_removed), obj);
483 priv->store_lock = g_mutex_new ();
485 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
487 g_printerr ("modest: cannot create platform factory\n");
488 g_object_unref (obj);
492 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
494 g_printerr ("modest: cannot create device instance\n");
495 g_object_unref (obj);
498 /* tny_device_force_online (priv->device); */
500 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
502 if (!priv->tny_session_camel) {
503 g_printerr ("modest: cannot create TnySessionCamel instance\n");
504 g_object_unref (obj);
508 return MODEST_TNY_ACCOUNT_STORE(obj);
513 add_account (TnyAccountStore *self, TnyAccount *account) {
515 ModestTnyAccountStore *account_store;
516 ModestTnyAccountStorePrivate *priv;
518 const gchar *account_name;
519 const gchar *hostname, *username, *proto;
521 g_return_val_if_fail (self, FALSE);
522 g_return_val_if_fail (account, FALSE);
524 account_store = MODEST_TNY_ACCOUNT_STORE(self);
525 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
527 account_name = tny_account_get_id(account);
529 g_printerr ("modest: failed to retrieve account name\n");
533 hostname = tny_account_get_hostname(account);
534 username = tny_account_get_user(account);
535 proto = tny_account_get_proto(account);
537 return modest_account_mgr_add_server_account (priv->account_mgr,
539 hostname, username, NULL,
545 modest_tny_account_store_add_store_account (TnyAccountStore *self,
546 TnyStoreAccount *account)
548 ModestTnyAccountStorePrivate *priv;
550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
551 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
552 priv->tny_session_camel);
554 if (!add_account (self, TNY_ACCOUNT(account)))
555 g_printerr ("modest: failed to add store account\n");
560 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
561 TnyTransportAccount *account)
563 ModestTnyAccountStorePrivate *priv;
565 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
566 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
567 priv->tny_session_camel);
569 if (!add_account (self, TNY_ACCOUNT(account)))
570 g_printerr ("modest: failed to add transport account\n");
575 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
576 ModestProtocolType modest_type)
578 ModestTnyAccountStorePrivate *priv;
582 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
584 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
585 key = MODEST_ACCOUNT_STORE_ACCOUNT;
586 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
587 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
589 g_assert_not_reached();
591 server = modest_account_mgr_get_string (priv->account_mgr,
597 if (!modest_account_mgr_account_exists (priv->account_mgr,
598 server, TRUE, NULL)) {
606 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
608 TnyGetAccountsRequestType type)
610 ModestTnyAccountStore *self;
611 ModestTnyAccountStorePrivate *priv;
612 GSList *accounts, *cursor;
613 ModestProtocolType modest_type;
615 g_return_if_fail (iface);
617 self = MODEST_TNY_ACCOUNT_STORE(iface);
618 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
621 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
622 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
624 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
625 modest_type = MODEST_PROTOCOL_TYPE_STORE;
627 case TNY_ACCOUNT_STORE_BOTH:
628 modest_type = MODEST_PROTOCOL_TYPE_ANY;
631 g_assert_not_reached ();
634 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
638 gchar *server_account;
640 gboolean is_server_account;
642 account_name = (gchar*)cursor->data;
644 is_server_account = FALSE;
646 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
647 g_free (account_name);
648 cursor = cursor->next;
652 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
653 server_account = get_server_account_for_account (self, account_name,
654 MODEST_PROTOCOL_TYPE_TRANSPORT);
655 if (server_account) {
656 account = tny_account_from_name (self, account_name,
658 MODEST_PROTOCOL_TYPE_TRANSPORT);
659 is_server_account = TRUE;
663 g_printerr ("modest: no transport account for '%s'\n",
666 tny_list_prepend (list, G_OBJECT(account));
668 g_free (server_account);
671 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
672 server_account = get_server_account_for_account (self, account_name,
673 MODEST_PROTOCOL_TYPE_STORE);
674 if (server_account) {
675 account = tny_account_from_name (self, account_name,
677 MODEST_PROTOCOL_TYPE_STORE);
678 is_server_account = TRUE;
682 g_printerr ("modest: no store account for '%s'\n",
685 tny_list_prepend (list, G_OBJECT(account));
686 g_free (server_account);
689 g_free (account_name);
690 cursor = cursor->next;
693 g_slist_free (accounts);
695 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
700 * the cache dir will be ~/.modest/cache
701 * might want to change this in a simple #define...
704 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
706 ModestTnyAccountStorePrivate *priv;
707 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
709 if (!priv->cache_dir)
710 priv->cache_dir = g_build_filename (g_get_home_dir(),
714 return priv->cache_dir;
719 * callers need to unref
722 modest_tny_account_store_get_device (TnyAccountStore *self)
724 ModestTnyAccountStorePrivate *priv;
726 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
728 return g_object_ref (G_OBJECT(priv->device));
734 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
737 g_printerr ("modest: alert [%d]: %s",
745 modest_tny_account_store_init (gpointer g, gpointer iface_data)
747 TnyAccountStoreIface *klass;
749 g_return_if_fail (g);
751 klass = (TnyAccountStoreIface *)g;
753 klass->get_accounts_func =
754 modest_tny_account_store_get_accounts;
755 klass->add_transport_account_func =
756 modest_tny_account_store_add_transport_account;
757 klass->add_store_account_func =
758 modest_tny_account_store_add_store_account;
759 klass->get_cache_dir_func =
760 modest_tny_account_store_get_cache_dir;
761 klass->get_device_func =
762 modest_tny_account_store_get_device;
764 modest_tny_account_store_alert;
768 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
769 ModestTnyGetPassFunc func)
771 g_warning (__FUNCTION__);
772 return; /* not implemented, we use signals */
777 tny_account_store_get_session (TnyAccountStore *self)
779 g_return_val_if_fail (self, NULL);
781 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;