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);
92 static gchar *modest_account_protocol_get_from_default (ModestAccountProtocol *self,
93 const gchar *account_id,
94 const gchar *mailbox);
95 static ModestPairList *modest_account_protocol_get_from_list_default (ModestAccountProtocol *self,
96 const gchar *account_id);
97 static gchar *modest_account_protocol_get_signature_default (ModestAccountProtocol *self,
98 const gchar *account_id,
99 const gchar *mailbox);
103 static GObjectClass *parent_class = NULL;
106 modest_account_protocol_get_type (void)
108 static GType my_type = 0;
111 static const GTypeInfo my_info = {
112 sizeof(ModestAccountProtocolClass),
113 NULL, /* base init */
114 NULL, /* base finalize */
115 (GClassInitFunc) modest_account_protocol_class_init,
116 NULL, /* class finalize */
117 NULL, /* class data */
118 sizeof(ModestAccountProtocol),
120 (GInstanceInitFunc) modest_account_protocol_instance_init,
124 my_type = g_type_register_static (MODEST_TYPE_PROTOCOL,
125 "ModestAccountProtocol",
132 modest_account_protocol_class_init (ModestAccountProtocolClass *klass)
134 GObjectClass *object_class;
135 ModestAccountProtocolClass *account_class;
137 object_class = (GObjectClass *) klass;
138 account_class = MODEST_ACCOUNT_PROTOCOL_CLASS (klass);
139 parent_class = g_type_class_peek_parent (klass);
140 object_class->finalize = modest_account_protocol_finalize;
141 object_class->set_property = modest_account_protocol_set_property;
142 object_class->get_property = modest_account_protocol_get_property;
144 g_object_class_install_property (object_class,
146 g_param_spec_uint ("port",
148 _("The standard port for the protocol"),
150 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
152 g_object_class_install_property (object_class,
154 g_param_spec_uint ("alternate-port",
156 _("The alternate port for the protocol (usually used in SSL)"),
158 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
160 g_object_class_install_property (object_class,
162 g_param_spec_gtype ("account-g-type",
163 _("Account factory GType"),
164 _("Account factory GType used for creating new instances."),
166 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
168 g_type_class_add_private (object_class,
169 sizeof(ModestAccountProtocolPrivate));
171 /* Virtual methods */
172 account_class->create_account_settings_dialog =
173 modest_account_protocol_create_account_settings_dialog_default;
174 account_class->get_easysetupwizard_tabs =
175 modest_account_protocol_get_easysetupwizard_tabs_default;
176 account_class->save_settings =
177 modest_account_protocol_save_settings_default;
178 account_class->save_wizard_settings =
179 modest_account_protocol_save_wizard_settings_default;
180 account_class->create_account =
181 modest_account_protocol_create_account_default;
182 account_class->get_wizard_response_override =
183 modest_account_protocol_get_wizard_response_override_default;
184 account_class->is_supported =
185 modest_account_protocol_is_supported_default;
186 account_class->check_support =
187 modest_account_protocol_check_support_default;
188 account_class->get_from =
189 modest_account_protocol_get_from_default;
190 account_class->get_from_list =
191 modest_account_protocol_get_from_list_default;
192 account_class->get_signature =
193 modest_account_protocol_get_signature_default;
197 modest_account_protocol_instance_init (ModestAccountProtocol *obj)
199 ModestAccountProtocolPrivate *priv;
201 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (obj);
204 priv->alternate_port = 0;
205 priv->account_g_type = 0;
206 priv->account_options = tny_simple_list_new ();
207 priv->custom_auth_mechs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
209 priv->account_dialogs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
213 remove_account (const gchar *account_name, GObject *account, GObject *account_to_remove)
215 return (account == account_to_remove);
219 account_dialog_weak_handler (ModestAccountProtocol *self, GObject *where_the_object_was)
221 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
223 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) remove_account, where_the_object_was);
227 dialogs_remove (const gchar *account_name, GObject *account_dialog, ModestAccountProtocol *self)
229 g_object_weak_unref (account_dialog, (GWeakNotify) account_dialog_weak_handler, self);
235 modest_account_protocol_finalize (GObject *obj)
237 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
238 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
240 if (priv->account_dialogs) {
241 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) dialogs_remove, obj);
242 g_hash_table_destroy (priv->account_dialogs);
245 if (priv->account_options)
246 g_object_unref (priv->account_options);
247 priv->account_options = NULL;
249 if (priv->custom_auth_mechs)
250 g_hash_table_destroy (priv->custom_auth_mechs);
251 priv->custom_auth_mechs = NULL;
253 G_OBJECT_CLASS (parent_class)->finalize (obj);
257 modest_account_protocol_get_property (GObject *obj,
262 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
263 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
265 switch (property_id) {
267 g_value_set_uint (value, priv->port);
269 case PROP_ALTERNATE_PORT:
270 g_value_set_uint (value, priv->alternate_port);
272 case PROP_ACCOUNT_G_TYPE:
273 g_value_set_gtype (value, priv->account_g_type);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
282 modest_account_protocol_set_property (GObject *obj,
287 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
289 switch (property_id) {
291 modest_account_protocol_set_port (protocol, g_value_get_uint (value));
293 case PROP_ALTERNATE_PORT:
294 modest_account_protocol_set_alternate_port (protocol, g_value_get_uint (value));
296 case PROP_ACCOUNT_G_TYPE:
297 modest_account_protocol_set_account_g_type (protocol, g_value_get_gtype (value));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
307 modest_account_protocol_new (const gchar *name, const gchar *display_name,
308 guint port, guint alternate_port,
309 GType account_g_type)
311 return g_object_new (MODEST_TYPE_ACCOUNT_PROTOCOL,
312 "display-name", display_name, "name", name,
313 "port", port, "alternate-port", alternate_port,
314 "account-g-type", account_g_type,
319 modest_account_protocol_get_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);
330 modest_account_protocol_set_port (ModestAccountProtocol *self,
333 ModestAccountProtocolPrivate *priv;
335 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
337 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
343 modest_account_protocol_get_alternate_port (ModestAccountProtocol *self)
345 ModestAccountProtocolPrivate *priv;
347 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
349 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
350 return priv->alternate_port;
354 modest_account_protocol_set_alternate_port (ModestAccountProtocol *self,
355 guint alternate_port)
357 ModestAccountProtocolPrivate *priv;
359 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
361 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
362 priv->alternate_port = alternate_port;
366 modest_account_protocol_get_account_g_type (ModestAccountProtocol *self)
368 ModestAccountProtocolPrivate *priv;
370 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
372 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
373 return priv->account_g_type;
377 modest_account_protocol_get_account_options (ModestAccountProtocol *self)
380 ModestAccountProtocolPrivate *priv;
382 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
383 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
385 result = tny_list_copy (priv->account_options);
391 modest_account_protocol_set_account_options (ModestAccountProtocol *self,
394 ModestAccountProtocolPrivate *priv;
396 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
397 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
399 if (priv->account_options) {
400 g_object_unref (priv->account_options);
401 priv->account_options = NULL;
403 priv->account_options = tny_list_copy (list);
407 modest_account_protocol_has_custom_secure_auth_mech (ModestAccountProtocol *self,
408 ModestProtocolType auth_protocol_type)
410 ModestAccountProtocolPrivate *priv;
412 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), FALSE);
413 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
415 return g_hash_table_lookup_extended (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), NULL, NULL);
419 modest_account_protocol_get_custom_secure_auth_mech (ModestAccountProtocol *self,
420 ModestProtocolType auth_protocol_type)
422 ModestAccountProtocolPrivate *priv;
424 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
425 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
427 return (const gchar *) g_hash_table_lookup (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
431 modest_account_protocol_set_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type, const gchar *secure_auth_mech)
433 ModestAccountProtocolPrivate *priv;
435 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
436 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
438 g_hash_table_replace (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), g_strdup (secure_auth_mech));
442 modest_account_protocol_unset_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type)
444 ModestAccountProtocolPrivate *priv;
446 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
447 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
449 g_hash_table_remove (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
454 modest_account_protocol_set_account_g_type (ModestAccountProtocol *self,
455 GType account_g_type)
457 ModestAccountProtocolPrivate *priv;
459 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
461 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
462 priv->account_g_type = account_g_type;
466 modest_account_protocol_create_account_default (ModestAccountProtocol *self)
468 ModestAccountProtocolPrivate *priv;
470 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
472 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
473 if (priv->account_g_type > 0) {
474 return g_object_new (priv->account_g_type, NULL);
481 modest_account_protocol_create_account (ModestAccountProtocol *self)
483 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
485 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account (self);
488 /* This is a template method for getting the account settings
489 dialog. It calls create_account_settings that must be implemented
490 by subclasses and then perform several common operations with the
492 ModestAccountSettingsDialog *
493 modest_account_protocol_get_account_settings_dialog (ModestAccountProtocol *self,
494 const gchar *account_name)
496 ModestAccountSettingsDialog *dialog;
497 ModestAccountSettings *settings;
498 ModestAccountProtocolPrivate *priv;
500 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
501 dialog = g_hash_table_lookup (priv->account_dialogs, account_name);
503 if (dialog == NULL) {
505 dialog = MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account_settings_dialog (self);
508 settings = modest_account_mgr_load_account_settings (modest_runtime_get_account_mgr (),
510 modest_account_settings_dialog_load_settings (dialog, settings);
512 /* Close dialog on response */
513 g_signal_connect_swapped (dialog,
515 G_CALLBACK (gtk_widget_destroy),
518 g_hash_table_insert (priv->account_dialogs, g_strdup (account_name), dialog);
519 g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) account_dialog_weak_handler, self);
526 modest_account_protocol_get_easysetupwizard_tabs (ModestAccountProtocol *self)
528 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_easysetupwizard_tabs (self);
533 modest_account_protocol_save_settings (ModestAccountProtocol *self,
534 ModestAccountSettingsDialog *dialog,
535 ModestAccountSettings *settings)
537 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_settings (self, dialog, settings);
541 modest_account_protocol_save_wizard_settings (ModestAccountProtocol *self,
543 ModestAccountSettings *settings)
545 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_wizard_settings (self, wizard_pages, settings);
548 /* Default implementations */
549 static ModestAccountSettingsDialog *
550 modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self)
552 return modest_default_account_settings_dialog_new ();
555 static ModestPairList*
556 modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self)
558 g_warning ("You must implement get_easysetupwizard_tabs");
563 modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
564 ModestAccountSettingsDialog *dialog,
565 ModestAccountSettings *settings)
567 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
568 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
569 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
571 g_warning ("You must implement save_settings");
575 modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
577 ModestAccountSettings *settings)
579 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
580 g_return_if_fail (wizard_pages);
581 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
583 g_warning ("You must implement save_wizard_settings");
586 static ModestWizardDialogResponseOverrideFunc
587 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self)
589 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
594 ModestWizardDialogResponseOverrideFunc
595 modest_account_protocol_get_wizard_response_override (ModestAccountProtocol *self)
597 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
599 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_wizard_response_override (self);
603 modest_account_protocol_is_supported_default (ModestAccountProtocol *self)
609 modest_account_protocol_is_supported (ModestAccountProtocol *self)
611 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->is_supported (self);
615 modest_account_protocol_check_support_default (ModestAccountProtocol *self,
616 ModestAccountProtocolCheckSupportFunc func,
620 func (self, TRUE, userdata);
624 modest_account_protocol_check_support (ModestAccountProtocol *self,
625 ModestAccountProtocolCheckSupportFunc func,
628 MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->check_support (self, func, userdata);
632 modest_account_protocol_get_from (ModestAccountProtocol *self,
633 const gchar *account_id,
634 const gchar *mailbox)
636 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from (self, account_id, mailbox);
639 modest_account_protocol_get_from_default (ModestAccountProtocol *self,
640 const gchar *account_id,
641 const gchar *mailbox)
643 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
649 modest_account_protocol_get_from_list (ModestAccountProtocol *self,
650 const gchar *account_id)
652 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from_list (self, account_id);
654 static ModestPairList *
655 modest_account_protocol_get_from_list_default (ModestAccountProtocol *self,
656 const gchar *account_id)
658 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
664 modest_account_protocol_get_signature (ModestAccountProtocol *self,
665 const gchar *account_id,
666 const gchar *mailbox)
668 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_signature (self, account_id, mailbox);
671 modest_account_protocol_get_signature_default (ModestAccountProtocol *self,
672 const gchar *account_id,
673 const gchar *mailbox)
675 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);