Updated README.md
[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 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         // Create the browser window.
102         mainWindow = new BrowserWindow({
103                 width: mainWindowState.width,
104                 height: mainWindowState.height,
105                 x: mainWindowState.x,
106                 y: mainWindowState.y,
107                 alwaysOnTop: false,
108                 frame: false,
109                 icon: __dirname + "/icon.png",
110                 minWidth: 415,
111                 minHeight: 32,
112                 backgroundColor: "#23232c"
113         });
114
115         mainWindow.setMenu(null);
116
117         // and load the index.html of the app.
118         mainWindow.loadURL('http://localhost:' + appConfig.serverPort);
119
120         mainWindow.on('closed', function () {
121                 mainWindow = null;
122         });
123
124         // Check if window was closed maximized and restore it
125         if (mainWindowState.maximized) {
126                 mainWindow.maximize();
127         }
128
129         // Save current window state
130         mainWindow.on('close', (event) => {
131                 if(appConfig.userDefined.minimizeToTray && !app.isQuiting){
132                         event.preventDefault()
133                         mainWindow.hide();
134                 } else {
135                         mainWindowState.saveState(mainWindow);
136                 }
137         });
138 }
139
140 app.on('ready', function(){
141         if (!app.serverMode){
142                 createWindow();
143                 createTray();
144         }
145 });
146
147 // Quit when all windows are closed.
148 app.on('window-all-closed', function () {
149         if (!appConfig.userDefined.minimizeToTray || app.isQuiting){
150                 app.quit();
151         }
152 });
153
154 app.on('activate', function () {
155         if (!app.serverMode && mainWindow === null) {
156                 createWindow();
157         }
158 });