on xenial help sign in
Added cachedunitymenumodel reference counting
54f4d73Nick Dedekind
plugins/Unity/Indicators/cachedunitymenumodel.cpp+11−1
@@ -31,6 +31,11 @@ CachedUnityMenuModel::CachedUnityMenuModel(QObject* parent)
31
31
32CachedUnityMenuModel::~CachedUnityMenuModel()
32CachedUnityMenuModel::~CachedUnityMenuModel()
33{
33{
34+ // deref old model.
35+ if (m_model) {
36+ UnityMenuModelCache::cache()->deref(m_menuObjectPath);
37+ m_model = NULL;
38+ }
34}
39}
35
40
36UnityMenuModel* CachedUnityMenuModel::model() const
41UnityMenuModel* CachedUnityMenuModel::model() const
@@ -80,7 +85,11 @@ QString CachedUnityMenuModel::menuObjectPath() const
80void CachedUnityMenuModel::setMenuObjectPath(const QString &path)
85void CachedUnityMenuModel::setMenuObjectPath(const QString &path)
81{
86{
82 if (m_menuObjectPath != path) {
87 if (m_menuObjectPath != path) {
88+ // deref old model.
89+ if (m_model) {
90+ UnityMenuModelCache::cache()->deref(m_menuObjectPath);
83− m_model = NULL;
91+ m_model = NULL;
92+ }
84
93
85 m_menuObjectPath = path;
94 m_menuObjectPath = path;
86 Q_EMIT menuObjectPathChanged(m_menuObjectPath);
95 Q_EMIT menuObjectPathChanged(m_menuObjectPath);
@@ -96,6 +105,7 @@ void CachedUnityMenuModel::setMenuObjectPath(const QString &path)
96
105
97 UnityMenuModelCache::cache()->registerModel(m_menuObjectPath, m_model);
106 UnityMenuModelCache::cache()->registerModel(m_menuObjectPath, m_model);
98 }
107 }
108+ UnityMenuModelCache::cache()->ref(m_menuObjectPath);
99 }
109 }
100
110
101 Q_EMIT modelChanged(m_model);
111 Q_EMIT modelChanged(m_model);
plugins/Unity/Indicators/unitymenumodelcache.cpp+22
@@ -46,3 +46,25 @@ UnityMenuModelCache* UnityMenuModelCache::cache()
46 static UnityMenuModelCache* model = new UnityMenuModelCache;
46 static UnityMenuModelCache* model = new UnityMenuModelCache;
47 return model;
47 return model;
48}
48}
49+
50+void UnityMenuModelCache::ref(const QString& path)
51+{
52+ if (!m_refs.contains(path)) {
53+ m_refs[path] = 1;
54+ } else {
55+ m_refs[path]++;
56+ }
57+}
58+
59+bool UnityMenuModelCache::deref(const QString& path)
60+{
61+ if (m_refs.contains(path)) {
62+ if (--m_refs[path] <= 0) {
63+ delete m_menuModels.value(path, NULL);
64+ m_menuModels.remove(path);
65+ m_refs.remove(path);
66+ return false;
67+ }
68+ }
69+ return true;
70+}
plugins/Unity/Indicators/unitymenumodelcache.h+4
@@ -37,10 +37,14 @@ public:
37 Q_INVOKABLE UnityMenuModel* model(const QString& path) const;
37 Q_INVOKABLE UnityMenuModel* model(const QString& path) const;
38 Q_INVOKABLE void registerModel(const QString& path, UnityMenuModel* menuModel);
38 Q_INVOKABLE void registerModel(const QString& path, UnityMenuModel* menuModel);
39
39
40+ void ref(const QString& path);
41+ bool deref(const QString& path);
42+
40 static UnityMenuModelCache* cache();
43 static UnityMenuModelCache* cache();
41
44
42private:
45private:
43 QHash<QString, UnityMenuModel*> m_menuModels;
46 QHash<QString, UnityMenuModel*> m_menuModels;
47+ QHash<QString, int> m_refs;
44};
48};
45
49
46#endif // UNITYMENUMODELCACHE_H
50#endif // UNITYMENUMODELCACHE_H