-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
360 lines (288 loc) · 8.99 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
const {app, BrowserWindow, ipcMain, Notification} = require('electron')
const {autoUpdater} = require("electron-updater");
var log = require('electron-log');
const path = require('path')
const url = require('url')
const shell = require('electron').shell
const mymac = require('getmac')
// var basepath = app.getAppPath();
var basepath = path.dirname (app.getPath ('exe'));
var AutoLaunch = require('auto-launch')
const isDev = require('electron-is-dev');
// this should be placed at top of main.js to handle setup events quickly
if (handleSquirrelEvent(app)) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
// IPC communication
// receives all messages from all processes, and can send to other processes
const ipc = require('electron').ipcMain
// Data Persistant
const Store = require('electron-store');
const store = new Store();
var ready_to_close = false;
store.set('ready_to_close', false);
store.delete('ready_to_close');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
//
// Detect which software to run, either server or the client software
//
var is_server = false;
mymac.getMac(function(err, macAddress){
if (err) throw err
log.info("Mac Address: " + macAddress);
store.set('my_mac_address', macAddress);
});
function getWidowDimensions() {
var screenElectron = require('electron').screen;
var mainScreen = screenElectron.getPrimaryDisplay();
var dimensions = mainScreen.size;
return dimensions;
}
// when update is being checked
autoUpdater.on('checking-for-update', (info) => {
log.info('checking for update');
win.webContents.on('did-finish-load', () => {
win.webContents.send('checkForUpdate', {});
})
});
// when update is available
autoUpdater.on('update-available', (info) => {
log.info('update is available');
win.webContents.send('updateAvailable');
});
// when update is not available
autoUpdater.on('update-not-available', (info) => {
log.info('no update is available');
if (win) {
win.webContents.send('noUpdateAvailable');
}
});
// download progress
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + Math.ceil(progressObj.percent) + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
log.info(log_message);
win.webContents.send('update_progress', log_message );
});
// when the update has been downloaded and is ready to be installed, notify the BrowserWindow
autoUpdater.on('update-downloaded', (info) => {
log.info('Update Downloaded');
win.webContents.send('updateDownloaded');
});
// // when receiving a quitAndInstall signal, quit and install the new version ;)
ipcMain.on("quitAndInstall", (event, arg) => {
autoUpdater.quitAndInstall();
});
function createWindow () {
log.info("New Main Window created...");
var dimen = getWidowDimensions();
// Create the browser window.
win = new BrowserWindow({
width: dimen.width,
height: dimen.height,
frame: false,
x:0,
y:0,
resizable: false,
movable: false,
minimizable: false,
fullscreen: true,
transparent: false,
backgroundColor: '#16191e',
icon: __dirname + '/logo.ico',
});
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'src/index.html'),
protocol: 'file:',
slashes: true
}));
// prevent closing of window
win.on('close', function (event) {
ready_to_close = store.get('ready_to_close');
if (!ready_to_close && isDev == false) {
event.preventDefault();
}
})
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
win = null
});
// keep our application always on top
set_alwaysonTop();
win.setFullScreen(true);
// add app to auto launch
start_auto_launch();
// check for update
// autoUpdater.checkForUpdates();
}
/**
* Page : for managing
*/
function createWindowManager() {
var dimen = getWidowDimensions();
var xpos = dimen.width/2 - (800/2) - 100;
// Create the browser window.
win = new BrowserWindow({
width: 1024,
height: 650,
frame: false,
x: xpos,
y: 50,
resizable: true,
movable: true,
minimizable: true,
fullscreen: false,
transparent: false,
backgroundColor: '#16191e',
icon: __dirname + '/logo.ico',
});
console.log('x:' + xpos);
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'src/manager.html'),
protocol: 'file:',
slashes: true
}));
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
win = null
});
}
// Add application to auto launch program
function start_auto_launch() {
var quarkclientAutoLauncher = new AutoLaunch({
name: 'QuarkClient',
// path: '/Applications/QuarkClient.app',
// path: '"C:\\Users\\C9G\\AppData\\Roaming\\uTorrent Web\\utweb.exe" /MINIMIZED',
path: basepath + '\\quarkclient.exe',
});
quarkclientAutoLauncher.enable();
quarkclientAutoLauncher.isEnabled()
.then(function(isEnabled){
if(isEnabled){
return;
}
quarkclientAutoLauncher.enable();
})
.catch(function(err){
// handle error
});
}
// 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', function() {
if (is_server) {
// open manager software
createWindowManager();
} else {
// open client software
createWindow();
}
})
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipc.on('update-notify-value', function(event, arg) {
win.webContents.send('targetPriceVal', arg);
console.log(arg)
console.log('closing window now');
callNotify();
});
ipc.on('lock-computer', function(event, arg) {
createWindow();
});
ipc.on('close-full-app', function(event, arg) {
app.close();
});
// resets application like a fresh install
function reset_app() {
store.set('is_registered_x', 0);
}
function callNotify() {
if (Notification.isSupported()) {
const notification = new Notification({ title: 'Title', body: 'I am body!!!'});
notification.show();
console.log("notification should be shown now")
}
}
// sets the application always on top no matter what
function set_alwaysonTop() {
setInterval(function(){
if (win) {
win.setAlwaysOnTop(true);
}
}, 1);
}
function handleSquirrelEvent(application) {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function(command, args) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, {
detached: true
});
} catch (error) {}
return spawnedProcess;
};
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(application.quit, 1000);
return true;
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
application.quit();
return true;
}
};