1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
This file is part of KDE.
Copyright (c) 2009 Eckhart Wörner <ewoerner@kde.org>
Copyright (c) 2009 Frederik Gladhorn <gladhorn@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "providermanager.h"
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QPluginLoader>
#include <QtCore/QSet>
#include <QtCore/QSignalMapper>
#include <QtCore/QTimer>
#include <QtCore/QProcess>
#include <QtNetwork/QAuthenticator>
#include <QtNetwork/QNetworkReply>
#if QT_VERSION >= 0x050000
#include <QtCore/QXmlStreamReader>
#else
#include <QtXml/QXmlStreamReader>
#endif // QT_VERSION
#include "platformdependent.h"
#include "qtplatformdependent.h"
#include <QLibraryInfo>
using namespace Attica;
#if QT_VERSION < 0x040700
uint qHash(const QUrl& key) {
return qHash(key.toString());
}
#endif
class ProviderManager::Private {
public:
PlatformDependent* m_internals;
QHash<QUrl, Provider> m_providers;
QHash<QUrl, QList<QString> > m_providerFiles;
QSignalMapper m_downloadMapping;
QHash<QString, QNetworkReply*> m_downloads;
QPluginLoader m_pluginLoader;
bool m_authenticationSuppressed;
Private()
: m_internals(0)
, m_authenticationSuppressed(false)
{
}
~Private()
{
// do not delete m_internals: it is the root component of a plugin!
}
};
PlatformDependent* ProviderManager::loadPlatformDependent(const ProviderFlags& flags)
{
// OS specific stuff
#if defined Q_WS_WIN
#define PATH_SEPARATOR ';'
#define LIB_EXTENSION "dll"
#else
#define PATH_SEPARATOR ':'
#define LIB_EXTENSION "so"
#endif
if (flags & ProviderManager::DisablePlugins)
{
//qDebug() << "Disabling provider plugins per application request";
return new QtPlatformDependent;
}
// use qt plugin dir, if that is not found, fall back to kde plugin path (the old way)
QStringList paths;
paths.append(QLibraryInfo::location(QLibraryInfo::PluginsPath));
// old plugin location, required for attica < 0.1.5
QString program(QLatin1String( "kde4-config" ));
QStringList arguments;
arguments << QLatin1String( "--path" ) << QLatin1String( "lib" );
QProcess process;
process.start(program, arguments);
process.waitForFinished();
/* Try to find the KDE plugin. This can be extended to include other platform specific plugins. */
paths.append(QString(QLatin1String( process.readAllStandardOutput() )).trimmed().split(QLatin1Char( PATH_SEPARATOR )));
//qDebug() << "Plugin paths: " << paths;
QString pluginName(QLatin1String( "attica_kde" ));
foreach(const QString& path, paths) {
QString libraryPath(path + QLatin1Char( '/' ) + pluginName + QLatin1Char( '.' ) + QLatin1String( LIB_EXTENSION ));
//qDebug() << "Trying to load Attica plugin: " << libraryPath;
if (QFile::exists(libraryPath)) {
d->m_pluginLoader.setFileName(libraryPath);
QObject* plugin = d->m_pluginLoader.instance();
if (plugin) {
PlatformDependent* platformDependent = qobject_cast<PlatformDependent*>(plugin);
if (platformDependent) {
//qDebug() << "Using Attica with KDE support";
return platformDependent;
}
}
}
}
//qDebug() << "Using Attica without KDE support";
return new QtPlatformDependent;
}
ProviderManager::ProviderManager(const ProviderFlags& flags)
: d(new Private)
{
d->m_internals = loadPlatformDependent(flags);
connect(d->m_internals->nam(), SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(authenticate(QNetworkReply*,QAuthenticator*)));
connect(&d->m_downloadMapping, SIGNAL(mapped(QString)), SLOT(fileFinished(QString)));
}
void ProviderManager::loadDefaultProviders()
{
QTimer::singleShot(0, this, SLOT(slotLoadDefaultProvidersInternal()));
}
void ProviderManager::setAuthenticationSuppressed(bool suppressed)
{
d->m_authenticationSuppressed = suppressed;
}
void ProviderManager::clear()
{
d->m_providerFiles.clear();
d->m_providers.clear();
}
void ProviderManager::slotLoadDefaultProvidersInternal()
{
foreach (const QUrl& url, d->m_internals->getDefaultProviderFiles()) {
addProviderFile(url);
}
if (d->m_downloads.isEmpty()) {
emit defaultProvidersLoaded();
}
}
QList<QUrl> ProviderManager::defaultProviderFiles()
{
return d->m_internals->getDefaultProviderFiles();
}
ProviderManager::~ProviderManager()
{
delete d;
}
void ProviderManager::addProviderFileToDefaultProviders(const QUrl& url)
{
d->m_internals->addDefaultProviderFile(url);
addProviderFile(url);
}
void ProviderManager::removeProviderFileFromDefaultProviders(const QUrl& url)
{
d->m_internals->removeDefaultProviderFile(url);
}
void ProviderManager::addProviderFile(const QUrl& url)
{
QString localFile = url.toLocalFile();
if (!localFile.isEmpty()) {
QFile file(localFile);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "ProviderManager::addProviderFile: could not open provider file: " << url.toString();
return;
}
addProviderFromXml(QLatin1String( file.readAll() ));
} else {
if (!d->m_downloads.contains(url.toString())) {
QNetworkReply* reply = d->m_internals->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), &d->m_downloadMapping, SLOT(map()));
d->m_downloadMapping.setMapping(reply, url.toString());
d->m_downloads.insert(url.toString(), reply);
}
}
}
void ProviderManager::fileFinished(const QString& url)
{
QNetworkReply* reply = d->m_downloads.take(url);
parseProviderFile(QLatin1String ( reply->readAll() ), url);
reply->deleteLater();
}
void ProviderManager::addProviderFromXml(const QString& providerXml)
{
parseProviderFile(providerXml, QString());
}
void ProviderManager::parseProviderFile(const QString& xmlString, const QString& url)
{
QXmlStreamReader xml(xmlString);
while (!xml.atEnd() && xml.readNext()) {
if (xml.isStartElement() && xml.name() == QLatin1String("provider")) {
QString baseUrl;
QString name;
QUrl icon;
QString person;
QString friendV;
QString message;
QString achievement;
QString activity;
QString content;
QString fan;
QString forum;
QString knowledgebase;
QString event;
QString comment;
QString registerUrl;
while (!xml.atEnd() && xml.readNext()) {
if (xml.isStartElement())
{
if (xml.name() == QLatin1String("location")) {
baseUrl = xml.readElementText();
} else if (xml.name() == QLatin1String("name")) {
name = xml.readElementText();
} else if (xml.name() == QLatin1String("icon")) {
icon = QUrl(xml.readElementText());
} else if (xml.name() == QLatin1String("person")) {
person = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("friend")) {
friendV = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("message")) {
message = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("achievement")) {
achievement = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("activity")) {
activity = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("content")) {
content = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("fan")) {
fan = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("forum")) {
forum = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("knowledgebase")) {
knowledgebase = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("event")) {
event = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("comment")) {
comment = xml.attributes().value(QLatin1String( "ocsversion" )).toString();
} else if (xml.name() == QLatin1String("register")) {
registerUrl = xml.readElementText();
}
} else if (xml.isEndElement() && xml.name() == QLatin1String("provider")) {
break;
}
}
if (!baseUrl.isEmpty()) {
//qDebug() << "Adding provider" << baseUrl;
d->m_providers.insert(baseUrl, Provider(d->m_internals, QUrl(baseUrl), name, icon,
person, friendV, message, achievement, activity, content, fan, forum, knowledgebase,
event, comment, registerUrl));
emit providerAdded(d->m_providers.value(baseUrl));
}
}
}
if (d->m_downloads.isEmpty()) {
emit defaultProvidersLoaded();
}
}
Provider ProviderManager::providerByUrl(const QUrl& url) const {
return d->m_providers.value(url);
}
QList<Provider> ProviderManager::providers() const {
return d->m_providers.values();
}
bool ProviderManager::contains(const QString& provider) const {
return d->m_providers.contains(provider);
}
QList<QUrl> ProviderManager::providerFiles() const {
return d->m_providerFiles.keys();
}
void ProviderManager::authenticate(QNetworkReply* reply, QAuthenticator* auth)
{
QUrl baseUrl;
QList<QUrl> urls = d->m_providers.keys();
foreach (const QUrl& url, urls) {
if (url.isParentOf(reply->url())) {
baseUrl = url;
break;
}
}
//qDebug() << "ProviderManager::authenticate" << baseUrl;
QString user;
QString password;
if (auth->user().isEmpty() && auth->password().isEmpty()) {
if (d->m_internals->hasCredentials(baseUrl)) {
if (d->m_internals->loadCredentials(baseUrl, user, password)) {
//qDebug() << "ProviderManager::authenticate: loading authentication";
auth->setUser(user);
auth->setPassword(password);
return;
}
}
}
if (!d->m_authenticationSuppressed && d->m_internals->askForCredentials(baseUrl, user, password)) {
//qDebug() << "ProviderManager::authenticate: asking internals for new credentials";
//auth->setUser(user);
//auth->setPassword(password);
return;
}
qWarning() << "ProviderManager::authenticate: No authentication credentials provided, aborting." << reply->url().toString();
emit authenticationCredentialsMissing(d->m_providers.value(baseUrl));
reply->abort();
}
void ProviderManager::proxyAuthenticationRequired(const QNetworkProxy& proxy, QAuthenticator* authenticator)
{
}
void ProviderManager::initNetworkAccesssManager()
{
connect(d->m_internals->nam(), SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(authenticate(QNetworkReply*,QAuthenticator*)));
connect(d->m_internals->nam(), SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
}
#include "providermanager.moc"
|