* cosmetic
[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 static const gchar *transport_protocols[] = { "smtp", NULL };
74
75 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
76 struct _ModestTnyAccountStorePrivate {
77
78         GMutex *store_lock;     
79         gchar *cache_dir;
80
81         TnySessionCamel *tny_session_camel;
82         TnyDeviceIface  *device;
83
84         ModestAccountMgr *modest_acc_mgr;
85         gint pw_invalid;
86         ModestTnyGetPassFunc get_pass_func;
87 };
88 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
89                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
90                                                       ModestTnyAccountStorePrivate))
91 /* globals */
92 static GObjectClass *parent_class = NULL;
93
94 static guint signals[LAST_SIGNAL] = {0};
95
96 GType
97 modest_tny_account_store_get_type (void)
98 {
99         static GType my_type = 0;
100         if (!my_type) {
101                 static const GTypeInfo my_info = {
102                         sizeof(ModestTnyAccountStoreClass),
103                         NULL,           /* base init */
104                         NULL,           /* base finalize */
105                         (GClassInitFunc) modest_tny_account_store_class_init,
106                         NULL,           /* class finalize */
107                         NULL,           /* class data */
108                         sizeof(ModestTnyAccountStore),
109                         1,              /* n_preallocs */
110                         (GInstanceInitFunc) modest_tny_account_store_init,
111                 };
112
113                 static const GInterfaceInfo iface_info = {
114                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
115                         NULL,         /* interface_finalize */
116                         NULL          /* interface_data */
117                 };
118                 /* hack hack */
119                 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
120                                                   "ModestTnyAccountStore", &my_info, 0);
121
122                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
123                                              &iface_info);
124         }
125         return my_type;
126 }
127
128 static void
129 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
130 {
131         GObjectClass *gobject_class;
132         gobject_class = (GObjectClass*) klass;
133
134         parent_class            = g_type_class_peek_parent (klass);
135         gobject_class->finalize = modest_tny_account_store_finalize;
136
137         g_type_class_add_private (gobject_class,
138                                   sizeof(ModestTnyAccountStorePrivate));
139
140         signals[PASSWORD_REQUESTED_SIGNAL] =
141                 g_signal_new ("password_requested",
142                               G_TYPE_FROM_CLASS (gobject_class),
143                               G_SIGNAL_RUN_FIRST,
144                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
145                               NULL, NULL,
146                               g_cclosure_marshal_VOID__STRING,
147                               G_TYPE_NONE, 1, G_TYPE_STRING);
148
149         signals[UPDATE_ACCOUNTS_SIGNAL] =
150                 g_signal_new ("update_accounts",
151                               G_TYPE_FROM_CLASS (gobject_class),
152                               G_SIGNAL_RUN_FIRST,
153                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, update_accounts),
154                               NULL, NULL,
155                               g_cclosure_marshal_VOID__STRING,
156                               G_TYPE_NONE, 1, G_TYPE_STRING);
157         
158 }
159
160 static void
161 modest_tny_account_store_init (ModestTnyAccountStore *obj)
162 {
163         ModestTnyAccountStorePrivate *priv =
164                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
165
166         priv->modest_acc_mgr         = NULL;
167         priv->device                 = NULL;
168         priv->cache_dir              = NULL;
169
170         priv->tny_session_camel      = NULL;
171         /* Meaning: if not indicated otherwise, we have valid password data */
172         priv->pw_invalid             = PW_NOT_INVALID;
173         priv->get_pass_func          = NULL;
174 }
175
176
177 static void
178 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
179                     gpointer user_data)
180 {
181         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
182
183         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
184                        account);
185         
186 }
187
188
189 static void
190 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
191                     const gchar *key, gpointer user_data)
192 {
193         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
194         
195         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
196                        account);
197 }
198
199
200
201 static gchar*
202 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
203 {
204         
205         const gchar *key;
206         const TnyAccountStoreIface *account_store;
207         ModestTnyAccountStore *self;
208         ModestTnyAccountStorePrivate *priv;
209         gchar *retval;
210
211         g_return_val_if_fail (account, NULL);
212
213         key = tny_account_iface_get_id (account);
214         account_store = tny_account_iface_get_account_store(account);
215
216         self = MODEST_TNY_ACCOUNT_STORE (account_store);
217         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
218
219         if (priv->pw_invalid==PW_NOT_INVALID) {
220                 retval = modest_account_mgr_get_string (priv->modest_acc_mgr,
221                                                         key,
222                                                         MODEST_ACCOUNT_PASSWORD,
223                                                         TRUE,
224                                                         NULL);
225         } else {
226                 retval = priv->get_pass_func(account, prompt, cancel);
227                 if (!*cancel) {
228                         priv->pw_invalid=PW_NOT_INVALID;
229                         modest_account_mgr_set_string(priv->modest_acc_mgr,
230                                                       key,
231                                                       MODEST_ACCOUNT_PASSWORD,
232                                                       retval, TRUE, 
233                                                       NULL);
234                 }
235         }
236         return retval;
237 }
238
239
240 static void
241 forget_password (TnyAccountIface *account) {
242
243         ModestTnyAccountStore *self;
244         ModestTnyAccountStorePrivate *priv;
245         const TnyAccountStoreIface *account_store;
246
247         account_store = tny_account_iface_get_account_store(account);
248         self = MODEST_TNY_ACCOUNT_STORE (account_store);
249         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
250
251         priv->pw_invalid=PW_INVALID;
252 }
253
254
255
256 static TnyAccountIface*
257 tny_account_from_name (ModestTnyAccountStore *self, const gchar *key, ModestProtoType modest_type)
258 {
259         TnyAccountIface *tny_account;
260         ModestTnyAccountStorePrivate *priv;
261         gchar *val;
262
263         g_return_val_if_fail (self, NULL);
264         g_return_val_if_fail (key, NULL);
265
266         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
267
268         /* is it a store or a transport? */
269         if  (modest_type == MODEST_PROTO_TYPE_STORE)
270                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
271         else if (modest_type == MODEST_PROTO_TYPE_TRANSPORT)
272                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
273         else
274                 g_assert_not_reached ();
275
276         if (!tny_account) {
277                 g_printerr ("modest: failed to create new tny account for '%s'\n",
278                             key);
279                 return NULL;
280         }
281         
282         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
283                                              TNY_ACCOUNT_STORE_IFACE(self));
284         /* id */
285         tny_account_iface_set_id (tny_account, key);
286         tny_account_iface_set_name (tny_account, key);
287
288         /* proto */
289         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
290                                              MODEST_ACCOUNT_PROTO, TRUE, NULL);
291         if (val) {
292                 tny_account_iface_set_proto (tny_account, val);
293                 g_free (val);
294         } else {
295                 g_printerr ("modest: protocol not defined for '%s'\n", key);
296                 g_object_unref (G_OBJECT(tny_account));
297                 return NULL;
298         }
299
300         /* hostname */
301         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
302                                              MODEST_ACCOUNT_HOSTNAME, TRUE,
303                                              NULL);
304         if (val) {
305                 tny_account_iface_set_hostname (tny_account, val);
306                 g_free (val);
307         }
308
309
310         /* username */
311         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
312                                              MODEST_ACCOUNT_USERNAME, TRUE,
313                                              NULL);
314         if (val) {
315                 tny_account_iface_set_user (tny_account, val);
316                 g_free (val);
317         }
318
319         tny_account_iface_set_pass_func (tny_account, get_password);
320         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
321
322         return tny_account;
323 }
324
325
326
327 static void
328 modest_tny_account_store_finalize (GObject *obj)
329 {
330         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
331         ModestTnyAccountStorePrivate *priv =
332                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
333
334         if (priv->modest_acc_mgr) {
335                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
336                 priv->modest_acc_mgr = NULL;
337         }
338
339         if (priv->tny_session_camel) {
340                 g_object_unref (G_OBJECT(priv->tny_session_camel));
341                 priv->tny_session_camel = NULL;
342         }
343
344         if (priv->device) {
345                 g_object_unref (G_OBJECT(priv->device));
346                 priv->device = NULL;
347         }
348
349         g_mutex_free (priv->store_lock);
350
351         g_free (priv->cache_dir);
352         priv->cache_dir = NULL;
353 }
354
355
356 GObject*
357 modest_tny_account_store_new (ModestAccountMgr *modest_acc_mgr) {
358
359         GObject *obj;
360         ModestTnyAccountStorePrivate *priv;
361
362         g_return_val_if_fail (modest_acc_mgr, NULL);
363
364         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
365
366         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
367
368         g_object_ref(G_OBJECT(modest_acc_mgr));
369         priv->modest_acc_mgr = modest_acc_mgr;
370
371         g_signal_connect (G_OBJECT(modest_acc_mgr), "account_changed",
372                           G_CALLBACK (on_account_changed), obj);
373         g_signal_connect (G_OBJECT(modest_acc_mgr), "account_removed",
374                           G_CALLBACK (on_account_removed), obj);
375
376         priv->store_lock = g_mutex_new ();
377
378         priv->device = (TnyDeviceIface*)tny_device_new();
379         if (!priv->device) {
380                 g_printerr ("modest: cannot create device instance\n");
381                 g_object_unref (obj);
382                 return NULL;
383         }
384         
385         priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE_IFACE(obj));
386
387         if (!priv->tny_session_camel) {
388                 g_printerr ("modest: cannot create TnySessionCamel instance\n");
389                 g_object_unref (obj);
390                 return NULL;
391         }
392
393         return obj;
394 }
395
396
397 static gboolean
398 add_account  (TnyAccountStoreIface *self, TnyAccountIface *account) {
399
400         TnyAccountIface       *account_iface;
401         ModestTnyAccountStore *account_store;
402         ModestTnyAccountStorePrivate *priv;
403
404         const gchar *account_name;
405         const gchar *hostname, *username, *proto;
406
407         g_return_val_if_fail (self, FALSE);
408         g_return_val_if_fail (account, FALSE);
409
410         account_iface  = TNY_ACCOUNT_IFACE(account);
411         account_store  = MODEST_TNY_ACCOUNT_STORE(self);
412         priv           = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
413
414         account_name   = tny_account_iface_get_id(account_iface);
415         if (!account_name) {
416                 g_printerr ("modest: failed to retrieve account name\n");
417                 return FALSE;
418         }
419
420         hostname =  tny_account_iface_get_hostname(account_iface);
421         username =  tny_account_iface_get_user(account_iface);
422         proto    =  tny_account_iface_get_proto(account_iface);
423
424         return modest_account_mgr_add_server_account (priv->modest_acc_mgr,
425                                                       account_name,
426                                                       hostname, username, NULL,
427                                                       proto);
428 }
429
430
431 static void
432 modest_tny_account_store_add_store_account  (TnyAccountStoreIface *self,
433                                              TnyStoreAccountIface *account)
434 {
435         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
436                 g_printerr ("modest: failed to add store account\n");
437 }
438
439
440 static void
441 modest_tny_account_store_add_transport_account  (TnyAccountStoreIface *self,
442                                                  TnyTransportAccountIface *account)
443 {
444         if (!add_account (self, TNY_ACCOUNT_IFACE(account)))
445                 g_printerr ("modest: failed to add transport account\n");
446 }
447
448
449 static void
450 modest_tny_account_store_get_accounts  (TnyAccountStoreIface *iface,
451                                         TnyListIface *list,
452                                         TnyGetAccountsRequestType type)
453 {
454         ModestTnyAccountStore        *self;
455         ModestTnyAccountStorePrivate *priv;
456         GSList                       *accounts, *cursor;
457         ModestProtoType              modest_type;
458         
459         g_return_if_fail (iface);
460
461         self = MODEST_TNY_ACCOUNT_STORE(iface);
462         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
463
464         switch (type) {
465         case TNY_ACCOUNT_STORE_IFACE_TRANSPORT_ACCOUNTS:
466                 modest_type = MODEST_PROTO_TYPE_TRANSPORT;
467                 break;
468         case TNY_ACCOUNT_STORE_IFACE_STORE_ACCOUNTS:
469                 modest_type = MODEST_PROTO_TYPE_STORE;
470                 break;
471         case TNY_ACCOUNT_STORE_IFACE_BOTH:
472                 modest_type = MODEST_PROTO_TYPE_ANY;
473                 break;
474         default:
475                 g_assert_not_reached ();
476         }
477
478         accounts = modest_account_mgr_search_server_accounts (priv->modest_acc_mgr,
479                                                               NULL, modest_type,
480                                                               NULL, TRUE);
481         cursor = accounts;
482         while (cursor) {
483                 gchar           *account_name;
484                 TnyAccountIface *account_iface;
485
486                 account_name =  (gchar*)cursor->data;
487                 account_iface = tny_account_from_name (self, account_name, modest_type);
488                 
489                 if (!account_iface)
490                         g_printerr ("modest: failed to create account iface for '%s'\n",
491                                     account_name);
492                 else
493                         tny_list_iface_prepend (list, account_iface);
494
495                 g_free (account_name);
496                 cursor = cursor->next;
497         }
498
499         g_slist_free (accounts);
500
501         tny_session_camel_set_current_accounts (priv->tny_session_camel,
502                                                 list);
503 }
504
505
506 static const gchar*
507 modest_tny_account_store_get_cache_dir (TnyAccountStoreIface *self)
508 {
509         ModestTnyAccountStorePrivate *priv;
510         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
511
512         if (!priv->cache_dir)
513                 priv->cache_dir = g_build_filename (g_get_home_dir(),
514                                                     ".modest", "cache", NULL);
515         return priv->cache_dir;
516 }
517
518
519 static const TnyDeviceIface*
520 modest_tny_account_store_get_device (TnyAccountStoreIface *self)
521 {
522         ModestTnyAccountStorePrivate *priv;
523
524         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
525
526         return priv->device;
527 }
528
529
530 static void
531 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
532 {
533         TnyAccountStoreIfaceClass *klass;
534
535         g_return_if_fail (g_iface);
536
537         klass = (TnyAccountStoreIfaceClass *)g_iface;
538
539         klass->get_accounts_func =
540                 modest_tny_account_store_get_accounts;
541         klass->add_transport_account_func  =
542                 modest_tny_account_store_add_transport_account;
543         klass->get_cache_dir_func =
544                 modest_tny_account_store_get_cache_dir;
545         klass->get_device_func =
546                 modest_tny_account_store_get_device;
547 }
548
549 void
550 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
551                                             ModestTnyGetPassFunc func)
552 {
553         ModestTnyAccountStorePrivate *priv;
554
555         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
556
557         priv->get_pass_func=func;
558 }
559
560
561 TnySessionCamel*
562 tny_account_store_get_session    (TnyAccountStore *self)
563 {
564         ModestTnyAccountStorePrivate *priv;
565
566         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
567
568         return priv->tny_session_camel;
569 }