1/*
2 SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "author.h"
8
9#include "quickengine.h"
10
11#include "core/author.h"
12#include "core/provider.h"
13
14#include "knewstuffquick_debug.h"
15
16#include <memory>
17
18namespace KNewStuffQuick
19{
20// This caching will want to eventually go into the Provider level (and be more generalised)
21typedef QHash<QString, std::shared_ptr<KNSCore::Author>> AllAuthorsHash;
22Q_GLOBAL_STATIC(AllAuthorsHash, allAuthors)
23
24class AuthorPrivate
25{
26public:
27 AuthorPrivate(Author *qq)
28 : q(qq)
29 {
30 }
31 Author *const q;
32 bool componentCompleted{false};
33 Engine *engine{nullptr};
34 QString providerId;
35 QString username;
36
37 QSharedPointer<KNSCore::Provider> provider;
38 void resetConnections()
39 {
40 if (!componentCompleted) {
41 return;
42 }
43 if (provider) {
44 provider->disconnect(receiver: q);
45 }
46 if (engine) {
47 provider = engine->provider(providerId);
48 if (!provider) {
49 provider = engine->defaultProvider();
50 }
51 }
52 if (provider) {
53 QObject::connect(sender: provider.get(), signal: &KNSCore::Provider::personLoaded, context: q, slot: [this](const std::shared_ptr<KNSCore::Author> author) {
54 allAuthors()->insert(QStringLiteral("%1 %2").arg(args: provider->id(), args: author->id()), value: author);
55 Q_EMIT q->dataChanged();
56 });
57 author(); // Check and make sure...
58 }
59 }
60
61 // TODO Having a shared ptr on a QSharedData class doesn't make sense
62 std::shared_ptr<KNSCore::Author> author()
63 {
64 std::shared_ptr<KNSCore::Author> ret;
65 if (provider && !username.isEmpty()) {
66 ret = allAuthors()->value(QStringLiteral("%1 %2").arg(args: provider->id(), args&: username));
67 if (!ret.get()) {
68 Q_EMIT provider->loadPerson(username);
69 }
70 }
71 return ret;
72 }
73};
74}
75
76using namespace KNewStuffQuick;
77
78Author::Author(QObject *parent)
79 : QObject(parent)
80 , d(new AuthorPrivate(this))
81{
82 connect(sender: this, signal: &Author::engineChanged, slot: &Author::dataChanged);
83 connect(sender: this, signal: &Author::providerIdChanged, slot: &Author::dataChanged);
84 connect(sender: this, signal: &Author::usernameChanged, slot: &Author::dataChanged);
85}
86
87Author::~Author() = default;
88
89void Author::classBegin()
90{
91}
92
93void Author::componentComplete()
94{
95 d->componentCompleted = true;
96 d->resetConnections();
97}
98
99QObject *Author::engine() const
100{
101 return d->engine;
102}
103
104void Author::setEngine(QObject *newEngine)
105{
106 if (d->engine != newEngine) {
107 d->engine = qobject_cast<Engine *>(object: newEngine);
108 d->resetConnections();
109 Q_EMIT engineChanged();
110 }
111}
112
113QString Author::providerId() const
114{
115 return d->providerId;
116}
117
118void Author::setProviderId(const QString &providerId)
119{
120 if (d->providerId != providerId) {
121 d->providerId = providerId;
122 d->resetConnections();
123 Q_EMIT providerIdChanged();
124 }
125}
126
127QString Author::username() const
128{
129 return d->username;
130}
131
132void Author::setUsername(const QString &username)
133{
134 if (d->username != username) {
135 d->username = username;
136 d->resetConnections();
137 Q_EMIT usernameChanged();
138 }
139}
140
141QString Author::name() const
142{
143 std::shared_ptr<KNSCore::Author> author = d->author();
144 if (author.get() && !author->name().isEmpty()) {
145 return author->name();
146 }
147 return d->username;
148}
149
150QString Author::description() const
151{
152 std::shared_ptr<KNSCore::Author> author = d->author();
153 if (author.get()) {
154 return author->description();
155 }
156 return QString{};
157}
158
159QString Author::homepage() const
160{
161 std::shared_ptr<KNSCore::Author> author = d->author();
162 if (author.get()) {
163 return author->homepage();
164 }
165 return QString{};
166}
167
168QString Author::profilepage() const
169{
170 std::shared_ptr<KNSCore::Author> author = d->author();
171 if (author.get()) {
172 return author->profilepage();
173 }
174 return QString{};
175}
176
177QUrl Author::avatarUrl() const
178{
179 std::shared_ptr<KNSCore::Author> author = d->author();
180 if (author.get()) {
181 return author->avatarUrl();
182 }
183 return QUrl{};
184}
185
186#include "moc_author.cpp"
187

source code of knewstuff/src/qtquick/author.cpp