| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2012 Red Hat, Inc. (www.redhat.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include "e-mail-config-ews-gal.h" |
| 9 |
|
| 10 |
#include <glib/gi18n-lib.h> |
| 11 |
|
| 12 |
#include <mail/e-mail-config-provider-page.h> |
| 13 |
|
| 14 |
#include "common/camel-ews-settings.h" |
| 15 |
|
| 16 |
#include "e-mail-config-ews-oal-combo-box.h" |
| 17 |
|
| 18 |
typedef struct _AsyncContext AsyncContext; |
| 19 |
|
| 20 |
struct _EMailConfigEwsGalPrivate { |
| 21 |
GtkWidget *toggle_button; /* not referenced */ |
| 22 |
GtkWidget *combo_box; /* not referenced */ |
| 23 |
GtkWidget *fetch_button; /* not referenced */ |
| 24 |
}; |
| 25 |
|
| 26 |
struct _AsyncContext { |
| 27 |
EMailConfigEwsGal *extension; |
| 28 |
EActivity *activity; |
| 29 |
}; |
| 30 |
|
| 31 |
G_DEFINE_DYNAMIC_TYPE_EXTENDED (EMailConfigEwsGal, e_mail_config_ews_gal, E_TYPE_EXTENSION, 0, |
| 32 |
G_ADD_PRIVATE_DYNAMIC (EMailConfigEwsGal)) |
| 33 |
|
| 34 |
static void |
| 35 |
async_context_free (AsyncContext *async_context) |
| 36 |
{ |
| 37 |
if (async_context->extension != NULL) |
| 38 |
g_object_unref (async_context->extension); |
| 39 |
|
| 40 |
if (async_context->activity != NULL) |
| 41 |
g_object_unref (async_context->activity); |
| 42 |
|
| 43 |
g_slice_free (AsyncContext, async_context); |
| 44 |
} |
| 45 |
|
| 46 |
static EMailConfigProviderPage * |
| 47 |
mail_config_ews_gal_get_provider_page (EMailConfigEwsGal *extension) |
| 48 |
{ |
| 49 |
EExtensible *extensible; |
| 50 |
|
| 51 |
extensible = e_extension_get_extensible (E_EXTENSION (extension)); |
| 52 |
|
| 53 |
return E_MAIL_CONFIG_PROVIDER_PAGE (extensible); |
| 54 |
} |
| 55 |
|
| 56 |
static gboolean |
| 57 |
mail_config_ews_gal_string_to_boolean (GBinding *binding, |
| 58 |
const GValue *source_value, |
| 59 |
GValue *target_value, |
| 60 |
gpointer unused) |
| 61 |
{ |
| 62 |
const gchar *v_string; |
| 63 |
gboolean v_boolean; |
| 64 |
|
| 65 |
v_string = g_value_get_string (source_value); |
| 66 |
v_boolean = (v_string != NULL && *v_string != '\0'); |
| 67 |
g_value_set_boolean (target_value, v_boolean); |
| 68 |
|
| 69 |
return TRUE; |
| 70 |
} |
| 71 |
|
| 72 |
static gboolean |
| 73 |
mail_config_ews_gal_oal_selected_to_active_id (GBinding *binding, |
| 74 |
const GValue *source_value, |
| 75 |
GValue *target_value, |
| 76 |
gpointer unused) |
| 77 |
{ |
| 78 |
GObject *target_object; |
| 79 |
GtkComboBox *combo_box; |
| 80 |
const gchar *selected; |
| 81 |
gchar *active_id; |
| 82 |
gchar *active_text; |
| 83 |
|
| 84 |
selected = g_value_get_string (source_value); |
| 85 |
|
| 86 |
if (selected == NULL) |
| 87 |
return FALSE; |
| 88 |
|
| 89 |
/* Selected value is of the form "id:name". */ |
| 90 |
active_id = g_strdup (selected); |
| 91 |
active_text = strrchr (active_id, ':'); |
| 92 |
|
| 93 |
if (active_text != NULL) { |
| 94 |
*active_text++ = '\0'; |
| 95 |
while (*active_text == '\\') |
| 96 |
active_text++; |
| 97 |
} else { |
| 98 |
g_free (active_id); |
| 99 |
return FALSE; |
| 100 |
} |
| 101 |
|
| 102 |
target_object = g_binding_get_target (binding); |
| 103 |
combo_box = GTK_COMBO_BOX (target_object); |
| 104 |
|
| 105 |
if (g_strcmp0 (active_id, gtk_combo_box_get_active_id (combo_box)) != 0) { |
| 106 |
/* The combo box might already have the OAL ID, in which case |
| 107 |
* we simply make it the active combo box row. Otherwise we |
| 108 |
* have to add a new row and make it the active row. */ |
| 109 |
if (!gtk_combo_box_set_active_id (combo_box, active_id)) { |
| 110 |
gtk_combo_box_text_append ( |
| 111 |
GTK_COMBO_BOX_TEXT (combo_box), |
| 112 |
active_id, active_text); |
| 113 |
gtk_combo_box_set_active_id (combo_box, active_id); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
g_value_set_string (target_value, active_id); |
| 118 |
g_free (active_id); |
| 119 |
|
| 120 |
return TRUE; |
| 121 |
} |
| 122 |
|
| 123 |
static gboolean |
| 124 |
mail_config_ews_gal_active_id_to_oal_selected (GBinding *binding, |
| 125 |
const GValue *source_value, |
| 126 |
GValue *target_value, |
| 127 |
gpointer unused) |
| 128 |
{ |
| 129 |
GObject *target_object; |
| 130 |
GtkComboBoxText *combo_box; |
| 131 |
const gchar *active_id; |
| 132 |
gchar *active_text; |
| 133 |
gchar *selected = NULL; |
| 134 |
|
| 135 |
target_object = g_binding_get_target (binding); |
| 136 |
combo_box = GTK_COMBO_BOX_TEXT (target_object); |
| 137 |
|
| 138 |
active_id = g_value_get_string (source_value); |
| 139 |
active_text = gtk_combo_box_text_get_active_text (combo_box); |
| 140 |
|
| 141 |
if (active_id != NULL && active_text != NULL) |
| 142 |
selected = g_strdup_printf ("%s:%s", active_id, active_text); |
| 143 |
|
| 144 |
g_value_set_string (target_value, selected); |
| 145 |
|
| 146 |
g_free (active_text); |
| 147 |
g_free (selected); |
| 148 |
|
| 149 |
return TRUE; |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
mail_config_ews_gal_fetch_list_cb (GObject *source_object, |
| 154 |
GAsyncResult *result, |
| 155 |
gpointer user_data) |
| 156 |
{ |
| 157 |
AsyncContext *async_context = user_data; |
| 158 |
EMailConfigEwsGal *extension; |
| 159 |
EAlertSink *alert_sink; |
| 160 |
GError *error = NULL; |
| 161 |
|
| 162 |
extension = async_context->extension; |
| 163 |
alert_sink = e_activity_get_alert_sink (async_context->activity); |
| 164 |
|
| 165 |
e_mail_config_ews_oal_combo_box_update_finish ( |
| 166 |
E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (source_object), |
| 167 |
result, &error); |
| 168 |
|
| 169 |
if (e_activity_handle_cancellation (async_context->activity, error)) { |
| 170 |
g_error_free (error); |
| 171 |
|
| 172 |
} else if (error != NULL) { |
| 173 |
e_alert_submit ( |
| 174 |
alert_sink, |
| 175 |
"ews:query-oal-error", |
| 176 |
error->message, NULL); |
| 177 |
g_error_free (error); |
| 178 |
} |
| 179 |
|
| 180 |
gtk_widget_set_sensitive (extension->priv->combo_box, TRUE); |
| 181 |
gtk_widget_set_sensitive (extension->priv->fetch_button, TRUE); |
| 182 |
|
| 183 |
async_context_free (async_context); |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
mail_config_ews_gal_fetch_list (EMailConfigEwsGal *extension) |
| 188 |
{ |
| 189 |
EActivity *activity; |
| 190 |
EMailConfigProviderPage *page; |
| 191 |
GtkWidget *combo_box; |
| 192 |
GCancellable *cancellable; |
| 193 |
AsyncContext *async_context; |
| 194 |
|
| 195 |
combo_box = extension->priv->combo_box; |
| 196 |
|
| 197 |
page = mail_config_ews_gal_get_provider_page (extension); |
| 198 |
|
| 199 |
activity = e_mail_config_activity_page_new_activity ( |
| 200 |
E_MAIL_CONFIG_ACTIVITY_PAGE (page)); |
| 201 |
cancellable = e_activity_get_cancellable (activity); |
| 202 |
|
| 203 |
e_activity_set_text (activity, _("Locating offline address books")); |
| 204 |
|
| 205 |
gtk_widget_set_sensitive (extension->priv->combo_box, FALSE); |
| 206 |
gtk_widget_set_sensitive (extension->priv->fetch_button, FALSE); |
| 207 |
|
| 208 |
async_context = g_slice_new0 (AsyncContext); |
| 209 |
async_context->extension = g_object_ref (extension); |
| 210 |
async_context->activity = activity; /* takes ownership */ |
| 211 |
|
| 212 |
e_mail_config_ews_oal_combo_box_update ( |
| 213 |
E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (combo_box), |
| 214 |
cancellable, mail_config_ews_gal_fetch_list_cb, |
| 215 |
async_context); |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
mail_config_ews_gal_fetch_button_clicked_cb (GtkButton *button, |
| 220 |
EMailConfigEwsGal *extension) |
| 221 |
{ |
| 222 |
mail_config_ews_gal_fetch_list (extension); |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
mail_config_ews_gal_constructed (GObject *object) |
| 227 |
{ |
| 228 |
EMailConfigEwsGal *extension; |
| 229 |
EMailConfigProviderPage *page; |
| 230 |
EMailConfigServiceBackend *backend; |
| 231 |
CamelSettings *settings; |
| 232 |
GtkWidget *container; |
| 233 |
GtkWidget *widget; |
| 234 |
GtkLabel *label; |
| 235 |
const gchar *text; |
| 236 |
gchar *markup; |
| 237 |
|
| 238 |
extension = E_MAIL_CONFIG_EWS_GAL (object); |
| 239 |
|
| 240 |
/* Chain up to parent's constructed() method. */ |
| 241 |
G_OBJECT_CLASS (e_mail_config_ews_gal_parent_class)->constructed (object); |
| 242 |
|
| 243 |
page = mail_config_ews_gal_get_provider_page (extension); |
| 244 |
backend = e_mail_config_provider_page_get_backend (page); |
| 245 |
settings = e_mail_config_service_backend_get_settings (backend); |
| 246 |
|
| 247 |
/* A provider page is created for every available CamelStore |
| 248 |
* class. We're only interested in the one for CamelEwsStore. |
| 249 |
* We determine this by the CamelSettings type returned. */ |
| 250 |
if (!CAMEL_IS_EWS_SETTINGS (settings)) |
| 251 |
return; |
| 252 |
|
| 253 |
container = e_mail_config_activity_page_get_internal_box (E_MAIL_CONFIG_ACTIVITY_PAGE (page)); |
| 254 |
|
| 255 |
text = _("Global Address List"); |
| 256 |
markup = g_markup_printf_escaped ("<b>%s</b>", text); |
| 257 |
widget = gtk_label_new (markup); |
| 258 |
gtk_widget_set_margin_top (widget, 6); |
| 259 |
gtk_label_set_use_markup (GTK_LABEL (widget), TRUE); |
| 260 |
gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5); |
| 261 |
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0); |
| 262 |
gtk_widget_show (widget); |
| 263 |
g_free (markup); |
| 264 |
|
| 265 |
widget = gtk_grid_new (); |
| 266 |
gtk_widget_set_margin_left (widget, 12); |
| 267 |
gtk_grid_set_row_spacing (GTK_GRID (widget), 6); |
| 268 |
gtk_grid_set_column_spacing (GTK_GRID (widget), 6); |
| 269 |
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0); |
| 270 |
gtk_widget_show (widget); |
| 271 |
|
| 272 |
e_binding_bind_property_full ( |
| 273 |
settings, "oaburl", |
| 274 |
widget, "sensitive", |
| 275 |
G_BINDING_SYNC_CREATE, |
| 276 |
mail_config_ews_gal_string_to_boolean, |
| 277 |
NULL, |
| 278 |
NULL, (GDestroyNotify) NULL); |
| 279 |
|
| 280 |
container = widget; |
| 281 |
|
| 282 |
text = _("Cache o_ffline address book"); |
| 283 |
widget = gtk_check_button_new_with_mnemonic (text); |
| 284 |
gtk_grid_attach (GTK_GRID (container), widget, 0, 0, 1, 1); |
| 285 |
extension->priv->toggle_button = widget; /* do not reference */ |
| 286 |
gtk_widget_show (widget); |
| 287 |
|
| 288 |
e_binding_bind_property ( |
| 289 |
settings, "oab-offline", |
| 290 |
widget, "active", |
| 291 |
G_BINDING_BIDIRECTIONAL | |
| 292 |
G_BINDING_SYNC_CREATE); |
| 293 |
|
| 294 |
widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE); |
| 295 |
gtk_widget_set_hexpand (widget, TRUE); |
| 296 |
gtk_widget_set_margin_left (widget, 12); |
| 297 |
gtk_box_set_spacing (GTK_BOX (widget), 6); |
| 298 |
gtk_grid_attach (GTK_GRID (container), widget, 0, 1, 1, 1); |
| 299 |
gtk_widget_show (widget); |
| 300 |
|
| 301 |
e_binding_bind_property ( |
| 302 |
settings, "oab-offline", |
| 303 |
widget, "sensitive", |
| 304 |
G_BINDING_SYNC_CREATE); |
| 305 |
|
| 306 |
container = widget; |
| 307 |
|
| 308 |
text = _("Select ad_dress list:"); |
| 309 |
widget = gtk_label_new_with_mnemonic (text); |
| 310 |
gtk_widget_set_margin_left (widget, 12); |
| 311 |
gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5); |
| 312 |
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0); |
| 313 |
gtk_widget_show (widget); |
| 314 |
|
| 315 |
label = GTK_LABEL (widget); |
| 316 |
|
| 317 |
widget = e_mail_config_ews_oal_combo_box_new (backend); |
| 318 |
gtk_label_set_mnemonic_widget (label, widget); |
| 319 |
gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0); |
| 320 |
extension->priv->combo_box = widget; /* do not reference */ |
| 321 |
gtk_widget_show (widget); |
| 322 |
|
| 323 |
e_binding_bind_property_full ( |
| 324 |
settings, "oal-selected", |
| 325 |
widget, "active-id", |
| 326 |
G_BINDING_BIDIRECTIONAL | |
| 327 |
G_BINDING_SYNC_CREATE, |
| 328 |
mail_config_ews_gal_oal_selected_to_active_id, |
| 329 |
mail_config_ews_gal_active_id_to_oal_selected, |
| 330 |
NULL, (GDestroyNotify) NULL); |
| 331 |
|
| 332 |
widget = gtk_button_new_with_label (_("Fetch List")); |
| 333 |
gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0); |
| 334 |
extension->priv->fetch_button = widget; /* do not reference */ |
| 335 |
gtk_widget_show (widget); |
| 336 |
|
| 337 |
g_signal_connect ( |
| 338 |
widget, "clicked", |
| 339 |
G_CALLBACK (mail_config_ews_gal_fetch_button_clicked_cb), |
| 340 |
extension); |
| 341 |
} |
| 342 |
|
| 343 |
static void |
| 344 |
e_mail_config_ews_gal_class_init (EMailConfigEwsGalClass *class) |
| 345 |
{ |
| 346 |
GObjectClass *object_class; |
| 347 |
EExtensionClass *extension_class; |
| 348 |
|
| 349 |
object_class = G_OBJECT_CLASS (class); |
| 350 |
object_class->constructed = mail_config_ews_gal_constructed; |
| 351 |
|
| 352 |
extension_class = E_EXTENSION_CLASS (class); |
| 353 |
extension_class->extensible_type = E_TYPE_MAIL_CONFIG_PROVIDER_PAGE; |
| 354 |
} |
| 355 |
|
| 356 |
static void |
| 357 |
e_mail_config_ews_gal_class_finalize (EMailConfigEwsGalClass *class) |
| 358 |
{ |
| 359 |
} |
| 360 |
|
| 361 |
static void |
| 362 |
e_mail_config_ews_gal_init (EMailConfigEwsGal *extension) |
| 363 |
{ |
| 364 |
extension->priv = e_mail_config_ews_gal_get_instance_private (extension); |
| 365 |
} |
| 366 |
|
| 367 |
void |
| 368 |
e_mail_config_ews_gal_type_register (GTypeModule *type_module) |
| 369 |
{ |
| 370 |
/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration |
| 371 |
* function, so we have to wrap it with a public function in |
| 372 |
* order to register types from a separate compilation unit. */ |
| 373 |
e_mail_config_ews_gal_register_type (type_module); |
| 374 |
} |
| 375 |
|