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);
101 static const GdkPixbuf *modest_account_protocol_get_icon_default (ModestAccountProtocol *self,
102 ModestAccountProtocolIconType icon_type,
106 static gchar *modest_account_protocol_get_service_name_default (ModestAccountProtocol *self,
107 const gchar *account_id,
108 const gchar *mailbox);
111 static GObjectClass *parent_class = NULL;
114 modest_account_protocol_get_type (void)
116 static GType my_type = 0;
119 static const GTypeInfo my_info = {
120 sizeof(ModestAccountProtocolClass),
121 NULL, /* base init */
122 NULL, /* base finalize */
123 (GClassInitFunc) modest_account_protocol_class_init,
124 NULL, /* class finalize */
125 NULL, /* class data */
126 sizeof(ModestAccountProtocol),
128 (GInstanceInitFunc) modest_account_protocol_instance_init,
132 my_type = g_type_register_static (MODEST_TYPE_PROTOCOL,
133 "ModestAccountProtocol",
140 modest_account_protocol_class_init (ModestAccountProtocolClass *klass)
142 GObjectClass *object_class;
143 ModestAccountProtocolClass *account_class;
145 object_class = (GObjectClass *) klass;
146 account_class = MODEST_ACCOUNT_PROTOCOL_CLASS (klass);
147 parent_class = g_type_class_peek_parent (klass);
148 object_class->finalize = modest_account_protocol_finalize;
149 object_class->set_property = modest_account_protocol_set_property;
150 object_class->get_property = modest_account_protocol_get_property;
152 g_object_class_install_property (object_class,
154 g_param_spec_uint ("port",
156 _("The standard port for the protocol"),
158 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
160 g_object_class_install_property (object_class,
162 g_param_spec_uint ("alternate-port",
164 _("The alternate port for the protocol (usually used in SSL)"),
166 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
168 g_object_class_install_property (object_class,
170 g_param_spec_gtype ("account-g-type",
171 _("Account factory GType"),
172 _("Account factory GType used for creating new instances."),
174 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
176 g_type_class_add_private (object_class,
177 sizeof(ModestAccountProtocolPrivate));
179 /* Virtual methods */
180 account_class->create_account_settings_dialog =
181 modest_account_protocol_create_account_settings_dialog_default;
182 account_class->get_easysetupwizard_tabs =
183 modest_account_protocol_get_easysetupwizard_tabs_default;
184 account_class->save_settings =
185 modest_account_protocol_save_settings_default;
186 account_class->save_wizard_settings =
187 modest_account_protocol_save_wizard_settings_default;
188 account_class->create_account =
189 modest_account_protocol_create_account_default;
190 account_class->get_wizard_response_override =
191 modest_account_protocol_get_wizard_response_override_default;
192 account_class->is_supported =
193 modest_account_protocol_is_supported_default;
194 account_class->check_support =
195 modest_account_protocol_check_support_default;
196 account_class->get_from =
197 modest_account_protocol_get_from_default;
198 account_class->get_from_list =
199 modest_account_protocol_get_from_list_default;
200 account_class->get_signature =
201 modest_account_protocol_get_signature_default;
202 account_class->get_icon =
203 modest_account_protocol_get_icon_default;
204 account_class->get_service_name =
205 modest_account_protocol_get_service_name_default;
209 modest_account_protocol_instance_init (ModestAccountProtocol *obj)
211 ModestAccountProtocolPrivate *priv;
213 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (obj);
216 priv->alternate_port = 0;
217 priv->account_g_type = 0;
218 priv->account_options = tny_simple_list_new ();
219 priv->custom_auth_mechs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
221 priv->account_dialogs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
225 remove_account (const gchar *account_name, GObject *account, GObject *account_to_remove)
227 return (account == account_to_remove);
231 account_dialog_weak_handler (ModestAccountProtocol *self, GObject *where_the_object_was)
233 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
235 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) remove_account, where_the_object_was);
239 dialogs_remove (const gchar *account_name, GObject *account_dialog, ModestAccountProtocol *self)
241 g_object_weak_unref (account_dialog, (GWeakNotify) account_dialog_weak_handler, self);
247 modest_account_protocol_finalize (GObject *obj)
249 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
250 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
252 if (priv->account_dialogs) {
253 g_hash_table_foreach_remove (priv->account_dialogs, (GHRFunc) dialogs_remove, obj);
254 g_hash_table_destroy (priv->account_dialogs);
257 if (priv->account_options)
258 g_object_unref (priv->account_options);
259 priv->account_options = NULL;
261 if (priv->custom_auth_mechs)
262 g_hash_table_destroy (priv->custom_auth_mechs);
263 priv->custom_auth_mechs = NULL;
265 G_OBJECT_CLASS (parent_class)->finalize (obj);
269 modest_account_protocol_get_property (GObject *obj,
274 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
275 ModestAccountProtocolPrivate *priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (protocol);
277 switch (property_id) {
279 g_value_set_uint (value, priv->port);
281 case PROP_ALTERNATE_PORT:
282 g_value_set_uint (value, priv->alternate_port);
284 case PROP_ACCOUNT_G_TYPE:
285 g_value_set_gtype (value, priv->account_g_type);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
294 modest_account_protocol_set_property (GObject *obj,
299 ModestAccountProtocol *protocol = MODEST_ACCOUNT_PROTOCOL (obj);
301 switch (property_id) {
303 modest_account_protocol_set_port (protocol, g_value_get_uint (value));
305 case PROP_ALTERNATE_PORT:
306 modest_account_protocol_set_alternate_port (protocol, g_value_get_uint (value));
308 case PROP_ACCOUNT_G_TYPE:
309 modest_account_protocol_set_account_g_type (protocol, g_value_get_gtype (value));
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
319 modest_account_protocol_new (const gchar *name, const gchar *display_name,
320 guint port, guint alternate_port,
321 GType account_g_type)
323 return g_object_new (MODEST_TYPE_ACCOUNT_PROTOCOL,
324 "display-name", display_name, "name", name,
325 "port", port, "alternate-port", alternate_port,
326 "account-g-type", account_g_type,
331 modest_account_protocol_get_port (ModestAccountProtocol *self)
333 ModestAccountProtocolPrivate *priv;
335 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
337 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
342 modest_account_protocol_set_port (ModestAccountProtocol *self,
345 ModestAccountProtocolPrivate *priv;
347 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
349 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
355 modest_account_protocol_get_alternate_port (ModestAccountProtocol *self)
357 ModestAccountProtocolPrivate *priv;
359 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
361 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
362 return priv->alternate_port;
366 modest_account_protocol_set_alternate_port (ModestAccountProtocol *self,
367 guint alternate_port)
369 ModestAccountProtocolPrivate *priv;
371 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
373 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
374 priv->alternate_port = alternate_port;
378 modest_account_protocol_get_account_g_type (ModestAccountProtocol *self)
380 ModestAccountProtocolPrivate *priv;
382 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), 0);
384 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
385 return priv->account_g_type;
389 modest_account_protocol_get_account_options (ModestAccountProtocol *self)
392 ModestAccountProtocolPrivate *priv;
394 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
395 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
397 result = tny_list_copy (priv->account_options);
403 modest_account_protocol_set_account_options (ModestAccountProtocol *self,
406 ModestAccountProtocolPrivate *priv;
408 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
409 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
411 if (priv->account_options) {
412 g_object_unref (priv->account_options);
413 priv->account_options = NULL;
415 priv->account_options = tny_list_copy (list);
419 modest_account_protocol_has_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), FALSE);
425 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
427 return g_hash_table_lookup_extended (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), NULL, NULL);
431 modest_account_protocol_get_custom_secure_auth_mech (ModestAccountProtocol *self,
432 ModestProtocolType auth_protocol_type)
434 ModestAccountProtocolPrivate *priv;
436 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
437 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
439 return (const gchar *) g_hash_table_lookup (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
443 modest_account_protocol_set_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type, const gchar *secure_auth_mech)
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_replace (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type), g_strdup (secure_auth_mech));
454 modest_account_protocol_unset_custom_secure_auth_mech (ModestAccountProtocol *self, ModestProtocolType auth_protocol_type)
456 ModestAccountProtocolPrivate *priv;
458 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
459 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
461 g_hash_table_remove (priv->custom_auth_mechs, GINT_TO_POINTER (auth_protocol_type));
466 modest_account_protocol_set_account_g_type (ModestAccountProtocol *self,
467 GType account_g_type)
469 ModestAccountProtocolPrivate *priv;
471 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
473 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
474 priv->account_g_type = account_g_type;
478 modest_account_protocol_create_account_default (ModestAccountProtocol *self)
480 ModestAccountProtocolPrivate *priv;
482 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
484 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
485 if (priv->account_g_type > 0) {
486 return g_object_new (priv->account_g_type, NULL);
493 modest_account_protocol_create_account (ModestAccountProtocol *self)
495 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
497 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account (self);
500 /* This is a template method for getting the account settings
501 dialog. It calls create_account_settings that must be implemented
502 by subclasses and then perform several common operations with the
504 ModestAccountSettingsDialog *
505 modest_account_protocol_get_account_settings_dialog (ModestAccountProtocol *self,
506 const gchar *account_name)
508 ModestAccountSettingsDialog *dialog;
509 ModestAccountSettings *settings;
510 ModestAccountProtocolPrivate *priv;
512 priv = MODEST_ACCOUNT_PROTOCOL_GET_PRIVATE (self);
513 dialog = g_hash_table_lookup (priv->account_dialogs, account_name);
515 if (dialog == NULL) {
517 dialog = MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->create_account_settings_dialog (self);
520 settings = modest_account_mgr_load_account_settings (modest_runtime_get_account_mgr (),
522 modest_account_settings_dialog_load_settings (dialog, settings);
524 /* Close dialog on response */
525 g_signal_connect_swapped (dialog,
527 G_CALLBACK (gtk_widget_destroy),
530 g_hash_table_insert (priv->account_dialogs, g_strdup (account_name), dialog);
531 g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) account_dialog_weak_handler, self);
538 modest_account_protocol_get_easysetupwizard_tabs (ModestAccountProtocol *self)
540 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_easysetupwizard_tabs (self);
545 modest_account_protocol_save_settings (ModestAccountProtocol *self,
546 ModestAccountSettingsDialog *dialog,
547 ModestAccountSettings *settings)
549 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_settings (self, dialog, settings);
553 modest_account_protocol_save_wizard_settings (ModestAccountProtocol *self,
555 ModestAccountSettings *settings)
557 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->save_wizard_settings (self, wizard_pages, settings);
560 /* Default implementations */
561 static ModestAccountSettingsDialog *
562 modest_account_protocol_create_account_settings_dialog_default (ModestAccountProtocol *self)
564 return modest_default_account_settings_dialog_new ();
567 static ModestPairList*
568 modest_account_protocol_get_easysetupwizard_tabs_default (ModestAccountProtocol *self)
570 g_warning ("You must implement get_easysetupwizard_tabs");
575 modest_account_protocol_save_settings_default (ModestAccountProtocol *self,
576 ModestAccountSettingsDialog *dialog,
577 ModestAccountSettings *settings)
579 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
580 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
581 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
583 g_warning ("You must implement save_settings");
587 modest_account_protocol_save_wizard_settings_default (ModestAccountProtocol *self,
589 ModestAccountSettings *settings)
591 g_return_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self));
592 g_return_if_fail (wizard_pages);
593 g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
595 g_warning ("You must implement save_wizard_settings");
598 static ModestWizardDialogResponseOverrideFunc
599 modest_account_protocol_get_wizard_response_override_default (ModestAccountProtocol *self)
601 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
606 ModestWizardDialogResponseOverrideFunc
607 modest_account_protocol_get_wizard_response_override (ModestAccountProtocol *self)
609 g_return_val_if_fail (MODEST_IS_ACCOUNT_PROTOCOL (self), NULL);
611 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_wizard_response_override (self);
615 modest_account_protocol_is_supported_default (ModestAccountProtocol *self)
621 modest_account_protocol_is_supported (ModestAccountProtocol *self)
623 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->is_supported (self);
627 modest_account_protocol_check_support_default (ModestAccountProtocol *self,
628 ModestAccountProtocolCheckSupportFunc func,
632 func (self, TRUE, userdata);
636 modest_account_protocol_check_support (ModestAccountProtocol *self,
637 ModestAccountProtocolCheckSupportFunc func,
640 MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->check_support (self, func, userdata);
644 modest_account_protocol_get_from (ModestAccountProtocol *self,
645 const gchar *account_id,
646 const gchar *mailbox)
648 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from (self, account_id, mailbox);
651 modest_account_protocol_get_from_default (ModestAccountProtocol *self,
652 const gchar *account_id,
653 const gchar *mailbox)
655 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
661 modest_account_protocol_get_from_list (ModestAccountProtocol *self,
662 const gchar *account_id)
664 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_from_list (self, account_id);
666 static ModestPairList *
667 modest_account_protocol_get_from_list_default (ModestAccountProtocol *self,
668 const gchar *account_id)
670 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
676 modest_account_protocol_get_signature (ModestAccountProtocol *self,
677 const gchar *account_id,
678 const gchar *mailbox,
679 gboolean *has_signature)
681 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_signature (self, account_id, mailbox, has_signature);
685 modest_account_protocol_get_signature_default (ModestAccountProtocol *self,
686 const gchar *account_id,
687 const gchar *mailbox,
688 gboolean *has_signature)
690 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
692 *has_signature = FALSE;
698 modest_account_protocol_get_icon (ModestAccountProtocol *self,
699 ModestAccountProtocolIconType icon_type,
703 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_icon (self, icon_type, object, icon_size);
706 static const GdkPixbuf *
707 modest_account_protocol_get_icon_default (ModestAccountProtocol *self, ModestAccountProtocolIconType icon_type,
708 GObject *object, guint icon_size)
710 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);
716 modest_account_protocol_get_service_name (ModestAccountProtocol *self,
717 const gchar *account_id,
718 const gchar *mailbox)
720 return MODEST_ACCOUNT_PROTOCOL_GET_CLASS (self)->get_service_name (self, account_id, mailbox);
724 modest_account_protocol_get_service_name_default (ModestAccountProtocol *self,
725 const gchar *account_id,
726 const gchar *mailbox)
728 g_return_val_if_fail (MODEST_ACCOUNT_PROTOCOL (self), NULL);