/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk

« back to all changes in this revision

Viewing changes to src/settings_data.js

  • Committer: Gustav Hartvigsson
  • Date: 2015-06-05 19:06:14 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150605190614-s410mh65n2jdxtcv
* Cleaded up a lille code
* Fixed a nasty segfault that could occur if the conig folder did not exist.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * in the file lgpl-3.0.txt in the root of this project.
6
6
 */
7
7
 
 
8
 
8
9
const Gio = imports.gi.Gio;
9
10
const GLib = imports.gi.GLib;
10
 
 
 
11
const GObject = imports.gi.GObject;
11
12
const Lang = imports.lang;
12
13
const System = imports.system;
13
14
 
14
15
let _default_settings_object = null;
15
16
 
16
 
/**
 
17
/** @file
 
18
 */
 
19
 
 
20
/** @function
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.
 
24
 *
 
25
 * @return The the default @c SettingsData object
20
26
 */
21
27
function get_settings () {
22
28
  if (!_default_settings_object) {
25
31
  return _default_settings_object;
26
32
}
27
33
 
28
 
/**
 
34
/** @enum
 
35
 */
 
36
const QUIT_ON_CLOSE = {
 
37
  /** 
 
38
   * Default value, will show the close dialogue on clicking the close button
 
39
   * in the main window.
 
40
   */
 
41
  UNKNOWN: -1,
 
42
  /** Will not exist the app on close of main window */
 
43
  NO: 0,
 
44
  /** Will exit on close of main window */
 
45
  YES: 1
 
46
}
 
47
 
 
48
/** @class
29
49
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
30
50
 */
31
51
const SettingsData = new Lang.Class ({
32
52
  Name: 'SettingsData',
 
53
  Extends: GObject.Object,
 
54
  Signals: {
 
55
    /** @signal change
 
56
     * When hooking up to this signal check that the name of the change.
 
57
     * 
 
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.
 
61
     *
 
62
     * @warning Always check the @c value type and the @c setting name.
 
63
     *
 
64
     * @return void
 
65
     */
 
66
    'change': {
 
67
      param_types: [GObject.TYPE_STRING, GObject.TYPE_INT]
 
68
    }
 
69
  },
 
70
  
33
71
  
34
72
  /* Member definitions */
35
73
  _settings_file_path: String,
36
74
  _settings: Array,
37
75
  _settings_file: Gio.File,
38
76
  
39
 
  _init: function () {
 
77
  /**
 
78
   * 
 
79
   */
 
80
  _init: function (params) {
 
81
    this.parent (params);
 
82
    
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 () +
 
86
                              "/gpump/gpump.json";
44
87
    print ("Config file is: " + this._settings_file_path);
45
88
    
46
89
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
47
90
    
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.
50
93
     */
51
94
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
63
106
            only_show_avatar: false
64
107
          },
65
108
          use_dark: false,
66
 
          first_run: true
 
109
          first_run: true,
 
110
          quit_on_close: QUIT_ON_CLOSE.UNKNOWN
67
111
        }
68
112
      };
69
113
      // DEBUG:
70
114
      print (JSON.stringify (this._settings, null, 2).toString () );
71
115
      
 
116
      // Check if the folder exists, and if it does not exist, create it.
 
117
      let folder_path = "" + GLib.get_user_config_dir () +
 
118
                                          "/gpump/"
 
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);
 
122
      }
 
123
      
72
124
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
73
125
                                                    null);
 
126
      
74
127
      file_stream.write_all (JSON.stringify (
75
128
          this._settings, null, 2).toString (), null);
76
129
      
96
149
    }
97
150
  },
98
151
  
99
 
  /**
 
152
  /** @method
100
153
   * Sets a value in the setting object.
101
154
   * 
102
 
   * return: false on fail
103
 
   * return: true when everything is OK.
 
155
   * @return false on fail
 
156
   * @return true when everything is OK.
104
157
   */
