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);
89 static GObjectClass *parent_class = NULL;
92 modest_account_protocol_get_type (void)
94 static GType my_type = 0;
97 static const GTypeInfo my_info = {
98 sizeof(ModestAccountProtocolClass),
100 NULL, /* base finalize */
101 (GClassInitFunc) modest_account_protocol_class_init,
102 NULL, /* class finalize */
103 NULL, /* class data */
104 sizeof(ModestAccountProtocol),
106 (GInstanceInitFunc) modest_account_protocol_instance_init,
110 my_type = g_type_register_static (MODEST_TYPE_PROTOCOL,
111 "ModestAccountProtocol",
118 modest_account_protocol_class_init (ModestAccountProtocolClass *klass)
120 GObjectClass *object_class;
121 ModestAccountProtocolClass *account_class;
123 object_class = (GObjectClass *) klass;
124 account_class = MODEST_ACCOUNT_PROTOCOL_CLASS (klass);
125 parent_class = g_type_class_peek_parent (klass);
126 object_class->finalize = modest_account_protocol_finalize;
127 object_class->set_property = modest_account_protocol_set_property;
128 object_class->get_property = modest_account_protocol_get_property;
130 g_object_class_install_property (object_class,
132 g_param_spec_uint ("port",
134 _("The standard port for the protocol"),
136 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
138 g_object_class_install_property (object_class,
140 g_param_spec_uint ("alternate-port",
142 _("The alternate port for the protocol (usually used in SSL)"),
144 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
146 g_object_class_install_property (object_class,
148 g_param_spec_gtype ("account-g-type",
149 _("Account factory GType"),
150 _("Account factory GType used for creating new instances."),
152 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
154 g_type_class_add_private (object_class,
155 sizeof(ModestAccountProtocolPrivate));
157 /* Virtual methods */
158 account_class->create_account_settings_dialog =
159 modest_account_protocol_create_account_settings_dialog_default;
160 account_class->get_easysetupwizard_tabs =
161 modest_account_protocol_get_easysetupwizard_tabs_default;
162 account_class->save_settings =
163 modest_account_protocol_save_settings_default;
164 account_class->save_wizard_settings =
165 modest_account_protocol_save_wizard_settings_default;
166 account_class->create_account =
167 modest_account_protocol_create_account_default;
168 account_class->get_wizard_response_override =
169 modest_account_protocol_get_wizard_response_override_default;
173 modest_account_protocol_instance_init (ModestAccountProtocol *obj)
175 ModestAccountProtocolPrivate *priv;
177 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (obj);
180 priv->alternate_port = 0;
181 priv->account_g_type = 0;
182 priv->account_options = tny_simple_list_new ();
183 priv->custom_auth_mechs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
185 priv->account_dialogs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
189 remove_account (const gchar *account_name, GObject *account, GObject *account_to_remove)
191 return (account == account_to_remove);
195 account_dialog_weak_handler (ModestAccountProtocol *self, GObject *where_the_object_was)
197 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
199 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) remove_account, where_the_object_was);
203 dialogs_remove (const gchar *account_name, GObject *account_dialog, ModestAccountProtocol *self)
205 g_object_weak_unref (account_dialog, (GWeakNotify) account_dialog_weak_handler, self);
211 modest_account_protocol_finalize (GObject *obj)
213 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
214 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
216 if (priv->account_dialogs) {
217 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) dialogs_remove, obj);
218 g_hash_table_destroy (priv->account_dialogs);
221 if (priv->account_options)
222 g_object_unref (priv->account_options);
223 priv->account_options = NULL;
225 if (priv->custom_auth_mechs)
226 g_hash_table_destroy (priv->custom_auth_mechs);
227 priv->custom_auth_mechs = NULL;
229 G_OBJECT_CLASS (parent_class)->finalize (obj);
233 modest_account_protocol_get_property (GObject *obj,
238 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
239 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
241 switch (property_id) {
243 g_value_set_uint (value, priv->port);
245 case PROP_ALTERNATE_PORT:
246 g_value_set_uint (value, priv->alternate_port);
248 case PROP_ACCOUNT_G_TYPE:
249 g_value_set_gtype (value, priv->account_g_type);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
258 modest_account_protocol_set_property (GObject *obj,
263 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
265 switch (property_id) {
267 modest_account_protocol_set_port (protocol, g_value_get_uint (value));
269 case PROP_ALTERNATE_PORT:
270 modest_account_protocol_set_alternate_port (protocol, g_value_get_uint (value));
272 case PROP_ACCOUNT_G_TYPE:
273 modest_account_protocol_set_account_g_type (protocol, g_value_get_gtype (value));
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
283 modest_account_protocol_new (const gchar *name, const gchar *display_name,
284 guint port, guint alternate_port,
285 GType account_g_type)
287 return g_object_new (MODEST_TYPE_ACCOUNT_PROTOCOL,
288 "display-name", display_name, "name", name,
289 "port", port, "alternate-port", alternate_port,
290 "account-g-type", account_g_type,
295 modest_account_protocol_get_port (ModestAccountProtocol *self)
297 ModestAccountProtocolPrivate *priv;
299 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
301 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
306 modest_account_protocol_set_port (ModestAccountProtocol *self,
309 ModestAccountProtocolPrivate *priv;
311 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
313 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
319 modest_account_protocol_get_alternate_port (ModestAccountProtocol *self)
321 ModestAccountProtocolPrivate *priv;
323 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
325 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
326 return priv->alternate_port;
330 modest_account_protocol_set_alternate_port (ModestAccountProtocol *self,
331 guint alternate_port)
333 ModestAccountProtocolPrivate *priv;
335 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
337 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
338 priv->alternate_port = alternate_port;
342 modest_account_protocol_get_account_g_type (ModestAccountProtocol *self)
344 ModestAccountProtocolPrivate *priv;
346 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
348 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
349 return priv->account_g_type;
353 modest_account_protocol_get_account_options (ModestAccountProtocol *self)
356 ModestAccountProtocolPrivate *priv;
358 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
359 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
361 result = tny_list_copy (priv->account_options);
367 modest_account_protocol_set_account_options (ModestAccountProtocol *self,
370 ModestAccountProtocolPrivate *priv;
372 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
373 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
375 if (priv->account_options) {
376 g_object_unref (priv->account_options);
377 priv->account_options = NULL;
379 priv->account_options = tny_list_copy (list);
383 modest_account_protocol_has_custom_secure_auth_mech (ModestAccountProtocol *self,
384 ModestProtocolType auth_protocol_type)
386 ModestAccountProtocolPrivate *priv;
388 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), FALSE);
389 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
391 return g_hash_table_lookup_extended (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), NULL, NULL);
395 modest_account_protocol_get_custom_secure_auth_mech (ModestAccountProtocol *self,
396 ModestProtocolType auth_protocol_type)
398 ModestAccountProtocolPrivate *priv;
400 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
401 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
403 return (const gchar *) g_hash_table_lookup (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
407 modest_account_protocol_set_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type, const gchar *secure_auth_mech)
409 ModestAccountProtocolPrivate *priv;
411 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
412 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
414 g_hash_table_replace (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), g_strdup (secure_auth_mech));
418 modest_account_protocol_unset_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type)
420 ModestAccountProtocolPrivate *priv;
422 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
423 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
425 g_hash_table_remove (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
430 modest_account_protocol_set_account_g_type (ModestAccountProtocol *self,
431 GType account_g_type)
433 ModestAccountProtocolPrivate *priv;
435 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
437 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
438 priv->account_g_type = account_g_type;
442 modest_account_protocol_create_account_default (ModestAccountProtocol *self)
444 ModestAccountProtocolPrivate *priv;
446 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
448 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
449 if (priv->account_g_type > 0) {
450 return g_object_new (priv->account_g_type, NULL);
457 modest_account_protocol_create_account (ModestAccountProtocol *self)
459 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
461 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account (self);
464 /* This is a template method for getting the account settings
465 dialog. It calls create_account_settings that must be implemented
466 by subclasses and then perform several common operations with the
468 ModestAccountSettingsDialog *
469 modest_account_protocol_get_account_settings_dialog (ModestAccountProtocol *self,
470 const gchar *account_name)
472 ModestAccountSettingsDialog *dialog;
473 ModestAccountSettings *settings;
474 ModestAccountProtocolPrivate *priv;
476 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
477 dialog = g_hash_table_lookup (priv->account_dialogs, account_name);
479 if (dialog == NULL) {
481 dialog = MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account_settings_dialog (self);
484 settings = modest_account_mgr_load_account_settings (modest_runtime_get_account_mgr (),
486 modest_account_settings_dialog_load_settings (dialog, settings);
488 /* Close dialog on response */
489 g_signal_connect_swapped (dialog,
491 G_CALLBACK (gtk_widget_destroy),
494 g_hash_table_insert (priv->account_dialogs, g_strdup (account_name), dialog);
495 g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) account_dialog_weak_handler, self);
502 modest_account_protocol_get_easysetupwizard_tabs (ModestAccountProtocol *self)
504 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_easysetupwizard_tabs (self);
509 modest_account_protocol_save_settings (ModestAccountProtocol *self,
510 ModestAccountSettingsDialog *dialog,
511 ModestAccountSettings *settings)
513 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_settings (self, dialog, settings);
517 modest_account_protocol_save_wizard_settings (ModestAccountProtocol *self,
519 ModestAccountSettings *settings)
521 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_wizard_settings (self, wizard_pages, settings);
524 /* Default implementations */
525 static ModestAccountSettingsDialog *
526 modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self)
528 return modest_default_account_settings_dialog_new ();
531 static ModestPairList*
532 modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self)
534 g_warning ("You must implement get_easysetupwizard_tabs");
539 modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
540 ModestAccountSettingsDialog *dialog,
541 ModestAccountSettings *settings)
543 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
544 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
545 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
547 g_warning ("You must implement save_settings");
551 modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
553 ModestAccountSettings *settings)
555 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
556 g_return_if_fail (wizard_pages);
557 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
559 g_warning ("You must implement save_wizard_settings");
562 static ModestWizardDialogResponseOverrideFunc
563 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self)
565 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
570 ModestWizardDialogResponseOverrideFunc
571 modest_account_protocol_get_wizard_response_override (ModestAccountProtocol *self)
573 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
575 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_wizard_response_override (self);