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>
12 #include <tny-store-account.h>
13 #include <tny-transport-account.h>
15 #include "modest-account-mgr.h"
16 #include "modest-tny-account-store.h"
18 /* 'private'/'protected' functions */
19 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
20 static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
21 static void modest_tny_account_store_finalize (GObject *obj);
23 /* implementations for tny-account-store-iface */
24 static void modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data);
26 static void modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
27 TnyStoreAccountIface *account);
28 static void modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
29 TnyTransportAccountIface *account);
30 static const GList* modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface);
31 static const GList* modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface);
40 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
41 struct _ModestTnyAccountStorePrivate {
42 ModestAccountMgr *modest_acc_mgr;
44 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
45 MODEST_TYPE_TNY_ACCOUNT_STORE, \
46 ModestTnyAccountStorePrivate))
48 static GObjectClass *parent_class = NULL;
50 /* uncomment the following if you have defined any signals */
51 /* static guint signals[LAST_SIGNAL] = {0}; */
54 modest_tny_account_store_get_type (void)
56 static GType my_type = 0;
58 static const GTypeInfo my_info = {
59 sizeof(ModestTnyAccountStoreClass),
61 NULL, /* base finalize */
62 (GClassInitFunc) modest_tny_account_store_class_init,
63 NULL, /* class finalize */
64 NULL, /* class data */
65 sizeof(ModestTnyAccountStore),
67 (GInstanceInitFunc) modest_tny_account_store_init,
70 static const GInterfaceInfo iface_info = {
71 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
72 NULL, /* interface_finalize */
73 NULL /* interface_data */
76 my_type = g_type_register_static (G_TYPE_OBJECT,
77 "ModestTnyAccountStore", &my_info, 0);
79 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
87 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
89 GObjectClass *gobject_class;
90 gobject_class = (GObjectClass*) klass;
92 parent_class = g_type_class_peek_parent (klass);
93 gobject_class->finalize = modest_tny_account_store_finalize;
95 g_type_class_add_private (gobject_class, sizeof(ModestTnyAccountStorePrivate));
97 /* signal definitions go here, e.g.: */
98 /* signals[MY_SIGNAL_1] = */
99 /* g_signal_new ("my_signal_1",....); */
100 /* signals[MY_SIGNAL_2] = */
101 /* g_signal_new ("my_signal_2",....); */
106 modest_tny_account_store_init (ModestTnyAccountStore *obj)
108 ModestTnyAccountStorePrivate *priv =
109 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
111 priv->modest_acc_mgr = NULL;
115 modest_tny_account_store_finalize (GObject *obj)
117 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
118 ModestTnyAccountStorePrivate *priv =
119 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
121 if (priv->modest_acc_mgr) {
122 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
123 priv->modest_acc_mgr = NULL;
128 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
131 ModestTnyAccountStorePrivate *priv;
133 g_return_val_if_fail (modest_acc_mgr, NULL);
135 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
137 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
138 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
145 /* FIXME: tinymail needs to change here */
146 /* a gpointer arg to get_password should be enough */
148 get_password (TnyAccountIface *account, const gchar *prompt)
150 const gchar *key = tny_account_iface_get_id (account);
151 const TnyAccountStoreIface *AccountStore = tny_account_iface_get_account_store(account);
152 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(AccountStore);
156 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
157 MODEST_ACCOUNT_PASSWORD, NULL);
159 * FIXME: if no password avail. in modest-conf, then we need to get the pw from
163 /* g_warning (val); */
170 forget_password (TnyAccountIface *account)
172 g_warning (__FUNCTION__);
178 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
180 TnyAccountIface *account_iface;
181 ModestTnyAccountStore *account_store;
182 ModestTnyAccountStorePrivate *priv;
184 const gchar* account_name;
185 const gchar *hostname, *username, *proto;
187 g_warning (__FUNCTION__);
189 g_return_val_if_fail (self, FALSE);
190 g_return_val_if_fail (account, FALSE);
192 account_iface = TNY_ACCOUNT_IFACE(account);
193 account_store = MODEST_TNY_ACCOUNT_STORE(self);
194 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
196 account_name = tny_account_iface_get_id(account_iface);
198 g_warning ("failed to retrieve account name");
202 hostname = tny_account_iface_get_hostname(account_iface);
203 username = tny_account_iface_get_user(account_iface);
204 proto = tny_account_iface_get_proto(account_iface);
206 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
208 hostname, username, NULL,
215 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
216 TnyStoreAccountIface *account)
218 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
219 g_warning ("failed to add store account");
225 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
226 TnyTransportAccountIface *account)
228 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
229 g_warning ("failed to add transport account");
235 static TnyAccountIface*
236 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
239 TnyAccountIface *tny_account;
240 ModestTnyAccountStorePrivate *priv;
243 g_return_val_if_fail (self, NULL);
244 g_return_val_if_fail (key, NULL);
246 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
248 /* is it a store or a transport? */
250 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
252 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
254 tny_account_iface_set_account_store (tny_account,
255 TNY_ACCOUNT_STORE_IFACE(self));
257 tny_account_iface_set_id (tny_account, key);
260 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
261 MODEST_ACCOUNT_HOSTNAME, NULL);
263 tny_account_iface_set_hostname (tny_account, val);
267 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
268 MODEST_ACCOUNT_USERNAME, NULL);
270 tny_account_iface_set_user (tny_account, val);
274 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
275 MODEST_ACCOUNT_PROTO, NULL);
277 tny_account_iface_set_proto (tny_account, val);
280 g_warning ("set_pass");
281 tny_account_iface_set_pass_func (tny_account, get_password);
282 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
289 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
292 GSList *cursor = accounts;
293 GList *tny_accounts = NULL;
295 g_return_val_if_fail (self, NULL);
298 TnyAccountIface *tny_account;
299 tny_account = tny_account_from_key (self, (gchar*)cursor->data, is_store);
301 g_warning ("could not create tnyaccount for %s",
302 (gchar*)cursor->data);
304 g_warning ("added %s",(gchar*)cursor->data);
306 g_list_append (tny_accounts, tny_account);
308 cursor = cursor->next;
318 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
320 ModestTnyAccountStore *self;
321 ModestTnyAccountStorePrivate *priv;
322 GSList *accounts, *cursor;
325 g_return_val_if_fail (iface, NULL);
327 g_warning ("i'm being called: %s", __FUNCTION__);
329 self = MODEST_TNY_ACCOUNT_STORE(iface);
330 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
333 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
335 MODEST_PROTO_TYPE_STORE,
338 g_warning ("accounts: %d", g_slist_length (accounts));
339 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
340 g_slist_free (accounts);
341 g_warning ("store accounts: %d", g_list_length (tny_accounts));
343 return tny_accounts; /* FIXME: who will free this? */
349 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
351 ModestTnyAccountStore *self;
352 ModestTnyAccountStorePrivate *priv;
353 GSList *accounts, *cursor;
356 g_return_val_if_fail (iface, NULL);
358 self = MODEST_TNY_ACCOUNT_STORE(iface);
359 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
362 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
364 MODEST_PROTO_TYPE_TRANSPORT,
366 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
367 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
369 g_slist_free (accounts);
371 return tny_accounts; /* FIXME: who will free this? */
376 * modest_tny_account_store_get_cache_dir:
377 * @self: self a TnyAccountStoreIface instance
379 * returns the pathname of the cache directory
381 * Returns: a newly allocated string with the value of the pathname
382 * to the cache directory or NULL if the environment variable $HOME is
384 * pointer has to be freed by caller
387 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
391 if (g_getenv("HOME") != NULL)
392 cache_dir = g_strconcat(g_getenv("HOME"), "/.modest/cache/", NULL);
402 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
404 TnyAccountStoreIfaceClass *klass;
406 g_return_if_fail (g_iface);
408 klass = (TnyAccountStoreIfaceClass *)g_iface;
410 klass->add_store_account_func =
411 modest_tny_account_store_add_store_account;
412 klass->get_store_accounts_func =
413 modest_tny_account_store_get_store_accounts;
414 klass->add_transport_account_func =
415 modest_tny_account_store_add_transport_account;
416 klass->get_transport_accounts_func =
417 modest_tny_account_store_get_transport_accounts;
418 klass->get_cache_dir_func =
419 modest_tny_account_store_get_cache_dir;