* Disconnect signals from account manager if account store is destroyed.
[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 manager_new_account (ModestAccountMgr *modest_acc_mgr, gchar *name, gpointer data)
154 {
155         g_print ("new account signal %s\n", name);
156 }
157
158
159 static void 
160 manager_remove_account (ModestAccountMgr *modest_acc_mgr,gchar *name, gpointer data)
161 {
162         g_print ("remove account signal %s\n", name);
163 }
164
165
166 static void 
167 manager_change_account (ModestAccountMgr *modest_acc_mgr, gchar *accountname, 
168         gchar *key, gchar* value, gpointer data)
169 {
170         g_print ("account change signal: account: %s key: %s value: %s\n", accountname, key, value);
171 }
172
173
174 static void
175 modest_tny_account_store_finalize (GObject *obj)
176 {
177         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
178         ModestTnyAccountStorePrivate *priv =
179                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
180
181         g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr), 
182                 G_CALLBACK(manager_new_account), NULL);
183         g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr), 
184                 G_CALLBACK(manager_remove_account), NULL);
185         g_signal_handlers_disconnect_by_func (G_OBJECT (priv->modest_acc_mgr), 
186                 G_CALLBACK(manager_change_account), NULL);
187                 
188         if (priv->modest_acc_mgr) {
189                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
190                 priv->modest_acc_mgr = NULL;
191         }
192
193         if (priv->tny_session_camel) {
194                 g_object_unref (G_OBJECT(priv->tny_session_camel));
195                 priv->tny_session_camel = NULL;
196         }
197
198         if (priv->device) {
199                 g_object_unref (G_OBJECT(priv->device));
200                 priv->device = NULL;
201         }
202
203         priv->store_accounts     = free_gobject_list (priv->store_accounts);
204         priv->transport_accounts = free_gobject_list (priv->store_accounts);
205
206         g_free (priv->cache_dir);
207         priv->cache_dir = NULL;
208
209
210 }
211
212
213 GObject*
214 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr)
215 {
216         GObject *obj;
217         ModestTnyAccountStorePrivate *priv;
218
219         g_return_val_if_fail (modest_acc_mgr, NULL);
220
221         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
222
223         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
224         
225         g_object_ref(G_OBJECT(modest_acc_mgr));
226         priv->modest_acc_mgr = modest_acc_mgr;
227         
228         priv->device = (TnyDeviceIface*)tny_device_new();
229         if (!priv->device) {
230                 g_warning ("Cannot create Device instance");
231                 g_object_unref (obj);
232                 return NULL;
233         }
234         priv->tny_session_camel = tny_session_camel_new
235                 (TNY_ACCOUNT_STORE_IFACE(obj));
236         if (!priv->tny_session_camel) {
237                 g_warning ("Cannot create TnySessionCamel instance");
238                 g_object_unref (obj);
239                 return NULL;
240         }
241         
242         g_signal_connect (G_OBJECT (modest_acc_mgr), "account-add", 
243                 G_CALLBACK(manager_new_account), NULL);
244         g_signal_connect (G_OBJECT (modest_acc_mgr), "account-remove", 
245                 G_CALLBACK(manager_remove_account), NULL);
246         g_signal_connect (G_OBJECT (modest_acc_mgr), "account-change", 
247                 G_CALLBACK(manager_change_account), NULL);
248
249         return obj;
250 }
251
252
253 static gchar*
254 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
255 {
256         const gchar *key;
257         const TnyAccountStoreIface *account_store;
258         ModestTnyAccountStore *self;
259         ModestTnyAccountStorePrivate *priv;
260         gchar *val;
261
262         g_return_val_if_fail (account, NULL);
263
264         key = tny_account_iface_get_id (account);
265         account_store = tny_account_iface_get_account_store(account);
266
267         self = MODEST_TNY_ACCOUNT_STORE (account_store);
268         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
269
270         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
271                                                                                                 MODEST_ACCOUNT_PASSWORD, NULL);
272         if (!val) {
273                 /* FIXME:
274                  * append the prompt to the emitted signal,
275                  * so the password dialog shows the prompt supplied by the caller of this function.
276                  */
277                 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, key);
278         }
279
280         return val;
281 }
282
283
284 static void
285 forget_password (TnyAccountIface *account)
286 {
287         g_warning (__FUNCTION__);
288 }
289
290
291
292 static gboolean
293 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account)
294 {
295         TnyAccountIface       *account_iface;
296         ModestTnyAccountStore *account_store;
297         ModestTnyAccountStorePrivate *priv;
298
299         const gchar *account_name;
300         const gchar *hostname, *username, *proto;
301
302         g_return_val_if_fail (self, FALSE);
303         g_return_val_if_fail (account, FALSE);
304
305         account_iface  = TNY_ACCOUNT_IFACE(account);
306         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
307         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
308
309         account_name   = tny_account_iface_get_id(account_iface);
310         if (!account_name) {
311                 g_warning ("failed to retrieve account name");
312                 return FALSE;
313         }
314
315         hostname =  tny_account_iface_get_hostname(account_iface);
316         username =  tny_account_iface_get_user(account_iface);
317         proto    =  tny_account_iface_get_proto(account_iface);
318
319         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
320                                                       account_name,
321                                                       hostname, username, NULL,
322                                                       proto);
323 }
324
325
326 static void
327 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
328                                              TnyStoreAccountIface *account)
329 {
330         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
331                 g_warning ("failed to add store account");
332 }
333
334
335 static void
336 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
337                                                  TnyTransportAccountIface *account)
338 {
339         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
340                 g_warning ("failed to add transport account");
341 }
342
343
344 static TnyAccountIface*
345 tny_account_from_key (ModestTnyAccountStore *self, const gchar *key,
346                       gboolean is_store)
347 {
348         TnyAccountIface *tny_account;
349         ModestTnyAccountStorePrivate *priv;
350         gchar *val;
351
352         g_return_val_if_fail (self, NULL);
353         g_return_val_if_fail (key, NULL);
354
355         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
356
357         /* is it a store or a transport? */
358         if (is_store)
359                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
360         else
361                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
362
363         if (!tny_account) {
364                 g_warning ("failed to create new tny %s account",
365                            is_store ? "store" : "transport");
366                 return NULL;
367         }
368
369         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
370                                              TNY_ACCOUNT_STORE_IFACE(self));
371         /* id */
372         tny_account_iface_set_id (tny_account, key);
373         tny_account_iface_set_name (tny_account, key);
374         
375         /* proto */
376         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
377                                                             MODEST_ACCOUNT_PROTO, NULL);
378         if (val) {
379                 tny_account_iface_set_proto (tny_account, val);
380                 g_free (val);
381         } else {
382                 g_warning ("protocol not defined for %s", key);
383                 g_object_unref (G_OBJECT(tny_account));
384                 return NULL;
385         }
386
387         /* hostname */
388         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
389                                                             MODEST_ACCOUNT_HOSTNAME,
390                                                             NULL);
391         if (val) {
392                 tny_account_iface_set_hostname (tny_account, val);
393                 g_free (val);
394         }
395
396
397         /* username */
398         val = modest_account_mgr_get_server_account_string (priv->modest_acc_mgr, key,
399                                                             MODEST_ACCOUNT_USERNAME,
400                                                             NULL);
401         if (val) {
402                 tny_account_iface_set_user (tny_account, val);
403                 g_free (val);
404         }
405
406         tny_account_iface_set_pass_func (tny_account, get_password);
407         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
408
409         return tny_account;
410 }
411
412
413 static GList*
414 tny_accounts_from_server_accounts (ModestTnyAccountStore *self, GSList *accounts,
415                                    gboolean is_store)
416 {
417         GSList *cursor = accounts;
418         GList *tny_accounts = NULL;
419
420         g_return_val_if_fail (self, NULL);
421
422         while (cursor) {
423                 TnyAccountIface *tny_account;
424                 tny_account = tny_account_from_key (self, (gchar*)cursor->data,
425                                                     is_store);
426                 if (!tny_account) {
427                         g_warning ("could not create tnyaccount for %s",
428                                    (gchar*)cursor->data);
429                 } else {
430                         tny_accounts =
431                                 g_list_append (tny_accounts, tny_account);
432                 }
433                 cursor = cursor->next;
434         }
435
436         return tny_accounts;
437 }
438
439
440 static const GList*
441 modest_tny_account_store_get_store_accounts  (TnyAccountStoreIface *iface)
442 {
443         ModestTnyAccountStore        *self;
444         ModestTnyAccountStorePrivate *priv;
445         GSList                       *accounts;
446
447         g_return_val_if_fail (iface, NULL);
448
449         self = MODEST_TNY_ACCOUNT_STORE(iface);
450         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
451
452         if (!priv->store_accounts) {
453                 accounts =
454                         modest_account_mgr_server_account_names (priv->modest_acc_mgr,
455                                                                  NULL,
456                                                                  MODEST_PROTO_TYPE_STORE,
457                                                                  NULL, FALSE);
458
459                 priv->store_accounts = tny_accounts_from_server_accounts (self, accounts, TRUE);
460                 g_slist_free (accounts);
461         }       
462
463         return priv->store_accounts;
464 }
465
466
467 static const GList*
468 modest_tny_account_store_get_transport_accounts (TnyAccountStoreIface *iface)
469 {
470         ModestTnyAccountStore        *self;
471         ModestTnyAccountStorePrivate *priv;
472         GSList                       *accounts;
473
474         g_return_val_if_fail (iface, NULL);
475
476         self = MODEST_TNY_ACCOUNT_STORE(iface);
477         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
478
479         
480         if (!priv->transport_accounts) {
481                 accounts =
482                         modest_account_mgr_server_account_names (priv->modest_acc_mgr,
483                                                                  NULL,
484                                                                  MODEST_PROTO_TYPE_TRANSPORT,
485                                                                  NULL, FALSE);
486                 priv->transport_accounts = tny_accounts_from_server_accounts (self, accounts, FALSE);
487                 g_slist_free (accounts);
488         }
489
490
491         return priv->transport_accounts;
492 }
493
494
495 ModestAccountMgr
496 *modest_tny_account_store_get_accout_mgr(ModestTnyAccountStore *self)
497 {
498         ModestTnyAccountStorePrivate *priv;
499         g_return_val_if_fail (self, NULL);
500
501         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
502
503         return priv->modest_acc_mgr;
504 }
505
506
507 TnySessionCamel*
508 tny_account_store_get_session (TnyAccountStore *self)
509 {
510         ModestTnyAccountStorePrivate *priv;
511         g_return_val_if_fail (self, NULL);
512
513         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
514
515         return priv->tny_session_camel;
516 }
517
518
519 /**
520  * modest_tny_account_store_get_cache_dir:
521  * @self: self a TnyAccountStoreIface instance
522  *
523  * returns the pathname of the cache directory
524  *
525  * Returns: a string with the value of the pathname
526  * to the cache directory or NULL if the environment variable $HOME is
527  * not set. string should _not_ be freed by caller
528  */
529 static const gchar*
530 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
531 {
532         ModestTnyAccountStorePrivate *priv;
533         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
534
535         if (!priv->cache_dir) {
536                 if (g_getenv("HOME") != NULL)
537                         priv->cache_dir = g_strconcat(g_getenv("HOME"),
538                                                       "/.modest/cache/", NULL);
539         }
540         return priv->cache_dir;
541 }
542
543
544 static const TnyDeviceIface*
545 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
546 {
547         ModestTnyAccountStorePrivate *priv;
548
549         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
550
551         return priv->device;
552 }
553
554
555 static void
556 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
557 {
558         TnyAccountStoreIfaceClass *klass;
559
560         g_return_if_fail (g_iface);
561
562         klass = (TnyAccountStoreIfaceClass *)g_iface;
563
564         klass->add_store_account_func      =
565                 modest_tny_account_store_add_store_account;
566         klass->get_store_accounts_func     =
567                 modest_tny_account_store_get_store_accounts;
568         klass->add_transport_account_func  =
569                 modest_tny_account_store_add_transport_account;
570         klass->get_transport_accounts_func =
571                 modest_tny_account_store_get_transport_accounts;
572         klass->get_cache_dir_func =
573                 modest_tny_account_store_get_cache_dir;
574         klass->get_device_func =
575                 modest_tny_account_store_get_device;
576 }