Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target.path = $$[QT_INSTALL_LIBS]
HEADERS = \
semaphore_p.h \
socialsyncinterface.h \
socialposthelpers.h \
abstractimagedownloader.h \
abstractimagedownloader_p.h \
abstractsocialcachedatabase.h \
Expand All @@ -36,6 +37,7 @@ HEADERS = \
SOURCES = \
semaphore_p.cpp \
socialsyncinterface.cpp \
socialposthelpers.cpp \
abstractimagedownloader.cpp \
abstractsocialcachedatabase.cpp \
abstractsocialpostcachedatabase.cpp \
Expand Down Expand Up @@ -65,4 +67,3 @@ QMAKE_PKGCONFIG_DESTDIR = pkgconfig
QMAKE_PKGCONFIG_VERSION = $$VERSION

INSTALLS += target headers

184 changes: 184 additions & 0 deletions src/lib/socialposthelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (C) 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 "socialposthelpers.h"

namespace SocialPostHelpers {

QVariantMap imageData(const SocialPostImage::ConstPtr &image)
{
QVariantMap data;
if (image.isNull()) {
return data;
}

data.insert(QStringLiteral("url"), image->url());
const QString typeValue = image->type() == SocialPostImage::Video
? QStringLiteral("video")
: QStringLiteral("photo");
data.insert(QStringLiteral("type"), typeValue);

return data;
}

QVariantList imageDataList(const QList<SocialPostImage::ConstPtr> &images)
{
QVariantList list;
Q_FOREACH (const SocialPostImage::ConstPtr &image, images) {
list.append(imageData(image));
}
return list;
}

QVariantList accountIdList(const QList<int> &accounts)
{
QVariantList list;
Q_FOREACH (int account, accounts) {
list.append(account);
}
return list;
}

QString extraString(const SocialPost::ConstPtr &post,
const QString &key,
const QString &defaultValue)
{
if (post.isNull()) {
return defaultValue;
}

const QVariant value = post->extra().value(key);
if (!value.isValid() || value.isNull()) {
return defaultValue;
}

return value.toString();
}

int extraInt(const SocialPost::ConstPtr &post,
const QString &key,
int defaultValue)
{
if (post.isNull()) {
return defaultValue;
}

bool ok = false;
const int parsed = post->extra().value(key).toInt(&ok);
return ok ? parsed : defaultValue;
}

bool extraBool(const SocialPost::ConstPtr &post,
const QString &key,
bool defaultValue)
{
if (post.isNull()) {
return defaultValue;
}

const QVariant value = post->extra().value(key);
if (!value.isValid() || value.isNull()) {
return defaultValue;
}

return value.toBool();
}

QString extraString(const SocialPost::ConstPtr &post,
const QStringList &keys,
const QString &defaultValue)
{
Q_FOREACH (const QString &key, keys) {
const QString value = extraString(post, key);
if (!value.isEmpty()) {
return value;
}
}
return defaultValue;
}

int extraInt(const SocialPost::ConstPtr &post,
const QStringList &keys,
int defaultValue)
{
Q_FOREACH (const QString &key, keys) {
const QVariant value = post.isNull() ? QVariant() : post->extra().value(key);
if (!value.isValid() || value.isNull()) {
continue;
}
bool ok = false;
const int parsed = value.toInt(&ok);
if (ok) {
return parsed;
}
}
return defaultValue;
}

bool extraBool(const SocialPost::ConstPtr &post,
const QStringList &keys,
bool defaultValue)
{
Q_FOREACH (const QString &key, keys) {
const QVariant value = post.isNull() ? QVariant() : post->extra().value(key);
if (!value.isValid() || value.isNull()) {
continue;
}
return value.toBool();
}
return defaultValue;
}

void appendCommonPostFields(QMap<int, QVariant> *rowData,
const SocialPost::ConstPtr &post,
int identifierRole,
int nameRole,
int bodyRole,
int timestampRole,
int iconRole,
int imagesRole,
int accountsRole)
{
if (!rowData || post.isNull()) {
return;
}

if (identifierRole >= 0) {
rowData->insert(identifierRole, post->identifier());
}
if (nameRole >= 0) {
rowData->insert(nameRole, post->name());
}
if (bodyRole >= 0) {
rowData->insert(bodyRole, post->body());
}
if (timestampRole >= 0) {
rowData->insert(timestampRole, post->timestamp());
}
if (iconRole >= 0) {
rowData->insert(iconRole, post->icon());
}
if (imagesRole >= 0) {
rowData->insert(imagesRole, imageDataList(post->images()));
}
if (accountsRole >= 0) {
rowData->insert(accountsRole, accountIdList(post->accounts()));
}
}

}
68 changes: 68 additions & 0 deletions src/lib/socialposthelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 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
*/

