@@ -21,7 +21,8 @@
21#include <QDateTime>
21#include <QDateTime>
23AppDrawerModel::AppDrawerModel(QObject *parent):
23AppDrawerModel::AppDrawerModel(QObject *parent):
24− AppDrawerModelInterface(parent)
24+ AppDrawerModelInterface(parent),
25+ m_ual(new UalWrapper(this))
26 Q_FOREACH (const QString &appId, UalWrapper::installedApps()) {
27 Q_FOREACH (const QString &appId, UalWrapper::installedApps()) {
27 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
28 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
@@ -29,10 +30,16 @@ AppDrawerModel::AppDrawerModel(QObject *parent):
29 qWarning() << "Failed to get app info for app" << appId;
30 qWarning() << "Failed to get app info for app" << appId;
32− m_list.append(new LauncherItem(appId, info.name, info.icon, this));
33+ LauncherItem* item = new LauncherItem(appId, info.name, info.icon, this);
33− m_list.last()->setKeywords(info.keywords);
34+ item->setKeywords(info.keywords);
35+ item->setPopularity(info.popularity);
35− qsrand(QDateTime::currentMSecsSinceEpoch() / 100);
38+
39+ // keep this a queued connection as it's coming from another thread.
40+ connect(m_ual, &UalWrapper::appAdded, this, &AppDrawerModel::appAdded, Qt::QueuedConnection);
41+ connect(m_ual, &UalWrapper::appRemoved, this, &AppDrawerModel::appRemoved, Qt::QueuedConnection);
42+ connect(m_ual, &UalWrapper::appInfoChanged, this, &AppDrawerModel::appInfoChanged, Qt::QueuedConnection);
38int AppDrawerModel::rowCount(const QModelIndex &parent) const
45int AppDrawerModel::rowCount(const QModelIndex &parent) const
@@ -53,10 +60,64 @@ QVariant AppDrawerModel::data(const QModelIndex &index, int role) const
53 case RoleKeywords:
60 case RoleKeywords:
54 return m_list.at(index.row())->keywords();
61 return m_list.at(index.row())->keywords();
55 case RoleUsage:
62 case RoleUsage:
56− // FIXME: u-a-l needs to provide API for usage stats.
63+ return m_list.at(index.row())->popularity();
57− // don't forget to drop the qsrand() call in the ctor when dropping this.
61 return QVariant();
66 return QVariant();
69+void AppDrawerModel::appAdded(const QString &appId)
71+ UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
73+ qWarning() << "App added signal received but failed to get app info for app" << appId;
77+ beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
78+ LauncherItem* item = new LauncherItem(appId, info.name, info.icon, this);
79+ item->setKeywords(info.keywords);
80+ item->setPopularity(info.popularity);
85+void AppDrawerModel::appRemoved(const QString &appId)
88+ for (int i = 0; i < m_list.count(); i++) {
89+ if (m_list.at(i)->appId() == appId) {
95+ qWarning() << "App removed signal received but app doesn't seem to be in the drawer model";
98+ beginRemoveRows(QModelIndex(), idx, idx);
99+ m_list.takeAt(idx)->deleteLater();
103+void AppDrawerModel::appInfoChanged(const QString &appId)
105+ LauncherItem *item = nullptr;
108+ for(int i = 0; i < m_list.count(); i++) {
109+ if (m_list.at(i)->appId() == appId) {
110+ item = m_list.at(i);
120+ UalWrapper::AppInfo info = m_ual->getApplicationInfo(appId);
121+ item->setPopularity(info.popularity);
122+ Q_EMIT dataChanged(index(idx), index(idx), {AppDrawerModelInterface::RoleUsage});