manager/main.js
2023-05-02 12:22:33 +02:00

449 lines
11 KiB
JavaScript

const {app, BrowserWindow, ipcMain, Notification, nativeImage, Tray, Menu, shell} = require("electron")
const path = require("path")
const fs = require("fs")
const ipc = ipcMain
const { io } = require("socket.io-client");
const log = require("./sub-log");
const updater = require("./updatetools")
// [Function] Main Window Function
// BUILDER "build": "
async function createWindow() {
const win = new BrowserWindow({
width: 300,
height: 500,
minWidth: 300,
minHeight: 500,
resizable: false,
movable: true,
closable: true,
frame: false,
icon: path.join(__dirname, './src/logo.ico'),
title: "Submanager",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: true
}
})
if(process.env.DEV != "true") {
updater.checkUpdate()
} else {
log.update("Desactivation des mises a jour -> DEV MOD ENABLE")
}
await win.loadFile("templates/login/login.html")
var socket = null
if(process.env.DEV == "true") {
socket = io("ws://localhost:4000", {
autoConnect: false,
reconnection: false
});
log.client("DEVELOPMENT MODE --- QUIT IMMEDIATELY")
} else {
socket = io("ws://alpha.raphix.fr:4000", {
autoConnect: false,
reconnection: false
})
}
socket.connect()
socket.on("connect", () => {
log.client("Connexion au serveur ... Finish !")
if(!fs.existsSync( __dirname + path.sep + "settings.json")) {
win.webContents.send("connectSuccess")
} else {
const settings = require( __dirname + path.sep + "settings.json")
if(settings.token) {
log.client("Auth : Connexion avec le Token Discord !")
socket.emit("authByToken", settings.token)
} else {
win.webContents.send("connectSuccess")
}
}
})
socket.on("authByTokenAnswer", (resp, token) => {
if(resp == true) {
win.loadFile("templates/app/app.html")
} else {
const settings = require( __dirname + path.sep + "settings.json")
settings["token"] = null;
fs.writeFile(__dirname + path.sep + "settings.json", JSON.stringify(settings, null, 2), () => {})
win.webContents.send("connectFailed")
}
})
socket.io.on("error", () => {
log.client("Erreur lors de la connexion entre le serveur et le client !")
win.webContents.send("connectFailed")
})
ipc.on("retryLogin", () => {
log.client("Reconnexion au serveur ... Progressing !")
socket.close()
socket.connect()
})
ipc.on("stateOfConnection", () => {
if(socket.disconnected == true) {
socket.connect()
} else {
if(!fs.existsSync( __dirname + path.sep + "settings.json")) {
win.webContents.send("connectSuccess")
} else {
const settings = require( __dirname + path.sep + "settings.json")
if(settings.token) {
log.client("Auth : Connexion avec le Token Discord !")
socket.emit("authByToken", settings.token)
} else {
win.webContents.send("connectSuccess")
}
}
}
})
ipc.on("discordlogin", () => {
socket.emit("authNeedLogin")
log.client("Discord Auth : Demande du Token vers le serveur !")
win.minimize()
})
socket.on("authFailed", () => {
win.webContents.send("discordFailed")
win.restore()
log.client("Discord Auth : Erreur lors de la connexion vers Discord !")
})
socket.on("checkFailed", () => {
win.webContents.send("discordCheckFailed")
win.restore()
log.client("Discord Auth : Erreur lors de la vérification de Discord !")
})
socket.on("authOpenLink", (link) => {
log.client("Discord Auth : Redirection vers le service Discord !")
shell.openExternal(link)
})
socket.on("successLogin", (token) => {
if(!fs.existsSync(__dirname + path.sep + "settings.json")) {
fs.writeFileSync(__dirname + path.sep + "settings.json")
}
const settings = require(__dirname + path.sep + "settings.json")
settings["token"] = token
fs.writeFile(__dirname + path.sep + "settings.json", JSON.stringify(settings, null, 2), (err) => {
log.client("Saving token in settings !")
log.client("Discord Auth : Redirection fait avec succes !")
win.loadFile("templates/app/app.html")
askUpdateState()
win.restore()
})
})
socket.on("updateState", (data) => {
log.client("State : Update en cours !")
win.webContents.send("updateData", data)
})
socket.on("actualize", (data) => {
log.client("Actualisation : Update en cours !")
win.webContents.send("actualize", data)
})
socket.on("findResult", (data) => {
log.client("Recherche : Termine et recupere")
win.webContents.send("findResult", data)
})
ipc.on("play", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Play / Pause Requested !")
socket.emit("play", settings.token)
})
ipc.on("loop", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Loop Requested !")
socket.emit("loop", settings.token)
})
ipc.on("forward", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Forward Requested !")
socket.emit("forward", settings.token)
})
ipc.on("backward", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Backward Requsted !")
socket.emit("backward", settings.token)
})
ipc.on("exitBot", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Exit Bot Requsted !")
socket.emit("exit", settings.token)
})
ipc.on("deleteQueue", (ev, identifier) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Delete Track queue Bot Requsted !")
socket.emit("deleteQueue", settings.token, identifier)
})
ipc.on("moveQueue", (ev, identifier) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Move queue Bot Requsted !")
socket.emit("moveQueue", settings.token, identifier)
})
ipc.on("findReq", (ev, value) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Find search Bot Requsted !")
socket.emit("find", settings.token, value)
})
ipc.on("addQueue", (ev, url) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Add song Bot Requsted !")
socket.emit("addQueue", settings.token, url)
})
ipc.on("listClear", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Clear Queue Bot Requsted !")
socket.emit("listClear", settings.token)
})
ipc.on("restart", () => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Restart Bot Requsted !")
socket.emit("restart", settings.token)
})
ipc.on("seek", (ev, pos) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Seek Bot Requsted !")
socket.emit("seek", settings.token, pos)
})
ipc.on("volume", (ev, pos) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Control : Volume Bot Requsted !")
socket.emit("volume", settings.token, pos)
})
ipc.on("signout", () => {
if(fs.statSync(__dirname + path.sep + "settings.json")) {
const settings = require( __dirname + path.sep + "settings.json")
settings["token"] = null;
fs.writeFile(__dirname + path.sep + "settings.json", JSON.stringify(settings, null, 2), () => {})
}
win.loadFile("template/login/login.html")
socket.disconnect()
})
ipc.on("report", (ev, report) => {
const settings = require( __dirname + path.sep + "settings.json")
log.client("Report : Envoi d'un rapport !")
report["version"] = require("./package.json").version
socket.emit("report", settings.token, report)
})
socket.on("reportAns", (ans) => {
win.webContents.send("reportForm", ans)
})
ipc.on("forceUpdate", () => {
if(process.env.DEV != "true") {
updater.update()
} else {
log.update("ABORT : MODE DEV ENABLE")
}
})
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('submanager', process.execPath, [path.resolve(process.argv[1])])
}
} else {
app.setAsDefaultProtocolClient('submanager')
}
ipc.on("close", () => {
socket.disconnect()
app.quit()
})
ipc.on("minimize", () => {
win.minimize()
})
ipc.on("askUpdateState", () => {
askUpdateState()
})
async function askUpdateState() {
const settings = require( __dirname + path.sep + "settings.json")
console.log(settings.token)
await socket.emit("getState", settings.token)
}
socket.on("disconnect", async () => {
await win.loadFile("templates/login/login.html")
await win.webContents.send("connectFailed")
});
}
function sts() {
const settings = require( __dirname + path.sep + "settings.json")
return settings
}
app.on("open-url", (ev, url) => {
app.quit()
})
app.whenReady().then(() => {
const icon = nativeImage.createFromPath(__dirname + path.sep + 'src' + path.sep + "logo.ico")
tray = new Tray(icon)
const contextMenu = Menu.buildFromTemplate([
{ label:"Quitter", click() { app.quit()}}
])
tray.setToolTip("Submanager")
tray.setContextMenu(contextMenu)
//[Operation] - Check settings
log.client("Submanager - Version : " + require("./package.json").version + " - Raphix")
log.client("Starting Application")
createWindow()
app.on('activate', () => {
autoUpdater.checkForUpdates()
if(BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
//EXPORT COMMAND : npx electron-packager . submanager-beta-1 --platform="win32" --out="C:\Users\picot\OneDrive\Bureau\"