thedesk/app/main/system.js

301 lines
8.0 KiB
JavaScript
Raw Permalink Normal View History

2019-06-15 19:52:28 +10:00
function system(mainWindow, dir, lang, dirname) {
2019-05-19 17:39:30 +10:00
const electron = require("electron");
const app = electron.app;
2019-10-31 02:30:26 +11:00
const join = require("path").join;
2019-04-03 14:59:29 +11:00
var Jimp = require("jimp");
const fs = require("fs");
2019-10-31 02:30:26 +11:00
var JSON5 = require("json5");
2019-04-03 14:59:29 +11:00
var ipc = electron.ipcMain;
2019-10-14 02:52:33 +11:00
const clipboard = electron.clipboard;
2019-04-03 14:59:29 +11:00
var tmp_img = join(app.getPath("userData"), "tmp.png");
var ha_path = join(app.getPath("userData"), "hardwareAcceleration");
2019-10-26 03:16:33 +11:00
var ua_path = join(app.getPath("userData"), "useragent");
2019-04-03 14:59:29 +11:00
var lang_path = join(app.getPath("userData"), "language");
2019-10-31 02:30:26 +11:00
var log_dir_path = join(app.getPath("userData"), "logs");
2019-10-31 03:00:06 +11:00
//ログ
var today = new Date();
//今日のやつ
var todayStr = today.getFullYear() + "" + (today.getMonth() + 1) + "" + today.getDate() + ".log";
//昨日のやつ
today.setDate(today.getDate() - 1);
var yestStr = today.getFullYear() + "" + (today.getMonth() + 1) + "" + today.getDate() + ".log";
//一昨日のやつ
today.setDate(today.getDate() - 1);
var yest2Str = today.getFullYear() + "" + (today.getMonth() + 1) + "" + today.getDate() + ".log";
2019-04-03 14:59:29 +11:00
const BrowserWindow = electron.BrowserWindow;
const dialog = electron.dialog;
2019-10-31 02:30:26 +11:00
const os = require("os");
2019-05-19 17:39:30 +10:00
const language = require("../main/language.js");
2019-06-15 00:25:27 +10:00
//プラットフォーム
2019-10-31 02:30:26 +11:00
ipc.on("getPlatform", function(e, arg) {
2019-08-26 01:09:01 +10:00
try {
2019-10-31 02:30:26 +11:00
var gitHash = fs.readFileSync("git", "utf8");
} catch {
var gitHash = null;
2019-08-26 01:09:01 +10:00
}
2019-10-31 02:30:26 +11: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
//言語
2019-10-31 02:30:26 +11:00
ipc.on("lang", function(e, arg) {
2019-05-19 17:39:30 +10:00
console.log("set:" + arg);
fs.writeFileSync(lang_path, arg);
2019-10-31 02:30:26 +11:00
e.sender.webContents.send("langres", arg);
});
2019-06-15 03:01:38 +10:00
//エクスポートのダイアログ
2019-10-31 02:30:26 +11:00
ipc.on("exportSettings", function(e, args) {
dialog.showSaveDialog(
null,
{
title: "Export",
properties: ["openFile", "createDirectory"],
defaultPath: "export.thedeskconfig.json5"
},
savedFiles => {
if (!savedFiles) {
return false;
}
e.sender.webContents.send("exportSettingsFile", savedFiles);
2019-06-15 03:01:38 +10:00
}
2019-10-31 02:30:26 +11:00
);
});
2019-06-15 03:01:38 +10:00
//インポートのダイアログ
2019-10-31 02:30:26 +11:00
ipc.on("importSettings", function(e, args) {
dialog.showOpenDialog(
null,
{
title: "Import",
properties: ["openFile"],
filters: [{ name: "TheDesk Config", extensions: ["thedeskconfig", "thedeskconfigv2", "json5"] }]
},
fileNames => {
if (!fileNames) {
return false;
}
e.sender.webContents.send("config", JSON5.parse(fs.readFileSync(fileNames[0], "utf8")));
2019-06-15 03:01:38 +10:00
}
2019-10-31 02:30:26 +11:00
);
});
2019-06-15 03:01:38 +10:00
//保存フォルダのダイアログ
2019-10-31 02:30:26 +11:00
ipc.on("savefolder", function(e, args) {
dialog.showOpenDialog(
null,
{
title: "Save folder",
properties: ["openDirectory"]
},
fileNames => {
e.sender.webContents.send("savefolder", fileNames[0]);
}
);
});
2019-06-15 03:01:38 +10:00
//カスタムサウンドのダイアログ
2019-10-31 02:30:26 +11:00
ipc.on("customSound", function(e, arg) {
dialog.showOpenDialog(
null,
{
title: "Custom sound",
properties: ["openFile"],
filters: [{ name: "Audio", extensions: ["mp3", "aac", "wav", "flac", "m4a"] }, { name: "All", extensions: ["*"] }]
},
fileNames => {
e.sender.webContents.send("customSoundRender", [arg, fileNames[0]]);
}
);
});
2019-06-15 02:32:59 +10:00
2019-05-19 17:39:30 +10:00
//ハードウェアアクセラレーションの無効化
2019-10-31 02:30:26 +11:00
ipc.on("ha", function(e, arg) {
2019-04-03 14:59:29 +11:00
if (arg == "true") {
fs.writeFileSync(ha_path, arg);
} else {
2019-10-31 02:30:26 +11:00
fs.unlink(ha_path, function(err) {});
2019-04-03 14:59:29 +11:00
}
2019-10-31 02:30:26 +11:00
app.relaunch();
app.exit();
});
ipc.on("ua", function(e, arg) {
2019-10-26 03:16:33 +11:00
if (arg == "") {
2019-10-31 02:30:26 +11:00
fs.unlink(ua_path, function(err) {});
2019-10-26 03:16:33 +11:00
} else {
fs.writeFileSync(ua_path, arg);
}
2019-10-31 02:30:26 +11:00
app.relaunch();
app.exit();
});
2019-04-03 14:59:29 +11:00
2019-10-31 02:30:26 +11:00
ipc.on("quit", (e, args) => {
2019-04-03 14:59:29 +11:00
app.quit();
});
2019-10-31 02:30:26 +11:00
ipc.on("about", (e, args) => {
2019-04-03 14:59:29 +11:00
about();
});
function about() {
2019-10-31 02:30:26 +11: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,
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
2019-04-03 14:59:29 +11:00
});
2019-10-31 02:30:26 +11:00
window.loadURL(dir + "/about.html?ver=" + ver);
return "true";
2019-04-03 14:59:29 +11:00
}
2019-10-31 02:30:26 +11:00
ipc.on("nano", function(e, x, y) {
var nano_info_path = join(app.getPath("userData"), "nano-window-position.json");
2019-04-03 14:59:29 +11:00
var window_pos;
try {
2019-10-31 02:30:26 +11:00
window_pos = JSON.parse(fs.readFileSync(nano_info_path, "utf8"));
2019-04-03 14:59:29 +11:00
} catch (e) {
window_pos = [0, 0]; // デフォルトバリュー
}
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,
2019-08-26 22:06:07 +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, // 枠の無いウィンドウ
resizable: false
2019-04-03 14:59:29 +11:00
});
2019-10-31 02:30:26 +11:00
nanowindow.loadURL(dir + "/nano.html");
2019-04-03 14:59:29 +11:00
nanowindow.setAlwaysOnTop(true);
2019-10-14 02:28:10 +11:00
//nanowindow.toggleDevTools()
2019-04-03 14:59:29 +11:00
nanowindow.setPosition(window_pos[0], window_pos[1]);
2019-10-31 02:30:26 +11:00
nanowindow.on("close", function() {
2019-04-03 14:59:29 +11:00
fs.writeFileSync(nano_info_path, JSON.stringify(nanowindow.getPosition()));
});
return true;
2019-10-31 02:30:26 +11:00
});
2019-04-03 14:59:29 +11:00
var cbTimer1;
2019-10-31 02:30:26 +11:00
ipc.on("startmem", (e, arg) => {
event = e.sender;
2019-06-16 02:34:49 +10:00
cbTimer1 = setInterval(mems, 1000);
2019-04-03 14:59:29 +11:00
});
2019-06-16 02:34:49 +10:00
function mems() {
var mem = os.totalmem() - os.freemem();
if (mainWindow) {
2019-10-31 02:30:26 +11:00
event.webContents.send("memory", [mem, os.cpus()[0].model, os.totalmem()]);
2019-06-16 02:34:49 +10:00
}
}
2019-10-31 02:30:26 +11:00
ipc.on("endmem", (e, arg) => {
2019-04-03 14:59:29 +11:00
if (cbTimer1) {
clearInterval(cbTimer1);
}
});
2019-10-31 02:30:26 +11:00
ipc.on("export", (e, args) => {
2019-08-29 02:20:47 +10:00
fs.writeFileSync(args[0], JSON5.stringify(args[1]));
2019-10-31 02:30:26 +11:00
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-10-31 02:30:26 +11:00
if (order === "asc") {
//指定があれば昇順(ASC)
2019-04-03 14:59:29 +11:00
num_a = 1;
num_b = -1;
}
2019-10-31 02:30:26 +11:00
data = data.sort(function(a, b) {
2019-04-03 14:59:29 +11:00
var x = a[key];
var y = b[key];
if (x > y) return num_a;
if (x < y) return num_b;
return 0;
});
//重複排除
var arrObj = {};
for (var i = 0; i < data.length; i++) {
2019-10-31 02:30:26 +11:00
arrObj[data[i]["family"]] = data[i];
2019-04-03 14:59:29 +11:00
}
data = [];
for (var key in arrObj) {
data.push(arrObj[key]);
}
fn(data); // ソート後の配列を返す
}
2019-10-31 02:30:26 +11:00
ipc.on("fonts", (e, arg) => {
const fm = require("font-manager");
2019-04-03 14:59:29 +11:00
var fonts = fm.getAvailableFontsSync();
2019-10-31 02:30:26 +11:00
object_array_sort(fonts, "family", "asc", function(fonts_sorted) {
e.sender.webContents.send("font-list", fonts_sorted);
2019-04-03 14:59:29 +11:00
});
});
2019-10-14 02:52:33 +11:00
//コピー
2019-10-31 02:30:26 +11:00
ipc.on("copy", (e, arg) => {
clipboard.writeText(arg);
});
//ログ
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) {
if (err) {
throw err;
}
});
});
2019-10-31 03:00:06 +11:00
ipc.on("getLogs", (e, arg) => {
var logs=""
2019-11-04 03:10:06 +11:00
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;
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")
}
if (file == yest2Str) {
yest2Log=fs.readFileSync(join(log_dir_path, file), "utf8")
}
logs = todayLog + yestLog + yest2Log;
2019-10-31 03:00:06 +11:00
});
2019-11-04 03:10:06 +11:00
logs = yest2Log + yestLog + todayLog;
2019-10-31 03:00:06 +11:00
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);
} else {
return;
}
} else {
fs.readdir(log_dir_path, function(err, files) {
if (err) throw err;
files.filter(function(file) {
if (file != todayStr && file != yestStr && file != yest2Str) {
fs.unlinkSync(join(log_dir_path, file));
}
});
});
}
2019-10-14 02:52:33 +11:00
});
2019-04-03 14:59:29 +11:00
}
2019-10-31 02:30:26 +11:00
exports.system = system;