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-iface.h>
33 #include <tny-account-store-iface.h>
34 #include <tny-store-account-iface.h>
35 #include <tny-transport-account-iface.h>
36 #include <tny-device-iface.h>
37 #include <tny-device.h>
38 #include <tny-account-store.h>
40 #include <tny-store-account.h>
41 #include <tny-transport-account.h>
43 #include "modest-account-mgr.h"
44 #include "modest-tny-account-store.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_iface_init (gpointer g_iface, gpointer iface_data);
53 static void modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
54 TnyStoreAccountIface *account);
55 static void modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
56 TnyTransportAccountIface *account);
57 static void modest_tny_account_store_get_accounts (TnyAccountStoreIface *iface, TnyListIface *list,
58 TnyGetAccountsRequestType type);
62 PASSWORD_REQUESTED_SIGNAL,
63 UPDATE_ACCOUNTS_SIGNAL,
73 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
74 struct _ModestTnyAccountStorePrivate {
79 TnySessionCamel *tny_session_camel;
80 TnyDeviceIface *device;
82 ModestAccountMgr *modest_acc_mgr;
84 ModestTnyGetPassFunc get_pass_func;
86 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
87 MODEST_TYPE_TNY_ACCOUNT_STORE, \
88 ModestTnyAccountStorePrivate))
90 static GObjectClass *parent_class = NULL;
92 static guint signals[LAST_SIGNAL] = {0};
95 modest_tny_account_store_get_type (void)
97 static GType my_type = 0;
99 static const GTypeInfo my_info = {
100 sizeof(ModestTnyAccountStoreClass),
101 NULL, /* base init */
102 NULL, /* base finalize */
103 (GClassInitFunc) modest_tny_account_store_class_init,
104 NULL, /* class finalize */
105 NULL, /* class data */
106 sizeof(ModestTnyAccountStore),
108 (GInstanceInitFunc) modest_tny_account_store_init,
111 static const GInterfaceInfo iface_info = {
112 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
113 NULL, /* interface_finalize */
114 NULL /* interface_data */
117 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
118 "ModestTnyAccountStore", &my_info, 0);
120 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
127 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
129 GObjectClass *gobject_class;
130 gobject_class = (GObjectClass*) klass;
132 parent_class = g_type_class_peek_parent (klass);
133 gobject_class->finalize = modest_tny_account_store_finalize;
135 g_type_class_add_private (gobject_class,
136 sizeof(ModestTnyAccountStorePrivate));
138 signals[PASSWORD_REQUESTED_SIGNAL] =
139 g_signal_new ("password_requested",
140 G_TYPE_FROM_CLASS (gobject_class),
142 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
144 g_cclosure_marshal_VOID__STRING,
145 G_TYPE_NONE, 1, G_TYPE_STRING);
147 signals[UPDATE_ACCOUNTS_SIGNAL] =
148 g_signal_new ("update_accounts",
149 G_TYPE_FROM_CLASS (gobject_class),
151 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, update_accounts),
153 g_cclosure_marshal_VOID__STRING,
154 G_TYPE_NONE, 1, G_TYPE_STRING);
159 modest_tny_account_store_init (ModestTnyAccountStore *obj)
161 ModestTnyAccountStorePrivate *priv =
162 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
164 priv->modest_acc_mgr = NULL;
166 priv->cache_dir = NULL;
168 priv->tny_session_camel = NULL;
169 /* Meaning: if not indicated otherwise, we have valid password data */
170 priv->pw_invalid = PW_NOT_INVALID;
171 priv->get_pass_func = NULL;
176 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
179 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
181 g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
188 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
189 const gchar *key, gpointer user_data)
191 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
193 g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
200 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
204 const TnyAccountStoreIface *account_store;
205 ModestTnyAccountStore *self;
206 ModestTnyAccountStorePrivate *priv;
209 g_return_val_if_fail (account, NULL);
211 key = tny_account_iface_get_id (account);
212 account_store = tny_account_iface_get_account_store(account);
214 self = MODEST_TNY_ACCOUNT_STORE (account_store);
215 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
217 if (priv->pw_invalid==PW_NOT_INVALID) {
218 retval = modest_account_mgr_get_string (priv->modest_acc_mgr,
220 MODEST_ACCOUNT_PASSWORD,
224 retval = priv->get_pass_func(account, prompt, cancel);
226 priv->pw_invalid=PW_NOT_INVALID;
227 modest_account_mgr_set_string(priv->modest_acc_mgr,
229 MODEST_ACCOUNT_PASSWORD,
239 forget_password (TnyAccountIface *account) {
241 ModestTnyAccountStore *self;
242 ModestTnyAccountStorePrivate *priv;
243 const TnyAccountStoreIface *account_store;
245 account_store = tny_account_iface_get_account_store(account);
246 self = MODEST_TNY_ACCOUNT_STORE (account_store);
247 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
249 priv->pw_invalid=PW_INVALID;
254 static TnyAccountIface*
255 tny_account_from_name (ModestTnyAccountStore *self, const gchar *key, ModestProtoType modest_type)
257 TnyAccountIface *tny_account;
258 ModestTnyAccountStorePrivate *priv;
261 g_return_val_if_fail (self, NULL);
262 g_return_val_if_fail (key, NULL);
264 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
266 /* is it a store or a transport? */
267 if (modest_type == MODEST_PROTO_TYPE_STORE)
268 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
269 else if (modest_type == MODEST_PROTO_TYPE_TRANSPORT)
270 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
272 g_assert_not_reached ();
275 g_printerr ("modest: failed to create new tny account for '%s'\n",
280 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
281 TNY_ACCOUNT_STORE_IFACE(self));
283 tny_account_iface_set_id (tny_account, key);
284 tny_account_iface_set_name (tny_account, key);
287 val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
288 MODEST_ACCOUNT_PROTO, TRUE, NULL);
290 tny_account_iface_set_proto (tny_account, val);
293 g_printerr ("modest: protocol not defined for '%s'\n", key);
294 g_object_unref (G_OBJECT(tny_account));
299 val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
300 MODEST_ACCOUNT_HOSTNAME, TRUE,
303 tny_account_iface_set_hostname (tny_account, val);
309 val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
310 MODEST_ACCOUNT_USERNAME, TRUE,
313 tny_account_iface_set_user (tny_account, val);
317 tny_account_iface_set_pass_func (tny_account, get_password);
318 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
326 modest_tny_account_store_finalize (GObject *obj)
328 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
329 ModestTnyAccountStorePrivate *priv =
330 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
332 if (priv->modest_acc_mgr) {
333 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
334 priv->modest_acc_mgr = NULL;
337 if (priv->tny_session_camel) {
338 g_object_unref (G_OBJECT(priv->tny_session_camel));
339 priv->tny_session_camel = NULL;
343 g_object_unref (G_OBJECT(priv->device));
347 g_mutex_free (priv->store_lock);
349 g_free (priv->cache_dir);
350 priv->cache_dir = NULL;
354 ModestTnyAccountStore*
355 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr) {
358 ModestTnyAccountStorePrivate *priv;
360 g_return_val_if_fail (modest_acc_mgr, NULL);
362 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
364 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
366 g_object_ref(G_OBJECT(modest_acc_mgr));
367 priv->modest_acc_mgr = modest_acc_mgr;
369 g_signal_connect (G_OBJECT(modest_acc_mgr), "account_changed",
370 G_CALLBACK (on_account_changed), obj);
371 g_signal_connect (G_OBJECT(modest_acc_mgr), "account_removed",
372 G_CALLBACK (on_account_removed), obj);
374 priv->store_lock = g_mutex_new ();
376 priv->device = (TnyDeviceIface*)tny_device_new();
378 g_printerr ("modest: cannot create device instance\n");
379 g_object_unref (obj);
383 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE_IFACE(obj));
385 if (!priv->tny_session_camel) {
386 g_printerr ("modest: cannot create TnySessionCamel instance\n");
387 g_object_unref (obj);
391 return MODEST_TNY_ACCOUNT_STORE(obj);
396 add_account (TnyAccountStoreIface *self, TnyAccountIface *account) {
398 TnyAccountIface *account_iface;
399 ModestTnyAccountStore *account_store;
400 ModestTnyAccountStorePrivate *priv;
402 const gchar *account_name;
403 const gchar *hostname, *username, *proto;
405 g_return_val_if_fail (self, FALSE);
406 g_return_val_if_fail (account, FALSE);
408 account_iface = TNY_ACCOUNT_IFACE(account);
409 account_store = MODEST_TNY_ACCOUNT_STORE(self);
410 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
412 account_name = tny_account_iface_get_id(account_iface);
414 g_printerr ("modest: failed to retrieve account name\n");
418 hostname = tny_account_iface_get_hostname(account_iface);
419 username = tny_account_iface_get_user(account_iface);
420 proto = tny_account_iface_get_proto(account_iface);
422 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
424 hostname, username, NULL,
430 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
431 TnyStoreAccountIface *account)
433 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
434 g_printerr ("modest: failed to add store account\n");
439 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
440 TnyTransportAccountIface *account)
442 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
443 g_printerr ("modest: failed to add transport account\n");
448 modest_tny_account_store_get_accounts (TnyAccountStoreIface *iface,
450 TnyGetAccountsRequestType type)
452 ModestTnyAccountStore *self;
453 ModestTnyAccountStorePrivate *priv;
454 GSList *accounts, *cursor;
455 ModestProtoType modest_type;
457 g_return_if_fail (iface);
459 self = MODEST_TNY_ACCOUNT_STORE(iface);
460 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
463 case TNY_ACCOUNT_STORE_IFACE_TRANSPORT_ACCOUNTS:
464 modest_type = MODEST_PROTO_TYPE_TRANSPORT;
466 case TNY_ACCOUNT_STORE_IFACE_STORE_ACCOUNTS:
467 modest_type = MODEST_PROTO_TYPE_STORE;
469 case TNY_ACCOUNT_STORE_IFACE_BOTH:
470 modest_type = MODEST_PROTO_TYPE_ANY;
473 g_assert_not_reached ();
476 accounts = modest_account_mgr_search_server_accounts (priv->modest_acc_mgr,
482 TnyAccountIface *account_iface;
484 account_name = (gchar*)cursor->data;
485 account_iface = tny_account_from_name (self, account_name, modest_type);
488 g_printerr ("modest: failed to create account iface for '%s'\n",
491 tny_list_iface_prepend (list, account_iface);
493 g_free (account_name);
494 cursor = cursor->next;
497 g_slist_free (accounts);
499 tny_session_camel_set_current_accounts (priv->tny_session_camel,
505 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
507 ModestTnyAccountStorePrivate *priv;
508 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
510 if (!priv->cache_dir)
511 priv->cache_dir = g_build_filename (g_get_home_dir(),
512 ".modest", "cache", NULL);
513 return priv->cache_dir;
517 static const TnyDeviceIface*
518 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
520 ModestTnyAccountStorePrivate *priv;
522 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
529 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
531 TnyAccountStoreIfaceClass *klass;
533 g_return_if_fail (g_iface);
535 klass = (TnyAccountStoreIfaceClass *)g_iface;
537 klass->get_accounts_func =
538 modest_tny_account_store_get_accounts;
539 klass->add_transport_account_func =
540 modest_tny_account_store_add_transport_account;
541 klass->get_cache_dir_func =
542 modest_tny_account_store_get_cache_dir;
543 klass->get_device_func =
544 modest_tny_account_store_get_device;
548 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
549 ModestTnyGetPassFunc func)
551 ModestTnyAccountStorePrivate *priv;
553 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
555 priv->get_pass_func=func;
560 tny_account_store_get_session (TnyAccountStore *self)
562 ModestTnyAccountStorePrivate *priv;
564 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
566 return priv->tny_session_camel;