2020-10-12 23:44:21 +03:00
|
|
|
/*
|
2020-10-19 20:34:47 +03:00
|
|
|
Copyright (C) 2020 Sebastian J. Wolf and other contributors
|
2020-10-12 23:44:21 +03:00
|
|
|
|
|
|
|
This file is part of Fernschreiber.
|
|
|
|
|
|
|
|
Fernschreiber is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Fernschreiber is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stickermanager.h"
|
|
|
|
#include <QListIterator>
|
|
|
|
|
2020-11-22 06:58:47 +03:00
|
|
|
#define DEBUG_MODULE StickerManager
|
|
|
|
#include "debuglog.h"
|
2020-10-12 23:44:21 +03:00
|
|
|
|
|
|
|
StickerManager::StickerManager(TDLibWrapper *tdLibWrapper, QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
LOG("Initializing...");
|
|
|
|
this->tdLibWrapper = tdLibWrapper;
|
|
|
|
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(recentStickersUpdated(QVariantList)), this, SLOT(handleRecentStickersUpdated(QVariantList)));
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(stickersReceived(QVariantList)), this, SLOT(handleStickersReceived(QVariantList)));
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(installedStickerSetsUpdated(QVariantList)), this, SLOT(handleInstalledStickerSetsUpdated(QVariantList)));
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(stickerSetsReceived(QVariantList)), this, SLOT(handleStickerSetsReceived(QVariantList)));
|
2020-10-15 00:25:56 +03:00
|
|
|
connect(this->tdLibWrapper, SIGNAL(stickerSetReceived(QVariantMap)), this, SLOT(handleStickerSetReceived(QVariantMap)));
|
2020-10-12 23:44:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
StickerManager::~StickerManager()
|
|
|
|
{
|
|
|
|
LOG("Destroying myself...");
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList StickerManager::getRecentStickers()
|
|
|
|
{
|
|
|
|
return this->recentStickers;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList StickerManager::getInstalledStickerSets()
|
|
|
|
{
|
|
|
|
return this->installedStickerSets;
|
|
|
|
}
|
|
|
|
|
2021-02-12 01:39:56 +03:00
|
|
|
QVariantMap StickerManager::getStickerSet(const QString &stickerSetId)
|
|
|
|
{
|
|
|
|
return this->stickerSets.value(stickerSetId).toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StickerManager::hasStickerSet(const QString &stickerSetId)
|
|
|
|
{
|
|
|
|
return this->stickerSets.contains(stickerSetId);
|
|
|
|
}
|
|
|
|
|
2021-02-13 02:34:01 +03:00
|
|
|
bool StickerManager::isStickerSetInstalled(const QString &stickerSetId)
|
|
|
|
{
|
|
|
|
return this->installedStickerSetIds.contains(stickerSetId);
|
|
|
|
}
|
|
|
|
|
2020-10-17 00:00:08 +03:00
|
|
|
bool StickerManager::needsReload()
|
|
|
|
{
|
|
|
|
return this->reloadNeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickerManager::setNeedsReload(const bool &reloadNeeded)
|
|
|
|
{
|
|
|
|
this->reloadNeeded = reloadNeeded;
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:44:21 +03:00
|
|
|
void StickerManager::handleRecentStickersUpdated(const QVariantList &stickerIds)
|
|
|
|
{
|
|
|
|
LOG("Receiving recent stickers...." << stickerIds);
|
|
|
|
this->recentStickerIds = stickerIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickerManager::handleStickersReceived(const QVariantList &stickers)
|
|
|
|
{
|
2020-10-16 01:02:58 +03:00
|
|
|
LOG("Receiving stickers....");
|
2020-10-12 23:44:21 +03:00
|
|
|
QListIterator<QVariant> stickersIterator(stickers);
|
|
|
|
while (stickersIterator.hasNext()) {
|
|
|
|
QVariantMap newSticker = stickersIterator.next().toMap();
|
|
|
|
this->stickers.insert(newSticker.value("sticker").toMap().value("id").toString(), newSticker);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->recentStickers.clear();
|
|
|
|
QListIterator<QVariant> stickerIdIterator(this->recentStickerIds);
|
|
|
|
while (stickerIdIterator.hasNext()) {
|
|
|
|
QString stickerId = stickerIdIterator.next().toString();
|
|
|
|
this->recentStickers.append(this->stickers.value(stickerId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickerManager::handleInstalledStickerSetsUpdated(const QVariantList &stickerSetIds)
|
|
|
|
{
|
|
|
|
LOG("Receiving installed sticker IDs...." << stickerSetIds);
|
|
|
|
this->installedStickerSetIds = stickerSetIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickerManager::handleStickerSetsReceived(const QVariantList &stickerSets)
|
|
|
|
{
|
2020-10-16 01:02:58 +03:00
|
|
|
LOG("Receiving sticker sets....");
|
2020-10-12 23:44:21 +03:00
|
|
|
QListIterator<QVariant> stickerSetsIterator(stickerSets);
|
|
|
|
while (stickerSetsIterator.hasNext()) {
|
|
|
|
QVariantMap newStickerSet = stickerSetsIterator.next().toMap();
|
2021-02-13 02:34:01 +03:00
|
|
|
QString newSetId = newStickerSet.value("id").toString();
|
|
|
|
bool isInstalled = newStickerSet.value("is_installed").toBool();
|
|
|
|
if (isInstalled && !this->installedStickerSetIds.contains(newSetId)) {
|
|
|
|
this->installedStickerSetIds.append(newSetId);
|
|
|
|
}
|
|
|
|
if (!isInstalled && this->installedStickerSetIds.contains(newSetId)) {
|
|
|
|
this->installedStickerSetIds.removeAll(newSetId);
|
|
|
|
}
|
|
|
|
this->stickerSets.insert(newSetId, newStickerSet);
|
2020-10-12 23:44:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this->installedStickerSets.clear();
|
|
|
|
QListIterator<QVariant> stickerSetIdIterator(this->installedStickerSetIds);
|
2020-10-15 00:25:56 +03:00
|
|
|
int i = 0;
|
|
|
|
this->stickerSetMap.clear();
|
2020-10-12 23:44:21 +03:00
|
|
|
while (stickerSetIdIterator.hasNext()) {
|
|
|
|
QString stickerSetId = stickerSetIdIterator.next().toString();
|
2021-02-12 01:39:56 +03:00
|
|
|
if (this->installedStickerSetIds.contains(stickerSetId)) {
|
|
|
|
this->installedStickerSets.append(this->stickerSets.value(stickerSetId));
|
|
|
|
this->stickerSetMap.insert(stickerSetId, i);
|
|
|
|
i++;
|
|
|
|
}
|
2020-10-12 23:44:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 00:25:56 +03:00
|
|
|
|
|
|
|
void StickerManager::handleStickerSetReceived(const QVariantMap &stickerSet)
|
|
|
|
{
|
|
|
|
QString stickerSetId = stickerSet.value("id").toString();
|
|
|
|
this->stickerSets.insert(stickerSetId, stickerSet);
|
2021-02-12 01:39:56 +03:00
|
|
|
if (this->installedStickerSetIds.contains(stickerSetId)) {
|
|
|
|
LOG("Receiving installed sticker set...." << stickerSetId);
|
|
|
|
int setIndex = this->stickerSetMap.value(stickerSetId).toInt();
|
|
|
|
this->installedStickerSets.replace(setIndex, stickerSet);
|
|
|
|
} else {
|
|
|
|
LOG("Receiving new sticker set...." << stickerSetId);
|
|
|
|
}
|
2020-10-16 00:43:55 +03:00
|
|
|
QVariantList stickerList = stickerSet.value("stickers").toList();
|
|
|
|
QListIterator<QVariant> stickerIterator(stickerList);
|
|
|
|
while (stickerIterator.hasNext()) {
|
|
|
|
QVariantMap singleSticker = stickerIterator.next().toMap();
|
2020-12-07 23:43:09 +03:00
|
|
|
QVariantMap thumbnailFile = singleSticker.value("thumbnail").toMap().value("file").toMap();
|
2020-10-16 00:43:55 +03:00
|
|
|
QVariantMap thumbnailLocalFile = thumbnailFile.value("local").toMap();
|
|
|
|
if (!thumbnailLocalFile.value("is_downloading_completed").toBool()) {
|
2020-11-08 07:00:13 +03:00
|
|
|
tdLibWrapper->downloadFile(thumbnailFile.value("id").toInt());
|
2020-10-17 00:00:08 +03:00
|
|
|
this->reloadNeeded = true;
|
2020-10-16 00:43:55 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 00:25:56 +03:00
|
|
|
}
|