2019-09-03 15:24:51 +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 "data/data_cloud_themes.h"
|
|
|
|
|
2019-09-06 13:35:26 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
|
|
|
#include "window/themes/window_theme_preview.h"
|
2019-09-06 14:24:22 +00:00
|
|
|
#include "window/themes/window_theme_editor_box.h"
|
|
|
|
#include "window/window_controller.h"
|
2019-09-03 15:24:51 +00:00
|
|
|
#include "data/data_session.h"
|
2019-09-03 18:04:38 +00:00
|
|
|
#include "data/data_document.h"
|
2019-09-06 13:35:26 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-04-09 12:27:53 +00:00
|
|
|
#include "data/data_document_media.h"
|
2019-09-03 15:24:51 +00:00
|
|
|
#include "main/main_session.h"
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2021-06-15 20:30:08 +00:00
|
|
|
#include "media/view/media_view_open_common.h"
|
2019-09-06 14:24:22 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-09-03 15:24:51 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Data {
|
2019-09-06 13:35:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-09-06 15:54:50 +00:00
|
|
|
constexpr auto kFirstReloadTimeout = 10 * crl::time(1000);
|
2019-09-06 13:35:26 +00:00
|
|
|
constexpr auto kReloadTimeout = 3600 * crl::time(1000);
|
|
|
|
|
2021-09-10 19:29:30 +00:00
|
|
|
bool IsTestingColors/* = false*/;
|
|
|
|
|
2019-09-06 13:35:26 +00:00
|
|
|
} // namespace
|
2019-09-03 15:24:51 +00:00
|
|
|
|
2019-09-03 18:04:38 +00:00
|
|
|
CloudTheme CloudTheme::Parse(
|
|
|
|
not_null<Main::Session*> session,
|
2021-08-25 16:16:50 +00:00
|
|
|
const MTPDtheme &data,
|
|
|
|
bool parseSettings) {
|
2019-09-03 18:04:38 +00:00
|
|
|
const auto document = data.vdocument();
|
2021-08-25 16:16:50 +00:00
|
|
|
const auto paper = [&]() -> std::optional<WallPaper> {
|
|
|
|
if (const auto settings = data.vsettings()) {
|
2021-08-26 13:25:48 +00:00
|
|
|
return settings->match([&](const MTPDthemeSettings &data) {
|
2021-08-25 16:16:50 +00:00
|
|
|
return data.vwallpaper()
|
2021-08-26 13:25:48 +00:00
|
|
|
? WallPaper::Create(session, *data.vwallpaper())
|
2021-08-25 16:16:50 +00:00
|
|
|
: std::nullopt;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
const auto outgoingMessagesColors = [&] {
|
|
|
|
auto result = std::vector<QColor>();
|
|
|
|
if (const auto settings = data.vsettings()) {
|
|
|
|
settings->match([&](const MTPDthemeSettings &data) {
|
|
|
|
if (const auto colors = data.vmessage_colors()) {
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &color : colors->v) {
|
2021-08-25 16:16:50 +00:00
|
|
|
result.push_back(ColorFromSerialized(color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const auto accentColor = [&]() -> std::optional<QColor> {
|
|
|
|
if (const auto settings = data.vsettings()) {
|
2021-08-26 13:25:48 +00:00
|
|
|
return settings->match([&](const MTPDthemeSettings &data) {
|
2021-08-25 16:16:50 +00:00
|
|
|
return ColorFromSerialized(data.vaccent_color().v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto outgoingAccentColor = [&]() -> std::optional<QColor> {
|
|
|
|
if (const auto settings = data.vsettings()) {
|
|
|
|
return settings->match([&](const MTPDthemeSettings &data) {
|
|
|
|
return MaybeColorFromSerialized(data.voutbox_accent_color());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
2021-08-26 13:25:48 +00:00
|
|
|
const auto basedOnDark = [&] {
|
|
|
|
if (const auto settings = data.vsettings()) {
|
|
|
|
return settings->match([&](const MTPDthemeSettings &data) {
|
|
|
|
return data.vbase_theme().match([](
|
|
|
|
const MTPDbaseThemeNight &) {
|
|
|
|
return true;
|
|
|
|
}, [](const MTPDbaseThemeTinted &) {
|
|
|
|
return true;
|
|
|
|
}, [](const auto &) {
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2019-09-03 18:04:38 +00:00
|
|
|
return {
|
2021-08-25 16:16:50 +00:00
|
|
|
.id = data.vid().v,
|
|
|
|
.accessHash = data.vaccess_hash().v,
|
|
|
|
.slug = qs(data.vslug()),
|
|
|
|
.title = qs(data.vtitle()),
|
|
|
|
.documentId = (document
|
2019-09-03 18:04:38 +00:00
|
|
|
? session->data().processDocument(*document)->id
|
|
|
|
: DocumentId(0)),
|
2021-08-25 16:16:50 +00:00
|
|
|
.createdBy = data.is_creator() ? session->userId() : UserId(0),
|
|
|
|
.usersCount = data.vinstalls_count().value_or_empty(),
|
|
|
|
.paper = parseSettings ? paper() : std::nullopt,
|
|
|
|
.accentColor = parseSettings ? accentColor() : std::nullopt,
|
2021-08-25 08:15:05 +00:00
|
|
|
.outgoingAccentColor = (parseSettings
|
|
|
|
? outgoingAccentColor()
|
|
|
|
: std::nullopt),
|
2021-08-25 16:16:50 +00:00
|
|
|
.outgoingMessagesColors = (parseSettings
|
|
|
|
? outgoingMessagesColors()
|
|
|
|
: std::vector<QColor>()),
|
2021-08-26 13:25:48 +00:00
|
|
|
.basedOnDark = parseSettings && basedOnDark(),
|
2019-09-03 18:04:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:16:50 +00:00
|
|
|
CloudTheme CloudTheme::Parse(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPTheme &data,
|
|
|
|
bool parseSettings) {
|
|
|
|
return data.match([&](const MTPDtheme &data) {
|
|
|
|
return CloudTheme::Parse(session, data, parseSettings);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:24:51 +00:00
|
|
|
QString CloudThemes::Format() {
|
|
|
|
static const auto kResult = QString::fromLatin1("tdesktop");
|
|
|
|
return kResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloudThemes::CloudThemes(not_null<Main::Session*> session)
|
2019-09-06 13:35:26 +00:00
|
|
|
: _session(session)
|
|
|
|
, _reloadCurrentTimer([=] { reloadCurrent(); }) {
|
|
|
|
setupReload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::setupReload() {
|
|
|
|
using namespace Window::Theme;
|
|
|
|
|
|
|
|
if (needReload()) {
|
|
|
|
_reloadCurrentTimer.callOnce(kFirstReloadTimeout);
|
|
|
|
}
|
2021-07-15 11:12:38 +00:00
|
|
|
Background()->updates(
|
2019-09-06 13:35:26 +00:00
|
|
|
) | rpl::filter([](const BackgroundUpdate &update) {
|
|
|
|
return (update.type == BackgroundUpdate::Type::ApplyingTheme);
|
|
|
|
}) | rpl::map([=] {
|
|
|
|
return needReload();
|
|
|
|
}) | rpl::start_with_next([=](bool need) {
|
2019-09-06 13:52:16 +00:00
|
|
|
install();
|
2019-09-06 13:35:26 +00:00
|
|
|
if (need) {
|
|
|
|
scheduleReload();
|
|
|
|
} else {
|
|
|
|
_reloadCurrentTimer.cancel();
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudThemes::needReload() const {
|
|
|
|
const auto &fields = Window::Theme::Background()->themeObject().cloud;
|
|
|
|
return fields.id && fields.documentId;
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:52:16 +00:00
|
|
|
void CloudThemes::install() {
|
|
|
|
using namespace Window::Theme;
|
|
|
|
|
|
|
|
const auto &fields = Background()->themeObject().cloud;
|
|
|
|
auto &themeId = IsNightMode()
|
|
|
|
? _installedNightThemeId
|
|
|
|
: _installedDayThemeId;
|
|
|
|
const auto cloudId = fields.documentId ? fields.id : uint64(0);
|
|
|
|
if (themeId == cloudId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
themeId = cloudId;
|
|
|
|
using Flag = MTPaccount_InstallTheme::Flag;
|
|
|
|
const auto flags = (IsNightMode() ? Flag::f_dark : Flag(0))
|
|
|
|
| Flag::f_format
|
|
|
|
| (themeId ? Flag::f_theme : Flag(0));
|
|
|
|
_session->api().request(MTPaccount_InstallTheme(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_string(Format()),
|
|
|
|
MTP_inputTheme(MTP_long(cloudId), MTP_long(fields.accessHash))
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2019-09-06 13:35:26 +00:00
|
|
|
void CloudThemes::reloadCurrent() {
|
|
|
|
if (!needReload()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto &fields = Window::Theme::Background()->themeObject().cloud;
|
|
|
|
_session->api().request(MTPaccount_GetTheme(
|
|
|
|
MTP_string(Format()),
|
|
|
|
MTP_inputTheme(MTP_long(fields.id), MTP_long(fields.accessHash)),
|
|
|
|
MTP_long(fields.documentId)
|
|
|
|
)).done([=](const MTPTheme &result) {
|
|
|
|
applyUpdate(result);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2019-09-06 13:35:26 +00:00
|
|
|
_reloadCurrentTimer.callOnce(kReloadTimeout);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::applyUpdate(const MTPTheme &theme) {
|
|
|
|
theme.match([&](const MTPDtheme &data) {
|
|
|
|
const auto cloud = CloudTheme::Parse(_session, data);
|
|
|
|
const auto &object = Window::Theme::Background()->themeObject();
|
|
|
|
if ((cloud.id != object.cloud.id)
|
2019-09-06 14:24:22 +00:00
|
|
|
|| (cloud.documentId == object.cloud.documentId)
|
|
|
|
|| !cloud.documentId) {
|
2019-09-06 13:35:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-09 09:13:07 +00:00
|
|
|
applyFromDocument(cloud);
|
2019-09-06 13:35:26 +00:00
|
|
|
});
|
|
|
|
scheduleReload();
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:24:22 +00:00
|
|
|
void CloudThemes::resolve(
|
2021-06-15 20:30:08 +00:00
|
|
|
not_null<Window::Controller*> controller,
|
2019-09-06 14:24:22 +00:00
|
|
|
const QString &slug,
|
|
|
|
const FullMsgId &clickFromMessageId) {
|
|
|
|
_session->api().request(_resolveRequestId).cancel();
|
|
|
|
_resolveRequestId = _session->api().request(MTPaccount_GetTheme(
|
|
|
|
MTP_string(Format()),
|
|
|
|
MTP_inputThemeSlug(MTP_string(slug)),
|
|
|
|
MTP_long(0)
|
|
|
|
)).done([=](const MTPTheme &result) {
|
2021-06-15 20:30:08 +00:00
|
|
|
showPreview(controller, result);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2019-09-06 14:24:22 +00:00
|
|
|
if (error.type() == qstr("THEME_FORMAT_INVALID")) {
|
2021-10-18 22:28:08 +00:00
|
|
|
controller->show(Box<Ui::InformBox>(
|
2019-09-06 14:24:22 +00:00
|
|
|
tr::lng_theme_no_desktop(tr::now)));
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:30:08 +00:00
|
|
|
void CloudThemes::showPreview(
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
const MTPTheme &data) {
|
2019-09-06 15:31:01 +00:00
|
|
|
data.match([&](const MTPDtheme &data) {
|
2021-06-15 20:30:08 +00:00
|
|
|
showPreview(controller, CloudTheme::Parse(_session, data));
|
2019-09-06 15:31:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:30:08 +00:00
|
|
|
void CloudThemes::showPreview(
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
const CloudTheme &cloud) {
|
2019-09-06 15:31:01 +00:00
|
|
|
if (const auto documentId = cloud.documentId) {
|
2021-06-15 20:30:08 +00:00
|
|
|
previewFromDocument(controller, cloud);
|
2019-09-06 15:31:01 +00:00
|
|
|
} else if (cloud.createdBy == _session->userId()) {
|
2021-06-15 20:30:08 +00:00
|
|
|
controller->show(Box(
|
2019-09-06 15:31:01 +00:00
|
|
|
Window::Theme::CreateForExistingBox,
|
2021-06-15 20:30:08 +00:00
|
|
|
controller,
|
2019-09-06 15:31:01 +00:00
|
|
|
cloud));
|
|
|
|
} else {
|
2021-10-18 22:28:08 +00:00
|
|
|
controller->show(Box<Ui::InformBox>(
|
2019-09-06 15:31:01 +00:00
|
|
|
tr::lng_theme_no_desktop(tr::now)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 09:13:07 +00:00
|
|
|
void CloudThemes::applyFromDocument(const CloudTheme &cloud) {
|
|
|
|
const auto document = _session->data().document(cloud.documentId);
|
2020-04-09 12:27:53 +00:00
|
|
|
loadDocumentAndInvoke(_updatingFrom, cloud, document, [=](
|
|
|
|
std::shared_ptr<Data::DocumentMedia> media) {
|
|
|
|
const auto document = media->owner();
|
2019-09-06 15:31:01 +00:00
|
|
|
auto preview = Window::Theme::PreviewFromFile(
|
2020-04-09 12:27:53 +00:00
|
|
|
media->bytes(),
|
2019-09-06 15:31:01 +00:00
|
|
|
document->location().name(),
|
|
|
|
cloud);
|
|
|
|
if (preview) {
|
|
|
|
Window::Theme::Apply(std::move(preview));
|
2019-09-06 15:54:50 +00:00
|
|
|
Window::Theme::KeepApplied();
|
2019-09-06 15:31:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:30:08 +00:00
|
|
|
void CloudThemes::previewFromDocument(
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
const CloudTheme &cloud) {
|
|
|
|
const auto sessionController = controller->sessionController();
|
|
|
|
if (!sessionController) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-09 09:13:07 +00:00
|
|
|
const auto document = _session->data().document(cloud.documentId);
|
2020-04-09 12:27:53 +00:00
|
|
|
loadDocumentAndInvoke(_previewFrom, cloud, document, [=](
|
|
|
|
std::shared_ptr<Data::DocumentMedia> media) {
|
|
|
|
const auto document = media->owner();
|
2021-06-15 20:30:08 +00:00
|
|
|
using Open = Media::View::OpenRequest;
|
|
|
|
controller->openInMediaView(Open(sessionController, document, cloud));
|
2019-09-06 15:31:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::loadDocumentAndInvoke(
|
|
|
|
LoadingDocument &value,
|
2019-09-09 08:51:07 +00:00
|
|
|
const CloudTheme &cloud,
|
2019-09-06 15:31:01 +00:00
|
|
|
not_null<DocumentData*> document,
|
2020-04-09 12:27:53 +00:00
|
|
|
Fn<void(std::shared_ptr<Data::DocumentMedia>)> callback) {
|
2019-09-06 15:31:01 +00:00
|
|
|
const auto alreadyWaiting = (value.document != nullptr);
|
|
|
|
if (alreadyWaiting) {
|
|
|
|
value.document->cancel();
|
|
|
|
}
|
|
|
|
value.document = document;
|
2020-04-09 12:27:53 +00:00
|
|
|
value.documentMedia = document->createMediaView();
|
2019-09-09 08:51:07 +00:00
|
|
|
value.document->save(
|
|
|
|
Data::FileOriginTheme(cloud.id, cloud.accessHash),
|
|
|
|
QString());
|
2019-09-06 15:31:01 +00:00
|
|
|
value.callback = std::move(callback);
|
2020-04-09 14:02:09 +00:00
|
|
|
if (value.documentMedia->loaded()) {
|
2019-09-06 15:31:01 +00:00
|
|
|
invokeForLoaded(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!alreadyWaiting) {
|
2020-07-01 08:03:34 +00:00
|
|
|
_session->downloaderTaskFinished(
|
2020-04-09 14:02:09 +00:00
|
|
|
) | rpl::filter([=, &value] {
|
|
|
|
return value.documentMedia->loaded();
|
2019-09-06 15:31:01 +00:00
|
|
|
}) | rpl::start_with_next([=, &value] {
|
|
|
|
invokeForLoaded(value);
|
|
|
|
}, value.subscription);
|
2019-09-06 13:35:26 +00:00
|
|
|
}
|
2019-09-06 15:31:01 +00:00
|
|
|
}
|
2019-09-06 13:35:26 +00:00
|
|
|
|
2019-09-06 15:31:01 +00:00
|
|
|
void CloudThemes::invokeForLoaded(LoadingDocument &value) {
|
|
|
|
const auto onstack = std::move(value.callback);
|
2020-04-09 12:27:53 +00:00
|
|
|
auto media = std::move(value.documentMedia);
|
2019-09-06 15:31:01 +00:00
|
|
|
value = LoadingDocument();
|
2020-04-09 12:27:53 +00:00
|
|
|
onstack(std::move(media));
|
2019-09-06 13:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::scheduleReload() {
|
|
|
|
if (needReload()) {
|
|
|
|
_reloadCurrentTimer.callOnce(kReloadTimeout);
|
|
|
|
} else {
|
|
|
|
_reloadCurrentTimer.cancel();
|
|
|
|
}
|
2019-09-03 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::refresh() {
|
2021-08-25 16:16:50 +00:00
|
|
|
if (_refreshRequestId) {
|
2019-09-03 15:24:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-08-25 16:16:50 +00:00
|
|
|
_refreshRequestId = _session->api().request(MTPaccount_GetThemes(
|
2019-09-03 15:24:51 +00:00
|
|
|
MTP_string(Format()),
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(_hash)
|
2019-09-03 15:24:51 +00:00
|
|
|
)).done([=](const MTPaccount_Themes &result) {
|
2021-08-25 16:16:50 +00:00
|
|
|
_refreshRequestId = 0;
|
2019-09-03 15:24:51 +00:00
|
|
|
result.match([&](const MTPDaccount_themes &data) {
|
|
|
|
_hash = data.vhash().v;
|
|
|
|
parseThemes(data.vthemes().v);
|
|
|
|
_updates.fire({});
|
|
|
|
}, [](const MTPDaccount_themesNotModified &) {
|
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-08-25 16:16:50 +00:00
|
|
|
_refreshRequestId = 0;
|
2019-09-03 15:24:51 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::parseThemes(const QVector<MTPTheme> &list) {
|
|
|
|
_list.clear();
|
|
|
|
_list.reserve(list.size());
|
|
|
|
for (const auto &theme : list) {
|
2021-08-25 16:16:50 +00:00
|
|
|
_list.push_back(CloudTheme::Parse(_session, theme));
|
2019-09-03 15:24:51 +00:00
|
|
|
}
|
2019-09-09 06:59:57 +00:00
|
|
|
checkCurrentTheme();
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:16:50 +00:00
|
|
|
void CloudThemes::refreshChatThemes() {
|
|
|
|
if (_chatThemesRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_chatThemesRequestId = _session->api().request(MTPaccount_GetChatThemes(
|
|
|
|
MTP_int(_chatThemesHash)
|
|
|
|
)).done([=](const MTPaccount_ChatThemes &result) {
|
|
|
|
_chatThemesRequestId = 0;
|
|
|
|
result.match([&](const MTPDaccount_chatThemes &data) {
|
|
|
|
_hash = data.vhash().v;
|
|
|
|
parseChatThemes(data.vthemes().v);
|
|
|
|
_chatThemesUpdates.fire({});
|
|
|
|
}, [](const MTPDaccount_chatThemesNotModified &) {
|
|
|
|
});
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_chatThemesRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<ChatTheme> &CloudThemes::chatThemes() const {
|
|
|
|
return _chatThemes;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> CloudThemes::chatThemesUpdated() const {
|
|
|
|
return _chatThemesUpdates.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ChatTheme> CloudThemes::themeForEmoji(
|
2021-09-24 15:10:25 +00:00
|
|
|
const QString &emoticon) const {
|
|
|
|
const auto emoji = Ui::Emoji::Find(emoticon);
|
|
|
|
if (!emoji) {
|
2021-08-25 16:16:50 +00:00
|
|
|
return {};
|
|
|
|
}
|
2021-09-24 15:10:25 +00:00
|
|
|
const auto i = ranges::find(_chatThemes, emoji, [](const ChatTheme &v) {
|
|
|
|
return Ui::Emoji::Find(v.emoticon);
|
|
|
|
});
|
2021-08-25 16:16:50 +00:00
|
|
|
return (i != end(_chatThemes)) ? std::make_optional(*i) : std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:25:48 +00:00
|
|
|
rpl::producer<std::optional<ChatTheme>> CloudThemes::themeForEmojiValue(
|
2021-09-24 15:10:25 +00:00
|
|
|
const QString &emoticon) {
|
2021-09-10 19:29:30 +00:00
|
|
|
const auto testing = TestingColors();
|
2021-09-24 15:10:25 +00:00
|
|
|
if (!Ui::Emoji::Find(emoticon)) {
|
2021-08-26 13:25:48 +00:00
|
|
|
return rpl::single<std::optional<ChatTheme>>(std::nullopt);
|
2021-09-24 15:10:25 +00:00
|
|
|
} else if (auto result = themeForEmoji(emoticon)) {
|
2021-09-10 19:29:30 +00:00
|
|
|
if (testing) {
|
|
|
|
return rpl::single(
|
|
|
|
std::move(result)
|
|
|
|
) | rpl::then(chatThemesUpdated(
|
|
|
|
) | rpl::map([=] {
|
2021-09-24 15:10:25 +00:00
|
|
|
return themeForEmoji(emoticon);
|
2021-09-10 19:29:30 +00:00
|
|
|
}) | rpl::filter([](const std::optional<ChatTheme> &theme) {
|
|
|
|
return theme.has_value();
|
|
|
|
}));
|
|
|
|
}
|
2021-08-26 13:25:48 +00:00
|
|
|
return rpl::single(std::move(result));
|
|
|
|
}
|
|
|
|
refreshChatThemes();
|
2021-09-10 19:29:30 +00:00
|
|
|
const auto limit = testing ? (1 << 20) : 1;
|
2021-08-26 13:25:48 +00:00
|
|
|
return rpl::single<std::optional<ChatTheme>>(
|
|
|
|
std::nullopt
|
|
|
|
) | rpl::then(chatThemesUpdated(
|
|
|
|
) | rpl::map([=] {
|
2021-09-24 15:10:25 +00:00
|
|
|
return themeForEmoji(emoticon);
|
2021-08-26 13:25:48 +00:00
|
|
|
}) | rpl::filter([](const std::optional<ChatTheme> &theme) {
|
|
|
|
return theme.has_value();
|
2021-09-10 19:29:30 +00:00
|
|
|
}) | rpl::take(limit));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudThemes::TestingColors() {
|
|
|
|
return IsTestingColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::SetTestingColors(bool testing) {
|
|
|
|
IsTestingColors = testing;
|
|
|
|
}
|
|
|
|
|
2021-09-19 08:39:38 +00:00
|
|
|
QString CloudThemes::prepareTestingLink(const CloudTheme &theme) const {
|
2021-09-10 19:29:30 +00:00
|
|
|
const auto hex = [](int value) {
|
|
|
|
return QChar((value < 10) ? ('0' + value) : ('a' + (value - 10)));
|
|
|
|
};
|
|
|
|
const auto hex2 = [&](int value) {
|
|
|
|
return QString() + hex(value / 16) + hex(value % 16);
|
|
|
|
};
|
|
|
|
const auto color = [&](const QColor &color) {
|
|
|
|
return hex2(color.red()) + hex2(color.green()) + hex2(color.blue());
|
|
|
|
};
|
|
|
|
const auto colors = [&](const std::vector<QColor> &colors) {
|
|
|
|
auto list = QStringList();
|
|
|
|
for (const auto &c : colors) {
|
|
|
|
list.push_back(color(c));
|
|
|
|
}
|
|
|
|
return list.join(",");
|
|
|
|
};
|
|
|
|
auto arguments = QStringList();
|
|
|
|
if (theme.basedOnDark) {
|
|
|
|
arguments.push_back("dark=1");
|
|
|
|
}
|
|
|
|
if (theme.accentColor) {
|
|
|
|
arguments.push_back("accent=" + color(*theme.accentColor));
|
|
|
|
}
|
|
|
|
if (theme.paper && !theme.paper->backgroundColors().empty()) {
|
|
|
|
arguments.push_back("bg=" + colors(theme.paper->backgroundColors()));
|
|
|
|
}
|
2021-09-19 08:39:38 +00:00
|
|
|
if (theme.paper/* && theme.paper->hasShareUrl()*/) {
|
|
|
|
arguments.push_back("intensity="
|
|
|
|
+ QString::number(theme.paper->patternIntensity()));
|
|
|
|
//const auto url = theme.paper->shareUrl(_session);
|
|
|
|
//const auto from = url.indexOf("bg/");
|
|
|
|
//const auto till = url.indexOf("?");
|
|
|
|
//if (from > 0 && till > from) {
|
|
|
|
// arguments.push_back("slug=" + url.mid(from + 3, till - from - 3));
|
|
|
|
//}
|
|
|
|
}
|
2021-09-10 19:29:30 +00:00
|
|
|
if (theme.outgoingAccentColor) {
|
|
|
|
arguments.push_back("out_accent" + color(*theme.outgoingAccentColor));
|
|
|
|
}
|
|
|
|
if (!theme.outgoingMessagesColors.empty()) {
|
|
|
|
arguments.push_back("out_bg=" + colors(theme.outgoingMessagesColors));
|
|
|
|
}
|
|
|
|
return arguments.isEmpty()
|
|
|
|
? QString()
|
|
|
|
: ("tg://test_chat_theme?" + arguments.join("&"));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<CloudTheme> CloudThemes::updateThemeFromLink(
|
2021-09-24 15:10:25 +00:00
|
|
|
const QString &emoticon,
|
2021-09-10 19:29:30 +00:00
|
|
|
const QMap<QString, QString> ¶ms) {
|
2021-09-24 15:10:25 +00:00
|
|
|
const auto emoji = Ui::Emoji::Find(emoticon);
|
|
|
|
if (!TestingColors() || !emoji) {
|
2021-09-10 19:29:30 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-09-24 15:10:25 +00:00
|
|
|
const auto i = ranges::find(_chatThemes, emoji, [](const ChatTheme &v) {
|
|
|
|
return Ui::Emoji::Find(v.emoticon);
|
|
|
|
});
|
2021-09-10 19:29:30 +00:00
|
|
|
if (i == end(_chatThemes)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
const auto hex = [](const QString &value) {
|
|
|
|
return (value.size() != 1)
|
|
|
|
? std::nullopt
|
|
|
|
: (value[0] >= 'a' && value[0] <= 'f')
|
|
|
|
? std::make_optional(10 + int(value[0].unicode() - 'a'))
|
2021-09-12 17:56:04 +00:00
|
|
|
: (value[0] >= 'A' && value[0] <= 'F')
|
|
|
|
? std::make_optional(10 + int(value[0].unicode() - 'A'))
|
2021-09-10 19:29:30 +00:00
|
|
|
: (value[0] >= '0' && value[0] <= '9')
|
|
|
|
? std::make_optional(int(value[0].unicode() - '0'))
|
|
|
|
: std::nullopt;
|
|
|
|
};
|
|
|
|
const auto hex2 = [&](const QString &value) {
|
|
|
|
const auto first = hex(value.mid(0, 1));
|
|
|
|
const auto second = hex(value.mid(1, 1));
|
|
|
|
return (first && second)
|
|
|
|
? std::make_optional((*first) * 16 + (*second))
|
|
|
|
: std::nullopt;
|
|
|
|
};
|
|
|
|
const auto color = [&](const QString &value) {
|
|
|
|
const auto red = hex2(value.mid(0, 2));
|
|
|
|
const auto green = hex2(value.mid(2, 2));
|
|
|
|
const auto blue = hex2(value.mid(4, 2));
|
|
|
|
return (red && green && blue)
|
|
|
|
? std::make_optional(QColor(*red, *green, *blue))
|
|
|
|
: std::nullopt;
|
|
|
|
};
|
|
|
|
const auto colors = [&](const QString &value) {
|
|
|
|
auto list = value.split(",");
|
|
|
|
auto result = std::vector<QColor>();
|
|
|
|
for (const auto &single : list) {
|
|
|
|
if (const auto c = color(single)) {
|
|
|
|
result.push_back(*c);
|
|
|
|
} else {
|
|
|
|
return std::vector<QColor>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (result.size() > 4) ? std::vector<QColor>() : result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &applyTo = params["dark"].isEmpty() ? i->light : i->dark;
|
|
|
|
applyTo.accentColor = color(params["accent"]);
|
|
|
|
const auto bg = colors(params["bg"]);
|
|
|
|
applyTo.paper = (applyTo.paper && !bg.empty())
|
|
|
|
? std::make_optional(applyTo.paper->withBackgroundColors(bg))
|
2021-09-19 08:39:38 +00:00
|
|
|
: applyTo.paper;
|
|
|
|
applyTo.paper = (applyTo.paper && params["intensity"].toInt())
|
|
|
|
? std::make_optional(
|
|
|
|
applyTo.paper->withPatternIntensity(params["intensity"].toInt()))
|
|
|
|
: applyTo.paper;
|
2021-09-10 19:29:30 +00:00
|
|
|
applyTo.outgoingAccentColor = color(params["out_accent"]);
|
|
|
|
applyTo.outgoingMessagesColors = colors(params["out_bg"]);
|
|
|
|
_chatThemesUpdates.fire({});
|
|
|
|
return applyTo;
|
2021-08-26 13:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:16:50 +00:00
|
|
|
void CloudThemes::parseChatThemes(const QVector<MTPChatTheme> &list) {
|
|
|
|
_chatThemes.clear();
|
|
|
|
_chatThemes.reserve(list.size());
|
|
|
|
for (const auto &theme : list) {
|
|
|
|
theme.match([&](const MTPDchatTheme &data) {
|
|
|
|
_chatThemes.push_back({
|
2021-09-24 15:10:25 +00:00
|
|
|
.emoticon = qs(data.vemoticon()),
|
2021-08-25 16:16:50 +00:00
|
|
|
.light = CloudTheme::Parse(_session, data.vtheme(), true),
|
|
|
|
.dark = CloudTheme::Parse(_session, data.vdark_theme(), true),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 06:59:57 +00:00
|
|
|
void CloudThemes::checkCurrentTheme() {
|
|
|
|
const auto &object = Window::Theme::Background()->themeObject();
|
|
|
|
if (!object.cloud.id || !object.cloud.documentId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = ranges::find(_list, object.cloud.id, &CloudTheme::id);
|
|
|
|
if (i == end(_list)) {
|
|
|
|
install();
|
|
|
|
}
|
2019-09-03 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> CloudThemes::updated() const {
|
|
|
|
return _updates.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<CloudTheme> &CloudThemes::list() const {
|
|
|
|
return _list;
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:01:45 +00:00
|
|
|
void CloudThemes::savedFromEditor(const CloudTheme &theme) {
|
2019-09-06 11:33:51 +00:00
|
|
|
const auto i = ranges::find(_list, theme.id, &CloudTheme::id);
|
|
|
|
if (i != end(_list)) {
|
|
|
|
*i = theme;
|
|
|
|
_updates.fire({});
|
2019-09-08 18:01:45 +00:00
|
|
|
} else {
|
|
|
|
_list.insert(begin(_list), theme);
|
|
|
|
_updates.fire({});
|
2019-09-06 11:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 17:05:26 +00:00
|
|
|
void CloudThemes::remove(uint64 cloudThemeId) {
|
|
|
|
const auto i = ranges::find(_list, cloudThemeId, &CloudTheme::id);
|
|
|
|
if (i == end(_list)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_session->api().request(MTPaccount_SaveTheme(
|
|
|
|
MTP_inputTheme(
|
|
|
|
MTP_long(i->id),
|
|
|
|
MTP_long(i->accessHash)),
|
|
|
|
MTP_bool(true)
|
|
|
|
)).send();
|
|
|
|
_list.erase(i);
|
|
|
|
_updates.fire({});
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:24:51 +00:00
|
|
|
} // namespace Data
|