* small cleanups
[modest] / src / modest-tny-account-store.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31
32 #include <tny-account-iface.h>
33 #include <tny-account-store-iface.h>
34 #include <tny-store-account-iface.h>
35 #include <tny-transport-account-iface.h>
36 #include <tny-device-iface.h>
37 #include <tny-device.h>
38 #include <tny-account-store.h>
39
40 #include <tny-store-account.h>
41 #include <tny-transport-account.h>
42
43 #include "modest-account-mgr.h"
44 #include "modest-tny-account-store.h"
45
46 /* 'private'/'protected' functions */
47 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
48 static void modest_tny_account_store_init         (ModestTnyAccountStore *obj);
49 static void modest_tny_account_store_finalize     (GObject *obj);
50
51 /* implementations for tny-account-store-iface */
52 static void    modest_tny_account_store_iface_init              (gpointer g_iface, gpointer iface_data);
53 static void    modest_tny_account_store_add_store_account       (TnyAccountStoreIface *self,
54                                                                  TnyStoreAccountIface *account);
55 static void    modest_tny_account_store_add_transport_account   (TnyAccountStoreIface *self,
56                                                                  TnyTransportAccountIface *account);
57 static void    modest_tny_account_store_get_accounts            (TnyAccountStoreIface *iface, TnyListIface *list,
58                                                                  TnyGetAccountsRequestType type);
59
60 /* list my signals */
61 enum {
62         PASSWORD_REQUESTED_SIGNAL,
63         UPDATE_ACCOUNTS_SIGNAL,
64         LAST_SIGNAL
65 };
66
67 /* Password Status */
68 enum {
69         PW_NOT_INVALID,
70         PW_INVALID
71 };
72
73 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
74 struct _ModestTnyAccountStorePrivate {
75
76         GMutex *store_lock;     
77         gchar *cache_dir;
78
79         TnySessionCamel *tny_session_camel;
80         TnyDeviceIface  *device;
81
82         ModestAccountMgr *modest_acc_mgr;
83         gint pw_invalid;
84         ModestTnyGetPassFunc get_pass_func;
85 };
86 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
87                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
88                                                       ModestTnyAccountStorePrivate))
89 /* globals */
90 static GObjectClass *parent_class = NULL;
91
92 static guint signals[LAST_SIGNAL] = {0};
93
94 GType
95 modest_tny_account_store_get_type (void)
96 {
97         static GType my_type = 0;
98         if (!my_type) {
99                 static const GTypeInfo my_info = {
100                         sizeof(ModestTnyAccountStoreClass),
101                         NULL,           /* base init */
102                         NULL,           /* base finalize */
103                         (GClassInitFunc) modest_tny_account_store_class_init,
104                         NULL,           /* class finalize */
105                         NULL,           /* class data */
106                         sizeof(ModestTnyAccountStore),
107                         1,              /* n_preallocs */
108                         (GInstanceInitFunc) modest_tny_account_store_init,
109                 };
110
111                 static const GInterfaceInfo iface_info = {
112                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
113                         NULL,         /* interface_finalize */
114                         NULL          /* interface_data */
115                 };
116                 /* hack hack */
117                 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
118                                                   "ModestTnyAccountStore", &my_info, 0);
119
120                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
121                                              &iface_info);
122         }
123         return my_type;
124 }
125
126 static void
127 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
128 {
129         GObjectClass *gobject_class;
130         gobject_class = (GObjectClass*) klass;
131
132         parent_class            = g_type_class_peek_parent (klass);
133         gobject_class->finalize = modest_tny_account_store_finalize;
134
135         g_type_class_add_private (gobject_class,
136                                   sizeof(ModestTnyAccountStorePrivate));
137
138         signals[PASSWORD_REQUESTED_SIGNAL] =
139                 g_signal_new ("password_requested",
140                               G_TYPE_FROM_CLASS (gobject_class),
141                               G_SIGNAL_RUN_FIRST,
142                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
143                               NULL, NULL,
144                               g_cclosure_marshal_VOID__STRING,
145                               G_TYPE_NONE, 1, G_TYPE_STRING);
146
147         signals[UPDATE_ACCOUNTS_SIGNAL] =
148                 g_signal_new ("update_accounts",
149                               G_TYPE_FROM_CLASS (gobject_class),
150                               G_SIGNAL_RUN_FIRST,
151                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, update_accounts),
152                               NULL, NULL,
153                               g_cclosure_marshal_VOID__STRING,
154                               G_TYPE_NONE, 1, G_TYPE_STRING);
155         
156 }
157
158 static void
159 modest_tny_account_store_init (ModestTnyAccountStore *obj)
160 {
161         ModestTnyAccountStorePrivate *priv =
162                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
163
164         priv->modest_acc_mgr         = NULL;
165         priv->device                 = NULL;
166         priv->cache_dir              = NULL;
167
168         priv->tny_session_camel      = NULL;
169         /* Meaning: if not indicated otherwise, we have valid password data */
170         priv->pw_invalid             = PW_NOT_INVALID;
171         priv->get_pass_func          = NULL;
172 }
173
174
175 static void
176 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
177                     gpointer user_data)
178 {
179         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
180
181         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
182                        account);
183         
184 }
185
186
187 static void
188 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
189                     const gchar *key, gpointer user_data)
190 {
191         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
192         
193         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
194                        account);
195 }
196
197
198
199 static gchar*
200 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
201 {
202         
203         const gchar *key;
204         const TnyAccountStoreIface *account_store;
205         ModestTnyAccountStore *self;
206         ModestTnyAccountStorePrivate *priv;
207         gchar *retval;
208
209         g_return_val_if_fail (account, NULL);
210
211         key = tny_account_iface_get_id (account);
212         account_store = tny_account_iface_get_account_store(account);
213
214         self = MODEST_TNY_ACCOUNT_STORE (account_store);
215         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
216
217         if (priv->pw_invalid==PW_NOT_INVALID) {
218                 retval = modest_account_mgr_get_string (priv->modest_acc_mgr,
219                                                         key,
220                                                         MODEST_ACCOUNT_PASSWORD,
221                                                         TRUE,
222                                                         NULL);
223         } else {
224                 retval = priv->get_pass_func(account, prompt, cancel);
225                 if (!*cancel) {
226                         priv->pw_invalid=PW_NOT_INVALID;
227                         modest_account_mgr_set_string(priv->modest_acc_mgr,
228                                                       key,
229                                                       MODEST_ACCOUNT_PASSWORD,
230                                                       retval, TRUE, 
231                                                       NULL);
232                 }
233         }
234         return retval;
235 }
236
237
238 static void
239 forget_password (TnyAccountIface *account) {
240
241         ModestTnyAccountStore *self;
242         ModestTnyAccountStorePrivate *priv;
243         const TnyAccountStoreIface *account_store;
244
245         account_store = tny_account_iface_get_account_store(account);
246         self = MODEST_TNY_ACCOUNT_STORE (account_store);
247         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
248
249         priv->pw_invalid=PW_INVALID;
250 }
251
252
253
254 static TnyAccountIface*
255 tny_account_from_name (ModestTnyAccountStore *self, const gchar *key, ModestProtoType modest_type)
256 {
257         TnyAccountIface *tny_account;
258         ModestTnyAccountStorePrivate *priv;
259         gchar *val;
260
261         g_return_val_if_fail (self, NULL);
262         g_return_val_if_fail (key, NULL);
263
264         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
265
266         /* is it a store or a transport? */
267         if  (modest_type == MODEST_PROTO_TYPE_STORE)
268                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
269         else if (modest_type == MODEST_PROTO_TYPE_TRANSPORT)
270                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
271         else
272                 g_assert_not_reached ();
273
274         if (!tny_account) {
275                 g_printerr ("modest: failed to create new tny account for '%s'\n",
276                             key);
277                 return NULL;
278         }
279         
280         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
281                                              TNY_ACCOUNT_STORE_IFACE(self));
282         /* id */
283         tny_account_iface_set_id (tny_account, key);
284         tny_account_iface_set_name (tny_account, key);
285
286         /* proto */
287         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
288                                              MODEST_ACCOUNT_PROTO, TRUE, NULL);
289         if (val) {
290                 tny_account_iface_set_proto (tny_account, val);
291                 g_free (val);
292         } else {
293                 g_printerr ("modest: protocol not defined for '%s'\n", key);
294                 g_object_unref (G_OBJECT(tny_account));
295                 return NULL;
296         }
297
298         /* hostname */
299         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
300                                              MODEST_ACCOUNT_HOSTNAME, TRUE,
301                                              NULL);
302         if (val) {
303                 tny_account_iface_set_hostname (tny_account, val);
304                 g_free (val);
305         }
306
307
308         /* username */
309         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
310                                              MODEST_ACCOUNT_USERNAME, TRUE,
311                                              NULL);
312         if (val) {
313                 tny_account_iface_set_user (tny_account, val);
314                 g_free (val);
315         }
316
317         tny_account_iface_set_pass_func (tny_account, get_password);
318         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
319
320         return tny_account;
321 }
322
323
324
325 static void
326 modest_tny_account_store_finalize (GObject *obj)
327 {
328         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
329         ModestTnyAccountStorePrivate *priv =
330                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
331
332         if (priv->modest_acc_mgr) {
333                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
334                 priv->modest_acc_mgr = NULL;
335         }
336
337         if (priv->tny_session_camel) {
338                 g_object_unref (G_OBJECT(priv->tny_session_camel));
339                 priv->tny_session_camel = NULL;
340         }
341
342         if (priv->device) {
343                 g_object_unref (G_OBJECT(priv->device));
344                 priv->device = NULL;
345         }
346
347         g_mutex_free (priv->store_lock);
348
349         g_free (priv->cache_dir);
350         priv->cache_dir = NULL;
351 }
352
353
354 ModestTnyAccountStore*
355 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr) {
356
357         GObject *obj;
358         ModestTnyAccountStorePrivate *priv;
359
360         g_return_val_if_fail (modest_acc_mgr, NULL);
361
362         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
363
364         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
365
366         g_object_ref(G_OBJECT(modest_acc_mgr));
367         priv->modest_acc_mgr = modest_acc_mgr;
368
369         g_signal_connect (G_OBJECT(modest_acc_mgr), "account_changed",
370                           G_CALLBACK (on_account_changed), obj);
371         g_signal_connect (G_OBJECT(modest_acc_mgr), "account_removed",
372                           G_CALLBACK (on_account_removed), obj);
373
374         priv->store_lock = g_mutex_new ();
375
376         priv->device = (TnyDeviceIface*)tny_device_new();
377         if (!priv->device) {
378                 g_printerr ("modest: cannot create device instance\n");
379                 g_object_unref (obj);
380                 return NULL;
381         }
382         
383         priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE_IFACE(obj));
384
385         if (!priv->tny_session_camel) {
386                 g_printerr ("modest: cannot create TnySessionCamel instance\n");
387                 g_object_unref (obj);
388                 return NULL;
389         }
390
391         return MODEST_TNY_ACCOUNT_STORE(obj);
392 }
393
394
395 static gboolean
396 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account) {
397
398         TnyAccountIface       *account_iface;
399         ModestTnyAccountStore *account_store;
400         ModestTnyAccountStorePrivate *priv;
401
402         const gchar *account_name;
403         const gchar *hostname, *username, *proto;
404
405         g_return_val_if_fail (self, FALSE);
406         g_return_val_if_fail (account, FALSE);
407
408         account_iface  = TNY_ACCOUNT_IFACE(account);
409         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
410         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
411
412         account_name   = tny_account_iface_get_id(account_iface);
413         if (!account_name) {
414                 g_printerr ("modest: failed to retrieve account name\n");
415                 return FALSE;
416         }
417
418         hostname =  tny_account_iface_get_hostname(account_iface);
419         username =  tny_account_iface_get_user(account_iface);
420         proto    =  tny_account_iface_get_proto(account_iface);
421
422         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
423                                                       account_name,
424                                                       hostname, username, NULL,
425                                                       proto);
426 }
427
428
429 static void
430 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
431                                              TnyStoreAccountIface *account)
432 {
433         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
434                 g_printerr ("modest: failed to add store account\n");
435 }
436
437
438 static void
439 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
440                                                  TnyTransportAccountIface *account)
441 {
442         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
443                 g_printerr ("modest: failed to add transport account\n");
444 }
445
446
447 static void
448 modest_tny_account_store_get_accounts  (TnyAccountStoreIface *iface,
449                                         TnyListIface *list,
450                                         TnyGetAccountsRequestType type)
451 {
452         ModestTnyAccountStore        *self;
453         ModestTnyAccountStorePrivate *priv;
454         GSList                       *accounts, *cursor;
455         ModestProtoType              modest_type;
456         
457         g_return_if_fail (iface);
458
459         self = MODEST_TNY_ACCOUNT_STORE(iface);
460         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
461
462         switch (type) {
463         case TNY_ACCOUNT_STORE_IFACE_TRANSPORT_ACCOUNTS:
464                 modest_type = MODEST_PROTO_TYPE_TRANSPORT;
465                 break;
466         case TNY_ACCOUNT_STORE_IFACE_STORE_ACCOUNTS:
467                 modest_type = MODEST_PROTO_TYPE_STORE;
468                 break;
469         case TNY_ACCOUNT_STORE_IFACE_BOTH:
470                 modest_type = MODEST_PROTO_TYPE_ANY;
471                 break;
472         default:
473                 g_assert_not_reached ();
474         }
475
476         accounts = modest_account_mgr_search_server_accounts (priv->modest_acc_mgr,
477                                                               NULL, modest_type,
478                                                               NULL, TRUE);
479         cursor = accounts;
480         while (cursor) {
481                 gchar           *account_name;
482                 TnyAccountIface *account_iface;
483
484                 account_name =  (gchar*)cursor->data;
485                 account_iface = tny_account_from_name (self, account_name, modest_type);
486                 
487                 if (!account_iface)
488                         g_printerr ("modest: failed to create account iface for '%s'\n",
489                                     account_name);
490                 else
491                         tny_list_iface_prepend (list, account_iface);
492
493                 g_free (account_name);
494                 cursor = cursor->next;
495         }
496
497         g_slist_free (accounts);
498
499         tny_session_camel_set_current_accounts (priv->tny_session_camel,
500                                                 list);
501 }
502
503
504 static const gchar*
505 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
506 {
507         ModestTnyAccountStorePrivate *priv;
508         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
509
510         if (!priv->cache_dir)
511                 priv->cache_dir = g_build_filename (g_get_home_dir(),
512                                                     ".modest", "cache", NULL);
513         return priv->cache_dir;
514 }
515
516
517 static const TnyDeviceIface*
518 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
519 {
520         ModestTnyAccountStorePrivate *priv;
521
522         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
523
524         return priv->device;
525 }
526
527
528 static void
529 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
530 {
531         TnyAccountStoreIfaceClass *klass;
532
533         g_return_if_fail (g_iface);
534
535         klass = (TnyAccountStoreIfaceClass *)g_iface;
536
537         klass->get_accounts_func =
538                 modest_tny_account_store_get_accounts;
539         klass->add_transport_account_func  =
540                 modest_tny_account_store_add_transport_account;
541         klass->get_cache_dir_func =
542                 modest_tny_account_store_get_cache_dir;
543         klass->get_device_func =
544                 modest_tny_account_store_get_device;
545 }
546
547 void
548 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
549                                             ModestTnyGetPassFunc func)
550 {
551         ModestTnyAccountStorePrivate *priv;
552
553         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
554
555         priv->get_pass_func=func;
556 }
557
558
559 TnySessionCamel*
560 tny_account_store_get_session    (TnyAccountStore *self)
561 {
562         ModestTnyAccountStorePrivate *priv;
563
564         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
565
566         return priv->tny_session_camel;
567 }