a370f4bef9f47a9e7e7ef803931a42742bf97029
[modest] / src / modest-tny-account-store.c
1 /* modest-tny-account-store.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <string.h>
6
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>
13
14 #include <tny-store-account.h>
15 #include <tny-transport-account.h>
16
17 #include "modest-account-mgr.h"
18 #include "modest-tny-account-store.h"
19
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);
24
25 /* implementations for tny-account-store-iface */
26 static void    modest_tny_account_store_iface_init              (gpointer g_iface, gpointer iface_data);
27
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);
34
35 /* list my signals */
36 enum {
37         PASSWORD_REQUESTED_SIGNAL,
38         LAST_SIGNAL
39 };
40
41 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
42 struct _ModestTnyAccountStorePrivate {
43
44         GList *store_accounts;
45         GList *transport_accounts;
46         gchar *cache_dir;
47
48         TnySessionCamel *tny_session_camel;
49         TnyDeviceIface  *device;
50
51         ModestAccountMgr *modest_acc_mgr;
52 };
53 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
54                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
55                                                       ModestTnyAccountStorePrivate))
56 /* globals */
57 static GObjectClass *parent_class = NULL;
58
59 static guint signals[LAST_SIGNAL] = {0};
60
61 GType
62 modest_tny_account_store_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestTnyAccountStoreClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_tny_account_store_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestTnyAccountStore),
74                         1,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_tny_account_store_init,
76                 };
77
78                 static const GInterfaceInfo iface_info = {
79                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
80                         NULL,         /* interface_finalize */
81                         NULL          /* interface_data */
82                 };
83                 /* hack hack */
84                 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
85                                                   "ModestTnyAccountStore", &my_info, 0);
86
87                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
88                                              &iface_info);
89         }
90         return my_type;
91 }
92
93 static void
94 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
95 {
96         GObjectClass *gobject_class;
97         gobject_class = (GObjectClass*) klass;
98
99         parent_class            = g_type_class_peek_parent (klass);
100         gobject_class->finalize = modest_tny_account_store_finalize;
101
102         g_type_class_add_private (gobject_class,
103                                   sizeof(ModestTnyAccountStorePrivate));
104
105         signals[PASSWORD_REQUESTED_SIGNAL] =
106                 g_signal_new ("password_requested",
107                               G_TYPE_FROM_CLASS (gobject_class),
108                               G_SIGNAL_RUN_FIRST,
109                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass,password_requested),
110                               NULL, NULL,
111                               g_cclosure_marshal_VOID__POINTER,
112                               G_TYPE_NONE, 1, G_TYPE_POINTER);
113 }
114
115 static void
116 modest_tny_account_store_init (ModestTnyAccountStore *obj)
117 {
118         ModestTnyAccountStorePrivate *priv =
119                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
120
121         priv->modest_acc_mgr         = NULL;
122         priv->device                 = NULL;
123
124         priv->store_accounts         = NULL;
125         priv->transport_accounts     = NULL;
126         priv->cache_dir              = NULL;
127
128         priv->tny_session_camel      = NULL;
129 }
130
131
132 static void
133 free_gobject (GObject *obj, gpointer user_data)
134 {
135         if (obj)
136                 g_object_unref (obj);
137 }
138
139
140 static GList*
141 free_gobject_list (GList *list)
142 {
143         if (list) {
144                 g_list_foreach (list, (GFunc)free_gobject, NULL);
145                 g_list_free (list);
146         }
147         return NULL;
148 }
149
150
151 static void
152 modest_tny_account_store_finalize (GObject *obj)
153 {
154         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
155         ModestTnyAccountStorePrivate *priv =
156                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
157
158         if (priv->modest_acc_mgr) {
159                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
160                 priv->modest_acc_mgr = NULL;
161         }
162
163         if (priv->tny_session_camel) {
164                 g_object_unref (G_OBJECT(priv->tny_session_camel));
165                 priv->tny_session_camel = NULL;
166         }
167
168         if (priv->device) {
169                 g_object_unref (G_OBJECT(priv->device));
170                 priv->device = NULL;
171         }
172
173         priv->store_accounts     = free_gobject_list (priv->store_accounts);
174         priv->transport_accounts = free_gobject_list (priv->store_accounts);
175
176         g_free (priv->cache_dir);
177         priv->cache_dir = NULL;
178
179
180
181 }
182
183 GObject*
184 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
185 {
186         GObject *obj;
187         ModestTnyAccountStorePrivate *priv;
188
189         g_return_val_if_fail (modest_acc_mgr, NULL);
190
191         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
192
193         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
194         g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
195
196         priv->device = (TnyDeviceIface*)tny_device_new();
197         if (!priv->device) {
198                 g_warning ("Cannot create Device instance");
199                 g_object_unref (obj);
200                 return NULL;
201         }
202         priv->tny_session_camel = tny_session_camel_new
203                 (TNY_ACCOUNT_STORE_IFACE(obj));
204         if (!priv->tny_session_camel) {
205                 g_warning ("Cannot create TnySessionCamel instance");
206                 g_object_unref (obj);
207                 return NULL;
208         }
209
210         return obj;
211 }
212
213
214 static gchar*
215 get_password (TnyAccountIface *account, const gchar *prompt)
216 {
217         const gchar *key;
218         const TnyAccountStoreIface *account_store;
219         ModestTnyAccountStore *self;
220         ModestTnyAccountStorePrivate *priv;
221         gchar *val;
222
223         g_return_val_if_fail (account, NULL);
224
225         key = tny_account_iface_get_id (account);
226         account_store = tny_account_iface_get_account_store(account);
227
228         self = MODEST_TNY_ACCOUNT_STORE (account_store);
229         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
230
231         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
232                                                             MODEST_ACCOUNT_PASSWORD,
233                                                             NULL);
234         if (!val)
235         {
236                 /* FIXME:
237                  * append the prompt to the emitted signal,
238                  * so the password dialog shows the prompt supplied by the caller of this function.
239                  */
240                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
241                                key);
242
243         }
244
245         return val;
246 }
247
248
249 static void
250 forget_password (TnyAccountIface *account)
251 {
252         g_warning (__FUNCTION__);
253 }
254
255
256
257 static gboolean
258 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account)
259 {
260         TnyAccountIface       *account_iface;
261         ModestTnyAccountStore *account_store;
262         ModestTnyAccountStorePrivate *priv;
263
264         const gchar* account_name;
265         const gchar *hostname, *username, *proto;
266
267         g_return_val_if_fail (self, FALSE);
268         g_return_val_if_fail (account, FALSE);
269
270         account_iface  = TNY_ACCOUNT_IFACE(account);
271         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
272         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
273
274         account_name   = tny_account_iface_get_id(account_iface);
275         if (!account_name) {
276                 g_warning ("failed to retrieve account name");
277                 return FALSE;
278         }
279
280         hostname =  tny_account_iface_get_hostname(account_iface);
281         username =  tny_account_iface_get_user(account_iface);
282         proto    =  tny_account_iface_get_proto(account_iface);
283
284         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
285                                                       account_name,
286                                                       hostname, username, NULL,
287                                                       proto);
288 }
289
290
291
292 static void
293 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
294                                              TnyStoreAccountIface *account)
295 {
296         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
297                 g_warning ("failed to add store account");
298 }
299
300
301
302 static void
303 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
304                                                  TnyTransportAccountIface *account)
305 {
306         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
307                 g_warning ("failed to add transport account");
308 }
309
310
311
312 static TnyAccountIface*
313 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
314                       gboolean is_store)
315 {
316         TnyAccountIface *tny_account;
317         ModestTnyAccountStorePrivate *priv;
318         gchar *val;
319
320         g_return_val_if_fail (self, NULL);
321         g_return_val_if_fail (key, NULL);
322
323         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
324
325         /* is it a store or a transport? */
326         if (is_store)
327                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
328         else
329                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
330
331         if (!tny_account) {
332                 g_warning ("failed to create new tny %s account",
333                            is_store ? "store" : "transport");
334                 return NULL;
335         }
336
337         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
338                                              TNY_ACCOUNT_STORE_IFACE(self));
339         /* id */
340         tny_account_iface_set_id (tny_account, key);
341         tny_account_iface_set_name (tny_account, key);
342         
343         /* proto */
344         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
345                                                             MODEST_ACCOUNT_PROTO, NULL);
346         if (val) {
347                 tny_account_iface_set_proto (tny_account, val);
348                 g_free (val);
349         } else {
350                 g_warning ("protocol not defined for %s", key);
351                 g_object_unref (G_OBJECT(tny_account));
352                 return NULL;
353         }
354
355         /* hostname */
356         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
357                                                             MODEST_ACCOUNT_HOSTNAME,
358                                                             NULL);
359         if (val) {
360                 tny_account_iface_set_hostname (tny_account, val);
361                 g_free (val);
362         }
363
364
365         /* username */
366         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
367                                                             MODEST_ACCOUNT_USERNAME,
368                                                             NULL);
369         if (val) {
370                 tny_account_iface_set_user (tny_account, val);
371                 g_free (val);
372         }
373
374         tny_account_iface_set_pass_func (tny_account, get_password);
375         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
376
377         return tny_account;
378 }
379
380
381 static GList*
382 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
383                                    gboolean is_store)
384 {
385         GSList *cursor = accounts;
386         GList *tny_accounts = NULL;
387
388         g_return_val_if_fail (self, NULL);
389
390         while (cursor) {
391                 TnyAccountIface *tny_account;
392                 gchar *key = cursor->data;
393                 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
394                                                     is_store);
395                 if (!tny_account) {
396                         g_warning ("could not create tnyaccount for %s",
397                                    (gchar*)cursor->data);
398                 } else {
399                         tny_accounts =
400                                 g_list_append (tny_accounts, tny_account);
401                 }
402                 cursor = cursor->next;
403         }
404
405         return tny_accounts;
406 }
407
408
409
410 static const GList*
411 modest_tny_account_store_get_store_accounts  (TnyAccountStoreIface *iface)
412 {
413         ModestTnyAccountStore        *self;
414         ModestTnyAccountStorePrivate *priv;
415         GSList                       *accounts, *cursor;
416         GList                        *tny_accounts;
417
418         g_return_val_if_fail (iface, NULL);
419
420         self = MODEST_TNY_ACCOUNT_STORE(iface);
421         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
422
423         accounts =
424                 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
425                                                          NULL,
426                                                          MODEST_PROTO_TYPE_STORE,
427                                                          NULL, FALSE);
428
429         tny_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
430         g_slist_free (accounts);
431         
432         /*
433          * FIXME: after gconf notification support is added,
434          * we can simply return priv->store_account
435          */
436         priv->store_accounts = free_gobject_list (priv->store_accounts);
437         priv->store_accounts = tny_accounts;
438
439         return tny_accounts;
440 }
441
442
443
444 static const GList*
445 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
446 {
447         ModestTnyAccountStore        *self;
448         ModestTnyAccountStorePrivate *priv;
449         GSList                       *accounts, *cursor;
450         GList                        *tny_accounts;
451
452         g_return_val_if_fail (iface, NULL);
453
454         self = MODEST_TNY_ACCOUNT_STORE(iface);
455         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
456
457         accounts =
458                 modest_account_mgr_server_account_names (priv->modest_acc_mgr,
459                                                          NULL,
460                                                          MODEST_PROTO_TYPE_TRANSPORT,
461                                                          NULL, FALSE);
462         tny_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
463         g_warning ("transport accounts: %d", g_list_length (tny_accounts));
464
465         g_slist_free (accounts);
466
467         /*
468          * FIXME: after gconf notification support is added,
469          * we can simply return priv->store_account
470          */
471         priv->transport_accounts = free_gobject_list (priv->transport_accounts);
472         priv->transport_accounts = tny_accounts;
473
474         return tny_accounts; /* FIXME: who will free this? */
475 }
476
477
478 ModestAccountMgr
479 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
480 {
481         ModestTnyAccountStorePrivate *priv;
482         g_return_val_if_fail (self, NULL);
483
484         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
485
486         return priv->modest_acc_mgr;
487 }
488
489
490
491 TnySessionCamel*
492 tny_account_store_get_session (TnyAccountStore *self)
493 {
494         ModestTnyAccountStorePrivate *priv;
495         g_return_val_if_fail (self, NULL);
496
497         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
498
499         return priv->tny_session_camel;
500 }
501
502
503
504 /**
505  * modest_tny_account_store_get_cache_dir:
506  * @self: self a TnyAccountStoreIface instance
507  *
508  * returns the pathname of the cache directory
509  *
510  * Returns: a string with the value of the pathname
511  * to the cache directory or NULL if the environment variable $HOME is
512  * not set. string should _not_ be freed by caller
513  */
514 static const gchar*
515 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
516 {
517         ModestTnyAccountStorePrivate *priv;
518         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
519
520         gchar *cache_dir;
521
522         if (!priv->cache_dir) {
523                 if (g_getenv("HOME") != NULL)
524                         priv->cache_dir = g_strconcat(g_getenv("HOME"),
525                                                       "/.modest/cache/", NULL);
526         }
527         return priv->cache_dir;
528 }
529
530
531
532 static const TnyDeviceIface*
533 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
534 {
535         ModestTnyAccountStorePrivate *priv;
536
537         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
538
539         return priv->device;
540 }
541
542
543
544 static void
545 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
546 {
547         TnyAccountStoreIfaceClass *klass;
548
549         g_return_if_fail (g_iface);
550
551         klass = (TnyAccountStoreIfaceClass *)g_iface;
552
553         klass->add_store_account_func      =
554                 modest_tny_account_store_add_store_account;
555         klass->get_store_accounts_func     =
556                 modest_tny_account_store_get_store_accounts;
557         klass->add_transport_account_func  =
558                 modest_tny_account_store_add_transport_account;
559         klass->get_transport_accounts_func =
560                 modest_tny_account_store_get_transport_accounts;
561         klass->get_cache_dir_func =
562                 modest_tny_account_store_get_cache_dir;
563         klass->get_device_func =
564                 modest_tny_account_store_get_device;
565
566 }