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