File indexing completed on 2024-04-14 14:16:57

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2009 Eckhart Wörner <ewoerner@kde.org>
0005     SPDX-FileCopyrightText: 2011 Laszlo Papp <djszapi@archlinux.us>
0006     SPDX-FileCopyrightText: 2012 Jeff Mitchell <mitchell@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0009 */
0010 
0011 #include "qtplatformdependent_p.h"
0012 
0013 #include <QDebug>
0014 #include <QStringList>
0015 #include <QUrl>
0016 
0017 using namespace Attica;
0018 
0019 QtPlatformDependent::QtPlatformDependent()
0020 {
0021     m_threadNamHash[QThread::currentThread()] = new QNetworkAccessManager();
0022     m_ourNamSet.insert(QThread::currentThread());
0023 }
0024 
0025 QtPlatformDependent::~QtPlatformDependent()
0026 {
0027     QThread *currThread = QThread::currentThread();
0028     if (m_threadNamHash.contains(currThread)) {
0029         if (m_ourNamSet.contains(currThread)) {
0030             delete m_threadNamHash[currThread];
0031         }
0032         m_threadNamHash.remove(currThread);
0033         m_ourNamSet.remove(currThread);
0034     }
0035 }
0036 
0037 void QtPlatformDependent::setNam(QNetworkAccessManager *nam)
0038 {
0039     if (!nam) {
0040         return;
0041     }
0042 
0043     QMutexLocker l(&m_accessMutex);
0044     QThread *currThread = QThread::currentThread();
0045     QNetworkAccessManager *oldNam = nullptr;
0046     if (m_threadNamHash.contains(currThread) && m_ourNamSet.contains(currThread)) {
0047         oldNam = m_threadNamHash[currThread];
0048     }
0049 
0050     if (oldNam == nam) {
0051         // If we're being passed back our own NAM, assume they want to
0052         // ensure that we don't delete it out from under them
0053         m_ourNamSet.remove(currThread);
0054         return;
0055     }
0056 
0057     m_threadNamHash[currThread] = nam;
0058     m_ourNamSet.remove(currThread);
0059 
0060     if (oldNam) {
0061         delete oldNam;
0062     }
0063 }
0064 
0065 QNetworkAccessManager *QtPlatformDependent::nam()
0066 {
0067     QMutexLocker l(&m_accessMutex);
0068     QThread *currThread = QThread::currentThread();
0069     if (!m_threadNamHash.contains(currThread)) {
0070         QNetworkAccessManager *newNam = new QNetworkAccessManager();
0071         m_threadNamHash[currThread] = newNam;
0072         m_ourNamSet.insert(currThread);
0073         return newNam;
0074     }
0075 
0076     return m_threadNamHash[currThread];
0077 }
0078 
0079 // TODO actually save and restore providers!
0080 QList<QUrl> Attica::QtPlatformDependent::getDefaultProviderFiles() const
0081 {
0082     // Return the defaultiest default provider (since that's what our documentation says we'll do)
0083     return QList<QUrl>{QUrl(QStringLiteral("https://autoconfig.kde.org/ocs/providers.xml"))};
0084 }
0085 
0086 void QtPlatformDependent::addDefaultProviderFile(const QUrl &)
0087 {
0088     qWarning() << "attica-qt does not support default providers yet";
0089 }
0090 
0091 void QtPlatformDependent::removeDefaultProviderFile(const QUrl &)
0092 {
0093 }
0094 
0095 void QtPlatformDependent::enableProvider(const QUrl &baseUrl, bool enabled) const
0096 {
0097     Q_UNUSED(baseUrl)
0098     Q_UNUSED(enabled)
0099     qWarning() << "attica-qt does not support disabling of providers yet";
0100 }
0101 
0102 bool QtPlatformDependent::isEnabled(const QUrl &baseUrl) const
0103 {
0104     Q_UNUSED(baseUrl)
0105     return true;
0106 }
0107 
0108 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, const QByteArray &data)
0109 {
0110     return nam()->post(request, data);
0111 }
0112 
0113 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, QIODevice *data)
0114 {
0115     return nam()->post(request, data);
0116 }
0117 
0118 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, const QByteArray &data)
0119 {
0120     return nam()->put(request, data);
0121 }
0122 
0123 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, QIODevice *data)
0124 {
0125     return nam()->put(request, data);
0126 }
0127 
0128 QNetworkReply *QtPlatformDependent::get(const QNetworkRequest &request)
0129 {
0130     return nam()->get(request);
0131 }
0132 
0133 QNetworkReply *QtPlatformDependent::deleteResource(const QNetworkRequest &request)
0134 {
0135     return nam()->deleteResource(request);
0136 }
0137 
0138 bool QtPlatformDependent::hasCredentials(const QUrl &baseUrl) const
0139 {
0140     return m_passwords.contains(baseUrl.toString());
0141 }
0142 
0143 bool QtPlatformDependent::saveCredentials(const QUrl &baseUrl, const QString &user, const QString &password)
0144 {
0145     m_passwords[baseUrl.toString()] = QPair<QString, QString>(user, password);
0146     return true;
0147 }
0148 
0149 bool QtPlatformDependent::loadCredentials(const QUrl &baseUrl, QString &user, QString &password)
0150 {
0151     if (!hasCredentials(baseUrl)) {
0152         return false;
0153     }
0154     QPair<QString, QString> userPass = m_passwords.value(baseUrl.toString());
0155     user = userPass.first;
0156     password = userPass.second;
0157     return true;
0158 }
0159 
0160 bool Attica::QtPlatformDependent::askForCredentials(const QUrl &baseUrl, QString &user, QString &password)
0161 {
0162     Q_UNUSED(baseUrl)
0163     Q_UNUSED(user)
0164     Q_UNUSED(password)
0165     return false;
0166 }