GSettingsEngine.vala 25.7 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
Lucas Beeler's avatar
Lucas Beeler committed
2 3
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
4
 * See the COPYING file in this distribution.
Lucas Beeler's avatar
Lucas Beeler committed
5 6 7 8 9 10 11 12 13 14
 */

public class GSettingsConfigurationEngine : ConfigurationEngine, GLib.Object {
    private const string ROOT_SCHEMA_NAME = "org.yorba.shotwell";
    private const string PREFS_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".preferences";
    private const string UI_PREFS_SCHEMA_NAME = PREFS_SCHEMA_NAME + ".ui";
    private const string SLIDESHOW_PREFS_SCHEMA_NAME = PREFS_SCHEMA_NAME + ".slideshow";
    private const string WINDOW_PREFS_SCHEMA_NAME =  PREFS_SCHEMA_NAME + ".window";
    private const string FILES_PREFS_SCHEMA_NAME = PREFS_SCHEMA_NAME + ".files";
    private const string EDITING_PREFS_SCHEMA_NAME = PREFS_SCHEMA_NAME + ".editing";
15
    private const string EXPORT_PREFS_SCHEMA_NAME = PREFS_SCHEMA_NAME + ".export";
Lucas Beeler's avatar
Lucas Beeler committed
16 17 18
    private const string VIDEO_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".video";
    private const string PRINTING_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".printing";
    private const string SHARING_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".sharing";
19
    private const string IMPORTING_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".dataimports";
20
    private const string CROP_SCHEMA_NAME = ROOT_SCHEMA_NAME + ".crop-settings";
Lucas Beeler's avatar
Lucas Beeler committed
21
    private const string SYSTEM_DESKTOP_SCHEMA_NAME = "org.gnome.desktop.background";
22
    private const string SYSTEM_SCREENSAVER_SCHEMA_NAME = "org.gnome.desktop.screensaver";
Lucas Beeler's avatar
Lucas Beeler committed
23 24 25 26 27
    private const string PLUGINS_ENABLE_DISABLE_SCHEMA_NAME = ROOT_SCHEMA_NAME +
        ".plugins.enable-state";

    private string[] schema_names;
    private string[] key_names;
Jens Georg's avatar
Jens Georg committed
28
    private Gee.HashMap<string, Settings> settings_cache = new Gee.HashMap<string, Settings>();
Lucas Beeler's avatar
Lucas Beeler committed
29 30 31 32 33
    
    public GSettingsConfigurationEngine() {
        schema_names = new string[ConfigurableProperty.NUM_PROPERTIES];

        schema_names[ConfigurableProperty.AUTO_IMPORT_FROM_LIBRARY] = FILES_PREFS_SCHEMA_NAME;
Jens Georg's avatar
Jens Georg committed
34
        schema_names[ConfigurableProperty.GTK_THEME_VARIANT] = UI_PREFS_SCHEMA_NAME;
35 36
        schema_names[ConfigurableProperty.TRANSPARENT_BACKGROUND_TYPE] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.TRANSPARENT_BACKGROUND_COLOR] = UI_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
37 38 39
        schema_names[ConfigurableProperty.COMMIT_METADATA_TO_MASTERS] = FILES_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DESKTOP_BACKGROUND_FILE] = SYSTEM_DESKTOP_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DESKTOP_BACKGROUND_MODE] = SYSTEM_DESKTOP_SCHEMA_NAME;
40 41
        schema_names[ConfigurableProperty.SCREENSAVER_FILE] = SYSTEM_SCREENSAVER_SCHEMA_NAME;
        schema_names[ConfigurableProperty.SCREENSAVER_MODE] = SYSTEM_SCREENSAVER_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
42 43 44 45 46 47 48
        schema_names[ConfigurableProperty.DIRECTORY_PATTERN] = FILES_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DIRECTORY_PATTERN_CUSTOM] = FILES_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DIRECT_WINDOW_HEIGHT] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DIRECT_WINDOW_MAXIMIZE] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DIRECT_WINDOW_WIDTH] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DISPLAY_BASIC_PROPERTIES] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DISPLAY_EXTENDED_PROPERTIES] = UI_PREFS_SCHEMA_NAME;
