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 gboolean modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
30 TnyStoreAccountIface *account);
31 static gboolean 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);
36 static gboolean destroy_all_accounts (ModestTnyAccountStore *self);
37 static gboolean get_all_accounts (ModestTnyAccountStore *self);
46 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
47 struct _ModestTnyAccountStorePrivate {
48 ModestAccountMgr *modest_acc_mgr;
49 GList *tny_transport_accounts;
50 GList *tny_store_accounts;
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 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
62 modest_tny_account_store_get_type (void)
64 static GType my_type = 0;
66 static const GTypeInfo my_info = {
67 sizeof(ModestTnyAccountStoreClass),
69 NULL, /* base finalize */
70 (GClassInitFunc) modest_tny_account_store_class_init,
71 NULL, /* class finalize */
72 NULL, /* class data */
73 sizeof(ModestTnyAccountStore),
75 (GInstanceInitFunc) modest_tny_account_store_init,
78 static const GInterfaceInfo iface_info = {
79 (GInterfaceInitFunc) modest_tny_account_store_iface_init,
80 NULL, /* interface_finalize */
81 NULL /* interface_data */
84 my_type = g_type_register_static (G_TYPE_OBJECT,
85 "ModestTnyAccountStore", &my_info, 0);
87 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
97 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
99 GObjectClass *gobject_class;
100 gobject_class = (GObjectClass*) klass;
102 parent_class = g_type_class_peek_parent (klass);
103 gobject_class->finalize = modest_tny_account_store_finalize;
105 g_type_class_add_private (gobject_class, sizeof(ModestTnyAccountStorePrivate));
107 /* signal definitions go here, e.g.: */
108 /* signals[MY_SIGNAL_1] = */
109 /* g_signal_new ("my_signal_1",....); */
110 /* signals[MY_SIGNAL_2] = */
111 /* g_signal_new ("my_signal_2",....); */
116 modest_tny_account_store_init (ModestTnyAccountStore *obj)
118 ModestTnyAccountStorePrivate *priv =
119 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
121 priv->modest_acc_mgr = NULL;
122 priv->tny_transport_accounts = NULL;
123 priv->tny_store_accounts = NULL;
127 modest_tny_account_store_finalize (GObject *obj)
129 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
130 ModestTnyAccountStorePrivate *priv =
131 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
133 if (priv->modest_acc_mgr) {
134 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
135 priv->modest_acc_mgr = NULL;
138 destroy_all_accounts (self);
142 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
145 ModestTnyAccountStorePrivate *priv;
147 g_return_val_if_fail (modest_acc_mgr, NULL);
149 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
151 if (!get_all_accounts (MODEST_TNY_ACCOUNT_STORE(obj))) {
152 g_warning ("could get accounts");
153 g_object_unref (obj);
157 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
158 g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
165 destroy_account (gpointer account)
167 g_object_unref (G_OBJECT(account));
172 destroy_all_accounts (ModestTnyAccountStore *self)
174 ModestTnyAccountStorePrivate *priv;
176 g_return_val_if_fail (self, FALSE);
178 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
180 /* clear up old stuff first */
181 if (priv->tny_transport_accounts) {
182 g_list_foreach (priv->tny_transport_accounts,
183 (GFunc)destroy_account, NULL);
184 g_list_free (priv->tny_transport_accounts);
185 priv->tny_transport_accounts = NULL;
189 if (priv->tny_store_accounts) {
190 g_list_foreach (priv->tny_store_accounts,
191 (GFunc)destroy_account, NULL);
192 g_list_free (priv->tny_store_accounts);
193 priv->tny_store_accounts = NULL;
200 /* FIXME: tinymail needs to change here */
201 /* a gpointer arg to get_password should be enough */
203 get_password (TnyAccountIface *account, const gchar *prompt)
205 /* don't want to create all these, but there's no other way right now */
206 ModestConf *modest_conf;
207 ModestAccountMgr *modest_acc_mgr;
209 const gchar *account_name;
211 g_return_val_if_fail (account, NULL);
213 modest_conf = MODEST_CONF(modest_conf_new ());
215 g_warning ("could not create conf");
219 modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
220 if (!modest_acc_mgr) {
221 g_object_unref (modest_conf);
222 g_warning ("could not create acc mgr");
226 account_name = tny_account_iface_get_id(account);
228 g_object_unref (modest_acc_mgr);
229 g_object_unref (modest_conf);
230 g_warning ("could not retrieve account name");
234 pw = modest_account_mgr_get_account_string (modest_acc_mgr, account_name,
235 MODEST_ACCOUNT_PASSWORD, NULL);
237 g_object_unref (G_OBJECT(modest_conf));
238 g_object_unref (G_OBJECT(modest_acc_mgr));
245 forget_password (TnyAccountIface *account)
247 g_warning (__FUNCTION__);
253 add_tny_account_from_account (ModestTnyAccountStore *self, const gchar* account_name)
256 TnyAccountIface *account_iface = NULL;
257 ModestTnyAccountStorePrivate *priv;
259 g_return_val_if_fail (self, FALSE);
260 g_return_val_if_fail (account_name, FALSE);
262 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
264 type = modest_account_mgr_get_account_string (priv->modest_acc_mgr,
266 MODEST_ACCOUNT_TYPE, NULL);
268 g_warning ("error in account %s: type not defined", account_name);
272 if (strcmp (type, MODEST_ACCOUNT_TYPE_STORE) == 0) {
273 account_iface = TNY_ACCOUNT_IFACE (tny_store_account_new ());
274 priv->tny_store_accounts = g_list_append (priv->tny_store_accounts,
277 } else if (strcmp (type, MODEST_ACCOUNT_TYPE_TRANSPORT) == 0) {
278 account_iface = TNY_ACCOUNT_IFACE (tny_transport_account_new ());
279 priv->tny_transport_accounts = g_list_append (priv->tny_transport_accounts,
282 g_warning ("invalid account '%s': type: '%s'", account_name, type);
288 tny_account_iface_set_id(account_iface, account_name);
290 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
291 MODEST_ACCOUNT_PROTO,NULL);
292 tny_account_iface_set_proto(account_iface, val);
295 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
296 MODEST_ACCOUNT_SERVER,NULL);
297 tny_account_iface_set_hostname(account_iface, val);
300 val = modest_account_mgr_get_account_string (priv->modest_acc_mgr, account_name,
301 MODEST_ACCOUNT_USER,NULL);
302 tny_account_iface_set_user(account_iface, val);
305 tny_account_iface_set_pass_func(account_iface, get_password);
306 tny_account_iface_set_forget_pass_func(account_iface, forget_password);
314 get_all_accounts (ModestTnyAccountStore *self)
316 ModestTnyAccountStorePrivate *priv;
317 GSList *account_names, *cursor;
319 g_return_if_fail (self);
321 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
323 destroy_all_accounts (self);
325 cursor = account_names = modest_account_mgr_account_names (priv->modest_acc_mgr,
328 const gchar *account_name = (const gchar*) cursor->data;
329 if (!add_tny_account_from_account (self, account_name)) {
330 g_warning ("cannot add iface for account %s",
334 cursor = cursor->next;
342 modest_tny_account_store_add_store_account (TnyAccountStoreIface *self,
343 TnyStoreAccountIface *account)
345 TnyAccountIface *account_iface;
346 ModestTnyAccountStore *account_store;
347 ModestTnyAccountStorePrivate *priv;
349 const gchar* account_name;
350 const gchar *hostname, *user, *proto;
353 g_return_if_fail (self);
354 g_return_if_fail (account);
356 account_iface = TNY_ACCOUNT_IFACE(account);
357 account_store = MODEST_TNY_ACCOUNT_STORE(self);
358 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
360 account_name = tny_account_iface_get_id(account_iface);
362 g_warning ("failed to retrieve account name");
366 if (!modest_account_mgr_add_account (priv->modest_acc_mgr, account_name, NULL)) {
367 g_warning ("failed to add account %s", account_name);
371 hostname = tny_account_iface_get_hostname(account_iface);
372 user = tny_account_iface_get_user(account_iface);
373 proto = tny_account_iface_get_proto(account_iface);
375 if (!hostname || !user || !proto) {
376 g_warning ("error in account data: hostname:%s; user:%s; proto:%s",
377 hostname ? hostname : "<none>",
378 user ? user : "<none>",
379 proto ? proto : "<none>");
383 check = modest_account_mgr_set_account_string (priv->modest_acc_mgr,
384 account_name,MODEST_ACCOUNT_SERVER,
386 check = check && modest_account_mgr_set_account_string (priv->modest_acc_mgr,
387 account_name,MODEST_ACCOUNT_USER,
389 check = check && modest_account_mgr_set_account_string (priv->modest_acc_mgr,
390 account_name, MODEST_ACCOUNT_PROTO,
393 g_warning ("failed to set some account data");
400 modest_tny_account_store_get_store_accounts (TnyAccountStoreIface *iface)
402 ModestTnyAccountStore *self;
403 ModestTnyAccountStorePrivate *priv;
405 g_return_val_if_fail (iface, NULL);
407 self = MODEST_TNY_ACCOUNT_STORE(iface);
408 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
410 if (!priv->tny_store_accounts)
411 get_all_accounts (self);
413 return priv->tny_store_accounts;
418 modest_tny_account_store_add_transport_account (TnyAccountStoreIface *self,
419 TnyTransportAccountIface *account)
421 g_return_if_fail (self);
422 g_return_if_fail (account);
424 return modest_tny_account_store_add_transport_account (self, account);
429 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
431 ModestTnyAccountStore *self;
432 ModestTnyAccountStorePrivate *priv;
434 g_return_if_fail (iface);
436 self = MODEST_TNY_ACCOUNT_STORE(iface);
437 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
439 if (!priv->tny_transport_accounts)
440 get_all_accounts (self);
442 return priv->tny_transport_accounts;
447 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
449 TnyAccountStoreIfaceClass *klass;
451 g_return_if_fail (g_iface);
453 klass = (TnyAccountStoreIfaceClass *)g_iface;
455 klass->add_store_account_func = modest_tny_account_store_add_store_account;
456 klass->get_store_accounts_func = modest_tny_account_store_get_store_accounts;
457 klass->add_transport_account_func = modest_tny_account_store_add_transport_account;
458 klass->get_transport_accounts_func = modest_tny_account_store_get_transport_accounts;