diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 7450675..75dd9c0 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -14,6 +14,7 @@ target.path = $$[QT_INSTALL_LIBS] HEADERS = \ semaphore_p.h \ socialsyncinterface.h \ + socialposthelpers.h \ abstractimagedownloader.h \ abstractimagedownloader_p.h \ abstractsocialcachedatabase.h \ @@ -36,6 +37,7 @@ HEADERS = \ SOURCES = \ semaphore_p.cpp \ socialsyncinterface.cpp \ + socialposthelpers.cpp \ abstractimagedownloader.cpp \ abstractsocialcachedatabase.cpp \ abstractsocialpostcachedatabase.cpp \ @@ -65,4 +67,3 @@ QMAKE_PKGCONFIG_DESTDIR = pkgconfig QMAKE_PKGCONFIG_VERSION = $$VERSION INSTALLS += target headers - diff --git a/src/lib/socialposthelpers.cpp b/src/lib/socialposthelpers.cpp new file mode 100644 index 0000000..bfcf56f --- /dev/null +++ b/src/lib/socialposthelpers.cpp @@ -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 &images) +{ + QVariantList list; + Q_FOREACH (const SocialPostImage::ConstPtr &image, images) { + list.append(imageData(image)); + } + return list; +} + +QVariantList accountIdList(const QList &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 *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())); + } +} + +} diff --git a/src/lib/socialposthelpers.h b/src/lib/socialposthelpers.h new file mode 100644 index 0000000..72e134f --- /dev/null +++ b/src/lib/socialposthelpers.h @@ -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 +#include +#include +#include +#include + +namespace SocialPostHelpers { + +QVariantMap imageData(const SocialPostImage::ConstPtr &image); +QVariantList imageDataList(const QList &images); +QVariantList accountIdList(const QList &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 *rowData, + const SocialPost::ConstPtr &post, + int identifierRole, + int nameRole, + int bodyRole, + int timestampRole, + int iconRole, + int imagesRole, + int accountsRole); + +} + +#endif // SOCIALPOSTHELPERS_H diff --git a/src/qml/facebook/facebookpostsmodel.cpp b/src/qml/facebook/facebookpostsmodel.cpp index a7dda44..f739bc7 100644 --- a/src/qml/facebook/facebookpostsmodel.cpp +++ b/src/qml/facebook/facebookpostsmodel.cpp @@ -20,8 +20,8 @@ #include "facebookpostsmodel.h" #include "abstractsocialcachemodel_p.h" #include "facebookpostsdatabase.h" +#include "socialposthelpers.h" #include -#include "postimagehelper_p.h" class FacebookPostsModelPrivate: public AbstractSocialCacheModelPrivate { @@ -84,17 +84,14 @@ void FacebookPostsModel::postsChanged() Q_FOREACH (const SocialPost::ConstPtr &post, postsData) { QMap 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)); @@ -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); } diff --git a/src/qml/postimagehelper_p.h b/src/qml/postimagehelper_p.h deleted file mode 100644 index fe61212..0000000 --- a/src/qml/postimagehelper_p.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2013 Jolla Ltd. - * Contact: Lucien Xu - * - * 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 POSTIMAGEHELPER_P_H -#define POSTIMAGEHELPER_P_H - -#include - -static const char *URL_KEY = "url"; -static const char *TYPE_KEY = "type"; -static const char *TYPE_PHOTO = "photo"; -static const char *TYPE_VIDEO = "video"; - -inline static QVariantMap createImageData(const SocialPostImage::ConstPtr &image) -{ - QVariantMap imageData; - imageData.insert(QLatin1String(URL_KEY), image->url()); - switch (image->type()) { - case SocialPostImage::Video: - imageData.insert(QLatin1String(TYPE_KEY), QLatin1String(TYPE_VIDEO)); - break; - default: - imageData.insert(QLatin1String(TYPE_KEY), QLatin1String(TYPE_PHOTO)); - break; - } - return imageData; -} - -#endif // POSTIMAGEHELPER_P_H diff --git a/src/qml/qml.pro b/src/qml/qml.pro index 1609326..23289a1 100644 --- a/src/qml/qml.pro +++ b/src/qml/qml.pro @@ -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 \ diff --git a/src/qml/twitter/twitterpostsmodel.cpp b/src/qml/twitter/twitterpostsmodel.cpp index a59a00f..c4d8f64 100644 --- a/src/qml/twitter/twitterpostsmodel.cpp +++ b/src/qml/twitter/twitterpostsmodel.cpp @@ -20,8 +20,8 @@ #include "twitterpostsmodel.h" #include "abstractsocialcachemodel_p.h" #include "twitterpostsdatabase.h" +#include "socialposthelpers.h" #include -#include "postimagehelper_p.h" class TwitterPostsModelPrivate: public AbstractSocialCacheModelPrivate { @@ -96,28 +96,19 @@ void TwitterPostsModel::postsChanged() QList postsData = d->database.posts(); Q_FOREACH (const SocialPost::ConstPtr &post, postsData) { QMap eventMap; - eventMap.insert(TwitterPostsModel::TwitterId, post->identifier()); - eventMap.insert(TwitterPostsModel::Name, post->name()); - eventMap.insert(TwitterPostsModel::Body, post->body()); - eventMap.insert(TwitterPostsModel::Timestamp, post->timestamp()); - eventMap.insert(TwitterPostsModel::Icon, post->icon()); - - QVariantList images; - Q_FOREACH (const SocialPostImage::ConstPtr &image, post->images()) { - images.append(createImageData(image)); - } - eventMap.insert(TwitterPostsModel::Images, images); + SocialPostHelpers::appendCommonPostFields(&eventMap, post, + TwitterPostsModel::TwitterId, + TwitterPostsModel::Name, + TwitterPostsModel::Body, + TwitterPostsModel::Timestamp, + TwitterPostsModel::Icon, + TwitterPostsModel::Images, + TwitterPostsModel::Accounts); eventMap.insert(TwitterPostsModel::ScreenName, d->database.screenName(post)); eventMap.insert(TwitterPostsModel::Retweeter, d->database.retweeter(post)); eventMap.insert(TwitterPostsModel::ConsumerKey, d->database.consumerKey(post)); eventMap.insert(TwitterPostsModel::ConsumerSecret, d->database.consumerSecret(post)); - - QVariantList accountsVariant; - Q_FOREACH (int account, post->accounts()) { - accountsVariant.append(account); - } - eventMap.insert(TwitterPostsModel::Accounts, accountsVariant); data.append(eventMap); } diff --git a/src/qml/vk/vkpostsmodel.cpp b/src/qml/vk/vkpostsmodel.cpp index 2c0b673..d54445e 100644 --- a/src/qml/vk/vkpostsmodel.cpp +++ b/src/qml/vk/vkpostsmodel.cpp @@ -20,8 +20,8 @@ #include "vkpostsmodel.h" #include "abstractsocialcachemodel_p.h" #include "vkpostsdatabase.h" +#include "socialposthelpers.h" #include -#include "postimagehelper_p.h" static const char *POST_LINK_KEY = "post_link_key"; @@ -129,11 +129,14 @@ void VKPostsModel::postsChanged() Q_FOREACH (const SocialPost::ConstPtr &post, postsData) { QMap eventMap; - eventMap.insert(VKPostsModel::VkId, post->identifier()); - eventMap.insert(VKPostsModel::Name, post->name()); - eventMap.insert(VKPostsModel::Body, post->body()); - eventMap.insert(VKPostsModel::Timestamp, post->timestamp()); - eventMap.insert(VKPostsModel::Icon, post->icon()); + SocialPostHelpers::appendCommonPostFields(&eventMap, post, + VKPostsModel::VkId, + VKPostsModel::Name, + VKPostsModel::Body, + VKPostsModel::Timestamp, + VKPostsModel::Icon, + VKPostsModel::Images, + VKPostsModel::Accounts); eventMap.insert(VKPostsModel::Link, post->extra().value(POST_LINK_KEY)); eventMap.insert(VKPostsModel::RepostOwnerName, post->extra().value(COPIED_POST_OWNER_NAME_KEY)); @@ -149,23 +152,11 @@ void VKPostsModel::postsChanged() Q_FOREACH (const QString url, imageUrls) { if (!url.isEmpty()) { SocialPostImage::Ptr repostImage = SocialPostImage::create(url, SocialPostImage::Photo); - QVariantMap tmp = createImageData(repostImage); + QVariantMap tmp = SocialPostHelpers::imageData(repostImage); repostImages.append(tmp); } } eventMap.insert(VKPostsModel::RepostImages, repostImages); - - QVariantList images; - Q_FOREACH (const SocialPostImage::ConstPtr &image, post->images()) { - images.append(createImageData(image)); - } - eventMap.insert(VKPostsModel::Images, images); - - QVariantList accountsVariant; - Q_FOREACH (int account, post->accounts()) { - accountsVariant.append(account); - } - eventMap.insert(VKPostsModel::Accounts, accountsVariant); data.append(eventMap); }