-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
302 lines (277 loc) · 8.42 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
const { autoUpdater } = require("electron-updater");
const { SSL_OP_EPHEMERAL_RSA } = require("constants");
const { app, BrowserWindow, ipcMain, protocol, ipcRenderer } = require("electron");
const { watchFile } = require("fs");
const { trackEvent } = require("./lib/analytics.js");
const {TitlebarRespect} = require('electron-titlebar-respect')
const glasstron = require('glasstron-clarity');
const electron = require("electron");
const isOnline = require('is-online');
const Store = require("electron-store");
const store = new Store();
const axios = require('axios');
token = store.get('token')
store.set('createonlogin', false)
// Notify
const { Notification } = require("electron");
electron.app.commandLine.appendSwitch("enable-transparent-visuals");
TitlebarRespect({})
// wait function
function wait(ms) {
var d = new Date();
var d2 = null;
do {
d2 = new Date();
} while (d2 - d < ms);
}
// Start the libaries
require("./lib/rpc.js");
console.log("RPC lib init.");
// Loading screen
/// create a global var, wich will keep a reference to out loadingScreen window
let loadingScreen;
const createLoadingScreen = () => {
loadingScreen = new BrowserWindow(
Object.assign({
width: 700,
height: 120,
alwaysOnTop: true,
frame: false,
fullscreen: false,
show: true,
transparent: true,
webPreferences: {
sandbox: true,
nodeIntergration: false,
contextIsolation: true
}
})
);
loadingScreen.setResizable(false);
loadingScreen.loadFile("./src/pages/splash.html");
loadingScreen.on("closed", () => (loadingScreen = null));
loadingScreen.webContents.on("did-finish-load", () => {
loadingScreen.show();
});
};
console.log("Loading screen ready.");
// Start the main program
let mainWindow;
function createWindow() {
mainWindow = new glasstron.BrowserWindow({
width: 800,
height: 600,
show: false,
frame: global.frame,
titleBarStyle: global.titleBarStyle,
fullscreen: false,
blur: true,
blurType: global.blurType,
modal: true,
icon: "snailfm.ico",
trafficLightPosition: { x: 24, y: 25 },
webPreferences: {
nodeIntegration: true,
nativeWindowOpen: true,
contextIsolation: false,
},
});
mainWindow.setMenuBarVisibility(false);
mainWindow.setResizable(false);
mainWindow.loadFile("index.html");
mainWindow.on("maximize", () => mainWindow.unmaximize());
ipcMain.on("main_show", () => mainWindow.show());
ipcMain.on('main_hide', () => {mainWindow.hide();})
ipcMain.on("load_acc", () => mainWindow.loadFile('myaccount.html'))
mainWindow.webContents.on("did-finish-load", () => {
if (loadingScreen) {
loadingScreen.close();
trackEvent("stnailfm.started", "SnailFM has just loaded up!");
}
var isDev = require("isdev");
if (isDev) {
console.log("In Development!");
} else {
console.log("Not in Development!");
}
(async () => {
if ((await isOnline() === false)) {
console.log("No internet");
const notification3 = {
title: "SnailFM",
body: "No valid network connection! Please reconnect!",
};
mainWindow.loadFile("./src/pages/nonet.html");
new Notification(notification3).show();
}
})();
mainWindow.show();
console.log("Ok! Window init, let's check for updates...");
autoUpdater.checkForUpdatesAndNotify();
console.log("Update checked. Let's see what happens!");
});
mainWindow.on("closed", function () {
ipcMain.emit('login_exit');
});
}
console.log("Main screen ready.");
console.log("Setup login function...");
var loginclose = 0
function createloginWindow() {
const loginWindow = (new BrowserWindow({
width: 1200,
height: 700,
minWidth: 800,
minHeight: 400,
center: true,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
}
}));
loginWindow.hide()
loginWindow.on('close', function (event) {
event.preventDefault();
const notification4 = {
title: "SnailFM",
body: "This window won't close unless you login! If you want to exit SnailFM, press the close button 3 times!",
};
loginclose = loginclose + 1;
if (loginclose === 3) {
store.set('logged_in', false)
console.log("App quitting...")
ipcMain.emit('appquit')
} else {
if (loginclose === 1) {
new Notification(notification4).show();
}
}
});
loginWindow.setMenuBarVisibility(false)
ipcMain.on('login_sar', () => {loginWindow.show(); loginWindow.loadURL("https://login.snaildos.com?redir=snailfm://login"); loginWindow.maximize(); ipcMain.emit('main_hide')})
ipcMain.on('login_show', () => {loginWindow.show(); loginWindow.maximize(); loginWindow.restore()})
ipcMain.on('login_hide', () => {loginWindow.hide();})
ipcMain.on('login_exit', () => {loginWindow.destroy();})
loginWindow.loadURL("https://login.snaildos.com?redir=snailfm://login");
}
app.whenReady().then(() => {
ipcMain.on('token_verify', function() {
if ((store.get('logged_in') === false)) {
console.log("All good! Login is disabled...");
createWindow()
} else {
if (token != null) {
console.log("Verifying Identity..")
async function verify() {
await axios.get('https://api.snaildos.com/users/@me', {
headers: {
Authorization: token
}}
)
.then(function (response) {
// handle success
if (( response.data.status ) === "false") {
config.set('token', null)
console.log("Token failure!")
ipcMain.emit("login_sar")
loadingScreen.close();
store.set('createonlogin', true)
} else {
console.log("Sucess!")
createWindow();
}
})
.catch(function (error) {
// handle error
console.log(error);
ipcMain.emit("login_sar")
console.log("Token failure!")
loadingScreen.close();
store.set('createonlogin', true)
})
.then(function () {
});
}
verify()
} else {
if ((store.get('logged_in') === false)) {
console.log("All good! Login is disabled...");
createWindow()
} else {
if ((store.get('setup') != undefined)) {
console.log("User has login enabled but is not logged in, showing login screen...");
ipcMain.emit("login_sar")
console.log("Token failure!")
loadingScreen.close();
} else {
createWindow()
}
}
}
}
})
app.setAsDefaultProtocolClient("snailfm");
protocol.registerHttpProtocol('snailfm', (req, cb) => {
const url = req.url.substr(22)
console.log("Logging in: "+url);
console.log("Token data: "+url)
store.set('token', url);
ipcMain.emit('load_acc');
ipcMain.emit('login_hide')
ipcMain.emit('main_show')
store.set('logged_in', true)
if ((store.get('createonlogin')) === true) {
createWindow()
store.set('createonlogin', false);
}
})
app.on("open-url", (event, url) => {
const url2 = req.url.substr(22)
console.log("Logging in: "+url2);
console.log("Token data: "+url2)
store.set('token', url2);
ipcMain.emit('load_acc');
ipcMain.emit('login_hide')
ipcMain.emit('main_show')
store.set('logged_in', true);
if ((store.get('createonlogin')) === true) {
createWindow()
store.set('createonlogin', false);
}
});
});
app.on("ready", () => {
createLoadingScreen();
console.log("Send check for updates signal...");
console.log("Alright, lets go!");
setTimeout(() => {
createloginWindow()
ipcMain.emit('token_verify');
}, 3000);
});
app.on("window-all-closed", function () {
if (process.platform !== "darwin") {
app.quit();
}
});
ipcMain.on("app_version", (event) => {
event.sender.send("app_version", { version: app.getVersion() });
});
autoUpdater.on("update-available", () => {
mainWindow.webContents.send("update_available");
});
autoUpdater.on("update-downloaded", () => {
mainWindow.webContents.send("update_downloaded");
});
ipcMain.on("relaunch", () => {
ipcMain.emit("close")
});
ipcMain.on("restart_app", () => {
autoUpdater.quitAndInstall();
});
ipcMain.on('appquit', () => {app.exit()})
ipcMain.on('minimize', () => {mainWindow.minimize()})
ipcMain.on('maximize', () => {mainWindow.maximize()})
ipcMain.on('restore', () => {mainWindow.restore()})
ipcMain.on('close', () => {mainWindow.close(); ipcMain.emit('login_exit');})