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