/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: 2014-12-15 21:53:59 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20141215215359-le7vlscruxsjy5m9
* Updated .po file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of GPump, a Pump.io client.
 
2
 *
 
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.
 
6
 */
 
7
 
 
8
 
 
9
const Gio = imports.gi.Gio;
 
10
const GLib = imports.gi.GLib;
 
11
const GObject = imports.gi.GObject;
 
12
const Lang = imports.lang;
 
13
const System = imports.system;
 
14
 
 
15
let _default_settings_object = null;
 
16
 
 
17
/** @file
 
18
 */
 
19
 
 
20
/** @function
 
21
 * Please use this function when getting the settings data object. It is not
 
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
 
26
 */
 
27
function get_settings () {
 
28
  if (!_default_settings_object) {
 
29
    _default_settings_object = new SettingsData ();
 
30
  }
 
31
  return _default_settings_object;
 
32
}
 
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
 
49
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
 
50
 */
 
51
const SettingsData = new Lang.Class ({
 
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
  
 
71
  
 
72
  /* Member definitions */
 
73
  _settings_file_path: String,
 
74
  _settings: Array,
 
75
  _settings_file: Gio.File,
 
76
  
 
77
  /**
 
78
   * 
 
79
   */
 
80
  _init: function (params) {
 
81
    this.parent (params);
 
82
    
 
83
    /* First we construct the path for where to store the settings file. */
 
84
    this._settings_file_path = "";
 
85
    this._settings_file_path += GLib.get_user_config_dir ();
 
86
    this._settings_file_path += "/gpump/gpump.json";
 
87
    print ("Config file is: " + this._settings_file_path);
 
88
    
 
89
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
 
90
    
 
91
    /* Then we check that the file exists. If the file does not exists we
 
92
     * construct some sane default values.
 
93
     */
 
94
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
 
95
      /* -1 is undefined, and will be set at construction of the window */
 
96
      this._settings = {
 
97
        ui: {
 
98
          x: -1,
 
99
          y: -1,
 
100
          h: 500,
 
101
          w: 500
 
102
        },
 
103
        main: {
 
104
          privacy: {
 
105
            show_full_name: true,
 
106
            only_show_avatar: false
 
107
          },
 
108
          use_dark: false,
 
109
          first_run: true,
 
110
          quit_on_close: QUIT_ON_CLOSE.UNKNOWN
 
111
        }
 
112
      };
 
113
      // DEBUG:
 
114
      print (JSON.stringify (this._settings, null, 2).toString () );
 
115
      
 
116
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
 
117
                                                    null);
 
118
      file_stream.write_all (JSON.stringify (
 
119
          this._settings, null, 2).toString (), null);
 
120
      
 
121
      file_stream.close (null);
 
122
      
 
123
    } else {
 
124
      /* The file exists, we load the settings data into memory */
 
125
      let file_stream = this._settings_file.read (null);
 
126
      
 
127
      /* See: http://stackoverflow.com/a/21146281
 
128
       */
 
129
      let file_info = this._settings_file.query_info("standard::size",
 
130
                                             Gio.FileQueryInfoFlags.NONE, null);
 
131
      let size = file_info.get_size ();
 
132
      
 
133
      let buffer = file_stream.read_bytes (size, null).get_data ();
 
134
      
 
135
      this._settings = JSON.parse (buffer);
 
136
      
 
137
      //DEBUG:
 
138
      print (JSON.stringify (this._settings, null, 2).toString () );
 
139
      
 
140
    }
 
141
  },
 
142
  
 
143
  /** @method
 
144
   * Sets a value in the setting object.
 
145
   * 
 
146
   * @return false on fail
 
147
   * @return true when everything is OK.
 
148
   */
 
