1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include <glib/gi18n.h>
33 #include <tny-account.h>
34 #include <tny-account-store.h>
35 #include <tny-store-account.h>
36 #include <tny-transport-account.h>
37 #include <tny-device.h>
38 #include <tny-account-store.h>
39 #include <tny-camel-transport-account.h>
40 #include <tny-camel-imap-store-account.h>
41 #include <tny-camel-pop-store-account.h>
42 #include <modest-marshal.h>
43 #include <modest-protocol-info.h>
44 #include <modest-local-folder-info.h>
45 #include "modest-account-mgr.h"
46 #include "modest-tny-account-store.h"
47 #include "modest-tny-platform-factory.h"
48 #include <tny-gtk-lockable.h>
49 #include <camel/camel.h>
51 /* 'private'/'protected' functions */
52 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
53 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
54 static void modest_tny_account_store_finalize (GObject *obj);
56 /* implementations for tny-account-store-iface */
57 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
59 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
60 static void modest_tny_account_store_add_store_account (TnyAccountStore *self,
61 TnyStoreAccount *account);
62 static void modest_tny_account_store_add_transport_account (TnyAccountStore *self,
63 TnyTransportAccount *account);
64 static void modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
65 TnyGetAccountsRequestType type);
68 PASSWORD_REQUESTED_SIGNAL,
69 ACCOUNT_UPDATE_SIGNAL,
73 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
74 struct _ModestTnyAccountStorePrivate {
78 GHashTable *password_hash;
80 TnyPlatformFactory *platform_fact;
81 TnySessionCamel *tny_session_camel;
84 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
85 MODEST_TYPE_TNY_ACCOUNT_STORE, \
86 ModestTnyAccountStorePrivate))
88 static GObjectClass *parent_class = NULL;
90 static guint signals[LAST_SIGNAL] = {0};
93 modest_tny_account_store_get_type (void)
95 static GType my_type = 0;
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),
107 (GInstanceInitFunc) modest_tny_account_store_instance_init,
111 static const GInterfaceInfo iface_info = {
112 (GInterfaceInitFunc) modest_tny_account_store_init,
113 NULL, /* interface_finalize */
114 NULL /* interface_data */
117 my_type = g_type_register_static (G_TYPE_OBJECT,
118 "ModestTnyAccountStore",
120 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
127 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
129 GObjectClass *gobject_class;
130 gobject_class = (GObjectClass*) klass;
132 parent_class = g_type_class_peek_parent (klass);
133 gobject_class->finalize = modest_tny_account_store_finalize;
135 g_type_class_add_private (gobject_class,
136 sizeof(ModestTnyAccountStorePrivate));
138 signals[PASSWORD_REQUESTED_SIGNAL] =
139 g_signal_new ("password_requested",
140 G_TYPE_FROM_CLASS (gobject_class),
142 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
144 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
145 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
147 signals[ACCOUNT_UPDATE_SIGNAL] =
148 g_signal_new ("account_update",
149 G_TYPE_FROM_CLASS (gobject_class),
151 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
153 g_cclosure_marshal_VOID__STRING,
154 G_TYPE_NONE, 1, G_TYPE_STRING);
159 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
161 ModestTnyAccountStorePrivate *priv =
162 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
164 priv->cache_dir = NULL;
166 priv->platform_fact = NULL;
167 priv->tny_session_camel = NULL;
170 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
176 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
179 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
181 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
188 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
189 const gchar *key, gpointer user_data)
191 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
193 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
198 static ModestTnyAccountStore*
199 get_account_store_for_account (TnyAccount *account)
201 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
207 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
209 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
213 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
216 ModestAccountMgr *account_mgr;
217 const TnyAccountStore *account_store;
218 ModestTnyAccountStore *self;
219 ModestTnyAccountStorePrivate *priv;
221 gboolean already_asked;
223 key = tny_account_get_id (account);
224 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
226 self = MODEST_TNY_ACCOUNT_STORE (account_store);
227 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
229 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
230 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
232 /* is it in the hash? if it's already there, it must be wrong... */
233 already_asked = g_hash_table_lookup_extended (priv->password_hash,
234 key, NULL, (gpointer *) &pwd);
236 /* if the password is not already there, try ModestConf */
237 if (!already_asked) {
238 pwd = modest_account_mgr_get_string (account_mgr,
239 key, MODEST_ACCOUNT_PASSWORD,
241 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
244 /* if it was already asked, it must have been wrong, so ask again */
245 if (already_asked || !pwd || strlen(pwd) == 0) {
247 /* we don't have it yet. we emit a signal to get the password somewhere */
248 const gchar* name = tny_account_get_name (account);
252 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
253 name, &pwd, cancel, &remember);
257 modest_account_mgr_set_string (account_mgr,
258 key, MODEST_ACCOUNT_PASSWORD,
261 /* We need to dup the string even knowing that
262 it's already a dup of the contents of an
263 entry, because it if it's wrong, then camel
265 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
267 g_hash_table_remove (priv->password_hash, key);
279 forget_password (TnyAccount *account) {
281 ModestTnyAccountStore *self;
282 ModestTnyAccountStorePrivate *priv;
283 const TnyAccountStore *account_store;
286 ModestAccountMgr *account_mgr;
288 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
289 self = MODEST_TNY_ACCOUNT_STORE (account_store);
290 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
291 key = tny_account_get_id (account);
293 /* Do not remove the key, this will allow us to detect that we
294 have already asked for it at least once */
295 pwd = g_hash_table_lookup (priv->password_hash, key);
297 memset (pwd, 0, strlen (pwd));
298 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
301 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
302 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
304 /* Remove from configuration system */
305 modest_account_mgr_unset (account_mgr,
306 key, MODEST_ACCOUNT_PASSWORD,
312 /* instantiate the correct tny account subclass */
314 tny_account_for_proto (ModestProtocol proto)
316 ModestProtocolType type;
317 TnyAccount *tny_account = NULL;
319 type = modest_protocol_info_get_protocol_type (proto);
321 if (type == MODEST_PROTOCOL_TYPE_TRANSPORT)
322 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
323 else if (proto == MODEST_PROTOCOL_STORE_POP)
324 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
325 else if (proto == MODEST_PROTOCOL_STORE_IMAP)
326 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
328 g_return_val_if_reached (NULL);
331 tny_account_set_proto (tny_account,
332 modest_protocol_info_get_protocol_name(proto));
334 g_printerr ("modest: could not get tny account for %d\n",
340 /* create a tnyaccount for the server account connected to the account with name 'key'
343 get_tny_account_from_server_account (ModestTnyAccountStore *self,
344 ModestServerAccountData *account_data,
345 ModestProtocolType modest_type)
347 TnyAccount *tny_account;
348 ModestTnyAccountStorePrivate *priv;
350 g_return_val_if_fail (self, NULL);
351 g_return_val_if_fail (account_data, NULL);
353 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
356 tny_account = tny_account_for_proto (account_data->proto);
358 g_printerr ("modest: could not create tny account for '%s'\n",
359 account_data->account_name);
363 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
364 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), /* session */
365 priv->tny_session_camel);
366 tny_account_set_id (tny_account, account_data->account_name); /* id */
368 if (account_data->hostname)
369 tny_account_set_hostname (tny_account, account_data->hostname);
371 if (account_data->username)
372 tny_account_set_user (tny_account, account_data->username);
374 tny_account_set_pass_func (tny_account, get_password);
375 tny_account_set_forget_pass_func (tny_account, forget_password);
383 modest_tny_account_store_finalize (GObject *obj)
385 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
386 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
388 if (priv->tny_session_camel) {
389 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
390 priv->tny_session_camel = NULL;
393 g_free (priv->cache_dir);
394 priv->cache_dir = NULL;
397 g_object_unref (priv->device);
401 if (priv->password_hash) {
402 g_hash_table_destroy (priv->password_hash);
403 priv->password_hash = NULL;
406 G_OBJECT_CLASS(parent_class)->finalize (obj);
410 ModestTnyAccountStore*
411 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
414 ModestTnyAccountStorePrivate *priv;
415 TnyPlatformFactory *pfact;
417 g_return_val_if_fail (account_mgr, NULL);
419 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
420 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
422 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
423 G_CALLBACK (on_account_changed), obj);
424 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
425 G_CALLBACK (on_account_removed), obj);
427 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
429 g_printerr ("modest: cannot get platform factory instance\n");
430 g_object_unref (obj);
433 priv->platform_fact = pfact;
435 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
436 if (!priv->tny_session_camel) {
437 g_printerr ("modest: cannot create TnySessionCamel instance\n");
438 g_object_unref (obj);
442 tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
444 return MODEST_TNY_ACCOUNT_STORE(obj);
449 add_account (TnyAccountStore *self, TnyAccount *account) {
451 ModestTnyAccountStore *account_store;
452 ModestTnyAccountStorePrivate *priv;
453 ModestAccountMgr *account_mgr;
454 const gchar *account_name;
455 const gchar *hostname, *username, *proto;
457 g_return_val_if_fail (self, FALSE);
458 g_return_val_if_fail (account, FALSE);
460 account_store = MODEST_TNY_ACCOUNT_STORE(self);
461 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
462 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
463 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
466 account_name = tny_account_get_id(account);
468 g_printerr ("modest: failed to retrieve account name\n");
472 hostname = tny_account_get_hostname(account);
473 username = tny_account_get_user(account);
474 proto = tny_account_get_proto(account);
476 return modest_account_mgr_add_server_account (account_mgr,
478 hostname, username, NULL,
479 modest_protocol_info_get_protocol(proto));
484 modest_tny_account_store_add_store_account (TnyAccountStore *self,
485 TnyStoreAccount *account)
487 ModestTnyAccountStorePrivate *priv;
489 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
490 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
491 priv->tny_session_camel);
493 if (!add_account (self, TNY_ACCOUNT(account)))
494 g_printerr ("modest: failed to add store account\n");
499 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
500 TnyTransportAccount *account)
502 ModestTnyAccountStorePrivate *priv;
504 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
505 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(account),
506 priv->tny_session_camel);
507 if (!add_account (self, TNY_ACCOUNT(account)))
508 g_printerr ("modest: failed to add transport account\n");
512 /* create a pseudo-account for our local folders */
514 get_local_folder_account (ModestTnyAccountStore *self)
516 TnyStoreAccount *tny_account;
518 gchar *maildir, *url_string;
519 ModestTnyAccountStorePrivate *priv;
521 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
523 tny_account = tny_camel_store_account_new ();
525 g_printerr ("modest: cannot create account for local folders");
529 maildir = modest_local_folder_info_get_maildir_path ();
531 url = camel_url_new ("maildir:", NULL);
532 camel_url_set_path (url, maildir);
534 url_string = camel_url_to_string (url, 0);
535 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
537 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
538 priv->tny_session_camel);
540 tny_account_set_name (TNY_ACCOUNT(tny_account), _("Local folders"));
542 camel_url_free (url);
546 return TNY_ACCOUNT(tny_account);
551 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
552 TnyGetAccountsRequestType type)
554 TnyAccount *tny_account = NULL;
555 ModestServerAccountData *server_account;
557 if (type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS && account_data->store_account)
558 server_account = account_data->store_account;
559 else if (type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS && account_data->transport_account)
560 server_account = account_data->transport_account;
562 g_return_val_if_reached (NULL);
564 if (!server_account) {
565 g_printerr ("modest: no %s account defined for '%s'\n",
566 type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS ? "store" : "transport",
567 account_data->display_name);
571 tny_account = get_tny_account_from_server_account (self, server_account, type);
573 g_printerr ("modest: failed to create tny account for %s\n",
574 account_data->account_name);
578 if (account_data->display_name)
579 tny_account_set_name (tny_account, account_data->display_name);
586 modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
587 TnyGetAccountsRequestType type)
589 ModestTnyAccountStore *self;
590 ModestTnyAccountStorePrivate *priv;
591 GSList *accounts, *cursor;
592 ModestAccountMgr *account_mgr;
593 TnyAccount *local_folder_account;
595 g_return_if_fail (iface);
596 g_return_if_fail (TNY_IS_LIST(list));
598 self = MODEST_TNY_ACCOUNT_STORE(iface);
599 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
600 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
601 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
603 if (type == TNY_ACCOUNT_STORE_BOTH) {
604 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
605 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
608 accounts = modest_account_mgr_account_names (account_mgr, NULL);
609 for (cursor = accounts; cursor; cursor = cursor->next) {
610 TnyAccount *tny_account;
611 ModestAccountData *account_data =
612 modest_account_mgr_get_account_data (account_mgr,
613 (gchar*)cursor->data);
614 if (account_data && account_data->enabled) {
615 tny_account = get_tny_account_from_account (self, account_data, type);
617 tny_list_prepend (list, G_OBJECT(tny_account));
619 g_free (cursor->data);
620 modest_account_mgr_free_account_data (account_mgr, account_data);
622 g_slist_free (accounts);
624 /* also, add the local folder pseudo-account */
625 local_folder_account = get_local_folder_account (MODEST_TNY_ACCOUNT_STORE(self));
626 if (!local_folder_account)
627 g_printerr ("modest: failed to add local folders account\n");
629 tny_list_prepend (list, G_OBJECT(local_folder_account));
631 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
636 * the cache dir will be ~/.modest/cache
637 * might want to change this in a simple #define...
640 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
642 ModestTnyAccountStorePrivate *priv;
643 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
645 if (!priv->cache_dir)
646 priv->cache_dir = g_build_filename (g_get_home_dir(), ".modest",
648 return priv->cache_dir;
653 * callers need to unref
656 modest_tny_account_store_get_device (TnyAccountStore *self)
658 ModestTnyAccountStorePrivate *priv;
660 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
663 priv->device = tny_platform_factory_new_device (priv->platform_fact);
665 return g_object_ref (G_OBJECT(priv->device));
671 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
674 g_printerr ("modest: alert [%d]: %s", type, prompt);
680 modest_tny_account_store_init (gpointer g, gpointer iface_data)
682 TnyAccountStoreIface *klass;
684 g_return_if_fail (g);
686 klass = (TnyAccountStoreIface *)g;
688 klass->get_accounts_func =
689 modest_tny_account_store_get_accounts;
690 klass->add_transport_account_func =
691 modest_tny_account_store_add_transport_account;
692 klass->add_store_account_func =
693 modest_tny_account_store_add_store_account;
694 klass->get_cache_dir_func =
695 modest_tny_account_store_get_cache_dir;
696 klass->get_device_func =
697 modest_tny_account_store_get_device;
699 modest_tny_account_store_alert;
703 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
704 ModestTnyGetPassFunc func)
706 g_message (__FUNCTION__);
707 return; /* not implemented, we use signals */
713 tny_account_store_get_session (TnyAccountStore *self)
715 g_return_val_if_fail (self, NULL);
716 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;