/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
18
 * good to 
19
 */
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
20
function get_settings () {
21
  if (!_default_settings_object) {
22
    _default_settings_object = new SettingsData ();
23
  }
24
  return _default_settings_object;
25
}
38 by Gustav Hartvigsson
* Can now load settings from file.
26
/**
27
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
28
 */
34 by Gustav Hartvigsson
* added GetText stuffs...
29
const SettingsData = new Lang.Class ({
30
  Name: 'SettingsData',
31
  
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
32
  /* Member definitions */
33
  _settings_file_path: String,
34
  _settings: Array,
35
  _settings_file: Gio.File,
36
  
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
37
  _init: function () {
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
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 */
38 by Gustav Hartvigsson
* Can now load settings from file.
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
      
37 by Gustav Hartvigsson
* finnihed the construction of the settings file.
94
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
95
  },
96
  
97
  /**
98
   * Sets a value in the setting object.
39 by Gustav Hartvigsson
* Fixed a runtime error
99
   * 
100
   * return: false on fail
101
   * return: true when everything is OK.
38 by Gustav Hartvigsson
* Can now load settings from file.
102
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
103
  set_setting: function (setting, value) {
104
    if (typeof setting != "string") {
105
      print ("ERROR: The \"setting\" paramenter must be a string... Exiting.");
106
      return false;
107
    }
38 by Gustav Hartvigsson
* Can now load settings from file.
108
    
109
  },
110
  
111
  /**
112
   * Gets a value from the settings object.
39 by Gustav Hartvigsson
* Fixed a runtime error
113
   *
114
   * returns a complex object with two fealds: ok and data.
115
   *
116
   * If ok is false something went wrong and the data feald will be undefined.
117
   *
118
   * If ok is true everything is ok and the data feald will be set to the value.
38 by Gustav Hartvigsson
* Can now load settings from file.
119
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
120
  get_setting: function (setting) {
121
    let ret_data = {
122
      ok: true,
123
      data: undefined
124
    };
38 by Gustav Hartvigsson
* Can now load settings from file.
125
    
39 by Gustav Hartvigsson
* Fixed a runtime error
126
    return ret_data;
38 by Gustav Hartvigsson
* Can now load settings from file.
127
  },
128
  
129
  /**
130
   * Commits changes to the settings object to file.
131
   */
39 by Gustav Hartvigsson
* Fixed a runtime error
132
  commit_to_file: function () {
38 by Gustav Hartvigsson
* Can now load settings from file.
133
    
36 by Gustav Hartvigsson
* changed from this.connect(...) to vfunc_ .
134
  }
34 by Gustav Hartvigsson
* added GetText stuffs...
135
  
136
});
137
39 by Gustav Hartvigsson
* Fixed a runtime error
138
139