* ModestTnyFolderTreeView => ModestFolderView
[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 /* list my signals */
60 enum {
61         PASSWORD_REQUESTED_SIGNAL,
62         UPDATE_ACCOUNTS_SIGNAL,
63         LAST_SIGNAL
64 };
65
66 /* Password Status */
67 enum {
68         PW_NOT_INVALID,
69         PW_INVALID
70 };
71
72 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
73 struct _ModestTnyAccountStorePrivate {
74
75         GMutex *store_lock;     
76         gchar *cache_dir;
77
78         TnySessionCamel *tny_session_camel;
79         TnyDeviceIface  *device;
80
81         ModestAccountMgr *modest_acc_mgr;
82         gint pw_invalid;
83         ModestTnyGetPassFunc get_pass_func;
84 };
85 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
86                                                       MODEST_TYPE_TNY_ACCOUNT_STORE, \
87                                                       ModestTnyAccountStorePrivate))
88 /* globals */
89 static GObjectClass *parent_class = NULL;
90
91 static guint signals[LAST_SIGNAL] = {0};
92
93 GType
94 modest_tny_account_store_get_type (void)
95 {
96         static GType my_type = 0;
97         if (!my_type) {
98                 static const GTypeInfo my_info = {
99                         sizeof(ModestTnyAccountStoreClass),
100                         NULL,           /* base init */
101                         NULL,           /* base finalize */
102                         (GClassInitFunc) modest_tny_account_store_class_init,
103                         NULL,           /* class finalize */
104                         NULL,           /* class data */
105                         sizeof(ModestTnyAccountStore),
106                         1,              /* n_preallocs */
107                         (GInstanceInitFunc) modest_tny_account_store_init,
108                 };
109
110                 static const GInterfaceInfo iface_info = {
111                         (GInterfaceInitFunc) modest_tny_account_store_iface_init,
112                         NULL,         /* interface_finalize */
113                         NULL          /* interface_data */
114                 };
115                 /* hack hack */
116                 my_type = g_type_register_static (TNY_TYPE_ACCOUNT_STORE,
117                                                   "ModestTnyAccountStore", &my_info, 0);
118
119                 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE_IFACE,
120                                              &iface_info);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_tny_account_store_finalize;
133
134         g_type_class_add_private (gobject_class,
135                                   sizeof(ModestTnyAccountStorePrivate));
136
137         signals[PASSWORD_REQUESTED_SIGNAL] =
138                 g_signal_new ("password_requested",
139                               G_TYPE_FROM_CLASS (gobject_class),
140                               G_SIGNAL_RUN_FIRST,
141                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
142                               NULL, NULL,
143                               g_cclosure_marshal_VOID__STRING,
144                               G_TYPE_NONE, 1, G_TYPE_STRING);
145
146         signals[UPDATE_ACCOUNTS_SIGNAL] =
147                 g_signal_new ("update_accounts",
148                               G_TYPE_FROM_CLASS (gobject_class),
149                               G_SIGNAL_RUN_FIRST,
150                               G_STRUCT_OFFSET(ModestTnyAccountStoreClass, update_accounts),
151                               NULL, NULL,
152                               g_cclosure_marshal_VOID__STRING,
153                               G_TYPE_NONE, 1, G_TYPE_STRING);
154         
155 }
156
157 static void
158 modest_tny_account_store_init (ModestTnyAccountStore *obj)
159 {
160         ModestTnyAccountStorePrivate *priv =
161                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
162
163         priv->modest_acc_mgr         = NULL;
164         priv->device                 = NULL;
165         priv->cache_dir              = NULL;
166
167         priv->tny_session_camel      = NULL;
168         /* Meaning: if not indicated otherwise, we have valid password data */
169         priv->pw_invalid             = PW_NOT_INVALID;
170         priv->get_pass_func          = NULL;
171 }
172
173
174 static void
175 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
176                     gpointer user_data)
177 {
178         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
179
180         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
181                        account);
182         
183 }
184
185
186 static void
187 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
188                     const gchar *key, gpointer user_data)
189 {
190         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
191         
192         g_signal_emit (G_OBJECT(self), signals[UPDATE_ACCOUNTS_SIGNAL], 0,
193                        account);
194 }
195
196
197
198 static gchar*
199 get_password (TnyAccountIface *account, const gchar *prompt, gboolean *cancel)
200 {
201         
202         const gchar *key;
203         const TnyAccountStoreIface *account_store;
204         ModestTnyAccountStore *self;
205         ModestTnyAccountStorePrivate *priv;
206         gchar *retval;
207
208         g_return_val_if_fail (account, NULL);
209
210         key = tny_account_iface_get_id (account);
211         account_store = tny_account_iface_get_account_store(account);
212
213         self = MODEST_TNY_ACCOUNT_STORE (account_store);
214         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
215
216         if (priv->pw_invalid==PW_NOT_INVALID) {
217                 retval = modest_account_mgr_get_string (priv->modest_acc_mgr,
218                                                         key,
219                                                         MODEST_ACCOUNT_PASSWORD,
220                                                         TRUE,
221                                                         NULL);
222         } else {
223                 retval = priv->get_pass_func(account, prompt, cancel);
224                 if (!*cancel) {
225                         priv->pw_invalid=PW_NOT_INVALID;
226                         modest_account_mgr_set_string(priv->modest_acc_mgr,
227                                                       key,
228                                                       MODEST_ACCOUNT_PASSWORD,
229                                                       retval, TRUE, 
230                                                       NULL);
231                 }
232         }
233         return retval;
234 }
235
236
237 static void
238 forget_password (TnyAccountIface *account) {
239
240         ModestTnyAccountStore *self;
241         ModestTnyAccountStorePrivate *priv;
242         const TnyAccountStoreIface *account_store;
243
244         account_store = tny_account_iface_get_account_store(account);
245         self = MODEST_TNY_ACCOUNT_STORE (account_store);
246         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
247
248         priv->pw_invalid=PW_INVALID;
249 }
250
251
252
253 static TnyAccountIface*
254 tny_account_from_name (ModestTnyAccountStore *self, const gchar *key, ModestProtoType modest_type)
255 {
256         TnyAccountIface *tny_account;
257         ModestTnyAccountStorePrivate *priv;
258         gchar *val;
259
260         g_return_val_if_fail (self, NULL);
261         g_return_val_if_fail (key, NULL);
262
263         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
264
265         /* is it a store or a transport? */
266         if  (modest_type == MODEST_PROTO_TYPE_STORE)
267                 tny_account = TNY_ACCOUNT_IFACE(tny_store_account_new ());
268         else if (modest_type == MODEST_PROTO_TYPE_TRANSPORT)
269                 tny_account = TNY_ACCOUNT_IFACE(tny_transport_account_new ());
270         else
271                 g_assert_not_reached ();
272
273         if (!tny_account) {
274                 g_printerr ("modest: failed to create new tny account for '%s'\n",
275                             key);
276                 return NULL;
277         }
278         
279         tny_account_iface_set_account_store (TNY_ACCOUNT_IFACE(tny_account),
280                                              TNY_ACCOUNT_STORE_IFACE(self));
281         /* id */
282         tny_account_iface_set_id (tny_account, key);
283         tny_account_iface_set_name (tny_account, key);
284
285         /* proto */
286         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
287                                              MODEST_ACCOUNT_PROTO, TRUE, NULL);
288         if (val) {
289                 tny_account_iface_set_proto (tny_account, val);
290                 g_free (val);
291         } else {
292                 g_printerr ("modest: protocol not defined for '%s'\n", key);
293                 g_object_unref (G_OBJECT(tny_account));
294                 return NULL;
295         }
296
297         /* hostname */
298         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
299                                              MODEST_ACCOUNT_HOSTNAME, TRUE,
300                                              NULL);
301         if (val) {
302                 tny_account_iface_set_hostname (tny_account, val);
303                 g_free (val);
304         }
305
306
307         /* username */
308         val = modest_account_mgr_get_string (priv->modest_acc_mgr, key,
309                                              MODEST_ACCOUNT_USERNAME, TRUE,
310                                              NULL);
311         if (val) {
312                 tny_account_iface_set_user (tny_account, val);
313                 g_free (val);
314         }
315
316         tny_account_iface_set_pass_func (tny_account, get_password);
317         tny_account_iface_set_forget_pass_func (tny_account, forget_password);
318
319         return tny_account;
320 }
321
322
323
324 static void
325 modest_tny_account_store_finalize (GObject *obj)
326 {
327         ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
328         ModestTnyAccountStorePrivate *priv =
329                 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
330
331         if (priv->modest_acc_mgr) {
332                 g_object_unref (G_OBJECT(priv->modest_acc_mgr));
333                 priv->modest_acc_mgr = NULL;
334         }
335
336         
337         if (priv->tny_session_camel) {
338 // FIXME: how to kill a camel
339                 //g_object_unref (G_OBJECT(priv->tny_session_camel));
340                 priv->tny_session_camel = NULL;
341         }
342
343         if (priv->device) {
344                 g_object_unref (G_OBJECT(priv->device));
345                 priv->device = NULL;
346         }
347
348         if (priv->store_lock)
349                 g_mutex_free (priv->store_lock);
350
351         g_free (priv->cache_dir);
352         priv->cache_dir = NULL;
353 }
354
355
356 ModestTnyAccountStore*
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 MODEST_TNY_ACCOUNT_STORE(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 gboolean
531 modest_tny_account_store_alert (TnyAccountStoreIface *self, TnyAlertType type,
532                                 const gchar *prompt)
533 {
534         return TRUE; /* FIXME: implement this */
535 }
536
537
538 static void
539 modest_tny_account_store_iface_init (gpointer g_iface, gpointer iface_data)
540 {
541         TnyAccountStoreIfaceClass *klass;
542
543         g_return_if_fail (g_iface);
544
545         klass = (TnyAccountStoreIfaceClass *)g_iface;
546
547         klass->get_accounts_func =
548                 modest_tny_account_store_get_accounts;
549         klass->add_transport_account_func  =
550                 modest_tny_account_store_add_transport_account;
551         klass->add_store_account_func =
552                 modest_tny_account_store_add_store_account;
553         klass->get_cache_dir_func =
554                 modest_tny_account_store_get_cache_dir;
555         klass->get_device_func =
556                 modest_tny_account_store_get_device;
557         klass->alert_func =
558                 modest_tny_account_store_alert;
559 }
560
561 void
562 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
563                                             ModestTnyGetPassFunc func)
564 {
565         ModestTnyAccountStorePrivate *priv;
566
567         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
568
569         priv->get_pass_func=func;
570 }
571
572
573 TnySessionCamel*
574 tny_account_store_get_session    (TnyAccountStore *self)
575 {
576         ModestTnyAccountStorePrivate *priv;
577
578         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
579
580         return priv->tny_session_camel;
581 }