1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/* This file is part of GPump, a Pump.io client.
*
* GPump (THE SOFTWARE) is made available under the terms and conditions of the
* GNU Lesser General Public Licence 3.0. A copy of the licence can be read
* in the file lgpl-3.0.txt in the root of this project.
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const System = imports.system;
let _default_settings_object = null;
/** @file
*/
/**
* Please use this function when getting the settings data object. It is not
* good to have multiple instances of the settings data object, it may
* cause problems down the line.
*/
function get_settings () {
if (!_default_settings_object) {
_default_settings_object = new SettingsData ();
}
return _default_settings_object;
}
/**
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
*/
const SettingsData = new Lang.Class ({
Name: 'SettingsData',
/* Member definitions */
_settings_file_path: String,
_settings: Array,
_settings_file: Gio.File,
_init: function () {
/* First we construct the path for where to store the settings file. */
this._settings_file_path = "";
this._settings_file_path += GLib.get_user_config_dir ();
this._settings_file_path += "/gpump/gpump.json";
print ("Config file is: " + this._settings_file_path);
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
/* Then we check that the file exists. If the file doen not ekist we
* construct some sane default values.
*/
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
/* -1 is undefined, and will be set at construction of the window */
this._settings = {
ui: {
x: -1,
y: -1,
h: 500,
w: 500
},
main: {
privacy: {
show_full_name: true,
only_show_avatar: false
},
use_dark: false,
first_run: true
}
};
// DEBUG:
print (JSON.stringify (this._settings, null, 2).toString () );
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
null);
file_stream.write_all (JSON.stringify (
this._settings, null, 2).toString (), null);
file_stream.close (null);
} else {
/* The file exists, we load the settings data into memory */
let file_stream = this._settings_file.read (null);
/* See: http://stackoverflow.com/a/21146281
*/
let file_info = this._settings_file.query_info("standard::size",
Gio.FileQueryInfoFlags.NONE, null);
let size = file_info.get_size ();
let buffer = file_stream.read_bytes (size, null).get_data ();
this._settings = JSON.parse (buffer);
//DEBUG:
print (JSON.stringify (this._settings, null, 2).toString () );
}
},
/**
* Sets a value in the setting object.
*
* return: false on fail
* return: true when everything is OK.
*/
set_setting: function (setting, value) {
if (typeof setting != "string") {
print ("ERROR: The \"setting\" parameter must be a string.");
return false;
}
switch (setting) {
case "ui.x":
if (typeof value == "number") {
this._settings.ui.x = value;
} else {
print ("The setting \"ui.x\" must be a number.");
}
break;
case "ui.y":
if (typeof value == "number") {
this._settings.ui.y = value;
} else {
print ("The setting \"ui.y\" must be a number.");
}
break;
case "ui.h":
if (typeof value == "number") {
this._settings.ui.h = value;
} else {
print ("The setting \"ui.h\" must be a number.");
}
break;
case "ui.w":
if (typeof value == "number") {
this._settings.ui.w = value;
} else {
print ("The setting \"ui.w\" must be a number.");
}
break;
case "main.use_dark":
if (typeof value == "boolean") {
this._settings.main.use_dark = value;
} else {
print ("The setting \"main.use_dark\" must be a boolean.");
}
break;
case "main.first_run":
if (typeof value == "boolean") {
this._settings.main.first_run = value;
} else {
print ("The setting \"main.first_run\" must be a boolean.");
}
break;
case "main.privacy.show_full_name":
if (typeof value == "boolean") {
this._settings.main.privacy.show_full_name = value;
} else {
print (
"The setting \"main.privacy.show_full_name\" must be a boolean.");
}
break;
case "main.privacy.only_show_avatar":
if (typeof value == "boolean") {
this._settings.main.privacy.only_show_avatar = value;
} else {
print (
"The setting \"main.privacy.only_show_avatar\" must be a boolean.");
}
break;
default:
print ("ERROR: The setting \"" + setting + "\" does not exist.");
return false;
break;
}
return true;
},
/**
* Gets a value from the settings object.
*
* returns a complex object with two field: ok and data.
*
* If ok is false something went wrong and the data field will be undefined.
*
* If ok is true everything is ok and the data field will be set to the value.
*/
get_setting: function (setting) {
let ret_data = {
ok: true,
data: undefined
};
if (typeof setting != "string") {
print ("ERROR: The \"setting\" parameter must be a string.");
ret_data.ok = false;
return ret_data;
}
switch (setting) {
case "ui.x":
ret_data.data = this._settings.ui.x;
break;
case "ui.y":
ret_data.data = this._settings.ui.y;
break;
case "ui.h":
ret_data.data = this._settings.ui.h;
break;
case "ui.w":
ret_data.data = this._settings.ui.w;
break;
case "main.use_dark":
ret_data.data = this._settings.main.use_dark;
break;
case "main.first_run":
ret_data.data = this._settings.main.first_run;
break;
case "main.privacy.show_full_name":
ret_data.data = this._settings.main.privacy.show_full_name;
break;
case "main.privacy.only_show_avatar":
ret_data.data = this._settings.main.privacy.only_show_avatar;
break;
default:
ret_data.ok = false;
print ("ERROR: The setting \"" + setting + "\" does not exist.");
break;
}
return ret_data;
},
/**
* Commits changes to the settings object to file.
*/
commit_to_file: function () {
print (JSON.stringify (this._settings, null, 2).toString () );
let file_stream = this._settings_file.replace (null,
false,
Gio.FileCreateFlags.PRIVATE,
null);
file_stream.write_all (JSON.stringify (
this._settings, null, 2).toString (), null);
file_stream.close (null);
}
// End of class.
});
|