/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-08-03 21:09:38 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140803210938-jfhdl23v4mzji6pf
* Added translation files (messeges.po and sv.po)
* fixed a few gettext errors in app.js
* TODO:
  * Make the translations not be as "fixed" as they are now.
  * Add auto update/compile for the translations to makefile.

  * Switch to a better build system?

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