149
  set_setting: function (setting, value) {
 
150
    let ret_val = true;
 
151
    if (typeof setting != "string") {
 
152
      print ("ERROR: The \"setting\" parameter must be a string.");
 
153
      return false;
 
154
    }
 
155
    switch (setting) {
 
156
      case "ui.x":
 
157
        if (typeof value == "number") {
 
158
          this._settings.ui.x = value;
 
159
        } else {
 
160
          print ("The setting \"ui.x\" must be a number.");
 
161
          ret_val = false;
 
162
        }
 
163
        break;
 
164
      case "ui.y":
 
165
        if (typeof value == "number") {
 
166
          this._settings.ui.y = value;
 
167
        } else {
 
168
          print ("The setting \"ui.y\" must be a number.");
 
169
          ret_val = false;
 
170
        }
 
171
        break;
 
172
      case "ui.h":
 
173
        if (typeof value == "number") {
 
174
          this._settings.ui.h = value;
 
175
        } else {
 
176
          print ("The setting \"ui.h\" must be a number.");
 
177
          ret_val = false;
 
178
        }
 
179
        break;
 
180
      case "ui.w":
 
181
        if (typeof value == "number") {
 
182
          this._settings.ui.w = value;
 
183
        } else {
 
184
          print ("The setting \"ui.w\" must be a number.");
 
185
          ret_val = false;
 
186
        }
 
187
        break;
 
188
      case "main.use_dark":
 
189
        if (typeof value == "boolean") {
 
190
          this._settings.main.use_dark = value;
 
191
        } else {
 
192
          print ("The setting \"main.use_dark\" must be a boolean.");
 
193
          ret_val = false;
 
194
        }
 
195
        break;
 
196
      case "main.first_run":
 
197
        if (typeof value == "boolean") {
 
198
          this._settings.main.first_run = value;
 
199
        } else {
 
200
          print ("The setting \"main.first_run\" must be a boolean.");
 
201
          ret_val = false;
 
202
        }
 
203
        break;
 
204
      case "main.quit_on_close":
 
205
        if (typeof value == "number") {
 
206
          this._settings.main.quit_on_close = value;
 
207
        } else {
 
208
          print (
 
209
            "The setting \"main.quit_on_close\" must be a number.");
 
210
          ret_val = false;
 
211
        }
 
212
        break;
 
213
      case "main.privacy.show_full_name":
 
214
        if (typeof value == "boolean") {
 
215
          this._settings.main.privacy.show_full_name = value;
 
216
        } else {
 
217
          print (
 
218
            "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
219
          ret_val = false;
 
220
        }
 
221
        break;
 
222
      case "main.privacy.only_show_avatar":
 
223
        if (typeof value == "boolean") {
 
224
          this._settings.main.privacy.only_show_avatar = value;
 
225
        } else {
 
226
          print (
 
227
            "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
228
          ret_val = false;
 
229
        }
 
230
        break;
 
231
      default:
 
232
        
 
233
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
 
234
        return false;
 
235
        break;
 
236
    }
 
237
    
 
238
    
 
239
    /* Because, reasons. */
 
240
    if (ret_val) {
 
241
      this.emit ("change", setting, value);
 
242
    }
 
243
    
 
244
    return ret_val;
 
245
  },
 
246
  
 
247
  /** @method
 
248
   * Gets a value from the settings object.
 
249
   *
 
250
   * @param setting the @c String that corrosponds to the setting to get.
 
251
   *
 
252
   * @return a <code> complex object </code> with two field: @c ok and @c data.
 
253
   *
 
254
   * If @c ok is false something went wrong and the data field will be undefined.
 
255
   *
 
256
   * If @c ok is true everything is ok and the data field will be set to the
 
257
   * value.
 
258
   */
 
259
  get_setting: function (setting) {
 
260
    let ret_data = {
 
261
      data: undefined,
 
262
      ok: true
 
263
    };
 
264
    
 
265
    if (typeof setting != "string") {
 
266
      print ("ERROR: The \"setting\" parameter must be a string.");
 
267
      ret_data.ok = false;
 
268
      return ret_data;
 
269
    }
 
270
    
 
271
    switch (setting) {
 
272
      case "ui.x":
 
273
        ret_data.data = this._settings.ui.x;
 
274
        break;
 
275
      case "ui.y":
 
276
        ret_data.data = this._settings.ui.y;
 
277
        break;
 
278
      case "ui.h":
 
279
        ret_data.data = this._settings.ui.h;
 
280
        break;
 
281
      case "ui.w":
 
282
        ret_data.data = this._settings.ui.w;
 
283
        break;
 
284
      case "main.use_dark":
 
285
        ret_data.data = this._settings.main.use_dark;
 
286
        break;
 
287
      case "main.first_run":
 
288
        ret_data.data = this._settings.main.first_run;
 
289
        break;
 
290
      case "main.privacy.show_full_name":
 
291
        ret_data.data = this._settings.main.privacy.show_full_name;
 
292
        break;
 
293
      case "main.privacy.only_show_avatar":
 
294
        ret_data.data = this._settings.main.privacy.only_show_avatar;
 
295
        break;
 
296
      case "main.quit_on_close":
 
297
        ret_data.data = this._settings.main.quit_on_close;
 
298
        break;
 
299
      default:
 
300
        ret_data.ok = false;
 
301
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
 
302
        break;
 
303
    }
 
304
    
 
305
    return ret_data;
 
306
  },
 
307
  
 
308
  /** @method
 
309
   * Commits changes to the settings object to file.
 
310
   */
 
311
  commit_to_file: function () {
 
312
    print (JSON.stringify (this._settings, null, 2).toString () );
 
313
      
 
314
      let file_stream = this._settings_file.replace (null,
 
315
                                                    false,
 
316
                                                    Gio.FileCreateFlags.PRIVATE,
 
317
                                                    null);
 
318
      file_stream.write_all (JSON.stringify (
 
319
          this._settings, null, 2).toString (), null);
 
320
      
 
321
      file_stream.close (null);
 
322
  }
 
323
  
 
324
  // End of class.
 
325
});
 
326
 
 
327
 
 
328