Implemented NextcloudApi and NotesApp as Singleton QML Types

This commit is contained in:
scharel 2021-05-27 19:28:40 +02:00
parent 6dc413fa2b
commit f64df0ddf6
8 changed files with 72 additions and 43 deletions

View file

@ -51,10 +51,6 @@ public slots:
return m_replies.removeOne(reply);
}
protected slots:
virtual void updateCapabilities(QJsonObject* capabilites) = 0;
virtual void updateReply(QNetworkReply* reply) = 0;
signals:
void installedChanged(bool);
void capabilitiesChanged(QJsonObject* json);

View file

@ -1,10 +1,26 @@
#include "notesapp.h"
NotesApp::NotesApp(QObject *parent, QString name, NextcloudApi* api)
: AbstractNextcloudApp(parent, name, api) {
NotesApp::NotesApp(QObject *parent, NextcloudApi* api)
: AbstractNextcloudApp(parent, "notes", api) {
m_notesProxy.setSourceModel(&m_notesModel);
}
// QML singleton
AbstractNextcloudApp * NotesApp::instance = nullptr;
void NotesApp::instantiate(QObject *parent, NextcloudApi *api) {
if (instance == nullptr) {
instance = new NotesApp(parent, api);
}
}
AbstractNextcloudApp & NotesApp::getInstance() {
return *instance;
}
QObject * NotesApp::provider(QQmlEngine *, QJSEngine *) {
return instance;
}
QVersionNumber NotesApp::serverVersion() const {
return QVersionNumber::fromString(m_capabilities.value("version").toString());

View file

@ -1,6 +1,8 @@
#ifndef NOTESAPP_H
#define NOTESAPP_H
#include <QQmlEngine>
#include <QJSEngine>
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include <QVersionNumber>
@ -21,10 +23,15 @@ class NotesApp : public AbstractNextcloudApp {
Q_PROPERTY(QList<QVersionNumber> apiVersions READ apiVersions NOTIFY capabilitiesChanged)
public:
NotesApp(QObject *parent = nullptr, QString name = QString(), NextcloudApi* api = nullptr);
NotesApp(QObject *parent = nullptr, NextcloudApi* api = nullptr);
virtual ~NotesApp() {}
// QML singleton
static void instantiate(QObject *parent = nullptr, NextcloudApi* api = nullptr);
static AbstractNextcloudApp & getInstance();
static QObject * provider(QQmlEngine *, QJSEngine *);
const QSortFilterProxyModel* model() { return &m_notesProxy; }
Q_INVOKABLE QVersionNumber serverVersion() const;
@ -36,8 +43,8 @@ public slots:
Q_INVOKABLE bool createNote(const QJsonObject& note, bool local = false);
Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note, bool local = false);
Q_INVOKABLE bool deleteNote(const int id, bool local = false);
Q_INVOKABLE bool getSettings();
Q_INVOKABLE bool changeSettings(const QJsonObject& settings);
//Q_INVOKABLE bool getSettings();
//Q_INVOKABLE bool changeSettings(const QJsonObject& settings);
protected slots:
virtual void updateReply(QNetworkReply* reply);
@ -46,6 +53,9 @@ signals:
void capabilitiesChanged(QJsonObject* json);
private:
// QML singleton
static AbstractNextcloudApp * instance;
NotesModel m_notesModel;
NotesProxyModel m_notesProxy;

View file

@ -75,7 +75,6 @@ const QHash<int, QByteArray> NotesModel::m_roleNames = QHash<int, QByteArray> (
const QString NotesModel::m_fileSuffix = "json";
NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) {
mp_notesApi = nullptr;
//m_fileDir.setCurrent(directory);
m_fileDir.setPath("");
m_fileDir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
@ -90,25 +89,6 @@ NotesModel::~NotesModel() {
m_files.take(i.key()).close();
i.toFront();
}*/
setNotesApi(nullptr);
}
void NotesModel::setNotesApi(NotesApi *notesApi) {
if (mp_notesApi) {
// disconnect stuff
disconnect(mp_notesApi, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
disconnect(mp_notesApi, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
disconnect(mp_notesApi, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
disconnect(mp_notesApi, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
}
mp_notesApi = notesApi;
if (mp_notesApi) {
// connect stuff
connect(mp_notesApi, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
connect(mp_notesApi, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
connect(mp_notesApi, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
connect(mp_notesApi, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
}
}
QString NotesModel::account() const {

View file

@ -9,7 +9,6 @@
#include <QJsonArray>
#include <QDateTime>
#include "note.h"
//#include "notesapi.h"
class NotesProxyModel : public QSortFilterProxyModel {
Q_OBJECT
@ -81,8 +80,6 @@ public:
virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
void setNotesApi(NotesApi* notesApi);
int newNotePosition(const int id) const;
Q_INVOKABLE const QVariantMap note(const int id) const;
Q_INVOKABLE bool setNote(const QVariantMap& note, int id = -1);
@ -105,8 +102,6 @@ private:
QDir m_fileDir;
const static QString m_fileSuffix;
//NotesApi* mp_notesApi;
};
#endif // NOTESMODEL_H

View file

@ -4,7 +4,8 @@
#include <QObject>
#include "accounthash.h"
#include "nextcloudapi.h"
#include "apps/notes/notesmodel.h"
#include "apps/abstractnextcloudapp.h"
#include "apps/notes/notesapp.h"
int main(int argc, char *argv[])
{
@ -17,9 +18,13 @@ int main(int argc, char *argv[])
qDebug() << app->applicationDisplayName() << app->applicationVersion();
NotesProxyModel* notesProxyModel = new NotesProxyModel;
AccountHash* accountHash = new AccountHash;
qmlRegisterType<NextcloudApi>("NextcloudApi", 1, 0, "Nextcloud");
NextcloudApi::instantiate(app);
qmlRegisterSingletonType<NextcloudApi>("harbour.nextcloudnotes.nextcloudapi", 1, 0, "Nextcloud", NextcloudApi::provider);
NotesApp::instantiate(&NextcloudApi::getInstance(), &NextcloudApi::getInstance());
qmlRegisterSingletonType<NotesApp>("harbour.nextcloudnotes.nextcloudapi", 1, 0, "Notes", NotesApp::provider);
//qmlRegisterType<NextcloudApi>("NextcloudApi", 1, 0, "Nextcloud");
//qmlRegisterType<NotesApp>("NextcloudApi", 1, 0, "Notes");
QQuickView* view = SailfishApp::createView();
#ifdef QT_DEBUG
@ -27,7 +32,6 @@ int main(int argc, char *argv[])
#else
view->rootContext()->setContextProperty("debug", QVariant(false));
#endif
view->rootContext()->setContextProperty("notesModel", notesProxyModel);
view->rootContext()->setContextProperty("accountHash", accountHash);
view->setSource(SailfishApp::pathTo("qml/harbour-nextcloudnotes.qml"));

View file

@ -44,6 +44,23 @@ NextcloudApi::~NextcloudApi() {
}
}
// QML singleton
NextcloudApi * NextcloudApi::instance = nullptr;
void NextcloudApi::instantiate(QObject *parent) {
if (instance == nullptr) {
instance = new NextcloudApi(parent);
}
}
NextcloudApi & NextcloudApi::getInstance() {
return *instance;
}
QObject * NextcloudApi::provider(QQmlEngine *, QJSEngine *) {
return instance;
}
void NextcloudApi::setVerifySsl(bool verify) {
if (verify != (m_request.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer) ||
verify != (m_authenticatedRequest.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer)) {
@ -135,9 +152,9 @@ void NextcloudApi::setPassword(QString password) {
}
}
bool NextcloudApi::notesAppInstalled() const {
QJsonObject notes = m_capabilities.value("notes").toObject();
return !notes.isEmpty();
bool NextcloudApi::appInstalled(const QString& name) const {
QJsonObject app = m_capabilities.value(name).toObject();
return !app.isEmpty();
}
QStringList NextcloudApi::notesAppApiVersions() const {

View file

@ -2,6 +2,8 @@
#define NEXTCLOUDAPI_H
#include <QObject>
#include <QQmlEngine>
#include <QJSEngine>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
@ -82,14 +84,17 @@ class NextcloudApi : public QObject
// Nextcloud capabilities
Q_PROPERTY(ApiCallStatus capabilitiesStatus READ capabilitiesStatus NOTIFY capabilitiesStatusChanged)
Q_PROPERTY(bool notesAppInstalled READ notesAppInstalled NOTIFY capabilitiesChanged)
Q_PROPERTY(QStringList notesAppApiVersions READ notesAppApiVersions NOTIFY capabilitiesChanged)
// TODO other property for server capabilities
public:
explicit NextcloudApi(QObject *parent = nullptr);
virtual ~NextcloudApi();
// QML singleton
static void instantiate(QObject *parent = nullptr);
static NextcloudApi & getInstance();
static QObject * provider(QQmlEngine *, QJSEngine *);
// API reply format
enum ReplyFormat {
ReplyJSON, // The reply should be in JSON format
@ -205,6 +210,9 @@ public slots:
Q_INVOKABLE bool getUserList();
Q_INVOKABLE bool getCapabilities();
// Capabilities
Q_INVOKABLE bool appInstalled(const QString& name) const;
signals:
// Generic API properties
void verifySslChanged(bool verify);
@ -248,6 +256,9 @@ private slots:
void replyFinished(QNetworkReply* reply);
private:
// QML singleton
static NextcloudApi * instance;
QUrl m_url;
QNetworkAccessManager m_manager;
QVector<QNetworkReply*> m_replies;