Skip to content

Commit 2be4908

Browse files
committed
Merge branch 'feature/change-data-dir' into develop
2 parents aab1e24 + 94d48fe commit 2be4908

File tree

17 files changed

+208
-54
lines changed

17 files changed

+208
-54
lines changed

src/common/i18n/languages/en.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
auto_refresh: 'Auto refresh',
1414
btn_cancel: 'Cancel',
1515
btn_ok: 'OK',
16+
change: 'Change',
1617
check_update: 'Check update',
1718
choice_mode: 'Choice mode',
1819
choice_mode_default: 'Default',
@@ -99,6 +100,8 @@ export default {
99100
move_items_to_trashcan: 'Move {0} items to trashcan',
100101
move_to_trashcan: 'Move to trashcan',
101102
need_to_relaunch: 'Need to relaunch',
103+
need_to_relaunch_after_setting_changed:
104+
'The setting has been changed and will take effect after the app is restarted.',
102105
never: 'Never',
103106
new: 'New',
104107
new_version_found: 'New version found',
@@ -125,6 +128,9 @@ export default {
125128
replace: 'Replace',
126129
replace_all: 'Replace all',
127130
replace_history: 'Replace history',
131+
reset: 'Reset',
132+
reset_data_dir_confirm:
133+
'Are you sure you want to restore the data folder to the default address ({0})?',
128134
reset_zoom: 'Reset zoom',
129135
search: 'Search',
130136
select_all: 'Select all',

src/common/i18n/languages/fr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
auto_refresh: 'Rafraîchissement automatique',
1414
btn_cancel: 'Annuler',
1515
btn_ok: 'OK',
16+
change: 'Changer',
1617
check_update: 'Vérifier les mises à jour',
1718
choice_mode: 'Choice mode',
1819
choice_mode_default: 'Défaut',
@@ -99,6 +100,8 @@ export default {
99100
move_items_to_trashcan: 'Déplacer {0} éléments dans la corbeille',
100101
move_to_trashcan: 'Déplacer dans la corbeille',
101102
need_to_relaunch: 'Besoin de redémarrer',
103+
need_to_relaunch_after_setting_changed:
104+
"Le paramètre a été modifié et prendra effet après le redémarrage de l'application.",
102105
never: 'Jamais',
103106
new: 'Nouveau',
104107
new_version_found: 'Nouvelle version trouvée',
@@ -125,6 +128,9 @@ export default {
125128
replace: 'Remplacer',
126129
replace_all: 'Tout remplacer',
127130
replace_history: "Remplacer l'historique",
131+
reset: 'Réinitialiser',
132+
reset_data_dir_confirm:
133+
"Êtes-vous sûr de vouloir réinitialiser le dossier de données à l'adresse par défaut?({0})?",
128134
reset_zoom: 'Réinitialiser le zoom',
129135
search: 'Rechercher',
130136
select_all: 'Tout sélectionner',

src/common/i18n/languages/zh.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const lang: LanguageDict = {
1515
auto_refresh: '自动刷新',
1616
btn_cancel: '取消',
1717
btn_ok: '确定',
18+
change: '更改',
1819
check_update: '检查更新',
1920
choice_mode: '选择模式',
2021
choice_mode_default: '默认',
@@ -98,6 +99,7 @@ const lang: LanguageDict = {
9899
move_items_to_trashcan: '移动 {0} 项到回收站',
99100
move_to_trashcan: '移到回收站',
100101
need_to_relaunch: '需要重启',
102+
need_to_relaunch_after_setting_changed: '设置已更改,应用重启后生效。',
101103
never: '从不',
102104
new: '新建',
103105
new_version_found: '发现新版本',
@@ -124,6 +126,8 @@ const lang: LanguageDict = {
124126
replace: '替换',
125127
replace_all: '替换所有',
126128
replace_history: '替换历史',
129+
reset: '重置',
130+
reset_data_dir_confirm: '确定要把数据文件夹重置为默认地址吗?({0})?',
127131
reset_zoom: '重置缩放',
128132
search: '搜索',
129133
select_all: '全选',

src/main/actions/cmd/changeDataDir.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @author: oldj
3+
* @homepage: https://oldj.net
4+
*/
5+
6+
import {
7+
app,
8+
BrowserWindow,
9+
dialog,
10+
OpenDialogOptions,
11+
OpenDialogReturnValue,
12+
} from 'electron'
13+
import { localdb } from '@main/data'
14+
import getDataFolder, { getDefaultDataDir } from '@main/libs/getDataDir'
15+
import getI18N from '@main/core/getI18N'
16+
import { IActionFunc } from '@root/main/types'
17+
18+
export default async function (
19+
this: IActionFunc,
20+
to_default?: boolean,
21+
): Promise<string | undefined> {
22+
let { sender } = this
23+
let { lang } = await getI18N()
24+
let current_dir = getDataFolder()
25+
let dir: string = ''
26+
27+
if (to_default) {
28+
dir = getDefaultDataDir()
29+
} else {
30+
let parent = BrowserWindow.fromWebContents(sender)
31+
if (parent?.isFullScreen()) {
32+
parent?.setFullScreen(false)
33+
}
34+
35+
let options: OpenDialogOptions = {
36+
// title: '选择数据目录',
37+
defaultPath: current_dir,
38+
properties: ['openDirectory', 'createDirectory'],
39+
}
40+
41+
let r: OpenDialogReturnValue
42+
43+
if (parent) {
44+
r = await dialog.showOpenDialog(parent, options)
45+
} else {
46+
r = await dialog.showOpenDialog(options)
47+
}
48+
49+
if (r.canceled) {
50+
return
51+
}
52+
53+
dir = r.filePaths[0]
54+
}
55+
56+
if (!dir || dir === current_dir) {
57+
return
58+
}
59+
60+
await localdb.dict.local.set('data_dir', dir)
61+
dialog.showMessageBoxSync({
62+
message: lang.need_to_relaunch_after_setting_changed,
63+
})
64+
app.relaunch()
65+
app.exit(0)
66+
67+
return dir
68+
}

src/main/actions/getDataDir.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @author: oldj
3+
* @homepage: https://oldj.net
4+
*/
5+
6+
import getDataDir from '@main/libs/getDataDir'
7+
8+
export default async () => getDataDir()

src/main/actions/getDataFolder.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/actions/getDefaultDataDir.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @author: oldj
3+
* @homepage: https://oldj.net
4+
*/
5+
6+
import { getDefaultDataDir } from '@main/libs/getDataDir'
7+
8+
export default async () => getDefaultDataDir()

src/main/actions/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
export { default as ping } from './ping'
88

9-
export { default as getDataFolder } from './getDataFolder'
109
export { default as getBasicData } from './getBasicData'
10+
export { default as getDataDir } from './getDataDir'
11+
export { default as getDefaultDataDir } from './getDefaultDataDir'
1112

1213
export { default as configGet } from './config/get'
1314
export { default as configSet } from './config/set'
@@ -40,6 +41,7 @@ export { default as cmdDeleteHistory } from './cmd/deleteHistory'
4041
export { default as cmdClearHistory } from './cmd/clearHistory'
4142
export { default as cmdFocusMainWindow } from './cmd/focusMainWindow'
4243
export { default as cmdToggleDevTools } from './cmd/toggleDevTools'
44+
export { default as cmdChangeDataDir } from './cmd/changeDataDir'
4345

4446
export { default as openUrl } from './openUrl'
4547
export { default as showItemInFolder } from './showItemInFolder'

src/main/actions/migrate/checkIfMigration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @homepage: https://oldj.net
66
*/
77

8-
import getDataFolder from '@main/libs/getDataFolder'
8+
import getDataFolder from '@main/libs/getDataDir'
99
import { isDir } from '@main/utils/fs2'
1010
import * as fs from 'fs'
1111
import * as path from 'path'

src/main/actions/migrate/migrateData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// migrate data from v3 to v4
88

99
import importV3Data from '@main/actions/migrate/importV3Data'
10-
import getDataFolder from '@main/libs/getDataFolder'
10+
import getDataFolder from '@main/libs/getDataDir'
1111
import { IHostsBasicData, VersionType } from '@root/common/data'
1212
import { cleanHostsList } from '@root/common/hostsFn'
1313
import version from '@root/version.json'

src/main/core/message.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import * as actions from '@main/actions'
8-
import { ActionData } from '@main/types'
8+
import { ActionData, IActionFunc } from '@main/types'
99
import { ipcMain } from 'electron'
1010
import { EventEmitter } from 'events'
1111

@@ -79,8 +79,9 @@ ipcMain.on('x_action', async (e, action_data: ActionData) => {
7979
}
8080

8181
try {
82+
let obj: IActionFunc = { sender }
8283
// @ts-ignore
83-
let v = await fn(...params)
84+
let v = await fn.call(obj, ...params)
8485
sendBack(sender, callback, [null, v])
8586
} catch (e) {
8687
console.error(e)

src/main/data/index.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@
44
* @homepage: https://oldj.net
55
*/
66

7-
import getDataFolder from '@main/libs/getDataFolder'
8-
import { app } from 'electron'
97
import * as path from 'path'
108
import PotDb from 'potdb'
9+
import { app } from 'electron'
10+
import getDataFolder from '@main/libs/getDataDir'
11+
import getConfigFolder from '@main/libs/getConfigDir'
1112

12-
let swhdb: PotDb
13-
let cfgdb: PotDb
1413
let localdb: PotDb
14+
let cfgdb: PotDb
15+
let swhdb: PotDb
1516

16-
if (!global.swhdb) {
17-
let db_dir: string = path.join(getDataFolder(), 'data')
18-
swhdb = new PotDb(db_dir)
19-
console.log(`data db: ${swhdb.dir}`)
20-
global.swhdb = swhdb
17+
if (!global.localdb) {
18+
let db_dir: string = path.join(app.getPath('userData'), 'swh_local')
19+
localdb = new PotDb(db_dir)
20+
console.log(`local db: ${localdb.dir}`)
21+
global.localdb = localdb
2122
} else {
22-
swhdb = global.swhdb
23+
localdb = global.localdb
2324
}
2425

2526
if (!global.cfgdb) {
26-
let db_dir: string = path.join(getDataFolder(), 'config')
27+
let db_dir: string = path.join(getConfigFolder(), 'config')
2728
cfgdb = new PotDb(db_dir)
2829
console.log(`config db: ${cfgdb.dir}`)
2930
global.cfgdb = cfgdb
3031
} else {
3132
cfgdb = global.cfgdb
3233
}
3334

34-
if (!global.localdb) {
35-
let db_dir: string = path.join(app.getPath('userData'), 'swh_local')
36-
localdb = new PotDb(db_dir)
37-
console.log(`local db: ${localdb.dir}`)
38-
global.localdb = localdb
39-
} else {
40-
localdb = global.localdb
35+
async function getSwhDb(): Promise<PotDb> {
36+
if (!swhdb) {
37+
global.data_dir = await localdb.dict.local.get('data_dir')
38+
let db_dir: string = path.join(getDataFolder(), 'data')
39+
swhdb = new PotDb(db_dir)
40+
console.log(`data db: ${swhdb.dir}`)
41+
global.swhdb = swhdb
42+
}
43+
44+
return swhdb
4145
}
4246

43-
export { swhdb, cfgdb, localdb }
47+
export { localdb, cfgdb, swhdb, getSwhDb }

src/main/libs/getDataFolder.ts renamed to src/main/libs/getConfigDir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { homedir } from 'os'
99
export default (): string => {
1010
// todo data folder should be current working dir for portable version
1111

12-
return global.db_dir || path.join(homedir(), '.SwitchHosts')
12+
return path.join(homedir(), '.SwitchHosts')
1313
}

src/main/libs/getDataDir.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @author: oldj
3+
* @homepage: https://oldj.net
4+
*/
5+
6+
import * as path from 'path'
7+
import { homedir } from 'os'
8+
9+
export function getDefaultDataDir() {
10+
return path.join(homedir(), '.SwitchHosts')
11+
}
12+
13+
export default (): string => {
14+
// todo data folder should be current working dir for portable version
15+
16+
return global.data_dir || getDefaultDataDir()
17+
}

src/main/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import { app, BrowserWindow, ipcMain, nativeTheme } from 'electron'
1717
import windowStateKeeper from 'electron-window-state'
1818
import * as path from 'path'
1919
import { v4 as uuid4 } from 'uuid'
20+
import { getSwhDb } from '@main/data'
2021

2122
let win: BrowserWindow | null
2223

2324
const createWindow = async () => {
25+
await getSwhDb()
2426
const configs = await configAll()
2527

2628
let main_window_state = windowStateKeeper({

src/main/types.d.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import Tracer from '@main/libs/tracer'
88
import { LocaleName } from '@root/common/i18n'
99
import SwhDb from 'potdb'
10-
import { BrowserWindow } from 'electron'
10+
import { BrowserWindow, WebContents } from 'electron'
1111
import * as actions from './actions'
1212

1313
export type Actions = typeof actions
@@ -22,21 +22,21 @@ export interface IHostsWriteOptions {
2222
sudo_pswd?: string
2323
}
2424

25+
export interface IActionFunc {
26+
sender: WebContents
27+
}
28+
2529
declare global {
26-
namespace NodeJS {
27-
interface Global {
28-
db_dir?: string
29-
swhdb: SwhDb
30-
cfgdb: SwhDb
31-
localdb: SwhDb
32-
ua: string // user agent
33-
session_id: string // A random value, refreshed every time the app starts, used to identify different startup sessions.
34-
main_win: BrowserWindow
35-
find_win?: BrowserWindow | null
36-
last_path?: string // the last path opened by SwitchHosts
37-
tracer: Tracer
38-
is_will_quit?: boolean
39-
system_locale?: LocaleName
40-
}
41-
}
30+
var data_dir: string | undefined
31+
var swhdb: SwhDb
32+
var cfgdb: SwhDb
33+
var localdb: SwhDb
34+
var ua: string // user agent
35+
var session_id: string // A random value, refreshed every time the app starts, used to identify different startup sessions.
36+
var main_win: BrowserWindow
37+
var find_win: BrowserWindow | null
38+
var last_path: string // the last path opened by SwitchHosts
39+
var tracer: Tracer
40+
var is_will_quit: boolean
41+
var system_locale: LocaleName
4242
}

0 commit comments

Comments
 (0)