First pass at supporting date/time added for Trust Anchors
[moonshot-ui.git] / src / moonshot-settings.vala
1 /*
2  * Copyright (c) 2011-2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31 */
32
33 using Gtk;
34
35    
36 private MoonshotLogger logger()
37 {
38     return get_logger("MoonshotSettings");
39 }
40
41 static const string KEY_FILE_NAME="moonshot-ui.config";
42
43 private KeyFile get_keyfile()
44 {
45     KeyFile key_file = new KeyFile();
46     string config_dir = Environment.get_user_config_dir();
47     logger().trace("get_keyfile: config_dir=" + config_dir);
48
49     File dir = File.new_for_path(config_dir);
50     string path = dir.get_child(KEY_FILE_NAME).get_path();
51
52     try {
53         if (key_file.load_from_file(path, KeyFileFlags.NONE))
54             logger().trace("get_keyfile: load_from_file returned successfully");
55         else
56             logger().trace("get_keyfile: load_from_file returned false");            
57     }
58     catch (FileError e) {
59         logger().trace("get_keyfile: FileError: " + e.message);
60     }
61     catch (KeyFileError e) {
62         logger().trace("get_keyfile: KeyFileError: " + e.message);
63     }
64
65     return key_file;
66 }
67
68
69 private void save_keyfile(KeyFile key_file)
70 {
71     string config_dir = Environment.get_user_config_dir();
72     File dest = null;
73
74     // Make the directory if it doesn't already exist; ignore errors.
75         try {
76                 File dir = File.new_for_path(config_dir);
77         dest = dir.get_child(KEY_FILE_NAME);
78                 dir.make_directory_with_parents();
79         } catch (Error e) {
80         logger().trace("save_keyfile: make_directory_with_parents threw error (this is usually ignorable) : " + e.message);
81         }
82
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());
86
87     string data = key_file.to_data();
88     try {
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);
93         string new_etag;
94         dest.replace_contents(data.data, null, false, FileCreateFlags.REPLACE_DESTINATION | FileCreateFlags.PRIVATE, out new_etag);
95     }
96     catch(Error e) {
97         logger().error("save_keyfile: error when writing to file: " + e.message);
98     }
99
100     // streams close automatically
101 }
102
103 internal void set_bool_setting(string group_name, string key_name, bool value, KeyFile? key_file=null)
104 {
105     KeyFile tmp_key_file = null;
106     if (key_file == null) {
107         // Use tmp_key_file to hold an owned reference (since key_file is unowned)
108         tmp_key_file = get_keyfile();
109         key_file = tmp_key_file;
110     }
111
112     key_file.set_boolean(group_name, key_name, value);
113
114     if (tmp_key_file != null) {
115         // This is a "one-shot" settings update; save it now.
116         save_keyfile(key_file);
117     }
118 }
119
120 internal bool get_bool_setting(string group_name, string key_name, bool default=false, KeyFile? key_file=null)
121 {
122     KeyFile tmp_key_file = null;
123     if (key_file == null) {
124         // Use tmp_key_file to hold an owned reference (since key_file is unowned)
125         tmp_key_file = get_keyfile();
126         key_file = tmp_key_file;
127     }
128
129     if (key_file == null)
130         return default;
131
132     try {
133         if (!key_file.has_key(group_name, key_name))
134         {
135             logger().info(@"get_bool_setting : key file doesn't contain key '$key_name' in group '$group_name'");
136             return default;
137         }
138     }
139     catch(KeyFileError e) {
140         logger().info(@"get_bool_setting : KeyFileError checking if key '$key_name' exists in group '$group_name' (maybe ignorable?) : " + e.message);
141     }
142
143     try {
144         // throws KeyFileError if key is not found
145         return key_file.get_boolean(group_name, key_name);
146     }
147     catch (KeyFileError e) {
148         logger().info("get_bool_setting got KeyFileError (may be ignorable) : " + e.message);
149     }
150     return default;
151 }
152
153
154 internal void set_string_setting(string group_name, string key_name, string value, KeyFile? key_file=null)
155 {
156     KeyFile tmp_key_file = null;
157     if (key_file == null) {
158         // Use tmp_key_file to hold an owned reference (since key_file is unowned)
159         tmp_key_file = get_keyfile();
160         key_file = tmp_key_file;
161     }
162
163     key_file.set_string(group_name, key_name, value);
164     if (tmp_key_file != null) {
165         // This is a "one-shot" settings update; save it now.
166         save_keyfile(key_file);
167     }
168 }
169
170 internal string get_string_setting(string group_name, string key_name, string default="", KeyFile? key_file=null)
171 {
172     KeyFile tmp_key_file = null;
173     if (key_file == null) {
174         // Use tmp_key_file to hold an owned reference (since key_file is unowned)
175         tmp_key_file = get_keyfile();
176         key_file = tmp_key_file;
177     }
178
179     if (key_file == null)
180         return default;
181
182     try {
183         if (!key_file.has_key(group_name, key_name))
184         {
185             logger().info(@"get_string_setting : key file doesn't contain key '$key_name' in group '$group_name'");
186             return default;
187         }
188     }
189     catch(KeyFileError e) {
190         logger().info(@"get_string_setting : KeyFileError checking if key '$key_name' exists in group '$group_name' (maybe ignorable?) : " + e.message);
191     }
192
193     try {
194         // throws KeyFileError if key is not found
195         return key_file.get_string(group_name, key_name);
196     }
197     catch (KeyFileError e) {
198         logger().info("get_string_setting got KeyFileError (may be ignorable) : " + e.message);
199     }
200     return default;
201 }