1
/* This file is part of GPump, a Pump.io client.
3
* GPump (THE SOFTWARE) is made available under the terms and conditions of the
4
* GNU Lesser General Public Licence 3.0. A copy of the licence can be read
5
* in the file lgpl-3.0.txt in the root of this project.
9
const Gio = imports.gi.Gio;
10
const GLib = imports.gi.GLib;
11
const Lang = imports.lang;
12
const System = imports.system;
14
let _default_settings_object = null;
20
* Please use this function when getting the settings data object. It is not
21
* good to have multiple instances of the settings data object, it may
22
* cause problems down the line.
24
function get_settings () {
25
if (!_default_settings_object) {
26
_default_settings_object = new SettingsData ();
28
return _default_settings_object;
32
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
34
const SettingsData = new Lang.Class ({
37
/* Member definitions */
38
_settings_file_path: String,
40
_settings_file: Gio.File,
43
/* First we construct the path for where to store the settings file. */
44
this._settings_file_path = "";
45
this._settings_file_path += GLib.get_user_config_dir ();
46
this._settings_file_path += "/gpump/gpump.json";
47
print ("Config file is: " + this._settings_file_path);
49
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
51
/* Then we check that the file exists. If the file doen not ekist we
52
* construct some sane default values.
54
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
55
/* -1 is undefined, and will be set at construction of the window */
66
only_show_avatar: false
73
print (JSON.stringify (this._settings, null, 2).toString () );
75
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
77
file_stream.write_all (JSON.stringify (
78
this._settings, null, 2).toString (), null);
80
file_stream.close (null);
83
/* The file exists, we load the settings data into memory */
84
let file_stream = this._settings_file.read (null);
86
/* See: http://stackoverflow.com/a/21146281
88
let file_info = this._settings_file.query_info("standard::size",
89
Gio.FileQueryInfoFlags.NONE, null);
90
let size = file_info.get_size ();
92
let buffer = file_stream.read_bytes (size, null).get_data ();
94
this._settings = JSON.parse (buffer);
97
print (JSON.stringify (this._settings, null, 2).toString () );
103
* Sets a value in the setting object.
105
* return: false on fail
106
* return: true when everything is OK.
108
set_setting: function (setting, value) {
109
if (typeof setting != "string") {
110
print ("ERROR: The \"setting\" parameter must be a string.");
115
if (typeof value == "number") {
116
this._settings.ui.x = value;
118
print ("The setting \"ui.x\" must be a number.");
122
if (typeof value == "number") {
123
this._settings.ui.y = value;
125
print ("The setting \"ui.y\" must be a number.");
129
if (typeof value == "number") {
130
this._settings.ui.h = value;
132
print ("The setting \"ui.h\" must be a number.");
136
if (typeof value == "number") {
137
this._settings.ui.w = value;
139
print ("The setting \"ui.w\" must be a number.");
142
case "main.use_dark":
143
if (typeof value == "boolean") {
144
this._settings.main.use_dark = value;
146
print ("The setting \"main.use_dark\" must be a boolean.");
149
case "main.first_run":
150
if (typeof value == "boolean") {
151
this._settings.main.first_run = value;
153
print ("The setting \"main.first_run\" must be a boolean.");
156
case "main.privacy.show_full_name":
157
if (typeof value == "boolean") {
158
this._settings.main.privacy.show_full_name = value;
161
"The setting \"main.privacy.show_full_name\" must be a boolean.");
164
case "main.privacy.only_show_avatar":
165
if (typeof value == "boolean") {
166
this._settings.main.privacy.only_show_avatar = value;
169
"The setting \"main.privacy.only_show_avatar\" must be a boolean.");
174
print ("ERROR: The setting \"" + setting + "\" does not exist.");
183
* Gets a value from the settings object.
185
* returns a complex object with two field: ok and data.
187
* If ok is false something went wrong and the data field will be undefined.
189
* If ok is true everything is ok and the data field will be set to the value.
191
get_setting: function (setting) {
197
if (typeof setting != "string") {
198
print ("ERROR: The \"setting\" parameter must be a string.");
205
ret_data.data = this._settings.ui.x;
208
ret_data.data = this._settings.ui.y;
211
ret_data.data = this._settings.ui.h;
214
ret_data.data = this._settings.ui.w;
216
case "main.use_dark":
217
ret_data.data = this._settings.main.use_dark;
219
case "main.first_run":
220
ret_data.data = this._settings.main.first_run;
222
case "main.privacy.show_full_name":
223
ret_data.data = this._settings.main.privacy.show_full_name;
225
case "main.privacy.only_show_avatar":
226
ret_data.data = this._settings.main.privacy.only_show_avatar;
230
print ("ERROR: The setting \"" + setting + "\" does not exist.");
238
* Commits changes to the settings object to file.
240
commit_to_file: function () {
241
print (JSON.stringify (this._settings, null, 2).toString () );
243
let file_stream = this._settings_file.replace (null,
245
Gio.FileCreateFlags.PRIVATE,
247
file_stream.write_all (JSON.stringify (
248
this._settings, null, 2).toString (), null);
250
file_stream.close (null);