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 | |
16 | namespace KNewStuffQuick |
17 | { |
18 | // This caching will want to eventually go into the Provider level (and be more generalised) |
19 | typedef QHash<QString, std::shared_ptr<KNSCore::Author>> AllAuthorsHash; |
20 | Q_GLOBAL_STATIC(AllAuthorsHash, allAuthors) |
21 | |
22 | class AuthorPrivate |
23 | { |
24 | public: |
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(receiver: 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(sender: provider.get(), signal: &KNSCore::Provider::personLoaded, context: q, slot: [this](const std::shared_ptr<KNSCore::Author> author) { |
52 | allAuthors()->insert(QStringLiteral("%1 %2" ).arg(args: provider->id(), args: author->id()), value: 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(args: provider->id(), args&: username)); |
65 | if (!ret.get()) { |
66 | provider->loadPerson(username); |
67 | } |
68 | } |
69 | return ret; |
70 | } |
71 | }; |
72 | } |
73 | |
74 | using namespace KNewStuffQuick; |
75 | |
76 | Author::Author(QObject *parent) |
77 | : QObject(parent) |
78 | , d(new AuthorPrivate(this)) |
79 | { |
80 | connect(sender: this, signal: &Author::engineChanged, slot: &Author::dataChanged); |
81 | connect(sender: this, signal: &Author::providerIdChanged, slot: &Author::dataChanged); |
82 | connect(sender: this, signal: &Author::usernameChanged, slot: &Author::dataChanged); |
83 | } |
84 | |
85 | Author::~Author() = default; |
86 | |
87 | void Author::classBegin() |
88 | { |
89 | } |
90 | |
91 | void Author::componentComplete() |
92 | { |
93 | d->componentCompleted = true; |
94 | d->resetConnections(); |
95 | } |
96 | |
97 | Engine *Author::engine() const |
98 | { |
99 | return d->engine; |
100 | } |
101 | |
102 | void Author::setEngine(Engine *newEngine) |
103 | { |
104 | if (d->engine != newEngine) { |
105 | d->engine = newEngine; |
106 | d->resetConnections(); |
107 | Q_EMIT engineChanged(); |
108 | } |
109 | } |
110 | |
111 | QString Author::providerId() const |
112 | { |
113 | return d->providerId; |
114 | } |
115 | |
116 | void 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 | |
125 | QString Author::username() const |
126 | { |
127 | return d->username; |
128 | } |
129 | |
130 | void 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 | |
139 | QString 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 | |
148 | QString 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 | |
157 | QString 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 | |
166 | QString 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 | |
175 | QUrl 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" |
185 | |