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;
64 ModestTnyGetPassFunc get_pass_func;
66 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
67 MODEST_TYPE_TNY_ACCOUNT_STORE, \
68 ModestTnyAccountStorePrivate))
70 static GObjectClass *parent_class = NULL;
72 static guint signals[LAST_SIGNAL] = {0};
75 modest_tny_account_store_get_type (void)
77 static GType my_type = 0;
79 static const GTypeInfo my_info = {
80 sizeof(ModestTnyAccountStoreClass),
82 NULL, /* base finalize */
83 (GClassInitFunc) modest_tny_account_store_class_init,
84 NULL, /* class finalize */
85 NULL, /* class data */
86 sizeof(ModestTnyAccountStore),
88 (GInstanceInitFunc) modest_tny_account_store_init,
91 static const GInterfaceInfo iface_info = {
92 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
93 NULL, /* interface_finalize */
94 NULL /* interface_data */
97 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
98 "ModestTnyAccountStore", &my_info, 0);
100 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
107 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
109 GObjectClass *gobject_class;
110 gobject_class = (GObjectClass*) klass;
112 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->finalize = modest_tny_account_store_finalize;
115 g_type_class_add_private (gobject_class,
116 sizeof(ModestTnyAccountStorePrivate));
118 signals[PASSWORD_REQUESTED_SIGNAL] =
119 g_signal_new ("password_requested",
120 G_TYPE_FROM_CLASS (gobject_class),
122 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
124 g_cclosure_marshal_VOID__POINTER,
125 G_TYPE_NONE, 1, G_TYPE_POINTER);
129 modest_tny_account_store_init (ModestTnyAccountStore *obj)
131 ModestTnyAccountStorePrivate *priv =
132 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
134 priv->modest_acc_mgr = NULL;
137 priv->store_lock = NULL;
138 priv->transport_lock = NULL;
139 priv->store_accounts = NULL;
140 priv->transport_accounts = NULL;
141 priv->cache_dir = NULL;
143 priv->tny_session_camel = NULL;
144 /* Meaning: if not indicated otherwise, we have valid password data */
145 priv->pw_invalid = PW_NOT_INVALID;
146 priv->get_pass_func = NULL;
151 free_gobject (GObject *obj, gpointer user_data)
154 g_object_unref (obj);
159 free_gobject_list (GList *list)
162 g_list_foreach (list, (GFunc)free_gobject, NULL);
169 get_password (TnyAccountIface *account,
174 const TnyAccountStoreIface *account_store;
175 ModestTnyAccountStore *self;
176 ModestTnyAccountStorePrivate *priv;
179 g_return_val_if_fail (account, NULL);
181 key = tny_account_iface_get_id (account);
182 account_store = tny_account_iface_get_account_store(account);
184 self = MODEST_TNY_ACCOUNT_STORE (account_store);
185 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
187 if (priv->pw_invalid==PW_NOT_INVALID) {
188 retval = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr,
190 MODEST_ACCOUNT_PASSWORD,
193 retval = priv->get_pass_func(account, prompt, cancel);
196 priv->pw_invalid=PW_NOT_INVALID;
197 modest_account_mgr_set_server_account_string(priv->modest_acc_mgr,
199 MODEST_ACCOUNT_PASSWORD,
209 forget_password (TnyAccountIface *account) {
211 ModestTnyAccountStore *self;
212 ModestTnyAccountStorePrivate *priv;
213 const TnyAccountStoreIface *account_store;
215 account_store = tny_account_iface_get_account_store(account);
216 self = MODEST_TNY_ACCOUNT_STORE (account_store);
217 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
219 priv->pw_invalid=PW_INVALID;
223 static TnyAccountIface*
224 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
227 TnyAccountIface *tny_account;
228 ModestTnyAccountStorePrivate *priv;
231 g_return_val_if_fail (self, NULL);
232 g_return_val_if_fail (key, NULL);
234 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
236 /* is it a store or a transport? */
238 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
240 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
243 g_warning ("failed to create new tny %s account",
244 is_store ? "store" : "transport");
248 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
249 TNY_ACCOUNT_STORE_IFACE(self));
251 tny_account_iface_set_id (tny_account, key);
252 tny_account_iface_set_name (tny_account, key);
255 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
256 MODEST_ACCOUNT_PROTO, NULL);
258 tny_account_iface_set_proto (tny_account, val);
261 g_warning ("protocol not defined for %s", key);
262 g_object_unref (G_OBJECT(tny_account));
267 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
268 MODEST_ACCOUNT_HOSTNAME,
271 tny_account_iface_set_hostname (tny_account, val);
277 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
278 MODEST_ACCOUNT_USERNAME,
281 tny_account_iface_set_user (tny_account, val);
285 tny_account_iface_set_pass_func (tny_account, get_password);
286 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
293 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
296 GSList *cursor = accounts;
297 GList *tny_accounts = NULL;
299 g_return_val_if_fail (self, NULL);
302 TnyAccountIface *tny_account;
303 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
306 g_warning ("could not create tnyaccount for %s",
307 (gchar*)cursor->data);
310 g_list_append (tny_accounts, tny_account);
312 cursor = cursor->next;
320 manager_new_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
322 ModestTnyAccountStore *self = data;
323 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
324 TnyAccountIface *new_account;
327 gboolean is_store = TRUE;
329 g_print ("new account signal %s\n", name);
331 proto = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, name,
332 MODEST_ACCOUNT_PROTO, NULL);
334 g_printerr ("Cannot add account: unknown type.");
338 while (transport_protocols [i]) {
339 if (!strcmp (transport_protocols [i], proto)) {
346 /* fill account lists */
347 if (!priv->store_accounts)
348 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
349 if (!priv->transport_accounts)
350 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
354 new_account = tny_account_from_key (self, name, is_store);
355 g_mutex_lock (priv->store_lock);
356 priv->store_accounts = g_list_append (priv->store_accounts, new_account);
357 g_mutex_unlock (priv->store_lock);
359 new_account = tny_account_from_key (self, name, is_store);
360 g_mutex_lock (priv->transport_lock);
361 priv->transport_accounts = g_list_append (priv->transport_accounts, new_account);
362 g_mutex_unlock (priv->transport_lock);
365 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNT_INSERTED], 0, new_account);
370 manager_remove_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
372 g_print ("remove account signal %s\n", name);
377 manager_change_account (ModestAccountMgr *modest_acc_mgr, gchar *accountname,
378 gchar *key, gchar* value, gpointer data)
380 ModestTnyAccountStore *self = data;
381 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
383 TnyAccountIface *account = NULL;
385 g_print ("account change signal: account: %s key: %s value: %s\n", accountname, key, value);
387 /* fill account lists */
388 if (!priv->store_accounts)
389 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
390 if (!priv->transport_accounts)
391 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
393 for (iter = priv->store_accounts; iter; iter = iter->next) {
394 TnyAccountIface *acc = iter->data;
395 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
402 for (iter = priv->transport_accounts; iter; iter = iter->next) {
403 TnyAccountIface *acc = iter->data;
404 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
411 g_printerr ("Couldn't find account - returning without applying changes.");
415 g_mutex_lock (priv->store_lock);
416 g_mutex_lock (priv->transport_lock);
418 if (!strcmp (key, MODEST_ACCOUNT_HOSTNAME))
419 tny_account_iface_set_hostname (account, value);
420 if (!strcmp (key, MODEST_ACCOUNT_USERNAME))
421 tny_account_iface_set_user (account, value);
423 g_mutex_unlock (priv->transport_lock);
424 g_mutex_unlock (priv->store_lock);
426 /* TODO: handle protocol and password changes */
428 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNTS_RELOADED], 0);
433 modest_tny_account_store_finalize (GObject *obj)
435 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
436 ModestTnyAccountStorePrivate *priv =
437 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
439 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
440 G_CALLBACK(manager_new_account), NULL);
441 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
442 G_CALLBACK(manager_remove_account), NULL);
443 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
444 G_CALLBACK(manager_change_account), NULL);
446 if (priv->modest_acc_mgr) {
447 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
448 priv->modest_acc_mgr = NULL;
451 if (priv->tny_session_camel) {
452 g_object_unref (G_OBJECT(priv->tny_session_camel));
453 priv->tny_session_camel = NULL;
457 g_object_unref (G_OBJECT(priv->device));
461 g_mutex_lock (priv->store_lock);
462 priv->store_accounts = free_gobject_list (priv->store_accounts);
463 g_mutex_unlock (priv->store_lock);
465 g_mutex_lock (priv->transport_lock);
466 priv->transport_accounts = free_gobject_list (priv->store_accounts);
467 g_mutex_unlock (priv->transport_lock);
470 g_mutex_free (priv->store_lock);
471 g_mutex_free (priv->transport_lock);
473 g_free (priv->cache_dir);
474 priv->cache_dir = NULL;
480 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr) {
483 ModestTnyAccountStorePrivate *priv;
485 g_return_val_if_fail (modest_acc_mgr, NULL);
487 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
489 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
491 g_object_ref(G_OBJECT(modest_acc_mgr));
492 priv->modest_acc_mgr = modest_acc_mgr;
494 priv->store_lock = g_mutex_new ();
495 priv->transport_lock = g_mutex_new ();
497 priv->device = (TnyDeviceIface*)tny_device_new();
499 g_warning ("Cannot create Device instance");
500 g_object_unref (obj);
503 priv->tny_session_camel = tny_session_camel_new
504 (TNY_ACCOUNT_STORE_IFACE(obj));
505 if (!priv->tny_session_camel) {
506 g_warning ("Cannot create TnySessionCamel instance");
507 g_object_unref (obj);
511 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-add",
512 G_CALLBACK(manager_new_account), obj);
513 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-remove",
514 G_CALLBACK(manager_remove_account), obj);
515 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-change",
516 G_CALLBACK(manager_change_account), obj);
523 add_account (TnyAccountStoreIface *self, TnyAccountIface *account) {
525 TnyAccountIface *account_iface;
526 ModestTnyAccountStore *account_store;
527 ModestTnyAccountStorePrivate *priv;
529 const gchar *account_name;
530 const gchar *hostname, *username, *proto;
532 g_return_val_if_fail (self, FALSE);
533 g_return_val_if_fail (account, FALSE);
535 account_iface = TNY_ACCOUNT_IFACE(account);
536 account_store = MODEST_TNY_ACCOUNT_STORE(self);
537 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
539 account_name = tny_account_iface_get_id(account_iface);
541 g_warning ("failed to retrieve account name");
545 hostname = tny_account_iface_get_hostname(account_iface);
546 username = tny_account_iface_get_user(account_iface);
547 proto = tny_account_iface_get_proto(account_iface);
549 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
551 hostname, username, NULL,
557 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
558 TnyStoreAccountIface *account)
560 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
561 g_warning ("failed to add store account");
566 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
567 TnyTransportAccountIface *account)
569 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
570 g_warning ("failed to add transport account");
575 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
577 ModestTnyAccountStore *self;
578 ModestTnyAccountStorePrivate *priv;
581 g_return_val_if_fail (iface, NULL);
583 self = MODEST_TNY_ACCOUNT_STORE(iface);
584 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
586 if (!priv->store_accounts) {
588 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
590 MODEST_PROTO_TYPE_STORE,
593 g_mutex_lock (priv->store_lock);
594 priv->store_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
595 g_mutex_unlock (priv->store_lock);
596 g_slist_free (accounts);
599 return priv->store_accounts;
604 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
606 ModestTnyAccountStore *self;
607 ModestTnyAccountStorePrivate *priv;
610 g_return_val_if_fail (iface, NULL);
612 self = MODEST_TNY_ACCOUNT_STORE(iface);
613 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
616 if (!priv->transport_accounts) {
618 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
620 MODEST_PROTO_TYPE_TRANSPORT,
622 g_mutex_lock (priv->transport_lock);
623 priv->transport_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
624 g_mutex_unlock (priv->transport_lock);
625 g_slist_free (accounts);
629 return priv->transport_accounts;
634 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
636 ModestTnyAccountStorePrivate *priv;
637 g_return_val_if_fail (self, NULL);
639 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
641 return priv->modest_acc_mgr;
646 tny_account_store_get_session (TnyAccountStore *self)
648 ModestTnyAccountStorePrivate *priv;
649 g_return_val_if_fail (self, NULL);
651 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
653 return priv->tny_session_camel;
658 * modest_tny_account_store_get_cache_dir:
659 * @self: self a TnyAccountStoreIface instance
661 * returns the pathname of the cache directory
663 * Returns: a string with the value of the pathname
664 * to the cache directory or NULL if the environment variable $HOME is
665 * not set. string should _not_ be freed by caller
668 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
670 ModestTnyAccountStorePrivate *priv;
671 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
673 if (!priv->cache_dir) {
674 if (g_getenv("HOME") != NULL)
675 priv->cache_dir = g_strconcat(g_getenv("HOME"),
676 "/.modest/cache/", NULL);
678 return priv->cache_dir;
682 static const TnyDeviceIface*
683 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
685 ModestTnyAccountStorePrivate *priv;
687 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
694 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
696 TnyAccountStoreIfaceClass *klass;
698 g_return_if_fail (g_iface);
700 klass = (TnyAccountStoreIfaceClass *)g_iface;
702 klass->add_store_account_func =
703 modest_tny_account_store_add_store_account;
704 klass->get_store_accounts_func =
705 modest_tny_account_store_get_store_accounts;
706 klass->add_transport_account_func =
707 modest_tny_account_store_add_transport_account;
708 klass->get_transport_accounts_func =
709 modest_tny_account_store_get_transport_accounts;
710 klass->get_cache_dir_func =
711 modest_tny_account_store_get_cache_dir;
712 klass->get_device_func =
713 modest_tny_account_store_get_device;
717 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self, ModestTnyGetPassFunc func) {
719 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
721 priv->get_pass_func=func;