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-account-store.h>
14 #include <tny-store-account.h>
15 #include <tny-transport-account.h>
17 #include "modest-account-mgr.h"
18 #include "modest-tny-account-store.h"
20 /* 'private'/'protected' functions */
21 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
22 static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
23 static void modest_tny_account_store_finalize (GObject *obj);
25 /* implementations for tny-account-store-iface */
26 static void modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data);
28 static void modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
29 TnyStoreAccountIface *account);
30 static void modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
31 TnyTransportAccountIface *account);
32 static const GList* modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface);
33 static const GList* modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface);
37 PASSWORD_REQUESTED_SIGNAL,
41 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
42 struct _ModestTnyAccountStorePrivate {
44 GList *store_accounts;
45 GList *transport_accounts;
48 TnySessionCamel *tny_session_camel;
49 TnyDeviceIface *device;
51 ModestAccountMgr *modest_acc_mgr;
53 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
54 MODEST_TYPE_TNY_ACCOUNT_STORE, \
55 ModestTnyAccountStorePrivate))
57 static GObjectClass *parent_class = NULL;
59 static guint signals[LAST_SIGNAL] = {0};
62 modest_tny_account_store_get_type (void)
64 static GType my_type = 0;
66 static const GTypeInfo my_info = {
67 sizeof(ModestTnyAccountStoreClass),
69 NULL, /* base finalize */
70 (GClassInitFunc) modest_tny_account_store_class_init,
71 NULL, /* class finalize */
72 NULL, /* class data */
73 sizeof(ModestTnyAccountStore),
75 (GInstanceInitFunc) modest_tny_account_store_init,
78 static const GInterfaceInfo iface_info = {
79 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
80 NULL, /* interface_finalize */
81 NULL /* interface_data */
84 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
85 "ModestTnyAccountStore", &my_info, 0);
87 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
94 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
96 GObjectClass *gobject_class;
97 gobject_class = (GObjectClass*) klass;
99 parent_class = g_type_class_peek_parent (klass);
100 gobject_class->finalize = modest_tny_account_store_finalize;
102 g_type_class_add_private (gobject_class,
103 sizeof(ModestTnyAccountStorePrivate));
105 signals[PASSWORD_REQUESTED_SIGNAL] =
106 g_signal_new ("password_requested",
107 G_TYPE_FROM_CLASS (gobject_class),
109 G_STRUCT_OFFSET(ModestTnyAccountStoreClass,password_requested),
111 g_cclosure_marshal_VOID__POINTER,
112 G_TYPE_NONE, 1, G_TYPE_POINTER);
116 modest_tny_account_store_init (ModestTnyAccountStore *obj)
118 ModestTnyAccountStorePrivate *priv =
119 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
121 priv->modest_acc_mgr = NULL;
124 priv->store_accounts = NULL;
125 priv->transport_accounts = NULL;
126 priv->cache_dir = NULL;
128 priv->tny_session_camel = NULL;
133 free_gobject (GObject *obj, gpointer user_data)
136 g_object_unref (obj);
141 free_gobject_list (GList *list)
144 g_list_foreach (list, (GFunc)free_gobject, NULL);
152 modest_tny_account_store_finalize (GObject *obj)
154 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
155 ModestTnyAccountStorePrivate *priv =
156 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
158 if (priv->modest_acc_mgr) {
159 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
160 priv->modest_acc_mgr = NULL;
163 if (priv->tny_session_camel) {
164 g_object_unref (G_OBJECT(priv->tny_session_camel));
165 priv->tny_session_camel = NULL;
169 g_object_unref (G_OBJECT(priv->device));
173 priv->store_accounts = free_gobject_list (priv->store_accounts);
174 priv->transport_accounts = free_gobject_list (priv->store_accounts);
176 g_free (priv->cache_dir);
177 priv->cache_dir = NULL;
184 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
187 ModestTnyAccountStorePrivate *priv;
189 g_return_val_if_fail (modest_acc_mgr, NULL);
191 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
193 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
194 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
196 priv->device = (TnyDeviceIface*)tny_device_new();
198 g_warning ("Cannot create Device instance");
199 g_object_unref (obj);
202 priv->tny_session_camel = tny_session_camel_new
203 (TNY_ACCOUNT_STORE_IFACE(obj));
204 if (!priv->tny_session_camel) {
205 g_warning ("Cannot create TnySessionCamel instance");
206 g_object_unref (obj);
215 get_password (TnyAccountIface *account, const gchar *prompt)
218 const TnyAccountStoreIface *account_store;
219 ModestTnyAccountStore *self;
220 ModestTnyAccountStorePrivate *priv;
223 g_return_val_if_fail (account, NULL);
225 key = tny_account_iface_get_id (account);
226 account_store = tny_account_iface_get_account_store(account);
228 self = MODEST_TNY_ACCOUNT_STORE (account_store);
229 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
231 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
232 MODEST_ACCOUNT_PASSWORD,
237 * append the prompt to the emitted signal,
238 * so the password dialog shows the prompt supplied by the caller of this function.
240 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
250 forget_password (TnyAccountIface *account)
252 g_warning (__FUNCTION__);
258 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
260 TnyAccountIface *account_iface;
261 ModestTnyAccountStore *account_store;
262 ModestTnyAccountStorePrivate *priv;
264 const gchar* account_name;
265 const gchar *hostname, *username, *proto;
267 g_return_val_if_fail (self, FALSE);
268 g_return_val_if_fail (account, FALSE);
270 account_iface = TNY_ACCOUNT_IFACE(account);
271 account_store = MODEST_TNY_ACCOUNT_STORE(self);
272 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
274 account_name = tny_account_iface_get_id(account_iface);
276 g_warning ("failed to retrieve account name");
280 hostname = tny_account_iface_get_hostname(account_iface);
281 username = tny_account_iface_get_user(account_iface);
282 proto = tny_account_iface_get_proto(account_iface);
284 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
286 hostname, username, NULL,
293 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
294 TnyStoreAccountIface *account)
296 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
297 g_warning ("failed to add store account");
303 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
304 TnyTransportAccountIface *account)
306 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
307 g_warning ("failed to add transport account");
312 static TnyAccountIface*
313 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
316 TnyAccountIface *tny_account;
317 ModestTnyAccountStorePrivate *priv;
320 g_return_val_if_fail (self, NULL);
321 g_return_val_if_fail (key, NULL);
323 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
325 /* is it a store or a transport? */
327 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
329 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
332 g_warning ("failed to create new tny %s account",
333 is_store ? "store" : "transport");
337 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
338 TNY_ACCOUNT_STORE_IFACE(self));
340 tny_account_iface_set_id (tny_account, key);
341 tny_account_iface_set_name (tny_account, key);
344 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
345 MODEST_ACCOUNT_PROTO, NULL);
347 tny_account_iface_set_proto (tny_account, val);
350 g_warning ("protocol not defined for %s", key);
351 g_object_unref (G_OBJECT(tny_account));
356 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
357 MODEST_ACCOUNT_HOSTNAME,
360 tny_account_iface_set_hostname (tny_account, val);
366 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
367 MODEST_ACCOUNT_USERNAME,
370 tny_account_iface_set_user (tny_account, val);
374 tny_account_iface_set_pass_func (tny_account, get_password);
375 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
382 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
385 GSList *cursor = accounts;
386 GList *tny_accounts = NULL;
388 g_return_val_if_fail (self, NULL);
391 TnyAccountIface *tny_account;
392 gchar *key = cursor->data;
393 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
396 g_warning ("could not create tnyaccount for %s",
397 (gchar*)cursor->data);
400 g_list_append (tny_accounts, tny_account);
402 cursor = cursor->next;
411 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
413 ModestTnyAccountStore *self;
414 ModestTnyAccountStorePrivate *priv;
415 GSList *accounts, *cursor;
418 g_return_val_if_fail (iface, NULL);
420 self = MODEST_TNY_ACCOUNT_STORE(iface);
421 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
424 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
426 MODEST_PROTO_TYPE_STORE,
429 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
430 g_slist_free (accounts);
433 * FIXME: after gconf notification support is added,
434 * we can simply return priv->store_account
436 priv->store_accounts = free_gobject_list (priv->store_accounts);
437 priv->store_accounts = tny_accounts;
445 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
447 ModestTnyAccountStore *self;
448 ModestTnyAccountStorePrivate *priv;
449 GSList *accounts, *cursor;
452 g_return_val_if_fail (iface, NULL);
454 self = MODEST_TNY_ACCOUNT_STORE(iface);
455 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
458 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
460 MODEST_PROTO_TYPE_TRANSPORT,
462 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
463 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
465 g_slist_free (accounts);
468 * FIXME: after gconf notification support is added,
469 * we can simply return priv->store_account
471 priv->transport_accounts = free_gobject_list (priv->transport_accounts);
472 priv->transport_accounts = tny_accounts;
474 return tny_accounts; /* FIXME: who will free this? */
479 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
481 ModestTnyAccountStorePrivate *priv;
482 g_return_val_if_fail (self, NULL);
484 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
486 return priv->modest_acc_mgr;
492 tny_account_store_get_session (TnyAccountStore *self)
494 ModestTnyAccountStorePrivate *priv;
495 g_return_val_if_fail (self, NULL);
497 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
499 return priv->tny_session_camel;
505 * modest_tny_account_store_get_cache_dir:
506 * @self: self a TnyAccountStoreIface instance
508 * returns the pathname of the cache directory
510 * Returns: a string with the value of the pathname
511 * to the cache directory or NULL if the environment variable $HOME is
512 * not set. string should _not_ be freed by caller
515 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
517 ModestTnyAccountStorePrivate *priv;
518 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
522 if (!priv->cache_dir) {
523 if (g_getenv("HOME") != NULL)
524 priv->cache_dir = g_strconcat(g_getenv("HOME"),
525 "/.modest/cache/", NULL);
527 return priv->cache_dir;
532 static const TnyDeviceIface*
533 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
535 ModestTnyAccountStorePrivate *priv;
537 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
545 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
547 TnyAccountStoreIfaceClass *klass;
549 g_return_if_fail (g_iface);
551 klass = (TnyAccountStoreIfaceClass *)g_iface;
553 klass->add_store_account_func =
554 modest_tny_account_store_add_store_account;
555 klass->get_store_accounts_func =
556 modest_tny_account_store_get_store_accounts;
557 klass->add_transport_account_func =
558 modest_tny_account_store_add_transport_account;
559 klass->get_transport_accounts_func =
560 modest_tny_account_store_get_transport_accounts;
561 klass->get_cache_dir_func =
562 modest_tny_account_store_get_cache_dir;
563 klass->get_device_func =
564 modest_tny_account_store_get_device;