summaryrefslogtreecommitdiff
path: root/plugins/a11y-keyboard/msd-a11y-preferences-dialog.c
blob: e477a6158e207dca355f2a933a8e6e58caa9aaf5 (plain)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#include <dbus/dbus-glib.h>

#include <gio/gio.h>

#include "msd-a11y-preferences-dialog.h"

#define SM_DBUS_NAME      "org.gnome.SessionManager"
#define SM_DBUS_PATH      "/org/gnome/SessionManager"
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"

#define GTKBUILDER_UI_FILE "msd-a11y-preferences-dialog.ui"

#define KEY_A11Y_SCHEMA              "org.mate.accessibility-keyboard"
#define KEY_STICKY_KEYS_ENABLED      "stickykeys-enable"
#define KEY_BOUNCE_KEYS_ENABLED      "bouncekeys-enable"
#define KEY_SLOW_KEYS_ENABLED        "slowkeys-enable"
#define KEY_MOUSE_KEYS_ENABLED       "mousekeys-enable"
#define KEY_CAPSLOCK_BEEP_ENABLED    "capslock-beep-enable"

#define KEY_AT_SCHEMA                   "org.mate.applications-at"
#define KEY_AT_SCREEN_KEYBOARD_ENABLED  "screen-keyboard-enabled"
#define KEY_AT_SCREEN_MAGNIFIER_ENABLED "screen-magnifier-enabled"
#define KEY_AT_SCREEN_READER_ENABLED    "screen-reader-enabled"

#define FONT_RENDER_SCHEMA        "org.mate.font-rendering"
#define KEY_FONT_DPI              "dpi"

/* X servers sometimes lie about the screen's physical dimensions, so we cannot
 * compute an accurate DPI value.  When this happens, the user gets fonts that
 * are too huge or too tiny.  So, we see what the server returns:  if it reports
 * something outside of the range [DPI_LOW_REASONABLE_VALUE,
 * DPI_HIGH_REASONABLE_VALUE], then we assume that it is lying and we use
 * DPI_FALLBACK instead.
 *
 * See get_dpi_from_gsettings_or_server() below, and also
 * https://bugzilla.novell.com/show_bug.cgi?id=217790
 */
#define DPI_LOW_REASONABLE_VALUE 50
#define DPI_HIGH_REASONABLE_VALUE 500

#define DPI_FACTOR_LARGE   1.25
#define DPI_FACTOR_LARGER  1.5
#define DPI_FACTOR_LARGEST 2.0
#define DPI_DEFAULT        96

#define KEY_INTERFACE_SCHEMA   "org.mate.interface"
#define KEY_GTK_THEME          "gtk-theme"
#define KEY_COLOR_SCHEME       "gtk-color-scheme"
#define KEY_ICON_THEME         "icon-theme"

#define KEY_MARCO_SCHEMA    "org.mate.Marco"
#define KEY_MARCO_THEME     "theme"

#define HIGH_CONTRAST_THEME    "HighContrast"

struct MsdA11yPreferencesDialogPrivate
{
        GtkWidget *sticky_keys_checkbutton;
        GtkWidget *slow_keys_checkbutton;
        GtkWidget *bounce_keys_checkbutton;
#ifdef HAVE_LIBATSPI
        GtkWidget *capslock_beep_checkbutton;
#endif

        GtkWidget *large_print_checkbutton;
        GtkWidget *high_contrast_checkbutton;

        GtkWidget *screen_reader_checkbutton;
        GtkWidget *screen_keyboard_checkbutton;
        GtkWidget *screen_magnifier_checkbutton;

        GSettings *settings_a11y;
        GSettings *settings_at;
        GSettings *settings_interface;
        GSettings *settings_marco;
};

enum {
        PROP_0,
};

static void     msd_a11y_preferences_dialog_finalize    (GObject *object);

G_DEFINE_TYPE_WITH_PRIVATE (MsdA11yPreferencesDialog, msd_a11y_preferences_dialog, GTK_TYPE_DIALOG)

static void
msd_a11y_preferences_dialog_class_init (MsdA11yPreferencesDialogClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = msd_a11y_preferences_dialog_finalize;
}