49
        schema_names[ConfigurableProperty.DISPLAY_SIDEBAR] = UI_PREFS_SCHEMA_NAME;
50
        schema_names[ConfigurableProperty.DISPLAY_TOOLBAR] = UI_PREFS_SCHEMA_NAME;
51
        schema_names[ConfigurableProperty.DISPLAY_SEARCH_BAR] = UI_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
52 53 54
        schema_names[ConfigurableProperty.DISPLAY_PHOTO_RATINGS] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DISPLAY_PHOTO_TAGS] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.DISPLAY_PHOTO_TITLES] = UI_PREFS_SCHEMA_NAME;
55
        schema_names[ConfigurableProperty.DISPLAY_PHOTO_COMMENTS] = UI_PREFS_SCHEMA_NAME;
56
        schema_names[ConfigurableProperty.DISPLAY_EVENT_COMMENTS] = UI_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
57 58 59
        schema_names[ConfigurableProperty.EVENT_PHOTOS_SORT_ASCENDING] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EVENT_PHOTOS_SORT_BY] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EVENTS_SORT_ASCENDING] = UI_PREFS_SCHEMA_NAME;
60 61 62 63 64 65
        schema_names[ConfigurableProperty.EXPORT_CONSTRAINT] = EXPORT_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXPORT_EXPORT_FORMAT_MODE] =  EXPORT_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXPORT_EXPORT_METADATA] =  EXPORT_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXPORT_PHOTO_FILE_FORMAT] =  EXPORT_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXPORT_QUALITY] =  EXPORT_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXPORT_SCALE] =  EXPORT_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
66 67
        schema_names[ConfigurableProperty.EXTERNAL_PHOTO_APP] = EDITING_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.EXTERNAL_RAW_APP] = EDITING_PREFS_SCHEMA_NAME;
68
        schema_names[ConfigurableProperty.HIDE_PHOTOS_ALREADY_IMPORTED] = UI_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
69 70
        schema_names[ConfigurableProperty.IMPORT_DIR] = FILES_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.KEEP_RELATIVITY] = UI_PREFS_SCHEMA_NAME;
71 72 73
        schema_names[ConfigurableProperty.LAST_CROP_HEIGHT] = CROP_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LAST_CROP_MENU_CHOICE] = CROP_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LAST_CROP_WIDTH] = CROP_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
74
        schema_names[ConfigurableProperty.LAST_USED_SERVICE] = SHARING_SCHEMA_NAME;
75
        schema_names[ConfigurableProperty.LAST_USED_DATAIMPORTS_SERVICE] = IMPORTING_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
76 77 78 79 80 81 82
        schema_names[ConfigurableProperty.LIBRARY_PHOTOS_SORT_ASCENDING] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LIBRARY_PHOTOS_SORT_BY] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LIBRARY_WINDOW_HEIGHT] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LIBRARY_WINDOW_MAXIMIZE] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.LIBRARY_WINDOW_WIDTH] = WINDOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.MODIFY_ORIGINALS] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PHOTO_THUMBNAIL_SCALE] = UI_PREFS_SCHEMA_NAME;
83
        schema_names[ConfigurableProperty.PIN_TOOLBAR_STATE] = UI_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
84 85 86 87 88 89 90 91 92 93
        schema_names[ConfigurableProperty.PRINTING_CONTENT_HEIGHT] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_CONTENT_LAYOUT] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_CONTENT_PPI] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_CONTENT_UNITS] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_CONTENT_WIDTH] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_IMAGES_PER_PAGE] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_MATCH_ASPECT_RATIO] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_PRINT_TITLES] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_SIZE_SELECTION] = PRINTING_SCHEMA_NAME;
        schema_names[ConfigurableProperty.PRINTING_TITLES_FONT] = PRINTING_SCHEMA_NAME;
94
        schema_names[ConfigurableProperty.RAW_DEVELOPER_DEFAULT] = FILES_PREFS_SCHEMA_NAME;;
