File indexing completed on 2024-05-19 05:47:32
0001 /* 0002 This file is part of KDE. 0003 0004 Copyright (c) 2009 Eckhart Wörner <ewoerner@kde.org> 0005 Copyright (c) 2011 Laszlo Papp <djszapi@archlinux.us> 0006 Copyright (c) 2012 Jeff Mitchell <mitchell@kde.org> 0007 0008 This library is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU Lesser General Public 0010 License as published by the Free Software Fo1undation; either 0011 version 2.1 of the License, or (at your option) version 3, or any 0012 later version accepted by the membership of KDE e.V. (or its 0013 successor approved by the membership of KDE e.V.), which shall 0014 act as a proxy defined in Section 6 of version 3 of the license. 0015 0016 This library is distributed in the hope that it will be useful, 0017 but WITHOUT ANY WARRANTY; without even the implied warranty of 0018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0019 Lesser General Public License for more details. 0020 0021 You should have received a copy of the GNU Lesser General Public 0022 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0023 0024 */ 0025 0026 #include "qtplatformdependent_p.h" 0027 0028 #include <QUrl> 0029 #include <QStringList> 0030 #include <QDebug> 0031 0032 using namespace Attica; 0033 0034 QtPlatformDependent::QtPlatformDependent() 0035 { 0036 m_threadNamHash[ QThread::currentThread() ] = new QNetworkAccessManager(); 0037 m_ourNamSet.insert(QThread::currentThread()); 0038 } 0039 0040 QtPlatformDependent::~QtPlatformDependent() 0041 { 0042 QThread *currThread = QThread::currentThread(); 0043 if (m_threadNamHash.contains(currThread)) { 0044 if (m_ourNamSet.contains(currThread)) { 0045 delete m_threadNamHash[ currThread ]; 0046 } 0047 m_threadNamHash.remove(currThread); 0048 m_ourNamSet.remove(currThread); 0049 } 0050 } 0051 0052 void QtPlatformDependent::setNam(QNetworkAccessManager *nam) 0053 { 0054 if (!nam) { 0055 return; 0056 } 0057 0058 QMutexLocker l(&m_accessMutex); 0059 QThread *currThread = QThread::currentThread(); 0060 QNetworkAccessManager *oldNam = nullptr; 0061 if (m_threadNamHash.contains(currThread) && m_ourNamSet.contains(currThread)) { 0062 oldNam = m_threadNamHash[ currThread ]; 0063 } 0064 0065 if (oldNam == nam) { 0066 // If we're being passed back our own NAM, assume they want to 0067 // ensure that we don't delete it out from under them 0068 m_ourNamSet.remove(currThread); 0069 return; 0070 } 0071 0072 m_threadNamHash[ currThread ] = nam; 0073 m_ourNamSet.remove(currThread); 0074 0075 if (oldNam) { 0076 delete oldNam; 0077 } 0078 } 0079 0080 QNetworkAccessManager *QtPlatformDependent::nam() 0081 { 0082 QMutexLocker l(&m_accessMutex); 0083 QThread *currThread = QThread::currentThread(); 0084 if (!m_threadNamHash.contains(currThread)) { 0085 QNetworkAccessManager *newNam = new QNetworkAccessManager(); 0086 m_threadNamHash[ currThread ] = newNam; 0087 m_ourNamSet.insert(currThread); 0088 return newNam; 0089 } 0090 0091 return m_threadNamHash[ currThread ]; 0092 } 0093 0094 // TODO actually save and restore providers! 0095 QList<QUrl> Attica::QtPlatformDependent::getDefaultProviderFiles() const 0096 { 0097 return QList<QUrl>(); 0098 } 0099 0100 void QtPlatformDependent::addDefaultProviderFile(const QUrl &) 0101 { 0102 qWarning() << "attica-qt does not support default providers yet"; 0103 } 0104 0105 void QtPlatformDependent::removeDefaultProviderFile(const QUrl &) 0106 { 0107 } 0108 0109 void QtPlatformDependent::enableProvider(const QUrl &baseUrl, bool enabled) const 0110 { 0111 Q_UNUSED(baseUrl) 0112 Q_UNUSED(enabled) 0113 qWarning() << "attica-qt does not support disabling of providers yet"; 0114 } 0115 0116 bool QtPlatformDependent::isEnabled(const QUrl &baseUrl) const 0117 { 0118 Q_UNUSED(baseUrl) 0119 return true; 0120 } 0121 0122 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, const QByteArray &data) 0123 { 0124 return nam()->post(request, data); 0125 } 0126 0127 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, QIODevice *data) 0128 { 0129 return nam()->post(request, data); 0130 } 0131 0132 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, const QByteArray &data) 0133 { 0134 return nam()->put(request, data); 0135 } 0136 0137 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, QIODevice *data) 0138 { 0139 return nam()->put(request, data); 0140 } 0141 0142 QNetworkReply *QtPlatformDependent::get(const QNetworkRequest &request) 0143 { 0144 return nam()->get(request); 0145 } 0146 0147 QNetworkReply *QtPlatformDependent::deleteResource(const QNetworkRequest &request) 0148 { 0149 return nam()->deleteResource(request); 0150 } 0151 0152 bool QtPlatformDependent::hasCredentials(const QUrl &baseUrl) const 0153 { 0154 return m_passwords.contains(baseUrl.toString()); 0155 } 0156 0157 bool QtPlatformDependent::saveCredentials(const QUrl &baseUrl, const QString &user, const QString &password) 0158 { 0159 m_passwords[baseUrl.toString()] = QPair<QString, QString> (user, password); 0160 return true; 0161 } 0162 0163 bool QtPlatformDependent::loadCredentials(const QUrl &baseUrl, QString &user, QString &password) 0164 { 0165 if (!hasCredentials(baseUrl)) { 0166 return false; 0167 } 0168 QPair<QString, QString> userPass = m_passwords.value(baseUrl.toString()); 0169 user = userPass.first; 0170 password = userPass.second; 0171 return true; 0172 } 0173 0174 bool Attica::QtPlatformDependent::askForCredentials(const QUrl &baseUrl, QString &user, QString &password) 0175 { 0176 Q_UNUSED(baseUrl) 0177 Q_UNUSED(user) 0178 Q_UNUSED(password) 0179 return false; 0180 } 0181