105
158
  set_setting: function (setting, value) {
 
159
    let ret_val = true;
106
160
    if (typeof setting != "string") {
107
161
      print ("ERROR: The \"setting\" parameter must be a string.");
108
162
      return false;
113
167
          this._settings.ui.x = value;
114
168
        } else {
115
169
          print ("The setting \"ui.x\" must be a number.");
 
170
          ret_val = false;
116
171
        }
117
172
        break;
118
173
      case "ui.y":
120
175
          this._settings.ui.y = value;
121
176
        } else {
122
177
          print ("The setting \"ui.y\" must be a number.");
 
178
          ret_val = false;
123
179
        }
124
180
        break;
125
181
      case "ui.h":
127
183
          this._settings.ui.h = value;
128
184
        } else {
129
185
          print ("The setting \"ui.h\" must be a number.");
 
186
          ret_val = false;
130
187
        }
131
188
        break;
132
189
      case "ui.w":
134
191
          this._settings.ui.w = value;
135
192
        } else {
136
193
          print ("The setting \"ui.w\" must be a number.");
 
194
          ret_val = false;
137
195
        }
138
196
        break;
139
197
      case "main.use_dark":
141
199
          this._settings.main.use_dark = value;
142
200
        } else {
143
201
          print ("The setting \"main.use_dark\" must be a boolean.");
 
202
          ret_val = false;
144
203
        }
145
204
        break;
146
205
      case "main.first_run":
148
207
          this._settings.main.first_run = value;
149
208
        } else {
150
209
          print ("The setting \"main.first_run\" must be a boolean.");
 
210
          ret_val = false;
 
211
        }
 
212
        break;
 
213
      case "main.quit_on_close":
 
214
        if (typeof value == "number") {
 
215
          this._settings.main.quit_on_close = value;
 
216
        } else {
 
217
          print (
 
218
            "The setting \"main.quit_on_close\" must be a number.");
 
219
          ret_val = false;
151
220
        }
152
221
        break;
153
222
      case "main.privacy.show_full_name":
155
224
          this._settings.main.privacy.show_full_name = value;
156
225
        } else {
157
226
          print (
158
 
          "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
227
            "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
228
          ret_val = false;
159
229
        }
160
230
        break;
161
231
      case "main.privacy.only_show_avatar":
163
233
          this._settings.main.privacy.only_show_avatar = value;
164
234
        } else {
165
235
          print (
166
 
          "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
236
            "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
237
          ret_val = false;
167
238
        }
168
239
        break;
169
240
      default:
173
244
        break;
174
245
    }
175
246
    
176
 
    return true;
 
247
    
 
248
    /* Because, reasons. */
 
249
    if (ret_val) {
 
250
      this.emit ("change", setting, value);
 
251
    }
 
252
    
 
253
    return ret_val;
177
254
  },
178
255
  
179
 
  /**
 
256
  /** @method
180
257
   * Gets a value from the settings object.
181
258
   *
182
 
   * returns a complex object with two field: ok and data.
183
 
   *
184
 
   * If ok is false something went wrong and the data field will be undefined.
185
 
   *
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.
 
260
   *
 
261
   * @return a <code> complex object </code> with two field: @c ok and @c data.
 
262
   *
 
263
   * If @c ok is false something went wrong and the data field will be undefined.
 
264
   *
 
265
   * If @c ok is true everything is ok and the data field will be set to the
 
266
   * value.
187
267
   */
188
268
  get_setting: function (setting) {
189
269
    let ret_data = {
190
 
      ok: true,
191
 
      data: undefined
 
270
      data: undefined,
 
271
      ok: true
192
272
    };
193
273
    
194
274
    if (typeof setting != "string") {
195
275
      print ("ERROR: The \"setting\" parameter must be a string.");
196
276
      ret_data.ok = false;
197
 
      return ret_data.ok;
 
277
      return ret_data;
198
278
    }
199
279
    
200
280
    switch (setting) {
222
302
      case "main.privacy.only_show_avatar":
223
303
        ret_data.data = this._settings.main.privacy.only_show_avatar;
224
304
        break;
 
305
      case "main.quit_on_close":
 
306
        ret_data.data = this._settings.main.quit_on_close;
 
307
        break;
225
308
      default:
226
309
        ret_data.ok = false;
227
310
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
231
314
    return ret_data;
232
315
  },
233
316
  
234
 
  /**
 
317
  /** @method
235
318
   * Commits changes to the settings object to file.
236
319
   */
237
320
  commit_to_file: function () {