1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
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"
#include "api/api_text_entities.h"
#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 "data/stickers/data_custom_emoji.h"
#include "data/data_session.h"
#include "ui/text/text_custom_emoji.h"
#include "ui/basic_click_handlers.h"
#include "ui/emoji_config.h"
#include "lang/lang_keys.h"
#include "platform/platform_specific.h"
#include "boxes/url_auth_box.h"
#include "main/main_account.h"
#include "main/main_session.h"
#include "main/main_app_config.h"
#include "window/window_controller.h"
#include "mainwindow.h"
namespace Core {
namespace {
const auto kGoodPrefix = u"https://"_q;
const auto kBadPrefix = u"http://"_q;
[[nodiscard]] QUrl UrlForAutoLogin(const QString &url) {
return (url.startsWith(kGoodPrefix, Qt::CaseInsensitive)
|| url.startsWith(kBadPrefix, Qt::CaseInsensitive))
? QUrl(url)
: QUrl();
}
[[nodiscard]] QString DomainForAutoLogin(const QUrl &url) {
return url.isValid() ? url.host().toLower() : QString();
}
[[nodiscard]] QString UrlWithAutoLoginToken(
const QString &url,
QUrl parsed,
const QString &domain) {
const auto &config = Core::App().activeAccount().appConfig();
const auto token = config.get<QString>("autologin_token", {});
const auto domains = config.get<std::vector<QString>>(
"autologin_domains",
{});
if (token.isEmpty()
|| domain.isEmpty()
|| !ranges::contains(domains, domain)) {
return url;
}
const auto added = "autologin_token=" + token;
parsed.setQuery(parsed.hasQuery()
? (parsed.query() + '&' + added)
: added);
if (url.startsWith(kBadPrefix, Qt::CaseInsensitive)) {
parsed.setScheme("https");
}
return QString::fromUtf8(parsed.toEncoded());
}
[[nodiscard]] bool BotAutoLogin(
const QString &url,
const QString &domain,
QVariant context) {
auto &account = Core::App().activeAccount();
const auto &config = account.appConfig();
const auto domains = config.get<std::vector<QString>>(
"url_auth_domains",
{});
if (!account.sessionExists()
|| domain.isEmpty()
|| !ranges::contains(domains, domain)) {
return false;
}
const auto good = url.startsWith(kBadPrefix, Qt::CaseInsensitive)
? (kGoodPrefix + url.mid(kBadPrefix.size()))
: url;
UrlAuthBox::Activate(&account.session(), good, context);
return true;
}
[[nodiscard]] QString OpenGLCheckFilePath() {
return cWorkingDir() + "tdata/opengl_crash_check";
}
[[nodiscard]] QString ANGLEBackendFilePath() {
return cWorkingDir() + "tdata/angle_backend";
}
} // namespace
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);
}
QString UiIntegration::emojiCacheFolder() {
return cWorkingDir() + "tdata/emoji";
}
QString UiIntegration::openglCheckFilePath() {
return OpenGLCheckFilePath();
}
QString UiIntegration::angleBackendFilePath() {
return ANGLEBackendFilePath();
}
void UiIntegration::textActionsUpdated() {
if (const auto window = Core::App().activeWindow()) {
window->widget()->updateGlobalMenu();
}
}
void UiIntegration::activationFromTopPanel() {
Platform::IgnoreApplicationActivationRightNow();
}
bool UiIntegration::screenIsLocked() {
return Core::App().screenIsLocked();
}
std::shared_ptr<ClickHandler> UiIntegration::createLinkHandler(
const EntityLinkData &data,
const std::any &context) {
const auto my = std::any_cast<MarkedTextContext>(&context);
switch (data.type) {
case EntityType::Url:
return (!data.data.isEmpty()
&& UrlClickHandler::IsSuspicious(data.data))
? std::make_shared<HiddenUrlClickHandler>(data.data)
: Integration::createLinkHandler(data, context);
case EntityType::CustomUrl:
return !data.data.isEmpty()
? std::make_shared<HiddenUrlClickHandler>(data.data)
: Integration::createLinkHandler(data, context);
case EntityType::BotCommand:
return std::make_shared<BotCommandClickHandler>(data.data);
case EntityType::Hashtag:
using HashtagMentionType = MarkedTextContext::HashtagMentionType;
if (my && my->type == HashtagMentionType::Twitter) {
return std::make_shared<UrlClickHandler>(
(u"https://twitter.com/hashtag/"_q
+ data.data.mid(1)
+ u"?src=hash"_q),
true);
} else if (my && my->type == HashtagMentionType::Instagram) {
return std::make_shared<UrlClickHandler>(
(u"https://instagram.com/explore/tags/"_q
+ data.data.mid(1)
+ '/'),
true);
}
return std::make_shared<HashtagClickHandler>(data.data);
case EntityType::Cashtag:
return std::make_shared<CashtagClickHandler>(data.data);
case EntityType::Mention:
using HashtagMentionType = MarkedTextContext::HashtagMentionType;
if (my && my->type == HashtagMentionType::Twitter) {
return std::make_shared<UrlClickHandler>(
u"https://twitter.com/"_q + data.data.mid(1),
true);
} else if (my && my->type == HashtagMentionType::Instagram) {
return std::make_shared<UrlClickHandler>(
u"https://instagram.com/"_q + data.data.mid(1) + '/',
true);
}
return std::make_shared<MentionClickHandler>(data.data);
case EntityType::MentionName: {
auto fields = TextUtilities::MentionNameDataToFields(data.data);
if (!my || !my->session) {
LOG(("Mention name without a session: %1").arg(data.data));
} else if (fields.userId) {
return std::make_shared<MentionNameClickHandler>(
my->session,
data.text,
fields.userId,
fields.accessHash);
} else {
LOG(("Bad mention name: %1").arg(data.data));
}
} break;
case EntityType::Code:
return std::make_shared<MonospaceClickHandler>(data.text, data.type);
case EntityType::Pre:
return std::make_shared<MonospaceClickHandler>(data.text, data.type);
}
return Integration::createLinkHandler(data, context);
}
bool UiIntegration::handleUrlClick(
const QString &url,
const QVariant &context) {
const auto local = Core::TryConvertUrlToLocal(url);
if (Core::InternalPassportLink(local)) {
return true;
}
if (UrlClickHandler::IsEmail(url)) {
File::OpenEmailLink(url);
return true;
} else if (local.startsWith(u"tg://"_q, Qt::CaseInsensitive)) {
Core::App().openLocalUrl(local, context);
return true;
} else if (local.startsWith(u"internal:"_q, Qt::CaseInsensitive)) {
Core::App().openInternalUrl(local, context);
return true;
}
auto parsed = UrlForAutoLogin(url);
const auto domain = DomainForAutoLogin(parsed);
const auto skip = context.value<ClickHandlerContext>().skipBotAutoLogin;
if (skip || !BotAutoLogin(url, domain, context)) {
File::OpenUrl(UrlWithAutoLoginToken(url, std::move(parsed), domain));
}
return true;
}
std::unique_ptr<Ui::Text::CustomEmoji> UiIntegration::createCustomEmoji(
const QString &data,
const std::any &context) {
const auto my = std::any_cast<MarkedTextContext>(&context);
if (!my || !my->session) {
return nullptr;
}
auto result = my->session->data().customEmojiManager().create(
data,
my->customEmojiRepaint);
if (my->customEmojiLoopLimit > 0) {
return std::make_unique<Ui::Text::LimitedLoopsEmoji>(
std::move(result),
my->customEmojiLoopLimit);
}
return result;
}
Fn<void()> UiIntegration::createSpoilerRepaint(const std::any &context) {
const auto my = std::any_cast<MarkedTextContext>(&context);
return my ? my->customEmojiRepaint : nullptr;
}
rpl::producer<> UiIntegration::forcePopupMenuHideRequests() {
return Core::App().passcodeLockChanges() | rpl::to_empty;
}
const Ui::Emoji::One *UiIntegration::defaultEmojiVariant(
const Ui::Emoji::One *emoji) {
if (!emoji || !emoji->hasVariants()) {
return emoji;
}
const auto nonColored = emoji->nonColoredId();
const auto &variants = Core::App().settings().emojiVariants();
const auto i = variants.find(nonColored);
const auto result = (i != end(variants))
? emoji->variant(i->second)
: emoji;
Core::App().settings().incrementRecentEmoji({ 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);
}
QString UiIntegration::phraseFormattingSpoiler() {
return tr::lng_menu_formatting_spoiler(tr::now);
}
QString UiIntegration::phraseButtonOk() {
return tr::lng_box_ok(tr::now);
}
QString UiIntegration::phraseButtonClose() {
return tr::lng_close(tr::now);
}
QString UiIntegration::phraseButtonCancel() {
return tr::lng_cancel(tr::now);
}
QString UiIntegration::phrasePanelCloseWarning() {
return tr::lng_bot_close_warning_title(tr::now);
}
QString UiIntegration::phrasePanelCloseUnsaved() {
return tr::lng_bot_close_warning(tr::now);
}
QString UiIntegration::phrasePanelCloseAnyway() {
return tr::lng_bot_close_warning_sure(tr::now);
}
#if 0 // disabled for now
QString UiIntegration::phraseBotSharePhone() {
return tr::lng_bot_share_phone(tr::now);
}
QString UiIntegration::phraseBotSharePhoneTitle() {
return tr::lng_settings_phone_label(tr::now);
}
QString UiIntegration::phraseBotSharePhoneConfirm() {
return tr::lng_bot_share_phone_confirm(tr::now);
}
#endif
bool OpenGLLastCheckFailed() {
return QFile::exists(OpenGLCheckFilePath());
}
} // namespace Core
|