Rename AccountStickerModel to AccountEmoticonModel

In preparation for using it for custom emojis as well
This commit is contained in:
Tobias Fella
2023-05-20 22:43:31 +02:00
parent ee53793a6d
commit f2aa375b43
7 changed files with 62 additions and 58 deletions

View File

@@ -15,7 +15,7 @@ add_library(neochat STATIC
models/messagefiltermodel.cpp
models/roomlistmodel.cpp
models/sortfilterspacelistmodel.cpp
models/accountstickermodel.cpp
models/accountemoticonmodel.cpp
spacehierarchycache.cpp
roommanager.cpp
neochatroom.cpp

View File

@@ -48,7 +48,7 @@
#include "logger.h"
#include "login.h"
#include "matriximageprovider.h"
#include "models/accountstickermodel.h"
#include "models/accountemoticonmodel.h"
#include "models/collapsestateproxymodel.h"
#include "models/customemojimodel.h"
#include "models/devicesmodel.h"
@@ -252,7 +252,7 @@ int main(int argc, char *argv[])
qmlRegisterType<KeywordNotificationRuleModel>("org.kde.neochat", 1, 0, "KeywordNotificationRuleModel");
qmlRegisterType<StickerModel>("org.kde.neochat", 1, 0, "StickerModel");
qmlRegisterType<ImagePacksModel>("org.kde.neochat", 1, 0, "ImagePacksModel");
qmlRegisterType<AccountStickerModel>("org.kde.neochat", 1, 0, "AccountStickerModel");
qmlRegisterType<AccountEmoticonModel>("org.kde.neochat", 1, 0, "AccountEmoticonModel");
qmlRegisterType<EmoticonFilterModel>("org.kde.neochat", 1, 0, "EmoticonFilterModel");
qmlRegisterUncreatableType<RoomMessageEvent>("org.kde.neochat", 1, 0, "RoomMessageEvent", "ENUM");
qmlRegisterUncreatableType<PushNotificationState>("org.kde.neochat", 1, 0, "PushNotificationState", "ENUM");

View File

@@ -1,19 +1,19 @@
// SPDX-FileCopyrightText: 2021-2023 Tobias Fella <tobias.fella@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "accountstickermodel.h"
#include "accountemoticonmodel.h"
#include <csapi/content-repo.h>
#include <qcoro/qcorosignal.h>
using namespace Quotient;
AccountStickerModel::AccountStickerModel(QObject *parent)
AccountEmoticonModel::AccountEmoticonModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int AccountStickerModel::rowCount(const QModelIndex &index) const
int AccountEmoticonModel::rowCount(const QModelIndex &index) const
{
Q_UNUSED(index);
if (!m_images) {
@@ -22,7 +22,7 @@ int AccountStickerModel::rowCount(const QModelIndex &index) const
return m_images->images.size();
}
QVariant AccountStickerModel::data(const QModelIndex &index, int role) const
QVariant AccountEmoticonModel::data(const QModelIndex &index, int role) const
{
const auto &row = index.row();
const auto &image = m_images->images[row];
@@ -62,23 +62,23 @@ QVariant AccountStickerModel::data(const QModelIndex &index, int role) const
return {};
}
QHash<int, QByteArray> AccountStickerModel::roleNames() const
QHash<int, QByteArray> AccountEmoticonModel::roleNames() const
{
return {
{AccountStickerModel::UrlRole, "url"},
{AccountStickerModel::BodyRole, "body"},
{AccountStickerModel::ShortCodeRole, "shortcode"},
{AccountStickerModel::IsStickerRole, "isSticker"},
{AccountStickerModel::IsEmojiRole, "isEmoji"},
{AccountEmoticonModel::UrlRole, "url"},
{AccountEmoticonModel::BodyRole, "body"},
{AccountEmoticonModel::ShortCodeRole, "shortcode"},
{AccountEmoticonModel::IsStickerRole, "isSticker"},
{AccountEmoticonModel::IsEmojiRole, "isEmoji"},
};
}
Connection *AccountStickerModel::connection() const
Connection *AccountEmoticonModel::connection() const
{
return m_connection;
}
void AccountStickerModel::setConnection(Connection *connection)
void AccountEmoticonModel::setConnection(Connection *connection)
{
if (m_connection) {
disconnect(m_connection, nullptr, this, nullptr);
@@ -88,13 +88,13 @@ void AccountStickerModel::setConnection(Connection *connection)
Q_EMIT connectionChanged();
connect(m_connection, &Connection::accountDataChanged, this, [this](QString type) {
if (type == QStringLiteral("im.ponies.user_emotes")) {
reloadStickers();
reloadEmoticons();
}
});
reloadStickers();
reloadEmoticons();
}
void AccountStickerModel::reloadStickers()
void AccountEmoticonModel::reloadEmoticons()
{
if (!m_connection->hasAccountData("im.ponies.user_emotes"_ls)) {
return;
@@ -106,7 +106,7 @@ void AccountStickerModel::reloadStickers()
endResetModel();
}
void AccountStickerModel::deleteSticker(int index)
void AccountEmoticonModel::deleteEmoticon(int index)
{
QJsonObject data;
m_images->images.removeAt(index);
@@ -114,7 +114,7 @@ void AccountStickerModel::deleteSticker(int index)
m_connection->setAccountData("im.ponies.user_emotes"_ls, data);
}
void AccountStickerModel::setStickerBody(int index, const QString &text)
void AccountEmoticonModel::setEmoticonBody(int index, const QString &text)
{
m_images->images[index].body = text;
QJsonObject data;
@@ -122,7 +122,7 @@ void AccountStickerModel::setStickerBody(int index, const QString &text)
m_connection->setAccountData("im.ponies.user_emotes"_ls, data);
}
void AccountStickerModel::setStickerShortcode(int index, const QString &shortcode)
void AccountEmoticonModel::setEmoticonShortcode(int index, const QString &shortcode)
{
m_images->images[index].shortcode = shortcode;
QJsonObject data;
@@ -130,12 +130,12 @@ void AccountStickerModel::setStickerShortcode(int index, const QString &shortcod
m_connection->setAccountData("im.ponies.user_emotes"_ls, data);
}
void AccountStickerModel::setStickerImage(int index, const QUrl &source)
void AccountEmoticonModel::setEmoticonImage(int index, const QUrl &source)
{
doSetStickerImage(index, source);
doSetEmoticonImage(index, source);
}
QCoro::Task<void> AccountStickerModel::doSetStickerImage(int index, QUrl source)
QCoro::Task<void> AccountEmoticonModel::doSetEmoticonImage(int index, QUrl source)
{
auto job = m_connection->uploadFile(source.isLocalFile() ? source.toLocalFile() : source.toString());
co_await qCoro(job, &BaseJob::finished);
@@ -153,7 +153,7 @@ QCoro::Task<void> AccountStickerModel::doSetStickerImage(int index, QUrl source)
m_connection->setAccountData("im.ponies.user_emotes"_ls, data);
}
QCoro::Task<void> AccountStickerModel::doAddSticker(QUrl source, QString shortcode, QString description)
QCoro::Task<void> AccountEmoticonModel::doAddEmoticon(QUrl source, QString shortcode, QString description, QString type)
{
auto job = m_connection->uploadFile(source.isLocalFile() ? source.toLocalFile() : source.toString());
co_await qCoro(job, &BaseJob::finished);
@@ -165,14 +165,14 @@ QCoro::Task<void> AccountStickerModel::doAddSticker(QUrl source, QString shortco
job->contentUri(),
description,
none,
QStringList{"sticker"_ls},
QStringList{type},
});
QJsonObject data;
m_images->fillJson(&data);
m_connection->setAccountData("im.ponies.user_emotes"_ls, data);
}
void AccountStickerModel::addSticker(const QUrl &source, const QString &shortcode, const QString &description)
void AccountEmoticonModel::addEmoticon(const QUrl &source, const QString &shortcode, const QString &description, const QString &type)
{
doAddSticker(source, shortcode, description);
doAddEmoticon(source, shortcode, description, type);
}

View File

@@ -14,30 +14,30 @@
class ImagePacksModel;
/**
* @class AccountStickerModel
* @class AccountEmoticonModel
*
* This class defines the model for visualising the account stickers.
* This class defines the model for visualising the account stickers and emojis.
*
* This is based upon the im.ponies.user_emotes spec (MSC2545).
*/
class AccountStickerModel : public QAbstractListModel
class AccountEmoticonModel : public QAbstractListModel
{
Q_OBJECT
/**
* @brief The connection to get stickers from.
* @brief The connection to get emoticons from.
*/
Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
public:
enum Roles {
UrlRole = Qt::UserRole + 1, /**< The URL for the sticker. */
ShortCodeRole, /**< The shortcode for the sticker. */
BodyRole, //**< A textual description of the sticker */
UrlRole = Qt::UserRole + 1, /**< The URL for the emoticon. */
ShortCodeRole, /**< The shortcode for the emoticon. */
BodyRole, //**< A textual description of the emoticon */
IsStickerRole, //**< Whether this emoticon is a sticker */
IsEmojiRole, //**< Whether this emoticon is an emoji */
};
explicit AccountStickerModel(QObject *parent = nullptr);
explicit AccountEmoticonModel(QObject *parent = nullptr);
/**
* @brief Number of rows in the model.
@@ -64,29 +64,29 @@ public:
void setConnection(Quotient::Connection *connection);
/**
* @brief Deletes the sticker at the given index.
* @brief Deletes the emoticon at the given index.
*/
Q_INVOKABLE void deleteSticker(int index);
Q_INVOKABLE void deleteEmoticon(int index);
/**
* @brief Changes the description for the sticker at the given index.
* @brief Changes the description for the emoticon at the given index.
*/
Q_INVOKABLE void setStickerBody(int index, const QString &text);
Q_INVOKABLE void setEmoticonBody(int index, const QString &text);
/**
* @brief Changes the shortcode for the sticker at the given index.
* @brief Changes the shortcode for the emoticon at the given index.
*/
Q_INVOKABLE void setStickerShortcode(int index, const QString &shortCode);
Q_INVOKABLE void setEmoticonShortcode(int index, const QString &shortCode);
/**
* @brief Changes the image for the sticker at the given index.
* @brief Changes the image for the emoticon at the given index.
*/
Q_INVOKABLE void setStickerImage(int index, const QUrl &source);
Q_INVOKABLE void setEmoticonImage(int index, const QUrl &source);
/**
* @brief Adds a sticker with the given parameters.
* @brief Add an emoticon with the given parameters.
*/
Q_INVOKABLE void addSticker(const QUrl &source, const QString &shortcode, const QString &description);
Q_INVOKABLE void addEmoticon(const QUrl &source, const QString &shortcode, const QString &description, const QString &type);
Q_SIGNALS:
void connectionChanged();
@@ -94,8 +94,8 @@ Q_SIGNALS:
private:
std::optional<Quotient::ImagePackEventContent> m_images;
QPointer<Quotient::Connection> m_connection;
QCoro::Task<void> doSetStickerImage(int index, QUrl source);
QCoro::Task<void> doAddSticker(QUrl source, QString shortcode, QString description);
QCoro::Task<void> doSetEmoticonImage(int index, QUrl source);
QCoro::Task<void> doAddEmoticon(QUrl source, QString shortcode, QString description, QString type);
void reloadStickers();
void reloadEmoticons();
};

View File

@@ -3,7 +3,7 @@
#include "emoticonfiltermodel.h"
#include "accountstickermodel.h"
#include "accountemoticonmodel.h"
EmoticonFilterModel::EmoticonFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
@@ -12,8 +12,8 @@ EmoticonFilterModel::EmoticonFilterModel(QObject *parent)
bool EmoticonFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
auto stickerUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountStickerModel::IsStickerRole).toBool();
auto emojiUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountStickerModel::IsEmojiRole).toBool();
auto stickerUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountEmoticonModel::IsStickerRole).toBool();
auto emojiUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountEmoticonModel::IsEmojiRole).toBool();
return (stickerUsage && m_showStickers) || (emojiUsage && m_showEmojis);
}
@@ -24,7 +24,9 @@ bool EmoticonFilterModel::showStickers() const
void EmoticonFilterModel::setShowStickers(bool showStickers)
{
beginResetModel();
m_showStickers = showStickers;
endResetModel();
Q_EMIT showStickersChanged();
}
@@ -35,6 +37,8 @@ bool EmoticonFilterModel::showEmojis() const
void EmoticonFilterModel::setShowEmojis(bool showEmojis)
{
beginResetModel();
m_showEmojis = showEmojis;
endResetModel();
Q_EMIT showEmojisChanged();
}

