Commit 29e4316c authored by Koen Martens's avatar Koen Martens

Discover principal.

parent 54f1ae42
......@@ -101,7 +101,7 @@ void CaldavClient::ignoreSslErrorsChanged()
// invokable
void CaldavClient::loadCalendars()
{
QUrl url = urlBuilder.build(account->property("userName").toString().append("/"));
QUrl url = urlBuilder.build(QStringLiteral("calendars/").append(account->property("userName").toString().append("/")));
CalendarListProcessor *processor = new CalendarListProcessor(&urlBuilder);
connect(processor, &CalendarListProcessor::calendarList,
......
#include <QDomDocument>
#include "xml.h"
#include "caldavdiscovery.h"
CaldavDiscovery::CaldavDiscovery(HttpClient *httpClient, UrlBuilder *urlBuilder, QObject *parent) : QObject(parent)
......@@ -8,8 +10,8 @@ CaldavDiscovery::CaldavDiscovery(HttpClient *httpClient, UrlBuilder *urlBuilder,
connect(discoveryProcessor, &DiscoveryProcessor::discoveryRedirected,
this, &CaldavDiscovery::redirected);
connect(discoveryProcessor, &DiscoveryProcessor::locationDiscovered,
this, &CaldavDiscovery::locationDiscovered);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryContentLoaded,
this, &CaldavDiscovery::contentLoaded);
connect(discoveryProcessor, &DiscoveryProcessor::discoveryFailed,
[this]() {
emit discoveryFailed();
......@@ -26,6 +28,7 @@ CaldavDiscovery::CaldavDiscovery(HttpClient *httpClient, UrlBuilder *urlBuilder,
void CaldavDiscovery::startDiscovery(QUrl url)
{
state = location;
discoveredUrl = url.resolved(QUrl(".well-known/caldav"));
this->httpClient->sendPropfind(
discoveredUrl,
......@@ -35,10 +38,14 @@ void CaldavDiscovery::startDiscovery(QUrl url)
);
}
void CaldavDiscovery::redirected(QString location)
void CaldavDiscovery::redirected(QString locationUrl)
{
if(state!=location) {
qDebug() << "Unexpected redirect in state " << state << ", aborting discovery";
emit discoveryFailed();
}
qDebug() << "requesting discovery redirect: " << location;
discoveredUrl = QUrl(location);
discoveredUrl = QUrl(locationUrl);
this->httpClient->sendPropfind(
discoveredUrl,
QStringLiteral(""),
......@@ -47,6 +54,23 @@ void CaldavDiscovery::redirected(QString location)
);
}
void CaldavDiscovery::contentLoaded(QByteArray data)
{
qDebug() << "Content received, state = " << state;
switch(state) {
case location:
locationDiscovered();
break;
case principal:
parsePrincipal(data);
break;
default:
qDebug() << "Invalid state " << state << ", aborting discovery";
emit discoveryFailed();
break;
}
}
void CaldavDiscovery::locationDiscovered()
{
QString location_str = discoveredUrl.toString();
......@@ -59,5 +83,37 @@ void CaldavDiscovery::locationDiscovered()
this->urlBuilder->setBase(location_str);
state = principal;
this->httpClient->sendPropfind(
this->urlBuilder->build(""),
QStringLiteral("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
"<A:propfind xmlns:A=\"DAV:\">" \
"<A:prop><A:current-user-principal/><A:resourcetype/></A:prop>" \
"</A:propfind>"),
1,
discoveryProcessor
);
}
void CaldavDiscovery::parsePrincipal(QByteArray &data)
{
QDomDocument domDocument;
qDebug() << "Principal body: " << data;
if (!domDocument.setContent(data, true)) {
qWarning() << "failed to parse calendar list xml";
} else {
QDomNodeList children = domDocument.elementsByTagName("multistatus");
// qDebug() << "firts node: " << domDocument.childNodes().at(0).nodeName();
// qDebug() << "firts node: " << domDocument.childNodes().at(1).nodeName();
// qDebug() << "firts node: " << response.firstChild().nodeName();
QUrl principal = XML::getNodeValueByPath(children.item(0), "d:multistatus/d:response/d:propstat/d:prop/d:current-user-principal/d:href");
qDebug() << "Principal: " << principal;
}
state = idle;
emit discoveryComplete();
}
......@@ -30,6 +30,9 @@ private:
DiscoveryProcessor *discoveryProcessor;
void locationDiscovered();
void parsePrincipal(QByteArray &data);
private slots:
void contentLoaded(QByteArray data);
void redirected(QString location);
signals:
void discoveryComplete();
......
......@@ -18,7 +18,7 @@ void DiscoveryProcessor::response(QByteArray data, int httpStatusCode, const QVa
emit discoveryRedirected(location_str);
} else {
qDebug() << "discovery complete, status " << httpStatusCode;
emit locationDiscovered();
emit discoveryContentLoaded(data);
}
}
......
......@@ -17,7 +17,7 @@ public:
signals:
void discoveryRedirected(QString location);
void locationDiscovered();
void discoveryContentLoaded(QByteArray data);
void discoveryFailed();
void authenticationError();
void error(HttpClientHelpers::ResponseProcessor::NetworkError errorCode);
......
......@@ -7,7 +7,7 @@ UrlBuilder::UrlBuilder()
void UrlBuilder::setBase(QUrl base)
{
this->base = base.resolved(QUrl("calendars/")); //base.resolved(QUrl("foo/remote.php/dav/calendars/"));
this->base = base; //base.resolved(QUrl("calendars/")); //base.resolved(QUrl("foo/remote.php/dav/calendars/"));
}
QUrl UrlBuilder::build(QUrl const &url)
......
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