tdesktop/Telegram/SourceFiles/core/ui_integration.cpp

239 lines
6.5 KiB
C++
Raw Normal View History

2019-09-16 11:14:06 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "core/ui_integration.h"
2019-09-16 11:14:06 +00:00
#include "core/local_url_handlers.h"
#include "core/file_utilities.h"
#include "core/application.h"
#include "core/sandbox.h"
#include "core/click_handler_types.h"
#include "ui/basic_click_handlers.h"
#include "ui/emoji_config.h"
#include "lang/lang_keys.h"
#include "platform/platform_specific.h"
#include "main/main_account.h"
#include "main/main_session.h"
#include "mainwindow.h"
namespace Core {
void UiIntegration::postponeCall(FnMut<void()> &&callable) {
Sandbox::Instance().postponeCall(std::move(callable));
}
void UiIntegration::registerLeaveSubscription(not_null<QWidget*> widget) {
Core::App().registerLeaveSubscription(widget);
}
void UiIntegration::unregisterLeaveSubscription(not_null<QWidget*> widget) {
Core::App().unregisterLeaveSubscription(widget);
}
void UiIntegration::writeLogEntry(const QString &entry) {
Logs::writeMain(entry);
}
QString UiIntegration::emojiCacheFolder() {
return cWorkingDir() + "tdata/emoji";
}
void UiIntegration::textActionsUpdated() {
if (const auto window = App::wnd()) {
window->updateGlobalMenu();
}
}
2019-09-16 11:14:06 +00:00
void UiIntegration::activationFromTopPanel() {
Platform::IgnoreApplicationActivationRightNow();
}
void UiIntegration::startFontsBegin() {
}
void UiIntegration::startFontsEnd() {
}
2020-09-30 09:11:44 +00:00
QString UiIntegration::timeFormat() {
return cTimeFormat();
}
2019-09-16 11:14:06 +00:00
std::shared_ptr<ClickHandler> UiIntegration::createLinkHandler(
2020-06-16 15:10:39 +00:00
const EntityLinkData &data,
const std::any &context) {
const auto my = std::any_cast<MarkedTextContext>(&context);
2020-06-16 15:10:39 +00:00
switch (data.type) {
case EntityType::Url:
2020-06-16 15:10:39 +00:00
return (!data.data.isEmpty()
&& UrlClickHandler::IsSuspicious(data.data))
? std::make_shared<HiddenUrlClickHandler>(data.data)
: Integration::createLinkHandler(data, context);
2019-09-16 11:14:06 +00:00
case EntityType::CustomUrl:
2020-06-16 15:10:39 +00:00
return !data.data.isEmpty()
? std::make_shared<HiddenUrlClickHandler>(data.data)
: Integration::createLinkHandler(data, context);
2019-09-16 11:14:06 +00:00
case EntityType::BotCommand:
2020-06-16 15:10:39 +00:00
return std::make_shared<BotCommandClickHandler>(data.data);
2019-09-16 11:14:06 +00:00
case EntityType::Hashtag:
using HashtagMentionType = MarkedTextContext::HashtagMentionType;
2020-06-16 15:10:39 +00:00
if (my && my->type == HashtagMentionType::Twitter) {
2019-09-16 11:14:06 +00:00
return std::make_shared<UrlClickHandler>(
(qsl("https://twitter.com/hashtag/")
2020-06-16 15:10:39 +00:00
+ data.data.mid(1)
2019-09-16 11:14:06 +00:00
+ qsl("?src=hash")),
true);
2020-06-16 15:10:39 +00:00
} else if (my && my->type == HashtagMentionType::Instagram) {
2019-09-16 11:14:06 +00:00
return std::make_shared<UrlClickHandler>(
(qsl("https://instagram.com/explore/tags/")
2020-06-16 15:10:39 +00:00
+ data.data.mid(1)
2019-09-16 11:14:06 +00:00
+ '/'),
true);
}
2020-06-16 15:10:39 +00:00
return std::make_shared<HashtagClickHandler>(data.data);
2019-09-16 11:14:06 +00:00
case EntityType::Cashtag:
2020-06-16 15:10:39 +00:00
return std::make_shared<CashtagClickHandler>(data.data);
2019-09-16 11:14:06 +00:00
case EntityType::Mention:
using HashtagMentionType = MarkedTextContext::HashtagMentionType;
2020-06-16 15:10:39 +00:00
if (my && my->type == HashtagMentionType::Twitter) {
2019-09-16 11:14:06 +00:00
return std::make_shared<UrlClickHandler>(
2020-06-16 15:10:39 +00:00
qsl("https://twitter.com/") + data.data.mid(1),
2019-09-16 11:14:06 +00:00
true);
2020-06-16 15:10:39 +00:00
} else if (my && my->type == HashtagMentionType::Instagram) {
2019-09-16 11:14:06 +00:00
return std::make_shared<UrlClickHandler>(
2020-06-16 15:10:39 +00:00
qsl("https://instagram.com/") + data.data.mid(1) + '/',
2019-09-16 11:14:06 +00:00
true);
}
2020-06-16 15:10:39 +00:00
return std::make_shared<MentionClickHandler>(data.data);
2019-09-16 11:14:06 +00:00
case EntityType::MentionName: {
2020-06-16 15:10:39 +00:00
auto fields = TextUtilities::MentionNameDataToFields(data.data);
if (!my || !my->session) {
LOG(("Mention name without a session: %1").arg(data.data));
} else if (fields.userId) {
2019-09-16 11:14:06 +00:00
return std::make_shared<MentionNameClickHandler>(
my->session,
2020-06-16 15:10:39 +00:00
data.text,
2019-09-16 11:14:06 +00:00
fields.userId,
fields.accessHash);
} else {
2020-06-16 15:10:39 +00:00
LOG(("Bad mention name: %1").arg(data.data));
2019-09-16 11:14:06 +00:00
}
} break;
}
2020-06-16 15:10:39 +00:00
return Integration::createLinkHandler(data, context);
2019-09-16 11:14:06 +00:00
}
bool UiIntegration::handleUrlClick(
const QString &url,
const QVariant &context) {
const auto local = Core::TryConvertUrlToLocal(url);
2019-09-16 11:14:06 +00:00
if (Core::InternalPassportLink(local)) {
return true;
}
if (UrlClickHandler::IsEmail(url)) {
File::OpenEmailLink(url);
return true;
} else if (local.startsWith(qstr("tg://"), Qt::CaseInsensitive)) {
Core::App().openLocalUrl(local, context);
2019-09-16 11:14:06 +00:00
return true;
} else if (local.startsWith(qstr("internal:"), Qt::CaseInsensitive)) {
Core::App().openInternalUrl(local, context);
return true;
2019-09-16 11:14:06 +00:00
}
File::OpenUrl(url);
return true;
2019-09-16 11:14:06 +00:00
}
rpl::producer<> UiIntegration::forcePopupMenuHideRequests() {
return Core::App().passcodeLockChanges() | rpl::to_empty;
2019-09-16 11:14:06 +00:00
}
QString UiIntegration::convertTagToMimeTag(const QString &tagId) {
if (TextUtilities::IsMentionLink(tagId)) {
if (const auto session = Core::App().activeAccount().maybeSession()) {
return tagId + ':' + QString::number(session->userId());
2019-09-16 11:14:06 +00:00
}
}
return tagId;
}
const Ui::Emoji::One *UiIntegration::defaultEmojiVariant(
const Ui::Emoji::One *emoji) {
if (!emoji || !emoji->hasVariants()) {
return emoji;
}
const auto nonColored = emoji->nonColoredId();
const auto it = cEmojiVariants().constFind(nonColored);
const auto result = (it != cEmojiVariants().cend())
? emoji->variant(it.value())
: emoji;
AddRecentEmoji(result);
return result;
}
QString UiIntegration::phraseContextCopyText() {
return tr::lng_context_copy_text(tr::now);
}
QString UiIntegration::phraseContextCopyEmail() {
return tr::lng_context_copy_email(tr::now);
}
QString UiIntegration::phraseContextCopyLink() {
return tr::lng_context_copy_link(tr::now);
}
QString UiIntegration::phraseContextCopySelected() {
return tr::lng_context_copy_selected(tr::now);
}
QString UiIntegration::phraseFormattingTitle() {
return tr::lng_menu_formatting(tr::now);
}
QString UiIntegration::phraseFormattingLinkCreate() {
return tr::lng_menu_formatting_link_create(tr::now);
}
QString UiIntegration::phraseFormattingLinkEdit() {
return tr::lng_menu_formatting_link_edit(tr::now);
}
QString UiIntegration::phraseFormattingClear() {
return tr::lng_menu_formatting_clear(tr::now);
}
QString UiIntegration::phraseFormattingBold() {
return tr::lng_menu_formatting_bold(tr::now);
}
QString UiIntegration::phraseFormattingItalic() {
return tr::lng_menu_formatting_italic(tr::now);
}
QString UiIntegration::phraseFormattingUnderline() {
return tr::lng_menu_formatting_underline(tr::now);
}
QString UiIntegration::phraseFormattingStrikeOut() {
return tr::lng_menu_formatting_strike_out(tr::now);
}
QString UiIntegration::phraseFormattingMonospace() {
return tr::lng_menu_formatting_monospace(tr::now);
}
} // namespace Core