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