From 6078bd7ddc6819d2650435313bb824442bbe033d Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Fri, 23 Nov 2012 12:03:07 -0300 Subject: Fixed QMenumodel behaviour when the GMenuModel is destroyed. --- libqmenumodel/src/qdbusmenumodel.cpp | 6 +++++- libqmenumodel/src/qmenumodel.cpp | 19 ++++++++++++++++++- libqmenumodel/src/qmenumodel.h | 3 +++ 3 files changed, 26 insertions(+), 2 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index 97990b4..070381f 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -89,7 +89,11 @@ void QDBusMenuModel::stop() /*! \internal */ void QDBusMenuModel::serviceVanish(GDBusConnection *) { - setMenuModel(NULL); + GMenuModel *model = menuModel(); + if (model != NULL) { + setMenuModel(NULL); + g_object_unref(model); + } } /*! \internal */ diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 8735e6c..a45d647 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -56,7 +56,11 @@ void QMenuModel::setMenuModel(GMenuModel *other) if (m_menuModel == other) { return; } + setMenuModelImpl(other); +} +void QMenuModel::setMenuModelImpl(GMenuModel *other) +{ beginResetModel(); clearModel(); @@ -64,6 +68,9 @@ void QMenuModel::setMenuModel(GMenuModel *other) m_menuModel = other; if (m_menuModel) { + g_object_weak_ref(reinterpret_cast(m_menuModel), + reinterpret_cast(QMenuModel::onGMenuModelDestroyed), + this); // this will trigger the menu load (void) g_menu_model_get_n_items(m_menuModel); m_signalChangedId = g_signal_connect(m_menuModel, @@ -85,9 +92,11 @@ GMenuModel *QMenuModel::menuModel() const void QMenuModel::clearModel() { if (m_menuModel) { + g_object_weak_unref(reinterpret_cast(m_menuModel), + reinterpret_cast(QMenuModel::onGMenuModelDestroyed), + this); g_signal_handler_disconnect(m_menuModel, m_signalChangedId); m_signalChangedId = 0; - g_object_unref(m_menuModel); m_menuModel = NULL; } @@ -223,6 +232,14 @@ QVariant QMenuModel::getExtraProperties(const QModelIndex &index) const return extra; } +/*! \internal */ +void QMenuModel::onGMenuModelDestroyed(gpointer data, + GObject *oldObject) +{ + QMenuModel *self = reinterpret_cast(data); + self->m_menuModel = NULL; + self->setMenuModelImpl(NULL); +} /*! \internal */ void QMenuModel::onItemsChanged(GMenuModel *, diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index beec7ba..f3b431c 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -26,6 +26,7 @@ typedef int gint; typedef unsigned int guint; typedef void* gpointer; typedef struct _GMenuModel GMenuModel; +typedef struct _GObject GObject; class QMenuModel : public QAbstractListModel { @@ -62,8 +63,10 @@ private: QVariant getExtraProperties(const QModelIndex &index) const; QString parseExtraPropertyName(const QString &name) const; void clearModel(); + void setMenuModelImpl(GMenuModel *model); static void onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data); + static void onGMenuModelDestroyed(gpointer data, GObject *oldObject); }; #endif -- cgit v1.2.3