Lucas Beeler's avatar
Lucas Beeler committed
95 96 97 98 99
        schema_names[ConfigurableProperty.SHOW_WELCOME_DIALOG] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.SIDEBAR_POSITION] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.SLIDESHOW_DELAY] = SLIDESHOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.SLIDESHOW_TRANSITION_DELAY] = SLIDESHOW_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.SLIDESHOW_TRANSITION_EFFECT_ID] = SLIDESHOW_PREFS_SCHEMA_NAME;
100
        schema_names[ConfigurableProperty.SLIDESHOW_SHOW_TITLE] = SLIDESHOW_PREFS_SCHEMA_NAME;
Lucas Beeler's avatar
Lucas Beeler committed
101 102 103 104 105 106 107
        schema_names[ConfigurableProperty.USE_24_HOUR_TIME] = UI_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.USE_LOWERCASE_FILENAMES] = FILES_PREFS_SCHEMA_NAME;
        schema_names[ConfigurableProperty.VIDEO_INTERPRETER_STATE_COOKIE] = VIDEO_SCHEMA_NAME;
        
        key_names = new string[ConfigurableProperty.NUM_PROPERTIES];
        
        key_names[ConfigurableProperty.AUTO_IMPORT_FROM_LIBRARY] = "auto-import";
Jens Georg's avatar
Jens Georg committed
108
        key_names[ConfigurableProperty.GTK_THEME_VARIANT] = "use-dark-theme";
109 110
        key_names[ConfigurableProperty.TRANSPARENT_BACKGROUND_TYPE] = "transparent-background-type";
        key_names[ConfigurableProperty.TRANSPARENT_BACKGROUND_COLOR] = "transparent-background-color";
Lucas Beeler's avatar
Lucas Beeler committed
111 112 113
        key_names[ConfigurableProperty.COMMIT_METADATA_TO_MASTERS] = "commit-metadata";
        key_names[ConfigurableProperty.DESKTOP_BACKGROUND_FILE] = "picture-uri";
        key_names[ConfigurableProperty.DESKTOP_BACKGROUND_MODE] = "picture-options";
114 115
        key_names[ConfigurableProperty.SCREENSAVER_FILE] = "picture-uri";
        key_names[ConfigurableProperty.SCREENSAVER_MODE] = "picture-options";
Lucas Beeler's avatar
Lucas Beeler committed
116 117 118 119 120 121 122
        key_names[ConfigurableProperty.DIRECTORY_PATTERN] = "directory-pattern";
        key_names[ConfigurableProperty.DIRECTORY_PATTERN_CUSTOM] = "directory-pattern-custom";
        key_names[ConfigurableProperty.DIRECT_WINDOW_HEIGHT] = "direct-height";
        key_names[ConfigurableProperty.DIRECT_WINDOW_MAXIMIZE] = "direct-maximize";
        key_names[ConfigurableProperty.DIRECT_WINDOW_WIDTH] = "direct-width";
        key_names[ConfigurableProperty.DISPLAY_BASIC_PROPERTIES] = "display-basic-properties";
        key_names[ConfigurableProperty.DISPLAY_EXTENDED_PROPERTIES] = "display-extended-properties";
123
        key_names[ConfigurableProperty.DISPLAY_SIDEBAR] = "display-sidebar";
124
        key_names[ConfigurableProperty.DISPLAY_TOOLBAR] = "display-toolbar";
125
        key_names[ConfigurableProperty.DISPLAY_SEARCH_BAR] = "display-search-bar";
Lucas Beeler's avatar
Lucas Beeler committed
126 127 128
        key_names[ConfigurableProperty.DISPLAY_PHOTO_RATINGS] = "display-photo-ratings";
        key_names[ConfigurableProperty.DISPLAY_PHOTO_TAGS] = "display-photo-tags";
        key_names[ConfigurableProperty.DISPLAY_PHOTO_TITLES] = "display-photo-titles";
129
        key_names[ConfigurableProperty.DISPLAY_PHOTO_COMMENTS] = "display-photo-comments";
130
        key_names[ConfigurableProperty.DISPLAY_EVENT_COMMENTS] = "display-event-comments";
