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>
13 #include <tny-store-account.h>
14 #include <tny-transport-account.h>
16 #include "modest-account-mgr.h"
17 #include "modest-tny-account-store.h"
19 /* 'private'/'protected' functions */
20 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
21 static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
22 static void modest_tny_account_store_finalize (GObject *obj);
24 /* implementations for tny-account-store-iface */
25 static void modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data);
27 static void modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
28 TnyStoreAccountIface *account);
29 static void modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
30 TnyTransportAccountIface *account);
31 static const GList* modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface);
32 static const GList* modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface);
41 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
42 struct _ModestTnyAccountStorePrivate {
44 GList *store_accounts;
45 GList *transport_accounts;
48 ModestAccountMgr *modest_acc_mgr;
50 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51 MODEST_TYPE_TNY_ACCOUNT_STORE, \
52 ModestTnyAccountStorePrivate))
54 static GObjectClass *parent_class = NULL;
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
60 modest_tny_account_store_get_type (void)
62 static GType my_type = 0;
64 static const GTypeInfo my_info = {
65 sizeof(ModestTnyAccountStoreClass),
67 NULL, /* base finalize */
68 (GClassInitFunc) modest_tny_account_store_class_init,
69 NULL, /* class finalize */
70 NULL, /* class data */
71 sizeof(ModestTnyAccountStore),
73 (GInstanceInitFunc) modest_tny_account_store_init,
76 static const GInterfaceInfo iface_info = {
77 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
78 NULL, /* interface_finalize */
79 NULL /* interface_data */
82 my_type = g_type_register_static (G_TYPE_OBJECT,
83 "ModestTnyAccountStore", &my_info, 0);
85 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
92 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
94 GObjectClass *gobject_class;
95 gobject_class = (GObjectClass*) klass;
97 parent_class = g_type_class_peek_parent (klass);
98 gobject_class->finalize = modest_tny_account_store_finalize;
100 g_type_class_add_private (gobject_class,
101 sizeof(ModestTnyAccountStorePrivate));
103 /* signal definitions go here, e.g.: */
104 /* signals[MY_SIGNAL_1] = */
105 /* g_signal_new ("my_signal_1",....); */
106 /* signals[MY_SIGNAL_2] = */
107 /* g_signal_new ("my_signal_2",....); */
112 modest_tny_account_store_init (ModestTnyAccountStore *obj)
114 ModestTnyAccountStorePrivate *priv =
115 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
117 priv->modest_acc_mgr = NULL;
119 priv->store_accounts = NULL;
120 priv->transport_accounts = NULL;
121 priv->cache_dir = NULL;
126 free_gobject (GObject *obj, gpointer user_data)
129 g_object_unref (obj);
134 free_gobject_list (GList *list)
137 g_list_foreach (list, (GFunc)free_gobject, NULL);
145 modest_tny_account_store_finalize (GObject *obj)
147 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
148 ModestTnyAccountStorePrivate *priv =
149 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
151 if (priv->modest_acc_mgr) {
152 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
153 priv->modest_acc_mgr = NULL;
156 priv->store_accounts = free_gobject_list (priv->store_accounts);
157 priv->transport_accounts = free_gobject_list (priv->store_accounts);
159 g_free (priv->cache_dir);
160 priv->cache_dir = NULL;
164 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
167 ModestTnyAccountStorePrivate *priv;
169 g_return_val_if_fail (modest_acc_mgr, NULL);
171 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
173 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
174 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
181 /* FIXME: tinymail needs to change here */
182 /* a gpointer arg to get_password should be enough */
184 get_password (TnyAccountIface *account, const gchar *prompt)
186 const gchar *key = tny_account_iface_get_id (account);
187 const TnyAccountStoreIface *AccountStore = tny_account_iface_get_account_store(account);
188 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(AccountStore);
192 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
193 MODEST_ACCOUNT_PASSWORD, NULL);
195 * FIXME: if no password avail. in modest-conf, then we need to get the pw from
198 * DJCB: good point: we should probably emit a signal for that..
201 /* g_warning (val); */
208 forget_password (TnyAccountIface *account)
210 g_warning (__FUNCTION__);
216 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
218 TnyAccountIface *account_iface;
219 ModestTnyAccountStore *account_store;
220 ModestTnyAccountStorePrivate *priv;
222 const gchar* account_name;
223 const gchar *hostname, *username, *proto;
225 g_warning (__FUNCTION__);
227 g_return_val_if_fail (self, FALSE);
228 g_return_val_if_fail (account, FALSE);
230 account_iface = TNY_ACCOUNT_IFACE(account);
231 account_store = MODEST_TNY_ACCOUNT_STORE(self);
232 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
234 account_name = tny_account_iface_get_id(account_iface);
236 g_warning ("failed to retrieve account name");
240 hostname = tny_account_iface_get_hostname(account_iface);
241 username = tny_account_iface_get_user(account_iface);
242 proto = tny_account_iface_get_proto(account_iface);
244 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
246 hostname, username, NULL,
253 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
254 TnyStoreAccountIface *account)
256 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
257 g_warning ("failed to add store account");
263 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
264 TnyTransportAccountIface *account)
266 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
267 g_warning ("failed to add transport account");
273 static TnyAccountIface*
274 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
277 TnyAccountIface *tny_account;
278 ModestTnyAccountStorePrivate *priv;
281 g_return_val_if_fail (self, NULL);
282 g_return_val_if_fail (key, NULL);
284 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
286 /* is it a store or a transport? */
288 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
290 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
293 g_warning ("failed to create new tny %s account",
294 is_store ? "store" : "transport");
298 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
299 TNY_ACCOUNT_STORE_IFACE(self));
301 tny_account_iface_set_id (tny_account, key);
304 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
305 MODEST_ACCOUNT_PROTO, NULL);
307 tny_account_iface_set_proto (tny_account, val);
310 g_warning ("protocol not defined for %s", key);
311 g_object_unref (G_OBJECT(tny_account));
317 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
318 MODEST_ACCOUNT_HOSTNAME, NULL);
320 g_warning ("hostname: %s", val);
321 tny_account_iface_set_hostname (tny_account, val);
327 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
328 MODEST_ACCOUNT_USERNAME, NULL);
330 tny_account_iface_set_user (tny_account, val);
334 tny_account_iface_set_pass_func (tny_account, get_password);
335 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
342 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
345 GSList *cursor = accounts;
346 GList *tny_accounts = NULL;
348 g_return_val_if_fail (self, NULL);
351 TnyAccountIface *tny_account;
352 tny_account = tny_account_from_key (self, (gchar*)cursor->data, is_store);
354 g_warning ("could not create tnyaccount for %s",
355 (gchar*)cursor->data);
357 g_warning ("added %s",(gchar*)cursor->data);
359 g_list_append (tny_accounts, tny_account);
361 cursor = cursor->next;
364 g_warning ("got %d %s account(s)", g_list_length(tny_accounts),
365 is_store ? "store" : "transport");
374 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
376 ModestTnyAccountStore *self;
377 ModestTnyAccountStorePrivate *priv;
378 GSList *accounts, *cursor;
381 g_return_val_if_fail (iface, NULL);
383 self = MODEST_TNY_ACCOUNT_STORE(iface);
384 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
387 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
389 MODEST_PROTO_TYPE_STORE,
392 g_message ("accounts: %d", g_slist_length (accounts));
393 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
394 g_slist_free (accounts);
395 g_message ("store accounts: %d", g_list_length (tny_accounts));
399 * FIXME: after gconf notification support is added,
400 * we can simply return priv->store_account
402 priv->store_accounts = free_gobject_list (priv->store_accounts);
403 priv->store_accounts = tny_accounts;
411 modest_tny_account_store_get_transport_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_TRANSPORT,
428 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
429 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
431 g_slist_free (accounts);
434 * FIXME: after gconf notification support is added,
435 * we can simply return priv->store_account
437 priv->transport_accounts = free_gobject_list (priv->transport_accounts);
438 priv->transport_accounts = tny_accounts;
440 return tny_accounts; /* FIXME: who will free this? */
445 * modest_tny_account_store_get_cache_dir:
446 * @self: self a TnyAccountStoreIface instance
448 * returns the pathname of the cache directory
450 * Returns: a string with the value of the pathname
451 * to the cache directory or NULL if the environment variable $HOME is
452 * not set. string should _not_ be freed by caller
455 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
457 ModestTnyAccountStorePrivate *priv;
458 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
462 if (!priv->cache_dir) {
463 if (g_getenv("HOME") != NULL)
464 priv->cache_dir = g_strconcat(g_getenv("HOME"),
465 "/.modest/cache/", NULL);
467 return priv->cache_dir;
473 static const TnyDeviceIface*
474 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
482 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
484 TnyAccountStoreIfaceClass *klass;
486 g_return_if_fail (g_iface);
488 klass = (TnyAccountStoreIfaceClass *)g_iface;
490 klass->add_store_account_func =
491 modest_tny_account_store_add_store_account;
492 klass->get_store_accounts_func =
493 modest_tny_account_store_get_store_accounts;
494 klass->add_transport_account_func =
495 modest_tny_account_store_add_transport_account;
496 klass->get_transport_accounts_func =
497 modest_tny_account_store_get_transport_accounts;
498 klass->get_cache_dir_func =
499 modest_tny_account_store_get_cache_dir;
500 klass->get_device_func =
501 modest_tny_account_store_get_device;