Attica

qtplatformdependent.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2009 Eckhart W├Ârner <[email protected]>
5  SPDX-FileCopyrightText: 2011 Laszlo Papp <[email protected]>
6  SPDX-FileCopyrightText: 2012 Jeff Mitchell <[email protected]>
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 <QUrl>
14 #include <QStringList>
15 #include <QDebug>
16 
17 using namespace Attica;
18 
19 QtPlatformDependent::QtPlatformDependent()
20 {
21  m_threadNamHash[ QThread::currentThread() ] = new QNetworkAccessManager();
22  m_ourNamSet.insert(QThread::currentThread());
23 }
24 
25 QtPlatformDependent::~QtPlatformDependent()
26 {
27  QThread *currThread = QThread::currentThread();
28  if (m_threadNamHash.contains(currThread)) {
29  if (m_ourNamSet.contains(currThread)) {
30  delete m_threadNamHash[ currThread ];
31  }
32  m_threadNamHash.remove(currThread);
33  m_ourNamSet.remove(currThread);
34  }
35 }
36 
37 void QtPlatformDependent::setNam(QNetworkAccessManager *nam)
38 {
39  if (!nam) {
40  return;
41  }
42 
43  QMutexLocker l(&m_accessMutex);
44  QThread *currThread = QThread::currentThread();
45  QNetworkAccessManager *oldNam = nullptr;
46  if (m_threadNamHash.contains(currThread) && m_ourNamSet.contains(currThread)) {
47  oldNam = m_threadNamHash[ currThread ];
48  }
49 
50  if (oldNam == nam) {
51  // If we're being passed back our own NAM, assume they want to
52  // ensure that we don't delete it out from under them
53  m_ourNamSet.remove(currThread);
54  return;
55  }
56 
57  m_threadNamHash[ currThread ] = nam;
58  m_ourNamSet.remove(currThread);
59 
60  if (oldNam) {
61  delete oldNam;
62  }
63 }
64 
65 QNetworkAccessManager *QtPlatformDependent::nam()
66 {
67  QMutexLocker l(&m_accessMutex);
68  QThread *currThread = QThread::currentThread();
69  if (!m_threadNamHash.contains(currThread)) {
71  m_threadNamHash[ currThread ] = newNam;
72  m_ourNamSet.insert(currThread);
73  return newNam;
74  }
75 
76  return m_threadNamHash[ currThread ];
77 }
78 
79 // TODO actually save and restore providers!
80 QList<QUrl> Attica::QtPlatformDependent::getDefaultProviderFiles() const
81 {
82  // Return the defaultiest default provider (since that's what our documentation says we'll do)
83  return QList<QUrl>{QUrl(QStringLiteral("https://autoconfig.kde.org/ocs/providers.xml"))};
84 }
85 
86 void QtPlatformDependent::addDefaultProviderFile(const QUrl &)
87 {
88  qWarning() << "attica-qt does not support default providers yet";
89 }
90 
91 void QtPlatformDependent::removeDefaultProviderFile(const QUrl &)
92 {
93 }
94 
95 void QtPlatformDependent::enableProvider(const QUrl &baseUrl, bool enabled) const
96 {
97  Q_UNUSED(baseUrl)
98  Q_UNUSED(enabled)
99  qWarning() << "attica-qt does not support disabling of providers yet";
100 }
101 
102 bool QtPlatformDependent::isEnabled(const QUrl &baseUrl) const
103 {
104  Q_UNUSED(baseUrl)
105  return true;
106 }
107 
108 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, const QByteArray &data)
109 {
110  return nam()->post(request, data);
111 }
112 
113 QNetworkReply *QtPlatformDependent::post(const QNetworkRequest &request, QIODevice *data)
114 {
115  return nam()->post(request, data);
116 }
117 
118 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, const QByteArray &data)
119 {
120  return nam()->put(request, data);
121 }
122 
123 QNetworkReply *QtPlatformDependent::put(const QNetworkRequest &request, QIODevice *data)
124 {
125  return nam()->put(request, data);
126 }
127 
128 QNetworkReply *QtPlatformDependent::get(const QNetworkRequest &request)
129 {
130  return nam()->get(request);
131 }
132 
133 QNetworkReply *QtPlatformDependent::deleteResource(const QNetworkRequest &request)
134 {
135  return nam()->deleteResource(request);
136 }
137 
138 bool QtPlatformDependent::hasCredentials(const QUrl &baseUrl) const
139 {
140  return m_passwords.contains(baseUrl.toString());
141 }
142 
143 bool QtPlatformDependent::saveCredentials(const QUrl &baseUrl, const QString &user, const QString &password)
144 {
145  m_passwords[baseUrl.toString()] = QPair<QString, QString> (user, password);
146  return true;
147 }
148 
149 bool QtPlatformDependent::loadCredentials(const QUrl &baseUrl, QString &user, QString &password)
150 {
151  if (!hasCredentials(baseUrl)) {
152  return false;
153  }
154  QPair<QString, QString> userPass = m_passwords.value(baseUrl.toString());
155  user = userPass.first;
156  password = userPass.second;
157  return true;
158 }
159 
160 bool Attica::QtPlatformDependent::askForCredentials(const QUrl &baseUrl, QString &user, QString &password)
161 {
162  Q_UNUSED(baseUrl)
163  Q_UNUSED(user)
164  Q_UNUSED(password)
165  return false;
166 }
167 
QNetworkReply * put(const QNetworkRequest &request, QIODevice *data)
bool isEnabled() const
Test if the provider is enabled by the settings.
Definition: provider.cpp:223
QString toString(QUrl::FormattingOptions options) const const
QNetworkReply * deleteResource(const QNetworkRequest &request)
QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
QThread * currentThread()
QNetworkReply * get(const QNetworkRequest &request)
bool hasCredentials() const
Test if the provider has user name/password available.
Definition: provider.cpp:263
QUrl baseUrl() const
A url that identifies this provider.
Definition: provider.cpp:213
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 03:59:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.