/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-02-08 17:53:39 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140208175339-yxn38uceduzzvswx
Initial code.

Non of the code is actualy functional.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
9
 
const GLib = imports.gi.GLib;
10
 
 
11
 
const Lang = imports.lang;
12
 
const System = imports.system;
13
 
 
14
 
let _default_settings_object = null;
15
 
 
16
 
/**
17
 
 * Please use this function when getting the settings data object. It is not
18
 
 * good to have multiple instances of the settings data object, it may
19
 
 * cause problems down the line.
20
 
 */
21
 
function get_settings () {
22
 
  if (!_default_settings_object) {
23
 
    _default_settings_object = new SettingsData ();
24
 
  }
25
 
  return _default_settings_object;
26
 
}
27
 
 
28
 
/**
29
 
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
30
 
 */
31
 
const SettingsData = new Lang.Class ({
32
 
  Name: 'SettingsData',
33
 
  
34
 
  /* Member definitions */
35
 
  _settings_file_path: String,
36
 
  _settings: Array,
37
 
  _settings_file: Gio.File,
38
 
  
39
 
  _init: function () {
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 */
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
 
      
96
 
    }
97
 
  },
98
 
  
99
 
  /**
100
 
   * Sets a value in the setting object.
101
 
   * 
102
 
   * return: false on fail
103
 
   * return: true when everything is OK.
104
 
   */
105
 
  set_setting: function (setting, value) {
106
 
    if (typeof setting != "string") {
107
 
      print ("ERROR: The \"setting\" parameter must be a string.");
108
 
      return false;
109
 
    }
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
 
    }
175
 
    
176
 
    return true;
177
 
  },
178
 
  
179
 
  /**
180
 
   * Gets a value from the settings object.
181
 
   *
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.
187
 
   */
188
 
  get_setting: function (setting) {
189
 
    let ret_data = {
190
 
      ok: true,
191
 
      data: undefined
192
 
    };
193
 
    
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
 
    
231
 
    return ret_data;
232
 
  },
233
 
  
234
 
  /**
235
 
   * Commits changes to the settings object to file.
236
 
   */
237
 
  commit_to_file: function () {
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);
248
 
  }
249
 
  
250
 
  // End of class.
251
 
});
252
 
 
253
 
 
254