-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
316 lines (269 loc) · 7.16 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Modules to control application life and create native browser window
require = require("esm")(module);
const { app, BrowserWindow, ipcMain, shell, globalShortcut } = require("electron");
const path = require("path");
var fs = require("fs");
http = require("http");
var os = require('os');
let spawn = require("child_process").spawn;
var mainWindow;
const appData = app.getPath('appData')
// These are stored in memory for faster loading
var years = []
var yearlySummarys = []
var os = os.type()
switch (os) {
case "Linux":
var dbPath = path.join(path.dirname(__dirname), 'memex', 'dataserver' ,'dataserver-Linux')
break;
case "Darwin":
var dbPath = path.join(path.dirname(__dirname), 'memex', 'dataserver' ,'dataserver-Darwin')
break;
case "Windows_NT":
var dbPath = path.join(path.dirname(__dirname), 'memex', 'dataserver' ,'dataserver-Windows')
break;
default:
}
console.log(dbPath);
let server = spawn(dbPath, ['-db', path.join(appData, 'takeout.db')]);
server.stdout.on("data", data => {
// Handle data...
console.log(data.toString());
});
server.stderr.on("data", err => {
// Handle error...
console.log(err.toString());
});
server.on("exit", code => {
// Handle exit
console.log(code.toString());
});
// IPC *************
ipcMain.on("getTimeline", (event, arg) => {
dataset = constructTimeline(arg);
event.reply("getTimeline-reply", dataset);
});
ipcMain.on("getCount", (event, arg) => {
var message = {
type: "getCount",
payload: arg
};
messageStr = JSON.stringify(message);
req = createRequest(event, "getCount-reply", arg);
req.write(messageStr);
req.end();
});
ipcMain.on("getYears", (event, arg) => {
//First check if the object is cached, if not ask the dataserver for it
var cached = false
if (years.length !== 0) {
event.reply("getYears-reply", years, "");
cached = true
}
// Not in cache so ask dataserver
if (!cached) {
var message = {
type: "getYears"
};
messageStr = JSON.stringify(message);
req = createRequest(event, "getYears-reply", "");
req.write(messageStr);
req.end();
}
});
ipcMain.on("getYearlySummary", (event, arg) => {
//First check if the object is cached, if not ask the dataserver for it
var cached = false
yearlySummarys.forEach(yearSum => {
if (arg === "Total") {
//Check for yearly
if (typeof yearSum.yearly !== 'undefined') {
event.reply("getYearlySummary-reply", yearSum, "");
cached = true
}
} else {
if (yearSum.year === arg) {
event.reply("getYearlySummary-reply", yearSum, "");
cached = true
}
}
});
// Not in cache so ask dataserver
if (!cached) {
var message = {
type: "getYearlySummary",
payload: arg.toString()
};
messageStr = JSON.stringify(message);
req = createRequest(event, "getYearlySummary-reply", "");
req.write(messageStr);
req.end();
}
});
ipcMain.on("importTakeout", (event, arg) => {
var message = {
type: "importTakeout",
payload: arg
};
messageStr = JSON.stringify(message);
req = createRequest(event, "importTakeout-reply", "");
req.write(messageStr);
req.end();
});
ipcMain.on("searchItems", (event, arg) => {
var message = {
type: "searchItems",
payload: arg
};
console.log("Searching for " + arg);
messageStr = JSON.stringify(message);
req = createRequest(event, "searchItems-reply", "");
req.write(messageStr);
req.end();
});
ipcMain.on("openHome", (event, arg) => {
cacheYears()
mainWindow.loadFile(path.join(__dirname, "src", "home.html"));
});
ipcMain.on("openLink", (event, arg) => {
shell.openExternal(arg);
});
// *************
function cacheYears() {
var message = {
type: "getYears"
};
messageStr = JSON.stringify(message);
console.log("Caching years");
req = createRequestToLoad("years");
req.write(messageStr);
req.end();
console.log("Finished caching years");
}
function cacheSummaries() {
console.log("Caching Summaries");
years.forEach(year => {
var message = {
type: "getYearlySummary",
payload: year
};
messageStr = JSON.stringify(message);
req = createRequestToLoad("summary");
req.write(messageStr);
req.end();
});
var message = {
type: "getYearlySummary",
payload: "Total"
};
messageStr = JSON.stringify(message);
req = createRequestToLoad("summary");
req.write(messageStr);
req.end();
console.log("Finished caching Summaries");
}
function createRequest(event, destination, type) {
var options = {
hostname: "localhost",
port: 40855,
path: "/",
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
var totalBody = "";
var req = http.request(options, function(res) {
res.setEncoding("utf8");
res.on("data", function(body) {
totalBody = totalBody + body;
});
res.on("end", () => {
message = JSON.parse(totalBody);
payload = JSON.parse(message.payload);
event.reply(destination, payload, type);
});
});
req.on("error", function(e) {
console.log("problem with request: " + e.message);
});
return req;
}
function createRequestToLoad(type) {
var options = {
hostname: "localhost",
port: 40855,
path: "/",
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
var totalBody = "";
var req = http.request(options, function(res) {
res.setEncoding("utf8");
res.on("data", function(body) {
totalBody = totalBody + body;
});
res.on("end", () => {
message = JSON.parse(totalBody);
payload = JSON.parse(message.payload);
if (type === 'years') {
//Set global var
years = payload;
cacheSummaries()
} else if (type === 'summary') {
yearlySummarys.push(payload)
}
});
});
req.on("error", function(e) {
console.log("problem with request: " + e.message);
});
return req;
}
function constructTimeline(searches) {
var dataset = [];
for (var i = 0; i < searches.length; i++) {
date = Date.parse(searches[i].date);
if (!isNaN(date)) {
if (searches[i].item.length < 50) {
// dataset.push({ start: date, content: searches[i].item });
var content = `
<p>${searches[i].item}</p>
`;
dataset.push({ start: date, content: content });
}
}
}
return dataset;
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
// fullscreen: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
nodeIntegration: true
}
});
// mainWindow.setMenu(null);
if (fs.existsSync(path.join(appData, 'takeout.db'))) {
cacheYears()
mainWindow.loadFile(path.join(__dirname, "src", "home.html"));
} else {
mainWindow.loadFile(path.join(__dirname, "src", "loadTakeout.html"));
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow);
// Quit when all windows are closed.
app.on("window-all-closed", function() {
server.kill(0)
});
app.on("activate", function() {
});