-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.js
214 lines (184 loc) · 5.28 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const electron = require('electron');
const {app} = electron;
const ipc = electron.ipcMain;
const {BrowserWindow} = electron;
const {globalShortcut} = electron;
const fs = require('fs');
const path = require('path');
const Channel = require('./src/channel');
const config = require('./config');
const serializeError = require('./src/utils').serializeError;
const mkdirp = require('mkdirp');
const SQL = require('sql.js');
// electron.crashReporter.start();
var Main = {
init : function () {
var self = this;
mkdirp.sync(config.cachePath);
mkdirp.sync(config.indexerPath);
mkdirp.sync(config.indexerBuiltinPath);
app.on('ready', self.ready.bind(self));
app.on('will-quit', self.willQuit.bind(self));
},
ready : function () {
var self = this;
self.main = new BrowserWindow({width: 1440, height: 900, titleBarStyle: 'hidden-inset'});
self.main.loadURL('file://' + __dirname + '/viewer.html');
if (config.DEBUG) self.main.openDevTools();
self.main.on('closed', function () {
self.main = null;
app.quit();
});
ipc.on('viewer', self.handleViewerIPC.bind(self));
self.openIndexerProcess();
},
willQuit : function () {
globalShortcut.unregisterAll();
},
openIndexerProcess : function () {
var self = this;
if (self.indexer) {
self.indexer.window.close();
}
self.indexer = new Channel({
ready : function () {
var self = this;
return new Promise(function (resolve, reject) {
self.window = new BrowserWindow({width: 800, height: 600, x: 0, y: 0, show: config.DEBUG });
self.window.loadURL('file://' + __dirname + '/indexer.html');
if (config.DEBUG) self.window.openDevTools();
self.window.webContents.on('did-finish-load', resolve);
});
},
recv : function (callback) {
ipc.on('indexer', function (e, args) {
// console.log('[main][chan=indexer] ipc.on', args);
callback(args);
});
},
send : function (args) {
this.window.send('indexer', args);
},
notification : function (args) {
self.main.webContents.send('viewer', args);
}
});
},
handleViewerIPC : function (e, args) {
var self = this;
console.log('[main][chan=viewer] ipc.on', args);
var func = self.IPCMethods[args.method];
var promise;
if (func) {
try {
promise = func.call(self, args.params);
} catch (e) {
promise = Promise.reject(e);
}
} else {
promise = Promise.reject("no such method");
}
promise.then(function (result) {
e.sender.send('viewer', {
id: args.id,
result: result
});
}, function (error) {
// console.log('Error', serializeError(error));
e.sender.send('viewer', {
id: args.id,
error: serializeError(error)
});
});
},
IPCMethods : {
echo : function (params) {
return this.indexer.request('echo', params);
},
settings: function (settings) {
var self = this;
console.log(settings);
if (config.DEBUG !== settings.developerMode) {
if (settings.developerMode) {
self.main.openDevTools();
self.indexer.window.openDevTools();
self.indexer.window.show();
} else {
self.main.closeDevTools();
self.indexer.window.closeDevTools();
self.indexer.window.hide();
}
config.DEBUG = settings.developerMode;
}
globalShortcut.unregisterAll();
if (settings.globalShortcut) {
var key = settings.globalShortcut.
replace(/Meta/, 'Super').
replace(/Key(.)/, '$1').
replace(/Digit(.)/, '$1').
replace(/Arrow([^ ]+)/, '$1').
replace(/ \+ /g, '+');
console.log('globalShortcut.register', key);
try {
var ret = globalShortcut.register(key, function() {
console.log('globalShortcut');
self.main.focus();
});
if (!ret) {
console.log('registration failed');
}
} catch (e) {
console.log(e);
}
}
return Promise.resolve();
},
getIndex : function (params) {
var self = this;
if (!params.docset) {
var filename = path.join(config.cachePath, params.id + '.dat');
return new Promise(function (resolve, reject) {
if (!params.reindex) {
fs.readFile(filename, 'utf8', function (err, data) {
if (err) return reject(err);
resolve(data);
});
} else {
return reject('force index');
}
}).
catch(function (error) {
console.log('REINDEX', error);
return self.indexer.request('createIndex', { id : params.id }).
then(function (data) {
console.log('INDEXED', data.length);
return new Promise(function (resolve, reject) {
console.log('WRITE INDEX TO', filename);
var meta = "\x01" + JSON.stringify({
created: new Date().getTime()
}) + "\n";
fs.writeFile(filename, meta + data, 'utf8', function (err) {
if (err) return reject(err);
resolve(data);
});
});
});
});
} else {
var docset = params.docset;
// var docroot = path.join(docset, 'Contents/Resources/Documents');
var dbfile = path.join(docset, 'Contents/Resources/docSet.dsidx');
var buf = require('fs').readFileSync(dbfile);
var db = new SQL.Database(buf);
var rows = db.exec('SELECT name, path FROM searchIndex')[0].values;
var ret = '';
for (var i = 0, it; (it = rows[i]); i++) {
console.log(it);
ret += it[0] + '\t' + it[1] + '\n';
}
return Promise.resolve(ret);
}
}
}
};
Main.init();