TeaSpeak-Client/modules/renderer/UnloadHandler.ts

60 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-08-21 07:37:10 -04:00
import {Settings, settings} from "tc-shared/settings";
import {tr} from "tc-shared/i18n/localize";
2020-10-01 04:56:22 -04:00
import {Arguments, processArguments} from "../shared/process-arguments";
2020-08-21 07:37:10 -04:00
import {remote} from "electron";
2020-09-24 16:06:52 -04:00
import {server_connections} from "tc-shared/ConnectionManager";
2020-08-21 07:37:10 -04:00
2020-08-23 15:26:27 -04:00
window.onbeforeunload = event => {
if(settings.getValue(Settings.KEY_DISABLE_UNLOAD_DIALOG))
2020-08-21 07:37:10 -04:00
return;
const active_connections = server_connections.getAllConnectionHandlers().filter(e => e.connected);
2020-08-21 07:37:10 -04:00
if(active_connections.length == 0) return;
const do_exit = (closeWindow: boolean) => {
const dp = server_connections.getAllConnectionHandlers().map(e => {
2020-08-21 07:37:10 -04:00
if(e.serverConnection.connected())
return e.serverConnection.disconnect(tr("client closed"))
2020-08-23 15:26:27 -04:00
.catch(error => {
console.warn(tr("Failed to disconnect from server %s on client close: %o"),
e.serverConnection.remote_address().host + ":" + e.serverConnection.remote_address().port,
error
);
});
2020-08-21 07:37:10 -04:00
return Promise.resolve();
});
if(closeWindow) {
const exit = () => {
const {remote} = window.require('electron');
remote.getCurrentWindow().close();
};
Promise.all(dp).then(exit);
/* force exit after 2500ms */
setTimeout(exit, 2500);
}
};
2020-10-01 04:56:22 -04:00
if(processArguments.has_flag(Arguments.DEBUG)) {
2020-08-21 07:37:10 -04:00
do_exit(false);
return;
}
remote.dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you really sure?\nYou\'re still connected!'
}).then(result => {
if(result.response === 0) {
/* prevent quitting because we try to disconnect */
2020-08-23 15:26:27 -04:00
window.onbeforeunload = e => e.preventDefault();
2020-08-21 07:37:10 -04:00
do_exit(true);
}
});
2020-08-23 15:26:27 -04:00
event.preventDefault();
event.returnValue = "question";
}