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,
42 static const gchar *transport_protocols[] = { "smtp", NULL };
44 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
45 struct _ModestTnyAccountStorePrivate {
48 GMutex *transport_lock;
49 GList *store_accounts;
50 GList *transport_accounts;
53 TnySessionCamel *tny_session_camel;
54 TnyDeviceIface *device;
56 ModestAccountMgr *modest_acc_mgr;
58 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59 MODEST_TYPE_TNY_ACCOUNT_STORE, \
60 ModestTnyAccountStorePrivate))
62 static GObjectClass *parent_class = NULL;
64 static guint signals[LAST_SIGNAL] = {0};
67 modest_tny_account_store_get_type (void)
69 static GType my_type = 0;
71 static const GTypeInfo my_info = {
72 sizeof(ModestTnyAccountStoreClass),
74 NULL, /* base finalize */
75 (GClassInitFunc) modest_tny_account_store_class_init,
76 NULL, /* class finalize */
77 NULL, /* class data */
78 sizeof(ModestTnyAccountStore),
80 (GInstanceInitFunc) modest_tny_account_store_init,
83 static const GInterfaceInfo iface_info = {
84 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
85 NULL, /* interface_finalize */
86 NULL /* interface_data */
89 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
90 "ModestTnyAccountStore", &my_info, 0);
92 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
99 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
101 GObjectClass *gobject_class;
102 gobject_class = (GObjectClass*) klass;
104 parent_class = g_type_class_peek_parent (klass);
105 gobject_class->finalize = modest_tny_account_store_finalize;
107 g_type_class_add_private (gobject_class,
108 sizeof(ModestTnyAccountStorePrivate));
110 signals[PASSWORD_REQUESTED_SIGNAL] =
111 g_signal_new ("password_requested",
112 G_TYPE_FROM_CLASS (gobject_class),
114 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
116 g_cclosure_marshal_VOID__POINTER,
117 G_TYPE_NONE, 1, G_TYPE_POINTER);
121 modest_tny_account_store_init (ModestTnyAccountStore *obj)
123 ModestTnyAccountStorePrivate *priv =
124 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
126 priv->modest_acc_mgr = NULL;
129 priv->store_lock = NULL;
130 priv->transport_lock = NULL;
131 priv->store_accounts = NULL;
132 priv->transport_accounts = NULL;
133 priv->cache_dir = NULL;
135 priv->tny_session_camel = NULL;
140 free_gobject (GObject *obj, gpointer user_data)
143 g_object_unref (obj);
148 free_gobject_list (GList *list)
151 g_list_foreach (list, (GFunc)free_gobject, NULL);
158 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
161 const TnyAccountStoreIface *account_store;
162 ModestTnyAccountStore *self;
163 ModestTnyAccountStorePrivate *priv;
166 g_return_val_if_fail (account, NULL);
168 key = tny_account_iface_get_id (account);
169 account_store = tny_account_iface_get_account_store(account);
171 self = MODEST_TNY_ACCOUNT_STORE (account_store);
172 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
174 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
175 MODEST_ACCOUNT_PASSWORD, NULL);
178 * append the prompt to the emitted signal,
179 * so the password dialog shows the prompt supplied by the caller of this function.
181 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key);
189 forget_password (TnyAccountIface *account)
191 g_warning (__FUNCTION__);
195 static TnyAccountIface*
196 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
199 TnyAccountIface *tny_account;
200 ModestTnyAccountStorePrivate *priv;
203 g_return_val_if_fail (self, NULL);
204 g_return_val_if_fail (key, NULL);
206 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
208 /* is it a store or a transport? */
210 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
212 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
215 g_warning ("failed to create new tny %s account",
216 is_store ? "store" : "transport");
220 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
221 TNY_ACCOUNT_STORE_IFACE(self));
223 tny_account_iface_set_id (tny_account, key);
224 tny_account_iface_set_name (tny_account, key);
227 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
228 MODEST_ACCOUNT_PROTO, NULL);
230 tny_account_iface_set_proto (tny_account, val);
233 g_warning ("protocol not defined for %s", key);
234 g_object_unref (G_OBJECT(tny_account));
239 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
240 MODEST_ACCOUNT_HOSTNAME,
243 tny_account_iface_set_hostname (tny_account, val);
249 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
250 MODEST_ACCOUNT_USERNAME,
253 tny_account_iface_set_user (tny_account, val);
257 tny_account_iface_set_pass_func (tny_account, get_password);
258 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
265 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
268 GSList *cursor = accounts;
269 GList *tny_accounts = NULL;
271 g_return_val_if_fail (self, NULL);
274 TnyAccountIface *tny_account;
275 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
278 g_warning ("could not create tnyaccount for %s",
279 (gchar*)cursor->data);
282 g_list_append (tny_accounts, tny_account);
284 cursor = cursor->next;
292 manager_new_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
294 ModestTnyAccountStore *self = data;
295 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
296 TnyAccountIface *new_account;
299 gboolean is_store = TRUE;
301 g_print ("new account signal %s\n", name);
304 proto = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, name,
305 MODEST_ACCOUNT_PROTO, NULL);
307 g_printerr ("Cannot add account: unknown type.");
311 while (transport_protocols [i]) {
312 if (!strcmp (transport_protocols [i], proto)) {
319 /* fill account lists */
320 if (!priv->store_accounts)
321 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
322 if (!priv->transport_accounts)
323 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
327 new_account = tny_account_from_key (self, name, is_store);
328 g_mutex_lock (priv->store_lock);
329 priv->store_accounts = g_list_append (priv->store_accounts, new_account);
330 g_mutex_unlock (priv->store_lock);
332 new_account = tny_account_from_key (self, name, is_store);
333 g_mutex_lock (priv->transport_lock);
334 priv->transport_accounts = g_list_append (priv->transport_accounts, new_account);
335 g_mutex_unlock (priv->transport_lock);
338 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNT_INSERTED], 0, new_account);
343 manager_remove_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
345 g_print ("remove account signal %s\n", name);
350 manager_change_account (ModestAccountMgr *modest_acc_mgr, gchar *accountname,
351 gchar *key, gchar* value, gpointer data)
353 ModestTnyAccountStore *self = data;
354 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
356 TnyAccountIface *account = NULL;
358 g_print ("account change signal: account: %s key: %s value: %s\n", accountname, key, value);
360 /* fill account lists */
361 if (!priv->store_accounts)
362 modest_tny_account_store_get_store_accounts (TNY_ACCOUNT_STORE_IFACE(self));
363 if (!priv->transport_accounts)
364 modest_tny_account_store_get_transport_accounts (TNY_ACCOUNT_STORE_IFACE(self));
366 for (iter = priv->store_accounts; iter; iter = iter->next) {
367 TnyAccountIface *acc = iter->data;
368 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
375 for (iter = priv->transport_accounts; iter; iter = iter->next) {
376 TnyAccountIface *acc = iter->data;
377 if (!strcmp (tny_account_iface_get_name (acc), accountname)) {
384 g_printerr ("Couldn't find account - returning without applying changes.");
388 g_mutex_lock (priv->store_lock);
389 g_mutex_lock (priv->transport_lock);
391 if (!strcmp (key, MODEST_ACCOUNT_HOSTNAME))
392 tny_account_iface_set_hostname (account, value);
393 if (!strcmp (key, MODEST_ACCOUNT_USERNAME))
394 tny_account_iface_set_user (account, value);
396 g_mutex_unlock (priv->transport_lock);
397 g_mutex_unlock (priv->store_lock);
399 /* TODO: handle protocol and password changes */
402 tny_account_store_iface_signals [TNY_ACCOUNT_STORE_IFACE_ACCOUNTS_RELOADED], 0);
408 modest_tny_account_store_finalize (GObject *obj)
410 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
411 ModestTnyAccountStorePrivate *priv =
412 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
414 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
415 G_CALLBACK(manager_new_account), NULL);
416 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
417 G_CALLBACK(manager_remove_account), NULL);
418 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
419 G_CALLBACK(manager_change_account), NULL);
421 if (priv->modest_acc_mgr) {
422 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
423 priv->modest_acc_mgr = NULL;
426 if (priv->tny_session_camel) {
427 g_object_unref (G_OBJECT(priv->tny_session_camel));
428 priv->tny_session_camel = NULL;
432 g_object_unref (G_OBJECT(priv->device));
436 g_mutex_lock (priv->store_lock);
437 priv->store_accounts = free_gobject_list (priv->store_accounts);
438 g_mutex_unlock (priv->store_lock);
440 g_mutex_lock (priv->transport_lock);
441 priv->transport_accounts = free_gobject_list (priv->store_accounts);
442 g_mutex_unlock (priv->transport_lock);
445 g_mutex_free (priv->store_lock);
446 g_mutex_free (priv->transport_lock);
448 g_free (priv->cache_dir);
449 priv->cache_dir = NULL;
455 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
458 ModestTnyAccountStorePrivate *priv;
460 g_return_val_if_fail (modest_acc_mgr, NULL);
462 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
464 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
466 g_object_ref(G_OBJECT(modest_acc_mgr));
467 priv->modest_acc_mgr = modest_acc_mgr;
469 priv->store_lock = g_mutex_new ();
470 priv->transport_lock = g_mutex_new ();
472 priv->device = (TnyDeviceIface*)tny_device_new();
474 g_warning ("Cannot create Device instance");
475 g_object_unref (obj);
478 priv->tny_session_camel = tny_session_camel_new
479 (TNY_ACCOUNT_STORE_IFACE(obj));
480 if (!priv->tny_session_camel) {
481 g_warning ("Cannot create TnySessionCamel instance");
482 g_object_unref (obj);
486 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-add",
487 G_CALLBACK(manager_new_account), obj);
488 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-remove",
489 G_CALLBACK(manager_remove_account), obj);
490 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-change",
491 G_CALLBACK(manager_change_account), obj);
498 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
500 TnyAccountIface *account_iface;
501 ModestTnyAccountStore *account_store;
502 ModestTnyAccountStorePrivate *priv;
504 const gchar *account_name;
505 const gchar *hostname, *username, *proto;
507 g_return_val_if_fail (self, FALSE);
508 g_return_val_if_fail (account, FALSE);
510 account_iface = TNY_ACCOUNT_IFACE(account);
511 account_store = MODEST_TNY_ACCOUNT_STORE(self);
512 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
514 account_name = tny_account_iface_get_id(account_iface);
516 g_warning ("failed to retrieve account name");
520 hostname = tny_account_iface_get_hostname(account_iface);
521 username = tny_account_iface_get_user(account_iface);
522 proto = tny_account_iface_get_proto(account_iface);
524 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
526 hostname, username, NULL,
532 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
533 TnyStoreAccountIface *account)
535 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
536 g_warning ("failed to add store account");
541 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
542 TnyTransportAccountIface *account)
544 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
545 g_warning ("failed to add transport account");
550 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
552 ModestTnyAccountStore *self;
553 ModestTnyAccountStorePrivate *priv;
556 g_return_val_if_fail (iface, NULL);
558 self = MODEST_TNY_ACCOUNT_STORE(iface);
559 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
561 if (!priv->store_accounts) {
563 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
565 MODEST_PROTO_TYPE_STORE,
568 g_mutex_lock (priv->store_lock);
569 priv->store_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
570 g_mutex_unlock (priv->store_lock);
571 g_slist_free (accounts);
574 return priv->store_accounts;
579 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
581 ModestTnyAccountStore *self;
582 ModestTnyAccountStorePrivate *priv;
585 g_return_val_if_fail (iface, NULL);
587 self = MODEST_TNY_ACCOUNT_STORE(iface);
588 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
591 if (!priv->transport_accounts) {
593 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
595 MODEST_PROTO_TYPE_TRANSPORT,
597 g_mutex_lock (priv->transport_lock);
598 priv->transport_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
599 g_mutex_unlock (priv->transport_lock);
600 g_slist_free (accounts);
604 return priv->transport_accounts;
609 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
611 ModestTnyAccountStorePrivate *priv;
612 g_return_val_if_fail (self, NULL);
614 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
616 return priv->modest_acc_mgr;
621 tny_account_store_get_session (TnyAccountStore *self)
623 ModestTnyAccountStorePrivate *priv;
624 g_return_val_if_fail (self, NULL);
626 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
628 return priv->tny_session_camel;
633 * modest_tny_account_store_get_cache_dir:
634 * @self: self a TnyAccountStoreIface instance
636 * returns the pathname of the cache directory
638 * Returns: a string with the value of the pathname
639 * to the cache directory or NULL if the environment variable $HOME is
640 * not set. string should _not_ be freed by caller
643 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
645 ModestTnyAccountStorePrivate *priv;
646 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
648 if (!priv->cache_dir) {
649 if (g_getenv("HOME") != NULL)
650 priv->cache_dir = g_strconcat(g_getenv("HOME"),
651 "/.modest/cache/", NULL);
653 return priv->cache_dir;
657 static const TnyDeviceIface*
658 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
660 ModestTnyAccountStorePrivate *priv;
662 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
669 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
671 TnyAccountStoreIfaceClass *klass;
673 g_return_if_fail (g_iface);
675 klass = (TnyAccountStoreIfaceClass *)g_iface;
677 klass->add_store_account_func =
678 modest_tny_account_store_add_store_account;
679 klass->get_store_accounts_func =
680 modest_tny_account_store_get_store_accounts;
681 klass->add_transport_account_func =
682 modest_tny_account_store_add_transport_account;
683 klass->get_transport_accounts_func =
684 modest_tny_account_store_get_transport_accounts;
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;