Combine startUrls and sendPaths

This commit allows to handle multiple URLs of all types as positional arguments simultaneously:
* tg:// links
* tonsite:// links
* interpret:// file paths
* generic file paths (to share files)

This allows to Drag'n'Drop files to the Telegram shortcut/binary.
This commit is contained in:
Ilya Fedin
2025-10-15 18:36:54 +00:00
committed by John Preston
parent f787e0fa1d
commit 5fbf280e4a
9 changed files with 90 additions and 152 deletions
+34 -45
View File
@@ -693,33 +693,26 @@ bool Application::eventFilter(QObject *object, QEvent *e) {
case QEvent::FileOpen: {
if (object == QCoreApplication::instance()) {
const auto event = static_cast<QFileOpenEvent*>(e);
const auto flushQueued = [=] {
if (_filesToOpen.isEmpty()) {
InvokeQueued(this, [=] {
cSetSendPaths(_filesToOpen);
_filesToOpen.clear();
checkSendPaths();
});
}
};
if (const auto file = event->file(); !file.isEmpty()) {
flushQueued();
_filesToOpen.append(file);
} else if (event->url().scheme() == u"tg"_q
|| event->url().scheme() == u"tonsite"_q) {
const auto url = QString::fromUtf8(
event->url().toEncoded().trimmed());
cSetStartUrl(url.mid(0, 8192));
checkStartUrl();
if (_lastActivePrimaryWindow
&& StartUrlRequiresActivate(url)) {
_lastActivePrimaryWindow->activate();
}
} else if (event->url().scheme() == u"interpret"_q) {
flushQueued();
_filesToOpen.append(event->url().toString());
if (_urlsToOpen.isEmpty()) {
InvokeQueued(this, [=] {
const auto activateRequired = ranges::any_of(
ranges::views::all(
_urlsToOpen
) | ranges::views::transform([](const QUrl &url) {
return url.toString();
}),
StartUrlRequiresActivate);
cRefStartUrls() << base::take(_urlsToOpen);
checkStartUrls();
if (_lastActivePrimaryWindow && activateRequired) {
_lastActivePrimaryWindow->activate();
}
});
}
const auto event = static_cast<QFileOpenEvent*>(e);
_urlsToOpen << event->url().toString(QUrl::FullyEncoded).mid(
0,
8192);
}
} break;
@@ -1091,31 +1084,27 @@ bool Application::canApplyLangPackWithoutRestart() const {
return true;
}
void Application::checkSendPaths() {
if (!cSendPaths().isEmpty()
void Application::checkStartUrls() {
if (!Core::App().passcodeLocked()) {
cRefStartUrls() = ranges::views::all(
cRefStartUrls()
) | ranges::views::filter([&](const QUrl &url) {
if (url.scheme() == u"tonsite"_q) {
iv().showTonSite(url.toString(), {});
return false;
} else if (_lastActivePrimaryWindow) {
return !openLocalUrl(url.toString(), {});
}
return true;
}) | ranges::to<QList<QUrl>>;
}
if (!cRefStartUrls().isEmpty()
&& _lastActivePrimaryWindow
&& !_lastActivePrimaryWindow->locked()) {
_lastActivePrimaryWindow->widget()->sendPaths();
}
}
void Application::checkStartUrl() {
if (!cStartUrl().isEmpty()) {
const auto url = cStartUrl();
if (!Core::App().passcodeLocked()) {
if (url.startsWith("tonsite://", Qt::CaseInsensitive)) {
cSetStartUrl(QString());
iv().showTonSite(url, {});
} else if (_lastActivePrimaryWindow) {
cSetStartUrl(QString());
if (!openLocalUrl(url, {})) {
cSetStartUrl(url);
}
}
}
}
}
bool Application::openLocalUrl(const QString &url, QVariant context) {
return openCustomUrl("tg://", LocalUrlHandlers(), url, context);
}