diff options
author | Olivier Tilloy <olivier.tilloy@canonical.com> | 2012-10-04 12:04:46 +0200 |
---|---|---|
committer | Olivier Tilloy <olivier.tilloy@canonical.com> | 2012-10-04 12:04:46 +0200 |
commit | 36426c5ef4e8d3c8e850996c447d6999e21479c6 (patch) | |
tree | c4d0e3c5572a9e344050ea240ce6d677cc8290e2 | |
parent | a9416ebfa48082278abd2a237964989334fc24c5 (diff) | |
download | qmenumodel-36426c5ef4e8d3c8e850996c447d6999e21479c6.tar.gz qmenumodel-36426c5ef4e8d3c8e850996c447d6999e21479c6.tar.bz2 qmenumodel-36426c5ef4e8d3c8e850996c447d6999e21479c6.zip |
Move the enums into a class that inherits QObject, so they can be exposed and used directly from QML.
-rw-r--r-- | src/QMenuModel/plugin.cpp | 2 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/dbus-enums.h | 53 | ||||
-rw-r--r-- | src/common/qdbusactiongroup.cpp | 4 | ||||
-rw-r--r-- | src/common/qdbusactiongroup.h | 4 | ||||
-rw-r--r-- | src/common/qdbusmenumodel.cpp | 4 | ||||
-rw-r--r-- | src/common/qdbusmenumodel.h | 4 | ||||
-rw-r--r-- | src/common/qdbusobject.cpp | 36 | ||||
-rw-r--r-- | src/common/qdbusobject.h | 31 | ||||
-rw-r--r-- | tests/client/actiongrouptest.cpp | 22 | ||||
-rw-r--r-- | tests/client/menuchangestest.cpp | 2 | ||||
-rw-r--r-- | tests/client/modeltest.cpp | 14 | ||||
-rw-r--r-- | tests/client/servicetest.cpp | 20 |
13 files changed, 121 insertions, 76 deletions
diff --git a/src/QMenuModel/plugin.cpp b/src/QMenuModel/plugin.cpp index 494dd2c..ec51ae9 100644 --- a/src/QMenuModel/plugin.cpp +++ b/src/QMenuModel/plugin.cpp @@ -32,6 +32,8 @@ void QMenuModelQmlPlugin::registerTypes(const char *uri) "QMenuModel is a interface"); qmlRegisterUncreatableType<QStateAction>(uri, 0, 1, "QStateAction", "QStateAction must be created by QDBusActionGroup::action"); + qmlRegisterUncreatableType<DBusEnums>(uri, 0, 1, "DBus", + "DBus is only a namespace"); qmlRegisterType<QDBusMenuModel>(uri, 0, 1, "QDBusMenuModel"); qmlRegisterType<QDBusActionGroup>(uri, 0, 1, "QDBusActionGroup"); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ad3343a..2b831b7 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -11,6 +11,7 @@ set(QMENUMODELCOMMON_SRC set(QMENUMODELCOMMON_HEADERS converter.h + dbus-enums.h qmenumodel.h qdbusobject.h qdbusmenumodel.h diff --git a/src/common/dbus-enums.h b/src/common/dbus-enums.h new file mode 100644 index 0000000..76149da --- /dev/null +++ b/src/common/dbus-enums.h @@ -0,0 +1,53 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Olivier Tilloy <olivier.tilloy@canonical.com> + */ + +#ifndef __DBUS_ENUMS__ +#define __DBUS_ENUMS__ + +#include <QObject> + +// This class acts as a namespace only, with the addition that its enums +// are registered to be exposed on the QML side. +class DBusEnums : public QObject +{ + Q_OBJECT + + Q_ENUMS(BusType) + Q_ENUMS(ConnectionStatus) + +public: + enum BusType { + None = 0, + SessionBus, + SystemBus, + LastBusType + }; + + enum ConnectionStatus { + Disconnected = 0, + Connecting, + Connected + }; + +private: + DBusEnums() {} +}; + +#endif // __DBUS_ENUMS__ + diff --git a/src/common/qdbusactiongroup.cpp b/src/common/qdbusactiongroup.cpp index 629915b..d25f2de 100644 --- a/src/common/qdbusactiongroup.cpp +++ b/src/common/qdbusactiongroup.cpp @@ -143,8 +143,8 @@ void QDBusActionGroup::stop() /*! \internal */ void QDBusActionGroup::setIntBusType(int busType) { - if ((busType > None) && (busType < LastBusType)) { - setBusType(static_cast<BusType>(busType)); + if ((busType > DBusEnums::None) && (busType < DBusEnums::LastBusType)) { + setBusType(static_cast<DBusEnums::BusType>(busType)); } } diff --git a/src/common/qdbusactiongroup.h b/src/common/qdbusactiongroup.h index dd3726c..2bd0a15 100644 --- a/src/common/qdbusactiongroup.h +++ b/src/common/qdbusactiongroup.h @@ -47,10 +47,10 @@ public: Q_INVOKABLE QVariant actionState(const QString &name); Q_SIGNALS: - void busTypeChanged(BusType type); + void busTypeChanged(DBusEnums::BusType type); void busNameChanged(const QString &busNameChanged); void objectPathChanged(const QString &objectPath); - void statusChanged(QDBusObject::ConnectionStatus status); + void statusChanged(DBusEnums::ConnectionStatus status); void actionAppear(const QString &name); void actionVanish(const QString &name); void actionStateChanged(const QString &name, QVariant state); diff --git a/src/common/qdbusmenumodel.cpp b/src/common/qdbusmenumodel.cpp index 9b824b4..354b10b 100644 --- a/src/common/qdbusmenumodel.cpp +++ b/src/common/qdbusmenumodel.cpp @@ -104,7 +104,7 @@ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) /*! \internal */ void QDBusMenuModel::setIntBusType(int busType) { - if ((busType > None) && (busType < LastBusType)) { - setBusType(static_cast<BusType>(busType)); + if ((busType > DBusEnums::None) && (busType < DBusEnums::LastBusType)) { + setBusType(static_cast<DBusEnums::BusType>(busType)); } } diff --git a/src/common/qdbusmenumodel.h b/src/common/qdbusmenumodel.h index 9af516d..2660ffe 100644 --- a/src/common/qdbusmenumodel.h +++ b/src/common/qdbusmenumodel.h @@ -38,10 +38,10 @@ public: ~QDBusMenuModel(); Q_SIGNALS: - void busTypeChanged(BusType type); + void busTypeChanged(DBusEnums::BusType type); void busNameChanged(const QString &busNameChanged); void objectPathChanged(const QString &objectPath); - void statusChanged(QDBusObject::ConnectionStatus status); + void statusChanged(DBusEnums::ConnectionStatus status); public Q_SLOTS: void start(); diff --git a/src/common/qdbusobject.cpp b/src/common/qdbusobject.cpp index d5ac44b..ff2a4a6 100644 --- a/src/common/qdbusobject.cpp +++ b/src/common/qdbusobject.cpp @@ -70,10 +70,10 @@ QDBusObject::QDBusObject() :m_watchId(0), - m_busType(None), - m_status(QDBusObject::Disconnected) + m_busType(DBusEnums::None), + m_status(DBusEnums::Disconnected) { - qRegisterMetaType<QDBusObject::ConnectionStatus>("QDBusObject::ConnectionStatus"); + qRegisterMetaType<DBusEnums::ConnectionStatus>("DBusEnums::ConnectionStatus"); } QDBusObject::~QDBusObject() @@ -84,15 +84,15 @@ QDBusObject::~QDBusObject() } } -QDBusObject::BusType QDBusObject::busType() const +DBusEnums::BusType QDBusObject::busType() const { return m_busType; } -void QDBusObject::setBusType(QDBusObject::BusType type) +void QDBusObject::setBusType(DBusEnums::BusType type) { if (m_busType != type) { - if (m_status != QDBusObject::Disconnected) + if (m_status != DBusEnums::Disconnected) disconnect(); m_busType = type; busTypeChanged(m_busType); @@ -107,7 +107,7 @@ QString QDBusObject::busName() const void QDBusObject::setBusName(const QString &busName) { if (m_busName != busName) { - if (m_status != QDBusObject::Disconnected) + if (m_status != DBusEnums::Disconnected) disconnect(); m_busName = busName; busNameChanged(m_busName); @@ -122,14 +122,14 @@ QString QDBusObject::objectPath() const void QDBusObject::setObjectPath(const QString &objectPath) { if (m_objectPath != objectPath) { - if (m_status != QDBusObject::Disconnected) + if (m_status != DBusEnums::Disconnected) disconnect(); m_objectPath = objectPath; objectPathChanged(m_objectPath); } } -void QDBusObject::setStatus(QDBusObject::ConnectionStatus status) +void QDBusObject::setStatus(DBusEnums::ConnectionStatus status) { if (m_status != status) { m_status = status; @@ -137,17 +137,17 @@ void QDBusObject::setStatus(QDBusObject::ConnectionStatus status) } } -QDBusObject::ConnectionStatus QDBusObject::status() const +DBusEnums::ConnectionStatus QDBusObject::status() const { return m_status; } void QDBusObject::connect() { - if (m_status != QDBusObject::Disconnected) { + if (m_status != DBusEnums::Disconnected) { return; - } else if ((m_busType > None) && !m_objectPath.isEmpty() && !m_busName.isEmpty()) { - GBusType type = m_busType == SessionBus ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM; + } else if ((m_busType > DBusEnums::None) && !m_objectPath.isEmpty() && !m_busName.isEmpty()) { + GBusType type = m_busType == DBusEnums::SessionBus ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM; m_watchId = g_bus_watch_name (type, m_busName.toLatin1(), G_BUS_NAME_WATCHER_FLAGS_NONE, @@ -156,7 +156,7 @@ void QDBusObject::connect() this, NULL); - setStatus(QDBusObject::Connecting); + setStatus(DBusEnums::Connecting); } else { qWarning() << "Invalid dbus connection args"; } @@ -164,10 +164,10 @@ void QDBusObject::connect() void QDBusObject::disconnect() { - if (m_status != QDBusObject::Disconnected) { + if (m_status != DBusEnums::Disconnected) { g_bus_unwatch_name (m_watchId); m_watchId = 0; - setStatus(QDBusObject::Disconnected); + setStatus(DBusEnums::Disconnected); } } @@ -175,7 +175,7 @@ void QDBusObject::onServiceAppeared(GDBusConnection *connection, const gchar *, { QDBusObject *self = reinterpret_cast<QDBusObject*>(data); - self->setStatus(QDBusObject::Connected); + self->setStatus(DBusEnums::Connected); self->serviceAppear(connection); } @@ -183,6 +183,6 @@ void QDBusObject::onServiceFanished(GDBusConnection *connection, const gchar *, { QDBusObject *self = reinterpret_cast<QDBusObject*>(data); - self->setStatus(QDBusObject::Connecting); + self->setStatus(DBusEnums::Connecting); self->serviceVanish(connection); } diff --git a/src/common/qdbusobject.h b/src/common/qdbusobject.h index c50d540..cad6658 100644 --- a/src/common/qdbusobject.h +++ b/src/common/qdbusobject.h @@ -24,27 +24,16 @@ #include <gio/gio.h> +#include "dbus-enums.h" + class QDBusObject { public: - enum BusType { - None = 0, - SessionBus, - SystemBus, - LastBusType - }; - - enum ConnectionStatus { - Disconnected = 0, - Connecting, - Connected - }; - QDBusObject(); ~QDBusObject(); - BusType busType() const; - void setBusType(BusType type); + DBusEnums::BusType busType() const; + void setBusType(DBusEnums::BusType type); QString busName() const; void setBusName(const QString &busName); @@ -52,7 +41,7 @@ public: QString objectPath() const; void setObjectPath(const QString &busName); - ConnectionStatus status() const; + DBusEnums::ConnectionStatus status() const; void connect(); void disconnect(); @@ -62,19 +51,19 @@ protected: virtual void serviceVanish(GDBusConnection *connection) = 0; // notify functions - virtual void busTypeChanged(BusType type) = 0; + virtual void busTypeChanged(DBusEnums::BusType type) = 0; virtual void busNameChanged(const QString &busNameChanged) = 0; virtual void objectPathChanged(const QString &objectPath) = 0; - virtual void statusChanged(ConnectionStatus status) = 0; + virtual void statusChanged(DBusEnums::ConnectionStatus status) = 0; private: guint m_watchId; - BusType m_busType; + DBusEnums::BusType m_busType; QString m_busName; QString m_objectPath; - ConnectionStatus m_status; + DBusEnums::ConnectionStatus m_status; - void setStatus(ConnectionStatus status); + void setStatus(DBusEnums::ConnectionStatus status); // glib slots static void onServiceAppeared(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer data); diff --git a/tests/client/actiongrouptest.cpp b/tests/client/actiongrouptest.cpp index c77c3d5..116bd44 100644 --- a/tests/client/actiongrouptest.cpp +++ b/tests/client/actiongrouptest.cpp @@ -49,12 +49,12 @@ private Q_SLOTS: void init() { m_model.stop(); - m_model.setBusType(QDBusObject::SessionBus); + m_model.setBusType(DBusEnums::SessionBus); m_model.setBusName(MENU_SERVICE_NAME); m_model.setObjectPath(MENU_OBJECT_PATH); m_actionGroup.stop(); - m_actionGroup.setBusType(QDBusObject::SessionBus); + m_actionGroup.setBusType(DBusEnums::SessionBus); m_actionGroup.setBusName(MENU_SERVICE_NAME); m_actionGroup.setObjectPath(MENU_OBJECT_PATH); } @@ -70,16 +70,16 @@ private Q_SLOTS: void testBusTypeProperty() { m_actionGroup.setProperty("busType", 1); - QCOMPARE(m_actionGroup.busType(), QDBusObject::SessionBus); + QCOMPARE(m_actionGroup.busType(), DBusEnums::SessionBus); m_actionGroup.setProperty("busType", 2); - QCOMPARE(m_actionGroup.busType(), QDBusObject::SystemBus); + QCOMPARE(m_actionGroup.busType(), DBusEnums::SystemBus); m_actionGroup.setProperty("busType", 0); - QCOMPARE(m_actionGroup.busType(), QDBusObject::SystemBus); + QCOMPARE(m_actionGroup.busType(), DBusEnums::SystemBus); m_actionGroup.setProperty("busType", 10); - QCOMPARE(m_actionGroup.busType(), QDBusObject::SystemBus); + QCOMPARE(m_actionGroup.busType(), DBusEnums::SystemBus); } /* @@ -90,12 +90,12 @@ private Q_SLOTS: { m_model.start(); m_actionGroup.start(); - QCOMPARE(m_actionGroup.status(), QDBusObject::Connecting); + QCOMPARE(m_actionGroup.status(), DBusEnums::Connecting); // Make menu available m_script.publishMenu(); - QCOMPARE(m_actionGroup.status(), QDBusObject::Connected); + QCOMPARE(m_actionGroup.status(), DBusEnums::Connected); } /* @@ -109,17 +109,17 @@ private Q_SLOTS: // Make menu available m_script.publishMenu(); - QCOMPARE(m_actionGroup.status(), QDBusObject::Connected); + QCOMPARE(m_actionGroup.status(), DBusEnums::Connected); // Append menus m_script.walk(2); // Remove menu from dbus m_script.unpublishMenu(); - QCOMPARE(m_actionGroup.status(), QDBusObject::Connecting); + QCOMPARE(m_actionGroup.status(), DBusEnums::Connecting); m_actionGroup.stop(); - QCOMPARE(m_actionGroup.status(), QDBusObject::Disconnected); + QCOMPARE(m_actionGroup.status(), DBusEnums::Disconnected); } /* diff --git a/tests/client/menuchangestest.cpp b/tests/client/menuchangestest.cpp index 64c1729..2015582 100644 --- a/tests/client/menuchangestest.cpp +++ b/tests/client/menuchangestest.cpp @@ -46,7 +46,7 @@ private Q_SLOTS: void init() { m_model.stop(); - m_model.setBusType(QDBusObject::SessionBus); + m_model.setBusType(DBusEnums::SessionBus); m_model.setBusName(MENU_SERVICE_NAME); m_model.setObjectPath(MENU_OBJECT_PATH); } diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp index 76a3e97..b1f2090 100644 --- a/tests/client/modeltest.cpp +++ b/tests/client/modeltest.cpp @@ -46,7 +46,7 @@ private Q_SLOTS: void init() { m_model.stop(); - m_model.setBusType(QDBusObject::SessionBus); + m_model.setBusType(DBusEnums::SessionBus); m_model.setBusName(MENU_SERVICE_NAME); m_model.setObjectPath(MENU_OBJECT_PATH); } @@ -70,16 +70,16 @@ private Q_SLOTS: void testBusTypeProperty() { m_model.setProperty("busType", 1); - QCOMPARE(m_model.busType(), QDBusObject::SessionBus); + QCOMPARE(m_model.busType(), DBusEnums::SessionBus); m_model.setProperty("busType", 2); - QCOMPARE(m_model.busType(), QDBusObject::SystemBus); + QCOMPARE(m_model.busType(), DBusEnums::SystemBus); m_model.setProperty("busType", 0); - QCOMPARE(m_model.busType(), QDBusObject::SystemBus); + QCOMPARE(m_model.busType(), DBusEnums::SystemBus); m_model.setProperty("busType", 10); - QCOMPARE(m_model.busType(), QDBusObject::SystemBus); + QCOMPARE(m_model.busType(), DBusEnums::SystemBus); } @@ -98,7 +98,7 @@ private Q_SLOTS: // Wait for dbus sync QTest::qWait(500); - QCOMPARE(m_model.status(), QDBusObject::Connected); + QCOMPARE(m_model.status(), DBusEnums::Connected); QCOMPARE(m_model.rowCount(), 4); // Label (String) @@ -146,7 +146,7 @@ private Q_SLOTS: // Wait for dbus sync QTest::qWait(500); - QCOMPARE(m_model.status(), QDBusObject::Connected); + QCOMPARE(m_model.status(), DBusEnums::Connected); QCOMPARE(m_model.rowCount(), 4); QVariant e = m_model.data(m_model.index(0, 0), QMenuModel::Extra); diff --git a/tests/client/servicetest.cpp b/tests/client/servicetest.cpp index cc47334..a8b0c2a 100644 --- a/tests/client/servicetest.cpp +++ b/tests/client/servicetest.cpp @@ -34,7 +34,7 @@ private: void setupModel(QDBusMenuModel *model) { - model->setBusType(QDBusObject::SessionBus); + model->setBusType(DBusEnums::SessionBus); model->setBusName(MENU_SERVICE_NAME); model->setObjectPath(MENU_OBJECT_PATH); } @@ -53,7 +53,7 @@ private Q_SLOTS: void init() { m_model.stop(); - m_model.setBusType(QDBusObject::SessionBus); + m_model.setBusType(DBusEnums::SessionBus); m_model.setBusName(MENU_SERVICE_NAME); m_model.setObjectPath(MENU_OBJECT_PATH); } @@ -66,10 +66,10 @@ private Q_SLOTS: void testMenuStartStopWithNoService() { m_model.start(); - QCOMPARE(m_model.status(), QDBusObject::Connecting); + QCOMPARE(m_model.status(), DBusEnums::Connecting); m_model.stop(); - QCOMPARE(m_model.status(), QDBusObject::Disconnected); + QCOMPARE(m_model.status(), DBusEnums::Disconnected); } void testMenuStartStopWithService() @@ -83,33 +83,33 @@ private Q_SLOTS: // Wait for dbus sync QTest::qWait(500); - QCOMPARE(m_model.status(), QDBusObject::Connected); + QCOMPARE(m_model.status(), DBusEnums::Connected); // Diconnect model m_model.stop(); - QCOMPARE(m_model.status(), QDBusObject::Disconnected); + QCOMPARE(m_model.status(), DBusEnums::Disconnected); } void testMenuServiceAppearAndDissapear() { m_model.start(); - QCOMPARE(m_model.status(), QDBusObject::Connecting); + QCOMPARE(m_model.status(), DBusEnums::Connecting); - QSignalSpy spy(&m_model, SIGNAL(statusChanged(QDBusObject::ConnectionStatus))); + QSignalSpy spy(&m_model, SIGNAL(statusChanged(DBusEnums::ConnectionStatus))); // Make menu available m_script.publishMenu(); // singal changed to connected QCOMPARE(spy.count(), 1); - QCOMPARE(m_model.status(), QDBusObject::Connected); + QCOMPARE(m_model.status(), DBusEnums::Connected); // remove menu service m_script.unpublishMenu(); // signal changed to connecting QCOMPARE(spy.count(), 2); - QCOMPARE(m_model.status(), QDBusObject::Connecting); + QCOMPARE(m_model.status(), DBusEnums::Connecting); } }; |