/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-09 10:35:45 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140609103545-6p42i7pegn48tps3
* Can now load settings from file.
* added a few comments
* added a skoleton for a few functions.

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
 
 
13
let _default_settings_object = null;
 
14
 
 
15
/**
 
16
 * Please use this function when getting the settings data object. It is not
 
17
 * good to 
 
18
 */
 
19
function get_settings () {
 
20
  if (!_default_settings_object) {
 
21
    _default_settings_object = new SettingsData ();
 
22
  }
 
23
  return _default_settings_object;
 
24
}
 
25
/**
 
26
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
 
27
 */
 
28
const SettingsData = new Lang.Class ({
 
29
  Name: 'SettingsData',
 
30
  
 
31
  /* Member definitions */
 
32
  _settings_file_path: String,
 
33
  _settings: Array,
 
34
  _settings_file: Gio.File,
 
35
  
 
36
  _init: function () {
 
37
    /* First we construct the path for where to store the settings file. */
 
38
    this._settings_file_path = "";
 
39
    this._settings_file_path += GLib.get_user_config_dir ();
 
40
    this._settings_file_path += "/gpump/gpump.json";
 
41
    print ("Config file is: " + this._settings_file_path);
 
42
    
 
43
    this._settings_file = Gio.File.new_for_path (this._settings_file_path);
 
44
    
 
45
    /* Then we check that the file exists. If the file doen not ekist we
 
46
     * construct some sane default values.
 
47
     */
 
48
    if (!GLib.file_test (this._settings_file_path, GLib.FileTest.EXISTS)) {
 
49
      /* -1 is undefined, and will be set at construction of the window */
 
50
      this._settings = {
 
51
        ui: {
 
52
          x: -1,
 
53
          y: -1,
 
54
          h: 500,
 
55
          w: 500
 
56
        },
 
57
        main: {
 
58
          privacy: {
 
59
            show_full_name: true,
 
60
            only_show_avatar: false
 
61
          },
 
62
          use_dark: false,
 
63
          first_run: true
 
64
        }
 
65
      };
 
66
      // DEBUG:
 
67
      print (JSON.stringify (this._settings, null, 2).toString () );
 
68
      
 
69
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
 
70
                                                    null);
 
71
      file_stream.write_all (JSON.stringify (
 
72
          this._settings, null, 2).toString (), null);
 
73
      
 
74
      file_stream.close (null);
 
75
      
 
76
    } else {
 
77
      /* The file exists, we load the settings data into memory */
 
78
      let file_stream = this._settings_file.read (null);
 
79
      
 
80
      /* See: http://stackoverflow.com/a/21146281
 
81
       */
 
82
      let file_info = this._settings_file.query_info("standard::size",
 
83
                                             Gio.FileQueryInfoFlags.NONE, null);
 
84
      let size = file_info.get_size ();
 
85
      
 
86
      let buffer = file_stream.read_bytes (size, null).get_data ();
 
87
      
 
88
      this._settings = JSON.parse (buffer);
 
89
      
 
90
      //DEBUG:
 
91
      print (JSON.stringify (this._settings, null, 2).toString () );
 
92
      
 
93
    }
 
94
  },
 
95
  
 
96
  /**
 
97
   * Sets a value in the setting object.
 
98
   */
 
99
  set_setting = function (setting: String, value) {
 
100
    
 
101
  },
 
102
  
 
103
  /**
 
104
   * Gets a value from the settings object.
 
105
   */
 
106
  get_setting = function (setting: String) {
 
107
    
 
108
  },
 
109
  
 
110
  /**
 
111
   * Commits changes to the settings object to file.
 
112
   */
 
113
  commit_to_file = function () {
 
114
    
 
115
  }
 
116
  
 
117
});
 
118