Cleaned API C++ code. Small changes in LoginPage.

This commit is contained in:
Scharel Clemens 2020-01-15 23:55:11 +01:00
parent 2be8a2675d
commit 9cb7237e81
6 changed files with 239 additions and 177 deletions

View file

@ -36,6 +36,7 @@ Dialog {
if (addingNew) appSettings.removeAccount(accountId) if (addingNew) appSettings.removeAccount(accountId)
notesApi.host = account.value("server", "", String) notesApi.host = account.value("server", "", String)
} }
onDone: notesApi.abortFlowV2Login()
Connections { Connections {
target: notesApi target: notesApi
@ -170,23 +171,20 @@ Dialog {
id: loginButton id: loginButton
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
property bool pushed: false property bool pushed: false
text: qsTr("Login") text: notesApi.loginBusy ? qsTr("Abort") : qsTr("Login")
enabled: !pushed onClicked: notesApi.loginBusy ? notesApi.abortFlowV2Login() : notesApi.initiateFlowV2Login()
onClicked: {
pushed = true
loginTimeout.start()
notesApi.initiateFlowV2Login()
}
BusyIndicator {
id: loginBusyIndicator
anchors.centerIn: parent
running: loginButton.pushed
}
Timer {
id: loginTimeout
interval: 60000
onTriggered: loginButton.pushed = false
} }
ProgressBar {
id: loginProgressBar
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width
highlighted: notesApi.loginBusy
indeterminate: notesApi.loginUrl.toString() !== ""
label: indeterminate ? qsTr("Follow the login procedure") : ""
//anchors.verticalCenter: loginButton.verticalCenter
//anchors.left: loginButton.right
//anchors.leftMargin: Theme.paddingMedium
//running: notesApi.loginBusy
} }
SectionHeader { SectionHeader {

View file

@ -4,11 +4,12 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
NotesApi::NotesApi(QObject *parent) : QObject(parent) NotesApi::NotesApi(const QString statusEndpoint, const QString loginEndpoint, const QString notesEndpoint, QObject *parent)
: QObject(parent), m_statusEndpoint(statusEndpoint), m_loginEndpoint(loginEndpoint), m_notesEndpoint(notesEndpoint)
{ {
m_loginPollTimer.setInterval(1000); // TODO verify connections (also in destructor)
m_loginPollTimer.setInterval(5000);
connect(&m_loginPollTimer, SIGNAL(timeout()), this, SLOT(pollLoginUrl())); connect(&m_loginPollTimer, SIGNAL(timeout()), this, SLOT(pollLoginUrl()));
m_online = m_manager.networkAccessible() == QNetworkAccessManager::Accessible;
mp_model = new NotesModel(this); mp_model = new NotesModel(this);
mp_modelProxy = new NotesProxyModel(this); mp_modelProxy = new NotesProxyModel(this);
mp_modelProxy->setSourceModel(mp_model); mp_modelProxy->setSourceModel(mp_model);
@ -17,14 +18,19 @@ NotesApi::NotesApi(QObject *parent) : QObject(parent)
mp_modelProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); mp_modelProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
mp_modelProxy->setFilterRole(NotesModel::ContentRole); mp_modelProxy->setFilterRole(NotesModel::ContentRole);
connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(verifyUrl(QUrl))); connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(verifyUrl(QUrl)));
connect(this, SIGNAL(statusBusyChanged(bool)), this, SIGNAL(busyChanged(bool)));
connect(this, SIGNAL(loginBusyChanged(bool)), this, SIGNAL(busyChanged(bool)));
connect(this, SIGNAL(notesBusyChanged(bool)), this, SIGNAL(busyChanged(bool)));
connect(&m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(requireAuthentication(QNetworkReply*,QAuthenticator*))); connect(&m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(requireAuthentication(QNetworkReply*,QAuthenticator*)));
connect(&m_manager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility))); connect(&m_manager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>))); connect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>)));
m_request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); m_request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
m_request.setHeader(QNetworkRequest::UserAgentHeader, QGuiApplication::applicationDisplayName() + " " + QGuiApplication::applicationVersion() + " - " + QSysInfo::machineHostName()); m_request.setHeader(QNetworkRequest::UserAgentHeader, QGuiApplication::applicationDisplayName() + " " + QGuiApplication::applicationVersion() + " - " + QSysInfo::machineHostName());
m_request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded").toUtf8());
m_request.setRawHeader("OCS-APIREQUEST", "true"); m_request.setRawHeader("OCS-APIREQUEST", "true");
m_request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/json").toUtf8()); m_authenticatedRequest = m_request;
m_authenticatedRequest.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/json").toUtf8());
connect(mp_model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(saveToFile(QModelIndex,QModelIndex,QVector<int>))); connect(mp_model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(saveToFile(QModelIndex,QModelIndex,QVector<int>)));
} }
@ -43,34 +49,24 @@ NotesApi::~NotesApi() {
} }
void NotesApi::setSslVerify(bool verify) { void NotesApi::setSslVerify(bool verify) {
if (verify != (m_request.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer)) { if (verify != (m_authenticatedRequest.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer)) {
m_request.sslConfiguration().setPeerVerifyMode(verify ? QSslSocket::VerifyPeer : QSslSocket::VerifyNone); m_authenticatedRequest.sslConfiguration().setPeerVerifyMode(verify ? QSslSocket::VerifyPeer : QSslSocket::VerifyNone);
emit sslVerifyChanged(verify); emit sslVerifyChanged(verify);
} }
} }
void NotesApi::setUrl(QUrl url) { void NotesApi::setUrl(QUrl url) {
if (url != m_url) { if (url != m_url) {
QUrl oldUrl = m_url;
QString oldServer = server(); QString oldServer = server();
m_url = url; setScheme(url.scheme());
setHost(url.host());
setPort(url.port());
setUsername(url.userName());
setPassword(url.password());
setPath(url.path());
emit urlChanged(m_url); emit urlChanged(m_url);
if (server() != oldServer) if (server() != oldServer)
emit serverChanged(server()); emit serverChanged(server());
if (m_url.scheme() != oldUrl.scheme())
emit schemeChanged(m_url.scheme());
if (m_url.host() != oldUrl.host())
emit hostChanged(m_url.host());
if (m_url.port() != oldUrl.port())
emit portChanged(m_url.port());
if (m_url.userName() != oldUrl.userName())
emit usernameChanged(m_url.userName());
if (m_url.password() != oldUrl.password())
emit passwordChanged(m_url.password());
if (m_url.path() != oldUrl.path())
emit pathChanged(m_url.path());
if (m_url.isValid())
qDebug() << "API URL:" << m_url.toDisplayString();
} }
} }
@ -95,139 +91,135 @@ void NotesApi::setServer(QString serverUrl) {
} }
void NotesApi::setScheme(QString scheme) { void NotesApi::setScheme(QString scheme) {
if (scheme == "http" || scheme == "https") { if (scheme != m_url.scheme() && (scheme == "http" || scheme == "https")) {
QUrl url = m_url; m_url.setScheme(scheme);
url.setScheme(scheme); emit schemeChanged(m_url.scheme());
setUrl(url); emit urlChanged(m_url);
} }
} }
void NotesApi::setHost(QString host) { void NotesApi::setHost(QString host) {
if (!host.isEmpty()) { if (host != m_url.host()) {
QUrl url = m_url; m_url.setHost(host);
url.setHost(host); emit hostChanged(m_url.host());
setUrl(url); emit urlChanged(m_url);
} }
} }
void NotesApi::setPort(int port) { void NotesApi::setPort(int port) {
if (port >= 1 && port <= 65535) { if (port != m_url.port() && port >= 1 && port <= 65535) {
QUrl url = m_url; m_url.setPort(port);
url.setPort(port); emit portChanged(m_url.port());
setUrl(url); emit urlChanged(m_url);
} }
} }
void NotesApi::setUsername(QString user) { void NotesApi::setUsername(QString username) {
if (!user.isEmpty()) { if (username != m_url.userName()) {
QUrl url = m_url; m_url.setUserName(username);
url.setUserName(user); QString concatenated = username + ":" + password();
QString concatenated = user + ":" + password();
QByteArray data = concatenated.toLocal8Bit().toBase64(); QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data; QString headerData = "Basic " + data;
m_request.setRawHeader("Authorization", headerData.toLocal8Bit()); m_authenticatedRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
setUrl(url); emit usernameChanged(m_url.userName());
emit urlChanged(m_url);
} }
} }
void NotesApi::setPassword(QString password) { void NotesApi::setPassword(QString password) {
if (!password.isEmpty()) { if (password != m_url.password()) {
QUrl url = m_url; m_url.setPassword(password);
url.setPassword(password);
QString concatenated = username() + ":" + password; QString concatenated = username() + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64(); QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data; QString headerData = "Basic " + data;
m_request.setRawHeader("Authorization", headerData.toLocal8Bit()); m_authenticatedRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
setUrl(url); emit passwordChanged(m_url.password());
emit urlChanged(m_url);
} }
} }
void NotesApi::setPath(QString path) { void NotesApi::setPath(QString path) {
if (!path.isEmpty()) { if (path != m_url.path()) {
QUrl url = m_url; m_url.setPath(path);
url.setPath(path); emit pathChanged(m_url.path());
setUrl(url); emit urlChanged(m_url);
} }
} }
void NotesApi::setDataFile(QString dataFile) { void NotesApi::setDataFile(QString dataFile) {
if (dataFile != m_jsonFile.fileName()) { if (dataFile != m_jsonFile.fileName()) {
m_jsonFile.close(); m_jsonFile.close();
if (!dataFile.isEmpty())
m_jsonFile.setFileName(dataFile); m_jsonFile.setFileName(dataFile);
emit dataFileChanged(m_jsonFile.fileName()); emit dataFileChanged(m_jsonFile.fileName());
//qDebug() << m_jsonFile.fileName();
} }
} }
bool NotesApi::busy() const {
bool busy = false;
QVector<QNetworkReply*> replies;
replies << m_replies << m_status_replies << m_login_replies << m_poll_replies;
for (int i = 0; i < replies.size(); ++i) {
busy |= replies[i]->isRunning();
}
return busy;
}
void NotesApi::getStatus() { void NotesApi::getStatus() {
QUrl url = server(); QUrl url = apiEndpointUrl(m_statusEndpoint);
QNetworkRequest request;
url.setPath(url.path() + "/status.php");
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "POST" << url.toDisplayString(); qDebug() << "POST" << url.toDisplayString();
request.setUrl(url); m_request.setUrl(url);
m_status_replies << m_manager.post(request, QByteArray()); m_statusReplies << m_manager.post(m_request, QByteArray());
emit busyChanged(busy()); emit statusBusyChanged(true);
} }
} }
void NotesApi::initiateFlowV2Login() { void NotesApi::initiateFlowV2Login() {
QUrl url = server(); QUrl url = apiEndpointUrl(m_loginEndpoint);
url.setPath(url.path() + "/index.php/login/v2");
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "POST" << url.toDisplayString(); qDebug() << "POST" << url.toDisplayString();
m_request.setUrl(url); m_request.setUrl(url);
m_login_replies << m_manager.post(m_request, QByteArray()); m_loginReplies << m_manager.post(m_request, QByteArray());
m_loginPollTimer.start(); m_loginPollTimer.start();
emit busyChanged(busy()); emit loginBusyChanged(true);
}
}
void NotesApi::abortFlowV2Login() {
// TODO crashes!
m_loginPollTimer.stop();
m_loginUrl.clear();
emit loginUrlChanged(m_loginUrl);
m_pollUrl.clear();
m_pollToken.clear();
for (int i = 0; i < m_loginReplies.size(); ++i) {
m_loginReplies[i]->abort();
}
for (int i = 0; i < m_pollReplies.size(); ++i) {
m_pollReplies[i]->abort();
} }
} }
void NotesApi::pollLoginUrl() { void NotesApi::pollLoginUrl() {
//QNetworkRequest request;
//request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/x-www-form-urlencoded").toUtf8());
if (m_pollUrl.isValid() && !m_pollUrl.scheme().isEmpty() && !m_pollUrl.host().isEmpty() && !m_pollToken.isEmpty()) { if (m_pollUrl.isValid() && !m_pollUrl.scheme().isEmpty() && !m_pollUrl.host().isEmpty() && !m_pollToken.isEmpty()) {
//qDebug() << "POST" << m_pollUrl.toDisplayString(); qDebug() << "POST" << m_pollUrl.toDisplayString();
m_request.setUrl(m_pollUrl); m_request.setUrl(m_pollUrl);
m_poll_replies << m_manager.post(m_request, QByteArray("token=").append(m_pollToken)); m_pollReplies << m_manager.post(m_request, QByteArray("token=").append(m_pollToken));
emit busyChanged(busy()); emit loginBusyChanged(true);
} }
} }
void NotesApi::getAllNotes(QStringList excludeFields) { void NotesApi::getAllNotes(QStringList excludeFields) {
QUrl url = server(); QUrl url = apiEndpointUrl(m_notesEndpoint + "/notes");
url.setPath(url.path() + "/index.php/apps/notes/api/v0.2" + "/notes");
if (!excludeFields.isEmpty()) if (!excludeFields.isEmpty())
url.setQuery(QString("exclude=").append(excludeFields.join(","))); url.setQuery(QString("exclude=").append(excludeFields.join(",")));
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "GET" << url.toDisplayString(); qDebug() << "GET" << url.toDisplayString();
m_request.setUrl(url); m_authenticatedRequest.setUrl(url);
m_replies << m_manager.get(m_request); m_notesReplies << m_manager.get(m_authenticatedRequest);
emit busyChanged(busy()); emit notesBusyChanged(true);
} }
} }
void NotesApi::getNote(double noteId, QStringList excludeFields) { void NotesApi::getNote(double noteId, QStringList excludeFields) {
QUrl url = m_url; QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/notes/%1").arg(noteId));
url.setPath(url.path() + "/index.php/apps/notes/api/v0.2" + QString("/notes/%1").arg(noteId));
if (!excludeFields.isEmpty()) if (!excludeFields.isEmpty())
url.setQuery(QString("exclude=").append(excludeFields.join(","))); url.setQuery(QString("exclude=").append(excludeFields.join(",")));
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "GET" << url.toDisplayString(); qDebug() << "GET" << url.toDisplayString();
m_request.setUrl(url); m_authenticatedRequest.setUrl(url);
m_replies << m_manager.get(m_request); m_notesReplies << m_manager.get(m_authenticatedRequest);
emit busyChanged(busy()); emit notesBusyChanged(true);
} }
} }
@ -237,13 +229,12 @@ void NotesApi::createNote(QVariantMap fields) {
mp_model->insertNote(note); mp_model->insertNote(note);
// Create note via the API // Create note via the API
QUrl url = m_url; QUrl url = apiEndpointUrl(m_notesEndpoint + "/notes");
url.setPath(url.path() + "/index.php/apps/notes/api/v0.2" + "/notes");
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "POST" << url.toDisplayString(); qDebug() << "POST" << url.toDisplayString();
m_request.setUrl(url); m_authenticatedRequest.setUrl(url);
m_replies << m_manager.post(m_request, note.toJsonDocument().toJson()); m_notesReplies << m_manager.post(m_authenticatedRequest, note.toJsonDocument().toJson());
emit busyChanged(busy()); emit notesBusyChanged(true);
} }
} }
@ -253,13 +244,12 @@ void NotesApi::updateNote(double noteId, QVariantMap fields) {
mp_model->insertNote(note); mp_model->insertNote(note);
// Update note on the server // Update note on the server
QUrl url = m_url; QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/notes/%1").arg(noteId));
url.setPath(url.path() + "/index.php/apps/notes/api/v0.2" + QString("/notes/%1").arg(noteId));
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "PUT" << url.toDisplayString(); qDebug() << "PUT" << url.toDisplayString();
m_request.setUrl(url); m_authenticatedRequest.setUrl(url);
m_replies << m_manager.put(m_request, note.toJsonDocument().toJson()); m_notesReplies << m_manager.put(m_authenticatedRequest, note.toJsonDocument().toJson());
emit busyChanged(busy()); emit notesBusyChanged(true);
} }
} }
@ -268,13 +258,12 @@ void NotesApi::deleteNote(double noteId) {
mp_model->removeNote(noteId); mp_model->removeNote(noteId);
// Remove note from the server // Remove note from the server
QUrl url = m_url; QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/notes/%1").arg(noteId));
url.setPath(url.path() + "/index.php/apps/notes/api/v0.2" + QString("/notes/%1").arg(noteId));
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
qDebug() << "DELETE" << url.toDisplayString(); qDebug() << "DELETE" << url.toDisplayString();
m_request.setUrl(url); m_authenticatedRequest.setUrl(url);
m_replies << m_manager.deleteResource(m_request); m_notesReplies << m_manager.deleteResource(m_authenticatedRequest);
emit busyChanged(busy()); emit notesBusyChanged(true);
} }
mp_model->removeNote(noteId); mp_model->removeNote(noteId);
} }
@ -296,61 +285,80 @@ void NotesApi::requireAuthentication(QNetworkReply *reply, QAuthenticator *authe
} }
void NotesApi::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) { void NotesApi::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) {
m_online = accessible == QNetworkAccessManager::Accessible; emit networkAccessibleChanged(accessible == QNetworkAccessManager::Accessible);
emit networkAccessibleChanged(m_online);
} }
void NotesApi::replyFinished(QNetworkReply *reply) { void NotesApi::replyFinished(QNetworkReply *reply) {
//qDebug() << reply->error() << reply->errorString(); //qDebug() << reply->error() << reply->errorString();
bool deleteReply = false;
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
emit error(NoError); emit error(NoError);
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
QJsonDocument json = QJsonDocument::fromJson(data); QJsonDocument json = QJsonDocument::fromJson(data);
if (m_login_replies.contains(reply)) { /*if (reply->url().toString().contains(m_loginEndpoint)) {
qDebug() << "Login reply";
}
else if (reply->url() == m_pollUrl) {
qDebug() << "Poll reply";
}
else if (reply->url().toString().contains(m_statusEndpoint)) {
qDebug() << "Status reply";
}
else if (reply->url().toString().contains(m_notesEndpoint)) {
qDebug() << "Notes reply";
}*/
if (m_loginReplies.contains(reply)) {
qDebug() << "Login reply";
if (json.isObject()) if (json.isObject())
updateLoginFlow(json.object()); updateLoginFlow(json.object());
m_login_replies.removeAll(reply); m_loginReplies.removeAll(reply);
emit loginBusyChanged(loginBusy());
} }
else if (m_poll_replies.contains(reply)) { else if (m_pollReplies.contains(reply)) {
qDebug() << "Poll reply, finished";
if (json.isObject()) if (json.isObject())
updateLoginCredentials(json.object()); updateLoginCredentials(json.object());
m_poll_replies.removeAll(reply); m_pollReplies.removeAll(reply);
m_loginPollTimer.stop(); abortFlowV2Login();
m_loginUrl.clear(); emit loginBusyChanged(loginBusy());
} }
else if (m_status_replies.contains(reply)) { else if (m_statusReplies.contains(reply)) {
qDebug() << "Status reply";
if (json.isObject()) if (json.isObject())
updateStatus(json.object()); updateStatus(json.object());
m_status_replies.removeAll(reply); m_statusReplies.removeAll(reply);
emit statusBusyChanged(statusBusy());
} }
else { else if (m_notesReplies.contains(reply)) {
qDebug() << "Notes reply";
if (mp_model) { if (mp_model) {
if (mp_model->fromJsonDocument(json)) { if (mp_model->fromJsonDocument(json)) {
m_lastSync = QDateTime::currentDateTimeUtc(); m_lastSync = QDateTime::currentDateTimeUtc();
emit lastSyncChanged(m_lastSync); emit lastSyncChanged(m_lastSync);
} }
} }
m_replies.removeAll(reply); m_notesReplies.removeAll(reply);
emit notesBusyChanged(notesBusy());
}
else {
qDebug() << "Unknown reply";
} }
//qDebug() << data; //qDebug() << data;
deleteReply = true;
} }
else if (reply->error() == QNetworkReply::AuthenticationRequiredError) { else if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
emit error(AuthenticationError); emit error(AuthenticationError);
deleteReply = true;
}
else {
if (!m_poll_replies.contains(reply)) {
emit error(CommunicationError);
deleteReply = true;
} }
else { else {
if (m_pollReplies.contains(reply)) {
qDebug() << "Poll reply";
//qDebug() << "Polling not finished yet" << m_pollUrl; //qDebug() << "Polling not finished yet" << m_pollUrl;
} }
else {
emit error(CommunicationError);
} }
emit busyChanged(busy()); }
//if (deleteReply) reply->deleteLater(); reply->deleteLater();
} }
void NotesApi::sslError(QNetworkReply *reply, const QList<QSslError> &errors) { void NotesApi::sslError(QNetworkReply *reply, const QList<QSslError> &errors) {
@ -373,6 +381,12 @@ void NotesApi::saveToFile(QModelIndex, QModelIndex, QVector<int>) {
emit error(LocalFileWriteError); emit error(LocalFileWriteError);
} }
QUrl NotesApi::apiEndpointUrl(const QString endpoint) const {
QUrl url = server();
url.setPath(url.path() + endpoint);
return url;
}
void NotesApi::updateStatus(const QJsonObject &status) { void NotesApi::updateStatus(const QJsonObject &status) {
if (!status.isEmpty()) { if (!status.isEmpty()) {
if (m_status_installed != status.value("installed").toBool()) { if (m_status_installed != status.value("installed").toBool()) {
@ -431,7 +445,6 @@ void NotesApi::updateLoginFlow(const QJsonObject &login) {
url = login.value("login").toString(); url = login.value("login").toString();
if (m_loginUrl != url && url.isValid()) { if (m_loginUrl != url && url.isValid()) {
m_loginUrl = url; m_loginUrl = url;
qDebug() << "Login URL: " << m_loginUrl;
emit loginUrlChanged(m_loginUrl); emit loginUrlChanged(m_loginUrl);
} }
} }
@ -443,13 +456,13 @@ void NotesApi::updateLoginCredentials(const QJsonObject &credentials) {
QString appPassword; QString appPassword;
if (!credentials.isEmpty()) { if (!credentials.isEmpty()) {
serverAddr = credentials.value("server").toString(); serverAddr = credentials.value("server").toString();
if (serverAddr != server()) if (!serverAddr.isEmpty() && serverAddr != server())
setServer(serverAddr); setServer(serverAddr);
loginName = credentials.value("loginName").toString(); loginName = credentials.value("loginName").toString();
if (loginName != username()) if (!loginName.isEmpty() && loginName != username())
setUsername(loginName); setUsername(loginName);
appPassword = credentials.value("appPassword").toString(); appPassword = credentials.value("appPassword").toString();
if (appPassword != password()) if (!appPassword.isEmpty() && appPassword != password())
setPassword(appPassword); setPassword(appPassword);
} }
qDebug() << "Login successfull for user" << loginName << "on" << serverAddr; qDebug() << "Login successfull for user" << loginName << "on" << serverAddr;

View file

@ -10,15 +10,22 @@
#include <QDebug> #include <QDebug>
#include "notesmodel.h" #include "notesmodel.h"
#define STATUS_ENDPOINT "/status.php"
#define LOGIN_ENDPOINT "/index.php/login/v2"
#define NOTES_ENDPOINT "/index.php/apps/notes/api/v0.2"
class NotesApi : public QObject class NotesApi : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit NotesApi(QObject *parent = nullptr); explicit NotesApi(const QString statusEndpoint = STATUS_ENDPOINT,
const QString loginEndpoint = LOGIN_ENDPOINT,
const QString notesEndpoint = NOTES_ENDPOINT,
QObject *parent = nullptr);
virtual ~NotesApi(); virtual ~NotesApi();
Q_PROPERTY(bool sslVerify READ sslVerify WRITE setSslVerify NOTIFY sslVerifyChanged) Q_PROPERTY(bool sslVerify READ sslVerify WRITE setSslVerify NOTIFY sslVerifyChanged)
bool sslVerify() const { return m_request.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer; } bool sslVerify() const { return m_authenticatedRequest.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer; }
void setSslVerify(bool verify); void setSslVerify(bool verify);
Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
@ -61,13 +68,19 @@ public:
void setDataFile(QString dataFile); void setDataFile(QString dataFile);
Q_PROPERTY(bool networkAccessible READ networkAccessible NOTIFY networkAccessibleChanged) Q_PROPERTY(bool networkAccessible READ networkAccessible NOTIFY networkAccessibleChanged)
bool networkAccessible() const { return m_online; } bool networkAccessible() const { return m_manager.networkAccessible() == QNetworkAccessManager::Accessible; }
Q_PROPERTY(QDateTime lastSync READ lastSync NOTIFY lastSyncChanged) Q_PROPERTY(QDateTime lastSync READ lastSync NOTIFY lastSyncChanged)
QDateTime lastSync() const { return m_lastSync; } QDateTime lastSync() const { return m_lastSync; }
Q_PROPERTY(bool statusBusy READ statusBusy NOTIFY statusBusyChanged)
bool statusBusy() const { return !m_statusReplies.empty(); }
Q_PROPERTY(bool loginBusy READ loginBusy NOTIFY loginBusyChanged)
bool loginBusy() const { return !m_loginReplies.empty() || !m_pollReplies.empty() || m_loginPollTimer.isActive(); }
Q_PROPERTY(bool notesBusy READ notesBusy NOTIFY notesBusyChanged)
bool notesBusy() const { return !m_notesReplies.empty(); }
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged) Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
bool busy() const; bool busy() const { return statusBusy() | loginBusy() | notesBusy(); }
Q_PROPERTY(bool statusInstalled READ statusInstalled NOTIFY statusInstalledChanged) Q_PROPERTY(bool statusInstalled READ statusInstalled NOTIFY statusInstalledChanged)
bool statusInstalled() const { return m_status_installed; } bool statusInstalled() const { return m_status_installed; }
@ -90,6 +103,7 @@ public:
Q_INVOKABLE void getStatus(); Q_INVOKABLE void getStatus();
Q_INVOKABLE void initiateFlowV2Login(); Q_INVOKABLE void initiateFlowV2Login();
Q_INVOKABLE void abortFlowV2Login();
Q_INVOKABLE void getAllNotes(QStringList excludeFields = QStringList()); Q_INVOKABLE void getAllNotes(QStringList excludeFields = QStringList());
Q_INVOKABLE void getNote(double noteId, QStringList excludeFields = QStringList()); Q_INVOKABLE void getNote(double noteId, QStringList excludeFields = QStringList());
Q_INVOKABLE void createNote(QVariantMap fields = QVariantMap()); Q_INVOKABLE void createNote(QVariantMap fields = QVariantMap());
@ -122,6 +136,9 @@ signals:
void dataFileChanged(QString dataFile); void dataFileChanged(QString dataFile);
void networkAccessibleChanged(bool accessible); void networkAccessibleChanged(bool accessible);
void lastSyncChanged(QDateTime lastSync); void lastSyncChanged(QDateTime lastSync);
void statusBusyChanged(bool busy);
void loginBusyChanged(bool busy);
void notesBusyChanged(bool busy);
void busyChanged(bool busy); void busyChanged(bool busy);
void statusInstalledChanged(bool installed); void statusInstalledChanged(bool installed);
void statusMaintenanceChanged(bool maintenance); void statusMaintenanceChanged(bool maintenance);
@ -146,18 +163,19 @@ private slots:
void saveToFile(QModelIndex,QModelIndex,QVector<int>); void saveToFile(QModelIndex,QModelIndex,QVector<int>);
private: private:
bool m_online;
QDateTime m_lastSync;
QUrl m_url; QUrl m_url;
QNetworkAccessManager m_manager; QNetworkAccessManager m_manager;
QNetworkRequest m_request; QNetworkRequest m_request;
QVector<QNetworkReply*> m_replies; QNetworkRequest m_authenticatedRequest;
QFile m_jsonFile; QFile m_jsonFile;
NotesModel* mp_model; NotesModel* mp_model;
NotesProxyModel* mp_modelProxy; NotesProxyModel* mp_modelProxy;
QUrl apiEndpointUrl(const QString endpoint) const;
// Nextcloud status.php
const QString m_statusEndpoint;
QVector<QNetworkReply*> m_statusReplies;
void updateStatus(const QJsonObject &status); void updateStatus(const QJsonObject &status);
QVector<QNetworkReply*> m_status_replies;
bool m_status_installed; bool m_status_installed;
bool m_status_maintenance; bool m_status_maintenance;
bool m_status_needsDbUpgrade; bool m_status_needsDbUpgrade;
@ -167,14 +185,21 @@ private:
QString m_status_productname; QString m_status_productname;
bool m_status_extendedSupport; bool m_status_extendedSupport;
// Nextcloud Login Flow v2 - https://docs.nextcloud.com/server/18/developer_manual/client_apis/LoginFlow/index.html#login-flow-v2
const QString m_loginEndpoint;
QVector<QNetworkReply*> m_loginReplies;
QVector<QNetworkReply*> m_pollReplies;
void updateLoginFlow(const QJsonObject &login); void updateLoginFlow(const QJsonObject &login);
void updateLoginCredentials(const QJsonObject &credentials); void updateLoginCredentials(const QJsonObject &credentials);
QVector<QNetworkReply*> m_login_replies;
QVector<QNetworkReply*> m_poll_replies;
QTimer m_loginPollTimer; QTimer m_loginPollTimer;
QUrl m_loginUrl; QUrl m_loginUrl;
QUrl m_pollUrl; QUrl m_pollUrl;
QString m_pollToken; QString m_pollToken;
// Nextcloud Notes API - https://github.com/nextcloud/notes/wiki/Notes-0.2
const QString m_notesEndpoint;
QVector<QNetworkReply*> m_notesReplies;
QDateTime m_lastSync;
}; };
#endif // NOTESAPI_H #endif // NOTESAPI_H

View file

@ -159,6 +159,14 @@
<source>Allow unencrypted connections</source> <source>Allow unencrypted connections</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Abort</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Follow the login procedure</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>MITLicense</name> <name>MITLicense</name>

View file

@ -159,6 +159,14 @@
<source>Allow unencrypted connections</source> <source>Allow unencrypted connections</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Abort</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Follow the login procedure</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>MITLicense</name> <name>MITLicense</name>

View file

@ -130,68 +130,78 @@
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="79"/> <location filename="../qml/pages/LoginDialog.qml" line="80"/>
<location filename="../qml/pages/LoginDialog.qml" line="175"/> <location filename="../qml/pages/LoginDialog.qml" line="174"/>
<source>Login</source> <source>Login</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="79"/> <location filename="../qml/pages/LoginDialog.qml" line="80"/>
<source>Save</source> <source>Save</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="97"/> <location filename="../qml/pages/LoginDialog.qml" line="98"/>
<source>Login Flow v2</source> <source>Login Flow v2</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="111"/> <location filename="../qml/pages/LoginDialog.qml" line="110"/>
<source>Legacy Login</source> <source>Legacy Login</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="121"/> <location filename="../qml/pages/LoginDialog.qml" line="120"/>
<source>Account name</source> <source>Account name</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="136"/> <location filename="../qml/pages/LoginDialog.qml" line="135"/>
<source>Nextcloud server</source> <source>Nextcloud server</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="151"/> <location filename="../qml/pages/LoginDialog.qml" line="150"/>
<source>Username</source> <source>Username</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="164"/> <location filename="../qml/pages/LoginDialog.qml" line="163"/>
<source>Password</source> <source>Password</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="195"/> <location filename="../qml/pages/LoginDialog.qml" line="174"/>
<source>Abort</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/LoginDialog.qml" line="183"/>
<source>Follow the login procedure</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/LoginDialog.qml" line="191"/>
<source>Security</source> <source>Security</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="202"/> <location filename="../qml/pages/LoginDialog.qml" line="198"/>
<source>&lt;strong&gt;CAUTION: Your password will be saved without any encryption on the device!&lt;/strong&gt;&lt;br&gt;Please consider creating a dedicated app password! Open your Nextcloud in a browser and go to &lt;i&gt;Settings&lt;/i&gt; &lt;i&gt;Security&lt;/i&gt;.</source> <source>&lt;strong&gt;CAUTION: Your password will be saved without any encryption on the device!&lt;/strong&gt;&lt;br&gt;Please consider creating a dedicated app password! Open your Nextcloud in a browser and go to &lt;i&gt;Settings&lt;/i&gt; &lt;i&gt;Security&lt;/i&gt;.</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="206"/> <location filename="../qml/pages/LoginDialog.qml" line="202"/>
<source>Do not check certificates</source> <source>Do not check certificates</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="207"/> <location filename="../qml/pages/LoginDialog.qml" line="203"/>
<source>Enable this option to allow selfsigned certificates</source> <source>Enable this option to allow selfsigned certificates</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../qml/pages/LoginDialog.qml" line="213"/> <location filename="../qml/pages/LoginDialog.qml" line="209"/>
<source>Allow unencrypted connections</source> <source>Allow unencrypted connections</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
@ -273,37 +283,37 @@
<context> <context>
<name>NotesApi</name> <name>NotesApi</name>
<message> <message>
<location filename="../src/notesapi.cpp" line="464"/> <location filename="../src/notesapi.cpp" line="477"/>
<source>No network connection available</source> <source>No network connection available</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="467"/> <location filename="../src/notesapi.cpp" line="480"/>
<source>Failed to communicate with the Nextcloud server</source> <source>Failed to communicate with the Nextcloud server</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="470"/> <location filename="../src/notesapi.cpp" line="483"/>
<source>An error happened while reading from the local storage</source> <source>An error happened while reading from the local storage</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="473"/> <location filename="../src/notesapi.cpp" line="486"/>
<source>An error happened while writing to the local storage</source> <source>An error happened while writing to the local storage</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="476"/> <location filename="../src/notesapi.cpp" line="489"/>
<source>An error occured while establishing an encrypted connection</source> <source>An error occured while establishing an encrypted connection</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="479"/> <location filename="../src/notesapi.cpp" line="492"/>
<source>Could not authenticate to the Nextcloud instance</source> <source>Could not authenticate to the Nextcloud instance</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="482"/> <location filename="../src/notesapi.cpp" line="495"/>
<source>Unknown error</source> <source>Unknown error</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>