summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGatis Paeglis <gatis.paeglis@qt.io>2016-12-19 15:35:28 +0100
committerGatis Paeglis <gatis.paeglis@qt.io>2016-12-21 11:32:58 +0000
commite8706a00dab31058b204882acc71732fa1515c8f (patch)
treeaf686842ba96daca55980a1c6bda09f5838fadfd
parent0e71fd622c82f692dac7ea422988f5ba10e57e1f (diff)
Make QOtaClient a singleton type
Having a several instances of this type does not make sense. This also is more consistent with the QML API where OtaClient is a singleton type. Change-Id: I11b704691f6e08730eb898520eeaa383e8549275 Reviewed-by: Gatis Paeglis <gatis.paeglis@qt.io>
-rw-r--r--examples/cpp/basic-daemon/main.cpp20
-rw-r--r--src/imports/pluginmain.cpp3
-rw-r--r--src/lib/qotaclient.cpp12
-rw-r--r--src/lib/qotaclient.h3
4 files changed, 25 insertions, 13 deletions
diff --git a/examples/cpp/basic-daemon/main.cpp b/examples/cpp/basic-daemon/main.cpp
index f67d1bd..1f0657d 100644
--- a/examples/cpp/basic-daemon/main.cpp
+++ b/examples/cpp/basic-daemon/main.cpp
@@ -40,16 +40,18 @@ class UpdateChecker : public QObject
{
Q_OBJECT
public:
- UpdateChecker(const QString &guiUpdater) : m_guiUpdaterPath(guiUpdater)
+ UpdateChecker(const QString &guiUpdater) :
+ m_device(&QOtaClient::instance()),
+ m_guiUpdaterPath(guiUpdater)
{
- connect(&m_device, &QOtaClient::fetchRemoteInfoFinished, this, &UpdateChecker::fetchFinished);
- connect(&m_device, &QOtaClient::statusStringChanged, this, &UpdateChecker::log);
- connect(&m_device, &QOtaClient::errorOccurred, this, &UpdateChecker::logError);
+ connect(m_device, &QOtaClient::fetchRemoteInfoFinished, this, &UpdateChecker::fetchFinished);
+ connect(m_device, &QOtaClient::statusStringChanged, this, &UpdateChecker::log);
+ connect(m_device, &QOtaClient::errorOccurred, this, &UpdateChecker::logError);
connect(&m_fetchTimer, &QTimer::timeout, this, &UpdateChecker::startFetch);
m_repoConfig.setUrl(QStringLiteral("http://www.b2qtupdate.com/ostree-repo"));
- if (!m_device.isRepositoryConfigSet(&m_repoConfig))
- m_device.setRepositoryConfig(&m_repoConfig);
+ if (!m_device->isRepositoryConfigSet(&m_repoConfig))
+ m_device->setRepositoryConfig(&m_repoConfig);
m_fetchTimer.setSingleShot(true);
m_fetchTimer.start();
@@ -63,12 +65,12 @@ public:
void startFetch()
{
log(QStringLiteral("verifying remote server for system updates..."));
- m_device.fetchRemoteInfo();
+ m_device->fetchRemoteInfo();
}
void fetchFinished(bool success)
{
- if (success && m_device.updateAvailable()) {
+ if (success && m_device->updateAvailable()) {
log(QStringLiteral("update available"));
// Any inter-process communication mechanism could be used here. In this demo we
// simply launch a GUI that can be used to execute the update commands (such as examples/qml/basic/).
@@ -91,7 +93,7 @@ public:
}
private:
- QOtaClient m_device;
+ QOtaClient *m_device;
QOtaRepositoryConfig m_repoConfig;
QTimer m_fetchTimer;
QString m_guiUpdaterPath;
diff --git a/src/imports/pluginmain.cpp b/src/imports/pluginmain.cpp
index 1355232..25f3a36 100644
--- a/src/imports/pluginmain.cpp
+++ b/src/imports/pluginmain.cpp
@@ -472,8 +472,9 @@ static QObject *otaClientSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
{
Q_UNUSED(qmlEngine);
Q_UNUSED(jsEngine);
- return new QOtaClient;
+ return &QOtaClient::instance();
}
+
class QOTAUpdatePlugin : public QQmlExtensionPlugin
{
Q_OBJECT
diff --git a/src/lib/qotaclient.cpp b/src/lib/qotaclient.cpp
index 30670a1..05ab97d 100644
--- a/src/lib/qotaclient.cpp
+++ b/src/lib/qotaclient.cpp
@@ -291,8 +291,7 @@ void QOtaClientPrivate::defaultRevisionChanged(const QString &defaultRevision)
\c nullptr value).
*/
-QOtaClient::QOtaClient(QObject *parent) :
- QObject(parent),
+QOtaClient::QOtaClient() :
d_ptr(new QOtaClientPrivate(this))
{
Q_D(QOtaClient);
@@ -318,6 +317,15 @@ QOtaClient::~QOtaClient()
}
/*!
+ Returns a singleton instance of QOtaClient.
+*/
+QOtaClient& QOtaClient::instance()
+{
+ static QOtaClient otaClient;
+ return otaClient;
+}
+
+/*!
//! [fetchremoteinfo-description]
Fetches OTA metadata from a remote server and updates the local metadata
cache. This metadata contains information on what system version is available
diff --git a/src/lib/qotaclient.h b/src/lib/qotaclient.h
index 66fbc2f..901c1ae 100644
--- a/src/lib/qotaclient.h
+++ b/src/lib/qotaclient.h
@@ -60,7 +60,7 @@ class Q_DECL_EXPORT QOtaClient : public QObject
Q_PROPERTY(QString rollbackRevision READ rollbackRevision NOTIFY rollbackInfoChanged)
Q_PROPERTY(QByteArray rollbackInfo READ rollbackInfo NOTIFY rollbackInfoChanged)
public:
- explicit QOtaClient(QObject *parent = nullptr);
+ static QOtaClient& instance();
virtual ~QOtaClient();
bool updateAvailable() const;
@@ -113,6 +113,7 @@ Q_SIGNALS:
void updateRemoteInfoOfflineFinished(bool success);
private:
+ QOtaClient();
Q_DISABLE_COPY(QOtaClient)
Q_DECLARE_PRIVATE(QOtaClient)
QOtaClientPrivate *const d_ptr;