aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/qdbusobject.cpp
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier.tilloy@canonical.com>2012-10-05 13:26:10 +0000
committerTarmac <>2012-10-05 13:26:10 +0000
commitc3fbab270f60fc374bae50e787e0b2046f127a07 (patch)
tree61d0eae8f1c1e3d23fb6559cb19e920b167b187c /libqmenumodel/src/qdbusobject.cpp
parent7961150ad1eaa1b3bc62d5215e274a7b58b0ef72 (diff)
parent696fba50395807c344325af11e71ec74cf370c3d (diff)
downloadqmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.tar.gz
qmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.tar.bz2
qmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.zip
Tweaks to the structure of the code, and added example code.. Approved by Renato Araujo Oliveira Filho, jenkins.
Diffstat (limited to 'libqmenumodel/src/qdbusobject.cpp')
-rw-r--r--libqmenumodel/src/qdbusobject.cpp188
1 files changed, 188 insertions, 0 deletions
diff --git a/libqmenumodel/src/qdbusobject.cpp b/libqmenumodel/src/qdbusobject.cpp
new file mode 100644
index 0000000..abc68b4
--- /dev/null
+++ b/libqmenumodel/src/qdbusobject.cpp
@@ -0,0 +1,188 @@
+/*
+ * 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:
+ * Renato Araujo Oliveira Filho <renato@canonical.com>
+ */
+
+#include "qdbusobject.h"
+
+#include <QDebug>
+
+/*!
+ \qmlclass QDBusObject
+ \brief The QDBusObject is a base class
+
+ \bold {This component is under heavy development.}
+
+ This is a abstracted class used by QDBusMenuModel and QDBusActionGroup
+*/
+
+/*!
+ \qmlproperty int QDBusObject::busType
+ This property holds the dbus session type which will be used during the connection.
+
+ This must be seteed before call start method
+ The valid values are:
+ \list
+ \o 1 - SessionBus
+ \o 2 - SystemBus
+ \endlist
+*/
+
+/*!
+ \qmlproperty int QDBusObject::busName
+ This property holds the dbus service name related with menu.
+
+ This must be seteed before call start method
+*/
+
+/*!
+ \qmlproperty int QDBusObject::objectPath
+ This property holds the dbus object path related with the menu.
+
+ This must be seteed before call start method
+*/
+
+/*!
+ \qmlproperty int QDBusObject::status
+ This property holds current dbus connection status
+
+ Te velid status are:
+ \list
+ \o 0 - Disconnected
+ \o 1 - Connecting
+ \o 2 - Connected
+ \endlist
+*/
+
+QDBusObject::QDBusObject()
+ :m_watchId(0),
+ m_busType(DBusEnums::None),
+ m_status(DBusEnums::Disconnected)
+{
+ qRegisterMetaType<DBusEnums::ConnectionStatus>("DBusEnums::ConnectionStatus");
+}
+
+QDBusObject::~QDBusObject()
+{
+ if (m_watchId != 0) {
+ g_bus_unwatch_name (m_watchId);
+ m_watchId = 0;
+ }
+}
+
+DBusEnums::BusType QDBusObject::busType() const
+{
+ return m_busType;
+}
+
+void QDBusObject::setBusType(DBusEnums::BusType type)
+{
+ if (m_busType != type) {
+ if (m_status != DBusEnums::Disconnected)
+ disconnect();
+ m_busType = type;
+ busTypeChanged(m_busType);
+ }
+}
+
+QString QDBusObject::busName() const
+{
+ return m_busName;
+}
+
+void QDBusObject::setBusName(const QString &busName)
+{
+ if (m_busName != busName) {
+ if (m_status != DBusEnums::Disconnected)
+ disconnect();
+ m_busName = busName;
+ busNameChanged(m_busName);
+ }
+}
+
+QString QDBusObject::objectPath() const
+{
+ return m_objectPath;
+}
+
+void QDBusObject::setObjectPath(const QString &objectPath)
+{
+ if (m_objectPath != objectPath) {
+ if (m_status != DBusEnums::Disconnected)
+ disconnect();
+ m_objectPath = objectPath;
+ objectPathChanged(m_objectPath);
+ }
+}
+
+void QDBusObject::setStatus(DBusEnums::ConnectionStatus status)
+{
+ if (m_status != status) {
+ m_status = status;
+ statusChanged(m_status);
+ }
+}
+
+DBusEnums::ConnectionStatus QDBusObject::status() const
+{
+ return m_status;
+}
+
+void QDBusObject::connect()
+{
+ if (m_status != DBusEnums::Disconnected) {
+ return;
+ } 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,
+ QDBusObject::onServiceAppeared,
+ QDBusObject::onServiceVanished,
+ this,
+ NULL);
+
+ setStatus(DBusEnums::Connecting);
+ } else {
+ qWarning() << "Invalid dbus connection args";
+ }
+}
+
+void QDBusObject::disconnect()
+{
+ if (m_status != DBusEnums::Disconnected) {
+ g_bus_unwatch_name (m_watchId);
+ m_watchId = 0;
+ setStatus(DBusEnums::Disconnected);
+ }
+}
+
+void QDBusObject::onServiceAppeared(GDBusConnection *connection, const gchar *, const gchar *, gpointer data)
+{
+ QDBusObject *self = reinterpret_cast<QDBusObject*>(data);
+
+ self->setStatus(DBusEnums::Connected);
+ self->serviceAppear(connection);
+}
+
+void QDBusObject::onServiceVanished(GDBusConnection *connection, const gchar *, gpointer data)
+{
+ QDBusObject *self = reinterpret_cast<QDBusObject*>(data);
+
+ self->setStatus(DBusEnums::Connecting);
+ self->serviceVanish(connection);
+}