Changeset View
Changeset View
Standalone View
Standalone View
resources/openxchange/oxa/connectiontestjob.cpp
Show All 16 Lines | 1 | /* | |||
---|---|---|---|---|---|
17 | along with this library; see the file COPYING.LIB. If not, write to the | 17 | along with this library; see the file COPYING.LIB. If not, write to the | ||
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
19 | 02110-1301, USA. | 19 | 02110-1301, USA. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "connectiontestjob.h" | 22 | #include "connectiontestjob.h" | ||
23 | 23 | | |||
24 | #include <kio/job.h> | 24 | #include <kio/job.h> | ||
25 | | ||||
26 | #include <QJsonDocument> | ||||
27 | #include <QJsonObject> | ||||
25 | #include <QUrl> | 28 | #include <QUrl> | ||
26 | 29 | | |||
27 | using namespace OXA; | 30 | using namespace OXA; | ||
28 | 31 | | |||
29 | ConnectionTestJob::ConnectionTestJob(const QString &url, const QString &user, const QString &password, QObject *parent) | 32 | ConnectionTestJob::ConnectionTestJob(const QString &url, const QString &user, const QString &password, QObject *parent) | ||
30 | : KJob(parent), mUrl(url), mUser(user), mPassword(password) | 33 | : KJob(parent), mUrl(url), mUser(user), mPassword(password) | ||
31 | { | 34 | { | ||
32 | } | 35 | } | ||
Show All 16 Lines | 50 | if (job->error()) { | |||
49 | setErrorText(job->errorText()); | 52 | setErrorText(job->errorText()); | ||
50 | emitResult(); | 53 | emitResult(); | ||
51 | return; | 54 | return; | ||
52 | } | 55 | } | ||
53 | 56 | | |||
54 | KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob *>(job); | 57 | KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob *>(job); | ||
55 | Q_ASSERT(transferJob); | 58 | Q_ASSERT(transferJob); | ||
56 | 59 | | |||
57 | const QString data = QString::fromUtf8(transferJob->data()); | 60 | const QJsonObject data = QJsonDocument::fromJson(transferJob->data()).object(); | ||
58 | 61 | | |||
59 | // on success data contains something like: {"session":"e530578bca504aa89738fadde9e44b3d","random":"ac9090d2cc284fed926fa3c7e316c43b"} | 62 | // on success data contains something like: {"session":"e530578bca504aa89738fadde9e44b3d","random":"ac9090d2cc284fed926fa3c7e316c43b"} | ||
60 | // on failure data contains something like: {"category":1,"error_params":[],"error":"Invalid credentials.","error_id":"-1529642166-37","code":"LGI-0006"} | 63 | // on failure data contains something like: {"category":1,"error_params":[],"error":"Invalid credentials.","error_id":"-1529642166-37","code":"LGI-0006"} | ||
61 | const int index = data.indexOf(QLatin1String("\"session\":\"")); | 64 | if (data.contains(QStringLiteral("error"))) { | ||
62 | if (index == -1) { // error case | 65 | const QString errorText = data[QStringLiteral("error")].toString(); | ||
63 | const int errorIndex = data.indexOf(QLatin1String("\"error\":\"")); | | |||
64 | const QString errorText = data.mid(errorIndex + 9, data.indexOf(QLatin1Char('"'), errorIndex + 10) - errorIndex - 9); | | |||
65 | 66 | | |||
66 | setError(UserDefinedError); | 67 | setError(UserDefinedError); | ||
67 | setErrorText(errorText); | 68 | setErrorText(errorText); | ||
68 | emitResult(); | 69 | emitResult(); | ||
69 | return; | 70 | return; | ||
70 | } else { // success case | 71 | } else { // success case | ||
71 | const QString sessionId = data.mid(index + 11, 33); // I assume here the session id is always 32 characters long :} | 72 | const QString sessionId = data[QStringLiteral("session")].toString(); | ||
72 | 73 | | |||
73 | // logout correctly... | 74 | // logout correctly... | ||
74 | const QUrl url(mUrl + QStringLiteral("/ajax/login?action=logout&session=%1").arg(sessionId)); | 75 | const QUrl url(mUrl + QStringLiteral("/ajax/login?action=logout&session=%1").arg(sessionId)); | ||
75 | KIO::storedGet(url, KIO::Reload, KIO::HideProgressInfo); | 76 | KIO::storedGet(url, KIO::Reload, KIO::HideProgressInfo); | ||
76 | } | 77 | } | ||
77 | 78 | | |||
78 | emitResult(); | 79 | emitResult(); | ||
79 | } | 80 | } | ||
80 | 81 | |