summaryrefslogtreecommitdiff
path: root/libmate-panel-applet/mate-panel-applet-gsettings.c
blob: 828676396ebc0a0c5beac7c7b4b0f9911418e31d (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
/*
 * mate-panel-applet-gsettings.c: panel applet preferences handling.
 *
 * Copyright (C) 2012 Stefano Karapetsas
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors:
 *     Stefano Karapetsas <stefano@karapetsas.com>
 */

#include <gtk/gtk.h>
#include <gio/gio.h>

#include "mate-panel-applet.h"
#include "mate-panel-applet-gsettings.h"

static GVariant *
add_to_dict (GVariant *dict, const gchar *schema, const gchar *path)
{
    GVariantIter iter;
    GVariantBuilder builder;
    gboolean is_schema_found;
    gboolean is_incorrect_schema;
    gint path_counter;

    gchar *key;
    gchar *value;

    g_variant_builder_init (&builder, (const GVariantType *) "a{ss}");
    g_variant_iter_init (&iter, dict);

    is_schema_found = FALSE;
    is_incorrect_schema = FALSE;
    path_counter = 0;

    while (g_variant_iter_next (&iter, "{ss}", &key, &value)) {
        gboolean path_is_found = FALSE;
        if (g_strcmp0 (value, path) == 0) {
            path_is_found = TRUE;
            path_counter++;
            if (g_strcmp0 (key, schema) == 0) {
                is_schema_found = TRUE;
            } else {
                // skip incoorect schema for path
                is_incorrect_schema = TRUE;
                g_free (key);
                g_free (value);
                continue;
            }
        }

        gboolean need_add_to_dict = !path_is_found || path_counter < 2;

        if (need_add_to_dict) {
            g_variant_builder_add (&builder, "{ss}", key, value);
        }

        g_free (key);
        g_free (value);
    }

    if (!is_schema_found) {
        g_variant_builder_add (&builder, "{ss}", schema, path);
    }

    if (!is_schema_found || is_incorrect_schema || (path_counter > 1)) {
        return g_variant_ref_sink (g_variant_builder_end (&builder));
    } else {
        g_variant_builder_clear (&builder);
        // no changes
        return NULL;
    }
}

static void
register_dconf_editor_relocatable_schema (const gchar *schema, const gchar *path)
{
    GSettings *dconf_editor_settings;
    dconf_editor_settings = g_settings_new ("ca.desrt.dconf-editor.Settings");

    if (dconf_editor_settings && g_settings_is_writable (dconf_editor_settings, "relocatable-schemas-user-paths")) {
        GVariant *relocatable_schemas = g_settings_get_value (dconf_editor_settings, "relocatable-schemas-user-paths");

        if (g_variant_is_of_type (relocatable_schemas, G_VARIANT_TYPE_DICTIONARY)) {
            GVariant * new_relocatable_schemas = add_to_dict (relocatable_schemas, schema, path);
            if (new_relocatable_schemas) {
                g_settings_set_value (dconf_editor_settings, "relocatable-schemas-user-paths", new_relocatable_schemas);
                g_variant_unref (new_relocatable_schemas);
            }
        }

        g_variant_unref (relocatable_schemas);
    }

    g_object_unref (dconf_editor_settings);
}

GSettings *
mate_panel_applet_settings_new (MatePanelApplet *applet, gchar *schema)
{
    GSettings *settings = NULL;
    gchar *path;

    g_return_val_if_fail (MATE_PANEL_IS_APPLET (applet), NULL);

    path = mate_panel_applet_get_preferences_path (applet);

    if (path) {
        settings = g_settings_new_with_path (schema, path);
        register_dconf_editor_relocatable_schema (schema, path);
        g_free (path);
    }

    return settings;
}

GList*
mate_panel_applet_settings_get_glist (GSettings *settings, gchar *key)
{
    gchar **array;
    GList *list = NULL;

    array = g_settings_get_strv (settings, key);
    if (array != NULL) {
        for (gint i = 0; array[i]; i++) {
            list = g_list_prepend (list, array[i]);
        }
        g_free (array);
    }
    return g_list_reverse (list);
}

void
mate_panel_applet_settings_set_glist (GSettings *settings, gchar *key, GList *list)
{
    GArray *array;

    array = g_array_new (TRUE, TRUE, sizeof (gchar *));
    for (GList *l = list; l; l = l->next) {
        array = g_array_append_val (array, l->data);
    }
    g_settings_set_strv (settings, key, (const gchar **) array->data);
    g_array_free (array, TRUE);
}

GSList*
mate_panel_applet_settings_get_gslist (GSettings *settings, gchar *key)
{
    gchar **array;
    GSList *list = NULL;

    array = g_settings_get_strv (settings, key);
    if (array != NULL) {
        for (gint i = 0; array[i]; i++) {
            list = g_slist_prepend (list, array[i]);
        }
        g_free (array);
    }
    return g_slist_reverse (list);
}

void
mate_panel_applet_settings_set_gslist (GSettings *settings, gchar *key, GSList *list)
{
    GArray *array;

    array = g_array_new (TRUE, TRUE, sizeof (gchar *));
    for (GSList *l = list; l; l = l->next) {
        array = g_array_append_val (array, l->data);
    }
    g_settings_set_strv (settings, key, (const gchar **) array->data);
    g_array_free (array, TRUE);
}