2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "application.h"
|
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "mainwindow.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-10-02 13:54:27 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2017-12-11 14:45:29 +00:00
|
|
|
#include "core/crash_reports.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "messenger.h"
|
2017-04-07 12:10:10 +00:00
|
|
|
#include "base/timer.h"
|
2018-08-09 20:39:22 +00:00
|
|
|
#include "base/concurrent_timer.h"
|
2018-04-09 17:48:29 +00:00
|
|
|
#include "base/qthelp_url.h"
|
|
|
|
#include "base/qthelp_regex.h"
|
2018-04-26 16:14:21 +00:00
|
|
|
#include "core/update_checker.h"
|
2017-12-11 14:45:29 +00:00
|
|
|
#include "core/crash_report_window.h"
|
2015-06-03 18:13:01 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-04-13 05:11:53 +00:00
|
|
|
constexpr auto kEmptyPidForCommandResponse = 0ULL;
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
QChar _toHex(ushort v) {
|
|
|
|
v = v & 0x000F;
|
|
|
|
return QChar::fromLatin1((v >= 10) ? ('a' + (v - 10)) : ('0' + v));
|
|
|
|
}
|
|
|
|
ushort _fromHex(QChar c) {
|
|
|
|
return ((c.unicode() >= uchar('a')) ? (c.unicode() - uchar('a') + 10) : (c.unicode() - uchar('0'))) & 0x000F;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString _escapeTo7bit(const QString &str) {
|
|
|
|
QString result;
|
|
|
|
result.reserve(str.size() * 2);
|
|
|
|
for (int i = 0, l = str.size(); i != l; ++i) {
|
|
|
|
QChar ch(str.at(i));
|
|
|
|
ushort uch(ch.unicode());
|
|
|
|
if (uch < 32 || uch > 127 || uch == ushort(uchar('%'))) {
|
|
|
|
result.append('%').append(_toHex(uch >> 12)).append(_toHex(uch >> 8)).append(_toHex(uch >> 4)).append(_toHex(uch));
|
|
|
|
} else {
|
|
|
|
result.append(ch);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:03:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
QString _escapeFrom7bit(const QString &str) {
|
|
|
|
QString result;
|
|
|
|
result.reserve(str.size());
|
|
|
|
for (int i = 0, l = str.size(); i != l; ++i) {
|
|
|
|
QChar ch(str.at(i));
|
|
|
|
if (ch == QChar::fromLatin1('%') && i + 4 < l) {
|
|
|
|
result.append(QChar(ushort((_fromHex(str.at(i + 1)) << 12) | (_fromHex(str.at(i + 2)) << 8) | (_fromHex(str.at(i + 3)) << 4) | _fromHex(str.at(i + 4)))));
|
|
|
|
i += 4;
|
|
|
|
} else {
|
|
|
|
result.append(ch);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:03:51 +00:00
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-07-31 17:56:54 +00:00
|
|
|
bool InternalPassportLink(const QString &url) {
|
2018-04-13 05:11:53 +00:00
|
|
|
const auto urlTrimmed = url.trimmed();
|
2018-07-31 17:56:54 +00:00
|
|
|
if (!urlTrimmed.startsWith(qstr("tg://"), Qt::CaseInsensitive)) {
|
|
|
|
return false;
|
2018-04-13 05:11:53 +00:00
|
|
|
}
|
|
|
|
const auto command = urlTrimmed.midRef(qstr("tg://").size());
|
|
|
|
|
|
|
|
using namespace qthelp;
|
|
|
|
const auto matchOptions = RegExOption::CaseInsensitive;
|
|
|
|
const auto authMatch = regex_match(
|
|
|
|
qsl("^passport/?\\?(.+)(#|$)"),
|
|
|
|
command,
|
|
|
|
matchOptions);
|
2018-07-31 17:56:54 +00:00
|
|
|
const auto usernameMatch = regex_match(
|
|
|
|
qsl("^resolve/?\\?(.+)(#|$)"),
|
2018-04-13 05:11:53 +00:00
|
|
|
command,
|
|
|
|
matchOptions);
|
2018-07-31 17:56:54 +00:00
|
|
|
const auto usernameValue = usernameMatch->hasMatch()
|
|
|
|
? url_parse_params(
|
|
|
|
usernameMatch->captured(1),
|
|
|
|
UrlParamNameTransform::ToLower).value(qsl("domain"))
|
|
|
|
: QString();
|
|
|
|
const auto authLegacy = (usernameValue == qstr("telegrampassport"));
|
|
|
|
return authMatch->hasMatch() || authLegacy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartUrlRequiresActivate(const QString &url) {
|
|
|
|
return Messenger::Instance().locked()
|
|
|
|
? true
|
|
|
|
: !InternalPassportLink(url);
|
2018-04-13 05:11:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:45:29 +00:00
|
|
|
Application::Application(
|
|
|
|
not_null<Core::Launcher*> launcher,
|
|
|
|
int &argc,
|
|
|
|
char **argv)
|
|
|
|
: QApplication(argc, argv)
|
2018-04-26 16:14:21 +00:00
|
|
|
, _launcher(launcher)
|
2018-07-11 22:14:44 +00:00
|
|
|
, _updateChecker(Core::UpdaterDisabled()
|
|
|
|
? nullptr
|
|
|
|
: std::make_unique<Core::UpdateChecker>()) {
|
2017-12-11 14:45:29 +00:00
|
|
|
const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath());
|
2014-07-18 10:37:34 +00:00
|
|
|
char h[33] = { 0 };
|
|
|
|
hashMd5Hex(d.constData(), d.size(), h);
|
2016-08-31 17:58:46 +00:00
|
|
|
#ifndef OS_MAC_STORE
|
2016-01-11 15:43:29 +00:00
|
|
|
_localServerName = psServerPrefix() + h + '-' + cGUIDStr();
|
2016-08-31 17:58:46 +00:00
|
|
|
#else // OS_MAC_STORE
|
|
|
|
h[4] = 0; // use only first 4 chars
|
|
|
|
_localServerName = psServerPrefix() + h;
|
|
|
|
#endif // OS_MAC_STORE
|
2016-01-11 15:43:29 +00:00
|
|
|
|
|
|
|
connect(&_localSocket, SIGNAL(connected()), this, SLOT(socketConnected()));
|
|
|
|
connect(&_localSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
|
|
|
|
connect(&_localSocket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socketError(QLocalSocket::LocalSocketError)));
|
|
|
|
connect(&_localSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(socketWritten(qint64)));
|
|
|
|
connect(&_localSocket, SIGNAL(readyRead()), this, SLOT(socketReading()));
|
2016-01-17 05:03:57 +00:00
|
|
|
connect(&_localServer, SIGNAL(newConnection()), this, SLOT(newInstanceConnected()));
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2016-02-29 16:53:26 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(startApplication()));
|
2016-01-30 18:24:18 +00:00
|
|
|
connect(this, SIGNAL(aboutToQuit()), this, SLOT(closeApplication()));
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (cManyInstance()) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Many instance allowed, starting..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
singleInstanceChecked();
|
|
|
|
} else {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Connecting local socket to %1...").arg(_localServerName));
|
2016-01-11 15:43:29 +00:00
|
|
|
_localSocket.connectToServer(_localServerName);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
Application::~Application() = default;
|
|
|
|
|
2016-09-15 10:50:43 +00:00
|
|
|
bool Application::event(QEvent *e) {
|
|
|
|
if (e->type() == QEvent::Close) {
|
|
|
|
App::quit();
|
|
|
|
}
|
|
|
|
return QApplication::event(e);
|
|
|
|
}
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::socketConnected() {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Socket connected, this is not the first application instance, sending show command..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
_secondInstance = true;
|
|
|
|
|
|
|
|
QString commands;
|
|
|
|
const QStringList &lst(cSendPaths());
|
|
|
|
for (QStringList::const_iterator i = lst.cbegin(), e = lst.cend(); i != e; ++i) {
|
|
|
|
commands += qsl("SEND:") + _escapeTo7bit(*i) + ';';
|
|
|
|
}
|
|
|
|
if (!cStartUrl().isEmpty()) {
|
|
|
|
commands += qsl("OPEN:") + _escapeTo7bit(cStartUrl()) + ';';
|
2018-06-06 09:06:07 +00:00
|
|
|
} else {
|
|
|
|
commands += qsl("CMD:show;");
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
DEBUG_LOG(("Application Info: writing commands %1").arg(commands));
|
|
|
|
_localSocket.write(commands.toLatin1());
|
|
|
|
}
|
2014-06-15 12:31:03 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::socketWritten(qint64/* bytes*/) {
|
|
|
|
if (_localSocket.state() != QLocalSocket::ConnectedState) {
|
2016-01-17 05:01:14 +00:00
|
|
|
LOG(("Socket is not connected %1").arg(_localSocket.state()));
|
2016-01-11 15:43:29 +00:00
|
|
|
return;
|
2014-12-19 21:20:30 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_localSocket.bytesToWrite()) {
|
|
|
|
return;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Show command written, waiting response..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::socketReading() {
|
|
|
|
if (_localSocket.state() != QLocalSocket::ConnectedState) {
|
2016-01-17 05:01:14 +00:00
|
|
|
LOG(("Socket is not connected %1").arg(_localSocket.state()));
|
2016-01-11 15:43:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_localSocketReadData.append(_localSocket.readAll());
|
|
|
|
if (QRegularExpression("RES:(\\d+);").match(_localSocketReadData).hasMatch()) {
|
|
|
|
uint64 pid = _localSocketReadData.mid(4, _localSocketReadData.length() - 5).toULongLong();
|
2018-03-31 17:07:29 +00:00
|
|
|
if (pid != kEmptyPidForCommandResponse) {
|
|
|
|
psActivateProcess(pid);
|
|
|
|
}
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Show command response received, pid = %1, activating and quitting...").arg(pid));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 20:19:05 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::socketError(QLocalSocket::LocalSocketError e) {
|
2016-03-02 18:34:42 +00:00
|
|
|
if (App::quitting()) return;
|
2016-01-21 06:58:58 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (_secondInstance) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Could not write show command, error %1, quitting...").arg(e));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (e == QLocalSocket::ServerNotFoundError) {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("This is the only instance of Telegram, starting server and app..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
} else {
|
2016-03-24 15:07:13 +00:00
|
|
|
LOG(("Socket connect error %1, starting server and app...").arg(e));
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
_localSocket.close();
|
2014-11-26 16:45:52 +00:00
|
|
|
|
2016-04-26 13:00:23 +00:00
|
|
|
// Local server does not work in WinRT build.
|
|
|
|
#ifndef Q_OS_WINRT
|
2016-01-11 15:43:29 +00:00
|
|
|
psCheckLocalSocket(_localServerName);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
if (!_localServer.listen(_localServerName)) {
|
2016-01-17 05:01:14 +00:00
|
|
|
LOG(("Failed to start listening to %1 server, error %2").arg(_localServerName).arg(int(_localServer.serverError())));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2016-04-26 13:00:23 +00:00
|
|
|
#endif // !Q_OS_WINRT
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-07-11 22:14:44 +00:00
|
|
|
if (!Core::UpdaterDisabled()
|
|
|
|
&& !cNoStartUpdate()
|
|
|
|
&& Core::checkReadyUpdate()) {
|
2016-01-11 15:43:29 +00:00
|
|
|
cSetRestartingUpdate(true);
|
2016-03-24 15:07:13 +00:00
|
|
|
DEBUG_LOG(("Application Info: installing update instead of starting app..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
2015-10-15 11:51:10 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
singleInstanceChecked();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::singleInstanceChecked() {
|
2016-01-17 05:01:14 +00:00
|
|
|
if (cManyInstance()) {
|
|
|
|
Logs::multipleInstances();
|
|
|
|
}
|
|
|
|
|
2016-02-08 14:54:55 +00:00
|
|
|
Sandbox::start();
|
2018-04-23 10:24:03 +00:00
|
|
|
refreshGlobalProxy();
|
2014-10-30 16:23:44 +00:00
|
|
|
|
2016-01-21 06:58:58 +00:00
|
|
|
if (!Logs::started() || (!cManyInstance() && !Logs::instanceChecked())) {
|
2016-01-25 10:22:58 +00:00
|
|
|
new NotStartedWindow();
|
2016-01-21 06:58:58 +00:00
|
|
|
} else {
|
2017-12-11 14:45:29 +00:00
|
|
|
const auto status = CrashReports::Start();
|
|
|
|
if (status == CrashReports::CantOpen) {
|
2016-01-25 10:22:58 +00:00
|
|
|
new NotStartedWindow();
|
2017-12-11 14:45:29 +00:00
|
|
|
} else if (status == CrashReports::LastCrashed) {
|
2016-02-08 14:54:55 +00:00
|
|
|
if (Sandbox::LastCrashDump().isEmpty()) { // don't handle bad closing for now
|
2017-12-11 14:45:29 +00:00
|
|
|
if (CrashReports::Restart() == CrashReports::CantOpen) {
|
2016-02-07 15:38:49 +00:00
|
|
|
new NotStartedWindow();
|
|
|
|
} else {
|
2016-02-08 14:54:55 +00:00
|
|
|
Sandbox::launch();
|
2016-02-07 15:38:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new LastCrashedWindow();
|
|
|
|
}
|
2016-01-21 06:58:58 +00:00
|
|
|
} else {
|
2016-02-08 14:54:55 +00:00
|
|
|
Sandbox::launch();
|
2016-01-21 06:58:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::socketDisconnected() {
|
|
|
|
if (_secondInstance) {
|
2016-03-24 15:07:13 +00:00
|
|
|
DEBUG_LOG(("Application Error: socket disconnected before command response received, quitting..."));
|
2016-01-11 15:43:29 +00:00
|
|
|
return App::quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::newInstanceConnected() {
|
|
|
|
DEBUG_LOG(("Application Info: new local socket connected"));
|
|
|
|
for (QLocalSocket *client = _localServer.nextPendingConnection(); client; client = _localServer.nextPendingConnection()) {
|
|
|
|
_localClients.push_back(LocalClient(client, QByteArray()));
|
|
|
|
connect(client, SIGNAL(readyRead()), this, SLOT(readClients()));
|
|
|
|
connect(client, SIGNAL(disconnected()), this, SLOT(removeClients()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::readClients() {
|
2017-07-05 18:45:46 +00:00
|
|
|
// This method can be called before Messenger is constructed.
|
2016-01-11 15:43:29 +00:00
|
|
|
QString startUrl;
|
|
|
|
QStringList toSend;
|
|
|
|
for (LocalClients::iterator i = _localClients.begin(), e = _localClients.end(); i != e; ++i) {
|
|
|
|
i->second.append(i->first->readAll());
|
|
|
|
if (i->second.size()) {
|
|
|
|
QString cmds(QString::fromLatin1(i->second));
|
|
|
|
int32 from = 0, l = cmds.length();
|
|
|
|
for (int32 to = cmds.indexOf(QChar(';'), from); to >= from; to = (from < l) ? cmds.indexOf(QChar(';'), from) : -1) {
|
|
|
|
QStringRef cmd(&cmds, from, to - from);
|
|
|
|
if (cmd.startsWith(qsl("CMD:"))) {
|
2016-02-08 14:54:55 +00:00
|
|
|
Sandbox::execExternal(cmds.mid(from + 4, to - from - 4));
|
2018-03-31 17:07:29 +00:00
|
|
|
const auto response = qsl("RES:%1;").arg(QCoreApplication::applicationPid()).toLatin1();
|
2016-01-11 15:43:29 +00:00
|
|
|
i->first->write(response.data(), response.size());
|
|
|
|
} else if (cmd.startsWith(qsl("SEND:"))) {
|
|
|
|
if (cSendPaths().isEmpty()) {
|
|
|
|
toSend.append(_escapeFrom7bit(cmds.mid(from + 5, to - from - 5)));
|
|
|
|
}
|
|
|
|
} else if (cmd.startsWith(qsl("OPEN:"))) {
|
2018-03-31 17:07:29 +00:00
|
|
|
auto activateRequired = true;
|
2016-01-11 15:43:29 +00:00
|
|
|
if (cStartUrl().isEmpty()) {
|
2016-07-28 17:01:08 +00:00
|
|
|
startUrl = _escapeFrom7bit(cmds.mid(from + 5, to - from - 5)).mid(0, 8192);
|
2018-03-31 17:07:29 +00:00
|
|
|
activateRequired = StartUrlRequiresActivate(startUrl);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
2018-03-31 17:07:29 +00:00
|
|
|
if (activateRequired) {
|
|
|
|
Sandbox::execExternal("show");
|
|
|
|
}
|
|
|
|
const auto responsePid = activateRequired
|
|
|
|
? QCoreApplication::applicationPid()
|
|
|
|
: kEmptyPidForCommandResponse;
|
|
|
|
const auto response = qsl("RES:%1;").arg(responsePid).toLatin1();
|
|
|
|
i->first->write(response.data(), response.size());
|
2016-01-11 15:43:29 +00:00
|
|
|
} else {
|
|
|
|
LOG(("Application Error: unknown command %1 passed in local socket").arg(QString(cmd.constData(), cmd.length())));
|
|
|
|
}
|
|
|
|
from = to + 1;
|
|
|
|
}
|
|
|
|
if (from > 0) {
|
|
|
|
i->second = i->second.mid(from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!toSend.isEmpty()) {
|
|
|
|
QStringList paths(cSendPaths());
|
|
|
|
paths.append(toSend);
|
|
|
|
cSetSendPaths(paths);
|
|
|
|
}
|
|
|
|
if (!cSendPaths().isEmpty()) {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->sendPaths();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!startUrl.isEmpty()) {
|
|
|
|
cSetStartUrl(startUrl);
|
|
|
|
}
|
2017-07-05 18:45:46 +00:00
|
|
|
if (auto messenger = Messenger::InstancePointer()) {
|
|
|
|
messenger->checkStartUrl();
|
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::removeClients() {
|
|
|
|
DEBUG_LOG(("Application Info: remove clients slot called, clients %1").arg(_localClients.size()));
|
|
|
|
for (LocalClients::iterator i = _localClients.begin(), e = _localClients.end(); i != e;) {
|
|
|
|
if (i->first->state() != QLocalSocket::ConnectedState) {
|
|
|
|
DEBUG_LOG(("Application Info: removing client"));
|
|
|
|
i = _localClients.erase(i);
|
|
|
|
e = _localClients.end();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:53:26 +00:00
|
|
|
void Application::startApplication() {
|
2016-03-02 18:34:42 +00:00
|
|
|
if (App::quitting()) {
|
2016-02-29 16:53:26 +00:00
|
|
|
quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void Application::createMessenger() {
|
2017-08-03 12:52:56 +00:00
|
|
|
Expects(!App::quitting());
|
2018-04-23 10:24:03 +00:00
|
|
|
|
2017-12-11 14:45:29 +00:00
|
|
|
_messengerInstance = std::make_unique<Messenger>(_launcher);
|
2017-02-23 10:59:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 10:24:03 +00:00
|
|
|
void Application::refreshGlobalProxy() {
|
|
|
|
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY
|
|
|
|
const auto proxy = [&] {
|
|
|
|
if (Global::started()) {
|
2018-04-27 17:26:45 +00:00
|
|
|
return Global::UseProxy()
|
|
|
|
? Global::SelectedProxy()
|
|
|
|
: ProxyData();
|
2018-04-23 10:24:03 +00:00
|
|
|
}
|
|
|
|
return Sandbox::PreLaunchProxy();
|
|
|
|
}();
|
2018-04-24 08:46:27 +00:00
|
|
|
if (proxy.type == ProxyData::Type::Socks5
|
|
|
|
|| proxy.type == ProxyData::Type::Http) {
|
2018-05-17 19:58:00 +00:00
|
|
|
QNetworkProxy::setApplicationProxy(
|
|
|
|
ToNetworkProxy(ToDirectIpProxy(proxy)));
|
2018-04-23 10:24:03 +00:00
|
|
|
} else {
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
|
|
|
}
|
|
|
|
#endif // TDESKTOP_DISABLE_NETWORK_PROXY
|
|
|
|
}
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
void Application::closeApplication() {
|
2016-03-03 18:37:15 +00:00
|
|
|
if (App::launchState() == App::QuitProcessed) return;
|
2016-03-02 18:34:42 +00:00
|
|
|
App::setLaunchState(App::QuitProcessed);
|
2016-03-02 20:50:58 +00:00
|
|
|
|
2017-05-03 11:36:39 +00:00
|
|
|
_messengerInstance.reset();
|
2016-02-29 16:53:26 +00:00
|
|
|
|
|
|
|
Sandbox::finish();
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
_localServer.close();
|
|
|
|
for (LocalClients::iterator i = _localClients.begin(), e = _localClients.end(); i != e; ++i) {
|
|
|
|
disconnect(i->first, SIGNAL(disconnected()), this, SLOT(removeClients()));
|
|
|
|
i->first->close();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-11 15:43:29 +00:00
|
|
|
_localClients.clear();
|
2016-02-29 16:53:26 +00:00
|
|
|
|
|
|
|
_localSocket.close();
|
|
|
|
|
2018-04-26 16:14:21 +00:00
|
|
|
_updateChecker = nullptr;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:43:29 +00:00
|
|
|
inline Application *application() {
|
|
|
|
return qobject_cast<Application*>(QApplication::instance());
|
|
|
|
}
|
|
|
|
|
2016-02-08 14:54:55 +00:00
|
|
|
namespace Sandbox {
|
2016-01-11 15:43:29 +00:00
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
QRect availableGeometry() {
|
|
|
|
if (auto a = application()) {
|
|
|
|
return a->desktop()->availableGeometry();
|
2016-02-05 19:53:01 +00:00
|
|
|
}
|
2017-02-23 10:59:19 +00:00
|
|
|
return QDesktopWidget().availableGeometry();
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
QRect screenGeometry(const QPoint &p) {
|
|
|
|
if (auto a = application()) {
|
|
|
|
return a->desktop()->screenGeometry(p);
|
2016-02-27 19:39:51 +00:00
|
|
|
}
|
2017-02-23 10:59:19 +00:00
|
|
|
return QDesktopWidget().screenGeometry(p);
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void setActiveWindow(QWidget *window) {
|
|
|
|
if (auto a = application()) {
|
|
|
|
a->setActiveWindow(window);
|
2016-10-28 12:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
bool isSavingSession() {
|
|
|
|
if (auto a = application()) {
|
|
|
|
return a->isSavingSession();
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void execExternal(const QString &cmd) {
|
|
|
|
DEBUG_LOG(("Application Info: executing external command '%1'").arg(cmd));
|
|
|
|
if (cmd == "show") {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->activate();
|
|
|
|
} else if (PreLaunchWindow::instance()) {
|
|
|
|
PreLaunchWindow::instance()->activate();
|
2016-01-11 15:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void adjustSingleTimers() {
|
|
|
|
if (auto a = application()) {
|
|
|
|
a->adjustSingleTimers();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-04-07 12:10:10 +00:00
|
|
|
base::Timer::Adjust();
|
2018-08-09 20:39:22 +00:00
|
|
|
base::ConcurrentTimerEnvironment::Adjust();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void connect(const char *signal, QObject *object, const char *method) {
|
|
|
|
if (auto a = application()) {
|
|
|
|
a->connect(a, signal, object, method);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
void launch() {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(application() != 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-10-15 11:44:48 +00:00
|
|
|
const auto dpi = Application::primaryScreen()->logicalDotsPerInch();
|
|
|
|
LOG(("Primary screen DPI: %1").arg(dpi));
|
2018-10-15 19:42:10 +00:00
|
|
|
if (dpi <= 108) {
|
|
|
|
cSetScreenScale(100); // 100%: 96 DPI (0-108)
|
|
|
|
} else if (dpi <= 132) {
|
|
|
|
cSetScreenScale(125); // 125%: 120 DPI (108-132)
|
|
|
|
} else if (dpi <= 168) {
|
|
|
|
cSetScreenScale(150); // 150%: 144 DPI (132-168)
|
|
|
|
} else if (dpi <= 216) {
|
|
|
|
cSetScreenScale(200); // 200%: 192 DPI (168-216)
|
|
|
|
} else if (dpi <= 264) {
|
|
|
|
cSetScreenScale(250); // 250%: 240 DPI (216-264)
|
|
|
|
} else {
|
|
|
|
cSetScreenScale(300); // 300%: 288 DPI (264-inf)
|
2017-02-23 10:59:19 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
auto devicePixelRatio = application()->devicePixelRatio();
|
|
|
|
if (devicePixelRatio > 1.) {
|
|
|
|
if ((cPlatform() != dbipMac && cPlatform() != dbipMacOld) || (devicePixelRatio != 2.)) {
|
|
|
|
LOG(("Found non-trivial Device Pixel Ratio: %1").arg(devicePixelRatio));
|
|
|
|
LOG(("Environmental variables: QT_DEVICE_PIXEL_RATIO='%1'").arg(QString::fromLatin1(qgetenv("QT_DEVICE_PIXEL_RATIO"))));
|
|
|
|
LOG(("Environmental variables: QT_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_SCALE_FACTOR"))));
|
|
|
|
LOG(("Environmental variables: QT_AUTO_SCREEN_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR"))));
|
|
|
|
LOG(("Environmental variables: QT_SCREEN_SCALE_FACTORS='%1'").arg(QString::fromLatin1(qgetenv("QT_SCREEN_SCALE_FACTORS"))));
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
2017-02-23 10:59:19 +00:00
|
|
|
cSetRetinaFactor(devicePixelRatio);
|
|
|
|
cSetIntRetinaFactor(int32(cRetinaFactor()));
|
2018-10-15 19:42:10 +00:00
|
|
|
cSetScreenScale(kInterfaceScaleDefault);
|
2015-03-02 12:34:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
application()->createMessenger();
|
2017-02-23 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 10:24:03 +00:00
|
|
|
void refreshGlobalProxy() {
|
|
|
|
if (const auto instance = application()) {
|
|
|
|
instance->refreshGlobalProxy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:59:19 +00:00
|
|
|
} // namespace Sandbox
|