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