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-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-marshal.h>
43 #include <modest-protocol-info.h>
44 #include "modest-account-mgr.h"
45 #include "modest-tny-account-store.h"
46 #include "modest-tny-platform-factory.h"
47 #include <tny-gtk-lockable.h>
48 #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 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);
277 forget_password (TnyAccount *account) {
279 ModestTnyAccountStore *self;
280 ModestTnyAccountStorePrivate *priv;
281 const TnyAccountStore *account_store;
285 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
286 self = MODEST_TNY_ACCOUNT_STORE (account_store);
287 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
288 key = tny_account_get_id (account);
290 /* Do not remove the key, this will allow us to detect that we
291 have already asked for it at least once */
292 pwd = g_hash_table_lookup (priv->password_hash, key);
294 memset (pwd, 0, strlen (pwd));
295 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
298 /* Remove from configuration system */
299 modest_account_mgr_unset (priv->account_mgr,
300 key, MODEST_ACCOUNT_PASSWORD,
306 /* instantiate the correct tny account subclass */
308 tny_account_for_proto (const gchar* proto /* ugly */, ModestProtocolType modest_type)
310 TnyAccount *tny_account = NULL;
312 /* is it a store or a transport? */
313 if (modest_type == MODEST_PROTOCOL_TYPE_STORE) {
314 if (strcmp (proto, "pop") == 0)
315 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
316 else if (strcmp (proto, "imap") == 0)
317 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
319 } else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT) {
320 if (strcmp (proto, "sendmail") == 0)
321 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
322 else if (strcmp (proto, "smtp") == 0)
323 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
327 tny_account_set_proto (tny_account, proto);
329 g_printerr ("modest: could not get tny %s account (%d)\n",
337 /* create a tnyaccount for the server account connected to the account with name 'key'
340 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
341 const gchar *server_account, ModestProtocolType modest_type)
343 TnyAccount *tny_account;
344 ModestTnyAccountStorePrivate *priv;
346 GSList *options = NULL;
347 GError *error = NULL;
349 g_return_val_if_fail (self, NULL);
350 g_return_val_if_fail (account, NULL);
351 g_return_val_if_fail (server_account, NULL);
353 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
356 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
357 MODEST_ACCOUNT_PROTO, TRUE, NULL);
359 g_printerr ("modest: protocol not defined for '%s:%s'\n",
360 account, server_account);
364 tny_account = tny_account_for_proto (val, modest_type);
366 g_printerr ("modest: could not create account for '%s:%s'\n",
367 account, server_account);
371 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
374 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
375 priv->tny_session_camel);
378 tny_account_set_id (tny_account, server_account);
381 val = modest_account_mgr_get_string (priv->account_mgr, account,
382 MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
384 tny_account_set_name (tny_account, val);
387 g_printerr ("modest: display name not defined for '%s:%s'\n",
388 account, server_account);
389 g_object_unref (G_OBJECT(tny_account));
394 options = modest_account_mgr_get_list (priv->account_mgr,
395 tny_account_get_id (tny_account),
396 MODEST_ACCOUNT_OPTIONS,
397 MODEST_CONF_VALUE_STRING,
402 g_warning ("Error retrieving account %s options: %s",
403 tny_account_get_id (tny_account), error->message);
404 g_error_free (error);
406 GSList *tmp = options;
408 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
409 g_free (options->data);
410 options = g_slist_next (options);
416 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
417 MODEST_ACCOUNT_HOSTNAME, TRUE,
420 tny_account_set_hostname (tny_account, val);
425 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
426 MODEST_ACCOUNT_USERNAME, TRUE,
429 tny_account_set_user (tny_account, val);
433 tny_account_set_pass_func (tny_account, get_password);
434 tny_account_set_forget_pass_func (tny_account, forget_password);
442 modest_tny_account_store_finalize (GObject *obj)
444 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
445 ModestTnyAccountStorePrivate *priv =
446 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
448 if (priv->account_mgr) {
449 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
451 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
453 g_object_unref (G_OBJECT(priv->account_mgr));
454 priv->account_mgr = NULL;
457 if (priv->tny_session_camel) {
458 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
459 priv->tny_session_camel = NULL;
463 g_object_unref (G_OBJECT(priv->device));
467 if (priv->store_lock)
468 g_mutex_free (priv->store_lock);
470 g_free (priv->cache_dir);
471 priv->cache_dir = NULL;
473 if (priv->password_hash) {
474 g_hash_table_destroy (priv->password_hash);
475 priv->password_hash = NULL;
478 G_OBJECT_CLASS(parent_class)->finalize (obj);
482 ModestTnyAccountStore*
483 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
486 ModestTnyAccountStorePrivate *priv;
487 TnyPlatformFactory *pfact;
489 g_return_val_if_fail (account_mgr, NULL);
491 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
493 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
495 g_object_ref(G_OBJECT(account_mgr));
496 priv->account_mgr = account_mgr;
498 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
499 G_CALLBACK (on_account_changed), obj);
500 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
501 G_CALLBACK (on_account_removed), obj);
503 priv->store_lock = g_mutex_new ();
505 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
507 g_printerr ("modest: cannot create platform factory\n");
508 g_object_unref (obj);
512 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
514 g_printerr ("modest: cannot create device instance\n");
515 g_object_unref (obj);
519 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
521 if (!priv->tny_session_camel) {
522 g_printerr ("modest: cannot create TnySessionCamel instance\n");
523 g_object_unref (obj);
527 tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
529 return MODEST_TNY_ACCOUNT_STORE(obj);
534 add_account (TnyAccountStore *self, TnyAccount *account) {
536 ModestTnyAccountStore *account_store;
537 ModestTnyAccountStorePrivate *priv;
539 const gchar *account_name;
540 const gchar *hostname, *username, *proto;
542 g_return_val_if_fail (self, FALSE);
543 g_return_val_if_fail (account, FALSE);
545 account_store = MODEST_TNY_ACCOUNT_STORE(self);
546 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
548 account_name = tny_account_get_id(account);
550 g_printerr ("modest: failed to retrieve account name\n");
554 hostname = tny_account_get_hostname(account);
555 username = tny_account_get_user(account);
556 proto = tny_account_get_proto(account);
558 return modest_account_mgr_add_server_account (priv->account_mgr,
560 hostname, username, NULL,
561 modest_protocol_info_get_protocol(proto));
566 modest_tny_account_store_add_store_account (TnyAccountStore *self,
567 TnyStoreAccount *account)
569 ModestTnyAccountStorePrivate *priv;
571 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
572 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
573 priv->tny_session_camel);
575 if (!add_account (self, TNY_ACCOUNT(account)))
576 g_printerr ("modest: failed to add store account\n");
581 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
582 TnyTransportAccount *account)
584 ModestTnyAccountStorePrivate *priv;
586 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
587 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
588 priv->tny_session_camel);
590 if (!add_account (self, TNY_ACCOUNT(account)))
591 g_printerr ("modest: failed to add transport account\n");
596 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
597 ModestProtocolType modest_type)
599 ModestTnyAccountStorePrivate *priv;
603 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
605 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
606 key = MODEST_ACCOUNT_STORE_ACCOUNT;
607 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
608 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
610 g_assert_not_reached();
612 server = modest_account_mgr_get_string (priv->account_mgr,
618 if (!modest_account_mgr_account_exists (priv->account_mgr,
619 server, TRUE, NULL)) {
627 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
629 TnyGetAccountsRequestType type)
631 ModestTnyAccountStore *self;
632 ModestTnyAccountStorePrivate *priv;
633 GSList *accounts, *cursor;
634 ModestProtocolType modest_type;
636 g_return_if_fail (iface);
638 self = MODEST_TNY_ACCOUNT_STORE(iface);
639 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
642 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
643 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
645 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
646 modest_type = MODEST_PROTOCOL_TYPE_STORE;
648 case TNY_ACCOUNT_STORE_BOTH:
649 modest_type = MODEST_PROTOCOL_TYPE_UNKNOWN;
652 g_assert_not_reached ();
655 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
659 gchar *server_account;
661 gboolean is_server_account;
663 account_name = (gchar*)cursor->data;
665 is_server_account = FALSE;
667 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
668 g_free (account_name);
669 cursor = cursor->next;
673 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
674 server_account = get_server_account_for_account (self, account_name,
675 MODEST_PROTOCOL_TYPE_TRANSPORT);
676 if (server_account) {
677 account = tny_account_from_name (self, account_name,
679 MODEST_PROTOCOL_TYPE_TRANSPORT);
680 is_server_account = TRUE;
684 g_printerr ("modest: no transport account for '%s'\n",
687 tny_list_prepend (list, G_OBJECT(account));
689 g_free (server_account);
692 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
693 server_account = get_server_account_for_account (self, account_name,
694 MODEST_PROTOCOL_TYPE_STORE);
695 if (server_account) {
696 account = tny_account_from_name (self, account_name,
698 MODEST_PROTOCOL_TYPE_STORE);
699 is_server_account = TRUE;
703 g_printerr ("modest: no store account for '%s'\n",
706 tny_list_prepend (list, G_OBJECT(account));
707 g_free (server_account);
710 g_free (account_name);
711 cursor = cursor->next;
714 g_slist_free (accounts);
716 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
721 * the cache dir will be ~/.modest/cache
722 * might want to change this in a simple #define...
725 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
727 ModestTnyAccountStorePrivate *priv;
728 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
730 if (!priv->cache_dir)
731 priv->cache_dir = g_build_filename (g_get_home_dir(),
735 return priv->cache_dir;
740 * callers need to unref
743 modest_tny_account_store_get_device (TnyAccountStore *self)
745 ModestTnyAccountStorePrivate *priv;
747 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
749 return g_object_ref (G_OBJECT(priv->device));
755 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
758 g_printerr ("modest: alert [%d]: %s",
766 modest_tny_account_store_init (gpointer g, gpointer iface_data)
768 TnyAccountStoreIface *klass;
770 g_return_if_fail (g);
772 klass = (TnyAccountStoreIface *)g;
774 klass->get_accounts_func =
775 modest_tny_account_store_get_accounts;
776 klass->add_transport_account_func =
777 modest_tny_account_store_add_transport_account;
778 klass->add_store_account_func =
779 modest_tny_account_store_add_store_account;
780 klass->get_cache_dir_func =
781 modest_tny_account_store_get_cache_dir;
782 klass->get_device_func =
783 modest_tny_account_store_get_device;
785 modest_tny_account_store_alert;
789 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
790 ModestTnyGetPassFunc func)
792 g_warning (__FUNCTION__);
793 return; /* not implemented, we use signals */
798 tny_account_store_get_session (TnyAccountStore *self)
800 g_return_val_if_fail (self, NULL);
802 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;