aboutsummaryrefslogtreecommitdiffstats
path: root/modules/settings/e-settings-deprecated.c
blob: b42952f67cd00fb666c51f568778cfd05d710fb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * e-settings-deprecated.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

/* This class is different from the others in this module.  Its purpose
 * is to transfer values from deprecated GSettings keys to the preferred
 * keys on startup, and keep them synchronized at all times for backward
 * compatibility. */

#include "e-settings-deprecated.h"

#include <shell/e-shell.h>
#include <libemail-engine/e-mail-enums.h>

#define E_SETTINGS_DEPRECATED_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_SETTINGS_DEPRECATED, ESettingsDeprecatedPrivate))

struct _ESettingsDeprecatedPrivate {
    GSettings *calendar_settings;
    gulong week_start_day_name_handler_id;
    gulong work_day_monday_handler_id;
    gulong work_day_tuesday_handler_id;
    gulong work_day_wednesday_handler_id;
    gulong work_day_thursday_handler_id;
    gulong work_day_friday_handler_id;
    gulong work_day_saturday_handler_id;
    gulong work_day_sunday_handler_id;

    GSettings *mail_settings;
    gulong forward_style_name_handler_id;
    gulong reply_style_name_handler_id;
    gulong image_loading_policy_handler_id;
};

/* Flag values used in the "working-days" key. */
enum {
    DEPRECATED_WORKING_DAYS_SUNDAY    = 1 << 0,
    DEPRECATED_WORKING_DAYS_MONDAY    = 1 << 1,
    DEPRECATED_WORKING_DAYS_TUESDAY   = 1 << 2,
    DEPRECATED_WORKING_DAYS_WEDNESDAY = 1 << 3,
    DEPRECATED_WORKING_DAYS_THURSDAY  = 1 << 4,
    DEPRECATED_WORKING_DAYS_FRIDAY    = 1 << 5,
    DEPRECATED_WORKING_DAYS_SATURDAY  = 1 << 6
};

G_DEFINE_DYNAMIC_TYPE (
    ESettingsDeprecated,
    e_settings_deprecated,
    E_TYPE_EXTENSION)

static void
settings_deprecated_week_start_day_name_cb (GSettings *settings,
                                            const gchar *key)
{
    GDateWeekday weekday;
    gint tm_wday;

    weekday = g_settings_get_enum (settings, "week-start-day-name");
    tm_wday = e_weekday_to_tm_wday (weekday);
    g_settings_set_int (settings, "week-start-day", tm_wday);
}

