1 | /* |
2 | This file is part of KDE. |
3 | |
4 | SPDX-FileCopyrightText: 2009 Eckhart Wörner <ewoerner@kde.org> |
5 | SPDX-FileCopyrightText: 2011 Laszlo Papp <djszapi@archlinux.us> |
6 | SPDX-FileCopyrightText: 2012 Jeff Mitchell <mitchell@kde.org> |
7 | |
8 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
9 | */ |
10 | |
11 | #include "qtplatformdependent_p.h" |
12 | |
13 | #include <QDebug> |
14 | #include <QStringList> |
15 | #include <QUrl> |
16 | |
17 | using namespace Attica; |
18 | |
19 | QtPlatformDependent::QtPlatformDependent() |
20 | { |
21 | m_threadNamHash[QThread::currentThread()] = new QNetworkAccessManager(); |
22 | m_ourNamSet.insert(value: QThread::currentThread()); |
23 | QMetaObject::invokeMethod(object: this, function: &QtPlatformDependent::readyChanged, type: Qt::QueuedConnection); |
24 | } |
25 | |
26 | QtPlatformDependent::~QtPlatformDependent() |
27 | { |
28 | QThread *currThread = QThread::currentThread(); |
29 | if (m_threadNamHash.contains(key: currThread)) { |
30 | if (m_ourNamSet.contains(value: currThread)) { |
31 | delete m_threadNamHash[currThread]; |
32 | } |
33 | m_threadNamHash.remove(key: currThread); |
34 | m_ourNamSet.remove(value: currThread); |
35 | } |
36 | } |
37 | |
38 | void QtPlatformDependent::setNam(QNetworkAccessManager *nam) |
39 | { |
40 | if (!nam) { |
41 | return; |
42 | } |
43 | |
44 | QMutexLocker l(&m_accessMutex); |
45 | QThread *currThread = QThread::currentThread(); |
46 | QNetworkAccessManager *oldNam = nullptr; |
47 | if (m_threadNamHash.contains(key: currThread) && m_ourNamSet.contains(value: currThread)) { |
48 | oldNam = m_threadNamHash[currThread]; |
49 | } |
50 | |
51 | if (oldNam == nam) { |
52 | // If we're being passed back our own NAM, assume they want to |
53 | // ensure that we don't delete it out from under them |
54 | m_ourNamSet.remove(value: currThread); |
55 | return; |
56 | } |
57 | |
58 | m_threadNamHash[currThread] = nam; |
59 | m_ourNamSet.remove(value: currThread); |
60 | |
61 | if (oldNam) { |
62 | delete oldNam; |
63 | } |
64 | } |
65 | |
66 | QNetworkAccessManager *QtPlatformDependent::nam() |
67 | { |
68 | QMutexLocker l(&m_accessMutex); |
69 | QThread *currThread = QThread::currentThread(); |
70 | if (!m_threadNamHash.contains(key: currThread)) { |
71 | QNetworkAccessManager *newNam = new QNetworkAccessManager(); |
72 | m_threadNamHash[currThread] = newNam; |
73 | m_ourNamSet.insert(value: currThread); |
74 | return newNam; |
75 | } |
76 | |
77 | return m_threadNamHash[currThread]; |
78 | } |
79 | |
80 | // TODO actually save and restore providers! |
81 | QList<QUrl> Attica::QtPlatformDependent::getDefaultProviderFiles() const |
82 | { |
83 | // Return the defaultiest default provider (since that's what our documentation says we'll do) |
84 | return QList<QUrl>{QUrl(QStringLiteral("https://autoconfig.kde.org/ocs/providers.xml" ))}; |
85 | } |
86 | |
87 | void QtPlatformDependent::addDefaultProviderFile(const QUrl &) |
88 | { |
89 | qWarning() << "attica-qt does not support default providers yet" ; |
90 | } |
91 | |
92 | void QtPlatformDependent::removeDefaultProviderFile(const QUrl &) |
93 | { |
94 | } |
95 | |
96 | void QtPlatformDependent::enableProvider(const QUrl &baseUrl, bool enabled) const |
97 | { |
98 | Q_UNUSED(baseUrl) |
99 | Q_UNUSED(enabled) |
100 | qWarning() << "attica-qt does not support disabling of providers yet" ; |
101 | } |
102 | |
103 | bool QtPlatformDependent::isEnabled(const QUrl &baseUrl) const |
104 | { |
105 | Q_UNUSED(baseUrl) |
106 | return true; |
107 | } |
108 | |
109 | QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, const QByteArray &data) |
110 | { |
111 | return nam()->post(request, data); |
112 | } |
113 | |
114 | QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, QIODevice *data) |
115 | { |
116 | return nam()->post(request, data); |
117 | } |
118 | |
119 | QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, const QByteArray &data) |
120 | { |
121 | return nam()->put(request, data); |
122 | } |
123 | |
124 | QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, QIODevice *data) |
125 | { |
126 | return nam()->put(request, data); |
127 | } |
128 | |
129 | QNetworkReply *QtPlatformDependent::get(const QNetworkRequest &request) |
130 | { |
131 | return nam()->get(request); |
132 | } |
133 | |
134 | QNetworkReply *QtPlatformDependent::deleteResource(const QNetworkRequest &request) |
135 | { |
136 | return nam()->deleteResource(request); |
137 | } |
138 | |
139 | bool QtPlatformDependent::hasCredentials(const QUrl &baseUrl) const |
140 | { |
141 | return m_passwords.contains(key: baseUrl.toString()); |
142 | } |
143 | |
144 | bool QtPlatformDependent::saveCredentials(const QUrl &baseUrl, const QString &user, const QString &password) |
145 | { |
146 | m_passwords[baseUrl.toString()] = QPair<QString, QString>(user, password); |
147 | return true; |
148 | } |
149 | |
150 | bool QtPlatformDependent::loadCredentials(const QUrl &baseUrl, QString &user, QString &password) |
151 | { |
152 | if (!hasCredentials(baseUrl)) { |
153 | return false; |
154 | } |
155 | QPair<QString, QString> userPass = m_passwords.value(key: baseUrl.toString()); |
156 | user = userPass.first; |
157 | password = userPass.second; |
158 | return true; |
159 | } |
160 | |
161 | bool Attica::QtPlatformDependent::askForCredentials(const QUrl &baseUrl, QString &user, QString &password) |
162 | { |
163 | Q_UNUSED(baseUrl) |
164 | Q_UNUSED(user) |
165 | Q_UNUSED(password) |
166 | return false; |
167 | } |
168 | |
169 | bool Attica::QtPlatformDependent::isReady() |
170 | { |
171 | return true; |
172 | } |
173 | |