Skip to content

Commit

Permalink
Merge pull request #564 from ipfs-shipyard/user-options
Browse files Browse the repository at this point in the history
User options and auto launch
  • Loading branch information
hacdias authored Dec 26, 2017
2 parents 531da6e + 864873f commit caa0b70
Show file tree
Hide file tree
Showing 22 changed files with 453 additions and 112 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "IPFS Native Application",
"main": "src/index.js",
"dependencies": {
"auto-launch": "^5.0.5",
"electron-compile": "^6.4.2",
"electron-is-dev": "^0.3.0",
"electron-menubar": "^1.0.1",
Expand Down
41 changes: 23 additions & 18 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import fs from 'fs'
import os from 'os'
import isDev from 'electron-is-dev'
import {app, dialog} from 'electron'

import FileHistory from './utils/file-history'
import KeyValueStore from './utils/key-value-store'

// Set up what to do on Uncaught Exceptions
process.on('uncaughtException', (error) => {
Expand Down Expand Up @@ -41,21 +43,13 @@ const ipfsAppData = (() => {
return p
})()

const ipfsPathFile = path.join(ipfsAppData, 'app-node-path')
const ipfsFileHistoryFile = path.join(ipfsAppData, 'file-history.json')

const ipfsPath = (() => {
let pathIPFS
const fileHistory = new FileHistory(path.join(ipfsAppData, 'file-history.json'))
const settingsStore = new KeyValueStore(path.join(ipfsAppData, 'config.json'))

if (fs.existsSync(ipfsPathFile)) {
pathIPFS = fs.readFileSync(ipfsPathFile, 'utf-8')
} else {
pathIPFS = path.join(process.env.IPFS_PATH ||
(process.env.HOME || process.env.USERPROFILE), '.ipfs')
}

return pathIPFS
})()
if (!settingsStore.get('ipfsPath')) {
const p = path.join(process.env.IPFS_PATH || (process.env.HOME || process.env.USERPROFILE), '.ipfs')
settingsStore.set('ipfsPath', p)
}

// Sets up the Logger
const logger = winston.createLogger({
Expand All @@ -82,11 +76,24 @@ if (isDev) {

export default {
logger: logger,
fileHistory: new FileHistory(ipfsFileHistoryFile),
fileHistory: fileHistory,
settingsStore: settingsStore,
logo: {
ice: logo('ice'),
black: logo('black')
},
// Will be replaced by a BrowserWindow instance.
settingsWindow: {
index: `file://${__dirname}/views/settings.html`,
title: 'IPFS Station Settings',
show: false,
icon: logo('ice'),
backgroundColor: '#252525',
resizable: false,
width: 450,
height: 450
},
// Will be replaced by a Menubar instance.
menubar: {
index: `file://${__dirname}/views/menubar.html`,
icon: logo('black'),
Expand All @@ -103,7 +110,5 @@ export default {
webSecurity: false
}
}
},
ipfsPath: ipfsPath,
ipfsPathFile: ipfsPathFile
}
}
21 changes: 21 additions & 0 deletions src/controls/main/auto-launch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const AutoLaunch = require('auto-launch')

const settingsOption = 'autoLaunch'
const autoLauncher = new AutoLaunch({
name: 'IPFS Station'
})

export default function (opts) {
let {logger, settingsStore} = opts

let activate = (value) => {
if (value === true) {
autoLauncher.enable().catch(logger.error)
} else {
autoLauncher.disable().catch(logger.error)
}
}

activate(settingsStore.get(settingsOption))
settingsStore.on(settingsOption, activate)
}
6 changes: 4 additions & 2 deletions src/controls/main/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import autoLaunch from './auto-launch'
import fileHistory from './file-history'
import openFileDialog from './open-file-dialog'
import openSettings from './open-settings'
import openWebUI from './open-webui'
import settings from './settings'
import takeScreenshot from './take-screenshot'
import toggleSticky from './toggle-sticky'
import uploadFiles from './upload-files'

export default function (opts) {
autoLaunch(opts)
fileHistory(opts)
openFileDialog(opts)
openSettings(opts)
openWebUI(opts)
settings(opts)
takeScreenshot(opts)
toggleSticky(opts)
uploadFiles(opts)
Expand Down
2 changes: 1 addition & 1 deletion src/controls/main/open-file-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {uploadFiles} from '../utils'
function openFileDialog (opts, dir = false) {
let window = opts.window

return (event, callback) => {
return (event) => {
dialog.showOpenDialog(window, {
properties: [dir ? 'openDirectory' : 'openFile', 'multiSelections']
}, (files) => {
Expand Down
8 changes: 0 additions & 8 deletions src/controls/main/open-settings.js

This file was deleted.

57 changes: 57 additions & 0 deletions src/controls/main/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {join} from 'path'
import {shell, ipcMain, BrowserWindow} from 'electron'

function setupWindow (opts) {
const options = opts.settingsWindow
const window = new BrowserWindow(options)

window.setMenu(null)
window.loadURL(options.index)
window.on('close', (event) => {
event.preventDefault()
window.hide()
})

// Replace the window settings by the actual
// instance of BrowserWindow
opts.settingsWindow = window
}

function open (opts) {
const {settingsWindow, settingsStore} = opts

return () => {
settingsWindow.webContents.send('settings', settingsStore.toObject())
settingsWindow.show()
settingsWindow.focus()
}
}

function openNodeConfig (opts) {
const {settingsStore} = opts

return () => {
const path = settingsStore.get('ipfsPath')
shell.openExternal(join(path, 'config'))
}
}

function updateSettings (opts) {
const {settingsStore} = opts

return (event, settings) => {
for (const key in settings) {
if (settings.hasOwnProperty(key)) {
settingsStore.set(key, settings[key])
}
}
}
}

export default function (opts) {
setupWindow(opts)

ipcMain.on('open-settings', open(opts))
ipcMain.on('update-settings', updateSettings(opts))
ipcMain.on('open-node-settings', openNodeConfig(opts))
}
26 changes: 21 additions & 5 deletions src/controls/main/take-screenshot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {clipboard, ipcMain, globalShortcut} from 'electron'

const settingsOption = 'screenshotShortcut'
const shortcut = 'CommandOrControl+Alt+S'

function handleScreenshot (opts) {
let {logger, fileHistory, ipfs} = opts

Expand All @@ -8,6 +11,11 @@ function handleScreenshot (opts) {

logger.info('Screenshot taken')

if (!ipfs()) {
logger.info('Daemon not running. Aborting screenshot upload.')
return
}

ipfs()
.add(Buffer.from(base64Data, 'base64'))
.then((res) => {
Expand All @@ -23,12 +31,20 @@ function handleScreenshot (opts) {
}

export default function (opts) {
let {send, logger} = opts
let {send, logger, settingsStore} = opts

globalShortcut.register('CommandOrControl+Alt+S', () => {
logger.info('Taking Screenshot')
send('screenshot')
})
let activate = (value) => {
if (value === true) {
globalShortcut.register(shortcut, () => {
logger.info('Taking Screenshot')
send('screenshot')
})
} else {
globalShortcut.unregister(shortcut)
}
}

activate(settingsStore.get(settingsOption))
settingsStore.on(settingsOption, activate)
ipcMain.on('screenshot', handleScreenshot(opts))
}
Loading

0 comments on commit caa0b70

Please sign in to comment.