/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
8
const Gio = imports.gi.Gio;
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
9
const GLib = imports.gi.GLib;
38 by Gustav Hartvigsson
* Can now load settings from file.
10
33 by Gustav Hartvigsson
* Main window no on par with the C version
11
const Lang = imports.lang;
39 by Gustav Hartvigsson
* Fixed a runtime error
12
const System = imports.system;
33 by Gustav Hartvigsson
* Main window no on par with the C version
13
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
14
let _default_settings_object = null;
38 by Gustav Hartvigsson
* Can now load settings from file.
15
16
/**
17
 * 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.
18
 * good to have multiple instances of the settings data object, it may
19
 * cause problems down the line.
38 by Gustav Hartvigsson
* Can now load settings from file.
20
 */
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
21
function get_settings () {
22
  if (!_default_settings_object) {
23
    _default_settings_object = new SettingsData ();
24
  }
25
  return _default_settings_object;
26
}
44 by Gustav Hartvigsson
* UI for preferences done, it is still not functional.
27
38 by Gustav Hartvigsson
* Can now load settings from file.
28
/**
29
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
30
 */
34 by Gustav Hartvigsson
* added GetText stuffs...
31
const SettingsData = new Lang.Class ({
32
  Name: 'SettingsData',
33
  
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
34
  /* Member definitions */
35
  _settings_file_path: String,
36
  _settings: Array,
37
  _settings_file: Gio.File,
38
  
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
39
  _init: function () {
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
40
    /* First we construct the path for where to store the settings file. */
41
    this._settings_file_path = "";
42
    this._settings_file_path += GLib.get_user_config_dir ();
43
    this._settings_file_path += "/gpump/gpump.json";
44
    print ("Config file is: " + this._settings_file_path);
45
    
46
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
47
    
48
    /* Then we check that the file exists. If the file doen not ekist we
49
     * construct some sane default values.
50
     */
51
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
52
      /* -1 is undefined, and will be set at construction of the window */
53
      this._settings = {
54
        ui: {
55
          x: -1,
56
          y: -1,
57
          h: 500,
58
          w: 500
59
        },
60
        main: {
61
          privacy: {
62
            show_full_name: true,
63
            only_show_avatar: false
64
          },
65
          use_dark: false,
66
          first_run: true
67
        }
68
      };
69
      // DEBUG:
70
      print (JSON.stringify (this._settings, null, 2).toString () );
71
      
72
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
73
                                                    null);
74
      file_stream.write_all (JSON.stringify (
75
          this._settings, null, 2).toString (), null);
76
      
77
      file_stream.close (null);
78
      
79
    } else {
80
      /* The file exists, we load the settings data into memory */
38 by Gustav Hartvigsson
* Can now load settings from file.
81
      let file_stream = this._settings_file.read (null);
82
      
83
      /* See: http://stackoverflow.com/a/21146281
84
       */
85
      let file_info = this._settings_file.query_info("standard::size",
86
                                             Gio.FileQueryInfoFlags.NONE, null);
87
      let size = file_info.get_size ();
88
      
89
      let buffer = file_stream.read_bytes (size, null).get_data ();
90
      
91
      this._settings = JSON.parse (buffer);
92
      
93
      //DEBUG:
94
      print (JSON.stringify (this._settings, null, 2).toString () );
95
      
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
96
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
97
  },
98
  
99
  /**
100
   * Sets a value in the setting object.
39 by Gustav Hartvigsson
* Fixed a runtime error
101
   * 
102
   * return: false on fail
103
   * return: true when everything is OK.
38 by Gustav Hartvigsson
* Can now load settings from file.
104
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
105
  set_setting: function (setting, value) {
106
    if (typeof setting != "string") {
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
107
      print ("ERROR: The \"setting\" parameter must be a string.");
39 by Gustav Hartvigsson
* Fixed a runtime error
108
      return false;
109
    }
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
110
    switch (setting) {
111
      case "ui.x":
112
        if (typeof value == "number") {
113
          this._settings.ui.x = value;
114
        } else {
115
          print ("The setting \"ui.x\" must be a number.");
116
        }
117
        break;
118
      case "ui.y":
119
        if (typeof value == "number") {
120
          this._settings.ui.y = value;
121
        } else {
122
          print ("The setting \"ui.y\" must be a number.");
123
        }
124
        break;
125
      case "ui.h":
126
        if (typeof value == "number") {
127
          this._settings.ui.h = value;
128
        } else {
129
          print ("The setting \"ui.h\" must be a number.");
130
        }
131
        break;
132
      case "ui.w":
133
        if (typeof value == "number") {
134
          this._settings.ui.w = value;
135
        } else {
136
          print ("The setting \"ui.w\" must be a number.");
137
        }
138
        break;
139
      case "main.use_dark":
140
        if (typeof value == "boolean") {
141
          this._settings.main.use_dark = value;
142
        } else {
143
          print ("The setting \"main.use_dark\" must be a boolean.");
144
        }
145
        break;
146
      case "main.first_run":
147
        if (typeof value == "boolean") {
148
          this._settings.main.first_run = value;
149
        } else {
150
          print ("The setting \"main.first_run\" must be a boolean.");
151
        }
152
        break;
153
      case "main.privacy.show_full_name":
154
        if (typeof value == "boolean") {
155
          this._settings.main.privacy.show_full_name = value;
156
        } else {
157
          print (
158
          "The setting \"main.privacy.show_full_name\" must be a boolean.");
159
        }
160
        break;
161
      case "main.privacy.only_show_avatar":
162
        if (typeof value == "boolean") {
163
          this._settings.main.privacy.only_show_avatar = value;
164
        } else {
165
          print (
166
          "The setting \"main.privacy.only_show_avatar\" must be a boolean.");
167
        }
168
        break;
169
      default:
170
        
171
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
172
        return false;
173
        break;
174
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
175
    
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
176
    return true;
38 by Gustav Hartvigsson
* Can now load settings from file.
177
  },
178
  
179
  /**
180
   * Gets a value from the settings object.
39 by Gustav Hartvigsson
* Fixed a runtime error
181
   *
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
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.
38 by Gustav Hartvigsson
* Can now load settings from file.
187
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
188
  get_setting: function (setting) {
189
    let ret_data = {
190
      ok: true,
191
      data: undefined
192
    };
38 by Gustav Hartvigsson
* Can now load settings from file.
193
    
41 by Gustav Hartvigsson
* finnished the getter and setter for the settings. Still untested.
194
    if (typeof setting != "string") {
195
      print ("ERROR: The \"setting\" parameter must be a string.");
196
      ret_data.ok = false;
197
      return ret_data.ok;
198
    }
199
    
200
    switch (setting) {
201
      case "ui.x":
202
        ret_data.data = this._settings.ui.x;
203
        break;
204
      case "ui.y":
205
        ret_data.data = this._settings.ui.y;
206
        break;
207
      case "ui.h":
208
        ret_data.data = this._settings.ui.h;
209
        break;
210
      case "ui.w":
211
        ret_data.data = this._settings.ui.w;
212
        break;
213
      case "main.use_dark":
214
        ret_data.data = this._settings.main.use_dark;
215
        break;
216
      case "main.first_run":
217
        ret_data.data = this._settings.main.first_run;
218
        break;
219
      case "main.privacy.show_full_name":
220
        ret_data.data = this._settings.main.privacy.show_full_name;
221
        break;
222
      case "main.privacy.only_show_avatar":
223
        ret_data.data = this._settings.main.privacy.only_show_avatar;
224
        break;
225
      default:
226
        ret_data.ok = false;
227
        print ("ERROR: The setting \"" + setting + "\" does not exist.");
228
        break;
229
    }
230
    
39 by Gustav Hartvigsson
* Fixed a runtime error
231
    return ret_data;
38 by Gustav Hartvigsson
* Can now load settings from file.
232
  },
233
  
234
  /**
235
   * Commits changes to the settings object to file.
236
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
237
  commit_to_file: function () {
42 by Gustav Hartvigsson
* Finnished the commit to file method. Still not tested!
238
    print (JSON.stringify (this._settings, null, 2).toString () );
239
      
240
      let file_stream = this._settings_file.replace (null,
241
                                                    false,
242
                                                    Gio.FileCreateFlags.PRIVATE,
243
                                                    null);
244
      file_stream.write_all (JSON.stringify (
245
          this._settings, null, 2).toString (), null);
246
      
247
      file_stream.close (null);
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
248
  }
34 by Gustav Hartvigsson
* added GetText stuffs...
249
  
42 by Gustav Hartvigsson
* Finnished the commit to file method. Still not tested!
250
  // End of class.
34 by Gustav Hartvigsson
* added GetText stuffs...
251
});
252
39 by Gustav Hartvigsson
* Fixed a runtime error
253
254