Support theme document file origins.

This commit is contained in:
John Preston 2019-09-09 11:51:07 +03:00
parent 51c1dc20e1
commit 9ff1fbcf47
6 changed files with 53 additions and 8 deletions

View File

@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_channel.h" #include "data/data_channel.h"
#include "data/data_chat.h" #include "data/data_chat.h"
#include "data/data_user.h" #include "data/data_user.h"
#include "data/data_cloud_themes.h"
#include "dialogs/dialogs_key.h" #include "dialogs/dialogs_key.h"
#include "core/core_cloud_password.h" #include "core/core_cloud_password.h"
#include "core/application.h" #include "core/application.h"
@ -3063,6 +3064,13 @@ void ApiWrap::refreshFileReference(
MTP_inputWallPaper( MTP_inputWallPaper(
MTP_long(data.paperId), MTP_long(data.paperId),
MTP_long(data.accessHash)))); MTP_long(data.accessHash))));
}, [&](Data::FileOriginTheme data) {
request(MTPaccount_GetTheme(
MTP_string(Data::CloudThemes::Format()),
MTP_inputTheme(
MTP_long(data.themeId),
MTP_long(data.accessHash)),
MTP_long(0)));
}, [&](std::nullopt_t) { }, [&](std::nullopt_t) {
fail(); fail();
}); });

View File

