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 GObject = imports.gi.GObject;
12
const Lang = imports.lang;
13
const System = imports.system;
15
let _default_settings_object = null;
21
* Please use this function when getting the settings data object. It is not
22
* good to have multiple instances of the settings data object, it may
23
* cause problems down the line.
25
* @return The the default @c SettingsData object
27
function get_settings () {
28
if (!_default_settings_object) {
29
_default_settings_object = new SettingsData ();
31
return _default_settings_object;
36
const QUIT_ON_CLOSE = {
38
* Default value, will show the close dialogue on clicking the close button
42
/** Will not exist the app on close of main window */
44
/** Will exit on close of main window */
49
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
51
const SettingsData = new Lang.Class ({
53
Extends: GObject.Object,
56
* When hooking up to this signal check that the name of the change.
58
* The function signature is <code> function my_callback (String: setting,
59
* Value: value) </code> where @c setting is the name of the setting,
60
* and @c value is the value to be that it has changed to.
62
* @warning Always check the @c value type and the @c setting name.
67
param_types: [GObject.TYPE_STRING, GObject.TYPE_INT]
72
/* Member definitions */
73
_settings_file_path: String,
75
_settings_file: Gio.File,
80
_init: function (params) {
83
/* First we construct the path for where to store the settings file. */
84
this._settings_file_path = "" +
85
GLib.get_user_config_dir () +
87
print ("Config file is: " + this._settings_file_path);
89
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
91
/* Then we check that the file exists. If the file does not exists we
92
* construct some sane default values.
94
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
95
/* -1 is undefined, and will be set at construction of the window */
105
show_full_name: true,
106
only_show_avatar: false
110
quit_on_close: QUIT_ON_CLOSE.UNKNOWN
114
print (JSON.stringify (this._settings, null, 2).toString () );
116
// Check if the folder exists, and if it does not exist, create it.
117
let folder_path = "" + GLib.get_user_config_dir () +
119
let folder = Gio.File.new_for_path (folder_path);
120
if (!GLib.file_test (folder_path, GLib.FileTest.EXISTS)) {
121
folder.make_directory (null);
124
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
127
file_stream.write_all (JSON.stringify (
128
this._settings, null, 2).toString (), null);
130
file_stream.close (null);
133
/* The file exists, we load the settings data into memory */
134
let file_stream = this._settings_file.read (null);
136
/* See: http://stackoverflow.com/a/21146281
138
let file_info = this._settings_file.query_info("standard::size",
139
Gio.FileQueryInfoFlags.NONE, null);
140
let size = file_info.get_size ();
142
let buffer = file_stream.read_bytes (size, null).get_data ();
144
this._settings = JSON.parse (buffer);
147
print (JSON.stringify (this._settings, null, 2).toString () );
153
* Sets a value in the setting object.
155
* @return false on fail
156
* @return true when everything is OK.
158
set_setting: function (setting, value) {
160
if (typeof setting != "string") {
161
print ("ERROR: The \"setting\" parameter must be a string.");
166
if (typeof value == "number") {
167
this._settings.ui.x = value;
169
print ("The setting \"ui.x\" must be a number.");
174
if (typeof value == "number") {
175
this._settings.ui.y = value;
177
print ("The setting \"ui.y\" must be a number.");
182
if (typeof value == "number") {
183
this._settings.ui.h = value;
185
print ("The setting \"ui.h\" must be a number.");
190
if (typeof value == "number") {
191
this._settings.ui.w = value;
193
print ("The setting \"ui.w\" must be a number.");
197
case "main.use_dark":
198
if (typeof value == "boolean") {
199
this._settings.main.use_dark = value;
201
print ("The setting \"main.use_dark\" must be a boolean.");
205
case "main.first_run":
206
if (typeof value == "boolean") {
207
this._settings.main.first_run = value;
209
print ("The setting \"main.first_run\" must be a boolean.");
213
case "main.quit_on_close":
214
if (typeof value == "number") {
215
this._settings.main.quit_on_close = value;
218
"The setting \"main.quit_on_close\" must be a number.");
222
case "main.privacy.show_full_name":
223
if (typeof value == "boolean") {
224
this._settings.main.privacy.show_full_name = value;
227
"The setting \"main.privacy.show_full_name\" must be a boolean.");
231
case "main.privacy.only_show_avatar":
232
if (typeof value == "boolean") {
233
this._settings.main.privacy.only_show_avatar = value;
236
"The setting \"main.privacy.only_show_avatar\" must be a boolean.");
242
print ("ERROR: The setting \"" + setting + "\" does not exist.");
248
/* Because, reasons. */
250
this.emit ("change", setting, value);
257
* Gets a value from the settings object.
259
* @param setting the @c String that corrosponds to the setting to get.
261
* @return a <code> complex object </code> with two field: @c ok and @c data.
263
* If @c ok is false something went wrong and the data field will be undefined.
265
* If @c ok is true everything is ok and the data field will be set to the
268
get_setting: function (setting) {
274
if (typeof setting != "string") {
275
print ("ERROR: The \"setting\" parameter must be a string.");
282
ret_data.data = this._settings.ui.x;
285
ret_data.data = this._settings.ui.y;
288
ret_data.data = this._settings.ui.h;
291
ret_data.data = this._settings.ui.w;
293
case "main.use_dark":
294
ret_data.data = this._settings.main.use_dark;
296
case "main.first_run":
297
ret_data.data = this._settings.main.first_run;
299
case "main.privacy.show_full_name":
300
ret_data.data = this._settings.main.privacy.show_full_name;
302
case "main.privacy.only_show_avatar":
303
ret_data.data = this._settings.main.privacy.only_show_avatar;
305
case "main.quit_on_close":
306
ret_data.data = this._settings.main.quit_on_close;
310
print ("ERROR: The setting \"" + setting + "\" does not exist.");
318
* Commits changes to the settings object to file.
320
commit_to_file: function () {
321
print (JSON.stringify (this._settings, null, 2).toString () );
323
let file_stream = this._settings_file.replace (null,
325
Gio.FileCreateFlags.PRIVATE,
327
file_stream.write_all (JSON.stringify (
328
this._settings, null, 2).toString (), null);
330
file_stream.close (null);