thedesk/app/main/system.js

351 lines
9.6 KiB
JavaScript
Raw Normal View History

const { shell } = require('electron')
2019-06-15 19:52:28 +10:00
function system(mainWindow, dir, lang, dirname) {
2020-05-11 19:42:52 +10:00
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
2022-10-10 17:08:57 +11:00
const nativeImage = electron.nativeImage
2020-05-11 19:42:52 +10:00
var tmp_img = join(app.getPath('userData'), 'tmp.png')
var ha_path = join(app.getPath('userData'), 'hardwareAcceleration')
2021-04-09 17:35:40 +10:00
var wv_path = join(app.getPath('userData'), 'webview')
2020-05-11 19:42:52 +10:00
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')
var frame_path = join(app.getPath('userData'), 'frame')
2019-10-31 03:00:06 +11:00
//ログ
2020-05-11 19:42:52 +10:00
var today = new Date()
2019-10-31 03:00:06 +11:00
//今日のやつ
2020-05-11 19:42:52 +10:00
var todayStr = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
//昨日のやつ
2020-05-11 19:42:52 +10:00
today.setDate(today.getDate() - 1)
var yestStr = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
//一昨日のやつ
2020-05-11 19:42:52 +10:00
today.setDate(today.getDate() - 1)
var yest2Str = today.getFullYear() + '' + (today.getMonth() + 1) + '' + today.getDate() + '.log'
2019-10-31 03:00:06 +11:00
2020-05-11 19:42:52 +10: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
//プラットフォーム
2020-05-11 19:42:52 +10:00
ipc.on('getPlatform', function (e, arg) {
2019-08-26 01:09:01 +10:00
try {
2020-05-11 19:42:52 +10:00
var gitHash = fs.readFileSync('git', 'utf8')
2019-10-31 02:30:26 +11:00
} catch {
2020-05-11 19:42:52 +10:00
var gitHash = null
2019-08-26 01:09:01 +10:00
}
e.sender.send('platform', [process.platform, process.arch, process.version, process.versions.chrome, process.versions.electron, gitHash])
2020-05-11 19:42:52 +10:00
})
2019-04-03 14:59:29 +11:00
//言語
2020-05-11 19:42:52 +10:00
ipc.on('lang', function (e, arg) {
console.log('set:' + arg)
fs.writeFileSync(lang_path, arg)
e.sender.send('langres', arg)
2020-05-11 19:42:52 +10:00
})
2019-06-15 03:01:38 +10:00
//エクスポートのダイアログ
2020-05-11 19:42:52 +10:00
ipc.on('exportSettings', function (e, args) {
let savedFiles = dialog.showSaveDialogSync(mainWindow, {
title: 'Export',
properties: ['openFile', 'createDirectory'],
defaultPath: 'export.thedeskconfig.json5',
})
if (!savedFiles) {
return false
}
e.sender.send('exportSettingsFile', savedFiles)
2020-05-11 19:42:52 +10:00
})
2019-06-15 03:01:38 +10:00
//インポートのダイアログ
2020-05-11 19:42:52 +10:00
ipc.on('importSettings', function (e, args) {
let fileNames = dialog.showOpenDialogSync(mainWindow, {
title: 'Import',
properties: ['openFile'],
filters: [{ name: 'TheDesk Config', extensions: ['thedeskconfig', 'thedeskconfigv2', 'json5'] }],
})
console.log('imported from: ', fileNames)
if (!fileNames) {
return false
}
e.sender.send('config', JSON5.parse(fs.readFileSync(fileNames[0], 'utf8')))
2020-05-11 19:42:52 +10:00
})
2019-06-15 03:01:38 +10:00
//保存フォルダのダイアログ
2020-05-11 19:42:52 +10:00
ipc.on('savefolder', function (e, args) {
let fileNames = dialog.showOpenDialogSync(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
2020-05-11 19:42:52 +10:00
title: 'Save folder',
properties: ['openDirectory'],
2019-10-31 02:30:26 +11:00
}
2020-05-11 19:42:52 +10:00
)
e.sender.send('savefolder', fileNames[0])
2020-05-11 19:42:52 +10:00
})
2019-06-15 03:01:38 +10:00
//カスタムサウンドのダイアログ
2020-05-11 19:42:52 +10:00
ipc.on('customSound', function (e, arg) {
let fileNames = dialog.showOpenDialogSync(
2019-12-14 03:54:40 +11:00
mainWindow,
2019-10-31 02:30:26 +11:00
{
2020-05-11 19:42:52 +10: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
}
2020-05-11 19:42:52 +10:00
)
e.sender.send('customSoundRender', [arg, fileNames[0]])
2020-05-11 19:42:52 +10:00
})
2019-06-15 02:32:59 +10:00
2019-05-19 17:39:30 +10:00
//ハードウェアアクセラレーションの無効化
2020-05-11 19:42:52 +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 {
2021-04-09 17:35:40 +10:00
fs.unlink(ha_path, function (err) { })
}
app.relaunch()
app.exit()
})
ipc.on('webview', function (e, arg) {
if (arg == 'true') {
fs.writeFileSync(wv_path, arg)
} else {
fs.unlink(wv_path, function (err) { })
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
app.relaunch()
app.exit()
})
//ユーザーエージェント
2020-05-11 19:42:52 +10:00
ipc.on('ua', function (e, arg) {
if (arg == '') {
2021-04-09 17:35:40 +10:00
fs.unlink(ua_path, function (err) { })
2019-10-26 03:16:33 +11:00
} else {
2020-05-11 19:42:52 +10:00
fs.writeFileSync(ua_path, arg)
2019-10-26 03:16:33 +11:00
}
2020-05-11 19:42:52 +10:00
app.relaunch()
app.exit()
})
//フレームのありなし
2020-05-11 19:42:52 +10:00
ipc.on('frameSet', function (e, arg) {
fs.writeFileSync(frame_path, arg)
app.relaunch()
app.exit()
})
//スクリーンリーダー
2020-05-11 19:42:52 +10:00
ipc.on('acsCheck', function (e, arg) {
if (app.accessibilitySupportEnabled) {
mainWindow.send('accessibility', 'true')
}
2020-05-11 19:42:52 +10:00
})
ipc.on('quit', (e, args) => {
app.quit()
})
ipc.on('about', (e, args) => {
about()
})
ipc.on('openUrl', function (event, arg) {
shell.openExternal(arg)
})
2019-04-03 14:59:29 +11:00
function about() {
2020-05-11 19:42:52 +10:00
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,
spellcheck: false,
sandbox: false,
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, // 枠の無いウィンドウ
2020-05-11 19:42:52 +10:00
resizable: false,
})
window.loadURL(dir + '/about.html?ver=' + ver)
return 'true'
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10: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 {
2020-05-11 19:42:52 +10:00
window_pos = JSON.parse(fs.readFileSync(nano_info_path, 'utf8'))
2019-04-03 14:59:29 +11:00
} catch (e) {
2020-05-11 19:42:52 +10:00
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,
sandbox: false,
2020-05-11 19:42:52 +10:00
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, // 枠の無いウィンドウ
2020-05-11 19:42:52 +10:00
resizable: false,
})
nanowindow.loadURL(dir + '/nano.html')
nanowindow.setAlwaysOnTop(true)
2020-09-07 01:14:51 +10:00
//nanowindow.toggleDevTools()
2020-05-11 19:42:52 +10:00
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() {
2020-05-11 19:42:52 +10:00
var mem = os.totalmem() - os.freemem()
if (mainWindow && event) {
event.send('memory', [mem, os.cpus()[0].model, os.totalmem(), os.cpus().length, os.uptime()])
2019-06-16 02:34:49 +10:00
}
}
2020-05-11 19:42:52 +10:00
ipc.on('endmem', (e, arg) => {
2019-04-03 14:59:29 +11:00
if (cbTimer1) {
2020-05-11 19:42:52 +10:00
clearInterval(cbTimer1)
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
})
2019-04-03 14:59:29 +11:00
2020-05-11 19:42:52 +10:00
ipc.on('export', (e, args) => {
fs.writeFileSync(args[0], JSON5.stringify(args[1]))
e.sender.send('exportAllComplete', '')
2020-05-11 19:42:52 +10:00
})
2019-04-03 14:59:29 +11:00
//フォント
function object_array_sort(data, key, order, fn) {
//デフォは降順(DESC)
2020-05-11 19:42:52 +10:00
var num_a = -1
var num_b = 1
2019-04-03 14:59:29 +11:00
2020-05-11 19:42:52 +10:00
if (order === 'asc') {
2019-10-31 02:30:26 +11:00
//指定があれば昇順(ASC)
2020-05-11 19:42:52 +10:00
num_a = 1
num_b = -1
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10: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
//重複排除
2020-05-11 19:42:52 +10:00
var arrObj = {}
2019-04-03 14:59:29 +11:00
for (var i = 0; i < data.length; i++) {
2020-05-11 19:42:52 +10:00
arrObj[data[i]['family']] = data[i]
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
data = []
2019-04-03 14:59:29 +11:00
for (var key in arrObj) {
2020-05-11 19:42:52 +10:00
data.push(arrObj[key])
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
fn(data) // ソート後の配列を返す
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
ipc.on('fonts', (e, arg) => {
var SystemFonts = require('system-font-families').default
var fm = new SystemFonts()
const fontList = fm.getFontsSync()
e.sender.send('font-list', fontList)
2020-05-11 19:42:52 +10:00
})
2019-10-14 02:52:33 +11:00
//コピー
2020-05-11 19:42:52 +10:00
ipc.on('copy', (e, arg) => {
clipboard.writeText(arg)
})
2022-10-10 17:08:57 +11:00
ipc.on('copyBinary', (e, arg) => {
const ni = nativeImage.createFromDataURL(arg)
clipboard.writeImage(ni)
})
2019-10-31 02:30:26 +11:00
//ログ
2020-05-11 19:42:52 +10: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) {
2020-05-11 19:42:52 +10:00
throw err
2019-10-31 02:30:26 +11:00
}
2020-05-11 19:42:52 +10:00
})
})
ipc.on('getLogs', (e, arg) => {
var logs = ''
var todayLog = ''
var yestLog = ''
var yest2Log = ''
fs.readdir(log_dir_path, function (err, files) {
if (err) throw err
files.filter(function (file) {
2019-11-04 03:10:06 +11:00
if (file == todayStr) {
2020-05-11 19:42:52 +10:00
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) {
2020-05-11 19:42:52 +10:00
yestLog = logs + fs.readFileSync(join(log_dir_path, file), 'utf8')
2019-11-04 03:10:06 +11:00
}
if (file == yest2Str) {
2020-05-11 19:42:52 +10:00
yest2Log = fs.readFileSync(join(log_dir_path, file), 'utf8')
2019-11-04 03:10:06 +11:00
}
2020-05-11 19:42:52 +10:00
logs = todayLog + yestLog + yest2Log
})
logs = yest2Log + yestLog + todayLog
e.sender.send('logData', logs)
2020-05-11 19:42:52 +10:00
})
})
2019-10-31 02:30:26 +11:00
//起動時ログディレクトリ存在確認と作成、古ログ削除
2020-05-11 19:42:52 +10:00
fs.access(log_dir_path, fs.constants.R_OK | fs.constants.W_OK, (error) => {
2019-10-31 02:30:26 +11:00
if (error) {
2020-05-11 19:42:52 +10:00
if (error.code === 'ENOENT') {
fs.mkdirSync(log_dir_path)
2019-10-31 02:30:26 +11:00
} else {
2020-05-11 19:42:52 +10:00
return
2019-10-31 02:30:26 +11:00
}
} else {
2020-05-11 19:42:52 +10:00
fs.readdir(log_dir_path, function (err, files) {
if (err) throw err
files.filter(function (file) {
2019-10-31 02:30:26 +11:00
if (file != todayStr && file != yestStr && file != yest2Str) {
2020-05-11 19:42:52 +10:00
fs.unlinkSync(join(log_dir_path, file))
2019-10-31 02:30:26 +11:00
}
2020-05-11 19:42:52 +10:00
})
})
2019-10-31 02:30:26 +11:00
}
2020-05-11 19:42:52 +10:00
})
2021-04-09 17:35:40 +10:00
ipc.on('twitterLogin', (e, args) => {
const window = new BrowserWindow({
webPreferences: {
webviewTag: false,
nodeIntegration: false,
contextIsolation: true,
sandbox: false,
2021-04-09 17:35:40 +10:00
preload: join(dirname, 'js', 'platform', 'preload.js'),
},
width: 414,
height: 736,
})
const login = `https://mobile.twitter.com/login?hide_message=true&redirect_after_login=https%3A%2F%2Ftweetdeck.twitter.com%2F%3Fvia_twitter_login%3Dtrue`
const logout = `https://mobile.twitter.com/logout?redirect_after_logout=https%3A%2F%2Ftweetdeck.twitter.com%2F`
window.loadURL(args ? logout : login)
window.webContents.on('did-navigate', () => {
const url = window.webContents.getURL()
if (url.match("https://tweetdeck.twitter.com")) {
window.close()
e.sender.send('twitterLoginComplete', '')
2021-04-09 17:35:40 +10:00
}
})
})
2019-04-03 14:59:29 +11:00
}
2020-05-11 19:42:52 +10:00
exports.system = system