2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 private MoonshotLogger logger()
38 return get_logger("MoonshotSettings");
41 static const string KEY_FILE_NAME="moonshot-ui.config";
43 private KeyFile get_keyfile()
45 KeyFile key_file = new KeyFile();
46 string config_dir = Environment.get_user_config_dir();
47 logger().trace("get_keyfile: config_dir=" + config_dir);
49 File dir = File.new_for_path(config_dir);
50 string path = dir.get_child(KEY_FILE_NAME).get_path();
53 if (key_file.load_from_file(path, KeyFileFlags.NONE))
54 logger().trace("get_keyfile: load_from_file returned successfully");
56 logger().trace("get_keyfile: load_from_file returned false");
59 logger().trace("get_keyfile: FileError: " + e.message);
61 catch (KeyFileError e) {
62 logger().trace("get_keyfile: KeyFileError: " + e.message);
69 private void save_keyfile(KeyFile key_file)
71 string config_dir = Environment.get_user_config_dir();
74 // Make the directory if it doesn't already exist; ignore errors.
76 File dir = File.new_for_path(config_dir);
77 dest = dir.get_child(KEY_FILE_NAME);
78 dir.make_directory_with_parents();
80 logger().trace("save_keyfile: make_directory_with_parents threw error (this is usually ignorable) : " + e.message);
83 // It would be nice to use key_file.save_to_file, but the binding doesn't exist
84 // in earlier versions of valac
85 // key_file.save_to_file(path.get_path());
87 string data = key_file.to_data();
89 logger().trace("save_keyfile: saving to file path '%s'".printf(dest.get_path()));
90 // FileOutputStream s = dest.create(FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE);
91 // var ds = new DataOutputStream(s);
92 // ds.put_string(data);
94 dest.replace_contents(data.data, null, false, FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE, out new_etag);
97 logger().error("save_keyfile: error when writing to file: " + e.message);
100 // streams close automatically
103 internal void set_bool_setting(string group_name, string key_name, bool value)
105 KeyFile key_file = get_keyfile();
107 key_file.set_boolean(group_name, key_name, value);
108 save_keyfile(key_file);
111 internal bool get_bool_setting(string group_name, string key_name, bool default=false)
113 KeyFile key_file = get_keyfile();
115 if (key_file == null)
119 if (!key_file.has_key(group_name, key_name))
121 logger().info(@"get_bool_setting : key file doesn't contain key '$key_name' in group '$group_name'");
125 catch(KeyFileError e) {
126 logger().info(@"get_bool_setting : KeyFileError checking if key '$key_name' exists in group '$group_name' (maybe ignorable?) : " + e.message);
130 // throws KeyFileError if key is not found
131 return key_file.get_boolean(group_name, key_name);
133 catch (KeyFileError e) {
134 logger().info("get_bool_setting got KeyFileError (may be ignorable) : " + e.message);