5
5
* in the file lgpl-3.0.txt in the root of this project.
8
9
const Gio = imports.gi.Gio;
9
10
const GLib = imports.gi.GLib;
11
const GObject = imports.gi.GObject;
11
12
const Lang = imports.lang;
12
13
const System = imports.system;
14
15
let _default_settings_object = null;
17
21
* Please use this function when getting the settings data object. It is not
18
22
* good to have multiple instances of the settings data object, it may
19
23
* cause problems down the line.
25
* @return The the default @c SettingsData object
21
27
function get_settings () {
22
28
if (!_default_settings_object) {
25
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 */
29
49
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
31
51
const SettingsData = new Lang.Class ({
32
52
Name: 'SettingsData',
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]
34
72
/* Member definitions */
35
73
_settings_file_path: String,
37
75
_settings_file: Gio.File,
80
_init: function (params) {
40
83
/* 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";
84
this._settings_file_path = "" +
85
GLib.get_user_config_dir () +
44
87
print ("Config file is: " + this._settings_file_path);
46
89
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
91
/* Then we check that the file exists. If the file does not exists we
49
92
* construct some sane default values.
51
94
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
63
106
only_show_avatar: false
110
quit_on_close: QUIT_ON_CLOSE.UNKNOWN
70
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);
72
124
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
74
127
file_stream.write_all (JSON.stringify (
75
128
this._settings, null, 2).toString (), null);
100
153
* Sets a value in the setting object.
102
* return: false on fail
103
* return: true when everything is OK.
155
* @return false on fail
156
* @return true when everything is OK.
105
158
set_setting: function (setting, value) {
106
160
if (typeof setting != "string") {
107
161
print ("ERROR: The \"setting\" parameter must be a string.");
148
207
this._settings.main.first_run = value;
150
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.");
153
222
case "main.privacy.show_full_name":
155
224
this._settings.main.privacy.show_full_name = value;
158
"The setting \"main.privacy.show_full_name\" must be a boolean.");
227
"The setting \"main.privacy.show_full_name\" must be a boolean.");
161
231
case "main.privacy.only_show_avatar":
248
/* Because, reasons. */
250
this.emit ("change", setting, value);
180
257
* 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.
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
188
268
get_setting: function (setting) {
194
274
if (typeof setting != "string") {
195
275
print ("ERROR: The \"setting\" parameter must be a string.");
196
276
ret_data.ok = false;
200
280
switch (setting) {
222
302
case "main.privacy.only_show_avatar":
223
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;
226
309
ret_data.ok = false;
227
310
print ("ERROR: The setting \"" + setting + "\" does not exist.");