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;
13
let _default_settings_object = null;
16
* Please use this function when getting the settings data object. It is not
19
function get_settings () {
20
if (!_default_settings_object) {
21
_default_settings_object = new SettingsData ();
23
return _default_settings_object;
26
* NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
28
const SettingsData = new Lang.Class ({
31
/* Member definitions */
32
_settings_file_path: String,
34
_settings_file: Gio.File,
37
/* First we construct the path for where to store the settings file. */
38
this._settings_file_path = "";
39
this._settings_file_path += GLib.get_user_config_dir ();
40
this._settings_file_path += "/gpump/gpump.json";
41
print ("Config file is: " + this._settings_file_path);
43
this._settings_file = Gio.File.new_for_path (this._settings_file_path);
45
/* Then we check that the file exists. If the file doen not ekist we
46
* construct some sane default values.
48
if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
49
/* -1 is undefined, and will be set at construction of the window */
60
only_show_avatar: false
67
print (JSON.stringify (this._settings, null, 2).toString () );
69
let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
71
file_stream.write_all (JSON.stringify (
72
this._settings, null, 2).toString (), null);
74
file_stream.close (null);
77
/* The file exists, we load the settings data into memory */
78
let file_stream = this._settings_file.read (null);
80
/* See: http://stackoverflow.com/a/21146281
82
let file_info = this._settings_file.query_info("standard::size",
83
Gio.FileQueryInfoFlags.NONE, null);
84
let size = file_info.get_size ();
86
let buffer = file_stream.read_bytes (size, null).get_data ();
88
this._settings = JSON.parse (buffer);
91
print (JSON.stringify (this._settings, null, 2).toString () );
97
* Sets a value in the setting object.
99
set_setting = function (setting: String, value) {
104
* Gets a value from the settings object.
106
get_setting = function (setting: String) {
111
* Commits changes to the settings object to file.
113
commit_to_file = function () {