/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-02 21:00:08 UTC
  • mfrom: (52.1.12 GPump_test)
  • Revision ID: gustav.hartvigsson@gmail.com-20141202210008-v9ma32lgz57i0ikb
* Made the window behave when it is shown

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 KILL_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
84
    this._settings_file_path = "";
42
85
    this._settings_file_path += GLib.get_user_config_dir ();
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
          kill_on_close: KILL_ON_CLOSE.UNKNOWN
67
111
        }
68
112
      };
69
113
      // DEBUG:
96
140
    }
97
141
  },
98
142
  
99
 
  /**
 
143
  /** @method
100
144
   * Sets a value in the setting object.
101
145
   * 
102
 
   * return: false on fail
103
 
   * return: true when everything is OK.
 
146
   * @return false on fail
 
147
   * @return true when everything is OK.
104
148
   */
105
149
  set_setting: function (setting, value) {
 
150
    let ret_val = true;
106
151
    if (typeof setting != "string") {
107
152
      print ("ERROR: The \"setting\" parameter must be a string.");
108
153
      return false;
113
158
          this._settings.ui.x = value;
114
159
        } else {
115
160
          print ("The setting \"ui.x\" must be a number.");
 
161
          ret_val = false;
116
162
        }
117
163
        break;
118
164
      case "ui.y":
120
166
          this._settings.ui.y = value;
121
167
        } else {
122
168
          print ("The setting \"ui.y\" must be a number.");
 
169
          ret_val = false;
123
170
        }
124
171
        break;
125
172
      case "ui.h":
127
174
          this._settings.ui.h = value;
128
175
        } else {
129
176
          print ("The setting \"ui.h\" must be a number.");
 
177
          ret_val = false;
130
178
        }
131
179
        break;
132
180
      case "ui.w":
134
182
          this._settings.ui.w = value;
135
183
        } else {
136
184
          print ("The setting \"ui.w\" must be a number.");
 
185
          ret_val = false;
137
186
        }
138
187
        break;
139
188
      case "main.use_dark":
141
190
          this._settings.main.use_dark = value;
142
191
        } else {
143
192
          print ("The setting \"main.use_dark\" must be a boolean.");
 
193
          ret_val = false;
144
194
        }
145
195
        break;
146
196
      case "main.first_run":
148
198
          this._settings.main.first_run = value;
149
199
        } else {
150
200
          print ("The setting \"main.first_run\" must be a boolean.");
 
201
          ret_val = false;
 
202
        }
 
203
        break;
 
204
      case "main.kill_on_close":
 
205
        if (typeof value == "boolean") {
 
206
          this._settings.main.window_close_kill = value;
 
207
        } else {
 
208
          print (
 
209
            "The setting \"main.kill_on_close\" must be a boolean.");
 
210
          ret_val = false;
151
211
        }
152
212
        break;
153
213
      case "main.privacy.show_full_name":
155
215
          this._settings.main.privacy.show_full_name = value;
156
216
        } else {
157
217
          print (
158
 
          "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
218
            "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
219
          ret_val = false;
159
220
        }
160
221
        break;
161
222
      case "main.privacy.only_show_avatar":
162
 
        if (typeof value == "boolean") {
 
223
        if (typeof value == "number") {
163
224
          this._settings.main.privacy.only_show_avatar = value;
164
225
        } else {
165
226
          print (
166
 
          "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
227
            "The setting \"main.privacy.only_show_avatar\" must be a number.");
 
228
          ret_val = false;
167
229
        }
168
230
        break;
169
231
      default:
173
235
        break;
174
236
    }
175
237
    
176
 
    return true;
 
238
    
 
239
    /* Because, reasons. */
 
240
    if (ret_val) {
 
241
      this.emit ("change", setting, value);
 
242
    }
 
243
    
 
244
    return ret_val;
177
245
  },
178
246
  
179
 
  /**
 
247
  /** @method
180
248
   * Gets a value from the settings object.
181
249
   *
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.
 
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.
187
258
   */
188
259
  get_setting: function (setting) {
189
260
    let ret_data = {
231
302
    return ret_data;
232
303
  },
233
304
  
234
 
  /**
 
305
  /** @method
235
306
   * Commits changes to the settings object to file.
236
307
   */
237
308
  commit_to_file: function () {