main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import url from 'url'
  2. import { app, BrowserWindow, ipcMain } from 'electron'
  3. import { is } from 'electron-util'
  4. import { join, dirname } from "path";
  5. import { writeFile, existsSync, mkdirSync } from "fs";
  6. import Store from 'electron-store'
  7. import { VERSION } from './version'
  8. console.log(VERSION)
  9. if (process.argv.some(a => a === '-v')) app.exit()
  10. let mainWindow
  11. const configData = new Store({
  12. defaults: {
  13. windowBounds: {
  14. main: { width: 1800, height: 800 }
  15. },
  16. user_data: app.getPath("userData"),
  17. scan_prefix: ''
  18. }
  19. });
  20. if (!configData.get('pdf_verzeichnis')) {
  21. configData.set('pdf_verzeichnis', join(app.getPath('documents'), app.getName(), 'Kurslisten'))
  22. }
  23. function createWindow() {
  24. mainWindow = new BrowserWindow({
  25. ...configData.get('windowBounds.main'),
  26. show: false,
  27. useContentSize: true,
  28. defaultEncoding: 'utf-8',
  29. webPreferences: {
  30. nodeIntegration: true
  31. },
  32. title: `${app.name} ${VERSION['buildVersion']}`
  33. // icon: join(__dirname, '../icons/icon.png')
  34. })
  35. mainWindow.removeMenu()
  36. mainWindow.loadURL(
  37. url.format({
  38. pathname: join(__dirname, "index.html"),
  39. protocol: "file:",
  40. slashes: true
  41. }))
  42. if (is.development || process.argv.some(a => a === '--devtools')) mainWindow.openDevTools()
  43. mainWindow.on('close', e => {
  44. if (!configData.get('close')) {
  45. e.preventDefault()
  46. configData.set('windowBounds.main', mainWindow.getBounds())
  47. mainWindow.webContents.send('close_db')
  48. console.log('Konfigurationsdaten gespeichert, Datenbank geschlossen')
  49. configData.set('close', true)
  50. mainWindow.close()
  51. }
  52. })
  53. mainWindow.once('ready-to-show', async () => {
  54. mainWindow.show()
  55. })
  56. }
  57. app.on('ready', createWindow)
  58. app.on('window-all-closed', function () {
  59. app.quit()
  60. })
  61. app.on('activate', function () {
  62. if (mainWindow === null) createWindow()
  63. })
  64. ipcMain.on('print', (event, arg) => {
  65. const options = {
  66. margins: 'none'
  67. }
  68. mainWindow.webContents.print(options, (success, errorType) => {
  69. event.reply('print-reply', success)
  70. })
  71. })
  72. function ensureDirectoryExistence(filePath) {
  73. const dir = dirname(filePath);
  74. if (existsSync(dir)) {
  75. return true;
  76. }
  77. ensureDirectoryExistence(dir);
  78. try {
  79. mkdirSync(dir);
  80. } catch (e) {
  81. console.log(
  82. `Verzeichnis ${dir} konnte nicht erstellt werden: `,
  83. e.message
  84. );
  85. }
  86. }
  87. ipcMain.on('pdf', async (event, pdf_name) => {
  88. const pdf_path = join(configData.get('pdf_verzeichnis'), pdf_name);
  89. const options = {
  90. marginsType: 1,
  91. };
  92. try {
  93. const data = await mainWindow.webContents.printToPDF(options);
  94. ensureDirectoryExistence(pdf_path);
  95. writeFile(pdf_path, data, error => {
  96. if (error) throw error;
  97. console.log(pdf_path, 'erfolgreich gesichert')
  98. event.reply('pdf-reply', true)
  99. });
  100. return true
  101. } catch (e) {
  102. console.log(
  103. `PDF konnte nicht geschrieben werden: `,
  104. e.message
  105. );
  106. event.reply('pdf-reply', false)
  107. }
  108. });
  109. ipcMain.handle('get_store', (event, key) => configData.store );
  110. ipcMain.handle('set_store', (event, value) => configData.set(value))