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.
8
const Gio = imports.gi.Gio;
9
const GLib = imports.gi.GLib;
11
const Lang = imports.lang;
12
const System = imports.system;
14
let _default_settings_object = null;
17
* Please use this function when getting the settings data object. It is not
18
* good to have multiple instances of the settings data object, it may
19
* cause problems down the line.
21
function get_settings () {
22
if (!_default_settings_object) {
23
_default_settings_object = new SettingsData ();
25
return _default_settings_object;
29
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
31
const SettingsData = new Lang.Class ({
34
/* Member definitions */
35
_settings_file_path: String,
37
_settings_file: Gio.File,
40
/* First we construct the path for where to store the settings file. */
41
this._settings_file_path = "";
42
this._settings_file_path += GLib.get_user_config_dir ();
43
this._settings_file_path += "/gpump/gpump.json";
44
print ("Config file is: " + this._settings_file_path);
46
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
48
/* Then we check that the file exists. If the file doen not ekist we
49
* construct some sane default values.
51
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
52
/* -1 is undefined, and will be set at construction of the window */
63
only_show_avatar: false
70
print (JSON.stringify (this._settings, null, 2).toString () );
72
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
74
file_stream.write_all (JSON.stringify (
75
this._settings, null, 2).toString (), null);
77
file_stream.close (null);
80
/* The file exists, we load the settings data into memory */
81
let file_stream = this._settings_file.read (null);
83
/* See: http://stackoverflow.com/a/21146281
85
let file_info = this._settings_file.query_info("standard::size",
86
Gio.FileQueryInfoFlags.NONE, null);
87
let size = file_info.get_size ();
89
let buffer = file_stream.read_bytes (size, null).get_data ();
91
this._settings = JSON.parse (buffer);
94
print (JSON.stringify (this._settings, null, 2).toString () );
100
* Sets a value in the setting object.
102
* return: false on fail
103
* return: true when everything is OK.
105
set_setting: function (setting, value) {
106
if (typeof setting != "string") {
107
print ("ERROR: The \"setting\" parameter must be a string.");
112
if (typeof value == "number") {
113
this._settings.ui.x = value;
115
print ("The setting \"ui.x\" must be a number.");
119
if (typeof value == "number") {
120
this._settings.ui.y = value;
122
print ("The setting \"ui.y\" must be a number.");
126
if (typeof value == "number") {
127
this._settings.ui.h = value;
129
print ("The setting \"ui.h\" must be a number.");
133
if (typeof value == "number") {
134
this._settings.ui.w = value;
136
print ("The setting \"ui.w\" must be a number.");
139
case "main.use_dark":
140
if (typeof value == "boolean") {
141
this._settings.main.use_dark = value;
143
print ("The setting \"main.use_dark\" must be a boolean.");
146
case "main.first_run":
147
if (typeof value == "boolean") {
148
this._settings.main.first_run = value;
150
print ("The setting \"main.first_run\" must be a boolean.");
153
case "main.privacy.show_full_name":
154
if (typeof value == "boolean") {
155
this._settings.main.privacy.show_full_name = value;
158
"The setting \"main.privacy.show_full_name\" must be a boolean.");
161
case "main.privacy.only_show_avatar":
162
if (typeof value == "boolean") {
163
this._settings.main.privacy.only_show_avatar = value;
166
"The setting \"main.privacy.only_show_avatar\" must be a boolean.");
171
print ("ERROR: The setting \"" + setting + "\" does not exist.");
180
* Gets a value from the settings object.
182
* returns a complex object with two field: ok and data.
184
* If ok is false something went wrong and the data field will be undefined.
186
* If ok is true everything is ok and the data field will be set to the value.
188
get_setting: function (setting) {
194
if (typeof setting != "string") {
195
print ("ERROR: The \"setting\" parameter must be a string.");
202
ret_data.data = this._settings.ui.x;
205
ret_data.data = this._settings.ui.y;
208
ret_data.data = this._settings.ui.h;
211
ret_data.data = this._settings.ui.w;
213
case "main.use_dark":
214
ret_data.data = this._settings.main.use_dark;
216
case "main.first_run":
217
ret_data.data = this._settings.main.first_run;
219
case "main.privacy.show_full_name":
220
ret_data.data = this._settings.main.privacy.show_full_name;
222
case "main.privacy.only_show_avatar":
223
ret_data.data = this._settings.main.privacy.only_show_avatar;
227
print ("ERROR: The setting \"" + setting + "\" does not exist.");
235
* Commits changes to the settings object to file.
237
commit_to_file: function () {
238
print (JSON.stringify (this._settings, null, 2).toString () );
240
let file_stream = this._settings_file.replace (null,
242
Gio.FileCreateFlags.PRIVATE,
244
file_stream.write_all (JSON.stringify (
245
this._settings, null, 2).toString (), null);
247
file_stream.close (null);