/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk
33 by Gustav Hartvigsson
* Main window no on par with the C version
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
52.1.1 by Gustav Hartvigsson
Button still not worknig, saving for prosparitny
8
33 by Gustav Hartvigsson
* Main window no on par with the C version
9
const Gio = imports.gi.Gio;
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
10
const GLib = imports.gi.GLib;
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
11
const GObject = imports.gi.GObject;
33 by Gustav Hartvigsson
* Main window no on par with the C version
12
const Lang = imports.lang;
39 by Gustav Hartvigsson
* Fixed a runtime error
13
const System = imports.system;
33 by Gustav Hartvigsson
* Main window no on par with the C version
14
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
15
let _default_settings_object = null;
38 by Gustav Hartvigsson
* Can now load settings from file.
16
52.1.1 by Gustav Hartvigsson
Button still not worknig, saving for prosparitny
17
/** @file
18
 */
19
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
20
/** @function
38 by Gustav Hartvigsson
* Can now load settings from file.
21
 * Please use this function when getting the settings data object. It is not
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
22
 * good to have multiple instances of the settings data object, it may
23
 * cause problems down the line.
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
24
 *
25
 * @return The the default @c SettingsData object
38 by Gustav Hartvigsson
* Can now load settings from file.
26
 */
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
27
function get_settings () {
28
  if (!_default_settings_object) {
29
    _default_settings_object = new SettingsData ();
30
  }
31
  return _default_settings_object;
32
}
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
33
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
34
/** @enum
35
 */
