Skip to content

Commit 69b5557

Browse files
author
liaotonglang
committed
add import/export setting
1 parent e789e54 commit 69b5557

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sguala",
33
"productName": "sguala",
4-
"version": "2.0.2",
4+
"version": "2.0.3",
55
"description": "My Electron application description",
66
"main": ".webpack/main",
77
"scripts": {

src/main/initIpc.ts

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Config, ipcMain } from "electron";
1+
import { Config, ipcMain, dialog } from "electron";
22
import conf, { Server, ServerGroup } from "./conf";
33
import { v4 as uuidv4 } from 'uuid';
4+
import { promises as fs } from 'fs';
45
import { emptyServerStat, ServerStat, SshRemote } from "./sshRemote";
56

67
export function initIpc() {
@@ -263,4 +264,60 @@ export function initIpc() {
263264
await conf.updateFetchInterval(interval);
264265
});
265266

267+
ipcMain.handle('conf-export-settings', async (event) => {
268+
// dialog show save file
269+
const result = await dialog.showSaveDialog({
270+
title: 'Export Settings',
271+
defaultPath: 'sguala.json',
272+
filters: [
273+
{ name: 'JSON', extensions: ['json'] },
274+
],
275+
});
276+
const filePath = result.filePath;
277+
if (!filePath) {
278+
console.log('cancel export');
279+
return;
280+
}
281+
console.log('export settings to', filePath);
282+
const c = conf.get();
283+
const data = JSON.stringify(c, null, 2);
284+
await fs.writeFile(filePath, data);
285+
});
286+
287+
ipcMain.handle('conf-import-settings', async (event) => {
288+
// dialog show open file
289+
const result = await dialog.showOpenDialog({
290+
title: 'Import Settings',
291+
filters: [
292+
{ name: 'JSON', extensions: ['json'] },
293+
],
294+
});
295+
const filePath = result.filePaths[0];
296+
if (!filePath) {
297+
console.log('cancel import');
298+
return;
299+
}
300+
console.log('import settings from', filePath);
301+
const data = await fs.readFile(filePath);
302+
const nc = JSON.parse(data.toString());
303+
const oc = conf.get();
304+
// merge
305+
for (const g of nc.groups) {
306+
const og = oc.groups.find(gg => gg.name == g.name);
307+
if (og) {
308+
// merge group, add server from g to og if not exists
309+
for (const s of g.servers) {
310+
const os = og.servers.find(ss => ss.name == s.name);
311+
if (!os) {
312+
og.servers.push(s);
313+
}
314+
}
315+
} else {
316+
// add group
317+
oc.groups.push(g);
318+
}
319+
}
320+
// save
321+
await conf.store(oc);
322+
});
266323
}

src/preload.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ contextBridge.exposeInMainWorld('main', {
7575

7676
updateInterval: async (interval: number) => {
7777
return await ipcRenderer.invoke('update-fetch-interval', interval);
78-
}
78+
},
79+
80+
exportSettings: async () => {
81+
return await ipcRenderer.invoke('conf-export-settings');
82+
},
83+
84+
importSettings: async (settings: any) => {
85+
return await ipcRenderer.invoke('conf-import-settings', settings);
86+
},
7987
},
8088

8189
remote: {

src/view/SettingPage.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Box from '@mui/material/Box';
33
import Link from '@mui/material/Link';
44

55
import { useTranslation } from 'react-i18next';
6-
import { Divider } from '@mui/material';
6+
import { Button, Divider } from '@mui/material';
77
import Typography from '@mui/material/Typography';
88

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

15+
16+
const exportSettings = async () => {
17+
main.conf.exportSettings();
18+
};
19+
20+
const importSettings = async () => {
21+
main.conf.importSettings();
22+
}
23+
1524
return (
1625
<Box>
1726
<Link href={sgualaRepo}
@@ -28,9 +37,13 @@ export function SettingPage() {
2837
</Link>.
2938
<Divider />
3039

31-
<Typography>
32-
没啥可设置的。
33-
</Typography>
40+
<Button onClick={() => exportSettings()} variant="contained">
41+
{t('Export Settings')}
42+
</Button>
43+
44+
<Button onClick={() => importSettings()} variant="contained">
45+
{t('Import Settings')}
46+
</Button>
3447

3548
</Box>
3649
);

src/view/locales/zh-CN/translation.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
"Use password instead of ssh-key": "使用密码登录",
1818
"Ssh private key pem": "SSH 私钥内容",
1919
"Upload": "上传",
20-
"Download": "下载"
20+
"Download": "下载",
21+
"Export Settings": "导出设置",
22+
"Import Settings": "导入设置"
2123
}

0 commit comments

Comments
 (0)