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