Skip to content

Commit

Permalink
add import/export setting
Browse files Browse the repository at this point in the history
  • Loading branch information
liaotonglang committed Mar 28, 2023
1 parent e789e54 commit 69b5557
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sguala",
"productName": "sguala",
"version": "2.0.2",
"version": "2.0.3",
"description": "My Electron application description",
"main": ".webpack/main",
"scripts": {
Expand Down
59 changes: 58 additions & 1 deletion src/main/initIpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Config, ipcMain } from "electron";
import { Config, ipcMain, dialog } from "electron";
import conf, { Server, ServerGroup } from "./conf";
import { v4 as uuidv4 } from 'uuid';
import { promises as fs } from 'fs';
import { emptyServerStat, ServerStat, SshRemote } from "./sshRemote";

export function initIpc() {
Expand Down Expand Up @@ -263,4 +264,60 @@ export function initIpc() {
await conf.updateFetchInterval(interval);
});

ipcMain.handle('conf-export-settings', async (event) => {
// dialog show save file
const result = await dialog.showSaveDialog({
title: 'Export Settings',
defaultPath: 'sguala.json',
filters: [
{ name: 'JSON', extensions: ['json'] },
],
});
const filePath = result.filePath;
if (!filePath) {
console.log('cancel export');
return;
}
console.log('export settings to', filePath);
const c = conf.get();
const data = JSON.stringify(c, null, 2);
await fs.writeFile(filePath, data);
});

ipcMain.handle('conf-import-settings', async (event) => {
// dialog show open file
const result = await dialog.showOpenDialog({
title: 'Import Settings',
filters: [
{ name: 'JSON', extensions: ['json'] },
],
});
const filePath = result.filePaths[0];
if (!filePath) {
console.log('cancel import');
return;
}
console.log('import settings from', filePath);
const data = await fs.readFile(filePath);
const nc = JSON.parse(data.toString());
const oc = conf.get();
// merge
for (const g of nc.groups) {
const og = oc.groups.find(gg => gg.name == g.name);
if (og) {
// merge group, add server from g to og if not exists
for (const s of g.servers) {
const os = og.servers.find(ss => ss.name == s.name);
if (!os) {
og.servers.push(s);
}
}
} else {
// add group
oc.groups.push(g);
}
}
// save
await conf.store(oc);
});
}
10 changes: 9 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ contextBridge.exposeInMainWorld('main', {

updateInterval: async (interval: number) => {
return await ipcRenderer.invoke('update-fetch-interval', interval);
}
},

exportSettings: async () => {
return await ipcRenderer.invoke('conf-export-settings');
},

importSettings: async (settings: any) => {
return await ipcRenderer.invoke('conf-import-settings', settings);
},
},

remote: {
Expand Down
21 changes: 17 additions & 4 deletions src/view/SettingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Box from '@mui/material/Box';
import Link from '@mui/material/Link';

import { useTranslation } from 'react-i18next';
import { Divider } from '@mui/material';
import { Button, Divider } from '@mui/material';
import Typography from '@mui/material/Typography';

export function SettingPage() {
Expand All @@ -12,6 +12,15 @@ export function SettingPage() {
const sgualaRepo = "https://github.com/zltl/sguala";
const gpl3Url = "https://www.gnu.org/licenses/gpl-3.0.en.html";


const exportSettings = async () => {
main.conf.exportSettings();
};

const importSettings = async () => {
main.conf.importSettings();
}

return (
<Box>
<Link href={sgualaRepo}
Expand All @@ -28,9 +37,13 @@ export function SettingPage() {
</Link>.
<Divider />

<Typography>
没啥可设置的。
</Typography>
<Button onClick={() => exportSettings()} variant="contained">
{t('Export Settings')}
</Button>

<Button onClick={() => importSettings()} variant="contained">
{t('Import Settings')}
</Button>

</Box>
);
Expand Down
4 changes: 3 additions & 1 deletion src/view/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
"Use password instead of ssh-key": "使用密码登录",
"Ssh private key pem": "SSH 私钥内容",
"Upload": "上传",
"Download": "下载"
"Download": "下载",
"Export Settings": "导出设置",
"Import Settings": "导入设置"
}

0 comments on commit 69b5557

Please sign in to comment.