-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
510 lines (389 loc) · 11.6 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
const { app, BrowserWindow, Menu, ipcMain, dialog, Tray } = require('electron');
const path = require('node:path');
const fs = require('fs');
const url = require('url');
const notifier = require('node-notifier');
const cron = require('node-cron');
const { exec } = require('child_process');
const AutoLaunch = require('auto-launch');
const myAppAutoLauncher = new AutoLaunch({
name: 'DentIMApp',
path: process.execPath,
isHidden: true,
});
app.on('ready', () => {
stopNotificationProcess();
const isAutoLaunch = app.getLoginItemSettings().wasOpenedAtLogin;
if (isAutoLaunch) {
mainWindow.once('ready-to-show', () => {
mainWindow.minimize();
});
}
myAppAutoLauncher.isEnabled().then((isEnabled) => {
if (!isEnabled) {
myAppAutoLauncher.enable().catch((err) => {
console.error('Failed to enable auto-launch:', err);
});
}
}).catch((err) => {
console.error('Auto-launch setup failed:', err);
});
});
let tray;
let isUpdateInProgress = false;
const { autoUpdater,AppUpdater } = require("electron-updater");
autoUpdater.autoDownload = false;
const pidFilePath = './notificationProcess.pid';
let notificationProcess = null;
app.on('ready', () => {
autoUpdater.checkForUpdates();
autoUpdater.on('update-available', () => {
isUpdateInProgress = true;
autoUpdater.downloadUpdate();
autoUpdater.on('update-downloaded', () => {
console.log('Update downloaded. Ready to install.');
app.removeAllListeners('before-quit');
autoUpdater.quitAndInstall();
});
});
autoUpdater.on('checking-for-update', () => {
console.log('Checking for update...');
});
autoUpdater.on('update-not-available', () => {
console.log('Update not available.');
});
autoUpdater.on('error', (err) => {
console.error('Error in auto-updater:', err);
});
autoUpdater.on('download-progress', (progressObj) => {
console.log(`Download speed: ${progressObj.bytesPerSecond}`);
console.log(`Downloaded ${progressObj.percent}%`);
});
tray = new Tray(path.join(__dirname, 'images/LogoDentread.png'));
});
function retrieveNotificationProcess() {
if (fs.existsSync(pidFilePath)) {
const pid = parseInt(fs.readFileSync(pidFilePath, 'utf8'));
try {
process.kill(pid, 0);
notificationProcess = pid;
} catch (error) {
fs.unlinkSync(pidFilePath);
}
}
}
function stopNotificationProcess() {
if (notificationProcess) {
try {
process.kill(notificationProcess);
notificationProcess = null;
fs.unlinkSync(pidFilePath);
} catch (error) {
console.error(`Error killing notification process: ${error}`);
}
}
}
let mainWindow;
let customDialog;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
icon: path.join(__dirname, 'images/LogoDentread.png'),
title: 'Dentread',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
},
});
mainWindow.loadFile('contents/login_dentread.html');
retrieveNotificationProcess();
stopNotificationProcess();
function sendTestNotification() {
setTimeout(function() {
notifier.notify({
title: 'IM App Session Expiry',
message: 'Your IM App session will expire soon please logout and login again',
sound: true,
wait: true,
icon: path.join(__dirname, 'images/LogoDentread.png'),
});
}, 2147483647);
}
sendTestNotification();
function preventClose(event) {
event.preventDefault();
try {
const { dialog } = require('electron');
dialog.showMessageBox(mainWindow, {
type: 'warning',
title: 'Warning',
message: 'Closing the window is not allowed while auto sync is on.',
buttons: ['OK']
});
} catch (error) {
console.error('Error showing warning dialog:', error);
}
}
mainWindow.once('ready-to-show', () => {
autoUpdater.checkForUpdatesAndNotify();
ipcMain.on('toggle-auto-sync', (event, status, syncedFoldersJSON) => {
if (status) {
mainWindow.hide();
mainWindow.on('close', preventClose);
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open', click: () => mainWindow.show() },
{ type: 'separator' },
{ label: 'Quit', role: 'quit' }
]);
tray.setContextMenu(contextMenu);
tray.on('click', () => {
if (!mainWindow.isVisible()) {
mainWindow.show();
} else {
mainWindow.focus();
}
});
function updateNotification() {
const syncedFoldersArray = JSON.parse(syncedFoldersJSON);
console.log(syncedFoldersArray, "syncedFoldersArray");
if (Array.isArray(syncedFoldersArray)) {
const totalCount = syncedFoldersArray.length;
notifier.notify({
title: 'Dentraed IM App Sync Update',
message: `Total Synced ${totalCount} Scans.`,
sound: true,
wait: true,
icon: path.join(__dirname, 'images/LogoDentread.png'),
});
} else {
console.error('Synced folders array not found in local storage.');
}
}
try {
const intervalId = setInterval(updateNotification, 2 * 60 * 60 * 1000);}
catch (error) {
console.error('Error in auto-update process:', error);
}
} else {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.removeListener('close', preventClose);
}
});
});
mainWindow.on('close', async(event) => {
if (isUpdateInProgress) {
return;
}
const choice = require('electron').dialog.showMessageBoxSync(mainWindow, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to close the application?'
})
if (choice === 0) {
try {
console.log('Disabling auto-launch...');
myAppAutoLauncher.disable().catch((err) => {
console.error('Failed to disable auto-launch:', err);
});
console.log('Deleting directory...');
await mainWindow.webContents.executeJavaScript(`window.versions.deleteDirectory()`);
console.log('Exiting application...');
app.quit();
} catch (error) {
console.error('Error during close operation:', error);
}
} else {
event.preventDefault();
}
})
// mainWindow.webContents.openDevTools();
}
app.whenReady().then(() => {
ipcMain.handle('ping', () => 'pong');
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
const menuTemplate = [
{
label: 'Menu',
submenu: [
{
label: 'Exit',
click() {
app.quit();
},
},
],
},
];
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(null);
});
function createCustomDialog() {
customDialog = new BrowserWindow({
width: 600,
height: 450,
parent: mainWindow,
modal: true,
show: false,
minimizable: false,
maximizable: false,
icon: path.join(__dirname, 'images/MySettings.png'),
title: 'Settings',
});
customDialog.loadURL(url.format({
pathname: path.join(__dirname, 'contents/settings.html'),
protocol: 'file:',
slashes: true,
}));
customDialog.setMenu(null);
customDialog.once('ready-to-show', () => {
customDialog.show();
});
customDialog.on('closed', () => {
customDialog = null;
mainWindow.reload();
});
// customDialog.webContents.openDevTools();
}
ipcMain.handle('open-settings', () => {
if (!customDialog) {
createCustomDialog();
}
return true;
});
let customlog;
function createcustomlog() {
customlog = new BrowserWindow({
width: 500,
height: 500,
parent: mainWindow,
modal: true,
show: false,
minimizable: false,
maximizable: false,
icon: path.join(__dirname, 'images/PlusFolder.png'),
title: 'Log',
});
customlog.loadURL(url.format({
pathname: path.join(__dirname, 'contents/log.html'),
protocol: 'file:',
slashes: true,
}));
customlog.setMenu(null);
customlog.once('ready-to-show', () => {
customlog.show();
});
customlog.on('closed', () => {
customlog = null;
});
}
ipcMain.handle('open-logs', () => {
if (!customlog) {
createcustomlog();
}
return true;
});
ipcMain.on('logInfo', (event, message) => {
const logWindow = BrowserWindow.getAllWindows().find(window => window.getTitle() === 'Log');
if (logWindow) {
logWindow.webContents.send('logInfo', message);
}
});
ipcMain.on('logError', (event, error) => {
const logWindow = BrowserWindow.getAllWindows().find(window => window.getTitle() === 'Log');
if (logWindow) {
logWindow.webContents.send('logError', error);
}
});
app.on('before-quit', async () => {
function startNotificationProcess() {
const { spawn } = require('child_process');
const nodePath = process.execPath;
const pidFilePath = path.join(__dirname, 'notification.pid');
try {
const notificationProcess = spawn(nodePath, ['notification.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
notificationProcess.unref();
notificationProcess.on('error', (error) => {
console.error(`Error starting notification process: ${error.message}`);
});
notificationProcess.on('exit', (code) => {
console.log(`Notification process exited with code ${code}`);
if (fs.existsSync(pidFilePath)) {
fs.unlinkSync(pidFilePath);
}
});
notificationProcess.on('spawn', () => {
try {
fs.writeFileSync(pidFilePath, notificationProcess.pid.toString(), { mode: 0o644 });
console.log(`Notification process started with PID: ${notificationProcess.pid}`);
} catch (fsError) {
console.error(`Error writing PID file: ${fsError.message}`);
}
});
} catch (spawnError) {
console.error(`Failed to spawn notification process: ${spawnError.message}`);
}
}
startNotificationProcess();
});
let customSchedule;
function createCustomScheduler() {
customSchedule = new BrowserWindow({
width: 520,
height: 420,
parent: mainWindow,
modal: true,
show: false,
minimizable: false,
maximizable: false,
icon: path.join(__dirname, 'images/MySettings.png'),
title: 'Scheduler',
});
customSchedule.loadURL(url.format({
pathname: path.join(__dirname, 'contents/Scheduler.html'),
protocol: 'file:',
slashes: true,
}));
customSchedule.setMenu(null);
customSchedule.once('ready-to-show', () => {
customSchedule.show();
});
customSchedule.on('closed', () => {
customSchedule = null;
mainWindow.reload();
});
}
ipcMain.handle('open-scheduler', () => {
if (!customSchedule) {
createCustomScheduler();
}
return true;
});
ipcMain.handle('open-reload-manual', () => {
mainWindow.reload();
return true;
});
// app.on('window-all-closed', () => {
// const projectPath = './';
// const directoryPath = path.join(projectPath, 'Dentread');
// if (fs.existsSync(directoryPath)) {
// rimraf.sync(directoryPath);
// console.log(`Directory deleted: ${directoryPath}`);
// } else {
// console.log('Directory does not exist');
// }
// if (process.platform !== 'darwin') {
// app.quit();
// }
// });