@ -182,7 +182,7 @@ void CloudThemes::showPreview(const CloudTheme &cloud) {
void CloudThemes::updateFromDocument( void CloudThemes::updateFromDocument(
const CloudTheme &cloud, const CloudTheme &cloud,
not_null<DocumentData*> document) { not_null<DocumentData*> document) {
loadDocumentAndInvoke(_updatingFrom, document, [=] { loadDocumentAndInvoke(_updatingFrom, cloud, document, [=] {
auto preview = Window::Theme::PreviewFromFile( auto preview = Window::Theme::PreviewFromFile(
document->data(), document->data(),
document->location().name(), document->location().name(),
@ -197,13 +197,14 @@ void CloudThemes::updateFromDocument(
void CloudThemes::previewFromDocument( void CloudThemes::previewFromDocument(
const CloudTheme &cloud, const CloudTheme &cloud,
not_null<DocumentData*> document) { not_null<DocumentData*> document) {
loadDocumentAndInvoke(_previewFrom, document, [=] { loadDocumentAndInvoke(_previewFrom, cloud, document, [=] {
Core::App().showTheme(document, cloud); Core::App().showTheme(document, cloud);
}); });
} }
void CloudThemes::loadDocumentAndInvoke( void CloudThemes::loadDocumentAndInvoke(
LoadingDocument &value, LoadingDocument &value,
const CloudTheme &cloud,
not_null<DocumentData*> document, not_null<DocumentData*> document,
Fn<void()> callback) { Fn<void()> callback) {
const auto alreadyWaiting = (value.document != nullptr); const auto alreadyWaiting = (value.document != nullptr);
@ -211,7 +212,9 @@ void CloudThemes::loadDocumentAndInvoke(
value.document->cancel(); value.document->cancel();
} }
value.document = document; value.document = document;
value.document->save(Data::FileOrigin(), QString()); // #TODO themes value.document->save(
Data::FileOriginTheme(cloud.id, cloud.accessHash),
QString());
value.callback = std::move(callback); value.callback = std::move(callback);
if (document->loaded()) { if (document->loaded()) {
invokeForLoaded(value); invokeForLoaded(value);

View File

@ -50,6 +50,7 @@ public:
private: private:
struct LoadingDocument { struct LoadingDocument {
CloudTheme theme;
DocumentData *document = nullptr; DocumentData *document = nullptr;
rpl::lifetime subscription; rpl::lifetime subscription;
Fn<void()> callback; Fn<void()> callback;
@ -71,6 +72,7 @@ private:
not_null<DocumentData*> document); not_null<DocumentData*> document);
void loadDocumentAndInvoke( void loadDocumentAndInvoke(
LoadingDocument &value, LoadingDocument &value,
const CloudTheme &cloud,
not_null<DocumentData*> document, not_null<DocumentData*> document,
Fn<void()> callback); Fn<void()> callback);
void invokeForLoaded(LoadingDocument &value); void invokeForLoaded(LoadingDocument &value);

View File

@ -38,6 +38,14 @@ struct FileReferenceAccumulator {
push(data.vdocument()); push(data.vdocument());
}); });
} }
void push(const MTPTheme &data) {
data.match([&](const MTPDtheme &data) {
if (const auto document = data.vdocument()) {
push(*document);
}
}, [&](const MTPDthemeDocumentNotModified &data) {
});
}
void push(const MTPWebPage &data) { void push(const MTPWebPage &data) {
data.match([&](const MTPDwebPage &data) { data.match([&](const MTPDwebPage &data) {
if (const auto document = data.vdocument()) { if (const auto document = data.vdocument()) {
@ -165,4 +173,8 @@ UpdatedFileReferences GetFileReferences(const MTPWallPaper &data) {
return GetFileReferencesHelper(data); return GetFileReferencesHelper(data);
} }
UpdatedFileReferences GetFileReferences(const MTPTheme &data) {
return GetFileReferencesHelper(data);
}
} // namespace Data } // namespace Data

View File

@ -74,6 +74,20 @@ struct FileOriginWallpaper {
} }
}; };
struct FileOriginTheme {
FileOriginTheme(uint64 themeId, uint64 accessHash)
: themeId(themeId)
, accessHash(accessHash) {
}
uint64 themeId = 0;
uint64 accessHash = 0;
inline bool operator<(const FileOriginTheme &other) const {
return themeId < other.themeId;
}
};
struct FileOrigin { struct FileOrigin {
using Variant = base::optional_variant< using Variant = base::optional_variant<
FileOriginMessage, FileOriginMessage,
@ -81,7 +95,8 @@ struct FileOrigin {
FileOriginPeerPhoto, FileOriginPeerPhoto,
FileOriginStickerSet, FileOriginStickerSet,
FileOriginSavedGifs, FileOriginSavedGifs,
FileOriginWallpaper>; FileOriginWallpaper,
FileOriginTheme>;
FileOrigin() = default; FileOrigin() = default;
FileOrigin(FileOriginMessage data) : data(data) { FileOrigin(FileOriginMessage data) : data(data) {
@ -96,6 +111,8 @@ struct FileOrigin {
} }
FileOrigin(FileOriginWallpaper data) : data(data) { FileOrigin(FileOriginWallpaper data) : data(data) {
} }
FileOrigin(FileOriginTheme data) : data(data) {
}
explicit operator bool() const { explicit operator bool() const {
return data.has_value(); return data.has_value();
@ -140,5 +157,6 @@ UpdatedFileReferences GetFileReferences(
UpdatedFileReferences GetFileReferences(const MTPmessages_StickerSet &data); UpdatedFileReferences GetFileReferences(const MTPmessages_StickerSet &data);
UpdatedFileReferences GetFileReferences(const MTPmessages_SavedGifs &data); UpdatedFileReferences GetFileReferences(const MTPmessages_SavedGifs &data);
UpdatedFileReferences GetFileReferences(const MTPWallPaper &data); UpdatedFileReferences GetFileReferences(const MTPWallPaper &data);
UpdatedFileReferences GetFileReferences(const MTPTheme &data);
} // namespace Data } // namespace Data

View File

@ -520,16 +520,18 @@ void CloudList::refreshElementUsing(
void CloudList::refreshColors(Element &element) { void CloudList::refreshColors(Element &element) {
const auto currentId = Background()->themeObject().cloud.id; const auto currentId = Background()->themeObject().cloud.id;
const auto documentId = element.theme.documentId; const auto &theme = element.theme;
const auto document = documentId const auto document = theme.documentId
? _window->session().data().document(documentId).get() ? _window->session().data().document(theme.documentId).get()
: nullptr; : nullptr;
if (element.id() == kFakeCloudThemeId if (element.id() == kFakeCloudThemeId
|| ((element.id() == currentId) || ((element.id() == currentId)
&& (!document || !document->isTheme()))) { && (!document || !document->isTheme()))) {
element.check->setColors(ColorsFromCurrentTheme()); element.check->setColors(ColorsFromCurrentTheme());
} else if (document) { } else if (document) {
document->save(Data::FileOrigin(), QString()); // #TODO themes document->save(
Data::FileOriginTheme(theme.id, theme.accessHash),
QString());
if (document->loaded()) { if (document->loaded()) {
refreshColorsFromDocument(element, document); refreshColorsFromDocument(element, document);
} else { } else {