55 by Gustav Hartvigsson
* Code cleanup
36
const QUIT_ON_CLOSE = {
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
37
  /** 
38
   * Default value, will show the close dialogue on clicking the close button
39
   * in the main window.
40
   */
55 by Gustav Hartvigsson
* Code cleanup
41
  UNKNOWN: -1,
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
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
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
48
/** @class
38 by Gustav Hartvigsson
* Can now load settings from file.
49
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
50
 */
34 by Gustav Hartvigsson
* added GetText stuffs...
51
const SettingsData = new Lang.Class ({
52
  Name: 'SettingsData',
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
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': {
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
67
      param_types: [GObject.TYPE_STRING, GObject.TYPE_INT]
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
68
    }
69
  },
70
  
34 by Gustav Hartvigsson
* added GetText stuffs...
71
  
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
72
  /* Member definitions */
73
  _settings_file_path: String,
74
  _settings: Array,
75
  _settings_file: Gio.File,
76
  
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
77
  /**
78
   * 
79
   */
80
  _init: function (params) {
81
    this.parent (params);
82
    
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
83
    /* First we construct the path for where to store the settings file. */
58 by Gustav Hartvigsson
* Cleaded up a lille code
84
    this._settings_file_path = "" + 
85
                              GLib.get_user_config_dir () +
86
                              "/gpump/gpump.json";
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
87
    print ("Config file is: " + this._settings_file_path);
88
    
89
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
90
    
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
91
    /* Then we check that the file exists. If the file does not exists we
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
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,
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
109
          first_run: true,
55 by Gustav Hartvigsson
* Code cleanup
110
          quit_on_close: QUIT_ON_CLOSE.UNKNOWN
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
111
        }
112
      };
113
      // DEBUG:
114
      print (JSON.stringify (this._settings, null, 2).toString () );
115
      
58 by Gustav Hartvigsson
* Cleaded up a lille code
116
      // Check if the folder exists, and if it does not exist, create it.
59 by Gustav Hartvigsson
* Woops
117
      let folder_path = "" +
118
                        GLib.get_user_config_dir () +
119
                        "/gpump/";
58 by Gustav Hartvigsson
* Cleaded up a lille code
120
      let folder = Gio.File.new_for_path (folder_path);
121
      if (!GLib.file_test (folder_path, GLib.FileTest.EXISTS)) {
122
        folder.make_directory (null);
123
      }
124
      
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
125
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
126
                                                    null);
58 by Gustav Hartvigsson
* Cleaded up a lille code
127
      
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
128
      file_stream.write_all (JSON.stringify (
129
          this._settings, null, 2).toString (), null);
130
      
131
      file_stream.close (null);
132
      
133
    } else {
134
      /* The file exists, we load the settings data into memory */
38 by Gustav Hartvigsson
* Can now load settings from file.
135
      let file_stream = this._settings_file.read (null);
136
      
137
      /* See: http://stackoverflow.com/a/21146281
138
       */
139
      let file_info = this._settings_file.query_info("standard::size",
140
                                             Gio.FileQueryInfoFlags.NONE, null);
141
      let size = file_info.get_size ();
142
      
143
      let buffer = file_stream.read_bytes (size, null).get_data ();
144
      
145
      this._settings = JSON.parse (buffer);
146
      
147
      //DEBUG:
148
      print (JSON.stringify (this._settings, null, 2).toString () );
149
      
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
150
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
151
  },
152
  
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
153
  /** @method
38 by Gustav Hartvigsson
* Can now load settings from file.
154
   * Sets a value in the setting object.
39 by Gustav Hartvigsson
* Fixed a runtime error
155
   * 
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
156
   * @return false on fail
157
   * @return true when everything is OK.
38 by Gustav Hartvigsson
* Can now load settings from file.
158
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
159
  set_setting: function (setting, value) {
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
160
    let ret_val = true;
39 by Gustav Hartvigsson
* Fixed a runtime error
161
    if (typeof setting != "string") {
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
162
      print ("ERROR: The \"setting\" parameter must be a string.");
39 by Gustav Hartvigsson
* Fixed a runtime error
163
      return false;
164
    }
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
165
    switch (setting) {
166
      case "ui.x":
167
        if (typeof value == "number") {
168
          this._settings.ui.x = value;
169
        } else {
170
          print ("The setting \"ui.x\" must be a number.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
171
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
172
        }
173
        break;
174
      case "ui.y":
175
        if (typeof value == "number") {
176
          this._settings.ui.y = value;
177
        } else {
178
          print ("The setting \"ui.y\" must be a number.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
179
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
180
        }
181
        break;
182
      case "ui.h":
183
        if (typeof value == "number") {
184
          this._settings.ui.h = value;
185
        } else {
186
          print ("The setting \"ui.h\" must be a number.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
187
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
188
        }
189
        break;
190
      case "ui.w":
191
        if (typeof value == "number") {
192
          this._settings.ui.w = value;
193
        } else {
194
          print ("The setting \"ui.w\" must be a number.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
195
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
196
        }
197
        break;
198
      case "main.use_dark":
199
        if (typeof value == "boolean") {
200
          this._settings.main.use_dark = value;
201
        } else {
202
          print ("The setting \"main.use_dark\" must be a boolean.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
203
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
204
        }
205
        break;
206
      case "main.first_run":
207
        if (typeof value == "boolean") {
208
          this._settings.main.first_run = value;
209
        } else {
210
          print ("The setting \"main.first_run\" must be a boolean.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
211
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
212
        }
213
        break;
55 by Gustav Hartvigsson
* Code cleanup
214
      case "main.quit_on_close":
215
        if (typeof value == "number") {
216
          this._settings.main.quit_on_close = value;
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
217
        } else {
218
          print (
55 by Gustav Hartvigsson
* Code cleanup
219
            "The setting \"main.quit_on_close\" must be a number.");
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
220
          ret_val = false;
221
        }
222
        break;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
223
      case "main.privacy.show_full_name":
224
        if (typeof value == "boolean") {
225
          this._settings.main.privacy.show_full_name = value;
226
        } else {
227
          print (
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
228
            "The setting \"main.privacy.show_full_name\" must be a boolean.");
229
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
230
        }
231
        break;
232
      case "main.privacy.only_show_avatar":
55 by Gustav Hartvigsson
* Code cleanup
233
        if (typeof value == "boolean") {
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
234
          this._settings.main.privacy.only_show_avatar = value;
235
        } else {
236
          print (
55 by Gustav Hartvigsson
* Code cleanup
237
            "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
238
          ret_val = false;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
239
        }
240
        break;
241
      default:
242
        
243
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
244
        return false;
245
        break;
246
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
247
    
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
248
    
52.1.10 by Gustav Hartvigsson
* made the UserButton work in tests.
249
    /* Because, reasons. */
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
250
    if (ret_val) {
251
      this.emit ("change", setting, value);
252
    }
253
    
254
    return ret_val;
38 by Gustav Hartvigsson
* Can now load settings from file.
255
  },