static void
on_response (MsdA11yPreferencesDialog *dialog G_GNUC_UNUSED,
             gint                      response_id)
{
        switch (response_id) {
        default:
                break;
        }
}

static gboolean
config_get_bool (GSettings  *settings,
                 const char *key,
                 gboolean   *is_writable)
{
        int          enabled;

        if (is_writable) {
                *is_writable = g_settings_is_writable (settings, key);
        }

        enabled = g_settings_get_boolean (settings, key);

        return enabled;
}

static double
dpi_from_pixels_and_mm (int pixels,
                        int mm)
{
        double dpi;

        if (mm >= 1) {
                dpi = pixels / (mm / 25.4);
        } else {
                dpi = 0;
        }

        return dpi;
}

static double
get_dpi_from_x_server (void)
{
        GdkScreen *screen;
        double     dpi;
        int        scale;

        screen = gdk_screen_get_default ();
        if (screen != NULL) {
                double width_dpi;
                double height_dpi;

                Screen *xscreen = gdk_x11_screen_get_xscreen (screen);

                scale = gdk_window_get_scale_factor (gdk_screen_get_root_window (screen));
                width_dpi = dpi_from_pixels_and_mm (WidthOfScreen (xscreen), WidthMMOfScreen (xscreen));
                height_dpi = dpi_from_pixels_and_mm (HeightOfScreen (xscreen), HeightMMOfScreen (xscreen));

                if (width_dpi < DPI_LOW_REASONABLE_VALUE
                    || width_dpi > DPI_HIGH_REASONABLE_VALUE
                    || height_dpi < DPI_LOW_REASONABLE_VALUE
                    || height_dpi > DPI_HIGH_REASONABLE_VALUE) {
                        dpi = DPI_DEFAULT;
                } else {
                        dpi = (width_dpi + height_dpi) / 2.0;
                }

                dpi *= scale;

        } else {
                /* Huh!?  No screen? */
                dpi = DPI_DEFAULT;
        }

        return dpi;
}

static gboolean
config_get_large_print (gboolean *is_writable)
{
        GSettings   *settings;
        gboolean     ret;
        gdouble      x_dpi;
        gdouble      u_dpi;
        gdouble      gs_dpi;

        settings = g_settings_new (FONT_RENDER_SCHEMA);

        gs_dpi = g_settings_get_double (settings, KEY_FONT_DPI);

        if (gs_dpi != 0) {
                u_dpi = gs_dpi;
        } else {
                u_dpi = DPI_DEFAULT;
        }

        x_dpi = get_dpi_from_x_server ();

        g_object_unref (settings);

        g_debug ("MsdA11yPreferences: got x-dpi=%f user-dpi=%f", x_dpi, u_dpi);

        ret = (((double)DPI_FACTOR_LARGE * x_dpi) < u_dpi);

        return ret;
}

static void
config_set_large_print (gboolean enabled)
{
        GSettings *settings;

        settings = g_settings_new (FONT_RENDER_SCHEMA);

        if (enabled) {
                gdouble x_dpi;
                gdouble u_dpi;

                x_dpi = get_dpi_from_x_server ();
                u_dpi = (double)DPI_FACTOR_LARGER * x_dpi;

                g_debug ("MsdA11yPreferences: setting x-dpi=%f user-dpi=%f", x_dpi, u_dpi);

                g_settings_set_double (settings, KEY_FONT_DPI, u_dpi);
        } else {
                g_settings_reset (settings, KEY_FONT_DPI);
        }

        g_object_unref (settings);
}

static gboolean
config_get_high_contrast (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        gboolean ret;
        char    *gtk_theme;

        ret = FALSE;

        *is_writable = g_settings_is_writable (dialog->priv->settings_interface, KEY_GTK_THEME);
        gtk_theme = g_settings_get_string (dialog->priv->settings_interface, KEY_GTK_THEME);

        if (gtk_theme != NULL && strcmp (gtk_theme, HIGH_CONTRAST_THEME) == 0) {
                ret = TRUE;
        }
        g_free (gtk_theme);

        return ret;
}

