aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel
diff options
context:
space:
mode:
authorLars Uebernickel <lars.uebernickel@canonical.com>2013-09-04 15:33:11 +0000
committerTarmac <>2013-09-04 15:33:11 +0000
commitce35aa4f68b0c35a75b92c55eefb7e6ffb4d88e6 (patch)
tree5f28842dc67709fb0738c58df9136e31d73f31fa /libqmenumodel
parent28a454801c39295dc8e87e012c5f209ba07943f3 (diff)
parente01b6b5a513c57c7a52c1a14d4a44dcf6a42025d (diff)
downloadqmenumodel-ce35aa4f68b0c35a75b92c55eefb7e6ffb4d88e6.tar.gz
qmenumodel-ce35aa4f68b0c35a75b92c55eefb7e6ffb4d88e6.tar.bz2
qmenumodel-ce35aa4f68b0c35a75b92c55eefb7e6ffb4d88e6.zip
Use fully namespaced action name in UnityMenuModel::activate and for the action role.
Approved by Nick Dedekind, PS Jenkins bot.
Diffstat (limited to 'libqmenumodel')
-rw-r--r--libqmenumodel/src/gtk/gtkmenutrackeritem.c25
-rw-r--r--libqmenumodel/src/gtk/gtkmenutrackeritem.h4
-rw-r--r--libqmenumodel/src/unitymenuaction.cpp16
-rw-r--r--libqmenumodel/src/unitymenuaction.h7
-rw-r--r--libqmenumodel/src/unitymenumodel.cpp87
-rw-r--r--libqmenumodel/src/unitymenumodel.h1
6 files changed, 113 insertions, 27 deletions
diff --git a/libqmenumodel/src/gtk/gtkmenutrackeritem.c b/libqmenumodel/src/gtk/gtkmenutrackeritem.c
index e2ed1f0..34e168a 100644
--- a/libqmenumodel/src/gtk/gtkmenutrackeritem.c
+++ b/libqmenumodel/src/gtk/gtkmenutrackeritem.c
@@ -181,7 +181,7 @@ gtk_menu_tracker_item_get_property (GObject *object,
g_value_set_boolean (value, gtk_menu_tracker_item_get_submenu_shown (self));
break;
case PROP_ACTION_NAME:
- g_value_set_string (value, gtk_menu_tracker_item_get_action_name (self));
+ g_value_take_string (value, gtk_menu_tracker_item_get_action_name (self));
case PROP_ACTION_STATE:
g_value_set_variant (value, self->action_state);
break;
@@ -621,16 +621,23 @@ gtk_menu_tracker_item_get_submenu_shown (GtkMenuTrackerItem *self)
* gtk_menu_tracker_item_get_action_name:
* @self: A #GtkMenuTrackerItem instance
*
- * Returns the action name
+ * Returns a newly-allocated string containing the name of the action
+ * associated with this menu item.
+ *
+ * Returns: (transfer full): the action name, free it with g_free()
*/
-const gchar *
+gchar *
gtk_menu_tracker_item_get_action_name (GtkMenuTrackerItem *self)
{
- const gchar *action_name = NULL;
+ const gchar *action_name;
- g_menu_item_get_attribute (self->item, G_MENU_ATTRIBUTE_ACTION, "&s", &action_name);
+ if (!g_menu_item_get_attribute (self->item, G_MENU_ATTRIBUTE_ACTION, "&s", &action_name))
+ return NULL;
- return action_name;
+ if (self->action_namespace)
+ return g_strjoin (".", self->action_namespace, action_name, NULL);
+ else
+ return g_strdup (action_name);
}
GVariant *
@@ -642,6 +649,12 @@ gtk_menu_tracker_item_get_action_state (GtkMenuTrackerItem *self)
return NULL;
}
+const gchar *
+gtk_menu_tracker_item_get_action_namespace (GtkMenuTrackerItem *self)
+{
+ return self->action_namespace;
+}
+
static void
gtk_menu_tracker_item_set_submenu_shown (GtkMenuTrackerItem *self,
gboolean submenu_shown)
diff --git a/libqmenumodel/src/gtk/gtkmenutrackeritem.h b/libqmenumodel/src/gtk/gtkmenutrackeritem.h
index 59a7080..dc62bf1 100644
--- a/libqmenumodel/src/gtk/gtkmenutrackeritem.h
+++ b/libqmenumodel/src/gtk/gtkmenutrackeritem.h
@@ -87,10 +87,12 @@ void gtk_menu_tracker_item_request_submenu_shown (GtkMenu
gboolean gtk_menu_tracker_item_get_submenu_shown (GtkMenuTrackerItem *self);
-const gchar * gtk_menu_tracker_item_get_action_name (GtkMenuTrackerItem *self);
+gchar * gtk_menu_tracker_item_get_action_name (GtkMenuTrackerItem *self);
GVariant * gtk_menu_tracker_item_get_action_state (GtkMenuTrackerItem *self);
+const gchar * gtk_menu_tracker_item_get_action_namespace (GtkMenuTrackerItem *self);
+
gboolean gtk_menu_tracker_item_get_attribute (GtkMenuTrackerItem *self,
const gchar *attribute,
const gchar *format,
diff --git a/libqmenumodel/src/unitymenuaction.cpp b/libqmenumodel/src/unitymenuaction.cpp
index b41fd3e..46edf16 100644
--- a/libqmenumodel/src/unitymenuaction.cpp
+++ b/libqmenumodel/src/unitymenuaction.cpp
@@ -26,7 +26,8 @@ UnityMenuAction::UnityMenuAction(QObject* parent)
: QObject(parent),
m_valid(false),
m_enabled(false),
- m_model(NULL)
+ m_model(NULL),
+ m_index(-1)
{
}
@@ -106,6 +107,19 @@ void UnityMenuAction::setValid(bool valid)
}
}
+int UnityMenuAction::index() const
+{
+ return m_index;
+}
+
+void UnityMenuAction::setIndex(int i)
+{
+ if (i != m_index) {
+ m_index = i;
+ Q_EMIT indexChanged(m_index);
+ }
+}
+
void UnityMenuAction::registerAction()
{
if (m_model) {
diff --git a/libqmenumodel/src/unitymenuaction.h b/libqmenumodel/src/unitymenuaction.h
index b8391fe..7479d2c 100644
--- a/libqmenumodel/src/unitymenuaction.h
+++ b/libqmenumodel/src/unitymenuaction.h
@@ -32,6 +32,8 @@ class UnityMenuAction: public QObject
Q_PROPERTY(bool enabled READ isEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
Q_PROPERTY(UnityMenuModel* model READ model WRITE setModel NOTIFY modelChanged)
+ Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged)
+
public:
UnityMenuAction(QObject* parent = 0);
~UnityMenuAction();
@@ -42,6 +44,9 @@ public:
UnityMenuModel* model() const;
void setModel(UnityMenuModel* model);
+ int index() const;
+ void setIndex(int i);
+
QVariant state() const;
bool isEnabled() const;
bool isValid() const;
@@ -55,6 +60,7 @@ Q_SIGNALS:
void stateChanged(const QVariant& name);
void enabledChanged(bool enabled);
void validChanged(bool valid);
+ void indexChanged(int index);
protected:
virtual bool event(QEvent* e);
@@ -72,6 +78,7 @@ private:
bool m_valid;
bool m_enabled;
UnityMenuModel* m_model;
+ int m_index;
};
#endif // UNITYMENUACTIONGROUP_H
diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp
index d457cad..a0ab4ff 100644
--- a/libqmenumodel/src/unitymenumodel.cpp
+++ b/libqmenumodel/src/unitymenumodel.cpp
@@ -93,6 +93,9 @@ public:
static void registeredActionEnabledChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, gboolean enabled);
static void registeredActionStateChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, GVariant *state);
static void registeredActionRemoved(GtkSimpleActionObserver *observer_item, const gchar *action_name);
+
+ gchar * fullActionName(UnityMenuAction *action);
+ void updateRegisteredAction(UnityMenuAction *action);
};
void menu_item_free (gpointer data)
@@ -432,8 +435,12 @@ QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
return map ? *map : QVariant();
}
- case ActionRole:
- return gtk_menu_tracker_item_get_action_name (item);
+ case ActionRole: {
+ gchar *action_name = gtk_menu_tracker_item_get_action_name (item);
+ QString v(action_name);
+ g_free(action_name);
+ return v;
+ }
case ActionStateRole:
return priv->itemState(item);
@@ -646,7 +653,7 @@ void UnityMenuModel::activate(int index, const QVariant& parameter)
if (parameter.isValid()) {
gchar *action;
- gtk_menu_tracker_item_get_attribute (item, "action", "s", &action);
+ action = gtk_menu_tracker_item_get_action_name (item);
g_action_group_activate_action (G_ACTION_GROUP (priv->muxer), action, Converter::toGVariant(parameter));
g_free (action);
@@ -751,6 +758,7 @@ void UnityMenuModel::registerAction(UnityMenuAction* action)
priv->registeredActions[action] = observer_item;
connect(action, SIGNAL(nameChanged(const QString&)), SLOT(onRegisteredActionNameChanged(const QString&)));
+ connect(action, SIGNAL(indexChanged(int)), SLOT(onRegisteredActionIndexChanged(int)));
connect(action, SIGNAL(activate(const QVariant&)), SLOT(onRegisteredActionActivated(const QVariant&)));
connect(action, SIGNAL(changeState(const QVariant&)), SLOT(onRegisteredActionStateChanged(const QVariant&)));
}
@@ -768,26 +776,53 @@ void UnityMenuModel::unregisterAction(UnityMenuAction* action)
}
}
-void UnityMenuModel::onRegisteredActionNameChanged(const QString& name)
+/* Returns the full name for @action
+ *
+ * If @action is associated with a menu item that is inside of a
+ * section or submenu with "action-namespace" set, this namespace
+ * is prepended to @action->name()
+ */
+char * UnityMenuModelPrivate::fullActionName(UnityMenuAction *action)
{
- UnityMenuAction* action = qobject_cast<UnityMenuAction*>(sender());
- if (!action || !priv->registeredActions.contains(action))
- return;
+ GSequenceIter *iter;
+ QByteArray bytes;
+ const gchar *name;
+ gchar *full_name = NULL;
- GtkSimpleActionObserver* observer_item;
- observer_item = priv->registeredActions[action];
+ bytes = action->name().toUtf8();
+ name = bytes.constData();
- QByteArray nameArray = name.toUtf8();
- const gchar* action_name = nameArray.constData();
+ iter = g_sequence_get_iter_at_pos (this->items, action->index());
+ if (!g_sequence_iter_is_end (iter)) {
+ GtkMenuTrackerItem *item;
+ const gchar *action_namespace;
- gtk_simple_action_observer_register_action (observer_item, action_name);
+ item = (GtkMenuTrackerItem *) g_sequence_get (iter);
+ action_namespace = gtk_menu_tracker_item_get_action_namespace (item);
+ if (action_namespace != NULL)
+ return g_strjoin (".", action_namespace, name, NULL);
+ }
+
+ return g_strdup (name);
+}
- const GVariantType *parameter_type;
+void UnityMenuModelPrivate::updateRegisteredAction(UnityMenuAction *action)
+{
+ GtkSimpleActionObserver *observer_item;
+ gchar *action_name;
gboolean enabled;
GVariant *state;
- if (g_action_group_query_action (G_ACTION_GROUP (priv->muxer), action_name,
- &enabled, &parameter_type, NULL, NULL, &state))
+ if (!action || !this->registeredActions.contains(action))
+ return;
+
+ action_name = fullActionName(action);
+
+ observer_item = this->registeredActions[action];
+ gtk_simple_action_observer_register_action (observer_item, action_name);
+
+ if (g_action_group_query_action (G_ACTION_GROUP (this->muxer), action_name,
+ &enabled, NULL, NULL, NULL, &state))
{
UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state));
QCoreApplication::sendEvent(action, &umaae);
@@ -796,6 +831,18 @@ void UnityMenuModel::onRegisteredActionNameChanged(const QString& name)
g_variant_unref (state);
}
}
+
+ g_free(action_name);
+}
+
+void UnityMenuModel::onRegisteredActionNameChanged(const QString& name)
+{
+ priv->updateRegisteredAction(qobject_cast<UnityMenuAction*>(sender()));
+}
+
+void UnityMenuModel::onRegisteredActionIndexChanged(int index)
+{
+ priv->updateRegisteredAction(qobject_cast<UnityMenuAction*>(sender()));
}
void UnityMenuModel::onRegisteredActionActivated(const QVariant& parameter)
@@ -804,10 +851,11 @@ void UnityMenuModel::onRegisteredActionActivated(const QVariant& parameter)
if (!action || action->name().isEmpty())
return;
- QByteArray nameArray = action->name().toUtf8();
- const gchar* action_name = nameArray.constData();
+ gchar* action_name = priv->fullActionName(action);
g_action_group_activate_action (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter));
+
+ g_free(action_name);
}
void UnityMenuModel::onRegisteredActionStateChanged(const QVariant& parameter)
@@ -816,10 +864,11 @@ void UnityMenuModel::onRegisteredActionStateChanged(const QVariant& parameter)
if (!action || action->name().isEmpty())
return;
- QByteArray nameArray = action->name().toUtf8();
- const gchar* action_name = nameArray.constData();
+ gchar* action_name = priv->fullActionName(action);
g_action_group_change_action_state (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter));
+
+ g_free(action_name);
}
void UnityMenuModelPrivate::registeredActionAdded(GtkSimpleActionObserver *observer_item,
diff --git a/libqmenumodel/src/unitymenumodel.h b/libqmenumodel/src/unitymenumodel.h
index a229f09..30bc62d 100644
--- a/libqmenumodel/src/unitymenumodel.h
+++ b/libqmenumodel/src/unitymenumodel.h
@@ -73,6 +73,7 @@ Q_SIGNALS:
protected Q_SLOTS:
void onRegisteredActionNameChanged(const QString& name);
+ void onRegisteredActionIndexChanged(int);
void onRegisteredActionActivated(const QVariant& parameter);
void onRegisteredActionStateChanged(const QVariant& parameter);