Lucas Beeler's avatar
Lucas Beeler committed
131 132 133
        key_names[ConfigurableProperty.EVENT_PHOTOS_SORT_ASCENDING] = "event-photos-sort-ascending";
        key_names[ConfigurableProperty.EVENT_PHOTOS_SORT_BY] = "event-photos-sort-by";
        key_names[ConfigurableProperty.EVENTS_SORT_ASCENDING] = "events-sort-ascending";
134 135 136 137 138 139
        key_names[ConfigurableProperty.EXPORT_CONSTRAINT] = "constraint";
        key_names[ConfigurableProperty.EXPORT_EXPORT_FORMAT_MODE] =  "export-format-mode";
        key_names[ConfigurableProperty.EXPORT_EXPORT_METADATA] =  "export-metadata";
        key_names[ConfigurableProperty.EXPORT_PHOTO_FILE_FORMAT] =  "photo-file-format";
        key_names[ConfigurableProperty.EXPORT_QUALITY] =  "quality";
        key_names[ConfigurableProperty.EXPORT_SCALE] =  "scale";
Lucas Beeler's avatar
Lucas Beeler committed
140 141
        key_names[ConfigurableProperty.EXTERNAL_PHOTO_APP] = "external-photo-editor";
        key_names[ConfigurableProperty.EXTERNAL_RAW_APP] = "external-raw-editor";
142
        key_names[ConfigurableProperty.HIDE_PHOTOS_ALREADY_IMPORTED] = "hide-photos-already-imported";
Lucas Beeler's avatar
Lucas Beeler committed
143 144
        key_names[ConfigurableProperty.IMPORT_DIR] = "import-dir";
        key_names[ConfigurableProperty.KEEP_RELATIVITY] = "keep-relativity";
145 146 147
        key_names[ConfigurableProperty.LAST_CROP_HEIGHT] = "last-crop-height";
        key_names[ConfigurableProperty.LAST_CROP_MENU_CHOICE] = "last-crop-menu-choice";
        key_names[ConfigurableProperty.LAST_CROP_WIDTH] = "last-crop-width";
Lucas Beeler's avatar
Lucas Beeler committed
148
        key_names[ConfigurableProperty.LAST_USED_SERVICE] = "last-used-service";
149
        key_names[ConfigurableProperty.LAST_USED_DATAIMPORTS_SERVICE] = "last-used-dataimports-service";
Lucas Beeler's avatar
Lucas Beeler committed
150 151 152 153 154 155 156
        key_names[ConfigurableProperty.LIBRARY_PHOTOS_SORT_ASCENDING] = "library-photos-sort-ascending";
        key_names[ConfigurableProperty.LIBRARY_PHOTOS_SORT_BY] = "library-photos-sort-by";
        key_names[ConfigurableProperty.LIBRARY_WINDOW_HEIGHT] = "library-height";
        key_names[ConfigurableProperty.LIBRARY_WINDOW_MAXIMIZE] = "library-maximize";
        key_names[ConfigurableProperty.LIBRARY_WINDOW_WIDTH] = "library-width";
        key_names[ConfigurableProperty.MODIFY_ORIGINALS] = "modify-originals";
        key_names[ConfigurableProperty.PHOTO_THUMBNAIL_SCALE] = "photo-thumbnail-scale";
157
        key_names[ConfigurableProperty.PIN_TOOLBAR_STATE] = "pin-toolbar-state";
Lucas Beeler's avatar
Lucas Beeler committed
158 159 160 161 162 163 164 165 166 167
        key_names[ConfigurableProperty.PRINTING_CONTENT_HEIGHT] = "content-height";
        key_names[ConfigurableProperty.PRINTING_CONTENT_LAYOUT] = "content-layout";
        key_names[ConfigurableProperty.PRINTING_CONTENT_PPI] = "content-ppi";
        key_names[ConfigurableProperty.PRINTING_CONTENT_UNITS] = "content-units";
        key_names[ConfigurableProperty.PRINTING_CONTENT_WIDTH] = "content-width";
        key_names[ConfigurableProperty.PRINTING_IMAGES_PER_PAGE] = "images-per-page";
        key_names[ConfigurableProperty.PRINTING_MATCH_ASPECT_RATIO] = "match-aspect-ratio";
        key_names[ConfigurableProperty.PRINTING_PRINT_TITLES] = "print-titles";
        key_names[ConfigurableProperty.PRINTING_SIZE_SELECTION] = "size-selection";
        key_names[ConfigurableProperty.PRINTING_TITLES_FONT] = "titles-font";