static void
settings_deprecated_work_day_monday_cb (GSettings *settings,
                                        const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-monday"))
        flags |= DEPRECATED_WORKING_DAYS_MONDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_MONDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_tuesday_cb (GSettings *settings,
                                         const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-tuesday"))
        flags |= DEPRECATED_WORKING_DAYS_TUESDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_TUESDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_wednesday_cb (GSettings *settings,
                                           const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-wednesday"))
        flags |= DEPRECATED_WORKING_DAYS_WEDNESDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_WEDNESDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_thursday_cb (GSettings *settings,
                                          const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-thursday"))
        flags |= DEPRECATED_WORKING_DAYS_THURSDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_THURSDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_friday_cb (GSettings *settings,
                                        const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-friday"))
        flags |= DEPRECATED_WORKING_DAYS_FRIDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_FRIDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_saturday_cb (GSettings *settings,
                                          const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-saturday"))
        flags |= DEPRECATED_WORKING_DAYS_SATURDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_SATURDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_work_day_sunday_cb (GSettings *settings,
                                        const gchar *key)
{
    gint flags;

    flags = g_settings_get_int (settings, "working-days");
    if (g_settings_get_boolean (settings, "work-day-sunday"))
        flags |= DEPRECATED_WORKING_DAYS_SUNDAY;
    else
        flags &= ~DEPRECATED_WORKING_DAYS_SUNDAY;
    g_settings_set_int (settings, "working-days", flags);
}

static void
settings_deprecated_forward_style_name_cb (GSettings *settings,
                                           const gchar *key)
{
    EMailForwardStyle style;

    style = g_settings_get_enum (settings, "forward-style-name");
    g_settings_set_int (settings, "forward-style", style);
}

static void
settings_deprecated_reply_style_name_cb (GSettings *settings,
                                         const gchar *key)
{
    /* XXX The "reply-style" key uses a completely different
     *     numbering than the EMailReplyStyle enum.  *sigh* */
    switch (g_settings_get_enum (settings, "reply-style-name")) {
        case E_MAIL_REPLY_STYLE_QUOTED:
            g_settings_set_int (settings, "reply-style", 2);
            break;
        case E_MAIL_REPLY_STYLE_DO_NOT_QUOTE:
            g_settings_set_int (settings, "reply-style", 3);
            break;
        case E_MAIL_REPLY_STYLE_ATTACH:
            g_settings_set_int (settings, "reply-style", 0);
            break;
        case E_MAIL_REPLY_STYLE_OUTLOOK:
            g_settings_set_int (settings, "reply-style", 1);
            break;
        default:
            g_warn_if_reached ();
    }
}

static void
settings_deprecated_image_loading_policy_cb (GSettings *settings,
                                             const gchar *key)
{
    EMailImageLoadingPolicy policy;

    policy = g_settings_get_enum (settings, "image-loading-policy");
    g_settings_set_int (settings, "load-http-images", policy);
}

static void
settings_deprecated_dispose (GObject *object)
{
    ESettingsDeprecatedPrivate *priv;

    priv = E_SETTINGS_DEPRECATED_GET_PRIVATE (object);

    if (priv->week_start_day_name_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->week_start_day_name_handler_id);
        priv->week_start_day_name_handler_id = 0;
    }

    if (priv->work_day_monday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_monday_handler_id);
        priv->work_day_monday_handler_id = 0;
    }

    if (priv->work_day_tuesday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_tuesday_handler_id);
        priv->work_day_tuesday_handler_id = 0;
    }

    if (priv->work_day_wednesday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_wednesday_handler_id);
        priv->work_day_wednesday_handler_id = 0;
    }

    if (priv->work_day_thursday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_thursday_handler_id);
        priv->work_day_thursday_handler_id = 0;
    }

    if (priv->work_day_friday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_friday_handler_id);
        priv->work_day_friday_handler_id = 0;
    }

    if (priv->work_day_saturday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_saturday_handler_id);
        priv->work_day_saturday_handler_id = 0;
    }

    if (priv->work_day_sunday_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->calendar_settings,
            priv->work_day_sunday_handler_id);
        priv->work_day_sunday_handler_id = 0;
    }

    if (priv->forward_style_name_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->mail_settings,
            priv->forward_style_name_handler_id);
        priv->forward_style_name_handler_id = 0;
    }

    if (priv->reply_style_name_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->mail_settings,
            priv->reply_style_name_handler_id);
        priv->reply_style_name_handler_id = 0;
    }

    if (priv->image_loading_policy_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->mail_settings,
            priv->image_loading_policy_handler_id);
        priv->image_loading_policy_handler_id = 0;
    }

    g_clear_object (&priv->calendar_settings);
    g_clear_object (&priv->mail_settings);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_settings_deprecated_parent_class)->dispose (object);
}

