From ebe349e1d96b28e31c29b3e563da1a65fb176e39 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Fri, 14 Sep 2012 16:11:28 -0300 Subject: Created function to convert from QVariant to GVariant. Moved QStateAction controler from QACtionGroup. --- src/QMenuModel/plugin.cpp | 5 +- src/common/converter.cpp | 53 +++++++++++++++++- src/common/converter.h | 5 +- src/common/qdbusactiongroup.cpp | 121 ++++++++++------------------------------ src/common/qdbusactiongroup.h | 21 +++---- src/common/qmenumodel.cpp | 2 +- src/common/qstateaction.cpp | 52 ++++++++++++++++- src/common/qstateaction.h | 23 +++++--- 8 files changed, 164 insertions(+), 118 deletions(-) (limited to 'src') 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(uri, 0, 1, "QMenuModel", "QMenuModel is a interface"); + qmlRegisterUncreatableType(uri, 0, 1, "QStateAction", + "QStateAction must be created by QDBusActionGroup::action"); + qmlRegisterType(uri, 0, 1, "QDBusMenuModel"); qmlRegisterType(uri, 0, 1, "QDBusActionGroup"); - qmlRegisterType(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 /*! \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()); + } else if (typeName == "short") { + return g_variant_new_int16(value.value()); + } else if (typeName == "ushort") { + return g_variant_new_uint16(value.value()); + } else if (typeName == "long") { + return g_variant_new_int64(value.value()); + } else if (typeName == "ulong") { + return g_variant_new_uint64(value.value()); + } 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()) { 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(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(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()) { + 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(data); - self->addAction(actionName, true); + Q_EMIT self->actionAppear(actionName); } /*! \internal */ void QDBusActionGroup::onActionRemoved(GDBusActionGroup *, gchar *actionName, gpointer data) { QDBusActionGroup *self = reinterpret_cast(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(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 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 #include +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; }; -- cgit v1.2.3