168
        key_names[ConfigurableProperty.RAW_DEVELOPER_DEFAULT] = "raw-developer-default";
Lucas Beeler's avatar
Lucas Beeler committed
169 170 171 172 173
        key_names[ConfigurableProperty.SHOW_WELCOME_DIALOG] = "show-welcome-dialog";
        key_names[ConfigurableProperty.SIDEBAR_POSITION] = "sidebar-position";
        key_names[ConfigurableProperty.SLIDESHOW_DELAY] = "delay";
        key_names[ConfigurableProperty.SLIDESHOW_TRANSITION_DELAY] = "transition-delay";
        key_names[ConfigurableProperty.SLIDESHOW_TRANSITION_EFFECT_ID] = "transition-effect-id";
174
        key_names[ConfigurableProperty.SLIDESHOW_SHOW_TITLE] = "show-title";
Lucas Beeler's avatar
Lucas Beeler committed
175 176 177 178
        key_names[ConfigurableProperty.USE_24_HOUR_TIME] = "use-24-hour-time";
        key_names[ConfigurableProperty.USE_LOWERCASE_FILENAMES] = "use-lowercase-filenames";
        key_names[ConfigurableProperty.VIDEO_INTERPRETER_STATE_COOKIE] = "interpreter-state-cookie";
    }
179

Jens Georg's avatar
Jens Georg committed
180 181 182 183 184 185 186 187
    private Settings get_settings(string schema) {
        if (!this.settings_cache.has_key(schema)) {
            this.settings_cache[schema] = new Settings(schema);
        }

        return this.settings_cache[schema];
    }

Lucas Beeler's avatar
Lucas Beeler committed
188
    private void check_key_valid(string schema, string key) throws ConfigurationError {
189 190 191
        var schema_source = SettingsSchemaSource.get_default ();
        var settings_scheme = schema_source.lookup (schema, true);
        if (settings_scheme == null) {
Lucas Beeler's avatar
Lucas Beeler committed
192
            throw new ConfigurationError.ENGINE_ERROR("schema '%s' is not installed".printf(schema));
193
        }
Lucas Beeler's avatar
Lucas Beeler committed
194

195
        if (!settings_scheme.has_key (key))
Lucas Beeler's avatar
Lucas Beeler committed
196 197 198 199 200 201 202
            throw new ConfigurationError.ENGINE_ERROR("schema '%s' does not define key '%s'".printf(
                schema, key));
    }

    private bool get_gs_bool(string schema, string key) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
203
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
204 205 206 207 208 209 210

        return schema_object.get_boolean(key);
    }
    
    private void set_gs_bool(string schema, string key, bool value) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
211
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
212 213 214 215

        schema_object.set_boolean(key, value);
    }

216 217 218
    private void set_gs_enum (string schema, string key, int value) throws ConfigurationError {
        check_key_valid (schema, key);

Jens Georg's avatar
Jens Georg committed
219
        var schema_object = get_settings (schema);
220 221 222 223 224 225
        schema_object.set_enum (key, value);
    }

    private int get_gs_enum (string schema, string key) throws ConfigurationError {
        check_key_valid (schema, key);

Jens Georg's avatar
Jens Georg committed
226
        var schema_object = get_settings (schema);
227 228 229
        return schema_object.get_enum (key);
    }

Lucas Beeler's avatar
Lucas Beeler committed
230 231 232
    private int get_gs_int(string schema, string key) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
233
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
234 235 236 237 238 239 240

        return schema_object.get_int(key);
    }
    
    private void set_gs_int(string schema, string key, int value) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