#ifndef SOCIALPOSTHELPERS_H
#define SOCIALPOSTHELPERS_H

#include "abstractsocialpostcachedatabase.h"

#include <QtCore/QMap>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtCore/QVariantList>
#include <QtCore/QVariantMap>

namespace SocialPostHelpers {

QVariantMap imageData(const SocialPostImage::ConstPtr &image);
QVariantList imageDataList(const QList<SocialPostImage::ConstPtr> &images);
QVariantList accountIdList(const QList<int> &accounts);

QString extraString(const SocialPost::ConstPtr &post,
const QString &key,
const QString &defaultValue = QString());
int extraInt(const SocialPost::ConstPtr &post,
const QString &key,
int defaultValue = 0);
bool extraBool(const SocialPost::ConstPtr &post,
const QString &key,
bool defaultValue = false);

QString extraString(const SocialPost::ConstPtr &post,
const QStringList &keys,
const QString &defaultValue = QString());
int extraInt(const SocialPost::ConstPtr &post,
const QStringList &keys,
int defaultValue = 0);
bool extraBool(const SocialPost::ConstPtr &post,
const QStringList &keys,
bool defaultValue = false);

void appendCommonPostFields(QMap<int, QVariant> *rowData,
const SocialPost::ConstPtr &post,
int identifierRole,
int nameRole,
int bodyRole,
int timestampRole,
int iconRole,
int imagesRole,
int accountsRole);

}

#endif // SOCIALPOSTHELPERS_H
27 changes: 9 additions & 18 deletions src/qml/facebook/facebookpostsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include "facebookpostsmodel.h"
#include "abstractsocialcachemodel_p.h"
#include "facebookpostsdatabase.h"
#include "socialposthelpers.h"
#include <QtCore/QDebug>
#include "postimagehelper_p.h"

class FacebookPostsModelPrivate: public AbstractSocialCacheModelPrivate
{
Expand Down Expand Up @@ -84,17 +84,14 @@ void FacebookPostsModel::postsChanged()

Q_FOREACH (const SocialPost::ConstPtr &post, postsData) {
QMap<int, QVariant> eventMap;
eventMap.insert(FacebookPostsModel::FacebookId, post->identifier());
eventMap.insert(FacebookPostsModel::Name, post->name());
eventMap.insert(FacebookPostsModel::Body, post->body());
eventMap.insert(FacebookPostsModel::Timestamp, post->timestamp());
eventMap.insert(FacebookPostsModel::Icon, post->icon());

QVariantList images;
Q_FOREACH (const SocialPostImage::ConstPtr &image, post->images()) {
images.append(createImageData(image));
}
eventMap.insert(FacebookPostsModel::Images, images);
SocialPostHelpers::appendCommonPostFields(&eventMap, post,
FacebookPostsModel::FacebookId,
FacebookPostsModel::Name,
FacebookPostsModel::Body,
FacebookPostsModel::Timestamp,
FacebookPostsModel::Icon,
FacebookPostsModel::Images,
FacebookPostsModel::Accounts);

eventMap.insert(FacebookPostsModel::AttachmentName, d->database.attachmentName(post));
eventMap.insert(FacebookPostsModel::AttachmentCaption, d->database.attachmentCaption(post));
Expand All @@ -104,12 +101,6 @@ void FacebookPostsModel::postsChanged()
eventMap.insert(FacebookPostsModel::AllowLike, d->database.allowLike(post));
eventMap.insert(FacebookPostsModel::AllowComment, d->database.allowComment(post));
eventMap.insert(FacebookPostsModel::ClientId, d->database.clientId(post));

QVariantList accountsVariant;
Q_FOREACH (int account, post->accounts()) {
accountsVariant.append(account);
}
eventMap.insert(FacebookPostsModel::Accounts, accountsVariant);
data.append(eventMap);
}

Expand Down
45 changes: 0 additions & 45 deletions src/qml/postimagehelper_p.h

This file was deleted.

1 change: 0 additions & 1 deletion src/qml/qml.pro
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ SOURCES += synchelper.cpp \
HEADERS += \
abstractsocialcachemodel.h \
abstractsocialcachemodel_p.h \
postimagehelper_p.h \
synchronizelists_p.h \
facebook/facebookimagecachemodel.h \
facebook/facebookimagedownloader.h \
Expand Down
Loading