First cut at supporting 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)
104 {
105     KeyFile key_file = get_keyfile();
106
107     key_file.set_boolean(group_name, key_name, value);
108     save_keyfile(key_file);
109 }
110
111 internal bool get_bool_setting(string group_name, string key_name, bool default=false)
112 {
113     KeyFile key_file = get_keyfile();
114
115     if (key_file == null)
116         return default;
117
118     try {
119         if (!key_file.has_key(group_name, key_name))
120         {
121             logger().info(@"get_bool_setting : key file doesn't contain key '$key_name' in group '$group_name'");
122             return default;
123         }
124     }
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);
127     }
128
129     try {
130         // throws KeyFileError if key is not found
131         return key_file.get_boolean(group_name, key_name);
132     }
133     catch (KeyFileError e) {
134         logger().info("get_bool_setting got KeyFileError (may be ignorable) : " + e.message);
135     }
136     return default;
137 }
138
139
140 internal void set_string_setting(string group_name, string key_name, string value)
141 {
142     KeyFile key_file = get_keyfile();
143
144     key_file.set_string(group_name, key_name, value);
145     save_keyfile(key_file);
146 }
147
148 internal string get_string_setting(string group_name, string key_name, string default="")
149 {
150     KeyFile key_file = get_keyfile();
151
152     if (key_file == null)
153         return default;
154
155     try {
156         if (!key_file.has_key(group_name, key_name))
157         {
158             logger().info(@"get_string_setting : key file doesn't contain key '$key_name' in group '$group_name'");
159             return default;
160         }
161     }
162     catch(KeyFileError e) {
163         logger().info(@"get_string_setting : KeyFileError checking if key '$key_name' exists in group '$group_name' (maybe ignorable?) : " + e.message);
164     }
165
166     try {
167         // throws KeyFileError if key is not found
168         return key_file.get_string(group_name, key_name);
169     }
170     catch (KeyFileError e) {
171         logger().info("get_string_setting got KeyFileError (may be ignorable) : " + e.message);
172     }
173     return default;
174 }