static void
config_set_high_contrast (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        if (enabled) {
                g_settings_set_string (dialog->priv->settings_interface, KEY_GTK_THEME, HIGH_CONTRAST_THEME);
                g_settings_set_string (dialog->priv->settings_interface, KEY_ICON_THEME, HIGH_CONTRAST_THEME);
                /* there isn't a high contrast marco theme afaik */
        } else {
                g_settings_reset (dialog->priv->settings_interface, KEY_GTK_THEME);
                g_settings_reset (dialog->priv->settings_interface, KEY_ICON_THEME);
                g_settings_reset (dialog->priv->settings_marco, KEY_MARCO_THEME);
        }
}

static gboolean
config_get_sticky_keys (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_a11y, KEY_STICKY_KEYS_ENABLED, is_writable);
}

static void
config_set_sticky_keys (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_a11y, KEY_STICKY_KEYS_ENABLED, enabled);
}

static gboolean
config_get_bounce_keys (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_a11y, KEY_BOUNCE_KEYS_ENABLED, is_writable);
}

static void
config_set_bounce_keys (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_a11y, KEY_BOUNCE_KEYS_ENABLED, enabled);
}

static gboolean
config_get_slow_keys (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_a11y, KEY_SLOW_KEYS_ENABLED, is_writable);
}

static void
config_set_slow_keys (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_a11y, KEY_SLOW_KEYS_ENABLED, enabled);
}

#ifdef HAVE_LIBATSPI
static gboolean
config_get_capslock_beep (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_a11y, KEY_CAPSLOCK_BEEP_ENABLED, is_writable);
}

static void
config_set_capslock_beep (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_a11y, KEY_CAPSLOCK_BEEP_ENABLED, enabled);
}
#endif

static gboolean
config_have_at_gsettings_condition (const char *condition)
{
        DBusGProxy      *sm_proxy;
        DBusGConnection *connection;
        GError          *error;
        gboolean         res;
        gboolean         is_handled;

        error = NULL;
        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
        if (connection == NULL) {
                g_warning ("Unable to connect to session bus: %s", error->message);
                return FALSE;
        }
        sm_proxy = dbus_g_proxy_new_for_name (connection,
                                              SM_DBUS_NAME,
                                              SM_DBUS_PATH,
                                              SM_DBUS_INTERFACE);
        if (sm_proxy == NULL) {
                return FALSE;
        }

        is_handled = FALSE;
        res = dbus_g_proxy_call (sm_proxy,
                                 "IsAutostartConditionHandled",
                                 &error,
                                 G_TYPE_STRING, condition,
                                 G_TYPE_INVALID,
                                 G_TYPE_BOOLEAN, &is_handled,
                                 G_TYPE_INVALID);
        if (! res) {
                g_warning ("Unable to call IsAutostartConditionHandled (%s): %s",
                           condition,
                           error->message);
        }

        g_object_unref (sm_proxy);

        return is_handled;
}

static gboolean
config_get_at_screen_reader (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_at, KEY_AT_SCREEN_READER_ENABLED, is_writable);
}

static gboolean
config_get_at_screen_keyboard (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_at, KEY_AT_SCREEN_KEYBOARD_ENABLED, is_writable);
}

static gboolean
config_get_at_screen_magnifier (MsdA11yPreferencesDialog *dialog, gboolean *is_writable)
{
        return config_get_bool (dialog->priv->settings_at, KEY_AT_SCREEN_MAGNIFIER_ENABLED, is_writable);
}

static void
config_set_at_screen_reader (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_at, KEY_AT_SCREEN_READER_ENABLED, enabled);
}

static void
config_set_at_screen_keyboard (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_at, KEY_AT_SCREEN_KEYBOARD_ENABLED, enabled);
}

static void
config_set_at_screen_magnifier (MsdA11yPreferencesDialog *dialog, gboolean enabled)
{
        g_settings_set_boolean (dialog->priv->settings_at, KEY_AT_SCREEN_MAGNIFIER_ENABLED, enabled);
}

