624700287e16664bdd8dc9b8540674392ae6d197
[DeezloaderRemix.git] / app / main.js
1 // Load settings before everything
2 let appConfig;
3 const fs = require("fs-extra");
4 const path = require('path');
5 const {app, BrowserWindow, ipcMain, Menu, Tray, Notification} = require('electron');
6 const os = require('os');
7 loadSettings();
8
9 const captcha = require('./public/js/captcha');
10 captcha.registerScheme();
11
12 const theApp = require('./app');
13 const WindowStateManager = require('electron-window-state-manager');
14 const url = require('url');
15
16 let tray = null;
17 let mainWindow;
18 let trayIcon = __dirname + "/icon.png";
19 let isTray = false;
20 // Create a new instance of the WindowStateManager
21 const mainWindowState = new WindowStateManager('mainWindow', {
22         defaultWidth: 1280,
23         defaultHeight: 800
24 });
25
26 var shouldQuit = !app.requestSingleInstanceLock()
27 app.on('second-instance', function (argv, cwd) {
28         if (mainWindow) {
29     if (mainWindow.isMinimized()) mainWindow.restore();
30                 if (!mainWindow.isVisible()) mainWindow.show();
31     mainWindow.focus();
32   }
33 })
34
35 if (shouldQuit) {
36   app.quit();
37   return;
38 }
39
40 app.isQuiting = false;
41
42 app.serverMode = (process.argv.indexOf("-s")>-1 || process.argv.indexOf("--server")>-1);
43
44 require('electron-context-menu')({
45         showInspectElement: false
46 });
47
48 function loadSettings(){
49         var userdata = "";
50         if(process.platform == "android"){
51                 userdata = os.homedir() + "/storage/shared/Deezloader Remix/";
52         }else{
53                 userdata = app.getPath("appData")+path.sep+"Deezloader Remix"+path.sep;
54         }
55
56         if(!fs.existsSync(userdata+"config.json")){
57                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
58         }
59
60         appConfig = require(userdata+path.sep+"config.json");
61
62         if( typeof appConfig.userDefined.numplaylistbyalbum != "boolean" ||
63                         typeof appConfig.userDefined.syncedlyrics != "boolean" ||
64                         typeof appConfig.userDefined.padtrck != "boolean" ||
65                         typeof appConfig.userDefined.albumNameTemplate != "string"
66                 ){
67                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
68                 appConfig = require(userdata+path.sep+"config.json");
69         }
70 }
71
72 function createTray(){
73         tray = new Tray(trayIcon);
74         const contextMenu = Menu.buildFromTemplate([
75                 {
76                         label: 'Show App',
77                         click: function() {
78                                 if (mainWindow) mainWindow.show();
79                         }
80                 },
81                 {
82                         label: 'Quit',
83                         click: function() {
84                                 app.isQuiting = true;
85                                 if (mainWindow){mainWindow.close()}else{app.quit()};
86                         }
87                 }
88         ]);
89         tray.setToolTip('Deezloader Remix');
90         tray.setContextMenu(contextMenu);
91
92         tray.on('click', function(e){
93                 if (mainWindow){
94                         if (mainWindow.isVisible()) {
95                                 mainWindow.hide()
96                         } else {
97                                 mainWindow.show()
98                         }
99                 }
100         });
101 }
102
103 function createWindow () {
104         // Create the browser window.
105         mainWindow = new BrowserWindow({
106                 width: mainWindowState.width,
107                 height: mainWindowState.height,
108                 x: mainWindowState.x,
109                 y: mainWindowState.y,
110                 alwaysOnTop: false,
111                 frame: false,
112                 icon: __dirname + "/icon.png",
113                 minWidth: 415,
114                 minHeight: 32,
115                 backgroundColor: "#23232c"
116         });
117
118         mainWindow.setMenu(null);
119         var template = [{
120             label: "Application",
121             submenu: [
122                 { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
123             ]}, {
124             label: "Edit",
125             submenu: [
126                 { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
127                 { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
128                 { type: "separator" },
129                 { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
130                 { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
131                 { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
132                 { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
133             ]}
134         ]
135
136         Menu.setApplicationMenu(Menu.buildFromTemplate(template))
137
138         // and load the index.html of the app.
139         mainWindow.loadURL('http://localhost:' + appConfig.serverPort);
140
141         mainWindow.on('closed', function () {
142                 mainWindow = null;
143         });
144
145         // Check if window was closed maximized and restore it
146         if (mainWindowState.maximized) {
147                 mainWindow.maximize();
148         }
149
150         // Save current window state
151         mainWindow.on('close', (event) => {
152                 if(appConfig.userDefined.minimizeToTray && !app.isQuiting){
153                         event.preventDefault()
154                         mainWindow.hide();
155                 } else {
156                         mainWindowState.saveState(mainWindow);
157                 }
158         });
159 }
160
161 app.on('ready', function(){
162         if (!app.serverMode){
163                 createWindow();
164                 createTray();
165         }
166         captcha.registerProtocol();
167 });
168
169 // Quit when all windows are closed.
170 app.on('window-all-closed', function () {
171         if (!appConfig.userDefined.minimizeToTray || app.isQuiting){
172                 app.quit();
173         }
174 });
175
176 app.on('activate', function () {
177         if (!app.serverMode && mainWindow === null) {
178                 createWindow();
179         }
180 });