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