static void
on_sticky_keys_checkbutton_toggled (GtkToggleButton          *button,
                                    MsdA11yPreferencesDialog *dialog)
{
        config_set_sticky_keys (dialog, gtk_toggle_button_get_active (button));
}

static void
on_bounce_keys_checkbutton_toggled (GtkToggleButton          *button,
                                    MsdA11yPreferencesDialog *dialog)
{
        config_set_bounce_keys (dialog, gtk_toggle_button_get_active (button));
}

static void
on_slow_keys_checkbutton_toggled (GtkToggleButton          *button,
                                  MsdA11yPreferencesDialog *dialog)
{
        config_set_slow_keys (dialog, gtk_toggle_button_get_active (button));
}

#ifdef HAVE_LIBATSPI
static void
on_capslock_beep_checkbutton_toggled (GtkToggleButton          *button,
                                      MsdA11yPreferencesDialog *dialog)
{
        config_set_capslock_beep (dialog, gtk_toggle_button_get_active (button));
}
#endif

static void
on_high_contrast_checkbutton_toggled (GtkToggleButton          *button,
                                      MsdA11yPreferencesDialog *dialog)
{
        config_set_high_contrast (dialog, gtk_toggle_button_get_active (button));
}

static void
on_at_screen_reader_checkbutton_toggled (GtkToggleButton          *button,
                                         MsdA11yPreferencesDialog *dialog)
{
        config_set_at_screen_reader (dialog, gtk_toggle_button_get_active (button));
}

static void
on_at_screen_keyboard_checkbutton_toggled (GtkToggleButton          *button,
                                           MsdA11yPreferencesDialog *dialog)
{
        config_set_at_screen_keyboard (dialog, gtk_toggle_button_get_active (button));
}

static void
on_at_screen_magnifier_checkbutton_toggled (GtkToggleButton          *button,
                                            MsdA11yPreferencesDialog *dialog)
{
        config_set_at_screen_magnifier (dialog, gtk_toggle_button_get_active (button));
}

static void
on_large_print_checkbutton_toggled (GtkToggleButton          *button,
                                    MsdA11yPreferencesDialog *dialog G_GNUC_UNUSED)
{
        config_set_large_print (gtk_toggle_button_get_active (button));
}

static void
ui_set_sticky_keys (MsdA11yPreferencesDialog *dialog,
                    gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->sticky_keys_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->sticky_keys_checkbutton), enabled);
        }
}

static void
ui_set_bounce_keys (MsdA11yPreferencesDialog *dialog,
                    gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->bounce_keys_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->bounce_keys_checkbutton), enabled);
        }
}

static void
ui_set_slow_keys (MsdA11yPreferencesDialog *dialog,
                  gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->slow_keys_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->slow_keys_checkbutton), enabled);
        }
}

#ifdef HAVE_LIBATSPI
static void
ui_set_capslock_beep (MsdA11yPreferencesDialog *dialog,
                      gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->capslock_beep_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->capslock_beep_checkbutton), enabled);
        }
}
#endif

static void
ui_set_high_contrast (MsdA11yPreferencesDialog *dialog,
                      gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->high_contrast_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->high_contrast_checkbutton), enabled);
        }
}

static void
ui_set_at_screen_reader (MsdA11yPreferencesDialog *dialog,
                         gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_reader_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_reader_checkbutton), enabled);
        }
}

static void
ui_set_at_screen_keyboard (MsdA11yPreferencesDialog *dialog,
                           gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_keyboard_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_keyboard_checkbutton), enabled);
        }
}

static void
ui_set_at_screen_magnifier (MsdA11yPreferencesDialog *dialog,
                            gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_magnifier_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->screen_magnifier_checkbutton), enabled);
        }
}

static void
ui_set_large_print (MsdA11yPreferencesDialog *dialog,
                    gboolean                  enabled)
{
        gboolean active;

        active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->large_print_checkbutton));
        if (active != enabled) {
                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->large_print_checkbutton), enabled);
        }
}

