/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-11-19 22:05:36 UTC
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: gustav.hartvigsson@gmail.com-20141119220536-982uyozuq4d0f13h
* Strted work on changing how the SettingsData class works,
  should be better when I am done.
* Started commenting my code so it will be easier to mentain in the future.
  Not that there is much code, but anyway.

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
/** @class
32
35
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
33
36
 */
34
37
const SettingsData = new Lang.Class ({
35
38
  Name: 'SettingsData',
 
39
  Extends: GObject.Object,
 
40
  Signals: {
 
41
    /** @signal change
 
42
     * When hooking up to this signal check that the name of the change.
 
43
     * 
 
44
     * The function signature is <code> function my_callback (String: setting,
 
45
     * Value: value) </code> where @c setting is the name of the setting,
 
46
     * and @c value is the value to be that it has changed to.
 
47
     *
 
48
     * @warning Always check the @c value type and the @c setting name.
 
49
     *
 
50
     * @return void
 
51
     */
 
52
    'change': {
 
53
      param_types: [GObject.TYPE_STRING, GObject.TYPE_BOOLEAN]
 
54
    }
 
55
  },
 
56
  
36
57
  
37
58
  /* Member definitions */
38
59
  _settings_file_path: String,
39
60
  _settings: Array,
40
61
  _settings_file: Gio.File,
41
62
  
42
 
  _init: function () {
 
63
  /**
 
64
   * 
 
65
   */
 
66
  _init: function (params) {
 
67
    this.parent (params);
 
68
    
43
69
    /* First we construct the path for where to store the settings file. */
44
70
    this._settings_file_path = "";
45
71
    this._settings_file_path += GLib.get_user_config_dir ();
99
125
    }
100
126
  },
101
127
  
102
 
  /**
 
128
  /** @method
103
129
   * Sets a value in the setting object.
104
130
   * 
105
 
   * return: false on fail
106
 
   * return: true when everything is OK.
 
131
   * @return false on fail
 
132
   * @return true when everything is OK.
107
133
   */
108
134
  set_setting: function (setting, value) {
 
135
    let ret_val = true;
109
136
    if (typeof setting != "string") {
110
137
      print ("ERROR: The \"setting\" parameter must be a string.");
111
138
      return false;
116
143
          this._settings.ui.x = value;
117
144
        } else {
118
145
          print ("The setting \"ui.x\" must be a number.");
 
146
          ret_val = false;
119
147
        }
120
148
        break;
121
149
      case "ui.y":
123
151
          this._settings.ui.y = value;
124
152
        } else {
125
153
          print ("The setting \"ui.y\" must be a number.");
 
154
          ret_val = false;
126
155
        }
127
156
        break;
128
157
      case "ui.h":
130
159
          this._settings.ui.h = value;
131
160
        } else {
132
161
          print ("The setting \"ui.h\" must be a number.");
 
162
          ret_val = false;
133
163
        }
134
164
        break;
135
165
      case "ui.w":
137
167
          this._settings.ui.w = value;
138
168
        } else {
139
169
          print ("The setting \"ui.w\" must be a number.");
 
170
          ret_val = false;
140
171
        }
141
172
        break;
142
173
      case "main.use_dark":
144
175
          this._settings.main.use_dark = value;
145
176
        } else {
146
177
          print ("The setting \"main.use_dark\" must be a boolean.");
 
178
          ret_val = false;
147
179
        }
148
180
        break;
149
181
      case "main.first_run":
151
183
          this._settings.main.first_run = value;
152
184
        } else {
153
185
          print ("The setting \"main.first_run\" must be a boolean.");
 
186
          ret_val = false;
154
187
        }
155
188
        break;
156
189
      case "main.privacy.show_full_name":
158
191
          this._settings.main.privacy.show_full_name = value;
159
192
        } else {
160
193
          print (
161
 
          "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
194
            "The setting \"main.privacy.show_full_name\" must be a boolean.");
 
195
          ret_val = false;
162
196
        }
163
197
        break;
164
198
      case "main.privacy.only_show_avatar":
166
200
          this._settings.main.privacy.only_show_avatar = value;
167
201
        } else {
168
202
          print (
169
 
          "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
203
            "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
 
204
          ret_val = false;
170
205
        }
171
206
        break;
172
207
      default:
176
211
        break;
177
212
    }
178
213
    
179
 
    return true;
 
214
    
 
215
    /* Becouse, reasons. */
 
216
    if (ret_val) {
 
217
      this.emit ("change", setting, value);
 
218
    }
 
219
    
 
220
    return ret_val;
180
221
  },
181
222
  
182
 
  /**
 
223
  /** @method
183
224
   * Gets a value from the settings object.
184
225
   *
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.
 
226
   * @param setting the @c String that corrosponds to the setting to get.
 
227
   *
 
228
   * @return a <code> complex object </code> with two field: @c ok and @c data.
 
229
   *
 
230
   * If @c ok is false something went wrong and the data field will be undefined.
 
231
   *
 
232
   * If @c ok is true everything is ok and the data field will be set to the
 
233
   * value.
190
234
   */
191
235
  get_setting: function (setting) {
192
236
    let ret_data = {
234
278
    return ret_data;
235
279
  },
236
280
  
237
 
  /**
 
281
  /** @method
238
282
   * Commits changes to the settings object to file.
239
283
   */
240
284
  commit_to_file: function () {