File indexing completed on 2024-04-21 07:43:23

0001 /*
0002     SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "author.h"
0008 
0009 #include "quickengine.h"
0010 
0011 #include "core/author.h"
0012 #include "core/provider.h"
0013 
0014 #include "knewstuffquick_debug.h"
0015 
0016 #include <memory>
0017 
0018 namespace KNewStuffQuick
0019 {
0020 // This caching will want to eventually go into the Provider level (and be more generalised)
0021 typedef QHash<QString, std::shared_ptr<KNSCore::Author>> AllAuthorsHash;
0022 Q_GLOBAL_STATIC(AllAuthorsHash, allAuthors)
0023 
0024 class AuthorPrivate
0025 {
0026 public:
0027     AuthorPrivate(Author *qq)
0028         : q(qq)
0029     {
0030     }
0031     Author *const q;
0032     bool componentCompleted{false};
0033     Engine *engine{nullptr};
0034     QString providerId;
0035     QString username;
0036 
0037     QSharedPointer<KNSCore::Provider> provider;
0038     void resetConnections()
0039     {
0040         if (!componentCompleted) {
0041             return;
0042         }
0043         if (provider) {
0044             provider->disconnect(q);
0045         }
0046         if (engine) {
0047             provider = engine->provider(providerId);
0048             if (!provider) {
0049                 provider = engine->defaultProvider();
0050             }
0051         }
0052         if (provider) {
0053             QObject::connect(provider.get(), &KNSCore::Provider::personLoaded, q, [this](const std::shared_ptr<KNSCore::Author> author) {
0054                 allAuthors()->insert(QStringLiteral("%1 %2").arg(provider->id(), author->id()), author);
0055                 Q_EMIT q->dataChanged();
0056             });
0057             author(); // Check and make sure...
0058         }
0059     }
0060 
0061     // TODO Having a shared ptr on a QSharedData class doesn't make sense
0062     std::shared_ptr<KNSCore::Author> author()
0063     {
0064         std::shared_ptr<KNSCore::Author> ret;
0065         if (provider && !username.isEmpty()) {
0066             ret = allAuthors()->value(QStringLiteral("%1 %2").arg(provider->id(), username));
0067             if (!ret.get()) {
0068                 Q_EMIT provider->loadPerson(username);
0069             }
0070         }
0071         return ret;
0072     }
0073 };
0074 }
0075 
0076 using namespace KNewStuffQuick;
0077 
0078 Author::Author(QObject *parent)
0079     : QObject(parent)
0080     , d(new AuthorPrivate(this))
0081 {
0082     connect(this, &Author::engineChanged, &Author::dataChanged);
0083     connect(this, &Author::providerIdChanged, &Author::dataChanged);
0084     connect(this, &Author::usernameChanged, &Author::dataChanged);
0085 }
0086 
0087 Author::~Author() = default;
0088 
0089 void Author::classBegin()
0090 {
0091 }
0092 
0093 void Author::componentComplete()
0094 {
0095     d->componentCompleted = true;
0096     d->resetConnections();
0097 }
0098 
0099 QObject *Author::engine() const
0100 {
0101     return d->engine;
0102 }
0103 
0104 void Author::setEngine(QObject *newEngine)
0105 {
0106     if (d->engine != newEngine) {
0107         d->engine = qobject_cast<Engine *>(newEngine);
0108         d->resetConnections();
0109         Q_EMIT engineChanged();
0110     }
0111 }
0112 
0113 QString Author::providerId() const
0114 {
0115     return d->providerId;
0116 }
0117 
0118 void Author::setProviderId(const QString &providerId)
0119 {
0120     if (d->providerId != providerId) {
0121         d->providerId = providerId;
0122         d->resetConnections();
0123         Q_EMIT providerIdChanged();
0124     }
0125 }
0126 
0127 QString Author::username() const
0128 {
0129     return d->username;
0130 }
0131 
0132 void Author::setUsername(const QString &username)
0133 {
0134     if (d->username != username) {
0135         d->username = username;
0136         d->resetConnections();
0137         Q_EMIT usernameChanged();
0138     }
0139 }
0140 
0141 QString Author::name() const
0142 {
0143     std::shared_ptr<KNSCore::Author> author = d->author();
0144     if (author.get() && !author->name().isEmpty()) {
0145         return author->name();
0146     }
0147     return d->username;
0148 }
0149 
0150 QString Author::description() const
0151 {
0152     std::shared_ptr<KNSCore::Author> author = d->author();
0153     if (author.get()) {
0154         return author->description();
0155     }
0156     return QString{};
0157 }
0158 
0159 QString Author::homepage() const
0160 {
0161     std::shared_ptr<KNSCore::Author> author = d->author();
0162     if (author.get()) {
0163         return author->homepage();
0164     }
0165     return QString{};
0166 }
0167 
0168 QString Author::profilepage() const
0169 {
0170     std::shared_ptr<KNSCore::Author> author = d->author();
0171     if (author.get()) {
0172         return author->profilepage();
0173     }
0174     return QString{};
0175 }
0176 
0177 QUrl Author::avatarUrl() const
0178 {
0179     std::shared_ptr<KNSCore::Author> author = d->author();
0180     if (author.get()) {
0181         return author->avatarUrl();
0182     }
0183     return QUrl{};
0184 }
0185 
0186 #include "moc_author.cpp"