static void
key_changed_cb (GSettings                *settings,
                gchar                    *key,
                MsdA11yPreferencesDialog *dialog)
{
        if (g_strcmp0 (key, KEY_STICKY_KEYS_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_sticky_keys (dialog, enabled);
        } else if (g_strcmp0 (key, KEY_BOUNCE_KEYS_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_bounce_keys (dialog, enabled);
        } else if (g_strcmp0 (key, KEY_SLOW_KEYS_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_slow_keys (dialog, enabled);
#ifdef HAVE_LIBATSPI
        } else if (g_strcmp0 (key, KEY_CAPSLOCK_BEEP_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_capslock_beep (dialog, enabled);
#endif
        } else if (g_strcmp0 (key, KEY_AT_SCREEN_READER_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_at_screen_reader (dialog, enabled);
        } else if (g_strcmp0 (key, KEY_AT_SCREEN_KEYBOARD_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_at_screen_keyboard (dialog, enabled);
        } else if (strcmp (key, KEY_AT_SCREEN_MAGNIFIER_ENABLED) == 0) {
                gboolean enabled;
                enabled = g_settings_get_boolean (settings, key);
                ui_set_at_screen_magnifier (dialog, enabled);
        } else {
                g_debug ("Config key not handled: %s", key);
        }
}

static void
setup_dialog (MsdA11yPreferencesDialog *dialog,
              GtkBuilder               *builder)
{
        GtkWidget   *widget;
        gboolean     enabled;
        gboolean     is_writable;

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "sticky_keys_checkbutton"));
        dialog->priv->sticky_keys_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_sticky_keys_checkbutton_toggled),
                          dialog);
        enabled = config_get_sticky_keys (dialog, &is_writable);
        ui_set_sticky_keys (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "bounce_keys_checkbutton"));
        dialog->priv->bounce_keys_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_bounce_keys_checkbutton_toggled),
                          dialog);
        enabled = config_get_bounce_keys (dialog, &is_writable);
        ui_set_bounce_keys (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "slow_keys_checkbutton"));
        dialog->priv->slow_keys_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_slow_keys_checkbutton_toggled),
                          dialog);
        enabled = config_get_slow_keys (dialog, &is_writable);
        ui_set_slow_keys (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "capslock_beep_checkbutton"));
#ifndef HAVE_LIBATSPI
        gtk_widget_hide (widget);
        gtk_widget_set_no_show_all (widget, TRUE);
#else
        dialog->priv->capslock_beep_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_capslock_beep_checkbutton_toggled),
                          dialog);
        enabled = config_get_capslock_beep (dialog, &is_writable);
        ui_set_capslock_beep (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }
#endif

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "high_contrast_checkbutton"));
        dialog->priv->high_contrast_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_high_contrast_checkbutton_toggled),
                          dialog);
        enabled = config_get_high_contrast (dialog, &is_writable);
        ui_set_high_contrast (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "at_screen_keyboard_checkbutton"));
        dialog->priv->screen_keyboard_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_at_screen_keyboard_checkbutton_toggled),
                          dialog);
        enabled = config_get_at_screen_keyboard (dialog, &is_writable);
        ui_set_at_screen_keyboard (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }
        gtk_widget_set_no_show_all (widget, TRUE);
        if (config_have_at_gsettings_condition ("MATE " KEY_AT_SCHEMA " " KEY_AT_SCREEN_KEYBOARD_ENABLED)) {
                gtk_widget_show_all (widget);
        } else {
                gtk_widget_hide (widget);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "at_screen_reader_checkbutton"));
        dialog->priv->screen_reader_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_at_screen_reader_checkbutton_toggled),
                          dialog);
        enabled = config_get_at_screen_reader (dialog, &is_writable);
        ui_set_at_screen_reader (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }
        gtk_widget_set_no_show_all (widget, TRUE);
        if (config_have_at_gsettings_condition ("MATE " KEY_AT_SCHEMA " " KEY_AT_SCREEN_READER_ENABLED)) {
                gtk_widget_show_all (widget);
        } else {
                gtk_widget_hide (widget);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "at_screen_magnifier_checkbutton"));
        dialog->priv->screen_magnifier_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_at_screen_magnifier_checkbutton_toggled),
                          dialog);
        enabled = config_get_at_screen_magnifier (dialog, &is_writable);
        ui_set_at_screen_magnifier (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }
        gtk_widget_set_no_show_all (widget, TRUE);
        if (config_have_at_gsettings_condition ("MATE " KEY_AT_SCHEMA " " KEY_AT_SCREEN_MAGNIFIER_ENABLED)) {
                gtk_widget_show_all (widget);
        } else {
                gtk_widget_hide (widget);
        }

        widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                     "large_print_checkbutton"));
        dialog->priv->large_print_checkbutton = widget;
        g_signal_connect (widget,
                          "toggled",
                          G_CALLBACK (on_large_print_checkbutton_toggled),
                          dialog);
        enabled = config_get_large_print (&is_writable);
        ui_set_large_print (dialog, enabled);
        if (! is_writable) {
                gtk_widget_set_sensitive (widget, FALSE);
        }

        g_signal_connect (dialog->priv->settings_a11y,
                          "changed",
                          G_CALLBACK (key_changed_cb),
                          dialog);
        g_signal_connect (dialog->priv->settings_at,
                          "changed",
                          G_CALLBACK (key_changed_cb),
                          dialog);
}

