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
20
function get_settings () {
21
if (!_default_settings_object) {
22
_default_settings_object = new SettingsData ();
24
return _default_settings_object;
27
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
29
const SettingsData = new Lang.Class ({
32
/* Member definitions */
33
_settings_file_path: String,
35
_settings_file: Gio.File,
38
/* First we construct the path for where to store the settings file. */
39
this._settings_file_path = "";
40
this._settings_file_path += GLib.get_user_config_dir ();
41
this._settings_file_path += "/gpump/gpump.json";
42
print ("Config file is: " + this._settings_file_path);
44
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
46
/* Then we check that the file exists. If the file doen not ekist we
47
* construct some sane default values.
49
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
50
/* -1 is undefined, and will be set at construction of the window */
61
only_show_avatar: false
68
print (JSON.stringify (this._settings, null, 2).toString () );
70
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
72
file_stream.write_all (JSON.stringify (
73
this._settings, null, 2).toString (), null);
75
file_stream.close (null);
78
/* The file exists, we load the settings data into memory */
79
let file_stream = this._settings_file.read (null);
81
/* See: http://stackoverflow.com/a/21146281
83
let file_info = this._settings_file.query_info("standard::size",
84
Gio.FileQueryInfoFlags.NONE, null);
85
let size = file_info.get_size ();
87
let buffer = file_stream.read_bytes (size, null).get_data ();
89
this._settings = JSON.parse (buffer);
92
print (JSON.stringify (this._settings, null, 2).toString () );
98
* Sets a value in the setting object.
100
* return: false on fail
101
* return: true when everything is OK.
103
set_setting: function (setting, value) {
104
if (typeof setting != "string") {
105
print ("ERROR: The \"setting\" parameter must be a string.");
110
if (typeof value == "number") {
111
this._settings.ui.x = value;
113
print ("The setting \"ui.x\" must be a number.");
117
if (typeof value == "number") {
118
this._settings.ui.y = value;
120
print ("The setting \"ui.y\" must be a number.");
124
if (typeof value == "number") {
125
this._settings.ui.h = value;
127
print ("The setting \"ui.h\" must be a number.");
131
if (typeof value == "number") {
132
this._settings.ui.w = value;
134
print ("The setting \"ui.w\" must be a number.");
137
case "main.use_dark":
138
if (typeof value == "boolean") {
139
this._settings.main.use_dark = value;
141
print ("The setting \"main.use_dark\" must be a boolean.");
144
case "main.first_run":
145
if (typeof value == "boolean") {
146
this._settings.main.first_run = value;
148
print ("The setting \"main.first_run\" must be a boolean.");
151
case "main.privacy.show_full_name":
152
if (typeof value == "boolean") {
153
this._settings.main.privacy.show_full_name = value;
156
"The setting \"main.privacy.show_full_name\" must be a boolean.");
159
case "main.privacy.only_show_avatar":
160
if (typeof value == "boolean") {
161
this._settings.main.privacy.only_show_avatar = value;
164
"The setting \"main.privacy.only_show_avatar\" must be a boolean.");
169
print ("ERROR: The setting \"" + setting + "\" does not exist.");
178
* Gets a value from the settings object.
180
* returns a complex object with two field: ok and data.
182
* If ok is false something went wrong and the data field will be undefined.
184
* If ok is true everything is ok and the data field will be set to the value.
186
get_setting: function (setting) {
192
if (typeof setting != "string") {
193
print ("ERROR: The \"setting\" parameter must be a string.");
200
ret_data.data = this._settings.ui.x;
203
ret_data.data = this._settings.ui.y;
206
ret_data.data = this._settings.ui.h;
209
ret_data.data = this._settings.ui.w;
211
case "main.use_dark":
212
ret_data.data = this._settings.main.use_dark;
214
case "main.first_run":
215
ret_data.data = this._settings.main.first_run;
217
case "main.privacy.show_full_name":
218
ret_data.data = this._settings.main.privacy.show_full_name;
220
case "main.privacy.only_show_avatar":
221
ret_data.data = this._settings.main.privacy.only_show_avatar;
225
print ("ERROR: The setting \"" + setting + "\" does not exist.");
233
* Commits changes to the settings object to file.
235
commit_to_file: function () {