KNewStuff

qtquick/author.cpp
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 <memory>
15
16namespace KNewStuffQuick
17{
18// This caching will want to eventually go into the Provider level (and be more generalised)
19typedef QHash<QString, std::shared_ptr<KNSCore::Author>> AllAuthorsHash;
20Q_GLOBAL_STATIC(AllAuthorsHash, allAuthors)
21
22class AuthorPrivate
23{
24public:
25 AuthorPrivate(Author *qq)
26 : q(qq)
27 {
28 }
29 Author *const q;
30 bool componentCompleted{false};
31 Engine *engine{nullptr};
32 QString providerId;
33 QString username;
34
35 QSharedPointer<KNSCore::Provider> provider;
36 void resetConnections()
37 {
38 if (!componentCompleted) {
39 return;
40 }
41 if (provider) {
42 provider->disconnect(q);
43 }
44 if (engine) {
45 provider = engine->provider(providerId);
46 if (!provider) {
47 provider = engine->defaultProvider();
48 }
49 }
50 if (provider) {
51 QObject::connect(provider.get(), &KNSCore::Provider::personLoaded, q, [this](const std::shared_ptr<KNSCore::Author> author) {
52 allAuthors()->insert(QStringLiteral("%1 %2").arg(provider->id(), author->id()), author);
53 Q_EMIT q->dataChanged();
54 });
55 author(); // Check and make sure...
56 }
57 }
58
59 // TODO Having a shared ptr on a QSharedData class doesn't make sense
60 std::shared_ptr<KNSCore::Author> author()
61 {
62 std::shared_ptr<KNSCore::Author> ret;
63 if (provider && !username.isEmpty()) {
64 ret = allAuthors()->value(QStringLiteral("%1 %2").arg(provider->id(), username));
65 if (!ret.get()) {
66 provider->loadPerson(username);
67 }
68 }
69 return ret;
70 }
71};
72}
73
74using namespace KNewStuffQuick;
75
76Author::Author(QObject *parent)
77 : QObject(parent)
78 , d(new AuthorPrivate(this))
79{
80 connect(this, &Author::engineChanged, &Author::dataChanged);
81 connect(this, &Author::providerIdChanged, &Author::dataChanged);
82 connect(this, &Author::usernameChanged, &Author::dataChanged);
83}
84
85Author::~Author() = default;
86
87void Author::classBegin()
88{
89}
90
91void Author::componentComplete()
92{
93 d->componentCompleted = true;
94 d->resetConnections();
95}
96
97Engine *Author::engine() const
98{
99 return d->engine;
100}
101
102void Author::setEngine(Engine *newEngine)
103{
104 if (d->engine != newEngine) {
105 d->engine = newEngine;
106 d->resetConnections();
107 Q_EMIT engineChanged();
108 }
109}
110
111QString Author::providerId() const
112{
113 return d->providerId;
114}
115
116void Author::setProviderId(const QString &providerId)
117{
118 if (d->providerId != providerId) {
119 d->providerId = providerId;
120 d->resetConnections();
121 Q_EMIT providerIdChanged();
122 }
123}
124
125QString Author::username() const
126{
127 return d->username;
128}
129
130void Author::setUsername(const QString &username)
131{
132 if (d->username != username) {
133 d->username = username;
134 d->resetConnections();
135 Q_EMIT usernameChanged();
136 }
137}
138
139QString Author::name() const
140{
141 std::shared_ptr<KNSCore::Author> author = d->author();
142 if (author.get() && !author->name().isEmpty()) {
143 return author->name();
144 }
145 return d->username;
146}
147
148QString Author::description() const
149{
150 std::shared_ptr<KNSCore::Author> author = d->author();
151 if (author.get()) {
152 return author->description();
153 }
154 return QString{};
155}
156
157QString Author::homepage() const
158{
159 std::shared_ptr<KNSCore::Author> author = d->author();
160 if (author.get()) {
161 return author->homepage();
162 }
163 return QString{};
164}
165
166QString Author::profilepage() const
167{
168 std::shared_ptr<KNSCore::Author> author = d->author();
169 if (author.get()) {
170 return author->profilepage();
171 }
172 return QString{};
173}
174
175QUrl Author::avatarUrl() const
176{
177 std::shared_ptr<KNSCore::Author> author = d->author();
178 if (author.get()) {
179 return author->avatarUrl();
180 }
181 return QUrl{};
182}
183
184#include "moc_author.cpp"
void personLoaded(const std::shared_ptr< KNSCore::Author > author)
Fired when the details of a person have been loaded.
Engine * engine
The NewStuffQuickEngine to interact with servers through.
QString username
The user ID for the user this object represents.
QString providerId
The ID of the provider which the user is registered on.
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:44 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.