1 /* modest-tny-account-store.c */
3 /* insert (c)/licensing information) */
7 #include <tny-account-store-iface.h>
8 #include <tny-account-iface.h>
9 #include <tny-account-store-iface.h>
11 #include <tny-account-iface.h>
12 #include <tny-store-account-iface.h>
13 #include <tny-transport-account-iface.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);
43 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
44 struct _ModestTnyAccountStorePrivate {
45 ModestAccountMgr *modest_acc_mgr;
47 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
48 MODEST_TYPE_TNY_ACCOUNT_STORE, \
49 ModestTnyAccountStorePrivate))
51 static GObjectClass *parent_class = NULL;
53 /* uncomment the following if you have defined any signals */
54 /* static guint signals[LAST_SIGNAL] = {0}; */
57 modest_tny_account_store_get_type (void)
59 static GType my_type = 0;
61 static const GTypeInfo my_info = {
62 sizeof(ModestTnyAccountStoreClass),
64 NULL, /* base finalize */
65 (GClassInitFunc) modest_tny_account_store_class_init,
66 NULL, /* class finalize */
67 NULL, /* class data */
68 sizeof(ModestTnyAccountStore),
70 (GInstanceInitFunc) modest_tny_account_store_init,
73 static const GInterfaceInfo iface_info = {
74 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
75 NULL, /* interface_finalize */
76 NULL /* interface_data */
79 my_type = g_type_register_static (G_TYPE_OBJECT,
80 "ModestTnyAccountStore", &my_info, 0);
82 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
90 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
92 GObjectClass *gobject_class;
93 gobject_class = (GObjectClass*) klass;
95 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->finalize = modest_tny_account_store_finalize;
98 g_type_class_add_private (gobject_class, sizeof(ModestTnyAccountStorePrivate));
100 /* signal definitions go here, e.g.: */
101 /* signals[MY_SIGNAL_1] = */
102 /* g_signal_new ("my_signal_1",....); */
103 /* signals[MY_SIGNAL_2] = */
104 /* g_signal_new ("my_signal_2",....); */
109 modest_tny_account_store_init (ModestTnyAccountStore *obj)
111 ModestTnyAccountStorePrivate *priv =
112 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
114 priv->modest_acc_mgr = NULL;
118 modest_tny_account_store_finalize (GObject *obj)
120 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
121 ModestTnyAccountStorePrivate *priv =
122 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
124 if (priv->modest_acc_mgr) {
125 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
126 priv->modest_acc_mgr = NULL;
131 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
134 ModestTnyAccountStorePrivate *priv;
136 g_return_val_if_fail (modest_acc_mgr, NULL);
138 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
140 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
141 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
148 /* FIXME: tinymail needs to change here */
149 /* a gpointer arg to get_password should be enough */
151 get_password (TnyAccountIface *account, const gchar *prompt)
153 g_warning ("%s: %s", __FUNCTION__, prompt);
154 return g_strdup("djcb123");
159 forget_password (TnyAccountIface *account)
161 g_warning (__FUNCTION__);
167 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
169 TnyAccountIface *account_iface;
170 ModestTnyAccountStore *account_store;
171 ModestTnyAccountStorePrivate *priv;
173 const gchar* account_name;
174 const gchar *hostname, *username, *proto;
176 g_warning (__FUNCTION__);
178 g_return_val_if_fail (self, FALSE);
179 g_return_val_if_fail (account, FALSE);
181 account_iface = TNY_ACCOUNT_IFACE(account);
182 account_store = MODEST_TNY_ACCOUNT_STORE(self);
183 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
185 account_name = tny_account_iface_get_id(account_iface);
187 g_warning ("failed to retrieve account name");
191 hostname = tny_account_iface_get_hostname(account_iface);
192 username = tny_account_iface_get_user(account_iface);
193 proto = tny_account_iface_get_proto(account_iface);
195 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
197 hostname, username, NULL,
204 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
205 TnyStoreAccountIface *account)
207 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
208 g_warning ("failed to add store account");
214 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
215 TnyTransportAccountIface *account)
217 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
218 g_warning ("failed to add transport account");
224 static TnyAccountIface*
225 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
228 TnyAccountIface *tny_account;
229 ModestTnyAccountStorePrivate *priv;
232 g_return_val_if_fail (self, NULL);
233 g_return_val_if_fail (key, NULL);
235 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
237 /* is it a store or a transport? */
239 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
241 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
243 tny_account_iface_set_account_store (tny_account,
244 TNY_ACCOUNT_STORE_IFACE(self));
246 tny_account_iface_set_id (tny_account, key);
249 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
250 MODEST_ACCOUNT_HOSTNAME, NULL);
252 tny_account_iface_set_hostname (tny_account, val);
256 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
257 MODEST_ACCOUNT_USERNAME, NULL);
259 tny_account_iface_set_user (tny_account, val);
263 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, key,
264 MODEST_ACCOUNT_PROTO, NULL);
266 tny_account_iface_set_proto (tny_account, val);
269 g_warning ("set_pass");
270 tny_account_iface_set_pass_func (tny_account, get_password);
271 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
278 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
281 GSList *cursor = accounts;
282 GList *tny_accounts = NULL;
284 g_return_val_if_fail (self, NULL);
287 TnyAccountIface *tny_account;
288 tny_account = tny_account_from_key (self, (gchar*)cursor->data, is_store);
290 g_warning ("could not create tnyaccount for %s",
291 (gchar*)cursor->data);
293 g_warning ("added %s",(gchar*)cursor->data);
295 g_list_append (tny_accounts, tny_account);
297 cursor = cursor->next;
307 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
309 ModestTnyAccountStore *self;
310 ModestTnyAccountStorePrivate *priv;
311 GSList *accounts, *cursor;
314 g_return_val_if_fail (iface, NULL);
316 g_warning ("i'm being called: %s", __FUNCTION__);
318 self = MODEST_TNY_ACCOUNT_STORE(iface);
319 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
322 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
324 MODEST_PROTO_TYPE_STORE,
327 g_warning ("accounts: %d", g_slist_length (accounts));
328 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
329 g_slist_free (accounts);
330 g_warning ("store accounts: %d", g_list_length (tny_accounts));
332 return tny_accounts; /* FIXME: who will free this? */
338 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
340 ModestTnyAccountStore *self;
341 ModestTnyAccountStorePrivate *priv;
342 GSList *accounts, *cursor;
345 g_return_val_if_fail (iface, NULL);
347 self = MODEST_TNY_ACCOUNT_STORE(iface);
348 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
351 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
353 MODEST_PROTO_TYPE_TRANSPORT,
355 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
356 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
358 g_slist_free (accounts);
360 return tny_accounts; /* FIXME: who will free this? */
365 * modest_tny_account_store_get_cache_dir:
366 * @self: self a TnyAccountStoreIface instance
368 * returns the pathname of the cache directory
370 * Returns: a newly allocated string with the value of the pathname
371 * to the cache directory or NULL if the environment variable $HOME is
373 * pointer has to be freed by caller
376 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
380 if (g_getenv("HOME") != NULL)
381 cache_dir = g_strconcat(g_getenv("HOME"), "/.modest/cache/", NULL);
391 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
393 TnyAccountStoreIfaceClass *klass;
395 g_return_if_fail (g_iface);
397 klass = (TnyAccountStoreIfaceClass *)g_iface;
399 klass->add_store_account_func =
400 modest_tny_account_store_add_store_account;
401 klass->get_store_accounts_func =
402 modest_tny_account_store_get_store_accounts;
403 klass->add_transport_account_func =
404 modest_tny_account_store_add_transport_account;
405 klass->get_transport_accounts_func =
406 modest_tny_account_store_get_transport_accounts;
407 klass->get_cache_dir_func =
408 modest_tny_account_store_get_cache_dir;