File indexing completed on 2024-04-21 15:02:24

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