10#include "providermanager.h"
12#include "attica_debug.h"
13#include "atticautils.h"
15#include <QAuthenticator>
16#include <QCoreApplication>
19#include <QNetworkProxy>
20#include <QPluginLoader>
22#include <QSignalMapper>
24#include <QXmlStreamReader>
26#include "platformdependent.h"
27#include "platformdependent_v3.h"
28#include "qtplatformdependent_p.h"
29#include <QLibraryInfo>
36 PlatformDependent *m_internals;
37 QHash<QUrl, Provider> m_providers;
38 QHash<QUrl, QUrl> m_providerTargets;
39 QHash<QString, QNetworkReply *> m_downloads;
40 bool m_authenticationSuppressed;
43 : m_internals(nullptr)
44 , m_authenticationSuppressed(false)
53PlatformDependent *ProviderManager::loadPlatformDependent(
const ProviderFlags &flags)
55 if (flags & ProviderManager::DisablePlugins) {
56 return new QtPlatformDependent;
59 QPluginLoader loader(QStringLiteral(
"attica_kde"));
62 return ret ? ret :
new QtPlatformDependent;
65ProviderManager::ProviderManager(
const ProviderFlags &flags)
68 d->m_internals = loadPlatformDependent(flags);
75 if (platformDependentV3 && !platformDependentV3->isReady()) {
79 &ProviderManager::slotLoadDefaultProvidersInternal,
89 d->m_authenticationSuppressed = suppressed;
94 d->m_providerTargets.clear();
95 d->m_providers.clear();
98void ProviderManager::slotLoadDefaultProvidersInternal()
100 const auto providerFiles = d->m_internals->getDefaultProviderFiles();
101 for (
const QUrl &url : providerFiles) {
102 addProviderFile(url);
104 if (d->m_downloads.
isEmpty()) {
105 Q_EMIT defaultProvidersLoaded();
111 return d->m_internals->getDefaultProviderFiles();
114ProviderManager::~ProviderManager()
121 d->m_internals->addDefaultProviderFile(url);
122 addProviderFile(url);
125void ProviderManager::removeProviderFileFromDefaultProviders(
const QUrl &url)
127 d->m_internals->removeDefaultProviderFile(url);
130void ProviderManager::addProviderFile(
const QUrl &url)
135 qWarning() <<
"ProviderManager::addProviderFile: could not open provider file: " << url.
toString();
138 parseProviderFile(QLatin1String(file.readAll()), url);
140 if (!d->m_downloads.contains(url.
toString())) {
141 QNetworkRequest req(url);
143 QNetworkReply *reply = d->m_internals->get(req);
144 qCDebug(ATTICA) <<
"executing" << Utils::toString(reply->
operation()) <<
"for" << reply->
url();
148 d->m_downloads.insert(url.
toString(), reply);
153void ProviderManager::fileFinished(
const QString &url)
155 QNetworkReply *reply = d->m_downloads.take(url);
157 if (reply->
error()) {
160 parseProviderFile(QLatin1String(reply->
readAll()), QUrl(url));
170 parseProviderFile(providerXml,
QUrl());
173void ProviderManager::parseProviderFile(
const QString &xmlString,
const QUrl &url)
176 while (!xml.atEnd() && xml.readNext()) {
177 if (xml.isStartElement() && xml.name() ==
QLatin1String(
"provider")) {
194 while (!xml.atEnd() && xml.readNext()) {
195 if (xml.isStartElement()) {
197 baseUrl =
QUrl(xml.readElementText());
199 name = xml.readElementText();
200 }
else if (xml.name() == QLatin1String(
"icon")) {
201 icon = QUrl(xml.readElementText());
202 }
else if (xml.name() == QLatin1String(
"person")) {
203 person = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
204 }
else if (xml.name() == QLatin1String(
"friend")) {
205 friendV = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
206 }
else if (xml.name() == QLatin1String(
"message")) {
207 message = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
208 }
else if (xml.name() == QLatin1String(
"achievement")) {
209 achievement = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
210 }
else if (xml.name() == QLatin1String(
"activity")) {
211 activity = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
212 }
else if (xml.name() == QLatin1String(
"content")) {
213 content = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
214 }
else if (xml.name() == QLatin1String(
"fan")) {
215 fan = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
216 }
else if (xml.name() == QLatin1String(
"forum")) {
217 forum = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
218 }
else if (xml.name() == QLatin1String(
"knowledgebase")) {
219 knowledgebase = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
220 }
else if (xml.name() == QLatin1String(
"event")) {
221 event = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
222 }
else if (xml.name() == QLatin1String(
"comment")) {
223 comment = xml.attributes().value(QLatin1String(
"ocsversion")).toString();
224 }
else if (xml.name() == QLatin1String(
"register")) {
225 registerUrl = xml.readElementText();
227 }
else if (xml.isEndElement() && xml.name() == QLatin1String(
"provider")) {
233 d->m_providers.insert(baseUrl,
234 Provider(d->m_internals,
250 d->m_providerTargets[url] = baseUrl;
251 Q_EMIT providerAdded(d->m_providers.value(baseUrl));
257 qCDebug(ATTICA) <<
"error:" << xml.errorString() <<
"in" << url;
260 if (d->m_downloads.isEmpty()) {
261 Q_EMIT defaultProvidersLoaded();
272 return d->m_providers.value(url);
277 return d->m_providers.values();
282 return d->m_providerTargets.
keys();
289 for (
const QUrl &url : urls) {
301 if (d->m_internals->hasCredentials(baseUrl)) {
302 if (d->m_internals->loadCredentials(baseUrl, user, password)) {
311 if (!d->m_authenticationSuppressed && d->m_internals->askForCredentials(baseUrl, user, password)) {
318 qWarning() <<
"ProviderManager::authenticate: No authentication credentials provided, aborting." << reply->
url().
toString();
319 Q_EMIT authenticationCredentialsMissing(d->m_providers.value(baseUrl));
323void ProviderManager::proxyAuthenticationRequired(
const QNetworkProxy &proxy, QAuthenticator *authenticator)
326 Q_UNUSED(authenticator)
329#include "moc_providermanager.cpp"
void addProviderFromXml(const QString &providerXml)
Parse a xml file containing a provider description.
void addProviderFileToDefaultProviders(const QUrl &url)
Add a provider file to the default providers (xml that contains provider descriptions).
void setAuthenticationSuppressed(bool suppressed)
Suppresses the authentication, so that the application can take care of authenticating itself.
QList< Provider > providers() const
Provider providerByUrl(const QUrl &url) const
void loadDefaultProviders()
Load available providers from configuration.
void clear()
Remove all providers and provider files that have been loaded.
QList< QUrl > defaultProviderFiles()
The list of provider files that get loaded by loadDefaultProviders.
Provider providerFor(const QUrl &url) const
The Provider class represents one Open Collaboration Service provider.
QString password() const const
void setPassword(const QString &password)
void setUser(const QString &user)
QString user() const const
bool isEmpty() const const
QList< Key > keys() const const
void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
NetworkError error() const const
QNetworkAccessManager::Operation operation() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
T qobject_cast(QObject *object)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isEmpty() const const
bool isLocalFile() const const
bool isParentOf(const QUrl &childUrl) const const
QString toLocalFile() const const
QString toString(FormattingOptions options) const const