Skip to content

Commit

Permalink
feat(config): saving configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Feb 4, 2018
1 parent 4abe33f commit 7247044
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 75 deletions.
10 changes: 5 additions & 5 deletions src/app/admin-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export default class AdminPage extends Page {
this.loadSettings()
}
loadSettings() {
window.torrentSocket.emit('admin', window.customLoader((options) => {
window.torrentSocket.emit('config', window.customLoader((options) => {
this.options = options;
console.log(this.options)
this.forceUpdate();
}));
}
saveSettings() {
window.torrentSocket.emit('setAdmin', this.options)
window.torrentSocket.emit('setConfig', this.options)
this.forceUpdate()
}
render() {
Expand All @@ -32,12 +32,12 @@ export default class AdminPage extends Page {
<div className='column center w100p pad0-75'>
<Toggle
style={{marginTop: '10px'}}
label="Disable DHT scanning"
toggled={this.options.dhtDisabled}
label="Enabled network scanning"
toggled={this.options.indexer}
thumbSwitchedStyle={{backgroundColor: 'red'}}
trackSwitchedStyle={{backgroundColor: '#ff9d9d'}}
onToggle={(e, checked) => {
this.options.dhtDisabled = checked
this.options.indexer = checked
this.saveSettings()
}}
/>
Expand Down
6 changes: 6 additions & 0 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const { ipcRenderer, remote } = require('electron');
});


ipcRenderer.on('url', (event, url) => {
console.log('url', url)
router(url)
});


// Needed for onTouchTap
// http://stackoverflow.com/a/34015469/988941
injectTapEventPlugin();
Expand Down
2 changes: 1 addition & 1 deletion src/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ router('/DMCA', () => {
});


router('/admi5p', () => {
router('/config', () => {
//singleton
let pie = new PagesPie;
pie.open(AdminPage, {replace: 'all'});
Expand Down
3 changes: 2 additions & 1 deletion src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import url from "url";
import { app, Menu, ipcMain, Tray } from "electron";
import { devMenuTemplate } from "./menu/dev_menu_template";
import { editMenuTemplate } from "./menu/edit_menu_template";
import { settingsMenuTemplate } from "./menu/config_menu_template";
import createWindow from "./helpers/window";

// Special module holding environment variables which you declared
Expand All @@ -19,7 +20,7 @@ const { spawn, exec } = require('child_process')
const fs = require('fs')

const setApplicationMenu = () => {
const menus = [editMenuTemplate];
const menus = [editMenuTemplate, settingsMenuTemplate];
if (env.name !== "production") {
menus.push(devMenuTemplate);
}
Expand Down
38 changes: 19 additions & 19 deletions src/background/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { app } from 'electron'

let config = {
indexer: true,

Expand All @@ -15,14 +17,6 @@ let config = {
connectionLimit: 30
},

mysql: {
host : 'localhost',
user : 'btsearch',
password : 'pirateal100x',
database : 'btsearch',
connectionLimit: 40
},

spider: {
walkInterval: 5,
cpuLimit: 0,
Expand All @@ -48,28 +42,34 @@ let config = {
const fs = require('fs');
const debug = require('debug')('config')

let configPath = 'config.json'
if(app.getPath("userData") && app.getPath("userData").length > 0)
{
configPath = app.getPath("userData") + '/config.json'
}

const configProxy = new Proxy(config, {
set: (target, prop, value, receiver) => {
target[prop] = value
console.log('set op', configPath)

if(!fs.existsSync('config.json'))
fs.writeFileSync('config.json', '{}')
if(!fs.existsSync(configPath))
fs.writeFileSync(configPath, '{}')

fs.readFile('config.json', 'utf8', (err, data) => {
let obj = JSON.parse(data)
obj[prop] = value;
fs.writeFileSync('config.json', JSON.stringify(obj, null, 4), 'utf8');
debug('saving config.json:', prop, '=', value)
})
const data = fs.readFileSync(configPath)
let obj = JSON.parse(data)
obj[prop] = value;
fs.writeFileSync(configPath, JSON.stringify(obj, null, 4), 'utf8');
debug('saving config.json:', prop, '=', value)
}
})

config.load = () => {
debug('loading configuration')
if(fs.existsSync('config.json'))
if(fs.existsSync(configPath))
{
debug('finded configuration config.json')
const data = fs.readFileSync('config.json', 'utf8')
debug('finded configuration', configPath)
const data = fs.readFileSync(configPath, 'utf8')
const obj = JSON.parse(data);
for(let prop in obj)
{
Expand Down
14 changes: 14 additions & 0 deletions src/background/menu/config_menu_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { app, BrowserWindow } from "electron";

export const settingsMenuTemplate = {
label: "Settings",
submenu: [
{
label: "Main settings",
accelerator: "CmdOrCtrl+O",
click: () => {
console.log(BrowserWindow.getFocusedWindow().webContents.send('url', '/config'))
}
}
]
};
Loading

0 comments on commit 7247044

Please sign in to comment.