static void
settings_deprecated_constructed (GObject *object)
{
    ESettingsDeprecatedPrivate *priv;
    gulong handler_id;
    gint int_value;

    priv = E_SETTINGS_DEPRECATED_GET_PRIVATE (object);

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_settings_deprecated_parent_class)->
        constructed (object);

    /* Migrate values from deprecated to preferred keys. */

    int_value = g_settings_get_int (
        priv->calendar_settings, "week-start-day");
    g_settings_set_enum (
        priv->calendar_settings, "week-start-day-name",
        e_weekday_from_tm_wday (int_value));

    int_value = g_settings_get_int (
        priv->calendar_settings, "working-days");
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-monday",
        (int_value & DEPRECATED_WORKING_DAYS_MONDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-tuesday",
        (int_value & DEPRECATED_WORKING_DAYS_TUESDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-wednesday",
        (int_value & DEPRECATED_WORKING_DAYS_WEDNESDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-thursday",
        (int_value & DEPRECATED_WORKING_DAYS_THURSDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-friday",
        (int_value & DEPRECATED_WORKING_DAYS_FRIDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-saturday",
        (int_value & DEPRECATED_WORKING_DAYS_SATURDAY) != 0);
    g_settings_set_boolean (
        priv->calendar_settings, "work-day-sunday",
        (int_value & DEPRECATED_WORKING_DAYS_SUNDAY) != 0);

    int_value = g_settings_get_int (
        priv->mail_settings, "forward-style");
    g_settings_set_enum (
        priv->mail_settings, "forward-style-name", int_value);

    /* XXX The "reply-style" key uses a completely different
     *     numbering than the EMailReplyStyle enum.  *sigh* */
    switch (g_settings_get_int (priv->mail_settings, "reply-style")) {
        case 0:
            g_settings_set_enum (
                priv->mail_settings,
                "reply-style-name",
                E_MAIL_REPLY_STYLE_ATTACH);
            break;
        case 1:
            g_settings_set_enum (
                priv->mail_settings,
                "reply-style-name",
                E_MAIL_REPLY_STYLE_OUTLOOK);
            break;
        case 2:
            g_settings_set_enum (
                priv->mail_settings,
                "reply-style-name",
                E_MAIL_REPLY_STYLE_QUOTED);
            break;
        case 3:
            g_settings_set_enum (
                priv->mail_settings,
                "reply-style-name",
                E_MAIL_REPLY_STYLE_DO_NOT_QUOTE);
            break;
        default:
            /* do nothing */
            break;
    }

    int_value = g_settings_get_int (
        priv->mail_settings, "load-http-images");
    g_settings_set_enum (
        priv->mail_settings, "image-loading-policy", int_value);

    /* Write changes back to the deprecated keys. */

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::week-start-day-name",
        G_CALLBACK (settings_deprecated_week_start_day_name_cb), NULL);
    priv->week_start_day_name_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-monday",
        G_CALLBACK (settings_deprecated_work_day_monday_cb), NULL);
    priv->work_day_monday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-tuesday",
        G_CALLBACK (settings_deprecated_work_day_tuesday_cb), NULL);
    priv->work_day_tuesday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-wednesday",
        G_CALLBACK (settings_deprecated_work_day_wednesday_cb), NULL);
    priv->work_day_wednesday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-thursday",
        G_CALLBACK (settings_deprecated_work_day_thursday_cb), NULL);
    priv->work_day_thursday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-friday",
        G_CALLBACK (settings_deprecated_work_day_friday_cb), NULL);
    priv->work_day_friday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-saturday",
        G_CALLBACK (settings_deprecated_work_day_saturday_cb), NULL);
    priv->work_day_saturday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->calendar_settings, "changed::work-day-sunday",
        G_CALLBACK (settings_deprecated_work_day_sunday_cb), NULL);
    priv->work_day_sunday_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->mail_settings, "changed::forward-style-name",
        G_CALLBACK (settings_deprecated_forward_style_name_cb), NULL);
    priv->forward_style_name_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->mail_settings, "changed::reply-style-name",
        G_CALLBACK (settings_deprecated_reply_style_name_cb), NULL);
    priv->reply_style_name_handler_id = handler_id;

    handler_id = g_signal_connect (
        priv->mail_settings, "changed::image-loading-policy",
        G_CALLBACK (settings_deprecated_image_loading_policy_cb), NULL);
}

static void
e_settings_deprecated_class_init (ESettingsDeprecatedClass *class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

    g_type_class_add_private (class, sizeof (ESettingsDeprecatedPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = settings_deprecated_dispose;
    object_class->constructed = settings_deprecated_constructed;

    extension_class = E_EXTENSION_CLASS (class);
    extension_class->extensible_type = E_TYPE_SHELL;
}

static void
e_settings_deprecated_class_finalize (ESettingsDeprecatedClass *class)
{
}

static void
e_settings_deprecated_init (ESettingsDeprecated *extension)
{
    GSettings *settings;

    extension->priv = E_SETTINGS_DEPRECATED_GET_PRIVATE (extension);

    settings = g_settings_new ("org.gnome.evolution.calendar");
    extension->priv->calendar_settings = settings;

    settings = g_settings_new ("org.gnome.evolution.mail");
    extension->priv->mail_settings = settings;
}

void
e_settings_deprecated_type_register (GTypeModule *type_module)
{
    /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
     *     function, so we have to wrap it with a public function in
     *     order to register types from a separate compilation unit. */
    e_settings_deprecated_register_type (type_module);
}