forked from z-------------/CPod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
411 lines (352 loc) · 11 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
const { app, dialog, shell, globalShortcut, ipcMain, BrowserWindow, Menu, Tray } = require("electron")
const path = require("path")
const url = require("url")
const autoUpdater = require("electron-updater").autoUpdater
const fs = require("fs")
const i18n = require("./lib/i18n.js")
const request = require("request")
let win, tray, menu // keep globals
const package = require("./package.json");
const configDirName = "cumulonimbus"
const configDirPath = path.join( path.parse(app.getPath("userData")).dir, configDirName )
try {
fs.mkdirSync(configDirPath)
} catch (exp) {
console.log("Looks like config dir already exists, continuing")
}
app.setPath("userData", configDirPath)
const APP_NAME = "CPod" // only used in this file
const USERDATA_DIR = app.getPath("userData")
const WINDOW_SIZE_FILE = path.join(USERDATA_DIR, "window_size")
const SETTINGS_FILE = path.join(USERDATA_DIR, "user_settings.json")
const DEFAULT_SETTINGS_FILE = path.join(app.getAppPath(), "public", "default_settings.json")
const ICON_WIN = path.join(__dirname, "build/icon.ico")
const ICON_MAC = path.join(__dirname, "build/icons/16x16.png")
const ICON_OTHER = path.join(__dirname, "build/icon.png")
const USER_AGENT = `CPod/${package.version} (github.com/z-------------)`;
let settings;
let size = {}
var beforeQuit = false
/* get settings and watch for changes */
console.log(`Using default settings file: ${DEFAULT_SETTINGS_FILE}`)
console.log(`Using user settings file: ${SETTINGS_FILE}`)
function updateSettings(json) {
for (let key in json) {
if (settings[key] != json[key]) console.log(`Updating setting in main: ${key}: ${JSON.stringify(settings[key])} --> ${JSON.stringify(json[key])}`);
settings[key] = json[key]
}
}
// load default settings
settings = JSON.parse(fs.readFileSync(DEFAULT_SETTINGS_FILE, "utf-8"));
console.log("Loaded default settings.");
// load user settings
try {
updateSettings(JSON.parse(fs.readFileSync(SETTINGS_FILE)));
} catch (exp) {
console.log("Could not load user settings.");
}
ipcMain.on("settingChanged", (e, message) => {
let obj = {};
obj[message.key] = message.value;
updateSettings(obj);
if (message.key === "taskbarShow") {
if (message.value === true) {
showTray();
} else if (message.value === false) {
hideTray();
}
}
});
/* create and handle window */
let windowOptions = {
title: APP_NAME
}
function mergeObjects() {
let finalObj = {}
for (let obj of arguments) {
for (let key in obj) {
finalObj[key] = obj[key]
}
}
return finalObj
}
function getIconPath() {
if (process.platform === "win32") {
return ICON_WIN;
} else if (process.platform === "darwin") {
return ICON_MAC;
} else {
return ICON_OTHER;
}
}
function trayUpdateNowPlaying(menuTemplate, arg) {
tray.setToolTip(
`${APP_NAME} - ${i18n.__("label_now_playing")}
${i18n.__("punc_quote_open")}${arg.episodeTitle}${i18n.__("punc_quote_close")}
${arg.podcastTitle}`)
menuTemplate[0].label = `${i18n.__("label_now_playing")}${i18n.__("punc_quote_open")}${arg.episodeTitle}${i18n.__("punc_quote_close")} - ${arg.podcastTitle}`
menuTemplate[0].visible = true
menu = Menu.buildFromTemplate(menuTemplate)
tray.setContextMenu(menu)
}
function showTray() {
if (tray && !tray.isDestroyed()) return;
tray = new Tray(getIconPath())
let menuTemplate = [{
id: "now_playing",
enabled: false,
visible: false
}, {
label: i18n.__("button_playback_playpause"),
click: function() {
win.webContents.send("playbackControl", "playpause")
}
}, {
label: i18n.__("button_playback-next"),
click: function() {
win.webContents.send("playbackControl", "next")
}
}, {
type: "separator"
}, {
id: "show_window",
label: i18n.__("label_show_window"),
click: showWindow
}, {
label: i18n.__("label_quit"),
click: app.quit
}]
menu = Menu.buildFromTemplate(menuTemplate)
tray.setToolTip(APP_NAME)
tray.setContextMenu(menu)
tray.on("click", showWindow)
ipcMain.on("nowPlayingInfo", (e, arg) => {
trayUpdateNowPlaying(menuTemplate, arg)
})
win.webContents.send("getNowPlayingInfo");
}
function hideTray() {
if (!tray || tray.isDestroyed()) return;
tray.destroy();
}
function writeWindowSize(win, cb) {
let dimens = win.getSize()
size.width = dimens[0]
size.height = dimens[1]
size.maximized = win.isMaximized()
fs.writeFile(WINDOW_SIZE_FILE, JSON.stringify(size) + "\n", {
encoding: "utf8"
}, (err) => {
cb(err)
})
}
function sizeWindow() {
win.setSize(size.width, size.height, false)
if (size.maximized) win.maximize()
}
function showWindow() {
if (win.isMinimized()) win.restore()
win.show()
sizeWindow()
win.focus()
}
function createWindow(width, height, maximize) {
windowOptions.icon = getIconPath()
if (maximize) {
win = new BrowserWindow(windowOptions)
win.maximize()
} else if (width && height) {
win = new BrowserWindow(mergeObjects(windowOptions, {
width: width,
height: height
}))
}
win.webContents.setUserAgent(USER_AGENT);
win.loadURL(url.format({
pathname: path.join(__dirname, "public/app/index.html"),
protocol: "file:",
slashes: true
}))
if (settings.taskbarShow) showTray();
win.on("close", e => {
if (settings.taskbarShow && settings.taskbarClose && !beforeQuit) {
e.preventDefault()
win.hide()
}
writeWindowSize(win, err => {
if (err) {
console.log("Error writing to window size file")
}
if (!(settings.taskbarShow && settings.taskbarClose)) {
win = null
}
})
})
win.on("minimize", e => {
if (settings.taskbarShow && settings.taskbarMinimize && !beforeQuit) {
e.preventDefault()
win.hide()
}
writeWindowSize(win, err => {
if (err) {
console.log("Error writing to window size file")
}
})
})
}
const isSecondInstance = app.makeSingleInstance((argv, wd) => {
if (win) showWindow()
})
if (isSecondInstance) app.quit();
app.on("ready", function() {
fs.readFile(WINDOW_SIZE_FILE, {
encoding: "utf8"
}, (err, data) => {
if (err) {
console.log("Error reading window size file")
createWindow(null, null, true)
} else {
try {
data = JSON.parse(data)
size.width = data.width
size.height = data.height
size.maximized = data.maximized
createWindow(size.width, size.height, size.maximized)
} catch (e) {
console.log("Invalid window size file")
createWindow(null, null, true)
}
}
})
if (settings.globalMediaKeysEnable) {
for (let accelerator of ["mediaplaypause", "medianexttrack", "mediaprevioustrack"]) {
globalShortcut.register(accelerator, function() {
win.webContents.send("globalShortcut", accelerator)
})
}
}
})
app.on("before-quit", () => {
beforeQuit = true
tray.destroy()
})
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})
app.on("activate", () => {
if (win === null) {
createWindow()
}
})
/* auto-update */
if (settings.autoUpdaterAllowPrerelease) {
autoUpdater.allowPrerelease = true
}
// autoUpdater.fullChangelog = true
autoUpdater.autoDownload = false
var currentVersion
autoUpdater.checkForUpdates()
.then((updateCheckResult) => {
let info = updateCheckResult.updateInfo
console.log(info, info.releaseNotes)
var isNewer = false;
currentVersion = package.version
let cvs = currentVersion.split(".").map(s => Number(s))
let uvs = info.version.split(".").map(s => Number(s))
if (
uvs[0] > cvs[0] ||
uvs[0] === cvs[0] && uvs[1] > cvs[1] ||
uvs[0] === cvs[0] && uvs[1] === cvs[1] && uvs[2] > cvs[2]
) {
isNewer = true;
}
if (isNewer) {
fs.readFile(path.join(app.getPath("userData"), "skip_version"), {
encoding: "utf8"
}, (err, data) => {
if (err) {
console.log("Error reading skip_version file")
data = "";
}
if (info.version !== data.trim()) {
request({
url: "https://api.github.com/repos/z-------------/cumulonimbus/releases/latest",
headers: { "User-Agent": USER_AGENT }
}, (err, response, body) => {
try {
var isPrerelease = true;
if (JSON.parse(body).tag_name.substring(1) === info.version) {
isPrerelease = false;
}
let messageBoxOptions = {
type: "question",
buttons: [
i18n.__("dialog_update-available_button_download"),
i18n.__("dialog_update-available_button_skip")
],
defaultId: 0,
cancelId: -1,
title: i18n.__("dialog_update-available_title"),
message: i18n.__(
"dialog_update-available_body-" + (isPrerelease ? "prerelease" : "stable"),
info.version, currentVersion
)
}
dialog.showMessageBox(win, messageBoxOptions, (responseIndex) => {
if (responseIndex === messageBoxOptions.defaultId) {
autoUpdater.downloadUpdate(updateCheckResult.cancellationToken)
} else if (responseIndex !== -1) {
fs.writeFile(
path.join(app.getPath("userData"), "skip_version"),
info.version.toString(),
(err) => {
if (err) {
console.log("Error writing to skip_version file")
} else {
console.log("Wrote to skip_version file")
}
}
)
}
})
} catch (e) {
console.log("Exception in update check", e)
}
})
} else {
console.log("Version " + info.version + " is skipped")
}
})
} else {
console.log("Update is not newer than current version, ignoring")
}
})
.catch(e => {
console.error("Auto-update error:")
console.error(e)
})
autoUpdater.on("update-downloaded", (info) => {
console.log(info)
let messageBoxOptions = {
type: "question",
buttons: [
i18n.__("dialog_update-downloaded_button_install"),
i18n.__("dialog_update-downloaded_button_cancel")
],
defaultId: 0,
cancelId: 1,
title: i18n.__("dialog_update-downloaded_title"),
message: i18n.__("dialog_update-downloaded_body", info.version, currentVersion)
}
dialog.showMessageBox(win, messageBoxOptions, (responseIndex) => {
if (responseIndex === messageBoxOptions.defaultId) {
autoUpdater.quitAndInstall()
}
})
})
/* chromium flags */
if (!settings.smoothScrolling) {
app.commandLine.appendSwitch("disable-smooth-scrolling")
}