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); return m_replies.removeOne(reply);
} }
protected slots:
virtual void updateCapabilities(QJsonObject* capabilites) = 0;
virtual void updateReply(QNetworkReply* reply) = 0;
signals: signals:
void installedChanged(bool); void installedChanged(bool);
void capabilitiesChanged(QJsonObject* json); void capabilitiesChanged(QJsonObject* json);

View file

@ -1,10 +1,26 @@
#include "notesapp.h" #include "notesapp.h"
NotesApp::NotesApp(QObject *parent, QString name, NextcloudApi* api) NotesApp::NotesApp(QObject *parent, NextcloudApi* api)
: AbstractNextcloudApp(parent, name, api) { : AbstractNextcloudApp(parent, "notes", api) {
m_notesProxy.setSourceModel(&m_notesModel); 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 { QVersionNumber NotesApp::serverVersion() const {
return QVersionNumber::fromString(m_capabilities.value("version").toString()); return QVersionNumber::fromString(m_capabilities.value("version").toString());

View file

@ -1,6 +1,8 @@
#ifndef NOTESAPP_H #ifndef NOTESAPP_H
#define NOTESAPP_H #define NOTESAPP_H
#include <QQmlEngine>
#include <QJSEngine>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QVersionNumber> #include <QVersionNumber>
@ -21,10 +23,15 @@ class NotesApp : public AbstractNextcloudApp {
Q_PROPERTY(QList<QVersionNumber> apiVersions READ apiVersions NOTIFY capabilitiesChanged) Q_PROPERTY(QList<QVersionNumber> apiVersions READ apiVersions NOTIFY capabilitiesChanged)
public: public:
NotesApp(QObject *parent = nullptr, QString name = QString(), NextcloudApi* api = nullptr); NotesApp(QObject *parent = nullptr, NextcloudApi* api = nullptr);
virtual ~NotesApp() {} 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; } const QSortFilterProxyModel* model() { return &m_notesProxy; }
Q_INVOKABLE QVersionNumber serverVersion() const; Q_INVOKABLE QVersionNumber serverVersion() const;
@ -36,8 +43,8 @@ public slots:
Q_INVOKABLE bool createNote(const QJsonObject& note, bool local = false); 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 updateNote(const int id, const QJsonObject& note, bool local = false);
Q_INVOKABLE bool deleteNote(const int id, bool local = false); Q_INVOKABLE bool deleteNote(const int id, bool local = false);
Q_INVOKABLE bool getSettings(); //Q_INVOKABLE bool getSettings();
Q_INVOKABLE bool changeSettings(const QJsonObject& settings); //Q_INVOKABLE bool changeSettings(const QJsonObject& settings);
protected slots: protected slots:
virtual void updateReply(QNetworkReply* reply); virtual void updateReply(QNetworkReply* reply);
@ -46,6 +53,9 @@ signals:
void capabilitiesChanged(QJsonObject* json); void capabilitiesChanged(QJsonObject* json);
private: private:
// QML singleton
static AbstractNextcloudApp * instance;
NotesModel m_notesModel; NotesModel m_notesModel;
NotesProxyModel m_notesProxy; 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"; const QString NotesModel::m_fileSuffix = "json";
NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) { NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) {
mp_notesApi = nullptr;
//m_fileDir.setCurrent(directory); //m_fileDir.setCurrent(directory);
m_fileDir.setPath(""); m_fileDir.setPath("");
m_fileDir.setFilter(QDir::Files | QDir::NoDotAndDotDot); m_fileDir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
@ -90,25 +89,6 @@ NotesModel::~NotesModel() {
m_files.take(i.key()).close(); m_files.take(i.key()).close();
i.toFront(); 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 { QString NotesModel::account() const {

View file

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

View file

@ -4,7 +4,8 @@
#include <QObject> #include <QObject>
#include "accounthash.h" #include "accounthash.h"
#include "nextcloudapi.h" #include "nextcloudapi.h"
#include "apps/notes/notesmodel.h" #include "apps/abstractnextcloudapp.h"
#include "apps/notes/notesapp.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -17,9 +18,13 @@ int main(int argc, char *argv[])
qDebug() << app->applicationDisplayName() << app->applicationVersion(); qDebug() << app->applicationDisplayName() << app->applicationVersion();
NotesProxyModel* notesProxyModel = new NotesProxyModel;
AccountHash* accountHash = new AccountHash; 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(); QQuickView* view = SailfishApp::createView();
#ifdef QT_DEBUG #ifdef QT_DEBUG
@ -27,7 +32,6 @@ int main(int argc, char *argv[])
#else #else
view->rootContext()->setContextProperty("debug", QVariant(false)); view->rootContext()->setContextProperty("debug", QVariant(false));
#endif #endif
view->rootContext()->setContextProperty("notesModel", notesProxyModel);
view->rootContext()->setContextProperty("accountHash", accountHash); view->rootContext()->setContextProperty("accountHash", accountHash);
view->setSource(SailfishApp::pathTo("qml/harbour-nextcloudnotes.qml")); 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) { void NextcloudApi::setVerifySsl(bool verify) {
if (verify != (m_request.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer) || if (verify != (m_request.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer) ||
verify != (m_authenticatedRequest.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer)) { verify != (m_authenticatedRequest.sslConfiguration().peerVerifyMode() == QSslSocket::VerifyPeer)) {
@ -135,9 +152,9 @@ void NextcloudApi::setPassword(QString password) {
} }
} }
bool NextcloudApi::notesAppInstalled() const { bool NextcloudApi::appInstalled(const QString& name) const {
QJsonObject notes = m_capabilities.value("notes").toObject(); QJsonObject app = m_capabilities.value(name).toObject();
return !notes.isEmpty(); return !app.isEmpty();
} }
QStringList NextcloudApi::notesAppApiVersions() const { QStringList NextcloudApi::notesAppApiVersions() const {

View file

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