@@ -21,15 +21,23 @@
2121#include "categories.h"
2222#include "categoryresults.h"
2323
24+// TODO: use something from libunity once it's public
25+enum CategoryColumn {
26+ ID,
27+ DISPLAY_NAME,
28+ ICON_HINT,
29+ RENDERER_NAME,
30+ HINTS
31+};
32+
2433Categories::Categories(QObject* parent)
2534 : DeeListModel(parent)
2635{
27− // FIXME: need to clean up unused filters on countChanged
2836 m_roles[Categories::RoleId] = "id";
2937 m_roles[Categories::RoleName] = "name";
3038 m_roles[Categories::RoleIcon] = "icon";
3139 m_roles[Categories::RoleRenderer] = "renderer";
32− m_roles[Categories::RoleContentType] = "content_type";
40+ m_roles[Categories::RoleContentType] = "contentType";
3341 m_roles[Categories::RoleHints] = "hints";
3442 m_roles[Categories::RoleResults] = "results";
3543 m_roles[Categories::RoleCount] = "count";
@@ -39,23 +47,19 @@ Categories::Categories(QObject* parent)
3947 // change of the search term harder to reproduce
4048 m_timer.setSingleShot(true);
4149 m_timer.setInterval(50);
42− connect(&m_timer, SIGNAL(timeout()), this, SLOT(onEmitCountChanged()));
43−}
44−
45−Categories::~Categories()
46−{
50+ connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged);
4751}
4852
4953DeeListModel*
50−Categories::getFilter(int index) const
54+Categories::getResults(int index) const
5155{
52− if (!m_results.contains(index) || m_results[index].isNull()) {
53− auto results = new CategoryResults;
56+ if (!m_results.contains(index)) {
57+ CategoryResults* results = new CategoryResults(const_cast<Categories*>(this));
5458 results->setCategoryIndex(index);
5559 connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged);
5660
57− unsigned cat_index = static_cast<unsigned>(index);
58− auto unity_results = m_unityScope->GetResultsForCategory(cat_index);
61+ unsigned categoryIndex = static_cast<unsigned>(index);
62+ auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex);
5963 results->setModel(unity_results->model());
6064
6165 m_results.insert(index, results);
@@ -68,6 +72,9 @@ void Categories::onCategoriesModelChanged(unity::glib::Object<DeeModel> model)
6872{
6973 m_updatedCategories.clear();
7074 // FIXME: this might destroy the renderer view and re-create it, optimize?
75+ Q_FOREACH(DeeListModel* model, m_results) {
76+ delete model;
77+ }
7178 m_results.clear();
7279 setModel(model);
7380}
@@ -80,7 +87,9 @@ Categories::setUnityScope(const unity::dash::Scope::Ptr& scope)
8087 // no need to call this, we'll get notified
8188 //setModel(m_unityScope->categories()->model());
8289
83− m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
90+ m_categoriesChangedConnection.disconnect();
91+ m_categoriesChangedConnection =
92+ m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged));
8493}
8594
8695void
@@ -98,9 +107,9 @@ Categories::onEmitCountChanged()
98107{
99108 QVector<int> roles;
100109 roles.append(Categories::RoleCount);
101− Q_FOREACH(int cat_index, m_updatedCategories) {
102− if (m_results[cat_index].isNull()) continue;
103− QModelIndex changedIndex = index(cat_index);
110+ Q_FOREACH(int categoryIndex, m_updatedCategories) {
111+ if (!m_results.contains(categoryIndex)) continue;
112+ QModelIndex changedIndex = index(categoryIndex);
104113 Q_EMIT dataChanged(changedIndex, changedIndex, roles);
105114 }
106115 m_updatedCategories.clear();
@@ -123,22 +132,22 @@ Categories::data(const QModelIndex& index, int role) const
123132 case RoleId:
124133 return QVariant::fromValue(index.row());
125134 case RoleName:
126− return DeeListModel::data(index, 1); //DISPLAY_NAME
135+ return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME);
127136 case RoleIcon:
128− return DeeListModel::data(index, 2); //ICON_HINT
137+ return DeeListModel::data(index, CategoryColumn::ICON_HINT);
129138 case RoleRenderer:
130− return DeeListModel::data(index, 3); //RENDERER_NAME
139+ return DeeListModel::data(index, CategoryColumn::RENDERER_NAME);
131140 case RoleContentType:
132141 {
133− auto hints = DeeListModel::data(index, 4).toHash();
142+ auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash();
134143 return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default"));
135144 }
136145 case RoleHints:
137− return DeeListModel::data(index, 4); //HINTS
146+ return DeeListModel::data(index, CategoryColumn::HINTS);
138147 case RoleResults:
139− return QVariant::fromValue(getFilter(index.row()));
148+ return QVariant::fromValue(getResults(index.row()));
140149 case RoleCount:
141− return QVariant::fromValue(getFilter(index.row())->rowCount());
150+ return QVariant::fromValue(getResults(index.row())->rowCount());
142151 default:
143152 return QVariant();
144153 }