Up to date commit
[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} = require('electron');
6 const os = require('os');
7 const {Menu, Tray} = require('electron')
8 loadSettings();
9
10 const theApp = require('./app');
11 const WindowStateManager = require('electron-window-state-manager');
12 const url = require('url');
13
14 let tray = null;
15 let mainWindow;
16 let trayIcon = __dirname + "/icon.png";
17 let isTray = false;
18 // Create a new instance of the WindowStateManager
19 const mainWindowState = new WindowStateManager('mainWindow', {
20         defaultWidth: 1280,
21         defaultHeight: 800
22 });
23
24 require('electron-context-menu')({
25         showInspectElement: false
26 });
27
28 function loadSettings(){
29         var userdata = "";
30         if(process.platform == "android"){
31                 userdata = os.homedir() + "/storage/shared/Deezloader Remix/";
32         }else{
33                 userdata = app.getPath("appData")+path.sep+"Deezloader Remix"+path.sep;
34         }
35
36         if(!fs.existsSync(userdata+"config.json")){
37                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
38         }
39
40         appConfig = require(userdata+path.sep+"config.json");
41
42         if( typeof appConfig.userDefined.numplaylistbyalbum != "boolean" ||
43                         typeof appConfig.userDefined.syncedlyrics != "boolean" ||
44                         typeof appConfig.userDefined.padtrck != "boolean" ||
45                         typeof appConfig.userDefined.albumNameTemplate != "string"
46                 ){
47                 fs.outputFileSync(userdata+"config.json",fs.readFileSync(__dirname+path.sep+"default.json",'utf8'));
48                 appConfig = require(userdata+path.sep+"config.json");
49         }
50 }
51
52 function createWindow () {
53
54         if (!(process.argv.indexOf("-s")>-1 || process.argv.indexOf("--server")>-1)){
55                 // Create the browser window.
56                 mainWindow = new BrowserWindow({
57                         width: mainWindowState.width,
58                         height: mainWindowState.height,
59                         x: mainWindowState.x,
60                         y: mainWindowState.y,
61                         alwaysOnTop: false,
62                         frame: false,
63                         icon: __dirname + "/icon.png",
64                         minWidth: 415,
65                         minHeight: 32,
66                         backgroundColor: "#23232c"
67                 });
68
69                 mainWindow.setMenu(null);
70
71                 // and load the index.html of the app.
72                 mainWindow.loadURL('http://localhost:' + appConfig.serverPort);
73
74                 mainWindow.on('closed', function () {
75                         mainWindow = null;
76                 });
77
78                 // Check if window was closed maximized and restore it
79                 if (mainWindowState.maximized) {
80                         mainWindow.maximize();
81                 }
82 mainWindow.on('minimize',function(event){
83                                 if(appConfig.userDefined.minimizeToTray){
84     event.preventDefault();
85         mainWindow.hide();
86 }
87 });
88 tray.on('click', function(e){
89     if (mainWindow.isVisible()) {
90       mainWindow.hide()
91     } else {
92       mainWindow.show()
93     }
94   });
95                 // Save current window state
96                 mainWindow.on('close', () => {
97                         if(appConfig.userDefined.minimizeToTray){
98                             if(!app.isQuitting){
99         event.preventDefault();
100         mainWindow.hide();
101     } else {
102                         mainWindowState.saveState(mainWindow);
103                 }
104         } else {
105                 mainWindowState.saveState(mainWindow);
106         }
107                 });
108         }
109         }
110
111 app.on('ready', function(){
112         if(appConfig.userDefined.minimizeToTray){
113                     tray = new Tray(trayIcon);
114     const contextMenu = Menu.buildFromTemplate([]);
115     tray.setToolTip('Deezloader Remix');
116     tray.setContextMenu(contextMenu);
117         createWindow();
118 }
119 });
120
121 // Quit when all windows are closed.
122 app.on('window-all-closed', function () {
123         app.quit();
124 });
125
126 app.on('activate', function () {
127         if (mainWindow === null) {
128                 createWindow();
129         }
130 });