File indexing completed on 2024-12-08 07:33:47

0001 // SPDX-FileCopyrightText: 2021-2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "stickermodel.h"
0005 
0006 #include "models/imagepacksmodel.h"
0007 
0008 using namespace Quotient;
0009 
0010 StickerModel::StickerModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012 {
0013 }
0014 
0015 int StickerModel::rowCount(const QModelIndex &index) const
0016 {
0017     Q_UNUSED(index);
0018     return m_images.size();
0019 }
0020 QVariant StickerModel::data(const QModelIndex &index, int role) const
0021 {
0022     const auto &row = index.row();
0023     const auto &image = m_images[row];
0024     if (role == UrlRole) {
0025         return m_room->connection()->makeMediaUrl(image.url);
0026     }
0027     if (role == BodyRole) {
0028         if (image.body) {
0029             return *image.body;
0030         }
0031     }
0032     if (role == IsStickerRole) {
0033         if (image.usage) {
0034             return image.usage->isEmpty() || image.usage->contains("sticker"_ls);
0035         }
0036         return true;
0037     }
0038     if (role == IsEmojiRole) {
0039         if (image.usage) {
0040             return image.usage->isEmpty() || image.usage->contains("emoticon"_ls);
0041         }
0042         return true;
0043     }
0044     return {};
0045 }
0046 
0047 QHash<int, QByteArray> StickerModel::roleNames() const
0048 {
0049     return {
0050         {UrlRole, "url"},
0051         {BodyRole, "body"},
0052         {IsStickerRole, "isSticker"},
0053         {IsEmojiRole, "isEmoji"},
0054     };
0055 }
0056 ImagePacksModel *StickerModel::model() const
0057 {
0058     return m_model;
0059 }
0060 
0061 void StickerModel::setModel(ImagePacksModel *model)
0062 {
0063     if (m_model) {
0064         disconnect(m_model, nullptr, this, nullptr);
0065     }
0066     connect(model, &ImagePacksModel::roomChanged, this, [this]() {
0067         reloadImages();
0068     });
0069     connect(model, &ImagePacksModel::imagesLoaded, this, &StickerModel::reloadImages);
0070     m_model = model;
0071     reloadImages();
0072     Q_EMIT modelChanged();
0073 }
0074 
0075 int StickerModel::packIndex() const
0076 {
0077     return m_index;
0078 }
0079 void StickerModel::setPackIndex(int index)
0080 {
0081     m_index = index;
0082     Q_EMIT packIndexChanged();
0083     reloadImages();
0084 }
0085 
0086 void StickerModel::reloadImages()
0087 {
0088     beginResetModel();
0089     if (m_model) {
0090         m_images = m_model->images(m_index);
0091     }
0092     endResetModel();
0093 }
0094 
0095 NeoChatRoom *StickerModel::room() const
0096 {
0097     return m_room;
0098 }
0099 
0100 void StickerModel::setRoom(NeoChatRoom *room)
0101 {
0102     if (room) {
0103         disconnect(room->connection(), nullptr, this, nullptr);
0104     }
0105     m_room = room;
0106     Q_EMIT roomChanged();
0107 }
0108 
0109 void StickerModel::postSticker(int index)
0110 {
0111     const auto &image = m_images[index];
0112     const auto &body = image.body ? *image.body : image.shortcode;
0113     QJsonObject infoJson;
0114     if (image.info) {
0115         infoJson["w"_ls] = image.info->imageSize.width();
0116         infoJson["h"_ls] = image.info->imageSize.height();
0117         infoJson["mimetype"_ls] = image.info->mimeType.name();
0118         infoJson["size"_ls] = image.info->payloadSize;
0119         // TODO thumbnail
0120     }
0121     QJsonObject content{
0122         {"body"_ls, body},
0123         {"url"_ls, image.url.toString()},
0124         {"info"_ls, infoJson},
0125     };
0126     m_room->postJson("m.sticker"_ls, content);
0127 }
0128 
0129 #include "moc_stickermodel.cpp"