MauiKit File Browsing

WebDAVClient.cpp
1#include <QAuthenticator>
2#include <QByteArray>
3#include <QDebug>
4#include <QHttpMultiPart>
5#include <QList>
6#include <QMap>
7#include <QNetworkAccessManager>
8#include <QNetworkReply>
9#include <QRegularExpression>
10#include <QSslError>
11#include <string>
12
13#include "WebDAVClient.hpp"
14#include "utils/NetworkHelper.hpp"
15#include "utils/WebDAVReply.hpp"
16
17WebDAVClient::WebDAVClient(QString host, QString username, QString password)
18{
19 this->networkHelper = new NetworkHelper(host, username, password);
20 this->xmlHelper = new XMLHelper();
21
22 // TODO: Check for Timeout error in case of wrong host
23}
24
25WebDAVReply *WebDAVClient::listDir(QString path)
26{
27 return this->listDir(path, ListDepthEnum::Infinity);
28}
29
30WebDAVReply *WebDAVClient::listDir(QString path, ListDepthEnum depth)
31{
32 WebDAVReply *reply = new WebDAVReply();
33 QString depthVal;
35 QNetworkReply *listDirReply;
36
37 switch (depth) {
38 case ListDepthEnum::Zero:
39 depthVal = QStringLiteral("0");
40 break;
41
42 case ListDepthEnum::One:
43 depthVal = QStringLiteral("1");
44 break;
45
46 case ListDepthEnum::Two:
47 depthVal = QStringLiteral("2");
48 break;
49
50 case ListDepthEnum::Infinity:
51 depthVal = QStringLiteral("infinity");
52 break;
53
54 default:
55 break;
56 }
57
58 headers.insert(QStringLiteral("Depth"), depthVal);
59
60 listDirReply = this->networkHelper->makeRequest(QStringLiteral("PROPFIND"), path, headers);
61
62 connect(listDirReply, &QNetworkReply::finished, [=]() {
63 reply->sendListDirResponseSignal(listDirReply, this->xmlHelper->parseListDirResponse(this, listDirReply->readAll()));
64 });
66 this->errorReplyHandler(reply, err);
67 });
68
69 return reply;
70}
71
72WebDAVReply *WebDAVClient::downloadFrom(QString path)
73{
74 return this->downloadFrom(path, 0, -1);
75}
76
77WebDAVReply *WebDAVClient::downloadFrom(QString path, qint64 startByte, qint64 endByte)
78{
79 WebDAVReply *reply = new WebDAVReply();
80 QString rangeVal;
81 QTextStream stream(&rangeVal);
83 QNetworkReply *downloadReply;
84
85 stream << "bytes=" << startByte << "-" << endByte;
86
87 headers.insert(QStringLiteral("Range"), rangeVal);
88
89 downloadReply = this->networkHelper->makeRequest(QStringLiteral("GET"), path, headers);
90
91 connect(downloadReply, &QNetworkReply::finished, [=]() {
92 reply->sendDownloadResponseSignal(downloadReply);
93 });
94
95 connect(downloadReply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal) {
96 if (bytesTotal == -1) {
97 QString contentRange = QString::fromStdString(downloadReply->rawHeader("Content-Range").toStdString());
98 QRegularExpression re(QStringLiteral("bytes (\\d*)-(\\d*)/(\\d*)"));
99 QRegularExpressionMatch match = re.match(contentRange);
100 qint64 contentSize = match.captured(2).toInt() - match.captured(1).toInt();
101
102 reply->sendDownloadProgressResponseSignal(bytesReceived, contentSize);
103 } else {
104 reply->sendDownloadProgressResponseSignal(bytesReceived, bytesTotal);
105 }
106 });
108 this->errorReplyHandler(reply, err);
109 });
110
111 return reply;
112}
113
114WebDAVReply *WebDAVClient::uploadTo(QString path, QString filename, QIODevice *file)
115{
116 WebDAVReply *reply = new WebDAVReply();
118 QNetworkReply *uploadReply;
119
120 uploadReply = this->networkHelper->makePutRequest(path + QStringLiteral("/") + filename, headers, file);
121
122 connect(uploadReply, &QNetworkReply::finished, [=]() {
123 reply->sendUploadFinishedResponseSignal(uploadReply);
124 });
125
127 this->errorReplyHandler(reply, err);
128 });
129
130 return reply;
131}
132
133WebDAVReply *WebDAVClient::createDir(QString path, QString dirName)
134{
135 WebDAVReply *reply = new WebDAVReply();
137 QNetworkReply *createDirReply;
138
139 createDirReply = this->networkHelper->makeRequest(QStringLiteral("MKCOL"), path + QStringLiteral("/") + dirName, headers);
140
141 connect(createDirReply, &QNetworkReply::finished, [=]() {
142 reply->sendDirCreatedResponseSignal(createDirReply);
143 });
144
146 this->errorReplyHandler(reply, err);
147 });
148
149 return reply;
150}
151
152WebDAVReply *WebDAVClient::copy(QString source, QString destination)
153{
154 WebDAVReply *reply = new WebDAVReply();
156 QNetworkReply *copyReply;
157
158 headers.insert(QStringLiteral("Destination"), destination);
159
160 copyReply = this->networkHelper->makeRequest(QStringLiteral("COPY"), source, headers);
161
162 connect(copyReply, &QNetworkReply::finished, [=]() {
163 reply->sendCopyResponseSignal(copyReply);
164 });
165
167 this->errorReplyHandler(reply, err);
168 });
169
170 return reply;
171}
172
173WebDAVReply *WebDAVClient::move(QString source, QString destination, bool overwrite)
174{
175 WebDAVReply *reply = new WebDAVReply();
177 QNetworkReply *moveReply;
178 QString overwriteVal = overwrite ? QStringLiteral("T") : QStringLiteral("F");
179
180 headers.insert(QStringLiteral("Destination"), destination);
181 headers.insert(QStringLiteral("Overwrite"), overwriteVal);
182
183 moveReply = this->networkHelper->makeRequest(QStringLiteral("MOVE"), source, headers);
184
185 connect(moveReply, &QNetworkReply::finished, [=]() {
186 reply->sendMoveResponseSignal(moveReply);
187 });
188
190 this->errorReplyHandler(reply, err);
191 });
192
193 return reply;
194}
195
196WebDAVReply *WebDAVClient::remove(QString path)
197{
198 WebDAVReply *reply = new WebDAVReply();
200 QNetworkReply *removeReply;
201
202 removeReply = this->networkHelper->makeRequest(QStringLiteral("DELETE"), path, headers);
203
204 connect(removeReply, &QNetworkReply::finished, [=]() {
205 reply->sendRemoveResponseSignal(removeReply);
206 });
207
209 this->errorReplyHandler(reply, err);
210 });
211
212 return reply;
213}
214
215void WebDAVClient::errorReplyHandler(WebDAVReply *reply, QNetworkReply::NetworkError err)
216{
217 reply->sendError(err);
218}
219
220WebDAVClient::~WebDAVClient()
221{
222 this->networkHelper->deleteLater();
223 delete this->xmlHelper;
224}
Wraps the available actions for a remote item.
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
KGuiItem overwrite()
std::string toStdString() const const
QByteArray readAll()
iterator insert(const Key &key, const T &value)
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void errorOccurred(QNetworkReply::NetworkError code)
QByteArray rawHeader(const QByteArray &headerName) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString fromStdString(const std::string &str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.