diff options
Diffstat (limited to 'eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp')
| -rw-r--r-- | eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp b/eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp new file mode 100644 index 0000000..48b3446 --- /dev/null +++ b/eventsview-plugins/eventsview-plugin-fediverse/fediversepostsmodel.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (C) 2013-2026 Jolla Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "fediversepostsmodel.h" + +#include <QtCore/QVariantList> + +namespace { + +QVariantList imageListForPost(const SocialPost::ConstPtr &post) +{ + QVariantList images; + if (!post) { + return images; + } + + const QList<SocialPostImage::ConstPtr> postImages = post->images(); + for (const SocialPostImage::ConstPtr &image : postImages) { + if (!image) { + continue; + } + + QVariantMap imageMap; + imageMap.insert(QStringLiteral("url"), image->url()); + imageMap.insert(QStringLiteral("type"), image->type() == SocialPostImage::Video + ? QStringLiteral("video") + : QStringLiteral("image")); + images.append(imageMap); + } + + return images; +} + +QVariantList accountListForPost(const SocialPost::ConstPtr &post) +{ + QVariantList accounts; + if (!post) { + return accounts; + } + + const QList<int> postAccounts = post->accounts(); + for (int accountId : postAccounts) { + accounts.append(accountId); + } + return accounts; +} + +void appendCommonPostFields(QMap<int, QVariant> *eventMap, + const SocialPost::ConstPtr &post, + int idRole, + int nameRole, + int bodyRole, + int timestampRole, + int iconRole, + int imagesRole, + int accountsRole) +{ + if (!eventMap || !post) { + return; + } + + eventMap->insert(idRole, post->identifier()); + eventMap->insert(nameRole, post->name()); + eventMap->insert(bodyRole, post->body()); + eventMap->insert(timestampRole, post->timestamp()); + eventMap->insert(iconRole, post->icon()); + eventMap->insert(imagesRole, imageListForPost(post)); + eventMap->insert(accountsRole, accountListForPost(post)); +} + +} + +FediversePostsModel::FediversePostsModel(QObject *parent) + : QAbstractListModel(parent) +{ + connect(&m_database, &AbstractSocialPostCacheDatabase::postsChanged, + this, &FediversePostsModel::postsChanged); + connect(&m_database, SIGNAL(accountIdFilterChanged()), + this, SIGNAL(accountIdFilterChanged())); +} + +int FediversePostsModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_data.count(); +} + +QVariant FediversePostsModel::data(const QModelIndex &index, int role) const +{ + const int row = index.row(); + if (!index.isValid() || row < 0 || row >= m_data.count()) { + return QVariant(); + } + + return m_data.at(row).value(role); +} + +QHash<int, QByteArray> FediversePostsModel::roleNames() const +{ + QHash<int, QByteArray> roleNames; + roleNames.insert(FediverseId, "fediverseId"); + roleNames.insert(Name, "name"); + roleNames.insert(AccountName, "accountName"); + roleNames.insert(Acct, "acct"); + roleNames.insert(Body, "body"); + roleNames.insert(Timestamp, "timestamp"); + roleNames.insert(Icon, "icon"); + roleNames.insert(Images, "images"); + roleNames.insert(Url, "url"); + roleNames.insert(Link, "link"); + roleNames.insert(BoostedBy, "boostedBy"); + roleNames.insert(RebloggedBy, "rebloggedBy"); + roleNames.insert(RepliesCount, "repliesCount"); + roleNames.insert(FavouritesCount, "favouritesCount"); + roleNames.insert(ReblogsCount, "reblogsCount"); + roleNames.insert(Favourited, "favourited"); + roleNames.insert(Reblogged, "reblogged"); + roleNames.insert(InstanceUrl, "instanceUrl"); + roleNames.insert(InstanceIconPath, "instanceIconPath"); + roleNames.insert(Accounts, "accounts"); + return roleNames; +} + +QVariantList FediversePostsModel::accountIdFilter() const +{ + return m_database.accountIdFilter(); +} + +void FediversePostsModel::setAccountIdFilter(const QVariantList &accountIds) +{ + m_database.setAccountIdFilter(accountIds); +} + +void FediversePostsModel::refresh() +{ + m_database.refresh(); +} + +void FediversePostsModel::postsChanged() +{ + QList<RowData> data; + QList<SocialPost::ConstPtr> postsData = m_database.posts(); + Q_FOREACH (const SocialPost::ConstPtr &post, postsData) { + RowData eventMap; + const QString accountName = m_database.accountName(post); + const QString postUrl = m_database.url(post); + const QString boostedBy = m_database.boostedBy(post); + const int repliesCount = m_database.repliesCount(post); + const int favouritesCount = m_database.favouritesCount(post); + const int reblogsCount = m_database.reblogsCount(post); + const bool favourited = m_database.favourited(post); + const bool reblogged = m_database.reblogged(post); + + appendCommonPostFields(&eventMap, post, + FediversePostsModel::FediverseId, + FediversePostsModel::Name, + FediversePostsModel::Body, + FediversePostsModel::Timestamp, + FediversePostsModel::Icon, + FediversePostsModel::Images, + FediversePostsModel::Accounts); + eventMap.insert(FediversePostsModel::AccountName, accountName); + eventMap.insert(FediversePostsModel::Acct, accountName); + eventMap.insert(FediversePostsModel::Url, postUrl); + eventMap.insert(FediversePostsModel::Link, postUrl); + eventMap.insert(FediversePostsModel::BoostedBy, boostedBy); + eventMap.insert(FediversePostsModel::RebloggedBy, boostedBy); + eventMap.insert(FediversePostsModel::RepliesCount, repliesCount); + eventMap.insert(FediversePostsModel::FavouritesCount, favouritesCount); + eventMap.insert(FediversePostsModel::ReblogsCount, reblogsCount); + eventMap.insert(FediversePostsModel::Favourited, favourited); + eventMap.insert(FediversePostsModel::Reblogged, reblogged); + eventMap.insert(FediversePostsModel::InstanceUrl, m_database.instanceUrl(post)); + eventMap.insert(FediversePostsModel::InstanceIconPath, m_database.instanceIconPath(post)); + data.append(eventMap); + } + + const int oldCount = m_data.count(); + beginResetModel(); + m_data = data; + endResetModel(); + if (oldCount != m_data.count()) { + emit countChanged(); + } +} |
