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