1 /* modest-tny-account-store.c */
3 /* insert (c)/licensing information) */
7 #include <tny-account-iface.h>
8 #include <tny-account-store-iface.h>
9 #include <tny-store-account-iface.h>
10 #include <tny-transport-account-iface.h>
11 #include <tny-device-iface.h>
12 #include <tny-device.h>
13 #include <tny-account-store.h>
15 #include <tny-store-account.h>
16 #include <tny-transport-account.h>
18 #include "modest-account-mgr.h"
19 #include "modest-tny-account-store.h"
21 /* 'private'/'protected' functions */
22 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
23 static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
24 static void modest_tny_account_store_finalize (GObject *obj);
26 /* implementations for tny-account-store-iface */
27 static void modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data);
29 static void modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
30 TnyStoreAccountIface *account);
31 static void modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
32 TnyTransportAccountIface *account);
33 static const GList* modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface);
34 static const GList* modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface);
38 PASSWORD_REQUESTED_SIGNAL,
48 static const gchar *transport_protocols[] = { "smtp", NULL };
50 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
51 struct _ModestTnyAccountStorePrivate {
54 GMutex *transport_lock;
55 GList *store_accounts;
56 GList *transport_accounts;
59 TnySessionCamel *tny_session_camel;
60 TnyDeviceIface *device;
62 ModestAccountMgr *modest_acc_mgr;
65 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
66 MODEST_TYPE_TNY_ACCOUNT_STORE, \
67 ModestTnyAccountStorePrivate))
69 static GObjectClass *parent_class = NULL;
71 static guint signals[LAST_SIGNAL] = {0};
74 modest_tny_account_store_get_type (void)
76 static GType my_type = 0;
78 static const GTypeInfo my_info = {
79 sizeof(ModestTnyAccountStoreClass),
81 NULL, /* base finalize */
82 (GClassInitFunc) modest_tny_account_store_class_init,
83 NULL, /* class finalize */
84 NULL, /* class data */
85 sizeof(ModestTnyAccountStore),
87 (GInstanceInitFunc) modest_tny_account_store_init,
90 static const GInterfaceInfo iface_info = {
91 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
92 NULL, /* interface_finalize */
93 NULL /* interface_data */
96 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
97 "ModestTnyAccountStore", &my_info, 0);
99 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
106 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
108 GObjectClass *gobject_class;
109 gobject_class = (GObjectClass*) klass;
111 parent_class = g_type_class_peek_parent (klass);
112 gobject_class->finalize = modest_tny_account_store_finalize;
114 g_type_class_add_private (gobject_class,
115 sizeof(ModestTnyAccountStorePrivate));
117 signals[PASSWORD_REQUESTED_SIGNAL] =
118 g_signal_new ("password_requested",
119 G_TYPE_FROM_CLASS (gobject_class),
121 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
123 g_cclosure_marshal_VOID__POINTER,
124 G_TYPE_NONE, 1, G_TYPE_POINTER);
128 modest_tny_account_store_init (ModestTnyAccountStore *obj)
130 ModestTnyAccountStorePrivate *priv =
131 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
133 priv->modest_acc_mgr = NULL;
136 priv->store_lock = NULL;
137 priv->transport_lock = NULL;
138 priv->store_accounts = NULL;
139 priv->transport_accounts = NULL;
140 priv->cache_dir = NULL;
142 priv->tny_session_camel = NULL;
143 /* Meaning: if not indicated otherwise, we have valid password data */
144 priv->pw_invalid = PW_NOT_INVALID;
149 free_gobject (GObject *obj, gpointer user_data)
152 g_object_unref (obj);
157 free_gobject_list (GList *list)
160 g_list_foreach (list, (GFunc)free_gobject, NULL);
167 get_password (TnyAccountIface *account,
172 const TnyAccountStoreIface *account_store;
173 ModestTnyAccountStore *self;
174 ModestTnyAccountStorePrivate *priv;
177 g_return_val_if_fail (account, NULL);
179 key = tny_account_iface_get_id (account);
180 account_store = tny_account_iface_get_account_store(account);
182 self = MODEST_TNY_ACCOUNT_STORE (account_store);
183 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
185 if (priv->pw_invalid==PW_NOT_INVALID) {
186 retval = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr,
188 MODEST_ACCOUNT_PASSWORD,
191 g_signal_emit(G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key);
192 priv->pw_invalid=PW_NOT_INVALID;
200 forget_password (TnyAccountIface *account) {
202 ModestTnyAccountStore *self;
203 ModestTnyAccountStorePrivate *priv;
204 const TnyAccountStoreIface *account_store;
206 account_store = tny_account_iface_get_account_store(account);
207 self = MODEST_TNY_ACCOUNT_STORE (account_store);
208 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
210 priv->pw_invalid=PW_INVALID;
214 static TnyAccountIface*
215 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
218 TnyAccountIface *tny_account;
219 ModestTnyAccountStorePrivate *priv;
222 g_return_val_if_fail (self, NULL);
223 g_return_val_if_fail (key, NULL);
225 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
227 /* is it a store or a transport? */
229 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
231 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
234 g_warning ("failed to create new tny %s account",
235 is_store ? "store" : "transport");
239 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
240 TNY_ACCOUNT_STORE_IFACE(self));
242 tny_account_iface_set_id (tny_account, key);
243 tny_account_iface_set_name (tny_account, key);
246 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
247 MODEST_ACCOUNT_PROTO, NULL);
249 tny_account_iface_set_proto (tny_account, val);
252 g_warning ("protocol not defined for %s", key);
253 g_object_unref (G_OBJECT(tny_account));
258 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
259 MODEST_ACCOUNT_HOSTNAME,
262 tny_account_iface_set_hostname (tny_account, val);
268 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
269 MODEST_ACCOUNT_USERNAME,
272 tny_account_iface_set_user (tny_account, val);
276 tny_account_iface_set_pass_func (tny_account, get_password);
277 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
284 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
287 GSList *cursor = accounts;
288 GList *tny_accounts = NULL;
290 g_return_val_if_fail (self, NULL);
293 TnyAccountIface *tny_account;
294 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
297 g_warning ("could not create tnyaccount for %s",
298 (gchar*)cursor->data);
301 g_list_append (tny_accounts, tny_account);
303 cursor = cursor->next;
311 manager_new_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
313 ModestTnyAccountStore *self = data;
314 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
315 TnyAccountIface *new_account;
318 gboolean is_store = TRUE;
320 g_print ("new account signal %s\n", name);
322 proto = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, name,
323 MODEST_ACCOUNT_PROTO, NULL);
325 g_printerr ("Cannot add account: unknown type.");
329 while (transport_protocols [i]) {
330 if (!strcmp (transport_protocols [i], proto)) {
337 /* fill account lists */
338 if (!priv->store_accounts)
339 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
340 if (!priv->transport_accounts)
341 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
345 new_account = tny_account_from_key (self, name, is_store);
346 g_mutex_lock (priv->store_lock);
347 priv->store_accounts = g_list_append (priv->store_accounts, new_account);
348 g_mutex_unlock (priv->store_lock);
350 new_account = tny_account_from_key (self, name, is_store);
351 g_mutex_lock (priv->transport_lock);
352 priv->transport_accounts = g_list_append (priv->transport_accounts, new_account);
353 g_mutex_unlock (priv->transport_lock);
356 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNT_INSERTED], 0, new_account);
361 manager_remove_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
363 g_print ("remove account signal %s\n", name);
368 manager_change_account (ModestAccountMgr *modest_acc_mgr, gchar *accountname,
369 gchar *key, gchar* value, gpointer data)
371 ModestTnyAccountStore *self = data;
372 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
374 TnyAccountIface *account = NULL;
376 g_print ("account change signal: account: %s key: %s value: %s\n", accountname, key, value);
378 /* fill account lists */
379 if (!priv->store_accounts)
380 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
381 if (!priv->transport_accounts)
382 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
384 for (iter = priv->store_accounts; iter; iter = iter->next) {
385 TnyAccountIface *acc = iter->data;
386 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
393 for (iter = priv->transport_accounts; iter; iter = iter->next) {
394 TnyAccountIface *acc = iter->data;
395 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
402 g_printerr ("Couldn't find account - returning without applying changes.");
406 g_mutex_lock (priv->store_lock);
407 g_mutex_lock (priv->transport_lock);
409 if (!strcmp (key, MODEST_ACCOUNT_HOSTNAME))
410 tny_account_iface_set_hostname (account, value);
411 if (!strcmp (key, MODEST_ACCOUNT_USERNAME))
412 tny_account_iface_set_user (account, value);
414 g_mutex_unlock (priv->transport_lock);
415 g_mutex_unlock (priv->store_lock);
417 /* TODO: handle protocol and password changes */
419 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNTS_RELOADED], 0);
424 modest_tny_account_store_finalize (GObject *obj)
426 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
427 ModestTnyAccountStorePrivate *priv =
428 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
430 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
431 G_CALLBACK(manager_new_account), NULL);
432 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
433 G_CALLBACK(manager_remove_account), NULL);
434 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
435 G_CALLBACK(manager_change_account), NULL);
437 if (priv->modest_acc_mgr) {
438 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
439 priv->modest_acc_mgr = NULL;
442 if (priv->tny_session_camel) {
443 g_object_unref (G_OBJECT(priv->tny_session_camel));
444 priv->tny_session_camel = NULL;
448 g_object_unref (G_OBJECT(priv->device));
452 g_mutex_lock (priv->store_lock);
453 priv->store_accounts = free_gobject_list (priv->store_accounts);
454 g_mutex_unlock (priv->store_lock);
456 g_mutex_lock (priv->transport_lock);
457 priv->transport_accounts = free_gobject_list (priv->store_accounts);
458 g_mutex_unlock (priv->transport_lock);
461 g_mutex_free (priv->store_lock);
462 g_mutex_free (priv->transport_lock);
464 g_free (priv->cache_dir);
465 priv->cache_dir = NULL;
471 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr) {
474 ModestTnyAccountStorePrivate *priv;
476 g_return_val_if_fail (modest_acc_mgr, NULL);
478 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
480 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
482 g_object_ref(G_OBJECT(modest_acc_mgr));
483 priv->modest_acc_mgr = modest_acc_mgr;
485 priv->store_lock = g_mutex_new ();
486 priv->transport_lock = g_mutex_new ();
488 priv->device = (TnyDeviceIface*)tny_device_new();
490 g_warning ("Cannot create Device instance");
491 g_object_unref (obj);
494 priv->tny_session_camel = tny_session_camel_new
495 (TNY_ACCOUNT_STORE_IFACE(obj));
496 if (!priv->tny_session_camel) {
497 g_warning ("Cannot create TnySessionCamel instance");
498 g_object_unref (obj);
502 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-add",
503 G_CALLBACK(manager_new_account), obj);
504 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-remove",
505 G_CALLBACK(manager_remove_account), obj);
506 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-change",
507 G_CALLBACK(manager_change_account), obj);
514 add_account (TnyAccountStoreIface *self, TnyAccountIface *account) {
516 TnyAccountIface *account_iface;
517 ModestTnyAccountStore *account_store;
518 ModestTnyAccountStorePrivate *priv;
520 const gchar *account_name;
521 const gchar *hostname, *username, *proto;
523 g_return_val_if_fail (self, FALSE);
524 g_return_val_if_fail (account, FALSE);
526 account_iface = TNY_ACCOUNT_IFACE(account);
527 account_store = MODEST_TNY_ACCOUNT_STORE(self);
528 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
530 account_name = tny_account_iface_get_id(account_iface);
532 g_warning ("failed to retrieve account name");
536 hostname = tny_account_iface_get_hostname(account_iface);
537 username = tny_account_iface_get_user(account_iface);
538 proto = tny_account_iface_get_proto(account_iface);
540 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
542 hostname, username, NULL,
548 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
549 TnyStoreAccountIface *account)
551 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
552 g_warning ("failed to add store account");
557 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
558 TnyTransportAccountIface *account)
560 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
561 g_warning ("failed to add transport account");
566 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
568 ModestTnyAccountStore *self;
569 ModestTnyAccountStorePrivate *priv;
572 g_return_val_if_fail (iface, NULL);
574 self = MODEST_TNY_ACCOUNT_STORE(iface);
575 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
577 if (!priv->store_accounts) {
579 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
581 MODEST_PROTO_TYPE_STORE,
584 g_mutex_lock (priv->store_lock);
585 priv->store_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
586 g_mutex_unlock (priv->store_lock);
587 g_slist_free (accounts);
590 return priv->store_accounts;
595 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
597 ModestTnyAccountStore *self;
598 ModestTnyAccountStorePrivate *priv;
601 g_return_val_if_fail (iface, NULL);
603 self = MODEST_TNY_ACCOUNT_STORE(iface);
604 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
607 if (!priv->transport_accounts) {
609 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
611 MODEST_PROTO_TYPE_TRANSPORT,
613 g_mutex_lock (priv->transport_lock);
614 priv->transport_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
615 g_mutex_unlock (priv->transport_lock);
616 g_slist_free (accounts);
620 return priv->transport_accounts;
625 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
627 ModestTnyAccountStorePrivate *priv;
628 g_return_val_if_fail (self, NULL);
630 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
632 return priv->modest_acc_mgr;
637 tny_account_store_get_session (TnyAccountStore *self)
639 ModestTnyAccountStorePrivate *priv;
640 g_return_val_if_fail (self, NULL);
642 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
644 return priv->tny_session_camel;
649 * modest_tny_account_store_get_cache_dir:
650 * @self: self a TnyAccountStoreIface instance
652 * returns the pathname of the cache directory
654 * Returns: a string with the value of the pathname
655 * to the cache directory or NULL if the environment variable $HOME is
656 * not set. string should _not_ be freed by caller
659 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
661 ModestTnyAccountStorePrivate *priv;
662 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
664 if (!priv->cache_dir) {
665 if (g_getenv("HOME") != NULL)
666 priv->cache_dir = g_strconcat(g_getenv("HOME"),
667 "/.modest/cache/", NULL);
669 return priv->cache_dir;
673 static const TnyDeviceIface*
674 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
676 ModestTnyAccountStorePrivate *priv;
678 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
685 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
687 TnyAccountStoreIfaceClass *klass;
689 g_return_if_fail (g_iface);
691 klass = (TnyAccountStoreIfaceClass *)g_iface;
693 klass->add_store_account_func =
694 modest_tny_account_store_add_store_account;
695 klass->get_store_accounts_func =
696 modest_tny_account_store_get_store_accounts;
697 klass->add_transport_account_func =
698 modest_tny_account_store_add_transport_account;
699 klass->get_transport_accounts_func =
700 modest_tny_account_store_get_transport_accounts;
701 klass->get_cache_dir_func =
702 modest_tny_account_store_get_cache_dir;
703 klass->get_device_func =
704 modest_tny_account_store_get_device;