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-device.h>
13 #include <tny-account-store.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);
38 PASSWORD_REQUESTED_SIGNAL,
42 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
43 struct _ModestTnyAccountStorePrivate {
45 GList *store_accounts;
46 GList *transport_accounts;
49 TnySessionCamel *tny_session_camel;
50 TnyDeviceIface *device;
52 ModestAccountMgr *modest_acc_mgr;
54 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55 MODEST_TYPE_TNY_ACCOUNT_STORE, \
56 ModestTnyAccountStorePrivate))
58 static GObjectClass *parent_class = NULL;
60 static guint signals[LAST_SIGNAL] = {0};
63 modest_tny_account_store_get_type (void)
65 static GType my_type = 0;
67 static const GTypeInfo my_info = {
68 sizeof(ModestTnyAccountStoreClass),
70 NULL, /* base finalize */
71 (GClassInitFunc) modest_tny_account_store_class_init,
72 NULL, /* class finalize */
73 NULL, /* class data */
74 sizeof(ModestTnyAccountStore),
76 (GInstanceInitFunc) modest_tny_account_store_init,
79 static const GInterfaceInfo iface_info = {
80 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
81 NULL, /* interface_finalize */
82 NULL /* interface_data */
85 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
86 "ModestTnyAccountStore", &my_info, 0);
88 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
95 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
97 GObjectClass *gobject_class;
98 gobject_class = (GObjectClass*) klass;
100 parent_class = g_type_class_peek_parent (klass);
101 gobject_class->finalize = modest_tny_account_store_finalize;
103 g_type_class_add_private (gobject_class,
104 sizeof(ModestTnyAccountStorePrivate));
106 signals[PASSWORD_REQUESTED_SIGNAL] =
107 g_signal_new ("password_requested",
108 G_TYPE_FROM_CLASS (gobject_class),
110 G_STRUCT_OFFSET(ModestTnyAccountStoreClass,password_requested),
112 g_cclosure_marshal_VOID__POINTER,
113 G_TYPE_NONE, 1, G_TYPE_POINTER);
117 modest_tny_account_store_init (ModestTnyAccountStore *obj)
119 ModestTnyAccountStorePrivate *priv =
120 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
122 priv->modest_acc_mgr = NULL;
125 priv->store_accounts = NULL;
126 priv->transport_accounts = NULL;
127 priv->cache_dir = NULL;
129 priv->tny_session_camel = NULL;
134 free_gobject (GObject *obj, gpointer user_data)
137 g_object_unref (obj);
142 free_gobject_list (GList *list)
145 g_list_foreach (list, (GFunc)free_gobject, NULL);
153 modest_tny_account_store_finalize (GObject *obj)
155 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
156 ModestTnyAccountStorePrivate *priv =
157 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
159 if (priv->modest_acc_mgr) {
160 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
161 priv->modest_acc_mgr = NULL;
164 if (priv->tny_session_camel) {
165 g_object_unref (G_OBJECT(priv->tny_session_camel));
166 priv->tny_session_camel = NULL;
170 g_object_unref (G_OBJECT(priv->device));
174 priv->store_accounts = free_gobject_list (priv->store_accounts);
175 priv->transport_accounts = free_gobject_list (priv->store_accounts);
177 g_free (priv->cache_dir);
178 priv->cache_dir = NULL;
185 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
188 ModestTnyAccountStorePrivate *priv;
190 g_return_val_if_fail (modest_acc_mgr, NULL);
192 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
194 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
195 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
197 priv->device = (TnyDeviceIface*)tny_device_new();
199 g_warning ("Cannot create Device instance");
200 g_object_unref (obj);
203 priv->tny_session_camel = tny_session_camel_new
204 (TNY_ACCOUNT_STORE_IFACE(obj));
205 if (!priv->tny_session_camel) {
206 g_warning ("Cannot create TnySessionCamel instance");
207 g_object_unref (obj);
216 get_password (TnyAccountIface *account, const gchar *prompt)
219 const TnyAccountStoreIface *account_store;
220 ModestTnyAccountStore *self;
221 ModestTnyAccountStorePrivate *priv;
224 g_message (__FUNCTION__);
226 g_return_val_if_fail (account, NULL);
228 key = tny_account_iface_get_id (account);
229 account_store = tny_account_iface_get_account_store(account);
231 self = MODEST_TNY_ACCOUNT_STORE (account_store);
232 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
234 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
235 MODEST_ACCOUNT_PASSWORD,
240 * append the prompt to the emitted signal,
241 * so the password dialog shows the prompt supplied by the caller of this function.
243 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
253 forget_password (TnyAccountIface *account)
255 g_warning (__FUNCTION__);
261 add_account (TnyAccountStoreIface *self, TnyAccountIface *account)
263 TnyAccountIface *account_iface;
264 ModestTnyAccountStore *account_store;
265 ModestTnyAccountStorePrivate *priv;
267 const gchar* account_name;
268 const gchar *hostname, *username, *proto;
270 g_return_val_if_fail (self, FALSE);
271 g_return_val_if_fail (account, FALSE);
273 account_iface = TNY_ACCOUNT_IFACE(account);
274 account_store = MODEST_TNY_ACCOUNT_STORE(self);
275 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
277 account_name = tny_account_iface_get_id(account_iface);
279 g_warning ("failed to retrieve account name");
283 hostname = tny_account_iface_get_hostname(account_iface);
284 username = tny_account_iface_get_user(account_iface);
285 proto = tny_account_iface_get_proto(account_iface);
287 return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
289 hostname, username, NULL,
296 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
297 TnyStoreAccountIface *account)
299 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
300 g_warning ("failed to add store account");
306 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
307 TnyTransportAccountIface *account)
309 if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
310 g_warning ("failed to add transport account");
315 static TnyAccountIface*
316 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
319 TnyAccountIface *tny_account;
320 ModestTnyAccountStorePrivate *priv;
323 g_return_val_if_fail (self, NULL);
324 g_return_val_if_fail (key, NULL);
326 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
328 /* is it a store or a transport? */
330 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
332 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
335 g_warning ("failed to create new tny %s account",
336 is_store ? "store" : "transport");
340 tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
341 TNY_ACCOUNT_STORE_IFACE(self));
343 tny_account_iface_set_id (tny_account, key);
346 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
347 MODEST_ACCOUNT_PROTO, NULL);
349 tny_account_iface_set_proto (tny_account, val);
352 g_warning ("protocol not defined for %s", key);
353 g_object_unref (G_OBJECT(tny_account));
359 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
360 MODEST_ACCOUNT_HOSTNAME,
363 tny_account_iface_set_hostname (tny_account, val);
369 val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
370 MODEST_ACCOUNT_USERNAME,
373 tny_account_iface_set_user (tny_account, val);
377 tny_account_iface_set_pass_func (tny_account, get_password);
378 tny_account_iface_set_forget_pass_func (tny_account, forget_password);
385 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
388 GSList *cursor = accounts;
389 GList *tny_accounts = NULL;
391 g_return_val_if_fail (self, NULL);
394 TnyAccountIface *tny_account;
395 gchar *key = cursor->data;
396 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
399 g_warning ("could not create tnyaccount for %s",
400 (gchar*)cursor->data);
403 g_list_append (tny_accounts, tny_account);
405 cursor = cursor->next;
414 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
416 ModestTnyAccountStore *self;
417 ModestTnyAccountStorePrivate *priv;
418 GSList *accounts, *cursor;
421 g_return_val_if_fail (iface, NULL);
423 self = MODEST_TNY_ACCOUNT_STORE(iface);
424 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
427 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
429 MODEST_PROTO_TYPE_STORE,
432 g_message ("accounts: %d", g_slist_length (accounts));
433 tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
434 g_slist_free (accounts);
435 g_message ("store accounts: %d", g_list_length (tny_accounts));
439 * FIXME: after gconf notification support is added,
440 * we can simply return priv->store_account
442 priv->store_accounts = free_gobject_list (priv->store_accounts);
443 priv->store_accounts = tny_accounts;
451 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
453 ModestTnyAccountStore *self;
454 ModestTnyAccountStorePrivate *priv;
455 GSList *accounts, *cursor;
458 g_return_val_if_fail (iface, NULL);
460 self = MODEST_TNY_ACCOUNT_STORE(iface);
461 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
464 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
466 MODEST_PROTO_TYPE_TRANSPORT,
468 tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
469 g_warning ("transport accounts: %d", g_list_length (tny_accounts));
471 g_slist_free (accounts);
474 * FIXME: after gconf notification support is added,
475 * we can simply return priv->store_account
477 priv->transport_accounts = free_gobject_list (priv->transport_accounts);
478 priv->transport_accounts = tny_accounts;
480 return tny_accounts; /* FIXME: who will free this? */
485 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
487 ModestTnyAccountStorePrivate *priv;
488 g_return_val_if_fail (self, NULL);
490 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
492 return priv->modest_acc_mgr;
498 tny_account_store_get_session (TnyAccountStore *self)
500 ModestTnyAccountStorePrivate *priv;
501 g_return_val_if_fail (self, NULL);
503 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
505 g_message ("returning tny session camel %p",
506 priv->tny_session_camel);
508 return priv->tny_session_camel;
514 * modest_tny_account_store_get_cache_dir:
515 * @self: self a TnyAccountStoreIface instance
517 * returns the pathname of the cache directory
519 * Returns: a string with the value of the pathname
520 * to the cache directory or NULL if the environment variable $HOME is
521 * not set. string should _not_ be freed by caller
524 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
526 ModestTnyAccountStorePrivate *priv;
527 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
531 if (!priv->cache_dir) {
532 if (g_getenv("HOME") != NULL)
533 priv->cache_dir = g_strconcat(g_getenv("HOME"),
534 "/.modest/cache/", NULL);
536 return priv->cache_dir;
541 static const TnyDeviceIface*
542 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
544 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
552 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
554 TnyAccountStoreIfaceClass *klass;
556 g_return_if_fail (g_iface);
558 klass = (TnyAccountStoreIfaceClass *)g_iface;
560 klass->add_store_account_func =
561 modest_tny_account_store_add_store_account;
562 klass->get_store_accounts_func =
563 modest_tny_account_store_get_store_accounts;
564 klass->add_transport_account_func =
565 modest_tny_account_store_add_transport_account;
566 klass->get_transport_accounts_func =
567 modest_tny_account_store_get_transport_accounts;
568 klass->get_cache_dir_func =
569 modest_tny_account_store_get_cache_dir;
570 klass->get_device_func =
571 modest_tny_account_store_get_device;