* cleanup for -Wall: unused variables removed, some return types fixed, includes...
[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-device.h>
13 #include <tny-account-store.h>
14
15 #include <tny-store-account.h>
16 #include <tny-transport-account.h>
17
18 #include "modest-account-mgr.h"
19 #include "modest-tny-account-store.h"
20
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);
25
26 /* implementations for tny-account-store-iface */
27 static void    modest_tny_account_store_iface_init              (gpointer g_iface, gpointer iface_data);
28
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);
35
36 /* list my signals */
37 enum {
38         PASSWORD_REQUESTED_SIGNAL,
39         LAST_SIGNAL
40 };
41
42 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
43 struct _ModestTnyAccountStorePrivate {
44
45         GList *store_accounts;
46         GList *transport_accounts;
47         gchar *cache_dir;
48
49         TnySessionCamel *tny_session_camel;
50         TnyDeviceIface  *device;
51
52         ModestAccountMgr *modest_acc_mgr;
53 };
54 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
56                                                       ModestTnyAccountStorePrivate))
57 /* globals */
58 static GObjectClass *parent_class = NULL;
59
60 static guint signals[LAST_SIGNAL] = {0};
61
62 GType
63 modest_tny_account_store_get_type (void)
64 {
65         static GType my_type = 0;
66         if (!my_type) {
67                 static const GTypeInfo my_info = {
68                         sizeof(ModestTnyAccountStoreClass),
69                         NULL,           /* base init */
70                         NULL,           /* base finalize */
71                         (GClassInitFunc) modest_tny_account_store_class_init,
72                         NULL,           /* class finalize */
73                         NULL,           /* class data */
74                         sizeof(ModestTnyAccountStore),
75                         1,              /* n_preallocs */
76                         (GInstanceInitFunc) modest_tny_account_store_init,
77                 };
78
79                 static const GInterfaceInfo iface_info = {
80                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
81                         NULL,         /* interface_finalize */
82                         NULL          /* interface_data */
83                 };
84                 /* hack hack */
85                 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
86                                                   "ModestTnyAccountStore", &my_info, 0);
87
88                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
89                                              &iface_info);
90         }
91         return my_type;
92 }
93
94 static void
95 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
96 {
97         GObjectClass *gobject_class;
98         gobject_class = (GObjectClass*) klass;
99
100         parent_class            = g_type_class_peek_parent (klass);
101         gobject_class->finalize = modest_tny_account_store_finalize;
102
103         g_type_class_add_private (gobject_class,
104                                   sizeof(ModestTnyAccountStorePrivate));
105
106         signals[PASSWORD_REQUESTED_SIGNAL] =
107                 g_signal_new ("password_requested",
108                               G_TYPE_FROM_CLASS (gobject_class),
109                               G_SIGNAL_RUN_FIRST,
110                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass,password_requested),
111                               NULL, NULL,
112                               g_cclosure_marshal_VOID__POINTER,
113                               G_TYPE_NONE, 1, G_TYPE_POINTER);
114 }
115
116 static void
117 modest_tny_account_store_init (ModestTnyAccountStore *obj)
118 {
119         ModestTnyAccountStorePrivate *priv =
120                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
121
122         priv->modest_acc_mgr         = NULL;
123         priv->device                 = NULL;
124
125         priv->store_accounts         = NULL;
126         priv->transport_accounts     = NULL;
127         priv->cache_dir              = NULL;
128
129         priv->tny_session_camel      = NULL;
130 }
131
132
133 static void
134 free_gobject (GObject *obj, gpointer user_data)
135 {
136         if (obj)
137                 g_object_unref (obj);
138 }
139
140
141 static GList*
142 free_gobject_list (GList *list)
143 {
144         if (list) {
145                 g_list_foreach (list, (GFunc)free_gobject, NULL);
146                 g_list_free (list);
147         }
148         return NULL;
149 }
150
151
152 static void
153 modest_tny_account_store_finalize (GObject *obj)
154 {
155         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
156         ModestTnyAccountStorePrivate *priv =
157                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
158
159         if (priv->modest_acc_mgr) {
160                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
161                 priv->modest_acc_mgr = NULL;
162         }
163
164         if (priv->tny_session_camel) {
165                 g_object_unref (G_OBJECT(priv->tny_session_camel));
166                 priv->tny_session_camel = NULL;
167         }
168
169         if (priv->device) {
170                 g_object_unref (G_OBJECT(priv->device));
171                 priv->device = NULL;
172         }
173
174         priv->store_accounts     = free_gobject_list (priv->store_accounts);
175         priv->transport_accounts = free_gobject_list (priv->store_accounts);
176
177         g_free (priv->cache_dir);
178         priv->cache_dir = NULL;
179
180
181
182 }
183
184 GObject*
185 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
186 {
187         GObject *obj;
188         ModestTnyAccountStorePrivate *priv;
189
190         g_return_val_if_fail (modest_acc_mgr, NULL);
191
192         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
193
194         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
195         g_object_ref(G_OBJECT(priv->modest_acc_mgr = modest_acc_mgr));
196
197         priv->device = (TnyDeviceIface*)tny_device_new();
198         if (!priv->device) {
199                 g_warning ("Cannot create Device instance");
200                 g_object_unref (obj);
201                 return NULL;
202         }
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);
208                 return NULL;
209         }
210
211         return obj;
212 }
213
214
215 static gchar*
216 get_password (TnyAccountIface *account, const gchar *prompt)
217 {
218         const gchar *key;
219         const TnyAccountStoreIface *account_store;
220         ModestTnyAccountStore *self;
221         ModestTnyAccountStorePrivate *priv;
222         gchar *val;
223
224         g_return_val_if_fail (account, NULL);
225
226         key = tny_account_iface_get_id (account);
227         account_store = tny_account_iface_get_account_store(account);
228
229         self = MODEST_TNY_ACCOUNT_STORE (account_store);
230         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
231
232         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
233                                                             MODEST_ACCOUNT_PASSWORD,
234                                                             NULL);
235         if (!val)
236         {
237                 /* FIXME:
238                  * append the prompt to the emitted signal,
239                  * so the password dialog shows the prompt supplied by the caller of this function.
240                  */
241                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
242                                key);
243
244         }
245
246         return val;
247 }
248
249
250 static void
251 forget_password (TnyAccountIface *account)
252 {
253         g_warning (__FUNCTION__);
254 }
255
256
257
258 static gboolean
259 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account)
260 {
261         TnyAccountIface       *account_iface;
262         ModestTnyAccountStore *account_store;
263         ModestTnyAccountStorePrivate *priv;
264
265         const gchar* account_name;
266         const gchar *hostname, *username, *proto;
267
268         g_return_val_if_fail (self, FALSE);
269         g_return_val_if_fail (account, FALSE);
270
271         account_iface  = TNY_ACCOUNT_IFACE(account);
272         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
273         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
274
275         account_name   = tny_account_iface_get_id(account_iface);
276         if (!account_name) {
277                 g_warning ("failed to retrieve account name");
278                 return FALSE;
279         }
280
281         hostname =  tny_account_iface_get_hostname(account_iface);
282         username =  tny_account_iface_get_user(account_iface);
283         proto    =  tny_account_iface_get_proto(account_iface);
284
285         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
286                                                       account_name,
287                                                       hostname, username, NULL,
288                                                       proto);
289 }
290
291
292
293 static void
294 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
295                                              TnyStoreAccountIface *account)
296 {
297         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
298                 g_warning ("failed to add store account");
299 }
300
301
302
303 static void
304 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
305                                                  TnyTransportAccountIface *account)
306 {
307         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
308                 g_warning ("failed to add transport account");
309 }
310
311
312
313 static TnyAccountIface*
314 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
315                       gboolean is_store)
316 {
317         TnyAccountIface *tny_account;
318         ModestTnyAccountStorePrivate *priv;
319         gchar *val;
320
321         g_return_val_if_fail (self, NULL);
322         g_return_val_if_fail (key, NULL);
323
324         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
325
326         /* is it a store or a transport? */
327         if (is_store)
328                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
329         else
330                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
331
332         if (!tny_account) {
333                 g_warning ("failed to create new tny %s account",
334                            is_store ? "store" : "transport");
335                 return NULL;
336         }
337
338         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
339                                              TNY_ACCOUNT_STORE_IFACE(self));
340         /* id */
341         tny_account_iface_set_id (tny_account, key);
342         tny_account_iface_set_name (tny_account, key);
343         
344         /* proto */
345         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
346                                                             MODEST_ACCOUNT_PROTO, NULL);
347         if (val) {
348                 tny_account_iface_set_proto (tny_account, val);
349                 g_free (val);
350         } else {
351                 g_warning ("protocol not defined for %s", key);
352                 g_object_unref (G_OBJECT(tny_account));
353                 return NULL;
354         }
355
356         /* hostname */
357         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
358                                                             MODEST_ACCOUNT_HOSTNAME,
359                                                             NULL);
360         if (val) {
361                 tny_account_iface_set_hostname (tny_account, val);
362                 g_free (val);
363         }
364
365
366         /* username */
367         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
368                                                             MODEST_ACCOUNT_USERNAME,
369                                                             NULL);
370         if (val) {
371                 tny_account_iface_set_user (tny_account, val);
372                 g_free (val);
373         }
374
375         tny_account_iface_set_pass_func (tny_account, get_password);
376         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
377
378         return tny_account;
379 }
380
381
382 static GList*
383 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
384                                    gboolean is_store)
385 {
386         GSList *cursor = accounts;
387         GList *tny_accounts = NULL;
388
389         g_return_val_if_fail (self, NULL);
390
391         while (cursor) {
392                 TnyAccountIface *tny_account;
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;
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;
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         if (!priv->cache_dir) {
521                 if (g_getenv("HOME") != NULL)
522                         priv->cache_dir = g_strconcat(g_getenv("HOME"),
523                                                       "/.modest/cache/", NULL);
524         }
525         return priv->cache_dir;
526 }
527
528
529
530 static const TnyDeviceIface*
531 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
532 {
533         ModestTnyAccountStorePrivate *priv;
534
535         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
536
537         return priv->device;
538 }
539
540
541
542 static void
543 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
544 {
545         TnyAccountStoreIfaceClass *klass;
546
547         g_return_if_fail (g_iface);
548
549         klass = (TnyAccountStoreIfaceClass *)g_iface;
550
551         klass->add_store_account_func      =
552                 modest_tny_account_store_add_store_account;
553         klass->get_store_accounts_func     =
554                 modest_tny_account_store_get_store_accounts;
555         klass->add_transport_account_func  =
556                 modest_tny_account_store_add_transport_account;
557         klass->get_transport_accounts_func =
558                 modest_tny_account_store_get_transport_accounts;
559         klass->get_cache_dir_func =
560                 modest_tny_account_store_get_cache_dir;
561         klass->get_device_func =
562                 modest_tny_account_store_get_device;
563
564 }