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,
100 gboolean *has_signature);
104 static GObjectClass *parent_class = NULL;
107 modest_account_protocol_get_type (void)
109 static GType my_type = 0;
112 static const GTypeInfo my_info = {
113 sizeof(ModestAccountProtocolClass),
114 NULL, /* base init */
115 NULL, /* base finalize */
116 (GClassInitFunc) modest_account_protocol_class_init,
117 NULL, /* class finalize */
118 NULL, /* class data */
119 sizeof(ModestAccountProtocol),
121 (GInstanceInitFunc) modest_account_protocol_instance_init,
125 my_type = g_type_register_static (MODEST_TYPE_PROTOCOL,
126 "ModestAccountProtocol",
133 modest_account_protocol_class_init (ModestAccountProtocolClass *klass)
135 GObjectClass *object_class;
136 ModestAccountProtocolClass *account_class;
138 object_class = (GObjectClass *) klass;
139 account_class = MODEST_ACCOUNT_PROTOCOL_CLASS (klass);
140 parent_class = g_type_class_peek_parent (klass);
141 object_class->finalize = modest_account_protocol_finalize;
142 object_class->set_property = modest_account_protocol_set_property;
143 object_class->get_property = modest_account_protocol_get_property;
145 g_object_class_install_property (object_class,
147 g_param_spec_uint ("port",
149 _("The standard port for the protocol"),
151 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
153 g_object_class_install_property (object_class,
155 g_param_spec_uint ("alternate-port",
157 _("The alternate port for the protocol (usually used in SSL)"),
159 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
161 g_object_class_install_property (object_class,
163 g_param_spec_gtype ("account-g-type",
164 _("Account factory GType"),
165 _("Account factory GType used for creating new instances."),
167 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
169 g_type_class_add_private (object_class,
170 sizeof(ModestAccountProtocolPrivate));
172 /* Virtual methods */
173 account_class->create_account_settings_dialog =
174 modest_account_protocol_create_account_settings_dialog_default;
175 account_class->get_easysetupwizard_tabs =
176 modest_account_protocol_get_easysetupwizard_tabs_default;
177 account_class->save_settings =
178 modest_account_protocol_save_settings_default;
179 account_class->save_wizard_settings =
180 modest_account_protocol_save_wizard_settings_default;
181 account_class->create_account =
182 modest_account_protocol_create_account_default;
183 account_class->get_wizard_response_override =
184 modest_account_protocol_get_wizard_response_override_default;
185 account_class->is_supported =
186 modest_account_protocol_is_supported_default;
187 account_class->check_support =
188 modest_account_protocol_check_support_default;
189 account_class->get_from =
190 modest_account_protocol_get_from_default;
191 account_class->get_from_list =
192 modest_account_protocol_get_from_list_default;
193 account_class->get_signature =
194 modest_account_protocol_get_signature_default;
198 modest_account_protocol_instance_init (ModestAccountProtocol *obj)
200 ModestAccountProtocolPrivate *priv;
202 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (obj);
205 priv->alternate_port = 0;
206 priv->account_g_type = 0;
207 priv->account_options = tny_simple_list_new ();
208 priv->custom_auth_mechs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
210 priv->account_dialogs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
214 remove_account (const gchar *account_name, GObject *account, GObject *account_to_remove)
216 return (account == account_to_remove);
220 account_dialog_weak_handler (ModestAccountProtocol *self, GObject *where_the_object_was)
222 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
224 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) remove_account, where_the_object_was);
228 dialogs_remove (const gchar *account_name, GObject *account_dialog, ModestAccountProtocol *self)
230 g_object_weak_unref (account_dialog, (GWeakNotify) account_dialog_weak_handler, self);
236 modest_account_protocol_finalize (GObject *obj)
238 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
239 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
241 if (priv->account_dialogs) {
242 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) dialogs_remove, obj);
243 g_hash_table_destroy (priv->account_dialogs);
246 if (priv->account_options)
247 g_object_unref (priv->account_options);
248 priv->account_options = NULL;
250 if (priv->custom_auth_mechs)
251 g_hash_table_destroy (priv->custom_auth_mechs);
252 priv->custom_auth_mechs = NULL;
254 G_OBJECT_CLASS (parent_class)->finalize (obj);
258 modest_account_protocol_get_property (GObject *obj,
263 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
264 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
266 switch (property_id) {
268 g_value_set_uint (value, priv->port);
270 case PROP_ALTERNATE_PORT:
271 g_value_set_uint (value, priv->alternate_port);
273 case PROP_ACCOUNT_G_TYPE:
274 g_value_set_gtype (value, priv->account_g_type);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
283 modest_account_protocol_set_property (GObject *obj,
288 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
290 switch (property_id) {
292 modest_account_protocol_set_port (protocol, g_value_get_uint (value));
294 case PROP_ALTERNATE_PORT:
295 modest_account_protocol_set_alternate_port (protocol, g_value_get_uint (value));
297 case PROP_ACCOUNT_G_TYPE:
298 modest_account_protocol_set_account_g_type (protocol, g_value_get_gtype (value));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
308 modest_account_protocol_new (const gchar *name, const gchar *display_name,
309 guint port, guint alternate_port,
310 GType account_g_type)
312 return g_object_new (MODEST_TYPE_ACCOUNT_PROTOCOL,
313 "display-name", display_name, "name", name,
314 "port", port, "alternate-port", alternate_port,
315 "account-g-type", account_g_type,
320 modest_account_protocol_get_port (ModestAccountProtocol *self)
322 ModestAccountProtocolPrivate *priv;
324 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
326 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
331 modest_account_protocol_set_port (ModestAccountProtocol *self,
334 ModestAccountProtocolPrivate *priv;
336 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
338 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
344 modest_account_protocol_get_alternate_port (ModestAccountProtocol *self)
346 ModestAccountProtocolPrivate *priv;
348 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
350 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
351 return priv->alternate_port;
355 modest_account_protocol_set_alternate_port (ModestAccountProtocol *self,
356 guint alternate_port)
358 ModestAccountProtocolPrivate *priv;
360 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
362 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
363 priv->alternate_port = alternate_port;
367 modest_account_protocol_get_account_g_type (ModestAccountProtocol *self)
369 ModestAccountProtocolPrivate *priv;
371 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
373 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
374 return priv->account_g_type;
378 modest_account_protocol_get_account_options (ModestAccountProtocol *self)
381 ModestAccountProtocolPrivate *priv;
383 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
384 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
386 result = tny_list_copy (priv->account_options);
392 modest_account_protocol_set_account_options (ModestAccountProtocol *self,
395 ModestAccountProtocolPrivate *priv;
397 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
398 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
400 if (priv->account_options) {
401 g_object_unref (priv->account_options);
402 priv->account_options = NULL;
404 priv->account_options = tny_list_copy (list);
408 modest_account_protocol_has_custom_secure_auth_mech (ModestAccountProtocol *self,
409 ModestProtocolType auth_protocol_type)
411 ModestAccountProtocolPrivate *priv;
413 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), FALSE);
414 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
416 return g_hash_table_lookup_extended (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), NULL, NULL);
420 modest_account_protocol_get_custom_secure_auth_mech (ModestAccountProtocol *self,
421 ModestProtocolType auth_protocol_type)
423 ModestAccountProtocolPrivate *priv;
425 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
426 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
428 return (const gchar *) g_hash_table_lookup (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
432 modest_account_protocol_set_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type, const gchar *secure_auth_mech)
434 ModestAccountProtocolPrivate *priv;
436 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
437 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
439 g_hash_table_replace (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), g_strdup (secure_auth_mech));
443 modest_account_protocol_unset_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type)
445 ModestAccountProtocolPrivate *priv;
447 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
448 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
450 g_hash_table_remove (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
455 modest_account_protocol_set_account_g_type (ModestAccountProtocol *self,
456 GType account_g_type)
458 ModestAccountProtocolPrivate *priv;
460 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
462 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
463 priv->account_g_type = account_g_type;
467 modest_account_protocol_create_account_default (ModestAccountProtocol *self)
469 ModestAccountProtocolPrivate *priv;
471 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
473 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
474 if (priv->account_g_type > 0) {
475 return g_object_new (priv->account_g_type, NULL);
482 modest_account_protocol_create_account (ModestAccountProtocol *self)
484 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
486 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account (self);
489 /* This is a template method for getting the account settings
490 dialog. It calls create_account_settings that must be implemented
491 by subclasses and then perform several common operations with the
493 ModestAccountSettingsDialog *
494 modest_account_protocol_get_account_settings_dialog (ModestAccountProtocol *self,
495 const gchar *account_name)
497 ModestAccountSettingsDialog *dialog;
498 ModestAccountSettings *settings;
499 ModestAccountProtocolPrivate *priv;
501 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
502 dialog = g_hash_table_lookup (priv->account_dialogs, account_name);
504 if (dialog == NULL) {
506 dialog = MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account_settings_dialog (self);
509 settings = modest_account_mgr_load_account_settings (modest_runtime_get_account_mgr (),
511 modest_account_settings_dialog_load_settings (dialog, settings);
513 /* Close dialog on response */
514 g_signal_connect_swapped (dialog,
516 G_CALLBACK (gtk_widget_destroy),
519 g_hash_table_insert (priv->account_dialogs, g_strdup (account_name), dialog);
520 g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) account_dialog_weak_handler, self);
527 modest_account_protocol_get_easysetupwizard_tabs (ModestAccountProtocol *self)
529 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_easysetupwizard_tabs (self);
534 modest_account_protocol_save_settings (ModestAccountProtocol *self,
535 ModestAccountSettingsDialog *dialog,
536 ModestAccountSettings *settings)
538 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_settings (self, dialog, settings);
542 modest_account_protocol_save_wizard_settings (ModestAccountProtocol *self,
544 ModestAccountSettings *settings)
546 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_wizard_settings (self, wizard_pages, settings);
549 /* Default implementations */
550 static ModestAccountSettingsDialog *
551 modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self)
553 return modest_default_account_settings_dialog_new ();
556 static ModestPairList*
557 modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self)
559 g_warning ("You must implement get_easysetupwizard_tabs");
564 modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
565 ModestAccountSettingsDialog *dialog,
566 ModestAccountSettings *settings)
568 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
569 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
570 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
572 g_warning ("You must implement save_settings");
576 modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
578 ModestAccountSettings *settings)
580 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
581 g_return_if_fail (wizard_pages);
582 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
584 g_warning ("You must implement save_wizard_settings");
587 static ModestWizardDialogResponseOverrideFunc
588 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self)
590 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
595 ModestWizardDialogResponseOverrideFunc
596 modest_account_protocol_get_wizard_response_override (ModestAccountProtocol *self)
598 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
600 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_wizard_response_override (self);
604 modest_account_protocol_is_supported_default (ModestAccountProtocol *self)
610 modest_account_protocol_is_supported (ModestAccountProtocol *self)
612 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->is_supported (self);
616 modest_account_protocol_check_support_default (ModestAccountProtocol *self,
617 ModestAccountProtocolCheckSupportFunc func,
621 func (self, TRUE, userdata);
625 modest_account_protocol_check_support (ModestAccountProtocol *self,
626 ModestAccountProtocolCheckSupportFunc func,
629 MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->check_support (self, func, userdata);
633 modest_account_protocol_get_from (ModestAccountProtocol *self,
634 const gchar *account_id,
635 const gchar *mailbox)
637 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from (self, account_id, mailbox);
640 modest_account_protocol_get_from_default (ModestAccountProtocol *self,
641 const gchar *account_id,
642 const gchar *mailbox)
644 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
650 modest_account_protocol_get_from_list (ModestAccountProtocol *self,
651 const gchar *account_id)
653 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from_list (self, account_id);
655 static ModestPairList *
656 modest_account_protocol_get_from_list_default (ModestAccountProtocol *self,
657 const gchar *account_id)
659 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
665 modest_account_protocol_get_signature (ModestAccountProtocol *self,
666 const gchar *account_id,
667 const gchar *mailbox,
668 gboolean *has_signature)
670 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_signature (self, account_id, mailbox, has_signature);
674 modest_account_protocol_get_signature_default (ModestAccountProtocol *self,
675 const gchar *account_id,
676 const gchar *mailbox,
677 gboolean *has_signature)
679 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
681 *has_signature = FALSE;