aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-09-14 16:11:28 -0300
committerRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-09-14 16:11:28 -0300
commitebe349e1d96b28e31c29b3e563da1a65fb176e39 (patch)
treebe1271f3d09159fa9f1a44b96fc62cff9cb3b72e /src
parent2b9cf1654e8eb602f70e14246c3c583962ce7254 (diff)
downloadqmenumodel-ebe349e1d96b28e31c29b3e563da1a65fb176e39.tar.gz
qmenumodel-ebe349e1d96b28e31c29b3e563da1a65fb176e39.tar.bz2
qmenumodel-ebe349e1d96b28e31c29b3e563da1a65fb176e39.zip
Created function to convert from QVariant to GVariant.
Moved QStateAction controler from QACtionGroup.
Diffstat (limited to 'src')
-rw-r--r--src/QMenuModel/plugin.cpp5
-rw-r--r--src/common/converter.cpp53
-rw-r--r--src/common/converter.h5
-rw-r--r--src/common/qdbusactiongroup.cpp121
-rw-r--r--src/common/qdbusactiongroup.h21
-rw-r--r--src/common/qmenumodel.cpp2
-rw-r--r--src/common/qstateaction.cpp52
-rw-r--r--src/common/qstateaction.h23
8 files changed, 164 insertions, 118 deletions
diff --git a/src/QMenuModel/plugin.cpp b/src/QMenuModel/plugin.cpp
index fbd943c..494dd2c 100644
--- a/src/QMenuModel/plugin.cpp
+++ b/src/QMenuModel/plugin.cpp
@@ -30,9 +30,12 @@ void QMenuModelQmlPlugin::registerTypes(const char *uri)
{
qmlRegisterUncreatableType<QMenuModel>(uri, 0, 1, "QMenuModel",
"QMenuModel is a interface");
+ qmlRegisterUncreatableType<QStateAction>(uri, 0, 1, "QStateAction",
+ "QStateAction must be created by QDBusActionGroup::action");
+
qmlRegisterType<QDBusMenuModel>(uri, 0, 1, "QDBusMenuModel");
qmlRegisterType<QDBusActionGroup>(uri, 0, 1, "QDBusActionGroup");
- qmlRegisterType<QStateAction>(uri, 0, 1, "QStateAction");
+
}
Q_EXPORT_PLUGIN2(qmenumodel, QMenuModelQmlPlugin)
diff --git a/src/common/converter.cpp b/src/common/converter.cpp
index c5cdade..54d4665 100644
--- a/src/common/converter.cpp
+++ b/src/common/converter.cpp
@@ -3,7 +3,7 @@
#include <QDebug>
/*! \internal */
-QVariant Converter::toGVariant(GVariant *value)
+QVariant Converter::toQVariant(GVariant *value)
{
QVariant result;
if (value == NULL) {
@@ -59,3 +59,54 @@ QVariant Converter::toGVariant(GVariant *value)
return result;
}
+
+GVariant* Converter::toGVariant(const QString &typeName, const QVariant &value)
+{
+ if (typeName == "uchar") {
+ return g_variant_new_byte(value.value<uchar>());
+ } else if (typeName == "short") {
+ return g_variant_new_int16(value.value<short>());
+ } else if (typeName == "ushort") {
+ return g_variant_new_uint16(value.value<ushort>());
+ } else if (typeName == "long") {
+ return g_variant_new_int64(value.value<long>());
+ } else if (typeName == "ulong") {
+ return g_variant_new_uint64(value.value<ulong>());
+ } else {
+ qWarning() << "QVariant type not supported:" << typeName;
+ }
+
+ return NULL;
+}
+
+GVariant* Converter::toGVariant(const QVariant &value)
+{
+ GVariant *result = NULL;
+ if (value.isNull() || !value.isValid())
+ return result;
+
+ switch(value.type()) {
+ case QVariant::Bool:
+ result = g_variant_new_boolean(value.toBool());
+ break;
+ case QVariant::ByteArray:
+ result = g_variant_new_bytestring(value.toByteArray());
+ break;
+ case QVariant::Double:
+ result = g_variant_new_double(value.toDouble());
+ break;
+ case QVariant::Int:
+ result = g_variant_new_int32(value.toInt());
+ break;
+ case QVariant::String:
+ result = g_variant_new_string(value.toString().toLatin1());
+ break;
+ case QVariant::UInt:
+ result = g_variant_new_uint32(value.toUInt());
+ break;
+ default:
+ result = toGVariant(value.typeName(), value);
+ }
+
+ return result;
+}
diff --git a/src/common/converter.h b/src/common/converter.h
index 6ad4b2c..3029b0d 100644
--- a/src/common/converter.h
+++ b/src/common/converter.h
@@ -26,11 +26,14 @@
class Converter
{
public:
- static QVariant toGVariant(GVariant *value);
+ static QVariant toQVariant(GVariant *value);
+ static GVariant* toGVariant(const QVariant &value);
private:
Converter();
Converter(const Converter &other);
+
+ static GVariant* toGVariant(const QString &typeName, const QVariant &value);
};
#endif
diff --git a/src/common/qdbusactiongroup.cpp b/src/common/qdbusactiongroup.cpp
index b6f52f5..305e12e 100644
--- a/src/common/qdbusactiongroup.cpp
+++ b/src/common/qdbusactiongroup.cpp
@@ -73,15 +73,24 @@ QStateAction *QDBusActionGroup::action(const QString &name)
{
QStateAction *act = actionImpl(name);
if (act == 0) {
- return addAction(name.toLatin1(), true);
+ return new QStateAction(this, name);
} else {
return act;
}
}
+bool QDBusActionGroup::hasAction(const QString &actionName)
+{
+ if (m_actionGroup) {
+ return g_action_group_has_action(m_actionGroup, actionName.toLatin1());
+ } else {
+ return false;
+ }
+}
+
QStateAction *QDBusActionGroup::actionImpl(const QString &name)
{
- Q_FOREACH(QStateAction *act, m_actions) {
+ Q_FOREACH(QStateAction *act, this->findChildren<QStateAction*>()) {
if (act->text() == name) {
return act;
}
@@ -89,19 +98,10 @@ QStateAction *QDBusActionGroup::actionImpl(const QString &name)
return 0;
}
-/*!
- \qmlproperty int QDBusActionGroup::count
- This property holds the number of actions inside of \l QDBusActionGroup
-*/
-int QDBusActionGroup::count() const
-{
- return m_actions.count();
-}
-
/*! \internal */
void QDBusActionGroup::serviceVanish(GDBusConnection *)
{
- clear();
+ setActionGroup(NULL);
}
/*! \internal */
@@ -147,8 +147,8 @@ void QDBusActionGroup::setActionGroup(GDBusActionGroup *ag)
g_signal_handler_disconnect(m_actionGroup, m_signalActionAddId);
g_signal_handler_disconnect(m_actionGroup, m_signalActionRemovedId);
g_signal_handler_disconnect(m_actionGroup, m_signalStateChangedId);
- m_signalActionAddId = m_signalActionRemovedId = m_signalStateChangedId = 0;
- g_object_unref(m_actionGroup);
+ m_signalActionAddId = m_signalActionRemovedId = m_signalStateChangedId = 0;
+ clear();
}
m_actionGroup = reinterpret_cast<GActionGroup*>(ag);
@@ -169,93 +169,32 @@ void QDBusActionGroup::setActionGroup(GDBusActionGroup *ag)
G_CALLBACK(QDBusActionGroup::onActionStateChanged),
this);
- gchar **actionNames = g_action_group_list_actions(m_actionGroup);
- for(int i=0; actionNames[i] != NULL; i++) {
- addAction(actionNames[i], true);
+ gchar **actions = g_action_group_list_actions(m_actionGroup);
+ for(int i=0; i < g_strv_length(actions); i++) {
+ Q_EMIT actionAppear(actions[i]);
}
- g_strfreev(actionNames);
+ g_strfreev(actions);
}
}
/*! \internal */
-QStateAction *QDBusActionGroup::addAction(const char *actionName, bool create)
-{
- bool isNew = false;
- QStateAction *act = actionImpl(actionName);
- if (act == 0) {
- if (create) {
- act = new QStateAction(actionName, this);
- isNew = true;
- } else {
- return 0;
- }
- }
-
- if (g_action_group_has_action(m_actionGroup, actionName)) {
- act->setEnabled(g_action_group_get_action_enabled(m_actionGroup, actionName));
-
- GVariant *actState = g_action_group_get_action_state(m_actionGroup, actionName);
- if (actState) {
- act->setState(Converter::toGVariant(actState));
- }
- act->setValid(true);
- } else {
- act->setValid(false);
- }
-
- if (isNew) {
- QObject::connect(act, SIGNAL(triggered()), this, SLOT(onActionTriggered()));
- m_actions.insert(act);
- Q_EMIT countChanged(m_actions.count());
- }
- return act;
-}
-
-/*! \internal */
-void QDBusActionGroup::onActionTriggered()
-{
- QStateAction *act = qobject_cast<QStateAction*>(QObject::sender());
- if (act->isValid()) {
- g_action_group_activate_action(m_actionGroup, act->text().toLatin1(), NULL);
- }
-}
-
-/*! \internal */
-void QDBusActionGroup::removeAction(const char *actionName, bool erase)
+void QDBusActionGroup::clear()
{
- Q_FOREACH(QStateAction *act, m_actions) {
- if (act->text() == actionName) {
- if (erase) {
- m_actions.remove(act);
- delete act;
- } else {
- act->setValid(false);
- }
- break;
- }
+ Q_FOREACH(QStateAction *act, this->findChildren<QStateAction*>()) {
+ Q_EMIT actionVanish(act->text());
}
-}
-/*! \internal */
-void QDBusActionGroup::updateAction(const char *actionName, GVariant *state)
-{
- QStateAction *action = this->action(actionName);
- if ((action != NULL) && (state != NULL)) {
- action->setState(Converter::toGVariant(state));
+ if (m_actionGroup != NULL) {
+ g_object_unref(m_actionGroup);
+ m_actionGroup = NULL;
}
}
/*! \internal */
-void QDBusActionGroup::clear()
+void QDBusActionGroup::updateActionState(const QString &actionName, const QVariant &state)
{
- Q_FOREACH(QAction *act, m_actions) {
- delete act;
- }
- m_actions.clear();
-
if (m_actionGroup != NULL) {
- g_object_unref(m_actionGroup);
- m_actionGroup = NULL;
+ g_action_group_activate_action(m_actionGroup, actionName.toLatin1(), Converter::toGVariant(state));
}
}
@@ -263,19 +202,19 @@ void QDBusActionGroup::clear()
void QDBusActionGroup::onActionAdded(GDBusActionGroup *, gchar *actionName, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
- self->addAction(actionName, true);
+ Q_EMIT self->actionAppear(actionName);
}
/*! \internal */
void QDBusActionGroup::onActionRemoved(GDBusActionGroup *, gchar *actionName, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
- self->removeAction(actionName, false);
+ Q_EMIT self->actionVanish(actionName);
}
/*! \internal */
-void QDBusActionGroup::onActionStateChanged(GDBusActionGroup *ag, gchar *actionName, GVariant *value, gpointer data)
+void QDBusActionGroup::onActionStateChanged(GDBusActionGroup *, gchar *actionName, GVariant *value, gpointer data)
{
QDBusActionGroup *self = reinterpret_cast<QDBusActionGroup*>(data);
- self->updateAction(actionName, value);
+ Q_EMIT self->actionStateChanged(actionName, Converter::toQVariant(value));
}
diff --git a/src/common/qdbusactiongroup.h b/src/common/qdbusactiongroup.h
index f2b2033..723094c 100644
--- a/src/common/qdbusactiongroup.h
+++ b/src/common/qdbusactiongroup.h
@@ -35,13 +35,13 @@ class QDBusActionGroup : public QObject, public QDBusObject
Q_PROPERTY(QString busName READ busName WRITE setBusName NOTIFY busNameChanged)
Q_PROPERTY(QString objectPath READ objectPath WRITE setObjectPath NOTIFY objectPathChanged)
Q_PROPERTY(int status READ status NOTIFY statusChanged)
- Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
QDBusActionGroup(QObject *parent=0);
~QDBusActionGroup();
- int count() const;
+ void updateActionState(const QString &actionName, const QVariant &state);
+ bool hasAction(const QString &actionName);
Q_INVOKABLE QStateAction *action(const QString &actionName);
@@ -50,25 +50,20 @@ Q_SIGNALS:
void busNameChanged(const QString &busNameChanged);
void objectPathChanged(const QString &objectPath);
void statusChanged(ConnectionStatus status);
- void countChanged(int count);
-
+ void actionAppear(const QString &actionName);
+ void actionVanish(const QString &actionName);
+ void actionStateChanged(const QString &actionName, QVariant state);
public Q_SLOTS:
void start();
void stop();
-
protected:
virtual void serviceAppear(GDBusConnection *connection);
virtual void serviceVanish(GDBusConnection *connection);
-
-private Q_SLOTS:
- void onActionTriggered();
-
private:
GActionGroup *m_actionGroup;
- QSet<QStateAction*> m_actions;
int m_signalActionAddId;
int m_signalActionRemovedId;
int m_signalStateChangedId;
@@ -77,12 +72,10 @@ private:
void setIntBusType(int busType);
void setActionGroup(GDBusActionGroup *ag);
- QStateAction *addAction(const char *actionName, bool create);
- void removeAction(const char *actionName, bool erase);
- void updateAction(const char *actionName, GVariant *state);
- void clear();
QStateAction *actionImpl(const QString &actionName);
+ void clear();
+
// glib slots
static void onActionAdded(GDBusActionGroup *ag, gchar *actionName, gpointer data);
static void onActionRemoved(GDBusActionGroup *ag, gchar *actionName, gpointer data);
diff --git a/src/common/qmenumodel.cpp b/src/common/qmenumodel.cpp
index f8d71ba..cc760c1 100644
--- a/src/common/qmenumodel.cpp
+++ b/src/common/qmenumodel.cpp
@@ -202,7 +202,7 @@ QVariant QMenuModel::getExtraProperties(const QModelIndex &index) const
while (g_menu_attribute_iter_get_next (iter, &attrName, &value)) {
if (strncmp("x-", attrName, 2) == 0) {
extra.insert(parseExtraPropertyName(attrName),
- Converter::toGVariant(value));
+ Converter::toQVariant(value));
}
}
diff --git a/src/common/qstateaction.cpp b/src/common/qstateaction.cpp
index 8e3c7c3..1369bfc 100644
--- a/src/common/qstateaction.cpp
+++ b/src/common/qstateaction.cpp
@@ -19,6 +19,8 @@
#include "qstateaction.h"
+#include "qdbusactiongroup.h"
+
/*!
\qmlclass QStateAction
\inherits QAction
@@ -44,9 +46,20 @@
*/
/*! \internal */
-QStateAction::QStateAction(const QString &text, QObject *parent)
- :QAction(text, parent)
+QStateAction::QStateAction(QDBusActionGroup *group, const QString &name)
+ : QAction(name, group),
+ m_group(group)
{
+ QObject::connect(this, SIGNAL(triggered()), this, SLOT(onTriggered()));
+
+ // This keep the code clean
+ // But maybe we need move the action state control to QActionGroup to optimizations
+ QObject::connect(m_group, SIGNAL(actionAppear(QString)),
+ this, SLOT(onActionAppear(QString)));
+ QObject::connect(m_group, SIGNAL(actionVanish(QString)),
+ this, SLOT(onActionVanish(QString)));
+ QObject::connect(m_group, SIGNAL(actionStateUpdated(QString,QVariant)),
+ this, SLOT(onActionStateUpdate(QString,QVariant)));
}
/*!
@@ -68,6 +81,11 @@ bool QStateAction::isValid() const
return m_valid;
}
+void QStateAction::updateState(const QVariant &state)
+{
+ m_group->updateActionState(text(), state);
+}
+
/*! \internal */
void QStateAction::setValid(bool valid)
{
@@ -85,3 +103,33 @@ void QStateAction::setState(const QVariant &state)
Q_EMIT stateChanged(m_state);
}
}
+
+/*! \internal */
+void QStateAction::onTriggered()
+{
+ updateState(QVariant());
+}
+
+/*! \internal */
+void QStateAction::onActionAppear(const QString &actionName)
+{
+ if (text() == actionName) {
+ setValid(true);
+ }
+}
+
+/*! \internal */
+void QStateAction::onActionVanish(const QString &actionName)
+{
+ if (text() == actionName) {
+ setValid(false);
+ }
+}
+
+/*! \internal */
+void QStateAction::onActionStateUpdate(const QString &actionName, const QVariant &state)
+{
+ if (text() == actionName) {
+ setState(state);
+ }
+}
diff --git a/src/common/qstateaction.h b/src/common/qstateaction.h
index 4019c64..c6bdcea 100644
--- a/src/common/qstateaction.h
+++ b/src/common/qstateaction.h
@@ -23,28 +23,37 @@
#include <QAction>
#include <QVariant>
+class QDBusActionGroup;
+
class QStateAction : public QAction
{
Q_OBJECT
Q_PROPERTY(QVariant state READ state WRITE setState NOTIFY stateChanged)
Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
-public:
- QStateAction(const QString &text="", QObject *parent=0);
-
- QVariant state() const;
- void setState(const QVariant &state);
-
+public:
+ QVariant state() const;
bool isValid() const;
+ Q_INVOKABLE void updateState(const QVariant &state);
+
Q_SIGNALS:
void stateChanged(QVariant state);
void validChanged(bool valid);
+private Q_SLOTS:
+ void onActionAppear(const QString &actionName);
+ void onActionVanish(const QString &actionName);
+ void onActionStateUpdate(const QString &actionNane, const QVariant &state);
+ void onTriggered();
+
private:
- QVariant m_state;
+ QDBusActionGroup *m_group;
+ QVariant m_state;
bool m_valid;
+ QStateAction(QDBusActionGroup *group, const QString &name);
void setValid(bool valid);
+ void setState(const QVariant &state);
friend class QDBusActionGroup;
};