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 {
76 GHashTable *password_hash;
78 TnyPlatformFactory *platform_fact;
79 TnySessionCamel *tny_session_camel;
82 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
83 MODEST_TYPE_TNY_ACCOUNT_STORE, \
84 ModestTnyAccountStorePrivate))
86 static GObjectClass *parent_class = NULL;
88 static guint signals[LAST_SIGNAL] = {0};
91 modest_tny_account_store_get_type (void)
93 static GType my_type = 0;
96 static const GTypeInfo my_info = {
97 sizeof(ModestTnyAccountStoreClass),
99 NULL, /* base finalize */
100 (GClassInitFunc) modest_tny_account_store_class_init,
101 NULL, /* class finalize */
102 NULL, /* class data */
103 sizeof(ModestTnyAccountStore),
105 (GInstanceInitFunc) modest_tny_account_store_instance_init,
109 static const GInterfaceInfo iface_info = {
110 (GInterfaceInitFunc) modest_tny_account_store_init,
111 NULL, /* interface_finalize */
112 NULL /* interface_data */
115 my_type = g_type_register_static (G_TYPE_OBJECT,
116 "ModestTnyAccountStore",
118 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
125 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
127 GObjectClass *gobject_class;
128 gobject_class = (GObjectClass*) klass;
130 parent_class = g_type_class_peek_parent (klass);
131 gobject_class->finalize = modest_tny_account_store_finalize;
133 g_type_class_add_private (gobject_class,
134 sizeof(ModestTnyAccountStorePrivate));
136 signals[PASSWORD_REQUESTED_SIGNAL] =
137 g_signal_new ("password_requested",
138 G_TYPE_FROM_CLASS (gobject_class),
140 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
142 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
143 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
145 signals[ACCOUNT_UPDATE_SIGNAL] =
146 g_signal_new ("account_update",
147 G_TYPE_FROM_CLASS (gobject_class),
149 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
151 g_cclosure_marshal_VOID__STRING,
152 G_TYPE_NONE, 1, G_TYPE_STRING);
157 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
159 ModestTnyAccountStorePrivate *priv =
160 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
162 priv->cache_dir = NULL;
163 priv->platform_fact = NULL;
164 priv->tny_session_camel = NULL;
167 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
173 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
176 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
178 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
185 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
186 const gchar *key, gpointer user_data)
188 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
190 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
195 static ModestTnyAccountStore*
196 get_account_store_for_account (TnyAccount *account)
198 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
204 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
206 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
210 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
213 ModestAccountMgr *account_mgr;
214 const TnyAccountStore *account_store;
215 ModestTnyAccountStore *self;
216 ModestTnyAccountStorePrivate *priv;
218 gboolean already_asked;
220 key = tny_account_get_id (account);
221 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
223 self = MODEST_TNY_ACCOUNT_STORE (account_store);
224 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
226 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
227 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
229 /* is it in the hash? if it's already there, it must be wrong... */
230 already_asked = g_hash_table_lookup_extended (priv->password_hash,
231 key, NULL, (gpointer *) &pwd);
233 /* if the password is not already there, try ModestConf */
234 if (!already_asked) {
235 pwd = modest_account_mgr_get_string (account_mgr,
236 key, MODEST_ACCOUNT_PASSWORD,
238 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
241 /* if it was already asked, it must have been wrong, so ask again */
242 if (already_asked || !pwd || strlen(pwd) == 0) {
244 /* we don't have it yet. we emit a signal to get the password somewhere */
245 const gchar* name = tny_account_get_name (account);
249 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
250 name, &pwd, cancel, &remember);
254 modest_account_mgr_set_string (account_mgr,
255 key, MODEST_ACCOUNT_PASSWORD,
258 /* We need to dup the string even knowing that
259 it's already a dup of the contents of an
260 entry, because it if it's wrong, then camel
262 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
264 g_hash_table_remove (priv->password_hash, key);
276 forget_password (TnyAccount *account) {
278 ModestTnyAccountStore *self;
279 ModestTnyAccountStorePrivate *priv;
280 const TnyAccountStore *account_store;
283 ModestAccountMgr *account_mgr;
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 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
299 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
301 /* Remove from configuration system */
302 modest_account_mgr_unset (account_mgr,
303 key, MODEST_ACCOUNT_PASSWORD,
309 /* instantiate the correct tny account subclass */
311 tny_account_for_proto (ModestProtocol proto)
313 ModestProtocolType type;
314 TnyAccount *tny_account = NULL;
316 type = modest_protocol_info_get_protocol_type (proto);
318 if (type == MODEST_PROTOCOL_TYPE_TRANSPORT)
319 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
320 else if (proto == MODEST_PROTOCOL_STORE_POP)
321 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
322 else if (proto == MODEST_PROTOCOL_STORE_IMAP)
323 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
325 g_return_val_if_reached (NULL);
328 tny_account_set_proto (tny_account,
329 modest_protocol_info_get_protocol_name(proto));
331 g_printerr ("modest: could not get tny account for %d\n",
337 /* create a tnyaccount for the server account connected to the account with name 'key'
340 get_tny_account_from_server_account (ModestTnyAccountStore *self,
341 ModestServerAccountData *account_data,
342 ModestProtocolType modest_type)
344 TnyAccount *tny_account;
345 ModestTnyAccountStorePrivate *priv;
347 g_return_val_if_fail (self, NULL);
348 g_return_val_if_fail (account_data, NULL);
350 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
353 tny_account = tny_account_for_proto (account_data->proto);
355 g_printerr ("modest: could not create tny account for '%s'\n",
356 account_data->account_name);
360 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
361 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), /* session */
362 priv->tny_session_camel);
363 tny_account_set_id (tny_account, account_data->account_name); /* id */
365 if (account_data->hostname)
366 tny_account_set_hostname (tny_account, account_data->hostname);
368 if (account_data->username)
369 tny_account_set_user (tny_account, account_data->username);
371 tny_account_set_pass_func (tny_account, get_password);
372 tny_account_set_forget_pass_func (tny_account, forget_password);
380 modest_tny_account_store_finalize (GObject *obj)
382 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
383 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
385 if (priv->tny_session_camel) {
386 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
387 priv->tny_session_camel = NULL;
390 if (priv->store_lock)
391 g_mutex_free (priv->store_lock);
393 g_free (priv->cache_dir);
394 priv->cache_dir = NULL;
397 g_object_unref (priv->device);
401 if (priv->password_hash) {
402 g_hash_table_destroy (priv->password_hash);
403 priv->password_hash = NULL;
406 G_OBJECT_CLASS(parent_class)->finalize (obj);
410 ModestTnyAccountStore*
411 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
414 ModestTnyAccountStorePrivate *priv;
415 TnyPlatformFactory *pfact;
417 g_return_val_if_fail (account_mgr, NULL);
419 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
420 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
422 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
423 G_CALLBACK (on_account_changed), obj);
424 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
425 G_CALLBACK (on_account_removed), obj);
426 priv->store_lock = g_mutex_new ();
428 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
430 g_printerr ("modest: cannot get platform factory instance\n");
431 g_object_unref (obj);
434 priv->platform_fact = pfact;
436 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
437 if (!priv->tny_session_camel) {
438 g_printerr ("modest: cannot create TnySessionCamel instance\n");
439 g_object_unref (obj);
443 tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
445 return MODEST_TNY_ACCOUNT_STORE(obj);
450 add_account (TnyAccountStore *self, TnyAccount *account) {
452 ModestTnyAccountStore *account_store;
453 ModestTnyAccountStorePrivate *priv;
454 ModestAccountMgr *account_mgr;
455 const gchar *account_name;
456 const gchar *hostname, *username, *proto;
458 g_return_val_if_fail (self, FALSE);
459 g_return_val_if_fail (account, FALSE);
461 account_store = MODEST_TNY_ACCOUNT_STORE(self);
462 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
463 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
464 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
467 account_name = tny_account_get_id(account);
469 g_printerr ("modest: failed to retrieve account name\n");
473 hostname = tny_account_get_hostname(account);
474 username = tny_account_get_user(account);
475 proto = tny_account_get_proto(account);
477 return modest_account_mgr_add_server_account (account_mgr,
479 hostname, username, NULL,
480 modest_protocol_info_get_protocol(proto));
485 modest_tny_account_store_add_store_account (TnyAccountStore *self,
486 TnyStoreAccount *account)
488 ModestTnyAccountStorePrivate *priv;
490 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
491 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
492 priv->tny_session_camel);
494 if (!add_account (self, TNY_ACCOUNT(account)))
495 g_printerr ("modest: failed to add store account\n");
500 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
501 TnyTransportAccount *account)
503 ModestTnyAccountStorePrivate *priv;
505 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
506 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
507 priv->tny_session_camel);
508 if (!add_account (self, TNY_ACCOUNT(account)))
509 g_printerr ("modest: failed to add transport account\n");
514 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
515 TnyGetAccountsRequestType type)
517 TnyAccount *tny_account = NULL;
518 ModestServerAccountData *server_account;
520 if (type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS && account_data->store_account)
521 server_account = account_data->store_account;
522 else if (type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS && account_data->transport_account)
523 server_account = account_data->transport_account;
525 g_return_val_if_reached (NULL);
527 if (!server_account) {
528 g_printerr ("modest: no %s account defined for '%s'\n",
529 type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS ? "store" : "transport",
530 account_data->display_name);
534 tny_account = get_tny_account_from_server_account (self, server_account, type);
536 g_printerr ("modest: failed to create tny account for %s\n",
537 account_data->account_name);
541 if (account_data->display_name)
542 tny_account_set_name (tny_account, account_data->display_name);
549 modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
550 TnyGetAccountsRequestType type)
552 ModestTnyAccountStore *self;
553 ModestTnyAccountStorePrivate *priv;
554 GSList *accounts, *cursor;
555 ModestAccountMgr *account_mgr;
557 g_return_if_fail (iface);
558 g_return_if_fail (TNY_IS_LIST(list));
560 self = MODEST_TNY_ACCOUNT_STORE(iface);
561 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
562 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
563 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
565 if (type == TNY_ACCOUNT_STORE_BOTH) {
566 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
567 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
570 accounts = modest_account_mgr_account_names (account_mgr, NULL);
571 for (cursor = accounts; cursor; cursor = cursor->next) {
572 TnyAccount *tny_account;
573 ModestAccountData *account_data =
574 modest_account_mgr_get_account_data (account_mgr,
575 (gchar*)cursor->data);
576 if (account_data && account_data->enabled) {
577 tny_account = get_tny_account_from_account (self, account_data, type);
579 tny_list_prepend (list, G_OBJECT(tny_account));
581 g_free (cursor->data);
582 modest_account_mgr_free_account_data (account_mgr, account_data);
584 g_slist_free (accounts);
585 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
590 * the cache dir will be ~/.modest/cache
591 * might want to change this in a simple #define...
594 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
596 ModestTnyAccountStorePrivate *priv;
597 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
599 if (!priv->cache_dir)
600 priv->cache_dir = g_build_filename (g_get_home_dir(), ".modest",
602 return priv->cache_dir;
607 * callers need to unref
610 modest_tny_account_store_get_device (TnyAccountStore *self)
612 ModestTnyAccountStorePrivate *priv;
614 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
617 priv->device = tny_platform_factory_new_device (priv->platform_fact);
619 return g_object_ref (G_OBJECT(priv->device));
625 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
628 g_printerr ("modest: alert [%d]: %s", type, prompt);
634 modest_tny_account_store_init (gpointer g, gpointer iface_data)
636 TnyAccountStoreIface *klass;
638 g_return_if_fail (g);
640 klass = (TnyAccountStoreIface *)g;
642 klass->get_accounts_func =
643 modest_tny_account_store_get_accounts;
644 klass->add_transport_account_func =
645 modest_tny_account_store_add_transport_account;
646 klass->add_store_account_func =
647 modest_tny_account_store_add_store_account;
648 klass->get_cache_dir_func =
649 modest_tny_account_store_get_cache_dir;
650 klass->get_device_func =
651 modest_tny_account_store_get_device;
653 modest_tny_account_store_alert;
657 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
658 ModestTnyGetPassFunc func)
660 g_warning (__FUNCTION__);
661 return; /* not implemented, we use signals */
666 tny_account_store_get_session (TnyAccountStore *self)
668 g_return_val_if_fail (self, NULL);
669 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;