View File

@@ -119,16 +119,16 @@ Kirigami.ScrollablePage {
enabled: !root.newSticker || (image.source && shortcode.text && description.text)
onClicked: {
if (root.newSticker) {
model.addSticker(image.source, shortcode.text, description.text)
model.addEmoticon(image.source, shortcode.text, description.text, "sticker")
} else {
if (description.text !== root.description) {
root.model.setStickerBody(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, description.text)
root.model.setEmoticonBody(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, description.text)
}
if (shortcode.text !== root.shortcode) {
root.model.setStickerShortcode(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, shortcode.text)
root.model.setEmoticonShortcode(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, shortcode.text)
}
if (image.source + "" !== root.url) {
root.model.setStickerImage(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, image.source)
root.model.setEmoticonImage(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, image.source)
}
}
root.closeDialog()

View File

@@ -30,7 +30,7 @@ Kirigami.ScrollablePage {
Repeater {
model: EmoticonFilterModel {
id: emoticonFilterModel
sourceModel: AccountStickerModel {
sourceModel: AccountEmoticonModel {
id: stickerModel
connection: Controller.activeConnection
}
@@ -84,7 +84,7 @@ Kirigami.ScrollablePage {
anchors.right: parent.right
anchors.margins: Kirigami.Units.smallSpacing
z: 2
onClicked: stickerModel.deleteSticker(emoticonFilterModel.mapToSource(emoticonFilterModel.index(model.index, 0)).row)
onClicked: stickerModel.deleteEmoticon(emoticonFilterModel.mapToSource(emoticonFilterModel.index(model.index, 0)).row)
}
}
}