-
Notifications
You must be signed in to change notification settings - Fork 14
/
filewatcher.js
174 lines (153 loc) · 5.49 KB
/
filewatcher.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const config_filename = GLib.get_user_config_dir() + '/syncthing/config.xml';
const configfile = Gio.File.new_for_path(config_filename);
function getCurrentDir() {
let stack = (new Error()).stack;
let stackLine = stack.split('\n')[1];
if (!stackLine)
throw new Error("Could not find current file.");
let match = new RegExp('@(.+):\\d+').exec(stackLine);
if (!match)
throw new Error("Could not find current file.");
let path = match[1];
let file = Gio.File.new_for_path(path);
return file.get_parent();
}
imports.searchPath.unshift(getCurrentDir().get_path());
const Sax = imports.sax;
const ConfigParser = new Lang.Class({
Name: 'ConfigParser',
_init: function() {
this.state = 'root';
this.config = {};
this._parser = Sax.sax.parser(true);
this._parser.onerror = Lang.bind(this, this._onError);
this._parser.onopentag = Lang.bind(this, this._onOpenTag);
this._parser.onclosetag = Lang.bind(this, this._onCloseTag);
this._parser.ontext = Lang.bind(this, this._onText);
},
run_sync: function(callback) {
try {
let success, data, tag;
[success, data, tag] = configfile.load_contents(null);
this._parser.write(data);
} catch (e) {
log("Failed to read " + config_filename + ": " + e.message);
}
// calculate the correct URI from variables 'tls' and 'address'
this.config['uri'] = this._getURI(this.config);
callback(this.config);
},
_getURI: function(config) {
let address = config['address'];
let tls = config['tls'];
if (address) {
if (tls)
return "https://" + address;
else
return "http://" + address;
}
return null;
},
_onError: function(error) {
log("Error parsing " + this.filename + ": " + error);
this.config = null;
// We should abort the parsing process here.
},
_onText: function(text) {
if (this.state === 'address') {
this.config['address'] = text;
}
if (this.state === 'apikey') {
this.config['apikey'] = text;
}
},
_onOpenTag: function(tag) {
if (this.state === 'root' && tag.name === 'gui') {
this.state = 'gui';
this.config['tls'] = (tag.attributes['tls'].toUpperCase() == "TRUE");
}
if (this.state === 'gui') {
if (tag.name === 'address')
this.state = 'address';
else if (tag.name === 'apikey')
this.state = 'apikey';
}
},
_onCloseTag: function(name) {
if (this.state === 'gui' && name === 'gui') {
this.state = 'end';
}
if (this.state === 'address' && name === 'address') {
this.state = 'gui';
}
if (this.state === 'apikey' && name === 'apikey') {
this.state = 'gui';
}
},
});
var ConfigFileWatcher = new Lang.Class({
Name: 'ConfigFileWatcher',
/* File Watcher with 4 internal states:
ready -> warmup -> running -> cooldown
^ |
--------------------------------
*/
// Stop warmup after 1 second, cooldown after 10 seconds.
WARMUP_TIME: 1,
COOLDOWN_TIME: 10,
_init: function(callback) {
this.callback = callback;
this.running_state = 'ready';
this.run_scheduled = false;
this.monitor = configfile.monitor_file(Gio.FileMonitorFlags.NONE, null);
this.monitor.connect('changed', Lang.bind(this, this._configfileChanged));
this._configfileChanged();
},
_configfileChanged: function(monitor, file, other_file, event_type) {
if (this.running_state === 'ready') {
this.running_state = 'warmup';
this._source = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT_IDLE, this.WARMUP_TIME, Lang.bind(this, this._nextState));
} else if (this.running_state === 'warmup') {
// Nothing to do here.
} else if (this.running_state === 'running') {
this.run_scheduled = true;
} else if (this.running_state === 'cooldown') {
this.run_scheduled = true;
}
},
_run: function() {
let configParser = new ConfigParser();
configParser.run_sync(Lang.bind(this, this._onRunFinished));
},
_onRunFinished: function(result) {
this.running_state = 'cooldown';
this._source = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT_IDLE, this.COOLDOWN_TIME, Lang.bind(this, this._nextState));
if (result != this.config) {
this.config = result;
this.callback(this.config);
}
},
_nextState: function() {
this._source = null;
if (this.running_state === 'warmup') {
this.running_state = 'running';
this.run_scheduled = false;
this._run();
} else {
// this.running_state === 'cooldown'
this.running_state = 'ready';
if (this.run_scheduled) {
this.running_state = 'warmup';
this._source = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT_IDLE, WARMUP_TIME, Lang.bind(this, this._nextState));
}
}
return GLib.SOURCE_REMOVE;
},
destroy: function() {
if (this._source)
GLib.Source.remove(this._source);
},
});