/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
 
 * good to 
 
22
 * good to have multiple instances of the settings data object, it may
 
23
 * cause problems down the line.
 
24
 *
 
25
 * @return The the default @c SettingsData object
19
26
 */
20
27
function get_settings () {
21
28
  if (!_default_settings_object) {
23
30
  }
24
31
  return _default_settings_object;
25
32
}
26
 
/**
 
33
 
 
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
27
49
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
28
50
 */
29
51
const SettingsData = new Lang.Class ({
30
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
  
31
71
  
32
72
  /* Member definitions */
33
73
  _settings_file_path: String,
34
74
  _settings: Array,
35
75
  _settings_file: Gio.File,
36
76
  
37
 
  _init: function () {
 
77
  /**
 
78
   * 
 
79
   */
 
80
  _init: function (params) {
 
81
    this.parent (params);
 
82
    
38
83
    /* First we construct the path for where to store the settings file. */
39
 
    this._settings_file_path = "";
40
 
    this._settings_file_path += GLib.get_user_config_dir ();
41
 
    this._settings_file_path += "/gpump/gpump.json";
 
84
    this._settings_file_path = "" + 
 
85
                              GLib.get_user_config_dir () +
 
86
                              "/gpump/gpump.json";
42
87
    print ("Config file is: " + this._settings_file_path);
43
88
    
44
89
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
45
90
    
46
 
    /* 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
47
92
     * construct some sane default values.
48
93
     */
49
94
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
61
106
            only_show_avatar: false
62
107
          },
63
108
          use_dark: false,
64
 
          first_run: true
 
109
          first_run: true,
 
110
          quit_on_close: QUIT_ON_CLOSE.UNKNOWN
65
111
        }
66
112
      };
67
113
      // DEBUG:
68
114
      print (JSON.stringify (this._settings, null, 2).toString () );
69
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
      
70
124
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
71
125
                                                    null);
 
126
      
72
127
      file_stream.write_all (JSON.stringify (
73
128
          this._settings, null, 2).toString (), null);
74
129
      
94
149
    }
95
150
  },
96
151
  
97
 
  /**
 
152
  /** @method
98
153
   * Sets a value in the setting object.
99
154
   * 
100
 
   * return: false on fail
101
 
   * return: true when everything is OK.
 
155
   * @return false on fail
 
156
   * @return true when everything is OK.
102
157
   */
103
158
  set_setting: function (setting, value) {
 
159
    let ret_val = true;
104
160
    if (typeof setting != "string") {
105
161
      print ("ERROR: The \"setting\" parameter must be a string.");
106
162
      return false;
111
167
          this._settings.ui.x = value;
112
168
        } else {
113
169
          print ("The setting \"ui.x\" must be a number.");
 
170
          ret_val = false;
114
171
        }
115
172
        break;
116
173
      case "ui.y":
118
175
          this._settings.ui.y = value;
119
176
        } else {
120
177
          print ("The setting \"ui.y\" must be a number.");
 
178
          ret_val = false;
121
179
        }
122
180
        break;
123
181
      case "ui.h":
125
183
          this._settings.ui.h = value;
126
184
        } else {
127
185
          print ("The setting \"ui.h\" must be a number.");
 
186
          ret_val = false;
128
187
        }
129
188
        break;
130
189
      case "ui.w":
132
191
          this._settings.ui.w = value;
133
192
        } else {
134
193
          print ("The setting \"ui.w\" must be a number.");
 
194
          ret_val = false;
135
195
        }
136
196
        break;
137
197
      case "main.use_dark":
139
199
          this._settings.main.use_dark = value;
140
200
        } else {
141
201
          print ("The setting \"main.use_dark\" must be a boolean.");
 
202
          ret_val = false;
142
203
        }
143
204
        break;
144
205
      case "main.first_run":
146
207
          this._settings.main.first_run = value;
147
208
        } else {
148
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;
149
220
        }
150
221
        break;
151
222
      case "main.privacy.show_full_name":
153
224
          this._settings.main.privacy.show_full_name = value;
154
225
        } else {
155
226
          print (
156
 
          "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;
157
229
        }
158
230
        break;
159
231
      case "main.privacy.only_show_avatar":
161
233
          this._settings.main.privacy.only_show_avatar = value;
162
234
        } else {
163
235
          print (
164
 
          "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;
165
238
        }
166
239
        break;
167
240
      default:
171
244
        break;
172
245
    }
173
246
    
174
 
    return true;
 
247
    
 
248
    /* Because, reasons. */
 
249
    if (ret_val) {
 
250
      this.emit ("change", setting, value);
 
251
    }
 
252
    
 
253
    return ret_val;
175
254
  },
176
255
  
177
 
  /**
 
256
  /** @method
178
257
   * Gets a value from the settings object.
179
258
   *
180
 
   * returns a complex object with two field: ok and data.
181
 
   *
182
 
   * If ok is false something went wrong and the data field will be undefined.
183
 
   *
184
 
   * 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.
185
267
   */
186
268
  get_setting: function (setting) {
187
269
    let ret_data = {
188
 
      ok: true,
189
 
      data: undefined
 
270
      data: undefined,
 
271
      ok: true
190
272
    };
191
273
    
192
274
    if (typeof setting != "string") {
193
275
      print ("ERROR: The \"setting\" parameter must be a string.");
194
276
      ret_data.ok = false;
195
 
      return ret_data.ok;
 
277
      return ret_data;
196
278
    }
197
279
    
198
280
    switch (setting) {
220
302
      case "main.privacy.only_show_avatar":
221
303
        ret_data.data = this._settings.main.privacy.only_show_avatar;
222
304
        break;
 
305
      case "main.quit_on_close":
 
306
        ret_data.data = this._settings.main.quit_on_close;
 
307
        break;
223
308
      default:
224
309
        ret_data.ok = false;
225
310
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
229
314
    return ret_data;
230
315
  },
231
316
  
232
 
  /**
 
317
  /** @method
233
318
   * Commits changes to the settings object to file.
234
319
   */
235
320
  commit_to_file: function () {
236
 
    
 
321
    print (JSON.stringify (this._settings, null, 2).toString () );
 
322
      
 
323
      let file_stream = this._settings_file.replace (null,
 
324
                                                    false,
 
325
                                                    Gio.FileCreateFlags.PRIVATE,
 
326
                                                    null);
 
327
      file_stream.write_all (JSON.stringify (
 
328
          this._settings, null, 2).toString (), null);
 
329
      
 
330
      file_stream.close (null);
237
331
  }
238
332
  
 
333
  // End of class.
239
334
});
240
335
 
241
336