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