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-imap-store-account.h>
40 #include <tny-camel-pop-store-account.h>
41 #include <modest-marshal.h>
42 #include <glib/gi18n.h>
43 #include "modest-account-mgr.h"
44 #include "modest-tny-account-store.h"
45 #include "modest-tny-platform-factory.h"
47 #include <camel/camel.h>
50 /* 'private'/'protected' functions */
51 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
52 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
53 static void modest_tny_account_store_finalize (GObject *obj);
55 /* implementations for tny-account-store-iface */
56 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
58 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
59 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
60 TnyStoreAccount *account);
61 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
62 TnyTransportAccount *account);
63 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
64 TnyGetAccountsRequestType type);
67 PASSWORD_REQUESTED_SIGNAL,
68 ACCOUNT_UPDATE_SIGNAL,
72 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
73 struct _ModestTnyAccountStorePrivate {
79 GHashTable *password_hash;
81 TnySessionCamel *tny_session_camel;
84 ModestAccountMgr *account_mgr;
87 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
88 MODEST_TYPE_TNY_ACCOUNT_STORE, \
89 ModestTnyAccountStorePrivate))
91 static GObjectClass *parent_class = NULL;
93 static guint signals[LAST_SIGNAL] = {0};
96 modest_tny_account_store_get_type (void)
98 static GType my_type = 0;
101 static const GTypeInfo my_info = {
102 sizeof(ModestTnyAccountStoreClass),
103 NULL, /* base init */
104 NULL, /* base finalize */
105 (GClassInitFunc) modest_tny_account_store_class_init,
106 NULL, /* class finalize */
107 NULL, /* class data */
108 sizeof(ModestTnyAccountStore),
110 (GInstanceInitFunc) modest_tny_account_store_instance_init,
114 static const GInterfaceInfo iface_info = {
115 (GInterfaceInitFunc) modest_tny_account_store_init,
116 NULL, /* interface_finalize */
117 NULL /* interface_data */
120 my_type = g_type_register_static (G_TYPE_OBJECT,
121 "ModestTnyAccountStore",
123 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
130 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
132 GObjectClass *gobject_class;
133 gobject_class = (GObjectClass*) klass;
135 parent_class = g_type_class_peek_parent (klass);
136 gobject_class->finalize = modest_tny_account_store_finalize;
138 g_type_class_add_private (gobject_class,
139 sizeof(ModestTnyAccountStorePrivate));
141 signals[PASSWORD_REQUESTED_SIGNAL] =
142 g_signal_new ("password_requested",
143 G_TYPE_FROM_CLASS (gobject_class),
145 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
147 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
148 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
150 signals[ACCOUNT_UPDATE_SIGNAL] =
151 g_signal_new ("account_update",
152 G_TYPE_FROM_CLASS (gobject_class),
154 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
156 g_cclosure_marshal_VOID__STRING,
157 G_TYPE_NONE, 1, G_TYPE_STRING);
162 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
164 ModestTnyAccountStorePrivate *priv =
165 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
167 priv->account_mgr = NULL;
169 priv->cache_dir = NULL;
170 priv->tny_session_camel = NULL;
172 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
178 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
181 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
183 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
190 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
191 const gchar *key, gpointer user_data)
193 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
195 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
200 static ModestTnyAccountStore*
201 get_account_store_for_account (TnyAccount *account)
203 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
209 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
211 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
215 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
218 const TnyAccountStore *account_store;
219 ModestTnyAccountStore *self;
220 ModestTnyAccountStorePrivate *priv;
222 gboolean already_asked;
224 gdk_threads_enter ();
226 key = tny_account_get_id (account);
227 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
229 self = MODEST_TNY_ACCOUNT_STORE (account_store);
230 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
232 /* is it in the hash? if it's already there, it must be wrong... */
233 already_asked = g_hash_table_lookup_extended (priv->password_hash,
234 key, NULL, (gpointer *) &pwd);
236 /* if the password is not already there, try ModestConf */
237 if (!already_asked) {
238 pwd = modest_account_mgr_get_string (priv->account_mgr,
239 key, MODEST_ACCOUNT_PASSWORD,
241 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
244 /* if it was already asked, it must have been wrong, so ask again */
245 if (already_asked || !pwd || strlen(pwd) == 0) {
247 /* we don't have it yet. we emit a signal to get the password somewhere */
248 const gchar* name = tny_account_get_name (account);
252 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
253 name, &pwd, cancel, &remember);
257 modest_account_mgr_set_string (priv->account_mgr,
258 key, MODEST_ACCOUNT_PASSWORD,
261 /* We need to dup the string even knowing that
262 it's already a dup of the contents of an
263 entry, because it if it's wrong, then camel
265 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
267 g_hash_table_remove (priv->password_hash, key);
274 gdk_threads_leave ();
281 forget_password (TnyAccount *account) {
283 ModestTnyAccountStore *self;
284 ModestTnyAccountStorePrivate *priv;
285 const TnyAccountStore *account_store;
289 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
290 self = MODEST_TNY_ACCOUNT_STORE (account_store);
291 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
292 key = tny_account_get_id (account);
294 /* Do not remove the key, this will allow us to detect that we
295 have already asked for it at least once */
296 pwd = g_hash_table_lookup (priv->password_hash, key);
298 memset (pwd, 0, strlen (pwd));
299 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
302 /* Remove from configuration system */
303 modest_account_mgr_unset (priv->account_mgr,
304 key, MODEST_ACCOUNT_PASSWORD,
310 /* instantiate the correct tny account subclass */
312 tny_account_for_proto (const gchar* proto /* ugly */, ModestProtocolType modest_type)
314 TnyAccount *tny_account = NULL;
316 /* is it a store or a transport? */
317 if (modest_type == MODEST_PROTOCOL_TYPE_STORE) {
318 if (strcmp (proto, "pop") == 0)
319 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
320 else if (strcmp (proto, "imap") == 0)
321 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
323 } else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT) {
324 if (strcmp (proto, "sendmail") == 0)
325 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
326 else if (strcmp (proto, "smtp") == 0)
327 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
331 tny_account_set_proto (tny_account, proto);
333 g_printerr ("modest: could not get tny %s account (%d)\n",
341 /* create a tnyaccount for the server account connected to the account with name 'key'
344 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
345 const gchar *server_account, ModestProtocolType modest_type)
347 TnyAccount *tny_account;
348 ModestTnyAccountStorePrivate *priv;
350 GSList *options = NULL;
351 GError *error = NULL;
353 g_return_val_if_fail (self, NULL);
354 g_return_val_if_fail (account, NULL);
355 g_return_val_if_fail (server_account, NULL);
357 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
360 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
361 MODEST_ACCOUNT_PROTO, TRUE, NULL);
363 g_printerr ("modest: protocol not defined for '%s:%s'\n",
364 account, server_account);
368 tny_account = tny_account_for_proto (val, modest_type);
370 g_printerr ("modest: could not create account for '%s:%s'\n",
371 account, server_account);
375 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
378 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
379 priv->tny_session_camel);
382 tny_account_set_id (tny_account, server_account);
385 val = modest_account_mgr_get_string (priv->account_mgr, account,
386 MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
388 tny_account_set_name (tny_account, val);
391 g_printerr ("modest: display name not defined for '%s:%s'\n",
392 account, server_account);
393 g_object_unref (G_OBJECT(tny_account));
398 options = modest_account_mgr_get_list (priv->account_mgr,
399 tny_account_get_id (tny_account),
400 MODEST_ACCOUNT_OPTIONS,
401 MODEST_CONF_VALUE_STRING,
406 g_warning ("Error retrieving account %s options: %s",
407 tny_account_get_id (tny_account), error->message);
408 g_error_free (error);
410 GSList *tmp = options;
412 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
413 g_free (options->data);
414 options = g_slist_next (options);
420 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
421 MODEST_ACCOUNT_HOSTNAME, TRUE,
424 tny_account_set_hostname (tny_account, val);
429 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
430 MODEST_ACCOUNT_USERNAME, TRUE,
433 tny_account_set_user (tny_account, val);
437 tny_account_set_pass_func (tny_account, get_password);
438 tny_account_set_forget_pass_func (tny_account, forget_password);
446 modest_tny_account_store_finalize (GObject *obj)
448 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
449 ModestTnyAccountStorePrivate *priv =
450 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
452 if (priv->account_mgr) {
453 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
455 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
457 g_object_unref (G_OBJECT(priv->account_mgr));
458 priv->account_mgr = NULL;
461 if (priv->tny_session_camel) {
462 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
463 priv->tny_session_camel = NULL;
467 g_object_unref (G_OBJECT(priv->device));
471 if (priv->store_lock)
472 g_mutex_free (priv->store_lock);
474 g_free (priv->cache_dir);
475 priv->cache_dir = NULL;
477 if (priv->password_hash) {
478 g_hash_table_destroy (priv->password_hash);
479 priv->password_hash = NULL;
482 G_OBJECT_CLASS(parent_class)->finalize (obj);
486 ModestTnyAccountStore*
487 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
490 ModestTnyAccountStorePrivate *priv;
491 TnyPlatformFactory *pfact;
493 g_return_val_if_fail (account_mgr, NULL);
495 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
497 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
499 g_object_ref(G_OBJECT(account_mgr));
500 priv->account_mgr = account_mgr;
502 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
503 G_CALLBACK (on_account_changed), obj);
504 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
505 G_CALLBACK (on_account_removed), obj);
507 priv->store_lock = g_mutex_new ();
509 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
511 g_printerr ("modest: cannot create platform factory\n");
512 g_object_unref (obj);
516 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
518 g_printerr ("modest: cannot create device instance\n");
519 g_object_unref (obj);
522 /* tny_device_force_online (priv->device); */
524 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
526 if (!priv->tny_session_camel) {
527 g_printerr ("modest: cannot create TnySessionCamel instance\n");
528 g_object_unref (obj);
532 return MODEST_TNY_ACCOUNT_STORE(obj);
537 add_account (TnyAccountStore *self, TnyAccount *account) {
539 ModestTnyAccountStore *account_store;
540 ModestTnyAccountStorePrivate *priv;
542 const gchar *account_name;
543 const gchar *hostname, *username, *proto;
545 g_return_val_if_fail (self, FALSE);
546 g_return_val_if_fail (account, FALSE);
548 account_store = MODEST_TNY_ACCOUNT_STORE(self);
549 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
551 account_name = tny_account_get_id(account);
553 g_printerr ("modest: failed to retrieve account name\n");
557 hostname = tny_account_get_hostname(account);
558 username = tny_account_get_user(account);
559 proto = tny_account_get_proto(account);
561 return modest_account_mgr_add_server_account (priv->account_mgr,
563 hostname, username, NULL,
569 modest_tny_account_store_add_store_account (TnyAccountStore *self,
570 TnyStoreAccount *account)
572 ModestTnyAccountStorePrivate *priv;
574 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
575 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
576 priv->tny_session_camel);
578 if (!add_account (self, TNY_ACCOUNT(account)))
579 g_printerr ("modest: failed to add store account\n");
584 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
585 TnyTransportAccount *account)
587 ModestTnyAccountStorePrivate *priv;
589 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
590 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
591 priv->tny_session_camel);
593 if (!add_account (self, TNY_ACCOUNT(account)))
594 g_printerr ("modest: failed to add transport account\n");
599 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
600 ModestProtocolType modest_type)
602 ModestTnyAccountStorePrivate *priv;
606 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
608 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
609 key = MODEST_ACCOUNT_STORE_ACCOUNT;
610 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
611 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
613 g_assert_not_reached();
615 server = modest_account_mgr_get_string (priv->account_mgr,
621 if (!modest_account_mgr_account_exists (priv->account_mgr,
622 server, TRUE, NULL)) {
630 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
632 TnyGetAccountsRequestType type)
634 ModestTnyAccountStore *self;
635 ModestTnyAccountStorePrivate *priv;
636 GSList *accounts, *cursor;
637 ModestProtocolType modest_type;
639 g_return_if_fail (iface);
641 self = MODEST_TNY_ACCOUNT_STORE(iface);
642 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
645 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
646 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
648 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
649 modest_type = MODEST_PROTOCOL_TYPE_STORE;
651 case TNY_ACCOUNT_STORE_BOTH:
652 modest_type = MODEST_PROTOCOL_TYPE_ANY;
655 g_assert_not_reached ();
658 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
662 gchar *server_account;
664 gboolean is_server_account;
666 account_name = (gchar*)cursor->data;
668 is_server_account = FALSE;
670 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
671 g_free (account_name);
672 cursor = cursor->next;
676 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
677 server_account = get_server_account_for_account (self, account_name,
678 MODEST_PROTOCOL_TYPE_TRANSPORT);
679 if (server_account) {
680 account = tny_account_from_name (self, account_name,
682 MODEST_PROTOCOL_TYPE_TRANSPORT);
683 is_server_account = TRUE;
687 g_printerr ("modest: no transport account for '%s'\n",
690 tny_list_prepend (list, G_OBJECT(account));
692 g_free (server_account);
695 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
696 server_account = get_server_account_for_account (self, account_name,
697 MODEST_PROTOCOL_TYPE_STORE);
698 if (server_account) {
699 account = tny_account_from_name (self, account_name,
701 MODEST_PROTOCOL_TYPE_STORE);
702 is_server_account = TRUE;
706 g_printerr ("modest: no store account for '%s'\n",
709 tny_list_prepend (list, G_OBJECT(account));
710 g_free (server_account);
713 g_free (account_name);
714 cursor = cursor->next;
717 g_slist_free (accounts);
719 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
724 * the cache dir will be ~/.modest/cache
725 * might want to change this in a simple #define...
728 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
730 ModestTnyAccountStorePrivate *priv;
731 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
733 if (!priv->cache_dir)
734 priv->cache_dir = g_build_filename (g_get_home_dir(),
738 return priv->cache_dir;
743 * callers need to unref
746 modest_tny_account_store_get_device (TnyAccountStore *self)
748 ModestTnyAccountStorePrivate *priv;
750 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
752 return g_object_ref (G_OBJECT(priv->device));
758 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
761 g_printerr ("modest: alert [%d]: %s",
769 modest_tny_account_store_init (gpointer g, gpointer iface_data)
771 TnyAccountStoreIface *klass;
773 g_return_if_fail (g);
775 klass = (TnyAccountStoreIface *)g;
777 klass->get_accounts_func =
778 modest_tny_account_store_get_accounts;
779 klass->add_transport_account_func =
780 modest_tny_account_store_add_transport_account;
781 klass->add_store_account_func =
782 modest_tny_account_store_add_store_account;
783 klass->get_cache_dir_func =
784 modest_tny_account_store_get_cache_dir;
785 klass->get_device_func =
786 modest_tny_account_store_get_device;
788 modest_tny_account_store_alert;
792 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
793 ModestTnyGetPassFunc func)
795 g_warning (__FUNCTION__);
796 return; /* not implemented, we use signals */
801 tny_account_store_get_session (TnyAccountStore *self)
803 g_return_val_if_fail (self, NULL);
805 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;