241
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
242 243 244 245 246 247 248

        schema_object.set_int(key, value);
    }

    private double get_gs_double(string schema, string key) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
249
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
250 251 252 253 254 255 256

        return schema_object.get_double(key);
    }
    
    private void set_gs_double(string schema, string key, double value) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
257
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
258 259 260 261 262 263 264

        schema_object.set_double(key, value);
    }

    private string get_gs_string(string schema, string key) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
265
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
266 267 268 269 270 271 272

        return schema_object.get_string(key);
    }
    
    private void set_gs_string(string schema, string key, string value) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
273
        Settings schema_object = get_settings(schema);
Lucas Beeler's avatar
Lucas Beeler committed
274 275 276

        schema_object.set_string(key, value);
    }
277 278 279 280
    
    private void reset_gs_to_default(string schema, string key) throws ConfigurationError {
        check_key_valid(schema, key);

Jens Georg's avatar
Jens Georg committed
281
        Settings schema_object = get_settings(schema);
282 283 284

        schema_object.reset(key);
    }
Lucas Beeler's avatar
Lucas Beeler committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

    private static string? clean_plugin_id(string id) {
        string cleaned = id.replace("/", "-");
        cleaned = cleaned.strip();
        
        return !is_string_empty(cleaned) ? cleaned : null;
    }
    
    private static string get_plugin_enable_disable_name(string id) {
        string? cleaned_id = clean_plugin_id(id);
        if (cleaned_id == null)
            cleaned_id = "default";

        cleaned_id = cleaned_id.replace("org.yorba.shotwell.", "");
        cleaned_id = cleaned_id.replace(".", "-");
        
        return cleaned_id;
    }
    
    private static string make_plugin_schema_name(string domain, string id) {
        string? cleaned_id = clean_plugin_id(id);
        if (cleaned_id == null)
            cleaned_id = "default";
308
        cleaned_id = cleaned_id.replace(".", "-");
Lucas Beeler's avatar
Lucas Beeler committed
309 310 311 312 313 314 315 316 317 318 319 320
        
        return "org.yorba.shotwell.%s.%s".printf(domain, cleaned_id);
    }
    
    private static string make_gsettings_key(string gconf_key) {
        return gconf_key.replace("_", "-");
    }

    public string get_name() {
        return "GSettings";
    }

321 322 323 324 325 326 327 328 329
    public int get_enum_property (ConfigurableProperty p) throws ConfigurationError{
        return get_gs_enum (schema_names[p], key_names[p]);
    }

    public void set_enum_property (ConfigurableProperty p, int val) throws ConfigurationError {
        set_gs_enum (schema_names[p], key_names[p], val);
        property_changed (p);
    }

Lucas Beeler's avatar
Lucas Beeler committed
330 331 332 333 334 335 336 337 338
    public int get_int_property(ConfigurableProperty p) throws ConfigurationError {
        return get_gs_int(schema_names[p], key_names[p]);
    }

    public void set_int_property(ConfigurableProperty p, int val) throws ConfigurationError {
        set_gs_int(schema_names[p], key_names[p], val);
        property_changed(p);
    }
    
Lucas Stephen Beeler's avatar
Lucas Stephen Beeler committed
339
    public string get_string_property(ConfigurableProperty p) throws ConfigurationError {
340 341 342 343 344 345 346 347 348 349
        string gs_result = get_gs_string(schema_names[p], key_names[p]);
        
        // if we're getting the desktop background file, convert the file uri we get back from
        // GSettings into a file path
        string result = gs_result;
        if (p == ConfigurableProperty.DESKTOP_BACKGROUND_FILE) {
            result = gs_result.substring(7);
        }
        
        return result;
Lucas Beeler's avatar
Lucas Beeler committed
350 351
    }
    