static void
msd_a11y_preferences_dialog_init (MsdA11yPreferencesDialog *dialog)
{
        static const gchar *ui_file_path = GTKBUILDERDIR "/" GTKBUILDER_UI_FILE;
        gchar *objects[] = {"main_box", NULL};
        GError *error = NULL;
        GtkBuilder  *builder;

        dialog->priv = msd_a11y_preferences_dialog_get_instance_private (dialog);

        dialog->priv->settings_a11y = g_settings_new (KEY_A11Y_SCHEMA);
        dialog->priv->settings_at = g_settings_new (KEY_AT_SCHEMA);
        dialog->priv->settings_interface = g_settings_new (KEY_INTERFACE_SCHEMA);
        dialog->priv->settings_marco = g_settings_new (KEY_MARCO_SCHEMA);

        builder = gtk_builder_new ();
        gtk_builder_set_translation_domain (builder, PACKAGE);
        if (gtk_builder_add_objects_from_file (builder, ui_file_path, objects,
                                               &error) == 0) {
                g_warning ("Could not load A11Y-UI: %s", error->message);
                g_error_free (error);
        } else {
                GtkWidget *widget;

                widget = GTK_WIDGET (gtk_builder_get_object (builder,
                                                             "main_box"));
                gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                                   widget);
                gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
                setup_dialog (dialog, builder);
       }

        g_object_unref (builder);

        gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
        gtk_window_set_title (GTK_WINDOW (dialog), _("Universal Access Preferences"));
        gtk_window_set_icon_name (GTK_WINDOW (dialog), "preferences-desktop-accessibility");
        g_object_set (dialog,
                      "resizable", FALSE,
                      NULL);

        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                                GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
                                NULL);
        g_signal_connect (dialog,
                          "response",
                          G_CALLBACK (on_response),
                          dialog);

        gtk_widget_show_all (GTK_WIDGET (dialog));
}

static void
msd_a11y_preferences_dialog_finalize (GObject *object)
{
        MsdA11yPreferencesDialog *dialog;

        g_return_if_fail (object != NULL);
        g_return_if_fail (MSD_IS_A11Y_PREFERENCES_DIALOG (object));

        dialog = MSD_A11Y_PREFERENCES_DIALOG (object);

        g_return_if_fail (dialog->priv != NULL);

        g_object_unref (dialog->priv->settings_a11y);
        g_object_unref (dialog->priv->settings_at);
        g_object_unref (dialog->priv->settings_interface);
        g_object_unref (dialog->priv->settings_marco);

        G_OBJECT_CLASS (msd_a11y_preferences_dialog_parent_class)->finalize (object);
}

GtkWidget *
msd_a11y_preferences_dialog_new (void)
{
        GObject *object;

        object = g_object_new (MSD_TYPE_A11Y_PREFERENCES_DIALOG,
                               NULL);

        return GTK_WIDGET (object);
}