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"
48 #include <camel/camel.h>
51 /* 'private'/'protected' functions */
52 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
53 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
54 static void modest_tny_account_store_finalize (GObject *obj);
56 /* implementations for tny-account-store-iface */
57 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
59 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
60 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
61 TnyStoreAccount *account);
62 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
63 TnyTransportAccount *account);
64 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
65 TnyGetAccountsRequestType type);
68 PASSWORD_REQUESTED_SIGNAL,
69 ACCOUNT_UPDATE_SIGNAL,
73 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
74 struct _ModestTnyAccountStorePrivate {
80 GHashTable *password_hash;
82 TnySessionCamel *tny_session_camel;
85 ModestAccountMgr *account_mgr;
88 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
89 MODEST_TYPE_TNY_ACCOUNT_STORE, \
90 ModestTnyAccountStorePrivate))
92 static GObjectClass *parent_class = NULL;
94 static guint signals[LAST_SIGNAL] = {0};
97 modest_tny_account_store_get_type (void)
99 static GType my_type = 0;
102 static const GTypeInfo my_info = {
103 sizeof(ModestTnyAccountStoreClass),
104 NULL, /* base init */
105 NULL, /* base finalize */
106 (GClassInitFunc) modest_tny_account_store_class_init,
107 NULL, /* class finalize */
108 NULL, /* class data */
109 sizeof(ModestTnyAccountStore),
111 (GInstanceInitFunc) modest_tny_account_store_instance_init,
115 static const GInterfaceInfo iface_info = {
116 (GInterfaceInitFunc) modest_tny_account_store_init,
117 NULL, /* interface_finalize */
118 NULL /* interface_data */
121 my_type = g_type_register_static (G_TYPE_OBJECT,
122 "ModestTnyAccountStore",
124 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
131 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
133 GObjectClass *gobject_class;
134 gobject_class = (GObjectClass*) klass;
136 parent_class = g_type_class_peek_parent (klass);
137 gobject_class->finalize = modest_tny_account_store_finalize;
139 g_type_class_add_private (gobject_class,
140 sizeof(ModestTnyAccountStorePrivate));
142 signals[PASSWORD_REQUESTED_SIGNAL] =
143 g_signal_new ("password_requested",
144 G_TYPE_FROM_CLASS (gobject_class),
146 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
148 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
149 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
151 signals[ACCOUNT_UPDATE_SIGNAL] =
152 g_signal_new ("account_update",
153 G_TYPE_FROM_CLASS (gobject_class),
155 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
157 g_cclosure_marshal_VOID__STRING,
158 G_TYPE_NONE, 1, G_TYPE_STRING);
163 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
165 ModestTnyAccountStorePrivate *priv =
166 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
168 priv->account_mgr = NULL;
170 priv->cache_dir = NULL;
171 priv->tny_session_camel = NULL;
173 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
179 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
182 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
184 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
191 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
192 const gchar *key, gpointer user_data)
194 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
196 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
201 static ModestTnyAccountStore*
202 get_account_store_for_account (TnyAccount *account)
204 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
210 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
212 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
216 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
219 const TnyAccountStore *account_store;
220 ModestTnyAccountStore *self;
221 ModestTnyAccountStorePrivate *priv;
223 gboolean already_asked;
225 gdk_threads_enter ();
227 key = tny_account_get_id (account);
228 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
230 self = MODEST_TNY_ACCOUNT_STORE (account_store);
231 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
233 /* is it in the hash? if it's already there, it must be wrong... */
234 already_asked = g_hash_table_lookup_extended (priv->password_hash,
235 key, NULL, (gpointer *) &pwd);
237 /* if the password is not already there, try ModestConf */
238 if (!already_asked) {
239 pwd = modest_account_mgr_get_string (priv->account_mgr,
240 key, MODEST_ACCOUNT_PASSWORD,
242 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
245 /* if it was already asked, it must have been wrong, so ask again */
246 if (already_asked || !pwd || strlen(pwd) == 0) {
248 /* we don't have it yet. we emit a signal to get the password somewhere */
249 const gchar* name = tny_account_get_name (account);
253 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
254 name, &pwd, cancel, &remember);
258 modest_account_mgr_set_string (priv->account_mgr,
259 key, MODEST_ACCOUNT_PASSWORD,
262 /* We need to dup the string even knowing that
263 it's already a dup of the contents of an
264 entry, because it if it's wrong, then camel
266 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
268 g_hash_table_remove (priv->password_hash, key);
275 gdk_threads_leave ();
282 forget_password (TnyAccount *account) {
284 ModestTnyAccountStore *self;
285 ModestTnyAccountStorePrivate *priv;
286 const TnyAccountStore *account_store;
290 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
291 self = MODEST_TNY_ACCOUNT_STORE (account_store);
292 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
293 key = tny_account_get_id (account);
295 /* Do not remove the key, this will allow us to detect that we
296 have already asked for it at least once */
297 pwd = g_hash_table_lookup (priv->password_hash, key);
299 memset (pwd, 0, strlen (pwd));
300 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
303 /* Remove from configuration system */
304 modest_account_mgr_unset (priv->account_mgr,
305 key, MODEST_ACCOUNT_PASSWORD,
311 /* instantiate the correct tny account subclass */
313 tny_account_for_proto (const gchar* proto /* ugly */, ModestProtocolType modest_type)
315 TnyAccount *tny_account = NULL;
317 /* is it a store or a transport? */
318 if (modest_type == MODEST_PROTOCOL_TYPE_STORE) {
319 if (strcmp (proto, "pop") == 0)
320 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
321 else if (strcmp (proto, "imap") == 0)
322 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
324 } else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT) {
325 if (strcmp (proto, "sendmail") == 0)
326 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
327 else if (strcmp (proto, "smtp") == 0)
328 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
332 tny_account_set_proto (tny_account, proto);
334 g_printerr ("modest: could not get tny %s account (%d)\n",
342 /* create a tnyaccount for the server account connected to the account with name 'key'
345 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
346 const gchar *server_account, ModestProtocolType modest_type)
348 TnyAccount *tny_account;
349 ModestTnyAccountStorePrivate *priv;
351 GSList *options = NULL;
352 GError *error = NULL;
354 g_return_val_if_fail (self, NULL);
355 g_return_val_if_fail (account, NULL);
356 g_return_val_if_fail (server_account, NULL);
358 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
361 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
362 MODEST_ACCOUNT_PROTO, TRUE, NULL);
364 g_printerr ("modest: protocol not defined for '%s:%s'\n",
365 account, server_account);
369 tny_account = tny_account_for_proto (val, modest_type);
371 g_printerr ("modest: could not create account for '%s:%s'\n",
372 account, server_account);
376 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
379 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
380 priv->tny_session_camel);
383 tny_account_set_id (tny_account, server_account);
386 val = modest_account_mgr_get_string (priv->account_mgr, account,
387 MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
389 tny_account_set_name (tny_account, val);
392 g_printerr ("modest: display name not defined for '%s:%s'\n",
393 account, server_account);
394 g_object_unref (G_OBJECT(tny_account));
399 options = modest_account_mgr_get_list (priv->account_mgr,
400 tny_account_get_id (tny_account),
401 MODEST_ACCOUNT_OPTIONS,
402 MODEST_CONF_VALUE_STRING,
407 g_warning ("Error retrieving account %s options: %s",
408 tny_account_get_id (tny_account), error->message);
409 g_error_free (error);
411 GSList *tmp = options;
413 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
414 g_free (options->data);
415 options = g_slist_next (options);
421 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
422 MODEST_ACCOUNT_HOSTNAME, TRUE,
425 tny_account_set_hostname (tny_account, val);
430 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
431 MODEST_ACCOUNT_USERNAME, TRUE,
434 tny_account_set_user (tny_account, val);
438 tny_account_set_pass_func (tny_account, get_password);
439 tny_account_set_forget_pass_func (tny_account, forget_password);
447 modest_tny_account_store_finalize (GObject *obj)
449 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
450 ModestTnyAccountStorePrivate *priv =
451 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
453 if (priv->account_mgr) {
454 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
456 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
458 g_object_unref (G_OBJECT(priv->account_mgr));
459 priv->account_mgr = NULL;
462 if (priv->tny_session_camel) {
463 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
464 priv->tny_session_camel = NULL;
468 g_object_unref (G_OBJECT(priv->device));
472 if (priv->store_lock)
473 g_mutex_free (priv->store_lock);
475 g_free (priv->cache_dir);
476 priv->cache_dir = NULL;
478 if (priv->password_hash) {
479 g_hash_table_destroy (priv->password_hash);
480 priv->password_hash = NULL;
483 G_OBJECT_CLASS(parent_class)->finalize (obj);
487 ModestTnyAccountStore*
488 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
491 ModestTnyAccountStorePrivate *priv;
492 TnyPlatformFactory *pfact;
494 g_return_val_if_fail (account_mgr, NULL);
496 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
498 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
500 g_object_ref(G_OBJECT(account_mgr));
501 priv->account_mgr = account_mgr;
503 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
504 G_CALLBACK (on_account_changed), obj);
505 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
506 G_CALLBACK (on_account_removed), obj);
508 priv->store_lock = g_mutex_new ();
510 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
512 g_printerr ("modest: cannot create platform factory\n");
513 g_object_unref (obj);
517 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
519 g_printerr ("modest: cannot create device instance\n");
520 g_object_unref (obj);
523 /* tny_device_force_online (priv->device); */
525 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
527 if (!priv->tny_session_camel) {
528 g_printerr ("modest: cannot create TnySessionCamel instance\n");
529 g_object_unref (obj);
533 return MODEST_TNY_ACCOUNT_STORE(obj);
538 add_account (TnyAccountStore *self, TnyAccount *account) {
540 ModestTnyAccountStore *account_store;
541 ModestTnyAccountStorePrivate *priv;
543 const gchar *account_name;
544 const gchar *hostname, *username, *proto;
546 g_return_val_if_fail (self, FALSE);
547 g_return_val_if_fail (account, FALSE);
549 account_store = MODEST_TNY_ACCOUNT_STORE(self);
550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
552 account_name = tny_account_get_id(account);
554 g_printerr ("modest: failed to retrieve account name\n");
558 hostname = tny_account_get_hostname(account);
559 username = tny_account_get_user(account);
560 proto = tny_account_get_proto(account);
562 return modest_account_mgr_add_server_account (priv->account_mgr,
564 hostname, username, NULL,
565 modest_protocol_info_get_protocol(proto));
570 modest_tny_account_store_add_store_account (TnyAccountStore *self,
571 TnyStoreAccount *account)
573 ModestTnyAccountStorePrivate *priv;
575 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
576 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
577 priv->tny_session_camel);
579 if (!add_account (self, TNY_ACCOUNT(account)))
580 g_printerr ("modest: failed to add store account\n");
585 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
586 TnyTransportAccount *account)
588 ModestTnyAccountStorePrivate *priv;
590 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
591 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
592 priv->tny_session_camel);
594 if (!add_account (self, TNY_ACCOUNT(account)))
595 g_printerr ("modest: failed to add transport account\n");
600 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
601 ModestProtocolType modest_type)
603 ModestTnyAccountStorePrivate *priv;
607 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
609 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
610 key = MODEST_ACCOUNT_STORE_ACCOUNT;
611 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
612 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
614 g_assert_not_reached();
616 server = modest_account_mgr_get_string (priv->account_mgr,
622 if (!modest_account_mgr_account_exists (priv->account_mgr,
623 server, TRUE, NULL)) {
631 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
633 TnyGetAccountsRequestType type)
635 ModestTnyAccountStore *self;
636 ModestTnyAccountStorePrivate *priv;
637 GSList *accounts, *cursor;
638 ModestProtocolType modest_type;
640 g_return_if_fail (iface);
642 self = MODEST_TNY_ACCOUNT_STORE(iface);
643 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
646 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
647 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
649 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
650 modest_type = MODEST_PROTOCOL_TYPE_STORE;
652 case TNY_ACCOUNT_STORE_BOTH:
653 modest_type = MODEST_PROTOCOL_TYPE_UNKNOWN;
656 g_assert_not_reached ();
659 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
663 gchar *server_account;
665 gboolean is_server_account;
667 account_name = (gchar*)cursor->data;
669 is_server_account = FALSE;
671 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
672 g_free (account_name);
673 cursor = cursor->next;
677 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
678 server_account = get_server_account_for_account (self, account_name,
679 MODEST_PROTOCOL_TYPE_TRANSPORT);
680 if (server_account) {
681 account = tny_account_from_name (self, account_name,
683 MODEST_PROTOCOL_TYPE_TRANSPORT);
684 is_server_account = TRUE;
688 g_printerr ("modest: no transport account for '%s'\n",
691 tny_list_prepend (list, G_OBJECT(account));
693 g_free (server_account);
696 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
697 server_account = get_server_account_for_account (self, account_name,
698 MODEST_PROTOCOL_TYPE_STORE);
699 if (server_account) {
700 account = tny_account_from_name (self, account_name,
702 MODEST_PROTOCOL_TYPE_STORE);
703 is_server_account = TRUE;
707 g_printerr ("modest: no store account for '%s'\n",
710 tny_list_prepend (list, G_OBJECT(account));
711 g_free (server_account);
714 g_free (account_name);
715 cursor = cursor->next;
718 g_slist_free (accounts);
720 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
725 * the cache dir will be ~/.modest/cache
726 * might want to change this in a simple #define...
729 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
731 ModestTnyAccountStorePrivate *priv;
732 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
734 if (!priv->cache_dir)
735 priv->cache_dir = g_build_filename (g_get_home_dir(),
739 return priv->cache_dir;
744 * callers need to unref
747 modest_tny_account_store_get_device (TnyAccountStore *self)
749 ModestTnyAccountStorePrivate *priv;
751 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
753 return g_object_ref (G_OBJECT(priv->device));
759 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
762 g_printerr ("modest: alert [%d]: %s",
770 modest_tny_account_store_init (gpointer g, gpointer iface_data)
772 TnyAccountStoreIface *klass;
774 g_return_if_fail (g);
776 klass = (TnyAccountStoreIface *)g;
778 klass->get_accounts_func =
779 modest_tny_account_store_get_accounts;
780 klass->add_transport_account_func =
781 modest_tny_account_store_add_transport_account;
782 klass->add_store_account_func =
783 modest_tny_account_store_add_store_account;
784 klass->get_cache_dir_func =
785 modest_tny_account_store_get_cache_dir;
786 klass->get_device_func =
787 modest_tny_account_store_get_device;
789 modest_tny_account_store_alert;
793 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
794 ModestTnyGetPassFunc func)
796 g_warning (__FUNCTION__);
797 return; /* not implemented, we use signals */
802 tny_account_store_get_session (TnyAccountStore *self)
804 g_return_val_if_fail (self, NULL);
806 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;