2022-04-01 13:28:21 +00:00
|
|
|
import { app, dialog, ipcMain } from 'electron';
|
2022-07-26 13:53:56 +00:00
|
|
|
import { autoUpdater, UpdateInfo } from 'electron-updater';
|
2022-03-16 09:49:48 +00:00
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
2022-03-31 07:33:58 +00:00
|
|
|
import databaseManager from '../backend/database/manager';
|
2022-03-16 09:49:48 +00:00
|
|
|
import { Main } from '../main';
|
2022-03-31 07:33:58 +00:00
|
|
|
import { DatabaseMethod } from '../utils/db/types';
|
|
|
|
import { IPC_ACTIONS } from '../utils/messages';
|
2022-05-19 05:56:33 +00:00
|
|
|
import { getUrlAndTokenString, sendError } from './contactMothership';
|
|
|
|
import { getLanguageMap } from './getLanguageMap';
|
2022-05-19 18:25:01 +00:00
|
|
|
import {
|
|
|
|
getConfigFilesWithModified,
|
2022-05-20 10:06:38 +00:00
|
|
|
getErrorHandledReponse,
|
2022-05-19 18:25:01 +00:00
|
|
|
setAndGetCleanedConfigFiles,
|
|
|
|
} from './helpers';
|
2022-05-19 12:08:46 +00:00
|
|
|
import { saveHtmlAsPdf } from './saveHtmlAsPdf';
|
2022-03-16 09:49:48 +00:00
|
|
|
|
2022-07-26 13:53:56 +00:00
|
|
|
autoUpdater.autoDownload = false;
|
|
|
|
|
|
|
|
autoUpdater.on('error', (error) => {
|
|
|
|
dialog.showErrorBox(
|
|
|
|
'Update Error: ',
|
|
|
|
error == null ? 'unknown' : (error.stack || error).toString()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', async (info: UpdateInfo) => {
|
|
|
|
const currentVersion = app.getVersion();
|
|
|
|
const nextVersion = info.version;
|
|
|
|
const isCurrentBeta = currentVersion.includes('beta');
|
|
|
|
const isNextBeta = nextVersion.includes('beta');
|
|
|
|
|
|
|
|
let downloadUpdate = true;
|
|
|
|
if (!isCurrentBeta && isNextBeta) {
|
|
|
|
const option = await dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: `Update Frappe Books?`,
|
|
|
|
message: `Download version ${nextVersion}?`,
|
|
|
|
buttons: ['Yes', 'No'],
|
|
|
|
});
|
|
|
|
|
|
|
|
downloadUpdate = option.response === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!downloadUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await autoUpdater.downloadUpdate();
|
|
|
|
});
|
|
|
|
|
2022-03-16 09:49:48 +00:00
|
|
|
export default function registerIpcMainActionListeners(main: Main) {
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_OPEN_FILEPATH, async (event, options) => {
|
|
|
|
return await dialog.showOpenDialog(main.mainWindow!, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_SAVE_FILEPATH, async (event, options) => {
|
|
|
|
return await dialog.showSaveDialog(main.mainWindow!, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_DIALOG_RESPONSE, async (event, options) => {
|
|
|
|
if (main.isDevelopment || main.isLinux) {
|
|
|
|
Object.assign(options, { icon: main.icon });
|
|
|
|
}
|
|
|
|
|
|
|
|
return await dialog.showMessageBox(main.mainWindow!, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.SHOW_ERROR, async (event, { title, content }) => {
|
|
|
|
return await dialog.showErrorBox(title, content);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(
|
|
|
|
IPC_ACTIONS.SAVE_HTML_AS_PDF,
|
|
|
|
async (event, html, savePath) => {
|
2022-05-19 12:08:46 +00:00
|
|
|
return await saveHtmlAsPdf(html, savePath, app);
|
2022-03-16 09:49:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.SAVE_DATA, async (event, data, savePath) => {
|
|
|
|
return await fs.writeFile(savePath, data, { encoding: 'utf-8' });
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.SEND_ERROR, (event, bodyJson) => {
|
|
|
|
sendError(bodyJson);
|
|
|
|
});
|
|
|
|
|
2022-07-26 13:53:56 +00:00
|
|
|
ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, async () => {
|
2022-03-16 09:49:48 +00:00
|
|
|
if (!main.isDevelopment && !main.checkedForUpdate) {
|
2022-07-26 13:53:56 +00:00
|
|
|
await autoUpdater.checkForUpdates();
|
2022-05-27 10:47:24 +00:00
|
|
|
main.checkedForUpdate = true;
|
2022-03-16 09:49:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_LANGUAGE_MAP, async (event, code) => {
|
|
|
|
const obj = { languageMap: {}, success: true, message: '' };
|
|
|
|
try {
|
2022-05-19 05:56:33 +00:00
|
|
|
obj.languageMap = await getLanguageMap(code);
|
2022-03-16 09:49:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
obj.success = false;
|
|
|
|
obj.message = (err as Error).message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_FILE, async (event, options) => {
|
|
|
|
const response = {
|
|
|
|
name: '',
|
|
|
|
filePath: '',
|
|
|
|
success: false,
|
|
|
|
data: Buffer.from('', 'utf-8'),
|
|
|
|
canceled: false,
|
|
|
|
};
|
|
|
|
const { filePaths, canceled } = await dialog.showOpenDialog(
|
|
|
|
main.mainWindow!,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
|
|
|
|
response.filePath = filePaths?.[0];
|
|
|
|
response.canceled = canceled;
|
|
|
|
|
|
|
|
if (!response.filePath) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.success = true;
|
|
|
|
if (canceled) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.name = path.basename(response.filePath);
|
|
|
|
response.data = await fs.readFile(response.filePath);
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_CREDS, async (event) => {
|
|
|
|
return await getUrlAndTokenString();
|
|
|
|
});
|
2022-04-01 13:28:21 +00:00
|
|
|
|
2022-05-13 11:58:42 +00:00
|
|
|
ipcMain.handle(IPC_ACTIONS.DELETE_FILE, async (_, filePath) => {
|
|
|
|
await fs.unlink(filePath);
|
|
|
|
});
|
|
|
|
|
2022-05-19 18:25:01 +00:00
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_DB_LIST, async (_) => {
|
|
|
|
const files = await setAndGetCleanedConfigFiles();
|
|
|
|
return await getConfigFilesWithModified(files);
|
|
|
|
});
|
|
|
|
|
2022-05-27 07:50:14 +00:00
|
|
|
ipcMain.handle(IPC_ACTIONS.GET_ENV, async (_) => {
|
|
|
|
return {
|
|
|
|
isDevelopment: main.isDevelopment,
|
|
|
|
platform: process.platform,
|
|
|
|
version: app.getVersion(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-03-31 07:33:58 +00:00
|
|
|
/**
|
|
|
|
* Database Related Actions
|
|
|
|
*/
|
|
|
|
|
|
|
|
ipcMain.handle(
|
|
|
|
IPC_ACTIONS.DB_CREATE,
|
2022-04-18 06:42:56 +00:00
|
|
|
async (_, dbPath: string, countryCode: string) => {
|
2022-05-20 10:06:38 +00:00
|
|
|
return await getErrorHandledReponse(async function dbFunc() {
|
|
|
|
return await databaseManager.createNewDatabase(dbPath, countryCode);
|
|
|
|
});
|
2022-03-31 07:33:58 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ipcMain.handle(
|
|
|
|
IPC_ACTIONS.DB_CONNECT,
|
|
|
|
async (_, dbPath: string, countryCode?: string) => {
|
2022-05-20 10:06:38 +00:00
|
|
|
return await getErrorHandledReponse(async function dbFunc() {
|
|
|
|
return await databaseManager.connectToDatabase(dbPath, countryCode);
|
|
|
|
});
|
2022-03-31 07:33:58 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ipcMain.handle(
|
|
|
|
IPC_ACTIONS.DB_CALL,
|
|
|
|
async (_, method: DatabaseMethod, ...args: unknown[]) => {
|
2022-05-20 10:06:38 +00:00
|
|
|
return await getErrorHandledReponse(async function dbFunc() {
|
|
|
|
return await databaseManager.call(method, ...args);
|
|
|
|
});
|
2022-03-31 07:33:58 +00:00
|
|
|
}
|
|
|
|
);
|
2022-03-31 12:04:30 +00:00
|
|
|
|
2022-04-11 06:04:55 +00:00
|
|
|
ipcMain.handle(
|
|
|
|
IPC_ACTIONS.DB_BESPOKE,
|
|
|
|
async (_, method: string, ...args: unknown[]) => {
|
2022-05-20 10:06:38 +00:00
|
|
|
return await getErrorHandledReponse(async function dbFunc() {
|
|
|
|
return await databaseManager.callBespoke(method, ...args);
|
|
|
|
});
|
2022-04-11 06:04:55 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-03-31 12:04:30 +00:00
|
|
|
ipcMain.handle(IPC_ACTIONS.DB_SCHEMA, async (_) => {
|
2022-05-20 10:06:38 +00:00
|
|
|
return await getErrorHandledReponse(async function dbFunc() {
|
|
|
|
return await databaseManager.getSchemaMap();
|
|
|
|
});
|
2022-03-31 12:04:30 +00:00
|
|
|
});
|
2022-03-16 09:49:48 +00:00
|
|
|
}
|