File: owncloudcontroller.cpp

package info (click to toggle)
kaccounts-providers 4%3A20.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 868 kB
  • sloc: cpp: 676; makefile: 3; sh: 3
file content (225 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 *  SPDX-FileCopyrightText: 2015 Martin Klapetek <mklapetek@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "owncloudcontroller.h"

#include <KIO/Job>
#include <KIO/DavJob>
#include <kio/global.h>
#include <KLocalizedString>

#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>

#include "../cloudurls.h"

OwncloudController::OwncloudController(QObject *parent)
    : QObject(parent),
      m_errorMessage(QString()),
      m_isWorking(false)
{
}

OwncloudController::~OwncloudController()
{
}

void OwncloudController::checkServer(const QString &username, const QString &password, const QString &path)
{
    m_errorMessage.clear();
    Q_EMIT errorMessageChanged();

    m_username = username;
    m_password = password;

    checkServer(createStatusUrl(path));
}

void OwncloudController::checkServer(const QUrl &url)
{
    qDebug() << "Checking for ownCloud instance at" << url;
    setWorking(true);
    KIO::TransferJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
    job->setUiDelegate(0);
    connect(job, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(dataReceived(KIO::Job*,QByteArray)));
    connect(job, SIGNAL(finished(KJob*)), this, SLOT(fileChecked(KJob*)));
}

void OwncloudController::figureOutServer(const QUrl& url)
{
    if (/*url == QLatin1String("/") ||*/ url.isEmpty()) {
        serverCheckResult(false);
        return;
    }

    m_json.clear();

    qDebug() << "Received url to figure out:" << url;
    // This needs 2x up cause first it just removes the status.php
    // and only the second call actually moves up
    QUrl urlUp = KIO::upUrl(KIO::upUrl(url));
    urlUp.setPath(urlUp.path() + '/' + "status.php");

    if (urlUp != url) {
        checkServer(urlUp.adjusted(QUrl::NormalizePathSegments));
    } else {
        serverCheckResult(false);
    }
}

void OwncloudController::dataReceived(KIO::Job *job, const QByteArray &data)
{
    Q_UNUSED(job);
    m_json.append(data);
}

void OwncloudController::fileChecked(KJob* job)
{
    KIO::TransferJob *kJob = qobject_cast<KIO::TransferJob *>(job);
    if (kJob->error()) {
        qDebug() << job->errorString();
        qDebug() << job->errorText();
        figureOutServer(kJob->url());
        return;
    }

    QJsonDocument parser = QJsonDocument::fromJson(m_json);
    QJsonObject map = parser.object();
    if (!map.contains("version")) {
        figureOutServer(kJob->url());
        qDebug() << "No json";
        return;
    }

    m_server = kJob->url().adjusted(QUrl::RemoveFilename).toString();
    qDebug() << "ownCloud appears to be running at the specified URL";
    serverCheckResult(true);
}

void OwncloudController::setWorking(bool start)
{
    if (start == m_isWorking) {
        return;
    }

    m_isWorking = start;
    Q_EMIT isWorkingChanged();
}

void OwncloudController::serverCheckResult(bool result)
{
    if (!result) {
        m_errorMessage = i18n("Unable to connect to ownCloud at the given server URL. Please check the server URL.");
        setWorking(false);
    } else {
        m_errorMessage.clear();

        qDebug() << "Server URL ok, checking auth...";

        m_json.clear();

        QUrl url(m_server);

        url.setUserName(m_username);
        url.setPassword(m_password);

        if (!url.path().endsWith(QLatin1String("/"))) {
            url.setPath(url.path() + '/');
        }

        url.setPath(url.path() + "remote.php/webdav");

        // Send a basic PROPFIND command to test access
        const QString requestStr = QStringLiteral(
            "<d:propfind xmlns:d=\"DAV:\">"
            "<d:prop>"
            "<d:current-user-principal />"
            "</d:prop>"
            "</d:propfind>");

        KIO::DavJob *job = KIO::davPropFind(url, QDomDocument(requestStr), "1", KIO::HideProgressInfo);
        connect(job, SIGNAL(finished(KJob*)), this, SLOT(authCheckResult(KJob*)));
        connect(job, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(dataReceived(KIO::Job*,QByteArray)));

        QVariantMap metadata{{"cookies","none"}, {"no-cache",true}};

        job->setMetaData(metadata);
        job->setUiDelegate(0);
        job->start();
    }

    Q_EMIT errorMessageChanged();

}

void OwncloudController::authCheckResult(KJob *job)
{
    if (job->error()) {
        qDebug() << job->errorString();
        qDebug() << job->errorText();
    }

    KIO::DavJob *kJob = qobject_cast<KIO::DavJob*>(job);
    qDebug() << "Auth job finished, received error page:" << kJob->isErrorPage();

    if (kJob->isErrorPage()) {
        m_errorMessage = i18n("Unable to authenticate using the provided username and password");
    } else {
        m_errorMessage.clear();
    }

    Q_EMIT errorMessageChanged();

    if (!kJob->isErrorPage()) {
        m_state = Services;
        Q_EMIT stateChanged();
    }

    setWorking(false);
}

bool OwncloudController::isWorking()
{
    return m_isWorking;
}

QString OwncloudController::errorMessage() const
{
    return m_errorMessage;
}

void OwncloudController::finish(const QStringList &disabledServices)
{
    QVariantMap data;
    data.insert("server", m_server);

    for (const QString &service : disabledServices) {
        data.insert("__service/" + service, false);
    }

    QUrl carddavUrl(m_server);
    carddavUrl.setPath(carddavUrl.path() + QString("/remote.php/carddav/addressbooks/%1").arg(m_username));

    data.insert("carddavUrl", carddavUrl);
    data.insert("dav/host", carddavUrl.host());
    data.insert("dav/storagePath", QStringLiteral("/remote.php/webdav"));

    Q_EMIT wizardFinished(m_username, m_password, data);
}

void OwncloudController::cancel()
{
    Q_EMIT wizardCancelled();
}

QVariantList OwncloudController::availableServices() const
{
    // TODO Find a way to not hardcode this
    return {
        QVariant::fromValue(Service{QStringLiteral("owncloud-storage"), i18n("Storage"), i18n("Storage integration")})
    };
}