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 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
43 struct _ModestTnyAccountStorePrivate {
45 GList *store_accounts;
46 GList *transport_accounts;
49 TnySessionCamel *tny_session_camel;
50 TnyDeviceIface *device;
52 ModestAccountMgr *modest_acc_mgr;
54 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55 MODEST_TYPE_TNY_ACCOUNT_STORE, \
56 ModestTnyAccountStorePrivate))
58 static GObjectClass *parent_class = NULL;
60 static guint signals[LAST_SIGNAL] = {0};
63 modest_tny_account_store_get_type (void)
65 static GType my_type = 0;
67 static const GTypeInfo my_info = {
68 sizeof(ModestTnyAccountStoreClass),
70 NULL, /* base finalize */
71 (GClassInitFunc) modest_tny_account_store_class_init,
72 NULL, /* class finalize */
73 NULL, /* class data */
74 sizeof(ModestTnyAccountStore),
76 (GInstanceInitFunc) modest_tny_account_store_init,
79 static const GInterfaceInfo iface_info = {
80 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
81 NULL, /* interface_finalize */
82 NULL /* interface_data */
85 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
86 "ModestTnyAccountStore", &my_info, 0);
88 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
95 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
97 GObjectClass *gobject_class;
98 gobject_class = (GObjectClass*) klass;
100 parent_class = g_type_class_peek_parent (klass);
101 gobject_class->finalize = modest_tny_account_store_finalize;
103 g_type_class_add_private (gobject_class,
104 sizeof(ModestTnyAccountStorePrivate));
106 signals[PASSWORD_REQUESTED_SIGNAL] =
107 g_signal_new ("password_requested",
108 G_TYPE_FROM_CLASS (gobject_class),
110 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
112 g_cclosure_marshal_VOID__POINTER,
113 G_TYPE_NONE, 1, G_TYPE_POINTER);
117 modest_tny_account_store_init (ModestTnyAccountStore *obj)
119 ModestTnyAccountStorePrivate *priv =
120 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
122 priv->modest_acc_mgr = NULL;
125 priv->store_accounts = NULL;
126 priv->transport_accounts = NULL;
127 priv->cache_dir = NULL;
129 priv->tny_session_camel = NULL;
134 free_gobject (GObject *obj, gpointer user_data)
137 g_object_unref (obj);
142 free_gobject_list (GList *list)
145 g_list_foreach (list, (GFunc)free_gobject, NULL);
153 manager_new_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
155 g_print ("new account signal %s\n", name);
160 manager_remove_account (ModestAccountMgr *modest_acc_mgr,gchar *name, gpointer data)
162 g_print ("remove account signal %s\n", name);
167 manager_change_account (ModestAccountMgr *modest_acc_mgr, gchar *accountname,
168 gchar *key, gchar* value, gpointer data)
170 g_print ("account change signal: account: %s key: %s value: %s\n", accountname, key, value);
175 modest_tny_account_store_finalize (GObject *obj)
177 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
178 ModestTnyAccountStorePrivate *priv =
179 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
181 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
182 G_CALLBACK(manager_new_account), NULL);
183 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
184 G_CALLBACK(manager_remove_account), NULL);
185 g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr),
186 G_CALLBACK(manager_change_account), NULL);
188 if (priv->modest_acc_mgr) {
189 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
190 priv->modest_acc_mgr = NULL;
193 if (priv->tny_session_camel) {
194 g_object_unref (G_OBJECT(priv->tny_session_camel));
195 priv->tny_session_camel = NULL;
199 g_object_unref (G_OBJECT(priv->device));
203 priv->store_accounts = free_gobject_list (priv->store_accounts);
204 priv->transport_accounts = free_gobject_list (priv->store_accounts);
206 g_free (priv->cache_dir);
207 priv->cache_dir = NULL;
214 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
217 ModestTnyAccountStorePrivate *priv;
219 g_return_val_if_fail (modest_acc_mgr, NULL);
221 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
223 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
225 g_object_ref(G_OBJECT(modest_acc_mgr));
226 priv->modest_acc_mgr = modest_acc_mgr;
228 priv->device = (TnyDeviceIface*)tny_device_new();
230 g_warning ("Cannot create Device instance");
231 g_object_unref (obj);
234 priv->tny_session_camel = tny_session_camel_new
235 (TNY_ACCOUNT_STORE_IFACE(obj));
236 if (!priv->tny_session_camel) {
237 g_warning ("Cannot create TnySessionCamel instance");
238 g_object_unref (obj);
242 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-add",
243 G_CALLBACK(manager_new_account), NULL);
244 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-remove",
245 G_CALLBACK(manager_remove_account), NULL);
246 g_signal_connect (G_OBJECT (modest_acc_mgr), "account-change",
247 G_CALLBACK(manager_change_account), NULL);
254 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
257 const TnyAccountStoreIface *account_store;
258 ModestTnyAccountStore *self;
259 ModestTnyAccountStorePrivate *priv;
262 g_return_val_if_fail (account, NULL);
264 key = tny_account_iface_get_id (account);
265 account_store = tny_account_iface_get_account_store(account);
267 self = MODEST_TNY_ACCOUNT_STORE (account_store);
268 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
270 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
271 MODEST_ACCOUNT_PASSWORD, NULL);
274 * append the prompt to the emitted signal,
275 * so the password dialog shows the prompt supplied by the caller of this function.
277 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key);
285 forget_password (TnyAccountIface *account)
287 g_warning (__FUNCTION__);
293 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
295 TnyAccountIface *account_iface;
296 ModestTnyAccountStore *account_store;
297 ModestTnyAccountStorePrivate *priv;
299 const gchar *account_name;
300 const gchar *hostname, *username, *proto;
302 g_return_val_if_fail (self, FALSE);
303 g_return_val_if_fail (account, FALSE);
305 account_iface = TNY_ACCOUNT_IFACE(account);
306 account_store = MODEST_TNY_ACCOUNT_STORE(self);
307 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
309 account_name = tny_account_iface_get_id(account_iface);
311 g_warning ("failed to retrieve account name");
315 hostname = tny_account_iface_get_hostname(account_iface);
316 username = tny_account_iface_get_user(account_iface);
317 proto = tny_account_iface_get_proto(account_iface);
319 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
321 hostname, username, NULL,
327 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
328 TnyStoreAccountIface *account)
330 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
331 g_warning ("failed to add store account");
336 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
337 TnyTransportAccountIface *account)
339 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
340 g_warning ("failed to add transport account");
344 static TnyAccountIface*
345 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
348 TnyAccountIface *tny_account;
349 ModestTnyAccountStorePrivate *priv;
352 g_return_val_if_fail (self, NULL);
353 g_return_val_if_fail (key, NULL);
355 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
357 /* is it a store or a transport? */
359 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
361 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
364 g_warning ("failed to create new tny %s account",
365 is_store ? "store" : "transport");
369 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
370 TNY_ACCOUNT_STORE_IFACE(self));
372 tny_account_iface_set_id (tny_account, key);
373 tny_account_iface_set_name (tny_account, key);
376 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
377 MODEST_ACCOUNT_PROTO, NULL);
379 tny_account_iface_set_proto (tny_account, val);
382 g_warning ("protocol not defined for %s", key);
383 g_object_unref (G_OBJECT(tny_account));
388 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
389 MODEST_ACCOUNT_HOSTNAME,
392 tny_account_iface_set_hostname (tny_account, val);
398 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
399 MODEST_ACCOUNT_USERNAME,
402 tny_account_iface_set_user (tny_account, val);
406 tny_account_iface_set_pass_func (tny_account, get_password);
407 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
414 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
417 GSList *cursor = accounts;
418 GList *tny_accounts = NULL;
420 g_return_val_if_fail (self, NULL);
423 TnyAccountIface *tny_account;
424 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
427 g_warning ("could not create tnyaccount for %s",
428 (gchar*)cursor->data);
431 g_list_append (tny_accounts, tny_account);
433 cursor = cursor->next;
441 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
443 ModestTnyAccountStore *self;
444 ModestTnyAccountStorePrivate *priv;
447 g_return_val_if_fail (iface, NULL);
449 self = MODEST_TNY_ACCOUNT_STORE(iface);
450 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
452 if (!priv->store_accounts) {
454 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
456 MODEST_PROTO_TYPE_STORE,
459 priv->store_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
460 g_slist_free (accounts);
463 return priv->store_accounts;
468 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
470 ModestTnyAccountStore *self;
471 ModestTnyAccountStorePrivate *priv;
474 g_return_val_if_fail (iface, NULL);
476 self = MODEST_TNY_ACCOUNT_STORE(iface);
477 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
480 if (!priv->transport_accounts) {
482 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
484 MODEST_PROTO_TYPE_TRANSPORT,
486 priv->transport_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
487 g_slist_free (accounts);
491 return priv->transport_accounts;
496 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
498 ModestTnyAccountStorePrivate *priv;
499 g_return_val_if_fail (self, NULL);
501 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
503 return priv->modest_acc_mgr;
508 tny_account_store_get_session (TnyAccountStore *self)
510 ModestTnyAccountStorePrivate *priv;
511 g_return_val_if_fail (self, NULL);
513 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
515 return priv->tny_session_camel;
520 * modest_tny_account_store_get_cache_dir:
521 * @self: self a TnyAccountStoreIface instance
523 * returns the pathname of the cache directory
525 * Returns: a string with the value of the pathname
526 * to the cache directory or NULL if the environment variable $HOME is
527 * not set. string should _not_ be freed by caller
530 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
532 ModestTnyAccountStorePrivate *priv;
533 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
535 if (!priv->cache_dir) {
536 if (g_getenv("HOME") != NULL)
537 priv->cache_dir = g_strconcat(g_getenv("HOME"),
538 "/.modest/cache/", NULL);
540 return priv->cache_dir;
544 static const TnyDeviceIface*
545 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
547 ModestTnyAccountStorePrivate *priv;
549 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
556 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
558 TnyAccountStoreIfaceClass *klass;
560 g_return_if_fail (g_iface);
562 klass = (TnyAccountStoreIfaceClass *)g_iface;
564 klass->add_store_account_func =
565 modest_tny_account_store_add_store_account;
566 klass->get_store_accounts_func =
567 modest_tny_account_store_get_store_accounts;
568 klass->add_transport_account_func =
569 modest_tny_account_store_add_transport_account;
570 klass->get_transport_accounts_func =
571 modest_tny_account_store_get_transport_accounts;
572 klass->get_cache_dir_func =
573 modest_tny_account_store_get_cache_dir;
574 klass->get_device_func =
575 modest_tny_account_store_get_device;