2019-09-03 18:24:51 +03: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"
|
|
|
|
|
|
|
|
#include "data/data_session.h"
|
2019-09-03 21:04:38 +03:00
|
|
|
#include "data/data_document.h"
|
2019-09-03 18:24:51 +03:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
2019-09-03 21:04:38 +03:00
|
|
|
CloudTheme CloudTheme::Parse(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const MTPDtheme &data) {
|
|
|
|
const auto document = data.vdocument();
|
|
|
|
return {
|
|
|
|
data.vid().v,
|
|
|
|
data.vaccess_hash().v,
|
|
|
|
qs(data.vslug()),
|
|
|
|
qs(data.vtitle()),
|
|
|
|
(document
|
|
|
|
? session->data().processDocument(*document)->id
|
|
|
|
: DocumentId(0)),
|
|
|
|
data.is_creator() ? session->userId() : UserId(0)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:24:51 +03:00
|
|
|
QString CloudThemes::Format() {
|
|
|
|
static const auto kResult = QString::fromLatin1("tdesktop");
|
|
|
|
return kResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloudThemes::CloudThemes(not_null<Main::Session*> session)
|
|
|
|
: _session(session) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::refresh() {
|
|
|
|
if (_requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requestId = _session->api().request(MTPaccount_GetThemes(
|
|
|
|
MTP_string(Format()),
|
|
|
|
MTP_int(_hash)
|
|
|
|
)).done([=](const MTPaccount_Themes &result) {
|
|
|
|
result.match([&](const MTPDaccount_themes &data) {
|
|
|
|
_hash = data.vhash().v;
|
|
|
|
parseThemes(data.vthemes().v);
|
|
|
|
_updates.fire({});
|
|
|
|
}, [](const MTPDaccount_themesNotModified &) {
|
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_requestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudThemes::parseThemes(const QVector<MTPTheme> &list) {
|
|
|
|
_list.clear();
|
|
|
|
_list.reserve(list.size());
|
|
|
|
for (const auto &theme : list) {
|
|
|
|
theme.match([&](const MTPDtheme &data) {
|
2019-09-03 21:04:38 +03:00
|
|
|
_list.push_back(CloudTheme::Parse(_session, data));
|
2019-09-03 18:24:51 +03:00
|
|
|
}, [&](const MTPDthemeDocumentNotModified &data) {
|
|
|
|
LOG(("API Error: Unexpected themeDocumentNotModified."));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> CloudThemes::updated() const {
|
|
|
|
return _updates.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<CloudTheme> &CloudThemes::list() const {
|
|
|
|
return _list;
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:33:51 +03:00
|
|
|
void CloudThemes::apply(const CloudTheme &theme) {
|
|
|
|
const auto i = ranges::find(_list, theme.id, &CloudTheme::id);
|
|
|
|
if (i != end(_list)) {
|
|
|
|
*i = theme;
|
|
|
|
_updates.fire({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:24:51 +03:00
|
|
|
} // namespace Data
|