thedesk/app/main/system.js

366 lines
9.2 KiB
JavaScript
Raw Normal View History

2019-06-15 19:52:28 +10:00
function system(mainWindow, dir, lang, dirname) {
const electron = require('electron')
const app = electron.app
const join = require('path').join
var Jimp = require('jimp')
const fs = require('fs')
var JSON5 = require('json5')
var ipc = electron.ipcMain
const clipboard = electron.clipboard
var tmp_img = join(app.getPath('userData'), 'tmp.png')
var ha_path = join(app.getPath('userData'), 'hardwareAcceleration')
var ua_path = join(app.getPath('userData'), 'useragent')
var lang_path = join(app.getPath('userData'), 'language')
var log_dir_path = join(app.getPath('userData'), 'logs')
2019-10-31 03:00:06 +11:00
//ログ
var today = new Date()
2019-10-31 03:00:06 +11:00
//今日のやつ
var todayStr = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
//昨日のやつ
today.setDate(today.getDate() - 1)
var yestStr = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
//一昨日のやつ
today.setDate(today.getDate() - 1)
var yest2Str = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
const BrowserWindow = electron.BrowserWindow
const dialog = electron.dialog
const os = require('os')
const language = require('../main/language.js')
2019-06-15 00:25:27 +10:00
//プラットフォーム
ipc.on('getPlatform', function(e, arg) {
2019-08-26 01:09:01 +10:00
try {
var gitHash = fs.readFileSync('git', 'utf8')
2019-10-31 02:30:26 +11:00
} catch {
var gitHash = null
2019-08-26 01:09:01 +10:00
}
e.sender.webContents.send('platform', [
process.platform,
process.arch,
process.version,
process.versions.chrome,
process.versions.electron,
gitHash
])
})
2019-04-03 14:59:29 +11:00
//言語
ipc.on('lang', function(e, arg) {
console.log('set:' + arg)
fs.writeFileSync(lang_path, arg)
e.sender.webContents.send('langres', arg)
})
2019-06-15 03:01:38 +10:00
//エクスポートのダイアログ
ipc.on('exportSettings', function(e, args) {
2019-10-31 02:30:26 +11:00
dialog.showSaveDialog(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
title: 'Export',
properties: ['openFile', 'createDirectory'],
defaultPath: 'export.thedeskconfig.json5'
2019-10-31 02:30:26 +11:00
},
savedFiles => {
if (!savedFiles) {
return false
2019-10-31 02:30:26 +11:00
}
e.sender.webContents.send('exportSettingsFile', savedFiles)
2019-06-15 03:01:38 +10:00
}
)
})
2019-06-15 03:01:38 +10:00
//インポートのダイアログ
ipc.on('importSettings', function(e, args) {
2019-10-31 02:30:26 +11:00
dialog.showOpenDialog(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
title: 'Import',
properties: ['openFile'],
filters: [
{ name: 'TheDesk Config', extensions: ['thedeskconfig', 'thedeskconfigv2', 'json5'] }
]
2019-10-31 02:30:26 +11:00
},
fileNames => {
console.log('imported from: ', fileNames)
2019-10-31 02:30:26 +11:00
if (!fileNames) {
return false
2019-10-31 02:30:26 +11:00
}
e.sender.webContents.send('config', JSON5.parse(fs.readFileSync(fileNames[0], 'utf8')))
2019-06-15 03:01:38 +10:00
}
)
})
2019-06-15 03:01:38 +10:00
//保存フォルダのダイアログ
ipc.on('savefolder', function(e, args) {
2019-10-31 02:30:26 +11:00
dialog.showOpenDialog(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
title: 'Save folder',
properties: ['openDirectory']
2019-10-31 02:30:26 +11:00
},
fileNames => {
e.sender.webContents.send('savefolder', fileNames[0])
2019-10-31 02:30:26 +11:00
}
)
})
2019-06-15 03:01:38 +10:00
//カスタムサウンドのダイアログ
ipc.on('customSound', function(e, arg) {
2019-10-31 02:30:26 +11:00
dialog.showOpenDialog(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
title: 'Custom sound',
properties: ['openFile'],
filters: [
{ name: 'Audio', extensions: ['mp3', 'aac', 'wav', 'flac', 'm4a'] },
{ name: 'All', extensions: ['*'] }
]
2019-10-31 02:30:26 +11:00
},
fileNames => {
e.sender.webContents.send('customSoundRender', [arg, fileNames[0]])
2019-10-31 02:30:26 +11:00
}
)
})
2019-06-15 02:32:59 +10:00
2019-05-19 17:39:30 +10:00
//ハードウェアアクセラレーションの無効化
ipc.on('ha', function(e, arg) {
if (arg == 'true') {
fs.writeFileSync(ha_path, arg)
2019-04-03 14:59:29 +11:00
} else {
fs.unlink(ha_path, function(err) {})
2019-04-03 14:59:29 +11:00
}
app.relaunch()
app.exit()
})
ipc.on('ua', function(e, arg) {
if (arg == '') {
fs.unlink(ua_path, function(err) {})
2019-10-26 03:16:33 +11:00
} else {
fs.writeFileSync(ua_path, arg)
2019-10-26 03:16:33 +11:00
}
app.relaunch()
app.exit()
})
2019-04-03 14:59:29 +11:00
ipc.on('quit', (e, args) => {
app.quit()
})
ipc.on('about', (e, args) => {
about()
})
2019-04-03 14:59:29 +11:00
function about() {
var ver = app.getVersion()
2019-04-03 14:59:29 +11:00
var window = new BrowserWindow({
2019-05-19 20:24:27 +10:00
webPreferences: {
2019-06-15 19:52:28 +10:00
webviewTag: false,
2019-08-26 22:06:07 +10:00
nodeIntegration: false,
contextIsolation: true,
preload: join(dirname, 'js', 'platform', 'preload.js')
2019-05-19 20:24:27 +10:00
},
2019-04-03 14:59:29 +11:00
width: 300,
2019-08-26 22:06:07 +10:00
height: 500,
2019-10-31 02:30:26 +11:00
transparent: false, // ウィンドウの背景を透過
frame: false, // 枠の無いウィンドウ
resizable: false
})
window.loadURL(dir + '/about.html?ver=' + ver)
return 'true'
2019-04-03 14:59:29 +11:00
}
ipc.on('nano', function(e, x, y) {
var nano_info_path = join(app.getPath('userData'), 'nano-window-position.json')
var window_pos
2019-04-03 14:59:29 +11:00
try {
window_pos = JSON.parse(fs.readFileSync(nano_info_path, 'utf8'))
2019-04-03 14:59:29 +11:00
} catch (e) {
window_pos = [0, 0] // デフォルトバリュー
2019-04-03 14:59:29 +11:00
}
var nanowindow = new BrowserWindow({
2019-05-19 20:24:27 +10:00
webPreferences: {
2019-06-15 19:52:28 +10:00
webviewTag: false,
nodeIntegration: false,
contextIsolation: true,
preload: join(dirname, 'js', 'platform', 'preload.js')
2019-05-19 20:24:27 +10:00
},
2019-04-03 14:59:29 +11:00
width: 350,
2019-10-14 02:28:10 +11:00
height: 140,
2019-10-31 02:30:26 +11:00
transparent: false, // ウィンドウの背景を透過
frame: false, // 枠の無いウィンドウ
resizable: false
})
nanowindow.loadURL(dir + '/nano.html')
nanowindow.setAlwaysOnTop(true)
2019-10-14 02:28:10 +11:00
//nanowindow.toggleDevTools()
nanowindow.setPosition(window_pos[0], window_pos[1])
nanowindow.on('close', function() {
fs.writeFileSync(nano_info_path, JSON.stringify(nanowindow.getPosition()))
})
return true
})
var cbTimer1
ipc.on('startmem', (e, arg) => {
event = e.sender
cbTimer1 = setInterval(mems, 1000)
})
2019-06-16 02:34:49 +10:00
function mems() {
var mem = os.totalmem() - os.freemem()
2019-06-16 02:34:49 +10:00
if (mainWindow) {
event.webContents.send('memory', [mem, os.cpus()[0].model, os.totalmem()])
2019-06-16 02:34:49 +10:00
}
}
ipc.on('endmem', (e, arg) => {
2019-04-03 14:59:29 +11:00
if (cbTimer1) {
clearInterval(cbTimer1)
2019-04-03 14:59:29 +11:00
}
})
2019-04-03 14:59:29 +11:00
ipc.on('export', (e, args) => {
fs.writeFileSync(args[0], JSON5.stringify(args[1]))
e.sender.webContents.send('exportAllComplete', '')
})
2019-04-03 14:59:29 +11:00
//フォント
function object_array_sort(data, key, order, fn) {
//デフォは降順(DESC)
var num_a = -1
var num_b = 1
2019-04-03 14:59:29 +11:00
if (order === 'asc') {
2019-10-31 02:30:26 +11:00
//指定があれば昇順(ASC)
num_a = 1
num_b = -1
2019-04-03 14:59:29 +11:00
}
2019-10-31 02:30:26 +11:00
data = data.sort(function(a, b) {
var x = a[key]
var y = b[key]
if (x > y) return num_a
if (x < y) return num_b
return 0
})
2019-04-03 14:59:29 +11:00
//重複排除
var arrObj = {}
2019-04-03 14:59:29 +11:00
for (var i = 0; i < data.length; i++) {
arrObj[data[i]['family']] = data[i]
2019-04-03 14:59:29 +11:00
}
data = []
2019-04-03 14:59:29 +11:00
for (var key in arrObj) {
data.push(arrObj[key])
2019-04-03 14:59:29 +11:00
}
fn(data) // ソート後の配列を返す
2019-04-03 14:59:29 +11:00
}
ipc.on('fonts', (e, arg) => {
const fm = require('font-manager')
var fonts = fm.getAvailableFontsSync()
object_array_sort(fonts, 'family', 'asc', function(fonts_sorted) {
e.sender.webContents.send('font-list', fonts_sorted)
})
})
2019-10-14 02:52:33 +11:00
//コピー
ipc.on('copy', (e, arg) => {
clipboard.writeText(arg)
})
2019-10-31 02:30:26 +11:00
//ログ
ipc.on('log', (e, arg) => {
var today = new Date()
var todayStr = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate()
var log_path = join(log_dir_path, todayStr + '.log')
fs.appendFile(log_path, '\n' + arg, function(err) {
2019-10-31 02:30:26 +11:00
if (err) {
throw err
2019-10-31 02:30:26 +11:00
}
})
})
ipc.on('getLogs', (e, arg) => {
var logs = ''
var todayLog = ''
var yestLog = ''
var yest2Log = ''
2019-10-31 03:00:06 +11:00
fs.readdir(log_dir_path, function(err, files) {
if (err) throw err
2019-10-31 03:00:06 +11:00
files.filter(function(file) {
2019-11-04 03:10:06 +11:00
if (file == todayStr) {
todayLog = fs.readFileSync(join(log_dir_path, file), 'utf8')
2019-10-31 03:00:06 +11:00
}
2019-11-04 03:10:06 +11:00
if (file == yestStr) {
yestLog = logs + fs.readFileSync(join(log_dir_path, file), 'utf8')
2019-11-04 03:10:06 +11:00
}
if (file == yest2Str) {
yest2Log = fs.readFileSync(join(log_dir_path, file), 'utf8')
2019-11-04 03:10:06 +11:00
}
logs = todayLog + yestLog + yest2Log
})
logs = yest2Log + yestLog + todayLog
e.sender.webContents.send('logData', logs)
})
})
2019-10-31 02:30:26 +11:00
//起動時ログディレクトリ存在確認と作成、古ログ削除
fs.access(log_dir_path, fs.constants.R_OK | fs.constants.W_OK, error => {
if (error) {
if (error.code === 'ENOENT') {
fs.mkdirSync(log_dir_path)
2019-10-31 02:30:26 +11:00
} else {
return
2019-10-31 02:30:26 +11:00
}
} else {
fs.readdir(log_dir_path, function(err, files) {
if (err) throw err
2019-10-31 02:30:26 +11:00
files.filter(function(file) {
if (file != todayStr && file != yestStr && file != yest2Str) {
fs.unlinkSync(join(log_dir_path, file))
2019-10-31 02:30:26 +11:00
}
})
})
2019-10-31 02:30:26 +11:00
}
})
2020-01-06 00:38:58 +11:00
var $ = require('nodobjc')
$.framework('Foundation')
$.framework('AppKit')
var GetSongs = $.NSObject.extend('Delegate')
GetSongs.addMethod('getMySongs:', 'v@:@', function(self, _cmd, notif) {
var userInfo = notif('userInfo')
var keys = userInfo('keyEnumerator')
var key
var song = {}
while ((key = keys('nextObject'))) {
var value = userInfo('objectForKey', key)
song[key.toString()] = value.toString()
}
console.log(song)
})
GetSongs.register()
var gs = GetSongs('alloc')('init')
var nc = $.NSDistributedNotificationCenter('defaultCenter')
var selector = $.NSSelectorFromString($('getMySongs:'))
var notification = $('com.apple.iTunes.playerInfo')
nc('addObserver', gs, 'selector', 'getMySongs:', 'name', notification, 'object', null)
2020-01-06 00:42:58 +11:00
var apps = $.NSApplication('sharedApplication')
2020-01-06 00:38:58 +11:00
function runLoop() {
var pool = $.NSAutoreleasePool('alloc')('init')
try {
2020-01-06 00:42:58 +11:00
apps(
2020-01-06 00:38:58 +11:00
'nextEventMatchingMask',
$.NSAnyEventMask.toString(),
'untilDate',
$.NSDate('distantFuture'),
'inMode',
$.NSDefaultRunLoopMode,
'dequeue',
1
)
} catch (e) {
console.error('run loop error', e.message)
}
pool('drain')
setTimeout(runLoop, 1000)
//process.nextTick(runLoop);
}
runLoop()
2019-04-03 14:59:29 +11:00
}
exports.system = system