| File: | libcafe-desktop/cafe-gsettings.c |
| Warning: | line 112, column 7 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * cafe-gsettings.c: helper API for GSettings |
| 3 | * |
| 4 | * Copyright (C) 2013 Stefano Karapetsas |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301, USA. |
| 20 | * |
| 21 | * Authors: |
| 22 | * Stefano Karapetsas <stefano@karapetsas.com> |
| 23 | */ |
| 24 | |
| 25 | #include "cafe-gsettings.h" |
| 26 | |
| 27 | /** |
| 28 | * cafe_gsettings_schema_exists: |
| 29 | * @schema: schema to check. |
| 30 | * |
| 31 | * Check if a given schema is installed in GSettings. |
| 32 | * |
| 33 | * Return value: TRUE if schema exists, FALSE if not. |
| 34 | * |
| 35 | * Since: 1.7.1 |
| 36 | */ |
| 37 | gboolean |
| 38 | cafe_gsettings_schema_exists (const gchar* schema) |
| 39 | { |
| 40 | GSettingsSchemaSource *schema_source; |
| 41 | GSettingsSchema *schema_schema; |
| 42 | gboolean schema_exists; |
| 43 | |
| 44 | schema_source = g_settings_schema_source_get_default(); |
| 45 | schema_schema = g_settings_schema_source_lookup (schema_source, schema, FALSE(0)); |
| 46 | schema_exists = (schema_schema != NULL((void*)0)); |
| 47 | if (schema_schema) |
| 48 | g_settings_schema_unref (schema_schema); |
| 49 | |
| 50 | return schema_exists; |
| 51 | } |
| 52 | |
| 53 | /* (copied from gnome-panel) |
| 54 | * Adapted from is_valid_keyname() in glib (gio/glib-compile-schemas.c) |
| 55 | * Differences: |
| 56 | * - gettext support removed (we don't need translations here) |
| 57 | * - remove support for allow_any_name |
| 58 | */ |
| 59 | gboolean |
| 60 | cafe_gsettings_is_valid_keyname (const gchar *key, |
| 61 | GError **error) |
| 62 | { |
| 63 | gint i; |
| 64 | |
| 65 | if (key[0] == '\0') |
| 66 | { |
| 67 | g_set_error_literal (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
| 68 | "empty names are not permitted"); |
| 69 | return FALSE(0); |
| 70 | } |
| 71 | |
| 72 | if (!g_ascii_islower (key[0])((g_ascii_table[(guchar) (key[0])] & G_ASCII_LOWER) != 0)) |
| 73 | { |
| 74 | g_set_error (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
| 75 | "invalid name '%s': names must begin " |
| 76 | "with a lowercase letter", key); |
| 77 | return FALSE(0); |
| 78 | } |
| 79 | |
| 80 | for (i = 1; key[i]; i++) |
| 81 | { |
| 82 | if (key[i] != '-' && |
| 83 | !g_ascii_islower (key[i])((g_ascii_table[(guchar) (key[i])] & G_ASCII_LOWER) != 0) && |
| 84 | !g_ascii_isdigit (key[i])((g_ascii_table[(guchar) (key[i])] & G_ASCII_DIGIT) != 0)) |
| 85 | { |
| 86 | g_set_error (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
| 87 | "invalid name '%s': invalid character '%c'; " |
| 88 | "only lowercase letters, numbers and dash ('-') " |
| 89 | "are permitted.", key, key[i]); |
| 90 | return FALSE(0); |
| 91 | } |
| 92 | |
| 93 | if (key[i] == '-' && key[i + 1] == '-') |
| 94 | { |
| 95 | g_set_error (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
| 96 | "invalid name '%s': two successive dashes ('--') " |
| 97 | "are not permitted.", key); |
| 98 | return FALSE(0); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (key[i - 1] == '-') |
| 103 | { |
| 104 | g_set_error (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
| 105 | "invalid name '%s': the last character may not be a " |
| 106 | "dash ('-').", key); |
| 107 | return FALSE(0); |
| 108 | } |
| 109 | |
| 110 | if (i > 32) |
| 111 | { |
| 112 | g_set_error (error, G_MARKUP_ERRORg_markup_error_quark (), G_MARKUP_ERROR_INVALID_CONTENT, |
This statement is never executed | |
| 113 | "invalid name '%s': maximum length is 32", key); |
| 114 | return FALSE(0); |
| 115 | } |
| 116 | |
| 117 | return TRUE(!(0)); |
| 118 | } |
| 119 | |
| 120 | gboolean |
| 121 | cafe_gsettings_append_strv (GSettings *settings, |
| 122 | const gchar *key, |
| 123 | const gchar *value) |
| 124 | { |
| 125 | gchar **old; |
| 126 | gchar **new; |
| 127 | gint size; |
| 128 | gboolean retval; |
| 129 | |
| 130 | old = g_settings_get_strv (settings, key); |
| 131 | |
| 132 | for (size = 0; old[size] != NULL((void*)0); size++); |
| 133 | |
| 134 | size += 1; /* appended value */ |
| 135 | size += 1; /* NULL */ |
| 136 | |
| 137 | new = g_realloc_n (old, size, sizeof (gchar *)); |
| 138 | |
| 139 | new[size - 2] = g_strdup (value)g_strdup_inline (value); |
| 140 | new[size - 1] = NULL((void*)0); |
| 141 | |
| 142 | retval = g_settings_set_strv (settings, key, |
| 143 | (const gchar **) new); |
| 144 | |
| 145 | g_strfreev (new); |
| 146 | |
| 147 | return retval; |
| 148 | } |
| 149 | |
| 150 | gboolean |
| 151 | cafe_gsettings_remove_all_from_strv (GSettings *settings, |
| 152 | const gchar *key, |
| 153 | const gchar *value) |
| 154 | { |
| 155 | GArray *array; |
| 156 | gchar **old; |
| 157 | gint i; |
| 158 | gboolean retval; |
| 159 | |
| 160 | old = g_settings_get_strv (settings, key); |
| 161 | array = g_array_new (TRUE(!(0)), TRUE(!(0)), sizeof (gchar *)); |
| 162 | |
| 163 | for (i = 0; old[i] != NULL((void*)0); i++) { |
| 164 | if (g_strcmp0 (old[i], value) != 0) |
| 165 | array = g_array_append_val (array, old[i])g_array_append_vals (array, &(old[i]), 1); |
| 166 | } |
| 167 | |
| 168 | retval = g_settings_set_strv (settings, key, |
| 169 | (const gchar **) array->data); |
| 170 | |
| 171 | g_strfreev (old); |
| 172 | g_array_free (array, TRUE(!(0))); |
| 173 | |
| 174 | return retval; |
| 175 | } |
| 176 | |
| 177 | GSList* |
| 178 | cafe_gsettings_strv_to_gslist (const gchar *const *array) |
| 179 | { |
| 180 | GSList *list = NULL((void*)0); |
| 181 | gint i; |
| 182 | if (array != NULL((void*)0)) { |
| 183 | for (i = 0; array[i]; i++) { |
| 184 | list = g_slist_append (list, g_strdup (array[i])g_strdup_inline (array[i])); |
| 185 | } |
| 186 | } |
| 187 | return list; |
| 188 | } |