diff --git a/src/apps/abstractnextcloudapp.h b/src/apps/abstractnextcloudapp.h index 4aeff82..a65ef22 100644 --- a/src/apps/abstractnextcloudapp.h +++ b/src/apps/abstractnextcloudapp.h @@ -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); diff --git a/src/apps/notes/notesapp.cpp b/src/apps/notes/notesapp.cpp index 68f8bb0..48d0e5c 100644 --- a/src/apps/notes/notesapp.cpp +++ b/src/apps/notes/notesapp.cpp @@ -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()); diff --git a/src/apps/notes/notesapp.h b/src/apps/notes/notesapp.h index 166c84b..3dd48b9 100644 --- a/src/apps/notes/notesapp.h +++ b/src/apps/notes/notesapp.h @@ -1,6 +1,8 @@ #ifndef NOTESAPP_H #define NOTESAPP_H +#include +#include #include #include #include @@ -21,10 +23,15 @@ class NotesApp : public AbstractNextcloudApp { Q_PROPERTY(QList 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; diff --git a/src/apps/notes/notesmodel.cpp b/src/apps/notes/notesmodel.cpp index 70b97b2..60cab84 100644 --- a/src/apps/notes/notesmodel.cpp +++ b/src/apps/notes/notesmodel.cpp @@ -75,7 +75,6 @@ const QHash NotesModel::m_roleNames = QHash ( 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 { diff --git a/src/apps/notes/notesmodel.h b/src/apps/notes/notesmodel.h index c2c3a5a..46333ef 100644 --- a/src/apps/notes/notesmodel.h +++ b/src/apps/notes/notesmodel.h @@ -9,7 +9,6 @@ #include #include #include "note.h" -//#include "notesapi.h" class NotesProxyModel : public QSortFilterProxyModel { Q_OBJECT @@ -81,8 +80,6 @@ public: virtual QMap itemData(const QModelIndex &index) const; virtual bool setItemData(const QModelIndex &index, const QMap &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 diff --git a/src/harbour-nextcloudnotes.cpp b/src/harbour-nextcloudnotes.cpp index 5cfce99..7ad5eca 100644 --- a/src/harbour-nextcloudnotes.cpp +++ b/src/harbour-nextcloudnotes.cpp @@ -4,7 +4,8 @@ #include #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", 1, 0, "Nextcloud"); + NextcloudApi::instantiate(app); + qmlRegisterSingletonType("harbour.nextcloudnotes.nextcloudapi", 1, 0, "Nextcloud", NextcloudApi::provider); + NotesApp::instantiate(&NextcloudApi::getInstance(), &NextcloudApi::getInstance()); + qmlRegisterSingletonType("harbour.nextcloudnotes.nextcloudapi", 1, 0, "Notes", NotesApp::provider); + //qmlRegisterType("NextcloudApi", 1, 0, "Nextcloud"); + //qmlRegisterType("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")); diff --git a/src/nextcloudapi.cpp b/src/nextcloudapi.cpp index 5f3b6dc..0d74775 100644 --- a/src/nextcloudapi.cpp +++ b/src/nextcloudapi.cpp @@ -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 { diff --git a/src/nextcloudapi.h b/src/nextcloudapi.h index 82bed9e..9620bb5 100644 --- a/src/nextcloudapi.h +++ b/src/nextcloudapi.h @@ -2,6 +2,8 @@ #define NEXTCLOUDAPI_H #include +#include +#include #include #include #include @@ -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 m_replies;