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 ACCOUNT_UPDATE_SIGNAL,
70 typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate;
71 struct _ModestTnyAccountStorePrivate {
75 GHashTable *password_hash;
77 TnySessionCamel *tny_session_camel;
79 ModestAccountMgr *account_mgr;
80 TnyAccount *local_folders;
83 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
84 MODEST_TYPE_TNY_ACCOUNT_STORE, \
85 ModestTnyAccountStorePrivate))
87 static void on_password_requested (ModestTnyAccountStore *account_store,
88 const gchar* account_name,
94 static GObjectClass *parent_class = NULL;
96 static guint signals[LAST_SIGNAL] = {0};
99 modest_tny_account_store_get_type (void)
101 static GType my_type = 0;
104 static const GTypeInfo my_info = {
105 sizeof(ModestTnyAccountStoreClass),
106 NULL, /* base init */
107 NULL, /* base finalize */
108 (GClassInitFunc) modest_tny_account_store_class_init,
109 NULL, /* class finalize */
110 NULL, /* class data */
111 sizeof(ModestTnyAccountStore),
113 (GInstanceInitFunc) modest_tny_account_store_instance_init,
117 static const GInterfaceInfo iface_info = {
118 (GInterfaceInitFunc) modest_tny_account_store_init,
119 NULL, /* interface_finalize */
120 NULL /* interface_data */
123 my_type = g_type_register_static (G_TYPE_OBJECT,
124 "ModestTnyAccountStore",
126 g_type_add_interface_static (my_type, TNY_TYPE_ACCOUNT_STORE,
133 modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
135 GObjectClass *gobject_class;
136 gobject_class = (GObjectClass*) klass;
138 parent_class = g_type_class_peek_parent (klass);
139 gobject_class->finalize = modest_tny_account_store_finalize;
141 g_type_class_add_private (gobject_class,
142 sizeof(ModestTnyAccountStorePrivate));
144 signals[ACCOUNT_UPDATE_SIGNAL] =
145 g_signal_new ("account_update",
146 G_TYPE_FROM_CLASS (gobject_class),
148 G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update),
150 g_cclosure_marshal_VOID__STRING,
151 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;
163 priv->account_mgr = NULL;
164 priv->tny_session_camel = NULL;
167 priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
170 priv->local_folders = NULL;
176 /* create a pseudo-account for our local folders */
178 get_local_folders_account (ModestTnyAccountStore *self)
180 TnyStoreAccount *tny_account;
182 gchar *maildir, *url_string;
183 ModestTnyAccountStorePrivate *priv;
185 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
187 tny_account = tny_camel_store_account_new ();
189 g_printerr ("modest: cannot create account for local folders");
193 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),priv->tny_session_camel);
195 maildir = modest_local_folder_info_get_maildir_path ();
196 url = camel_url_new ("maildir:", NULL);
197 camel_url_set_path (url, maildir);
198 url_string = camel_url_to_string (url, 0);
200 tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
201 tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME);
202 tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME);
204 camel_url_free (url);
208 return TNY_ACCOUNT(tny_account);
214 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
217 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
219 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
226 on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
227 const gchar *key, gpointer user_data)
229 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
231 g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0,
236 static ModestTnyAccountStore*
237 get_account_store_for_account (TnyAccount *account)
239 return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
246 set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
248 g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
252 on_password_requested (ModestTnyAccountStore *account_store,
253 const gchar* account_name,
259 GtkWidget *dialog, *entry, *remember_pass_check;
261 dialog = gtk_dialog_new_with_buttons (_("Password requested"),
270 txt = g_strdup_printf (_("Please enter your password for %s"), account_name);
271 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), gtk_label_new(txt),
275 entry = gtk_entry_new_with_max_length (40);
276 gtk_entry_set_visibility (GTK_ENTRY(entry), FALSE);
277 gtk_entry_set_invisible_char (GTK_ENTRY(entry), 0x2022); /* bullet unichar */
279 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), entry,
282 remember_pass_check = gtk_check_button_new_with_label (_("Remember password"));
283 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), remember_pass_check,
286 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
288 if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
289 *password = g_strdup (gtk_entry_get_text (GTK_ENTRY(entry)));
296 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (remember_pass_check)))
301 gtk_widget_destroy (dialog);
303 while (gtk_events_pending ())
304 gtk_main_iteration ();
308 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
311 const TnyAccountStore *account_store;
312 ModestTnyAccountStore *self;
313 ModestTnyAccountStorePrivate *priv;
316 gboolean already_asked;
319 key = tny_account_get_id (account);
320 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
322 self = MODEST_TNY_ACCOUNT_STORE (account_store);
323 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
325 /* is it in the hash? if it's already there, it must be wrong... */
326 pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
327 * type-punned ptrs...*/
328 already_asked = g_hash_table_lookup_extended (priv->password_hash,
331 (gpointer*)&pwd_ptr);
333 /* if the password is not already there, try ModestConf */
334 if (!already_asked) {
335 pwd = modest_account_mgr_get_string (priv->account_mgr,
336 key, MODEST_ACCOUNT_PASSWORD,
338 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
341 /* if it was already asked, it must have been wrong, so ask again */
342 if (already_asked || !pwd || strlen(pwd) == 0) {
344 /* we don't have it yet. Get the password from the user */
345 const gchar* name = tny_account_get_name (account);
349 on_password_requested (self, name, &pwd, cancel, &remember);
353 modest_account_mgr_set_string (priv->account_mgr,
354 key, MODEST_ACCOUNT_PASSWORD,
357 /* We need to dup the string even knowing that
358 it's already a dup of the contents of an
359 entry, because it if it's wrong, then camel
361 g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
363 g_hash_table_remove (priv->password_hash, key);
375 forget_password (TnyAccount *account)
377 ModestTnyAccountStore *self;
378 ModestTnyAccountStorePrivate *priv;
379 const TnyAccountStore *account_store;
383 account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
384 self = MODEST_TNY_ACCOUNT_STORE (account_store);
385 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
386 key = tny_account_get_id (account);
388 /* Do not remove the key, this will allow us to detect that we
389 have already asked for it at least once */
390 pwd = g_hash_table_lookup (priv->password_hash, key);
392 memset (pwd, 0, strlen (pwd));
393 g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
396 /* Remove from configuration system */
397 modest_account_mgr_unset (priv->account_mgr,
398 key, MODEST_ACCOUNT_PASSWORD,
404 /* instantiate the correct tny account subclass */
406 tny_account_for_proto (ModestProtocol proto)
408 ModestProtocolType type;
409 TnyAccount *tny_account = NULL;
411 type = modest_protocol_info_get_protocol_type (proto);
413 if (type == MODEST_PROTOCOL_TYPE_TRANSPORT)
414 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
415 else if (proto == MODEST_PROTOCOL_STORE_POP)
416 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
417 else if (proto == MODEST_PROTOCOL_STORE_IMAP)
418 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
420 g_return_val_if_reached (NULL);
423 tny_account_set_proto (tny_account,
424 modest_protocol_info_get_protocol_name(proto));
426 g_printerr ("modest: could not get tny account for %d\n",
432 /* create a tnyaccount for the server account connected to the account with name 'key'
435 get_tny_account_from_server_account (ModestTnyAccountStore *self,
436 ModestServerAccountData *account_data,
437 ModestProtocolType modest_type)
439 TnyAccount *tny_account;
440 ModestTnyAccountStorePrivate *priv;
442 g_return_val_if_fail (self, NULL);
443 g_return_val_if_fail (account_data, NULL);
445 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
448 if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) {
449 g_printerr ("modest: '%s' does not provide a protocol\n",
450 account_data->account_name);
454 tny_account = tny_account_for_proto (account_data->proto);
456 g_printerr ("modest: could not create tny account for '%s'\n",
457 account_data->account_name);
461 /* Set account store, session and id */
462 set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
463 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), /* session */
464 priv->tny_session_camel);
465 tny_account_set_id (tny_account, account_data->account_name); /* id */
468 if (account_data->options) {
469 GSList *tmp = account_data->options;
471 tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account),
473 tmp = g_slist_next (tmp);
476 /* Hostname & Username */
477 if (account_data->username)
478 tny_account_set_user (tny_account, account_data->username);
480 if (account_data->hostname)
481 tny_account_set_hostname (tny_account, account_data->hostname);
483 /* Password functions */
484 tny_account_set_pass_func (tny_account, get_password);
485 tny_account_set_forget_pass_func (tny_account, forget_password);
493 modest_tny_account_store_finalize (GObject *obj)
495 ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj);
496 ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
498 if (priv->tny_session_camel) {
499 camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
500 priv->tny_session_camel = NULL;
503 g_free (priv->cache_dir);
504 priv->cache_dir = NULL;
507 g_object_unref (priv->device);
511 if (priv->password_hash) {
512 g_hash_table_destroy (priv->password_hash);
513 priv->password_hash = NULL;
516 if (priv->account_mgr) {
517 g_object_unref (priv->account_mgr);
518 priv->account_mgr = NULL;
521 G_OBJECT_CLASS(parent_class)->finalize (obj);
525 ModestTnyAccountStore*
526 modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
529 ModestTnyAccountStorePrivate *priv;
531 g_return_val_if_fail (account_mgr, NULL);
533 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
534 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
536 priv->account_mgr = account_mgr;
537 g_object_ref (G_OBJECT(priv->account_mgr));
539 /* The session needs the platform factory */
540 priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
541 if (!priv->tny_session_camel) {
542 g_printerr ("modest: cannot create TnySessionCamel instance\n");
543 g_object_unref (obj);
547 tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
548 /* FIXME: unref this in the end? */
550 /* Connect signals */
551 g_signal_connect (G_OBJECT(account_mgr), "account_changed",
552 G_CALLBACK (on_account_changed), obj);
553 g_signal_connect (G_OBJECT(account_mgr), "account_removed",
554 G_CALLBACK (on_account_removed), obj);
556 return MODEST_TNY_ACCOUNT_STORE(obj);
560 modest_tny_account_store_add_store_account (TnyAccountStore *self,
561 TnyStoreAccount *account)
563 /* we should not need this...*/
564 g_printerr ("modest: add_store_account_func not implemented\n");
569 modest_tny_account_store_add_transport_account (TnyAccountStore *self,
570 TnyTransportAccount *account)
572 /* we should not need this...*/
573 g_printerr ("modest: add_transport_account_func not implemented\n");
579 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
580 TnyGetAccountsRequestType type)
582 TnyAccount *tny_account = NULL;
583 ModestServerAccountData *server_account = NULL;
585 if (type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS && account_data->store_account)
586 server_account = account_data->store_account;
587 else if (type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS && account_data->transport_account)
588 server_account = account_data->transport_account;
590 if (!server_account) {
591 g_printerr ("modest: no %s account defined for '%s'\n",
592 type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS ? "store" : "transport",
593 account_data->display_name);
597 tny_account = get_tny_account_from_server_account (self, server_account, type);
599 g_printerr ("modest: failed to create tny account for %s\n",
600 account_data->account_name);
604 if (account_data->display_name)
605 tny_account_set_name (tny_account, account_data->display_name);
612 modest_tny_account_store_get_accounts (TnyAccountStore *account_store, TnyList *list,
613 TnyGetAccountsRequestType type)
615 ModestTnyAccountStore *self;
616 ModestTnyAccountStorePrivate *priv;
617 GSList *accounts, *cursor;
619 g_return_if_fail (account_store);
620 g_return_if_fail (TNY_IS_LIST(list));
622 self = MODEST_TNY_ACCOUNT_STORE(account_store);
623 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
625 if (type == TNY_ACCOUNT_STORE_BOTH) {
626 modest_tny_account_store_get_accounts (account_store, list,
627 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
628 modest_tny_account_store_get_accounts (account_store, list,
629 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
632 accounts = modest_account_mgr_account_names (priv->account_mgr, NULL);
633 for (cursor = accounts; cursor; cursor = cursor->next) {
634 TnyAccount *tny_account = NULL;
635 ModestAccountData *account_data =
636 modest_account_mgr_get_account_data (priv->account_mgr,
637 (gchar*)cursor->data);
638 if (account_data && account_data->enabled) {
639 tny_account = get_tny_account_from_account (self, account_data, type);
641 tny_list_prepend (list, G_OBJECT(tny_account));
643 g_free (cursor->data);
644 modest_account_mgr_free_account_data (priv->account_mgr, account_data);
646 g_slist_free (accounts);
648 /* also, add the local folder pseudo-account */
649 if (type != TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
650 if (!priv->local_folders)
651 priv->local_folders = get_local_folders_account (self);
652 if (!priv->local_folders)
653 g_printerr ("modest: no local folders account\n");
655 tny_list_prepend (list, G_OBJECT(priv->local_folders));
657 tny_session_camel_set_account_store (priv->tny_session_camel, account_store);
661 modest_tny_account_store_get_cache_dir (TnyAccountStore *self)
663 ModestTnyAccountStorePrivate *priv;
664 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
666 if (!priv->cache_dir)
667 priv->cache_dir = g_build_filename (g_get_home_dir(),
671 return priv->cache_dir;
676 * callers need to unref
679 modest_tny_account_store_get_device (TnyAccountStore *self)
681 ModestTnyAccountStorePrivate *priv;
683 priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
686 priv->device = tny_platform_factory_new_device
687 (modest_tny_platform_factory_get_instance());
689 return g_object_ref (G_OBJECT(priv->device));
695 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
698 g_printerr ("modest: alert_func not implemented (%d, %s)\n",
705 modest_tny_account_store_init (gpointer g, gpointer iface_data)
707 TnyAccountStoreIface *klass;
709 g_return_if_fail (g);
711 klass = (TnyAccountStoreIface *)g;
713 klass->get_accounts_func =
714 modest_tny_account_store_get_accounts;
715 klass->add_transport_account_func =
716 modest_tny_account_store_add_transport_account;
717 klass->add_store_account_func =
718 modest_tny_account_store_add_store_account;
719 klass->get_cache_dir_func =
720 modest_tny_account_store_get_cache_dir;
721 klass->get_device_func =
722 modest_tny_account_store_get_device;
724 modest_tny_account_store_alert;
728 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
729 ModestTnyGetPassFunc func)
731 /* not implemented, we use signals */
732 g_printerr ("modest: set_get_pass_func not implemented\n");
736 tny_account_store_get_session (TnyAccountStore *self)
738 g_return_val_if_fail (self, NULL);
739 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;
744 modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
746 g_return_val_if_fail (self, NULL);
748 return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->local_folders;