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;
50 ModestAccountMgr *modest_acc_mgr;
52 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53 MODEST_TYPE_TNY_ACCOUNT_STORE, \
54 ModestTnyAccountStorePrivate))
56 static GObjectClass *parent_class = NULL;
58 static guint signals[LAST_SIGNAL] = {0};
61 modest_tny_account_store_get_type (void)
63 static GType my_type = 0;
65 static const GTypeInfo my_info = {
66 sizeof(ModestTnyAccountStoreClass),
68 NULL, /* base finalize */
69 (GClassInitFunc) modest_tny_account_store_class_init,
70 NULL, /* class finalize */
71 NULL, /* class data */
72 sizeof(ModestTnyAccountStore),
74 (GInstanceInitFunc) modest_tny_account_store_init,
77 static const GInterfaceInfo iface_info = {
78 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
79 NULL, /* interface_finalize */
80 NULL /* interface_data */
83 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
84 "ModestTnyAccountStore", &my_info, 0);
86 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
93 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
95 GObjectClass *gobject_class;
96 gobject_class = (GObjectClass*) klass;
98 parent_class = g_type_class_peek_parent (klass);
99 gobject_class->finalize = modest_tny_account_store_finalize;
101 g_type_class_add_private (gobject_class,
102 sizeof(ModestTnyAccountStorePrivate));
104 signals[PASSWORD_REQUESTED_SIGNAL] =
105 g_signal_new ("password_requested",
106 G_TYPE_FROM_CLASS (gobject_class),
108 G_STRUCT_OFFSET(ModestTnyAccountStoreClass,password_requested),
110 g_cclosure_marshal_VOID__POINTER,
111 G_TYPE_NONE, 1, G_TYPE_POINTER);
115 modest_tny_account_store_init (ModestTnyAccountStore *obj)
117 ModestTnyAccountStorePrivate *priv =
118 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
120 priv->modest_acc_mgr = NULL;
122 priv->store_accounts = NULL;
123 priv->transport_accounts = NULL;
124 priv->cache_dir = NULL;
126 priv->tny_session_camel = NULL;
131 free_gobject (GObject *obj, gpointer user_data)
134 g_object_unref (obj);
139 free_gobject_list (GList *list)
142 g_list_foreach (list, (GFunc)free_gobject, NULL);
150 modest_tny_account_store_finalize (GObject *obj)
152 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
153 ModestTnyAccountStorePrivate *priv =
154 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
156 if (priv->modest_acc_mgr) {
157 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
158 priv->modest_acc_mgr = NULL;
161 if (priv->tny_session_camel) {
162 g_object_unref (G_OBJECT(priv->tny_session_camel));
163 priv->tny_session_camel = NULL;
166 priv->store_accounts = free_gobject_list (priv->store_accounts);
167 priv->transport_accounts = free_gobject_list (priv->store_accounts);
169 g_free (priv->cache_dir);
170 priv->cache_dir = NULL;
177 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
180 ModestTnyAccountStorePrivate *priv;
182 g_return_val_if_fail (modest_acc_mgr, NULL);
184 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
186 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
187 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
189 priv->tny_session_camel = tny_session_camel_new
190 (TNY_ACCOUNT_STORE_IFACE(obj));
191 if (!priv->tny_session_camel) {
192 g_warning ("cannot create TnySessionCamel instance");
193 g_object_unref (obj);
202 get_password (TnyAccountIface *account, const gchar *prompt)
205 const TnyAccountStoreIface *account_store;
206 ModestTnyAccountStore *self;
207 ModestTnyAccountStorePrivate *priv;
210 g_message (__FUNCTION__);
212 g_return_val_if_fail (account, NULL);
214 key = tny_account_iface_get_id (account);
215 account_store = tny_account_iface_get_account_store(account);
217 self = MODEST_TNY_ACCOUNT_STORE (account_store);
218 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
220 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
221 MODEST_ACCOUNT_PASSWORD,
223 if (!val || strlen(val)==0)
225 g_message("Key vor Aufruf: %s", key);
226 /* g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key); */
227 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key);
236 forget_password (TnyAccountIface *account)
238 g_warning (__FUNCTION__);
244 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
246 TnyAccountIface *account_iface;
247 ModestTnyAccountStore *account_store;
248 ModestTnyAccountStorePrivate *priv;
250 const gchar* account_name;
251 const gchar *hostname, *username, *proto;
253 g_return_val_if_fail (self, FALSE);
254 g_return_val_if_fail (account, FALSE);
256 account_iface = TNY_ACCOUNT_IFACE(account);
257 account_store = MODEST_TNY_ACCOUNT_STORE(self);
258 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
260 account_name = tny_account_iface_get_id(account_iface);
262 g_warning ("failed to retrieve account name");
266 hostname = tny_account_iface_get_hostname(account_iface);
267 username = tny_account_iface_get_user(account_iface);
268 proto = tny_account_iface_get_proto(account_iface);
270 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
272 hostname, username, NULL,
279 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
280 TnyStoreAccountIface *account)
282 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
283 g_warning ("failed to add store account");
289 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
290 TnyTransportAccountIface *account)
292 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
293 g_warning ("failed to add transport account");
298 static TnyAccountIface*
299 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
302 TnyAccountIface *tny_account;
303 ModestTnyAccountStorePrivate *priv;
306 g_return_val_if_fail (self, NULL);
307 g_return_val_if_fail (key, NULL);
309 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
311 /* is it a store or a transport? */
313 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
315 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
318 g_warning ("failed to create new tny %s account",
319 is_store ? "store" : "transport");
323 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
324 TNY_ACCOUNT_STORE_IFACE(self));
326 tny_account_iface_set_id (tny_account, key);
329 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
330 MODEST_ACCOUNT_PROTO, NULL);
332 tny_account_iface_set_proto (tny_account, val);
335 g_warning ("protocol not defined for %s", key);
336 g_object_unref (G_OBJECT(tny_account));
342 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
343 MODEST_ACCOUNT_HOSTNAME,
346 tny_account_iface_set_hostname (tny_account, val);
352 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
353 MODEST_ACCOUNT_USERNAME,
356 tny_account_iface_set_user (tny_account, val);
360 tny_account_iface_set_pass_func (tny_account, get_password);
361 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
368 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
371 GSList *cursor = accounts;
372 GList *tny_accounts = NULL;
374 g_return_val_if_fail (self, NULL);
377 TnyAccountIface *tny_account;
378 gchar *key = cursor->data;
379 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
382 g_warning ("could not create tnyaccount for %s",
383 (gchar*)cursor->data);
386 g_list_append (tny_accounts, tny_account);
388 cursor = cursor->next;
397 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
399 ModestTnyAccountStore *self;
400 ModestTnyAccountStorePrivate *priv;
401 GSList *accounts, *cursor;
404 g_return_val_if_fail (iface, NULL);
406 self = MODEST_TNY_ACCOUNT_STORE(iface);
407 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
410 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
412 MODEST_PROTO_TYPE_STORE,
415 g_message ("accounts: %d", g_slist_length (accounts));
416 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
417 g_slist_free (accounts);
418 g_message ("store accounts: %d", g_list_length (tny_accounts));
422 * FIXME: after gconf notification support is added,
423 * we can simply return priv->store_account
425 priv->store_accounts = free_gobject_list (priv->store_accounts);
426 priv->store_accounts = tny_accounts;
434 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
436 ModestTnyAccountStore *self;
437 ModestTnyAccountStorePrivate *priv;
438 GSList *accounts, *cursor;
441 g_return_val_if_fail (iface, NULL);
443 self = MODEST_TNY_ACCOUNT_STORE(iface);
444 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
447 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
449 MODEST_PROTO_TYPE_TRANSPORT,
451 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
452 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
454 g_slist_free (accounts);
457 * FIXME: after gconf notification support is added,
458 * we can simply return priv->store_account
460 priv->transport_accounts = free_gobject_list (priv->transport_accounts);
461 priv->transport_accounts = tny_accounts;
463 return tny_accounts; /* FIXME: who will free this? */
467 ModestAccountMgr *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
469 ModestTnyAccountStorePrivate *priv;
470 g_return_val_if_fail (self, NULL);
472 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
474 return priv->modest_acc_mgr;
480 tny_account_store_get_session (TnyAccountStore *self)
482 ModestTnyAccountStorePrivate *priv;
483 g_return_val_if_fail (self, NULL);
485 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
487 g_message ("returning tny session camel %p",
488 priv->tny_session_camel);
490 return priv->tny_session_camel;
496 * modest_tny_account_store_get_cache_dir:
497 * @self: self a TnyAccountStoreIface instance
499 * returns the pathname of the cache directory
501 * Returns: a string with the value of the pathname
502 * to the cache directory or NULL if the environment variable $HOME is
503 * not set. string should _not_ be freed by caller
506 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
508 ModestTnyAccountStorePrivate *priv;
509 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
513 if (!priv->cache_dir) {
514 if (g_getenv("HOME") != NULL)
515 priv->cache_dir = g_strconcat(g_getenv("HOME"),
516 "/.modest/cache/", NULL);
518 return priv->cache_dir;
523 static const TnyDeviceIface*
524 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
526 return NULL; /* FIXME */
532 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
534 TnyAccountStoreIfaceClass *klass;
536 g_return_if_fail (g_iface);
538 klass = (TnyAccountStoreIfaceClass *)g_iface;
540 klass->add_store_account_func =
541 modest_tny_account_store_add_store_account;
542 klass->get_store_accounts_func =
543 modest_tny_account_store_get_store_accounts;
544 klass->add_transport_account_func =
545 modest_tny_account_store_add_transport_account;
546 klass->get_transport_accounts_func =
547 modest_tny_account_store_get_transport_accounts;
548 klass->get_cache_dir_func =
549 modest_tny_account_store_get_cache_dir;
550 klass->get_device_func =
551 modest_tny_account_store_get_device;