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>
46 #include <modest-account-mgr.h>
47 #include <modest-account-mgr-helpers.h>
49 #include "modest-tny-account-store.h"
50 #include "modest-tny-platform-factory.h"
51 #include <tny-gtk-lockable.h>
52 #include <camel/camel.h>
54 /* 'private'/'protected' functions */
55 static void modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass);
56 //static void modest_tny_account_store_init (ModestTnyAccountStore *obj);
57 static void modest_tny_account_store_finalize (GObject *obj);
59 /* implementations for tny-account-store-iface */
60 static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj);
61 static void modest_tny_account_store_init (gpointer g, gpointer iface_data);
66 PASSWORD_REQUESTED_SIGNAL,
67 ACCOUNT_UPDATE_SIGNAL,
71 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
72 struct _ModestTnyAccountStorePrivate {
76 GHashTable *password_hash;
78 TnyPlatformFactory *platform_fact;
79 TnySessionCamel *tny_session_camel;
82 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
83 MODEST_TYPE_TNY_ACCOUNT_STORE, \
84 ModestTnyAccountStorePrivate))
86 static GObjectClass *parent_class = NULL;
88 static guint signals[LAST_SIGNAL] = {0};
91 modest_tny_account_store_get_type (void)
93 static GType my_type = 0;
96 static const GTypeInfo my_info = {
97 sizeof(ModestTnyAccountStoreClass),
99 NULL, /* base finalize */
100 (GClassInitFunc) modest_tny_account_store_class_init,
101 NULL, /* class finalize */
102 NULL, /* class data */
103 sizeof(ModestTnyAccountStore),
105 (GInstanceInitFunc) modest_tny_account_store_instance_init,
109 static const GInterfaceInfo iface_info = {
110 (GInterfaceInitFunc) modest_tny_account_store_init,
111 NULL, /* interface_finalize */
112 NULL /* interface_data */
115 my_type = g_type_register_static (G_TYPE_OBJECT,
116 "ModestTnyAccountStore",
118 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
125 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
127 GObjectClass *gobject_class;
128 gobject_class = (GObjectClass*) klass;
130 parent_class = g_type_class_peek_parent (klass);
131 gobject_class->finalize = modest_tny_account_store_finalize;
133 g_type_class_add_private (gobject_class,
134 sizeof(ModestTnyAccountStorePrivate));
136 signals[PASSWORD_REQUESTED_SIGNAL] =
137 g_signal_new ("password_requested",
138 G_TYPE_FROM_CLASS (gobject_class),
140 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
142 modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
143 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
145 signals[ACCOUNT_UPDATE_SIGNAL] =
146 g_signal_new ("account_update",
147 G_TYPE_FROM_CLASS (gobject_class),
149 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
151 g_cclosure_marshal_VOID__STRING,
152 G_TYPE_NONE, 1, G_TYPE_STRING);
157 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
159 ModestTnyAccountStorePrivate *priv =
160 MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
162 priv->cache_dir = NULL;
164 priv->platform_fact = NULL;
165 priv->tny_session_camel = NULL;
168 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
174 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
177 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
179 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
186 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
187 const gchar *key, gpointer user_data)
189 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
191 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
196 static ModestTnyAccountStore*
197 get_account_store_for_account (TnyAccount *account)
199 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
205 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
207 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
211 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
214 ModestAccountMgr *account_mgr;
215 const TnyAccountStore *account_store;
216 ModestTnyAccountStore *self;
217 ModestTnyAccountStorePrivate *priv;
220 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 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
234 * type-punned ptrs...*/
235 already_asked = g_hash_table_lookup_extended (priv->password_hash,
238 (gpointer*)&pwd_ptr);
240 /* if the password is not already there, try ModestConf */
241 if (!already_asked) {
242 pwd = modest_account_mgr_get_string (account_mgr,
243 key, MODEST_ACCOUNT_PASSWORD,
245 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
248 /* if it was already asked, it must have been wrong, so ask again */
249 if (already_asked || !pwd || strlen(pwd) == 0) {
251 /* we don't have it yet. we emit a signal to get the password somewhere */
252 const gchar* name = tny_account_get_name (account);
256 g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
257 name, &pwd, cancel, &remember);
261 modest_account_mgr_set_string (account_mgr,
262 key, MODEST_ACCOUNT_PASSWORD,
265 /* We need to dup the string even knowing that
266 it's already a dup of the contents of an
267 entry, because it if it's wrong, then camel
269 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
271 g_hash_table_remove (priv->password_hash, key);
283 forget_password (TnyAccount *account) {
285 ModestTnyAccountStore *self;
286 ModestTnyAccountStorePrivate *priv;
287 const TnyAccountStore *account_store;
290 ModestAccountMgr *account_mgr;
292 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
293 self = MODEST_TNY_ACCOUNT_STORE (account_store);
294 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
295 key = tny_account_get_id (account);
297 /* Do not remove the key, this will allow us to detect that we
298 have already asked for it at least once */
299 pwd = g_hash_table_lookup (priv->password_hash, key);
301 memset (pwd, 0, strlen (pwd));
302 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
305 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
306 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
308 /* Remove from configuration system */
309 modest_account_mgr_unset (account_mgr,
310 key, MODEST_ACCOUNT_PASSWORD,
316 /* instantiate the correct tny account subclass */
318 tny_account_for_proto (ModestProtocol proto)
320 ModestProtocolType type;
321 TnyAccount *tny_account = NULL;
323 type = modest_protocol_info_get_protocol_type (proto);
325 if (type == MODEST_PROTOCOL_TYPE_TRANSPORT)
326 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
327 else if (proto == MODEST_PROTOCOL_STORE_POP)
328 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
329 else if (proto == MODEST_PROTOCOL_STORE_IMAP)
330 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
332 g_return_val_if_reached (NULL);
335 tny_account_set_proto (tny_account,
336 modest_protocol_info_get_protocol_name(proto));
338 g_printerr ("modest: could not get tny account for %d\n",
344 /* create a tnyaccount for the server account connected to the account with name 'key'
347 get_tny_account_from_server_account (ModestTnyAccountStore *self,
348 ModestServerAccountData *account_data,
349 ModestProtocolType modest_type)
351 TnyAccount *tny_account;
352 ModestTnyAccountStorePrivate *priv;
354 g_return_val_if_fail (self, NULL);
355 g_return_val_if_fail (account_data, NULL);
357 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
360 if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
361 g_printerr ("modest: '%s' does not provide a protocol\n",
362 account_data->account_name);
366 tny_account = tny_account_for_proto (account_data->proto);
368 g_printerr ("modest: could not create tny account for '%s'\n",
369 account_data->account_name);
373 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
374 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), /* session */
375 priv->tny_session_camel);
376 tny_account_set_id (tny_account, account_data->account_name); /* id */
378 if (account_data->hostname)
379 tny_account_set_hostname (tny_account, account_data->hostname);
381 if (account_data->username)
382 tny_account_set_user (tny_account, account_data->username);
384 tny_account_set_pass_func (tny_account, get_password);
385 tny_account_set_forget_pass_func (tny_account, forget_password);
393 modest_tny_account_store_finalize (GObject *obj)
395 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
396 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
398 if (priv->tny_session_camel) {
399 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
400 priv->tny_session_camel = NULL;
403 g_free (priv->cache_dir);
404 priv->cache_dir = NULL;
407 g_object_unref (priv->device);
411 if (priv->password_hash) {
412 g_hash_table_destroy (priv->password_hash);
413 priv->password_hash = NULL;
416 G_OBJECT_CLASS(parent_class)->finalize (obj);
420 ModestTnyAccountStore*
421 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
424 ModestTnyAccountStorePrivate *priv;
425 TnyPlatformFactory *pfact;
427 g_return_val_if_fail (account_mgr, NULL);
429 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
430 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
432 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
433 G_CALLBACK (on_account_changed), obj);
434 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
435 G_CALLBACK (on_account_removed), obj);
437 pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
439 g_printerr ("modest: cannot get platform factory instance\n");
440 g_object_unref (obj);
443 priv->platform_fact = pfact;
445 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
446 if (!priv->tny_session_camel) {
447 g_printerr ("modest: cannot create TnySessionCamel instance\n");
448 g_object_unref (obj);
452 tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
454 return MODEST_TNY_ACCOUNT_STORE(obj);
458 modest_tny_account_store_add_store_account (TnyAccountStore *self,
459 TnyStoreAccount *account)
461 /* we should not need this...*/
462 g_printerr ("modest: add_store_account_func not implemented\n");
467 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
468 TnyTransportAccount *account)
470 /* we should not need this...*/
471 g_printerr ("modest: add_transport_account_func not implemented\n");
475 /* create a pseudo-account for our local folders */
477 get_local_folder_account (ModestTnyAccountStore *self)
479 TnyStoreAccount *tny_account;
481 gchar *maildir, *url_string;
482 ModestTnyAccountStorePrivate *priv;
484 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
486 tny_account = tny_camel_store_account_new ();
488 g_printerr ("modest: cannot create account for local folders");
492 maildir = modest_local_folder_info_get_maildir_path ();
494 url = camel_url_new ("maildir:", NULL);
495 camel_url_set_path (url, maildir);
497 url_string = camel_url_to_string (url, 0);
498 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
500 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
501 priv->tny_session_camel);
502 tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME);
503 tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME);
505 camel_url_free (url);
509 return TNY_ACCOUNT(tny_account);
514 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
515 TnyGetAccountsRequestType type)
517 TnyAccount *tny_account = NULL;
518 ModestServerAccountData *server_account = NULL;
520 if (type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS && account_data->store_account)
521 server_account = account_data->store_account;
522 else if (type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS && account_data->transport_account)
523 server_account = account_data->transport_account;
525 if (!server_account) {
526 g_printerr ("modest: no %s account defined for '%s'\n",
527 type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS ? "store" : "transport",
528 account_data->display_name);
532 tny_account = get_tny_account_from_server_account (self, server_account, type);
534 g_printerr ("modest: failed to create tny account for %s\n",
535 account_data->account_name);
539 if (account_data->display_name)
540 tny_account_set_name (tny_account, account_data->display_name);
547 modest_tny_account_store_get_accounts (TnyAccountStore *iface, TnyList *list,
548 TnyGetAccountsRequestType type)
550 ModestTnyAccountStore *self;
551 ModestTnyAccountStorePrivate *priv;
552 GSList *accounts, *cursor;
553 ModestAccountMgr *account_mgr;
554 TnyAccount *local_folder_account;
556 g_return_if_fail (iface);
557 g_return_if_fail (TNY_IS_LIST(list));
559 self = MODEST_TNY_ACCOUNT_STORE(iface);
560 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
561 account_mgr = modest_tny_platform_factory_get_account_mgr_instance
562 (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
564 if (type == TNY_ACCOUNT_STORE_BOTH) {
565 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
566 modest_tny_account_store_get_accounts (iface, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
569 accounts = modest_account_mgr_account_names (account_mgr, NULL);
570 for (cursor = accounts; cursor; cursor = cursor->next) {
571 TnyAccount *tny_account;
572 ModestAccountData *account_data =
573 modest_account_mgr_get_account_data (account_mgr,
574 (gchar*)cursor->data);
575 if (account_data && account_data->enabled) {
576 tny_account = get_tny_account_from_account (self, account_data, type);
578 tny_list_prepend (list, G_OBJECT(tny_account));
580 g_free (cursor->data);
581 modest_account_mgr_free_account_data (account_mgr, account_data);
583 g_slist_free (accounts);
585 /* also, add the local folder pseudo-account */
586 local_folder_account = get_local_folder_account (MODEST_TNY_ACCOUNT_STORE(self));
587 if (!local_folder_account)
588 g_printerr ("modest: failed to add local folders account\n");
590 tny_list_prepend (list, G_OBJECT(local_folder_account));
592 tny_session_camel_set_account_store (priv->tny_session_camel, iface);
596 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
598 ModestTnyAccountStorePrivate *priv;
599 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
601 if (!priv->cache_dir)
602 priv->cache_dir = g_build_filename (g_get_home_dir(),
606 return priv->cache_dir;
611 * callers need to unref
614 modest_tny_account_store_get_device (TnyAccountStore *self)
616 ModestTnyAccountStorePrivate *priv;
618 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
621 priv->device = tny_platform_factory_new_device (priv->platform_fact);
623 return g_object_ref (G_OBJECT(priv->device));
629 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
632 g_printerr ("modest: alert_func not implemented (%d, %s)\n",
639 modest_tny_account_store_init (gpointer g, gpointer iface_data)
641 TnyAccountStoreIface *klass;
643 g_return_if_fail (g);
645 klass = (TnyAccountStoreIface *)g;
647 klass->get_accounts_func =
648 modest_tny_account_store_get_accounts;
649 klass->add_transport_account_func =
650 modest_tny_account_store_add_transport_account;
651 klass->add_store_account_func =
652 modest_tny_account_store_add_store_account;
653 klass->get_cache_dir_func =
654 modest_tny_account_store_get_cache_dir;
655 klass->get_device_func =
656 modest_tny_account_store_get_device;
658 modest_tny_account_store_alert;
662 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
663 ModestTnyGetPassFunc func)
665 g_message (__FUNCTION__);
666 return; /* not implemented, we use signals */
672 tny_account_store_get_session (TnyAccountStore *self)
674 g_return_val_if_fail (self, NULL);
675 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;