Lucas Stephen Beeler's avatar
Lucas Stephen Beeler committed
352
    public void set_string_property(ConfigurableProperty p, string val) throws ConfigurationError {
353
        // if we're setting the desktop background/screensaver file, convert the filename into a file URI
354
        string converted_val = val;
355 356
        if (p == ConfigurableProperty.DESKTOP_BACKGROUND_FILE
            || p == ConfigurableProperty.SCREENSAVER_FILE) {
357 358 359 360
            converted_val = "file://" + val;
        }

        set_gs_string(schema_names[p], key_names[p], converted_val);
Lucas Beeler's avatar
Lucas Beeler committed
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
        property_changed(p);
    }
    
    public bool get_bool_property(ConfigurableProperty p) throws ConfigurationError {
        return get_gs_bool(schema_names[p], key_names[p]);
    }
    
    public void set_bool_property(ConfigurableProperty p, bool val) throws ConfigurationError {
        set_gs_bool(schema_names[p], key_names[p], val);
        property_changed(p);
    }
    
    public double get_double_property(ConfigurableProperty p) throws ConfigurationError {
        return get_gs_double(schema_names[p], key_names[p]);
    }
    
    public void set_double_property(ConfigurableProperty p, double val) throws ConfigurationError {
        set_gs_double(schema_names[p], key_names[p], val);
        property_changed(p);
    }

    public bool get_plugin_bool(string domain, string id, string key, bool def) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            return get_gs_bool(schema_name, make_gsettings_key(key));
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
            return def;
        }
    }
    
    public void set_plugin_bool(string domain, string id, string key, bool val) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            set_gs_bool(schema_name, make_gsettings_key(key), val);
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
    }

    public double get_plugin_double(string domain, string id, string key, double def) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            return get_gs_double(schema_name, make_gsettings_key(key));
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
            return def;
        }
    }

    public void set_plugin_double(string domain, string id, string key, double val) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            set_gs_double(schema_name, make_gsettings_key(key), val);
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
    }

    public int get_plugin_int(string domain, string id, string key, int def) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            return get_gs_int(schema_name, make_gsettings_key(key));
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
            return def;
        }
    }
    
    public void set_plugin_int(string domain, string id, string key, int val) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            set_gs_int(schema_name, make_gsettings_key(key), val);
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
    }
    
    public string? get_plugin_string(string domain, string id, string key, string? def) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            return get_gs_string(schema_name, make_gsettings_key(key));
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
            return def;
        }
    }
    
    public void set_plugin_string(string domain, string id, string key, string? val) {
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            set_gs_string(schema_name, make_gsettings_key(key), val);
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
    }

    public void unset_plugin_key(string domain, string id, string key) {
467 468 469 470 471 472 473
        string schema_name = make_plugin_schema_name(domain, id);
        
        try {
            reset_gs_to_default(schema_name, make_gsettings_key(key));
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
Lucas Beeler's avatar
Lucas Beeler committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    }
    
    public FuzzyPropertyState is_plugin_enabled(string id) {
        string enable_disable_name = get_plugin_enable_disable_name(id);
        
        try {
            return (get_gs_bool(PLUGINS_ENABLE_DISABLE_SCHEMA_NAME, enable_disable_name)) ?
                FuzzyPropertyState.ENABLED : FuzzyPropertyState.DISABLED;
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
            return FuzzyPropertyState.UNKNOWN;
        }
    }

    public void set_plugin_enabled(string id, bool enabled) {
        string enable_disable_name = get_plugin_enable_disable_name(id);
        
        try {
            set_gs_bool(PLUGINS_ENABLE_DISABLE_SCHEMA_NAME, enable_disable_name, enabled);
        } catch (ConfigurationError err) {
            critical("GSettingsConfigurationEngine: error: %s", err.message);
        }
    }
497
    
498 499 500 501 502
    /*! @brief Migrates settings data over from old-style /apps/ paths to /org/yorba/ ones.
     *  Should only be called ONCE, during DB upgrading; otherwise, stale data may be copied
     *  over newer data by accident.
     */
    public static void run_gsettings_migrator() {
503
        string cmd_line = "sh " + AppDirs.get_settings_migrator_bin().get_path();
504 505 506 507 508 509 510 511

        try {
            Process.spawn_command_line_sync(cmd_line);
        } catch (Error err) {
            message("Error running shotwell-settings-migrator: %s", err.message);
        }
    }

Lucas Beeler's avatar
Lucas Beeler committed
512
}