aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Uebernickel <lars.uebernickel@canonical.com>2013-06-24 13:09:43 -0400
committerLars Uebernickel <lars.uebernickel@canonical.com>2013-06-24 13:09:43 -0400
commit1bc24273997f94cae871613dd655ab548f97565c (patch)
treeaee1e36cbc5fc31c831f56f3d7acf0fc7aa79475
parent7b0620547a3a42c3aefa926312cc9d06b87e817c (diff)
downloadqmenumodel-1bc24273997f94cae871613dd655ab548f97565c.tar.gz
qmenumodel-1bc24273997f94cae871613dd655ab548f97565c.tar.bz2
qmenumodel-1bc24273997f94cae871613dd655ab548f97565c.zip
unitymenumodel: set free func on GSequence
Makes item removal code much cleaner (no need for the explicit free), at the cost of another qdata.
-rw-r--r--libqmenumodel/src/unitymenumodel.cpp39
1 files changed, 10 insertions, 29 deletions
diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp
index 525e115..58b9fda 100644
--- a/libqmenumodel/src/unitymenumodel.cpp
+++ b/libqmenumodel/src/unitymenumodel.cpp
@@ -25,6 +25,7 @@ extern "C" {
G_DEFINE_QUARK (UNITY_MENU_MODEL, unity_menu_model)
G_DEFINE_QUARK (UNITY_SUBMENU_MODEL, unity_submenu_model)
+G_DEFINE_QUARK (UNITY_MENU_ITEM_ITERATOR, unity_menu_item_iterator)
enum MenuRoles {
ActionRole = Qt::DisplayRole + 1,
@@ -55,7 +56,6 @@ public:
QByteArray actionObjectPath;
QByteArray menuObjectPath;
- static void freeMenuItem(gpointer data, gpointer user_data);
static void nameAppeared(GDBusConnection *connection, const gchar *name, const gchar *owner, gpointer user_data);
static void nameVanished(GDBusConnection *connection, const gchar *name, gpointer user_data);
static void menuItemInserted(GtkMenuTrackerItem *item, gint position, gpointer user_data);
@@ -63,20 +63,12 @@ public:
static void menuItemChanged(GObject *object, GParamSpec *pspec, gpointer user_data);
};
-/*
- * Same as g_sequence_foreach_range, but calls func with the GSequenceIter
- * instead of the item.
- */
-static void
-g_sequence_foreach_iter_range (GSequenceIter *begin,
- GSequenceIter *end,
- GFunc func,
- gpointer user_data)
+void menu_item_free (gpointer data)
{
- GSequenceIter *it;
+ GtkMenuTrackerItem *item = (GtkMenuTrackerItem *) data;
- for (it = begin; it != end; it = g_sequence_iter_next (it))
- func (it, user_data);
+ g_signal_handlers_disconnect_by_func (item, (gpointer) UnityMenuModelPrivate::menuItemChanged, NULL);
+ g_object_unref (item);
}
UnityMenuModelPrivate::UnityMenuModelPrivate(UnityMenuModel *model)
@@ -89,7 +81,7 @@ UnityMenuModelPrivate::UnityMenuModelPrivate(UnityMenuModel *model)
this->muxer = gtk_action_muxer_new ();
g_object_set_qdata (G_OBJECT (this->muxer), unity_menu_model_quark (), model);
- this->items = g_sequence_new (NULL);
+ this->items = g_sequence_new (menu_item_free);
}
UnityMenuModelPrivate::~UnityMenuModelPrivate()
@@ -104,16 +96,6 @@ UnityMenuModelPrivate::~UnityMenuModelPrivate()
g_bus_unwatch_name (this->nameWatchId);
}
-void UnityMenuModelPrivate::freeMenuItem (gpointer data, gpointer user_data)
-{
- GSequenceIter *it = (GSequenceIter *) data;
- GtkMenuTrackerItem *item;
-
- item = (GtkMenuTrackerItem *) g_sequence_get (it);
- g_signal_handlers_disconnect_by_func (item, (gpointer) menuItemChanged, it);
- g_object_unref (item);
-}
-
void UnityMenuModelPrivate::clearItems(bool resetModel)
{
GSequenceIter *begin;
@@ -124,7 +106,6 @@ void UnityMenuModelPrivate::clearItems(bool resetModel)
begin = g_sequence_get_begin_iter (this->items);
end = g_sequence_get_end_iter (this->items);
- g_sequence_foreach_iter_range (begin, end, freeMenuItem, NULL);
g_sequence_remove_range (begin, end);
if (resetModel)
@@ -199,6 +180,7 @@ void UnityMenuModelPrivate::menuItemInserted(GtkMenuTrackerItem *item, gint posi
priv->model->beginInsertRows(QModelIndex(), position, position);
it = g_sequence_get_iter_at_pos (priv->items, position);
+ g_object_set_qdata (G_OBJECT (item), unity_menu_item_iterator_quark (), it);
g_signal_connect (item, "notify", G_CALLBACK (menuItemChanged), it);
g_sequence_insert_before (it, g_object_ref (item));
@@ -212,21 +194,20 @@ void UnityMenuModelPrivate::menuItemRemoved(gint position, gpointer user_data)
priv->model->beginRemoveRows(QModelIndex(), position, position);
- it = g_sequence_get_iter_at_pos (priv->items, position);
- freeMenuItem ((gpointer) it, NULL);
- g_sequence_remove (it);
+ g_sequence_remove (g_sequence_get_iter_at_pos (priv->items, position));
priv->model->endRemoveRows();
}
void UnityMenuModelPrivate::menuItemChanged(GObject *object, GParamSpec *pspec, gpointer user_data)
{
- GSequenceIter *it = (GSequenceIter *) user_data;
+ GSequenceIter *it;
GtkMenuTrackerItem *item;
GtkActionObservable *muxer;
UnityMenuModel *model;
gint position;
+ it = (GSequenceIter *) g_object_get_qdata (object, unity_menu_item_iterator_quark ());
item = (GtkMenuTrackerItem *) g_sequence_get (it);
muxer = _gtk_menu_tracker_item_get_observable (item);
model = (UnityMenuModel *) g_object_get_qdata (G_OBJECT (muxer), unity_menu_model_quark ());