Commit 54f1ae42 authored by Koen Martens's avatar Koen Martens

Pull out caldav discovery from CaldavClient class into CaldavDiscovery class

parent c5635609
......@@ -17,6 +17,7 @@ CONFIG += qmltypes
SOURCES += src/harbour-tasks.cpp \
src/caldavclient/caldavclient.cpp \
src/caldavclient/caldavdiscovery.cpp \
src/caldavclient/calendardetailprocessor.cpp \
src/caldavclient/calendarlistprocessor.cpp \
src/caldavclient/calendar.cpp \
......@@ -76,6 +77,7 @@ PKGCONFIG += sailfishsecrets
HEADERS += \
src/caldavclient/caldavclient.h \
src/caldavclient/caldavdiscovery.h \
src/caldavclient/calendardetailprocessor.h \
src/caldavclient/calendarlistprocessor.h \
src/caldavclient/calendar.h \
......
......@@ -13,19 +13,17 @@
CaldavClient::CaldavClient(QObject *parent) : QObject(parent)
{
connect(discoveryProcessor, &DiscoveryProcessor::discoveryRedirected,
this, &CaldavClient::discoveryRedirected);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryComplete,
connect(discoveryEngine, &CaldavDiscovery::discoveryComplete,
this, &CaldavClient::discoveryComplete);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryFailed,
connect(discoveryEngine, &CaldavDiscovery::discoveryFailed,
[this]() {
emit discoveryError(CaldavClient::Error::DiscoveryFailed);
emit todoItemDeleteError(CaldavClient::Error::DiscoveryFailed);
});
connect(discoveryProcessor, &DiscoveryProcessor::authenticationError,
connect(discoveryEngine, &CaldavDiscovery::authenticationError,
[this]() {
emit discoveryError(CaldavClient::Error::AuthenticationFailed);
});
connect(discoveryProcessor, &DiscoveryProcessor::error,
connect(discoveryEngine, &CaldavDiscovery::networkError,
[this](HttpClientHelpers::ResponseProcessor::NetworkError error) {
emit discoveryError(networkErrorToCaldavClientError(error));
});
......@@ -90,13 +88,7 @@ void CaldavClient::urlChanged()
this->setReady(false);
qDebug() << "URL CHANGED!";
discoveredUrl = QUrl(account->property("url").toString()).resolved(QUrl(".well-known/caldav"));
this->httpClient.sendPropfind(
discoveredUrl,
QStringLiteral(""),
1,
discoveryProcessor
);
discoveryEngine->startDiscovery(QUrl(account->property("url").toString()));
}
// slot
......@@ -273,31 +265,6 @@ void CaldavClient::todoItemFromProcessor(Todo *item)
emit todoItem(item);
}
void CaldavClient::discoveryRedirected(QString location)
{
qDebug() << "requesting discovery redirect: " << location;
discoveredUrl = QUrl(location);
this->httpClient.sendPropfind(
discoveredUrl,
QStringLiteral(""),
0,
discoveryProcessor
);
}
void CaldavClient::discoveryComplete()
{
QString location_str = discoveredUrl.toString();
qDebug() << "discovery completed, discovered url: " << location_str;
if(!location_str.endsWith('/')) {
location_str.append(QStringLiteral("/"));
}
this->urlBuilder.setBase(location_str);
this->setReady(true);
}
CaldavClient::Error CaldavClient::networkErrorToCaldavClientError(
HttpClientHelpers::ResponseProcessor::NetworkError networkError)
......@@ -309,3 +276,8 @@ CaldavClient::Error CaldavClient::networkErrorToCaldavClientError(
return CaldavClient::Error::Unspecified;
}
}
void CaldavClient::discoveryComplete()
{
this->setReady(true);
}
......@@ -7,7 +7,7 @@
#include "urlbuilder.h"
#include "calendar.h"
#include "todo.h"
#include "discoveryprocessor.h"
#include "caldavdiscovery.h"
class CaldavClient : public QObject
{
......@@ -74,13 +74,11 @@ private:
QObject *account = nullptr;
HttpClient httpClient;
UrlBuilder urlBuilder;
DiscoveryProcessor *discoveryProcessor = new DiscoveryProcessor(&urlBuilder);
QUrl discoveredUrl;
CaldavDiscovery *discoveryEngine = new CaldavDiscovery(&httpClient, &urlBuilder);
bool ready = false;
void loadCalendarDetails(QList<QUrl> url);
void discoveryComplete();
void discoveryRedirected(QString location);
private slots:
void userNameChanged();
......@@ -92,7 +90,7 @@ private slots:
void calendarDetails(Calendar *calendar, QList<QUrl> remainder);
void todoItemFromProcessor(Todo *item);
void discoveryComplete();
CaldavClient::Error networkErrorToCaldavClientError(
HttpClientHelpers::ResponseProcessor::NetworkError networkError);
......
#include "caldavdiscovery.h"
CaldavDiscovery::CaldavDiscovery(HttpClient *httpClient, UrlBuilder *urlBuilder, QObject *parent) : QObject(parent)
{
this->httpClient = httpClient;
this->urlBuilder = urlBuilder;
this->discoveryProcessor = new DiscoveryProcessor(urlBuilder);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryRedirected,
this, &CaldavDiscovery::redirected);
connect(discoveryProcessor, &DiscoveryProcessor::locationDiscovered,
this, &CaldavDiscovery::locationDiscovered);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryFailed,
[this]() {
emit discoveryFailed();
});
connect(discoveryProcessor, &DiscoveryProcessor::authenticationError,
[this]() {
emit authenticationError();
});
connect(discoveryProcessor, &DiscoveryProcessor::error,
[this](HttpClientHelpers::ResponseProcessor::NetworkError error) {
emit networkError(error);
});
}
void CaldavDiscovery::startDiscovery(QUrl url)
{
discoveredUrl = url.resolved(QUrl(".well-known/caldav"));
this->httpClient->sendPropfind(
discoveredUrl,
QStringLiteral(""),
1,
discoveryProcessor
);
}
void CaldavDiscovery::redirected(QString location)
{
qDebug() << "requesting discovery redirect: " << location;
discoveredUrl = QUrl(location);
this->httpClient->sendPropfind(
discoveredUrl,
QStringLiteral(""),
0,
discoveryProcessor
);
}
void CaldavDiscovery::locationDiscovered()
{
QString location_str = discoveredUrl.toString();
qDebug() << "location discovered: " << location_str;
if(!location_str.endsWith('/')) {
location_str.append(QStringLiteral("/"));
}
this->urlBuilder->setBase(location_str);
emit discoveryComplete();
}
#ifndef CALDAVDISCOVERY_H
#define CALDAVDISCOVERY_H
#include <QObject>
#include "httpclient.h"
#include "urlbuilder.h"
#include "discoveryprocessor.h"
class CaldavDiscovery : public QObject
{
Q_OBJECT
public:
explicit CaldavDiscovery(HttpClient *httpClient, UrlBuilder *urlBuilder, QObject *parent = nullptr);
void startDiscovery(QUrl url);
private:
enum DiscoveryState {
idle = 0,
location = 1,
principal = 2,
root = 3
};
DiscoveryState state = idle;
HttpClient *httpClient;
UrlBuilder *urlBuilder;
QUrl discoveredUrl;
DiscoveryProcessor *discoveryProcessor;
void locationDiscovered();
void redirected(QString location);
signals:
void discoveryComplete();
void discoveryFailed();
void authenticationError();
void networkError(HttpClientHelpers::ResponseProcessor::NetworkError error);
};
#endif // CALDAVDISCOVERY_H
......@@ -18,7 +18,7 @@ void DiscoveryProcessor::response(QByteArray data, int httpStatusCode, const QVa
emit discoveryRedirected(location_str);
} else {
qDebug() << "discovery complete, status " << httpStatusCode;
emit discoveryComplete();
emit locationDiscovered();
}
}
......
......@@ -17,7 +17,7 @@ public:
signals:
void discoveryRedirected(QString location);
void discoveryComplete();
void locationDiscovered();
void discoveryFailed();
void authenticationError();
void error(HttpClientHelpers::ResponseProcessor::NetworkError errorCode);
......
......@@ -24,6 +24,7 @@ SOURCES += \
main.cpp \
../app/src/util/uuidgenerator.cpp \
../app/src/caldavclient/caldavclient.cpp \
../app/src/caldavclient/caldavdiscovery.cpp \
../app/src/caldavclient/discoveryprocessor.cpp \
../app/src/caldavclient/calendardetailprocessor.cpp \
../app/src/caldavclient/calendarlistprocessor.cpp \
......@@ -40,6 +41,7 @@ SOURCES += \
HEADERS += \
../app/src/util/uuidgenerator.h \
../app/src/caldavclient/caldavclient.h \
../app/src/caldavclient/caldavdiscovery.h \
../app/src/caldavclient/discoveryprocessor.h \
../app/src/caldavclient/calendardetailprocessor.h \
../app/src/caldavclient/calendarlistprocessor.h \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment