1 /* Copyright (c) 2008, 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.
30 #include <tny-simple-list.h>
31 #include "modest-account-protocol.h"
32 #include "modest-account-mgr-helpers.h"
33 #include "widgets/modest-default-account-settings-dialog.h"
34 #include "modest-runtime.h"
43 typedef struct _ModestAccountProtocolPrivate ModestAccountProtocolPrivate;
44 struct _ModestAccountProtocolPrivate {
47 TnyList *account_options;
48 GHashTable *custom_auth_mechs;
51 GHashTable *account_dialogs;
54 /* 'private'/'protected' functions */
55 static void modest_account_protocol_class_init (ModestAccountProtocolClass *klass);
56 static void modest_account_protocol_finalize (GObject *obj);
57 static void modest_account_protocol_get_property (GObject *obj,
61 static void modest_account_protocol_set_property (GObject *obj,
65 static void modest_account_protocol_instance_init (ModestAccountProtocol *obj);
67 #define MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
68 MODEST_TYPE_ACCOUNT_PROTOCOL, \
69 ModestAccountProtocolPrivate))
71 static TnyAccount *modest_account_protocol_create_account_default (ModestAccountProtocol *self);
73 static ModestAccountSettingsDialog *modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self);
75 static ModestPairList* modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self);
77 static void modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
78 ModestAccountSettingsDialog *dialog,
79 ModestAccountSettings *settings);
81 static void modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
83 ModestAccountSettings *settings);
85 static ModestWizardDialogResponseOverrideFunc
86 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self);
88 static void modest_account_protocol_check_support_default (ModestAccountProtocol *self,
89 ModestAccountProtocolCheckSupportFunc func,
91 static gboolean modest_account_protocol_is_supported_default (ModestAccountProtocol *self);
94 static GObjectClass *parent_class = NULL;
97 modest_account_protocol_get_type (void)
99 static GType my_type = 0;
102 static const GTypeInfo my_info = {
103 sizeof(ModestAccountProtocolClass),
104 NULL, /* base init */
105 NULL, /* base finalize */
106 (GClassInitFunc) modest_account_protocol_class_init,
107 NULL, /* class finalize */
108 NULL, /* class data */
109 sizeof(ModestAccountProtocol),
111 (GInstanceInitFunc) modest_account_protocol_instance_init,
115 my_type = g_type_register_static (MODEST_TYPE_PROTOCOL,
116 "ModestAccountProtocol",
123 modest_account_protocol_class_init (ModestAccountProtocolClass *klass)
125 GObjectClass *object_class;
126 ModestAccountProtocolClass *account_class;
128 object_class = (GObjectClass *) klass;
129 account_class = MODEST_ACCOUNT_PROTOCOL_CLASS (klass);
130 parent_class = g_type_class_peek_parent (klass);
131 object_class->finalize = modest_account_protocol_finalize;
132 object_class->set_property = modest_account_protocol_set_property;
133 object_class->get_property = modest_account_protocol_get_property;
135 g_object_class_install_property (object_class,
137 g_param_spec_uint ("port",
139 _("The standard port for the protocol"),
141 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
143 g_object_class_install_property (object_class,
145 g_param_spec_uint ("alternate-port",
147 _("The alternate port for the protocol (usually used in SSL)"),
149 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
151 g_object_class_install_property (object_class,
153 g_param_spec_gtype ("account-g-type",
154 _("Account factory GType"),
155 _("Account factory GType used for creating new instances."),
157 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
159 g_type_class_add_private (object_class,
160 sizeof(ModestAccountProtocolPrivate));
162 /* Virtual methods */
163 account_class->create_account_settings_dialog =
164 modest_account_protocol_create_account_settings_dialog_default;
165 account_class->get_easysetupwizard_tabs =
166 modest_account_protocol_get_easysetupwizard_tabs_default;
167 account_class->save_settings =
168 modest_account_protocol_save_settings_default;
169 account_class->save_wizard_settings =
170 modest_account_protocol_save_wizard_settings_default;
171 account_class->create_account =
172 modest_account_protocol_create_account_default;
173 account_class->get_wizard_response_override =
174 modest_account_protocol_get_wizard_response_override_default;
175 account_class->is_supported =
176 modest_account_protocol_is_supported_default;
177 account_class->check_support =
178 modest_account_protocol_check_support_default;
182 modest_account_protocol_instance_init (ModestAccountProtocol *obj)
184 ModestAccountProtocolPrivate *priv;
186 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (obj);
189 priv->alternate_port = 0;
190 priv->account_g_type = 0;
191 priv->account_options = tny_simple_list_new ();
192 priv->custom_auth_mechs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
194 priv->account_dialogs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
198 remove_account (const gchar *account_name, GObject *account, GObject *account_to_remove)
200 return (account == account_to_remove);
204 account_dialog_weak_handler (ModestAccountProtocol *self, GObject *where_the_object_was)
206 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
208 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) remove_account, where_the_object_was);
212 dialogs_remove (const gchar *account_name, GObject *account_dialog, ModestAccountProtocol *self)
214 g_object_weak_unref (account_dialog, (GWeakNotify) account_dialog_weak_handler, self);
220 modest_account_protocol_finalize (GObject *obj)
222 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
223 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
225 if (priv->account_dialogs) {
226 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) dialogs_remove, obj);
227 g_hash_table_destroy (priv->account_dialogs);
230 if (priv->account_options)
231 g_object_unref (priv->account_options);
232 priv->account_options = NULL;
234 if (priv->custom_auth_mechs)
235 g_hash_table_destroy (priv->custom_auth_mechs);
236 priv->custom_auth_mechs = NULL;
238 G_OBJECT_CLASS (parent_class)->finalize (obj);
242 modest_account_protocol_get_property (GObject *obj,
247 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
248 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
250 switch (property_id) {
252 g_value_set_uint (value, priv->port);
254 case PROP_ALTERNATE_PORT:
255 g_value_set_uint (value, priv->alternate_port);
257 case PROP_ACCOUNT_G_TYPE:
258 g_value_set_gtype (value, priv->account_g_type);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
267 modest_account_protocol_set_property (GObject *obj,
272 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
274 switch (property_id) {
276 modest_account_protocol_set_port (protocol, g_value_get_uint (value));
278 case PROP_ALTERNATE_PORT:
279 modest_account_protocol_set_alternate_port (protocol, g_value_get_uint (value));
281 case PROP_ACCOUNT_G_TYPE:
282 modest_account_protocol_set_account_g_type (protocol, g_value_get_gtype (value));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
292 modest_account_protocol_new (const gchar *name, const gchar *display_name,
293 guint port, guint alternate_port,
294 GType account_g_type)
296 return g_object_new (MODEST_TYPE_ACCOUNT_PROTOCOL,
297 "display-name", display_name, "name", name,
298 "port", port, "alternate-port", alternate_port,
299 "account-g-type", account_g_type,
304 modest_account_protocol_get_port (ModestAccountProtocol *self)
306 ModestAccountProtocolPrivate *priv;
308 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
310 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
315 modest_account_protocol_set_port (ModestAccountProtocol *self,
318 ModestAccountProtocolPrivate *priv;
320 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
322 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
328 modest_account_protocol_get_alternate_port (ModestAccountProtocol *self)
330 ModestAccountProtocolPrivate *priv;
332 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
334 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
335 return priv->alternate_port;
339 modest_account_protocol_set_alternate_port (ModestAccountProtocol *self,
340 guint alternate_port)
342 ModestAccountProtocolPrivate *priv;
344 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
346 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
347 priv->alternate_port = alternate_port;
351 modest_account_protocol_get_account_g_type (ModestAccountProtocol *self)
353 ModestAccountProtocolPrivate *priv;
355 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
357 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
358 return priv->account_g_type;
362 modest_account_protocol_get_account_options (ModestAccountProtocol *self)
365 ModestAccountProtocolPrivate *priv;
367 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
368 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
370 result = tny_list_copy (priv->account_options);
376 modest_account_protocol_set_account_options (ModestAccountProtocol *self,
379 ModestAccountProtocolPrivate *priv;
381 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
382 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
384 if (priv->account_options) {
385 g_object_unref (priv->account_options);
386 priv->account_options = NULL;
388 priv->account_options = tny_list_copy (list);
392 modest_account_protocol_has_custom_secure_auth_mech (ModestAccountProtocol *self,
393 ModestProtocolType auth_protocol_type)
395 ModestAccountProtocolPrivate *priv;
397 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), FALSE);
398 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
400 return g_hash_table_lookup_extended (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), NULL, NULL);
404 modest_account_protocol_get_custom_secure_auth_mech (ModestAccountProtocol *self,
405 ModestProtocolType auth_protocol_type)
407 ModestAccountProtocolPrivate *priv;
409 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
410 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
412 return (const gchar *) g_hash_table_lookup (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
416 modest_account_protocol_set_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type, const gchar *secure_auth_mech)
418 ModestAccountProtocolPrivate *priv;
420 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
421 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
423 g_hash_table_replace (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), g_strdup (secure_auth_mech));
427 modest_account_protocol_unset_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type)
429 ModestAccountProtocolPrivate *priv;
431 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
432 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
434 g_hash_table_remove (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
439 modest_account_protocol_set_account_g_type (ModestAccountProtocol *self,
440 GType account_g_type)
442 ModestAccountProtocolPrivate *priv;
444 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
446 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
447 priv->account_g_type = account_g_type;
451 modest_account_protocol_create_account_default (ModestAccountProtocol *self)
453 ModestAccountProtocolPrivate *priv;
455 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
457 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
458 if (priv->account_g_type > 0) {
459 return g_object_new (priv->account_g_type, NULL);
466 modest_account_protocol_create_account (ModestAccountProtocol *self)
468 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
470 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account (self);
473 /* This is a template method for getting the account settings
474 dialog. It calls create_account_settings that must be implemented
475 by subclasses and then perform several common operations with the
477 ModestAccountSettingsDialog *
478 modest_account_protocol_get_account_settings_dialog (ModestAccountProtocol *self,
479 const gchar *account_name)
481 ModestAccountSettingsDialog *dialog;
482 ModestAccountSettings *settings;
483 ModestAccountProtocolPrivate *priv;
485 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
486 dialog = g_hash_table_lookup (priv->account_dialogs, account_name);
488 if (dialog == NULL) {
490 dialog = MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account_settings_dialog (self);
493 settings = modest_account_mgr_load_account_settings (modest_runtime_get_account_mgr (),
495 modest_account_settings_dialog_load_settings (dialog, settings);
497 /* Close dialog on response */
498 g_signal_connect_swapped (dialog,
500 G_CALLBACK (gtk_widget_destroy),
503 g_hash_table_insert (priv->account_dialogs, g_strdup (account_name), dialog);
504 g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) account_dialog_weak_handler, self);
511 modest_account_protocol_get_easysetupwizard_tabs (ModestAccountProtocol *self)
513 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_easysetupwizard_tabs (self);
518 modest_account_protocol_save_settings (ModestAccountProtocol *self,
519 ModestAccountSettingsDialog *dialog,
520 ModestAccountSettings *settings)
522 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_settings (self, dialog, settings);
526 modest_account_protocol_save_wizard_settings (ModestAccountProtocol *self,
528 ModestAccountSettings *settings)
530 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_wizard_settings (self, wizard_pages, settings);
533 /* Default implementations */
534 static ModestAccountSettingsDialog *
535 modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self)
537 return modest_default_account_settings_dialog_new ();
540 static ModestPairList*
541 modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self)
543 g_warning ("You must implement get_easysetupwizard_tabs");
548 modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
549 ModestAccountSettingsDialog *dialog,
550 ModestAccountSettings *settings)
552 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
553 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
554 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
556 g_warning ("You must implement save_settings");
560 modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
562 ModestAccountSettings *settings)
564 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
565 g_return_if_fail (wizard_pages);
566 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
568 g_warning ("You must implement save_wizard_settings");
571 static ModestWizardDialogResponseOverrideFunc
572 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self)
574 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
579 ModestWizardDialogResponseOverrideFunc
580 modest_account_protocol_get_wizard_response_override (ModestAccountProtocol *self)
582 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
584 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_wizard_response_override (self);
588 modest_account_protocol_is_supported_default (ModestAccountProtocol *self)
594 modest_account_protocol_is_supported (ModestAccountProtocol *self)
596 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->is_supported (self);
600 modest_account_protocol_check_support_default (ModestAccountProtocol *self,
601 ModestAccountProtocolCheckSupportFunc func,
605 func (self, TRUE, userdata);
609 modest_account_protocol_check_support (ModestAccountProtocol *self,
610 ModestAccountProtocolCheckSupportFunc func,
613 MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->check_support (self, func, userdata);