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 "modest-account-mgr.h"
42 #include "modest-tny-account-store.h"
43 #include "modest-tny-platform-factory.h"
45 /* 'private'/'protected' functions */
46 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
47 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
48 static void modest_tny_account_store_finalize (GObject *obj);
50 /* implementations for tny-account-store-iface */
51 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
53 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
54 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
55 TnyStoreAccount *account);
56 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
57 TnyTransportAccount *account);
58 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
59 TnyGetAccountsRequestType type);
62 PASSWORD_REQUESTED_SIGNAL,
63 ACCOUNT_UPDATE_SIGNAL,
67 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
68 struct _ModestTnyAccountStorePrivate {
74 GHashTable *password_hash;
76 TnySessionCamel *tny_session_camel;
79 ModestAccountMgr *account_mgr;
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,
143 G_TYPE_NONE, 3, G_TYPE_STRING, 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->account_mgr = NULL;
164 priv->cache_dir = NULL;
165 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);
211 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
214 const TnyAccountStore *account_store;
215 ModestTnyAccountStore *self;
216 ModestTnyAccountStorePrivate *priv;
218 gboolean already_asked;
220 g_return_val_if_fail (account, NULL);
222 key = tny_account_get_id (account);
223 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
225 self = MODEST_TNY_ACCOUNT_STORE (account_store);
226 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
228 /* is it in the hash? if it's already there, it must be wrong... */
229 // already_asked = g_hash_table_lookup (priv->password_hash, key) != NULL;
230 /* if (already_asked) */
231 /* g_warning ("password already asked for or in config (%s)", */
234 /* g_warning ("password not yet asked for or in config (%s)", */
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,
243 /* if it was already asked, it must have been wrong, so ask again */
244 /* if (!already_asked || !pwd || strlen(pwd) == 0) { */
245 /* g_warning ("emit signal to get pass for %s", key); */
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); */
249 /* *cancel = TRUE; */
251 /* g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, */
252 /* name, &pwd, cancel); */
253 /* if (!*cancel) /\* remember the password *\/ */
254 /* modest_account_mgr_set_string (priv->account_mgr, */
255 /* key, MODEST_ACCOUNT_PASSWORD, */
256 /* pwd, TRUE, NULL); */
258 /* *cancel = FALSE; */
260 // g_hash_table_insert (priv->password_hash, (gpointer)key, (gpointer)pwd);
267 forget_password (TnyAccount *account) {
269 ModestTnyAccountStore *self;
270 ModestTnyAccountStorePrivate *priv;
271 const TnyAccountStore *account_store;
273 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
274 self = MODEST_TNY_ACCOUNT_STORE (account_store);
275 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
280 /* create a tnyaccount for the server account connected to the account with name 'key'
283 tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
284 const gchar *server_account, ModestProtocolType modest_type)
286 TnyAccount *tny_account;
287 ModestTnyAccountStorePrivate *priv;
290 g_return_val_if_fail (self, NULL);
291 g_return_val_if_fail (account, NULL);
292 g_return_val_if_fail (server_account, NULL);
294 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
296 /* is it a store or a transport? */
297 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
298 tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
299 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
300 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
302 g_assert_not_reached ();
305 g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
306 account, server_account);
310 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
313 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
314 priv->tny_session_camel);
317 tny_account_set_id (tny_account, server_account);
318 tny_account_set_name (tny_account, account);
321 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
322 MODEST_ACCOUNT_PROTO, TRUE, NULL);
324 tny_account_set_proto (tny_account, val);
327 g_printerr ("modest: protocol not defined for '%s:%s'\n",
328 account, server_account);
329 g_object_unref (G_OBJECT(tny_account));
334 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
335 MODEST_ACCOUNT_HOSTNAME, TRUE,
338 tny_account_set_hostname (tny_account, val);
343 val = modest_account_mgr_get_string (priv->account_mgr, server_account,
344 MODEST_ACCOUNT_USERNAME, TRUE,
347 tny_account_set_user (tny_account, val);
351 tny_account_set_pass_func (tny_account, get_password);
352 tny_account_set_forget_pass_func (tny_account, forget_password);
360 modest_tny_account_store_finalize (GObject *obj)
362 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
363 ModestTnyAccountStorePrivate *priv =
364 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
366 if (priv->account_mgr) {
367 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
369 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
371 g_object_unref (G_OBJECT(priv->account_mgr));
372 priv->account_mgr = NULL;
375 if (priv->tny_session_camel) {
376 // FIXME: how to kill a camel
377 priv->tny_session_camel = NULL;
381 g_object_unref (G_OBJECT(priv->device));
385 if (priv->store_lock)
386 g_mutex_free (priv->store_lock);
388 g_free (priv->cache_dir);
389 priv->cache_dir = NULL;
391 if (priv->password_hash) {
392 g_hash_table_destroy (priv->password_hash);
393 priv->password_hash = NULL;
396 G_OBJECT_CLASS(parent_class)->finalize (obj);
400 ModestTnyAccountStore*
401 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
404 ModestTnyAccountStorePrivate *priv;
405 TnyPlatformFactory *pfact;
407 g_return_val_if_fail (account_mgr, NULL);
409 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
411 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
413 g_object_ref(G_OBJECT(account_mgr));
414 priv->account_mgr = account_mgr;
416 priv->sig1 = g_signal_connect (G_OBJECT(account_mgr), "account_changed",
417 G_CALLBACK (on_account_changed), obj);
418 priv->sig2 = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
419 G_CALLBACK (on_account_removed), obj);
421 priv->store_lock = g_mutex_new ();
423 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
425 g_printerr ("modest: cannot create platform factory\n");
426 g_object_unref (obj);
430 priv->device = TNY_DEVICE(tny_platform_factory_new_device(pfact));
432 g_printerr ("modest: cannot create device instance\n");
433 g_object_unref (obj);
436 tny_device_force_online (priv->device);
438 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
440 if (!priv->tny_session_camel) {
441 g_printerr ("modest: cannot create TnySessionCamel instance\n");
442 g_object_unref (obj);
446 return MODEST_TNY_ACCOUNT_STORE(obj);
451 add_account (TnyAccountStore *self, TnyAccount *account) {
453 ModestTnyAccountStore *account_store;
454 ModestTnyAccountStorePrivate *priv;
456 const gchar *account_name;
457 const gchar *hostname, *username, *proto;
459 g_return_val_if_fail (self, FALSE);
460 g_return_val_if_fail (account, FALSE);
462 account_store = MODEST_TNY_ACCOUNT_STORE(self);
463 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
465 account_name = tny_account_get_id(account);
467 g_printerr ("modest: failed to retrieve account name\n");
471 hostname = tny_account_get_hostname(account);
472 username = tny_account_get_user(account);
473 proto = tny_account_get_proto(account);
475 return modest_account_mgr_add_server_account (priv->account_mgr,
477 hostname, username, NULL,
483 modest_tny_account_store_add_store_account (TnyAccountStore *self,
484 TnyStoreAccount *account)
486 ModestTnyAccountStorePrivate *priv;
488 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
489 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
490 priv->tny_session_camel);
492 if (!add_account (self, TNY_ACCOUNT(account)))
493 g_printerr ("modest: failed to add store account\n");
498 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
499 TnyTransportAccount *account)
501 ModestTnyAccountStorePrivate *priv;
503 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
504 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
505 priv->tny_session_camel);
507 if (!add_account (self, TNY_ACCOUNT(account)))
508 g_printerr ("modest: failed to add transport account\n");
513 get_server_account_for_account (ModestTnyAccountStore *self, const gchar *account_name,
514 ModestProtocolType modest_type)
516 ModestTnyAccountStorePrivate *priv;
520 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
522 if (modest_type == MODEST_PROTOCOL_TYPE_STORE)
523 key = MODEST_ACCOUNT_STORE_ACCOUNT;
524 else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
525 key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT;
527 g_assert_not_reached();
529 server = modest_account_mgr_get_string (priv->account_mgr,
535 if (!modest_account_mgr_account_exists (priv->account_mgr,
536 server, TRUE, NULL)) {
544 modest_tny_account_store_get_accounts (TnyAccountStore *iface,
546 TnyGetAccountsRequestType type)
548 ModestTnyAccountStore *self;
549 ModestTnyAccountStorePrivate *priv;
550 GSList *accounts, *cursor;
551 ModestProtocolType modest_type;
553 g_return_if_fail (iface);
555 self = MODEST_TNY_ACCOUNT_STORE(iface);
556 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
559 case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
560 modest_type = MODEST_PROTOCOL_TYPE_TRANSPORT;
562 case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
563 modest_type = MODEST_PROTOCOL_TYPE_STORE;
565 case TNY_ACCOUNT_STORE_BOTH:
566 modest_type = MODEST_PROTOCOL_TYPE_ANY;
569 g_assert_not_reached ();
572 cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
576 gchar *server_account;
579 account_name = (gchar*)cursor->data;
582 if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) {
583 g_free (account_name);
584 cursor = cursor->next;
588 if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
589 server_account = get_server_account_for_account (self, account_name,
590 MODEST_PROTOCOL_TYPE_TRANSPORT);
592 account = tny_account_from_name (self, account_name,
594 MODEST_PROTOCOL_TYPE_TRANSPORT);
596 g_printerr ("modest: no transport account for '%s'\n",
599 tny_list_prepend (list, G_OBJECT(account));
601 g_free (server_account);
604 if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
605 server_account = get_server_account_for_account (self, account_name,
606 MODEST_PROTOCOL_TYPE_STORE);
608 account = tny_account_from_name (self, account_name,
610 MODEST_PROTOCOL_TYPE_STORE);
612 g_printerr ("modest: no store account for '%s'\n",
615 tny_list_prepend (list, G_OBJECT(account));
616 g_free (server_account);
619 g_free (account_name);
620 cursor = cursor->next;
623 g_slist_free (accounts);
625 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
626 /* tny_session_camel_set_current_accounts (priv->tny_session_camel, */
632 * the cache dir will be ~/.modest/cache
633 * might want to change this in a simple #define...
636 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
638 ModestTnyAccountStorePrivate *priv;
639 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
641 if (!priv->cache_dir)
642 priv->cache_dir = g_build_filename (g_get_home_dir(),
646 return priv->cache_dir;
651 * callers need to unref
654 modest_tny_account_store_get_device (TnyAccountStore *self)
656 ModestTnyAccountStorePrivate *priv;
658 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
660 return g_object_ref (G_OBJECT(priv->device));
666 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
669 g_printerr ("modest: alert [%d]: %s",
677 modest_tny_account_store_init (gpointer g, gpointer iface_data)
679 TnyAccountStoreIface *klass;
681 g_return_if_fail (g);
683 klass = (TnyAccountStoreIface *)g;
685 klass->get_accounts_func =
686 modest_tny_account_store_get_accounts;
687 klass->add_transport_account_func =
688 modest_tny_account_store_add_transport_account;
689 klass->add_store_account_func =
690 modest_tny_account_store_add_store_account;
691 klass->get_cache_dir_func =
692 modest_tny_account_store_get_cache_dir;
693 klass->get_device_func =
694 modest_tny_account_store_get_device;
696 modest_tny_account_store_alert;
700 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
701 ModestTnyGetPassFunc func)
703 g_warning (__FUNCTION__);
704 return; /* not implemented, we use signals */
709 tny_account_store_get_session (TnyAccountStore *self)
711 g_return_val_if_fail (self, NULL);
713 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;