KNewStuff

qtquick/author.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <[email protected]>
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/engine.h"
13 #include "core/provider.h"
14 
15 #include "knewstuffquick_debug.h"
16 
17 #include <memory>
18 
19 namespace KNewStuffQuick {
20 
21 // This caching will want to eventually go into the Provider level (and be more generalised)
23 Q_GLOBAL_STATIC(AllAuthorsHash, allAuthors)
24 
25 class Author::Private {
26 public:
27  Private(Author *qq)
28  : q(qq)
29  {}
30  Author *q;
31  bool componentCompleted{false};
32  Engine *engine{nullptr};
33  QString providerId;
34  QString username;
35 
37  void resetConnections() {
38  if (!componentCompleted) {
39  return;
40  }
41  if (provider) {
42  provider->disconnect(q);
43  }
44  if (engine && engine->engine()) {
45  KNSCore::Engine *coreEngine = qobject_cast<KNSCore::Engine*>(engine->engine());
46  if (coreEngine) {
47  provider = coreEngine->provider(providerId);
48  }
49  if (!provider) {
50  provider = coreEngine->defaultProvider();
51  }
52  }
53  if (provider) {
54  connect(provider.get(), &KNSCore::Provider::personLoaded, q, [=](const std::shared_ptr< KNSCore::Author > author){
55  allAuthors()->insert(QString::fromLatin1("%1 %2").arg(provider->id()).arg(author->id()), author);
56  emit q->dataChanged();
57  });
58  author(); // Check and make sure...
59  }
60  }
61 
62  std::shared_ptr<KNSCore::Author> author()
63  {
64  std::shared_ptr<KNSCore::Author> ret;
65  if (provider && !username.isEmpty()) {
66  ret = allAuthors()->value(QString::fromLatin1("%1 %2").arg(provider->id()).arg(username));
67  if(!ret.get()) {
68  provider->loadPerson(username);
69  }
70  }
71  return ret;
72  }
73 };
74 }
75 
76 using namespace KNewStuffQuick;
77 
78 Author::Author(QObject *parent)
79  : QObject(parent)
80  , d(new Private(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 
87 Author::~Author()
88 {
89  delete d;
90 }
91 
92 void Author::classBegin()
93 { }
94 
95 void Author::componentComplete()
96 {
97  d->componentCompleted = true;
98  d->resetConnections();
99 }
100 
101 QObject *Author::engine() const
102 {
103  return d->engine;
104 }
105 
106 void Author::setEngine(QObject *newEngine)
107 {
108  if (d->engine != newEngine) {
109  d->engine = qobject_cast<Engine*>(newEngine);
110  d->resetConnections();
111  emit engineChanged();
112  }
113 }
114 
115 QString Author::providerId() const
116 {
117  return d->providerId;
118 }
119 
120 void Author::setProviderId(const QString &providerId)
121 {
122  if (d->providerId != providerId) {
123  d->providerId = providerId;
124  d->resetConnections();
125  emit providerIdChanged();
126  }
127 }
128 
129 QString Author::username() const
130 {
131  return d->username;
132 }
133 
134 void Author::setUsername(const QString &username)
135 {
136  if (d->username != username) {
137  d->username = username;
138  d->resetConnections();
139  emit usernameChanged();
140  }
141 }
142 
143 QString Author::name() const
144 {
145  std::shared_ptr<KNSCore::Author> author = d->author();
146  if (author.get() && !author->name().isEmpty()) {
147  return author->name();
148  }
149  return d->username;
150 }
151 
153 {
154  std::shared_ptr<KNSCore::Author> author = d->author();
155  if (author.get()) {
156  return author->description();
157  }
158  return QString{};
159 }
160 
162 {
163  std::shared_ptr<KNSCore::Author> author = d->author();
164  if (author.get()) {
165  return author->homepage();
166  }
167  return QString{};
168 }
169 
171 {
172  std::shared_ptr<KNSCore::Author> author = d->author();
173  if (author.get()) {
174  return author->profilepage();
175  }
176  return QString{};
177 }
178 
179 QUrl Author::avatarUrl() const
180 {
181  std::shared_ptr<KNSCore::Author> author = d->author();
182  if (author.get()) {
183  return author->avatarUrl();
184  }
185  return QUrl{};
186 }
Encapsulates a KNSCore::Author for use in Qt Quick.
Encapsulates a KNSCore::Engine for use in Qt Quick.
Definition: quickengine.h:23
QUrl avatarUrl() const
Retrieve the url of the user&#39;s avatar image.
bool isEmpty() const const
QString description() const
Retrieve the user&#39;s description text.
QSharedPointer< Provider > provider(const QString &providerId) const
The Provider instance with the passed ID.
Definition: engine.cpp:925
QString homepage() const
Retrieve the author&#39;s homepage.
void personLoaded(const std::shared_ptr< KNSCore::Author > author)
Fired when the details of a person have been loaded.
QString profilepage() const
Retrieve the author&#39;s profile page.
KNewStuff engine.
Definition: engine.h:59
QSharedPointer< Provider > defaultProvider() const
Return the first provider in the providers list (usually the default provider)
Definition: engine.cpp:930
QString fromLatin1(const char *str, int size)
QString name() const
Retrieve the author&#39;s name.
Definition: core/author.cpp:82
T * get() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 22:43:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.