/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This file is part of GPump, a Pump.io client.
 *
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
 * in the file lgpl-3.0.txt in the root of this project.
 */

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;

const Lang = imports.lang;

let _default_settings_object = null;

/**
 * Please use this function when getting the settings data object. It is not
 * good to 
 */
function get_settings () {
  if (!_default_settings_object) {
    _default_settings_object = new SettingsData ();
  }
  return _default_settings_object;
}
/**
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
 */
const SettingsData = new Lang.Class ({
  Name: 'SettingsData',
  
  /* Member definitions */
  _settings_file_path: String,
  _settings: Array,
  _settings_file: Gio.File,
  
  _init: function () {
    /* First we construct the path for where to store the settings file. */
    this._settings_file_path = "";
    this._settings_file_path += GLib.get_user_config_dir ();
    this._settings_file_path += "/gpump/gpump.json";
    print ("Config file is: " + this._settings_file_path);
    
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
    
    /* Then we check that the file exists. If the file doen not ekist we
     * construct some sane default values.
     */
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
      /* -1 is undefined, and will be set at construction of the window */
      this._settings = {
        ui: {
          x: -1,
          y: -1,
          h: 500,
          w: 500
        },
        main: {
          privacy: {
            show_full_name: true,
            only_show_avatar: false
          },
          use_dark: false,
          first_run: true
        }
      };
      // DEBUG:
      print (JSON.stringify (this._settings, null, 2).toString () );
      
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
                                                    null);
      file_stream.write_all (JSON.stringify (
          this._settings, null, 2).toString (), null);
      
      file_stream.close (null);
      
    } else {
      /* The file exists, we load the settings data into memory */
      let file_stream = this._settings_file.read (null);
      
      /* See: http://stackoverflow.com/a/21146281
       */
      let file_info = this._settings_file.query_info("standard::size",
                                             Gio.FileQueryInfoFlags.NONE, null);
      let size = file_info.get_size ();
      
      let buffer = file_stream.read_bytes (size, null).get_data ();
      
      this._settings = JSON.parse (buffer);
      
      //DEBUG:
      print (JSON.stringify (this._settings, null, 2).toString () );
      
    }
  },
  
  /**
   * Sets a value in the setting object.
   */
  set_setting = function (setting: String, value) {
    
  },
  
  /**
   * Gets a value from the settings object.
   */
  get_setting = function (setting: String) {
    
  },
  
  /**
   * Commits changes to the settings object to file.
   */
  commit_to_file = function () {
    
  }
  
});