256
  
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
257
  /** @method
38 by Gustav Hartvigsson
* Can now load settings from file.
258
   * Gets a value from the settings object.
39 by Gustav Hartvigsson
* Fixed a runtime error
259
   *
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
260
   * @param setting the @c String that corrosponds to the setting to get.
261
   *
262
   * @return a <code> complex object </code> with two field: @c ok and @c data.
263
   *
264
   * If @c ok is false something went wrong and the data field will be undefined.
265
   *
266
   * If @c ok is true everything is ok and the data field will be set to the
267
   * value.
38 by Gustav Hartvigsson
* Can now load settings from file.
268
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
269
  get_setting: function (setting) {
270
    let ret_data = {
55 by Gustav Hartvigsson
* Code cleanup
271
      data: undefined,
272
      ok: true
39 by Gustav Hartvigsson
* Fixed a runtime error
273
    };
38 by Gustav Hartvigsson
* Can now load settings from file.
274
    
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
275
    if (typeof setting != "string") {
276
      print ("ERROR: The \"setting\" parameter must be a string.");
277
      ret_data.ok = false;
50 by Gustav Hartvigsson
* bound the GtkSwitch for "use dark theme"
278
      return ret_data;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
279
    }
280
    
281
    switch (setting) {
282
      case "ui.x":
283
        ret_data.data = this._settings.ui.x;
284
        break;
285
      case "ui.y":
286
        ret_data.data = this._settings.ui.y;
287
        break;
288
      case "ui.h":
289
        ret_data.data = this._settings.ui.h;
290
        break;
291
      case "ui.w":
292
        ret_data.data = this._settings.ui.w;
293
        break;
294
      case "main.use_dark":
295
        ret_data.data = this._settings.main.use_dark;
296
        break;
297
      case "main.first_run":
298
        ret_data.data = this._settings.main.first_run;
299
        break;
300
      case "main.privacy.show_full_name":
301
        ret_data.data = this._settings.main.privacy.show_full_name;
302
        break;
303
      case "main.privacy.only_show_avatar":
304
        ret_data.data = this._settings.main.privacy.only_show_avatar;
305
        break;
55 by Gustav Hartvigsson
* Code cleanup
306
      case "main.quit_on_close":
307
        ret_data.data = this._settings.main.quit_on_close;
308
        break;
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
309
      default:
310
        ret_data.ok = false;
311
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
312
        break;
313
    }
314
    
39 by Gustav Hartvigsson
* Fixed a runtime error
315
    return ret_data;
38 by Gustav Hartvigsson
* Can now load settings from file.
316
  },
317
  
52.1.8 by Gustav Hartvigsson
* Strted work on changing how the SettingsData class works,
318
  /** @method
38 by Gustav Hartvigsson
* Can now load settings from file.
319
   * Commits changes to the settings object to file.
320
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
321
  commit_to_file: function () {
42 by Gustav Hartvigsson
* Finnished the commit to file method. Still not tested!
322
    print (JSON.stringify (this._settings, null, 2).toString () );
323
      
324
      let file_stream = this._settings_file.replace (null,
325
                                                    false,
326
                                                    Gio.FileCreateFlags.PRIVATE,
327
                                                    null);
328
      file_stream.write_all (JSON.stringify (
329
          this._settings, null, 2).toString (), null);
330
      
331
      file_stream.close (null);
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
332
  }
34 by Gustav Hartvigsson
* added GetText stuffs...
333
  
42 by Gustav Hartvigsson
* Finnished the commit to file method. Still not tested!
334
  // End of class.
34 by Gustav Hartvigsson
* added GetText stuffs...
335
});
336
39 by Gustav Hartvigsson
* Fixed a runtime error
337
338