18#include "appdrawermodel.h"
19#include "ualwrapper.h"
20#include "xdgwatcher.h"
21#include "iconcachewatcher.h"
25#include <QtConcurrentRun>
27static std::shared_ptr<LauncherItem> makeSharedLauncherItem(
28 const QString &appId,
const QString &name,
const QString &icon, QObject * parent)
30 return std::shared_ptr<LauncherItem>(
31 new LauncherItem(appId, name, icon, parent),
32 [] (LauncherItem *item) { item->deleteLater(); });
35AppDrawerModel::AppDrawerModel(QObject *parent):
36 AppDrawerModelInterface(parent),
37 m_ual(new UalWrapper(this)),
38 m_xdgWatcher(new XdgWatcher(this)),
39 m_iconCacheWatcher(new IconCacheWatcher(this)),
42 connect(&m_refreshFutureWatcher, &QFutureWatcher<ItemList>::finished,
43 this, &AppDrawerModel::onRefreshFinished);
46 connect(m_xdgWatcher, &XdgWatcher::appAdded,
this, &AppDrawerModel::appAdded, Qt::QueuedConnection);
47 connect(m_xdgWatcher, &XdgWatcher::appRemoved,
this, &AppDrawerModel::appRemoved, Qt::QueuedConnection);
48 connect(m_xdgWatcher, &XdgWatcher::appInfoChanged,
this, &AppDrawerModel::appInfoChanged, Qt::QueuedConnection);
51 connect(m_iconCacheWatcher, &IconCacheWatcher::iconCacheChanged,
this, &AppDrawerModel::refresh, Qt::QueuedConnection);
56int AppDrawerModel::rowCount(
const QModelIndex &parent)
const
59 return m_list.count();
62QVariant AppDrawerModel::data(
const QModelIndex &index,
int role)
const
66 return m_list.at(index.row())->appId();
68 return m_list.at(index.row())->name();
70 return m_list.at(index.row())->icon();
72 return m_list.at(index.row())->keywords();
74 return m_list.at(index.row())->popularity();
80void AppDrawerModel::appAdded(
const QString &appId)
86 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
88 qWarning() <<
"App added signal received but failed to get app info for app" << appId;
92 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
93 auto item = makeSharedLauncherItem(appId, info.name, info.icon,
nullptr);
94 item->setKeywords(info.keywords);
95 item->setPopularity(info.popularity);
96 m_list.append(std::move(item));
100void AppDrawerModel::appRemoved(
const QString &appId)
107 for (
int i = 0; i < m_list.count(); i++) {
108 if (m_list.at(i)->appId() == appId) {
114 qWarning() <<
"App removed signal received but app doesn't seem to be in the drawer model";
117 beginRemoveRows(QModelIndex(), idx, idx);
118 m_list.removeAt(idx);
122void AppDrawerModel::appInfoChanged(
const QString &appId)
128 std::shared_ptr<LauncherItem> item;
131 for(
int i = 0; i < m_list.count(); i++) {
132 if (m_list.at(i)->appId() == appId) {
143 UalWrapper::AppInfo info = m_ual->getApplicationInfo(appId);
144 item->setPopularity(info.popularity);
145 Q_EMIT dataChanged(index(idx), index(idx), {AppDrawerModelInterface::RoleUsage});
148bool AppDrawerModel::refreshing() {
152void AppDrawerModel::refresh() {
156 m_refreshFutureWatcher.setFuture(QtConcurrent::run([](QThread *thread) {
159 Q_FOREACH (
const QString &appId, UalWrapper::installedApps()) {
160 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
162 qWarning() <<
"Failed to get app info for app" << appId;
167 auto item = makeSharedLauncherItem(appId, info.name, info.icon,
nullptr);
168 item->setKeywords(info.keywords);
169 item->setPopularity(info.popularity);
170 item->moveToThread(thread);
171 list.append(std::move(item));
178 Q_EMIT refreshingChanged();
181void AppDrawerModel::onRefreshFinished() {
182 if (m_refreshFutureWatcher.isCanceled())
188 m_list = m_refreshFutureWatcher.result();
191 m_refreshFutureWatcher.setFuture(QFuture<ItemList>());
195 m_refreshing =
false;
196 Q_EMIT refreshingChanged();