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