|
@@ -1,24 +1,19 @@
|
|
|
-import { join } from 'path'
|
|
|
import url from 'url'
|
|
|
import { app, BrowserWindow, ipcMain } from 'electron'
|
|
|
import { is } from 'electron-util'
|
|
|
-import Store from 'electron-store'
|
|
|
-
|
|
|
-const configFile = new Store({
|
|
|
- defaults: {
|
|
|
- windowBounds: {
|
|
|
- main: { width: 1800, height: 800 }
|
|
|
- },
|
|
|
- scan_prefix: ''
|
|
|
- }
|
|
|
-});
|
|
|
-const configData = configFile.store
|
|
|
+import { join, dirname } from "path";
|
|
|
+import { writeFile, existsSync, mkdirSync } from "fs";
|
|
|
+import configData from './configstore'
|
|
|
|
|
|
let mainWindow
|
|
|
|
|
|
+if (!configData.get('pdf_verzeichnis')) {
|
|
|
+ configData.set('pdf_verzeichnis', join(app.getPath('documents'), app.getName(), 'Kurslisten'))
|
|
|
+}
|
|
|
+
|
|
|
function createWindow() {
|
|
|
mainWindow = new BrowserWindow({
|
|
|
- ...configData.windowBounds.main,
|
|
|
+ ...configData.get('windowBounds.main'),
|
|
|
show: false,
|
|
|
useContentSize: true,
|
|
|
defaultEncoding: 'utf-8',
|
|
@@ -38,12 +33,12 @@ function createWindow() {
|
|
|
if (is.development || process.argv.some(a => a === '--devtools')) mainWindow.openDevTools()
|
|
|
|
|
|
mainWindow.on('close', e => {
|
|
|
- if (!configData.close) {
|
|
|
+ if (!configData.get('close')) {
|
|
|
e.preventDefault()
|
|
|
- configFile.set('windowBounds.main', mainWindow.getBounds())
|
|
|
+ configData.set('windowBounds.main', mainWindow.getBounds())
|
|
|
mainWindow.webContents.send('close_db')
|
|
|
console.log('Konfigurationsdaten gespeichert, Datenbank geschlossen')
|
|
|
- configData.close = true
|
|
|
+ configData.set('close', true)
|
|
|
mainWindow.close()
|
|
|
}
|
|
|
})
|
|
@@ -65,6 +60,45 @@ ipcMain.on('print', (event, arg) => {
|
|
|
margins: 'none'
|
|
|
}
|
|
|
mainWindow.webContents.print(options, (success, errorType) => {
|
|
|
- event.reply('asynchronous-reply', success)
|
|
|
+ event.reply('print-reply', success)
|
|
|
})
|
|
|
-})
|
|
|
+})
|
|
|
+
|
|
|
+function ensureDirectoryExistence(filePath) {
|
|
|
+ const dir = dirname(filePath);
|
|
|
+ if (existsSync(dir)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ ensureDirectoryExistence(dir);
|
|
|
+ try {
|
|
|
+ mkdirSync(dir);
|
|
|
+ } catch (e) {
|
|
|
+ console.log(
|
|
|
+ `Verzeichnis ${dir} konnte nicht erstellt werden: `,
|
|
|
+ e.message
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+ipcMain.on('pdf', async (event, pdf_name) => {
|
|
|
+ const pdf_path = join(configData.get('pdf_verzeichnis'), pdf_name);
|
|
|
+ const options = {
|
|
|
+ marginsType: 1,
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const data = await mainWindow.webContents.printToPDF(options);
|
|
|
+ ensureDirectoryExistence(pdf_path);
|
|
|
+ writeFile(pdf_path, data, error => {
|
|
|
+ if (error) throw error;
|
|
|
+ console.log(pdf_path, 'erfolgreich gesichert')
|
|
|
+ event.reply('pdf-reply', true)
|
|
|
+ });
|
|
|
+ return true
|
|
|
+ } catch (e) {
|
|
|
+ console.log(
|
|
|
+ `PDF konnte nicht geschrieben werden: `,
|
|
|
+ e.message
|
|
|
+ );
|
|
|
+ event.reply('pdf-reply', false)
|
|
|
+ }
|
|
|
+});
|