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/src') 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 From af103ebfb8945c903accb42944516d234fe5744c Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Fri, 23 Nov 2012 14:25:39 -0300 Subject: Replaced 'reinterpret_cast' for gobject cast when possible. --- libqmenumodel/src/qdbusmenumodel.cpp | 6 +++--- libqmenumodel/src/qmenumodel.cpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'libqmenumodel/src') diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index 070381f..d3369dd 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -99,9 +99,9 @@ void QDBusMenuModel::serviceVanish(GDBusConnection *) /*! \internal */ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) { - GMenuModel *model = reinterpret_cast(g_dbus_menu_model_get(connection, - busName().toUtf8().data(), - objectPath().toUtf8().data())); + GMenuModel *model = G_MENU_MODEL(g_dbus_menu_model_get(connection, + busName().toUtf8().data(), + objectPath().toUtf8().data())); setMenuModel(model); if (model == NULL) { stop(); diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index a45d647..3ae2e3e 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -68,7 +68,7 @@ void QMenuModel::setMenuModelImpl(GMenuModel *other) m_menuModel = other; if (m_menuModel) { - g_object_weak_ref(reinterpret_cast(m_menuModel), + g_object_weak_ref(G_OBJECT(m_menuModel), reinterpret_cast(QMenuModel::onGMenuModelDestroyed), this); // this will trigger the menu load @@ -92,7 +92,7 @@ GMenuModel *QMenuModel::menuModel() const void QMenuModel::clearModel() { if (m_menuModel) { - g_object_weak_unref(reinterpret_cast(m_menuModel), + g_object_weak_unref(G_OBJECT(m_menuModel), reinterpret_cast(QMenuModel::onGMenuModelDestroyed), this); g_signal_handler_disconnect(m_menuModel, m_signalChangedId); -- cgit v1.2.3 From aa56c7b79317fb88c12fc0573fc23b6939337fe4 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Fri, 23 Nov 2012 14:41:21 -0300 Subject: Take care of object ref internally on QMenumodel, instead of QDBusMenuModel. --- libqmenumodel/src/qdbusmenumodel.cpp | 9 ++++----- libqmenumodel/src/qmenumodel.cpp | 22 +++------------------- libqmenumodel/src/qmenumodel.h | 1 - 3 files changed, 7 insertions(+), 25 deletions(-) (limited to 'libqmenumodel/src') diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index d3369dd..abf259f 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -89,11 +89,7 @@ void QDBusMenuModel::stop() /*! \internal */ void QDBusMenuModel::serviceVanish(GDBusConnection *) { - GMenuModel *model = menuModel(); - if (model != NULL) { - setMenuModel(NULL); - g_object_unref(model); - } + setMenuModel(NULL); } /*! \internal */ @@ -105,6 +101,9 @@ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) setMenuModel(model); if (model == NULL) { stop(); + } else { + //setModel take care of the ref + g_object_unref(model); } } diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 3ae2e3e..ec5570f 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -56,11 +56,7 @@ void QMenuModel::setMenuModel(GMenuModel *other) if (m_menuModel == other) { return; } - setMenuModelImpl(other); -} -void QMenuModel::setMenuModelImpl(GMenuModel *other) -{ beginResetModel(); clearModel(); @@ -68,9 +64,7 @@ void QMenuModel::setMenuModelImpl(GMenuModel *other) m_menuModel = other; if (m_menuModel) { - g_object_weak_ref(G_OBJECT(m_menuModel), - reinterpret_cast(QMenuModel::onGMenuModelDestroyed), - this); + g_object_ref(m_menuModel); // this will trigger the menu load (void) g_menu_model_get_n_items(m_menuModel); m_signalChangedId = g_signal_connect(m_menuModel, @@ -92,11 +86,9 @@ GMenuModel *QMenuModel::menuModel() const void QMenuModel::clearModel() { if (m_menuModel) { - g_object_weak_unref(G_OBJECT(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; } @@ -194,7 +186,7 @@ QVariant QMenuModel::getLink(const QModelIndex &index, index.row(), linkName.toUtf8().data()); - if (link) { + if (link) { QMenuModel *other = new QMenuModel(link, const_cast(this)); return QVariant::fromValue(other); } @@ -232,14 +224,6 @@ 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 f3b431c..b3ef460 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -66,7 +66,6 @@ private: 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 From bdc349d16b6e88c64e6ad16513f33d71bf1a643d Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Fri, 23 Nov 2012 14:56:22 -0300 Subject: Removed missing function declaration. --- libqmenumodel/src/qdbusmenumodel.cpp | 8 ++------ libqmenumodel/src/qmenumodel.h | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) (limited to 'libqmenumodel/src') diff --git a/libqmenumodel/src/qdbusmenumodel.cpp b/libqmenumodel/src/qdbusmenumodel.cpp index abf259f..b404d0b 100644 --- a/libqmenumodel/src/qdbusmenumodel.cpp +++ b/libqmenumodel/src/qdbusmenumodel.cpp @@ -99,12 +99,8 @@ void QDBusMenuModel::serviceAppear(GDBusConnection *connection) busName().toUtf8().data(), objectPath().toUtf8().data())); setMenuModel(model); - if (model == NULL) { - stop(); - } else { - //setModel take care of the ref - g_object_unref(model); - } + //setModel take care of the ref + g_object_unref(model); } /*! \internal */ diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index b3ef460..1ab1e7a 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -63,7 +63,6 @@ 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); }; -- cgit v1.2.3