/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-06-08 17:08:16 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140608170816-ugb3d4x26gm7chwc
* Main window no on par with the C version

Show diffs side-by-side

added added

removed removed

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