on f85133b help sign in
unity8/plugins/Unity/Launcher/appdrawermodel.cpp
125 lines 3.9 kB history raw
1/*
2 * Copyright (C) 2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "appdrawermodel.h"
18#include "ualwrapper.h"
19#include "xdgwatcher.h"
21#include <QDebug>
22#include <QDateTime>
24AppDrawerModel::AppDrawerModel(QObject *parent):
25 AppDrawerModelInterface(parent),
26 m_ual(new UalWrapper(this)),
27 m_xdgWatcher(new XdgWatcher(this))
29 Q_FOREACH (const QString &appId, UalWrapper::installedApps()) {
30 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
31 if (!info.valid) {
32 qWarning() << "Failed to get app info for app" << appId;
33 continue;
34 }
35 LauncherItem* item = new LauncherItem(appId, info.name, info.icon, this);
36 item->setKeywords(info.keywords);
37 item->setPopularity(info.popularity);
38 m_list.append(item);
39 }
41 // keep this a queued connection as it's coming from another thread.
42 connect(m_xdgWatcher, &XdgWatcher::appAdded, this, &AppDrawerModel::appAdded, Qt::QueuedConnection);
43 connect(m_xdgWatcher, &XdgWatcher::appRemoved, this, &AppDrawerModel::appRemoved, Qt::QueuedConnection);
44 connect(m_xdgWatcher, &XdgWatcher::appInfoChanged, this, &AppDrawerModel::appInfoChanged, Qt::QueuedConnection);
47int AppDrawerModel::rowCount(const QModelIndex &parent) const
49 Q_UNUSED(parent)
50 return m_list.count();
53QVariant AppDrawerModel::data(const QModelIndex &index, int role) const
55 switch (role) {
56 case RoleAppId:
57 return m_list.at(index.row())->appId();
58 case RoleName:
59 return m_list.at(index.row())->name();
60 case RoleIcon:
61 return m_list.at(index.row())->icon();
62 case RoleKeywords:
63 return m_list.at(index.row())->keywords();
64 case RoleUsage:
65 return m_list.at(index.row())->popularity();
66 }
68 return QVariant();
71void AppDrawerModel::appAdded(const QString &appId)
73 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
74 if (!info.valid) {
75 qWarning() << "App added signal received but failed to get app info for app" << appId;
76 return;
77 }
79 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
80 LauncherItem* item = new LauncherItem(appId, info.name, info.icon, this);
81 item->setKeywords(info.keywords);
82 item->setPopularity(info.popularity);
83 m_list.append(item);
84 endInsertRows();
87void AppDrawerModel::appRemoved(const QString &appId)
89 int idx = -1;
90 for (int i = 0; i < m_list.count(); i++) {
91 if (m_list.at(i)->appId() == appId) {
92 idx = i;
93 break;
94 }
95 }
96 if (idx < 0) {
97 qWarning() << "App removed signal received but app doesn't seem to be in the drawer model";
98 return;
99 }
100 beginRemoveRows(QModelIndex(), idx, idx);
101 m_list.takeAt(idx)->deleteLater();
102 endRemoveRows();
105void AppDrawerModel::appInfoChanged(const QString &appId)
107 LauncherItem *item = nullptr;
108 int idx = -1;
110 for(int i = 0; i < m_list.count(); i++) {
111 if (m_list.at(i)->appId() == appId) {
112 item = m_list.at(i);
113 idx = i;
114 break;
115 }
116 }
118 if (!item) {
119 return;
120 }
122 UalWrapper::AppInfo info = m_ual->getApplicationInfo(appId);
123 item->setPopularity(info.popularity);
124 Q_EMIT dataChanged(index(idx), index(idx), {AppDrawerModelInterface::RoleUsage});