config file loading and saving

This commit is contained in:
2024-06-03 16:32:05 +02:00
parent 57ece16e14
commit c4cc2170f5
7 changed files with 149 additions and 52 deletions
+89 -33
View File
@@ -1,4 +1,4 @@
const { app, BrowserWindow, BaseWindow, nativeTheme, Menu, MenuItem } = require('electron/main');
const { app, BrowserWindow, ipcMain, nativeTheme, Menu, MenuItem } = require('electron/main');
const path = require('node:path');
const fs = require('fs');
const os = require('os');
@@ -14,6 +14,7 @@ const isMac = process.platform === 'darwin';
//------------------------------------------------------------------------------
let config;
let configPath;
let configWindow;
let win;
let menu;
@@ -40,28 +41,104 @@ function loadConfig() {
__dirname
];
let filePath;
// Check all locations
for (const location of locations) {
// Update the filepath
filePath = path.join(location, 'config.json');
configPath = path.join(location, 'config.json');
try {
// Try access
fs.accessSync(filePath);
fs.accessSync(configPath);
// Parse the file if found
console.info('Found config file at ' + filePath);
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
console.info('Found config file at ' + configPath);
const data = fs.readFileSync(configPath, { encoding: 'utf8' });
config = JSON.parse(data);
// Break the loop
break;
} catch (err) {
console.warn('No config file found at ' + filePath);
console.warn('No config file found at ' + configPath);
}
}
}
function saveConfig(newConfig) {
if (!configPath) {
console.error("No config file path was set");
return;
}
if(config.url != newConfig.url) {
config.url = newConfig.url;
loadUrlAsync(config.url);
}
config.whitelist = [];
let whitelist = newConfig.whitelist.split(";");
whitelist = whitelist.filter(function (entry) { return entry.trim() != ''; });
whitelist.forEach(
token => {
config.whitelist.push(token.replace(/\r?\n|\r/g, ""));
}
);
config.logPath = newConfig.logPath;
fs.writeFile(configPath, JSON.stringify(config, null, " "), (error) => {
if (error) log.warn('Error writing to ' + configPath, error);
log.info("Saved config to " + configPath);
});
// updare UI with new values
configWindow.webContents.send('update-config', config);
}
/**
* Creates a modal config view and attaches as to the main window
*/
function showConfigWindow() {
// create lazily
if (configWindow == undefined) {
configWindow = new BrowserWindow({
parent: win,
width: 640,
height: 460,
minimizable: false,
maximizable: false,
fullscreenable: false,
backgroundColor: '#1f1f1f',
autoHideMenuBar: true,
excludedFromShownWindowsMenu: true,
webPreferences: {
preload: path.join(__dirname, 'js/configPreload.js')
}
});
configWindow.on('close', (event) => {
event.preventDefault();
configWindow.hide();
});
ipcMain.on('save-config', (event, configData) => {
configWindow.hide();
saveConfig(configData);
});
ipcMain.on('cancel-config', (event) => {
configWindow.hide();
configWindow.webContents.send('update-config', config);
});
configWindow.loadFile('./html/config.html');
}
// update text field values
configWindow.webContents.send('update-config', config);
// show window
configWindow.show();
}
@@ -75,8 +152,9 @@ function createMainWindow() {
backgroundColor: '#000000',
icon: 'images/icon-512.png',
autoHideMenuBar: true,
kiosk: true,
webPreferences: {
preload: path.join(__dirname, './js/preload.js'),
preload: path.join(__dirname, 'js/preload.js'),
webSecurity: false,
disableDialogs: true
},
@@ -99,11 +177,6 @@ function createMainWindow() {
});
win.on("page-title-updated", (event) => event.preventDefault());
// show initial black window
// win.setKiosk(true);
win.show();
// Load page from config file
if (config != undefined && 'url' in config) {
loadUrlAsync(config.url);
@@ -119,22 +192,6 @@ function createMainWindow() {
}
}
/**
* Creates a modal config view and attaches as to the main window
*/
function createConfigWindow() {
configWindow = new BrowserWindow({
parent: win,
modal: true,
width: 640,
height: 460,
show: false,
backgroundColor: '#1f1f1f'
});
configWindow.loadFile('./html/config.html');
}
/**
* Creates all available keyboard shortcuts
@@ -155,7 +212,7 @@ function createMenu() {
{
label: 'Config',
accelerator: isMac ? 'Cmd+,' : 'Ctrl+,',
click: () => { configWindow.isVisible() ? configWindow.hide() : configWindow.show(); }
click: () => { showConfigWindow(); }
}
]
}));
@@ -178,7 +235,7 @@ function validateDomain(event) {
// allow local urls
if (['file:', 'file'].includes(url.protocol)) return true;
if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) {
if ('whitelist' in config && !config.whitelist.includes(url.hostname)) {
event.preventDefault();
log.info("Navigation to " + event.url + " prevented");
return false;
@@ -256,7 +313,6 @@ app.whenReady().then(() => {
createMenu();
createMainWindow();
createConfigWindow()
});
app.on('activate', () => {