/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:
7
7
 
8
8
const Gio = imports.gi.Gio;
9
9
const GLib = imports.gi.GLib;
 
10
 
10
11
const Lang = imports.lang;
11
12
 
12
 
 
13
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
 */
14
19
function get_settings () {
15
20
  if (!_default_settings_object) {
16
21
    _default_settings_object = new SettingsData ();
17
22
  }
18
23
  return _default_settings_object;
19
24
}
20
 
 
 
25
/**
 
26
 * NO NOT CREATE INSTANCES FROM THIS OBJECT, USE THE get_settings () FUNCTION!
 
27
 */
21
28
const SettingsData = new Lang.Class ({
22
29
  Name: 'SettingsData',
23
30
  
59
66
      // DEBUG:
60
67
      print (JSON.stringify (this._settings, null, 2).toString () );
61
68
      
62
 
      
63
69
      let file_stream = this._settings_file.create (Gio.FileCreateFlags.PRIVATE,
64
70
                                                    null);
65
71
      file_stream.write_all (JSON.stringify (
69
75
      
70
76
    } else {
71
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
      
72
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
    
73
115
  }
74
116
  
75
117
});