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 "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)
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
38 void resetConnections()
39 {
40 if (!componentCompleted) {
41 return;
42 }
43 if (provider) {
44 provider->disconnect(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(provider.get(), &KNSCore::Provider::personLoaded, q, [this](const std::shared_ptr<KNSCore::Author> author) {
54 allAuthors()->insert(QStringLiteral("%1 %2").arg(provider->id(), author->id()), 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(provider->id(), 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(this, &Author::engineChanged, &Author::dataChanged);
83 connect(this, &Author::providerIdChanged, &Author::dataChanged);
84 connect(this, &Author::usernameChanged, &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
100{
101 return d->engine;
102}
103
104void Author::setEngine(QObject *newEngine)
105{
106 if (d->engine != newEngine) {
108 d->resetConnections();
109 Q_EMIT engineChanged();
110 }
111}
112
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
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"
KNSCore::EngineBase for interfacing with QML.
Definition quickengine.h:27
QSharedPointer< Provider > provider(const QString &providerId) const
The Provider instance with the passed ID.
QSharedPointer< Provider > defaultProvider() const
Return the first provider in the providers list (usually the default provider)
void personLoaded(const std::shared_ptr< KNSCore::Author > author)
Fired when the details of a person have been loaded.
Encapsulates a KNSCore::Author for use in Qt Quick.
QString username
The user ID for the user this object represents.
QString providerId
The ID of the provider which the user is registered on.
QObject * engine
The NewStuffQuickEngine to interact with servers through.
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T * get() const const
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.