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 /* 'private'/'protected' functions */
47 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
48 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
49 static void modest_tny_account_store_finalize (GObject *obj);
51 /* implementations for tny-account-store-iface */
52 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
54 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
55 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
56 TnyStoreAccount *account);
57 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
58 TnyTransportAccount *account);
59 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
60 TnyGetAccountsRequestType type);
63 PASSWORD_REQUESTED_SIGNAL,
64 ACCOUNT_UPDATE_SIGNAL,
68 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
69 struct _ModestTnyAccountStorePrivate {
75 GHashTable *password_hash;
77 TnySessionCamel *tny_session_camel;
80 ModestAccountMgr *account_mgr;
83 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
84 MODEST_TYPE_TNY_ACCOUNT_STORE, \
85 ModestTnyAccountStorePrivate))
87 static GObjectClass *parent_class = NULL;
89 static guint signals[LAST_SIGNAL] = {0};
92 modest_tny_account_store_get_type (void)
94 static GType my_type = 0;
97 static const GTypeInfo my_info = {
98 sizeof(ModestTnyAccountStoreClass),
100 NULL, /* base finalize */
101 (GClassInitFunc) modest_tny_account_store_class_init,
102 NULL, /* class finalize */
103 NULL, /* class data */
104 sizeof(ModestTnyAccountStore),
106 (GInstanceInitFunc) modest_tny_account_store_instance_init,
110 static const GInterfaceInfo iface_info = {
111 (GInterfaceInitFunc) modest_tny_account_store_init,
112 NULL, /* interface_finalize */
113 NULL /* interface_data */
116 my_type = g_type_register_static (G_TYPE_OBJECT,
117 "ModestTnyAccountStore",
119 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
126 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
128 GObjectClass *gobject_class;
129 gobject_class = (GObjectClass*) klass;
131 parent_class = g_type_class_peek_parent (klass);
132 gobject_class->finalize = modest_tny_account_store_finalize;
134 g_type_class_add_private (gobject_class,
135 sizeof(ModestTnyAccountStorePrivate));
137 signals[PASSWORD_REQUESTED_SIGNAL] =
138 g_signal_new ("password_requested",
139 G_TYPE_FROM_CLASS (gobject_class),
141 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
143 modest_marshal_VOID__STRING_POINTER_POINTER,
144 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER);
146 signals[ACCOUNT_UPDATE_SIGNAL] =
147 g_signal_new ("account_update",
148 G_TYPE_FROM_CLASS (gobject_class),
150 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
152 g_cclosure_marshal_VOID__STRING,
153 G_TYPE_NONE, 1, G_TYPE_STRING);
158 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
160 ModestTnyAccountStorePrivate *priv =
161 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
163 priv->account_mgr = NULL;
165 priv->cache_dir = NULL;
166 priv->tny_session_camel = NULL;
168 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
174 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
177 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
179 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
186 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
187 const gchar *key, gpointer user_data)
189 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
191 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
196 static ModestTnyAccountStore*
197 get_account_store_for_account (TnyAccount *account)
199 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
205 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
207 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
212 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
215 const TnyAccountStore *account_store;
216 ModestTnyAccountStore *self;
217 ModestTnyAccountStorePrivate *priv;
219 gboolean already_asked;
221 g_return_val_if_fail (account, NULL);
223 key = tny_account_get_id (account);
224 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
226 self = MODEST_TNY_ACCOUNT_STORE (account_store);
227 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
229 /* is it in the hash? if it's already there, it must be wrong... */
230 already_asked = g_hash_table_lookup (priv->password_hash, key) != NULL;
232 /* if the password is not already there, try ModestConf */
233 /* if (!already_asked) */
234 pwd = modest_account_mgr_get_string (priv->account_mgr,
235 key, MODEST_ACCOUNT_PASSWORD,
238 /* /\* if it was already asked, it must have been wrong, so ask again *\/ */
239 /* if (!already_asked || !pwd || strlen(pwd) == 0) { */
241 /* /\* we don't have it yet. we emit a signal to get the password somewhere *\/ */
242 /* const gchar* name = tny_account_get_name (account); */
243 /* *cancel = TRUE; */
245 /* g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, */
246 /* name, &pwd, cancel); */
247 /* if (!*cancel) /\* remember the password *\/ */
248 /* modest_account_mgr_set_string (priv->account_mgr, */
249 /* key, MODEST_ACCOUNT_PASSWORD, */
250 /* pwd, TRUE, NULL); */
252 /* *cancel = FALSE; */
254 /* g_hash_table_insert (priv->password_hash, key, pwd); */
261 forget_password (TnyAccount *account) {
263 ModestTnyAccountStore *self;
264 ModestTnyAccountStorePrivate *priv;
265 const TnyAccountStore *account_store;
267 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
268 self = MODEST_TNY_ACCOUNT_STORE (account_store);
269 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
274 /* create a tnyaccount for the server account connected to the account with name 'key'
277 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
278 const gchar *server_account, ModestProtocolType modest_type)
280 TnyAccount *tny_account;
281 ModestTnyAccountStorePrivate *priv;
284 g_return_val_if_fail (self, NULL);
285 g_return_val_if_fail (account, NULL);
286 g_return_val_if_fail (server_account, NULL);
288 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
290 /* is it a store or a transport? */
291 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
292 tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
293 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
294 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
296 g_assert_not_reached ();
299 g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
300 account, server_account);
304 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
307 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
308 priv->tny_session_camel);
311 tny_account_set_id (tny_account, server_account);
312 tny_account_set_name (tny_account, account);
315 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
316 MODEST_ACCOUNT_PROTO, TRUE, NULL);
318 tny_account_set_proto (tny_account, val);
321 g_printerr ("modest: protocol not defined for '%s:%s'\n",
322 account, server_account);
323 g_object_unref (G_OBJECT(tny_account));
328 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
329 MODEST_ACCOUNT_HOSTNAME, TRUE,
332 tny_account_set_hostname (tny_account, val);
337 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
338 MODEST_ACCOUNT_USERNAME, TRUE,
341 tny_account_set_user (tny_account, val);
345 tny_account_set_pass_func (tny_account, get_password);
346 tny_account_set_forget_pass_func (tny_account, forget_password);
354 modest_tny_account_store_finalize (GObject *obj)
356 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
357 ModestTnyAccountStorePrivate *priv =
358 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
360 if (priv->account_mgr) {
361 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
363 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
365 g_object_unref (G_OBJECT(priv->account_mgr));
366 priv->account_mgr = NULL;
369 if (priv->tny_session_camel) {
370 // FIXME: how to kill a camel
371 priv->tny_session_camel = NULL;
375 g_object_unref (G_OBJECT(priv->device));
379 if (priv->store_lock)
380 g_mutex_free (priv->store_lock);
382 g_free (priv->cache_dir);
383 priv->cache_dir = NULL;
385 if (priv->password_hash) {
386 g_hash_table_destroy (priv->password_hash);
387 priv->password_hash = NULL;
390 G_OBJECT_CLASS(parent_class)->finalize (obj);
394 ModestTnyAccountStore*
395 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
398 ModestTnyAccountStorePrivate *priv;
399 TnyPlatformFactory *pfact;
401 g_return_val_if_fail (account_mgr, NULL);
403 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
405 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
407 g_object_ref(G_OBJECT(account_mgr));
408 priv->account_mgr = account_mgr;
410 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
411 G_CALLBACK (on_account_changed), obj);
412 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
413 G_CALLBACK (on_account_removed), obj);
415 priv->store_lock = g_mutex_new ();
417 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
419 g_printerr ("modest: cannot create platform factory\n");
420 g_object_unref (obj);
424 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
426 g_printerr ("modest: cannot create device instance\n");
427 g_object_unref (obj);
430 tny_device_force_online (priv->device);
432 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
434 if (!priv->tny_session_camel) {
435 g_printerr ("modest: cannot create TnySessionCamel instance\n");
436 g_object_unref (obj);
440 return MODEST_TNY_ACCOUNT_STORE(obj);
445 add_account (TnyAccountStore *self, TnyAccount *account) {
447 ModestTnyAccountStore *account_store;
448 ModestTnyAccountStorePrivate *priv;
450 const gchar *account_name;
451 const gchar *hostname, *username, *proto;
453 g_return_val_if_fail (self, FALSE);
454 g_return_val_if_fail (account, FALSE);
456 account_store = MODEST_TNY_ACCOUNT_STORE(self);
457 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
459 account_name = tny_account_get_id(account);
461 g_printerr ("modest: failed to retrieve account name\n");
465 hostname = tny_account_get_hostname(account);
466 username = tny_account_get_user(account);
467 proto = tny_account_get_proto(account);
469 return modest_account_mgr_add_server_account (priv->account_mgr,
471 hostname, username, NULL,
477 modest_tny_account_store_add_store_account (TnyAccountStore *self,
478 TnyStoreAccount *account)
480 ModestTnyAccountStorePrivate *priv;
482 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
483 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
484 priv->tny_session_camel);
486 if (!add_account (self, TNY_ACCOUNT(account)))
487 g_printerr ("modest: failed to add store account\n");
492 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
493 TnyTransportAccount *account)
495 ModestTnyAccountStorePrivate *priv;
497 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
498 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
499 priv->tny_session_camel);
501 if (!add_account (self, TNY_ACCOUNT(account)))
502 g_printerr ("modest: failed to add transport account\n");
507 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
508 ModestProtocolType modest_type)
510 ModestTnyAccountStorePrivate *priv;
514 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
516 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
517 key = MODEST_ACCOUNT_STORE_ACCOUNT;
518 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
519 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
521 g_assert_not_reached();
523 server = modest_account_mgr_get_string (priv->account_mgr,
529 if (!modest_account_mgr_account_exists (priv->account_mgr,
530 server, TRUE, NULL)) {
538 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
540 TnyGetAccountsRequestType type)
542 ModestTnyAccountStore *self;
543 ModestTnyAccountStorePrivate *priv;
544 GSList *accounts, *cursor;
545 ModestProtocolType modest_type;
547 g_return_if_fail (iface);
549 self = MODEST_TNY_ACCOUNT_STORE(iface);
550 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
553 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
554 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
556 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
557 modest_type = MODEST_PROTOCOL_TYPE_STORE;
559 case TNY_ACCOUNT_STORE_BOTH:
560 modest_type = MODEST_PROTOCOL_TYPE_ANY;
563 g_assert_not_reached ();
566 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
570 gchar *server_account;
573 account_name = (gchar*)cursor->data;
576 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
577 g_free (account_name);
578 cursor = cursor->next;
582 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
583 server_account = get_server_account_for_account (self, account_name,
584 MODEST_PROTOCOL_TYPE_TRANSPORT);
586 account = tny_account_from_name (self, account_name,
588 MODEST_PROTOCOL_TYPE_TRANSPORT);
590 g_printerr ("modest: no transport account for '%s'\n",
593 tny_list_prepend (list, G_OBJECT(account));
595 g_free (server_account);
598 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
599 server_account = get_server_account_for_account (self, account_name,
600 MODEST_PROTOCOL_TYPE_STORE);
602 account = tny_account_from_name (self, account_name,
604 MODEST_PROTOCOL_TYPE_STORE);
606 g_printerr ("modest: no store account for '%s'\n",
609 tny_list_prepend (list, G_OBJECT(account));
610 g_free (server_account);
613 g_free (account_name);
614 cursor = cursor->next;
617 g_slist_free (accounts);
619 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
620 /* tny_session_camel_set_current_accounts (priv->tny_session_camel, */
626 * the cache dir will be ~/.modest/cache
627 * might want to change this in a simple #define...
630 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
632 ModestTnyAccountStorePrivate *priv;
633 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
635 if (!priv->cache_dir)
636 priv->cache_dir = g_build_filename (g_get_home_dir(),
640 return priv->cache_dir;
645 * callers need to unref
648 modest_tny_account_store_get_device (TnyAccountStore *self)
650 ModestTnyAccountStorePrivate *priv;
652 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
654 return g_object_ref (G_OBJECT(priv->device));
660 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
663 g_printerr ("modest: alert [%d]: %s",
671 modest_tny_account_store_init (gpointer g, gpointer iface_data)
673 TnyAccountStoreIface *klass;
675 g_return_if_fail (g);
677 klass = (TnyAccountStoreIface *)g;
679 klass->get_accounts_func =
680 modest_tny_account_store_get_accounts;
681 klass->add_transport_account_func =
682 modest_tny_account_store_add_transport_account;
683 klass->add_store_account_func =
684 modest_tny_account_store_add_store_account;
685 klass->get_cache_dir_func =
686 modest_tny_account_store_get_cache_dir;
687 klass->get_device_func =
688 modest_tny_account_store_get_device;
690 modest_tny_account_store_alert;
694 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
695 ModestTnyGetPassFunc func)
697 g_warning (__FUNCTION__);
698 return; /* not implemented, we use signals */
703 tny_account_store_get_session (TnyAccountStore *self)
705 g_return_val_if_fail (self, NULL);
707 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;