on bionic help sign in
unity8/plugins/Unity/Launcher/appdrawermodel.cpp
123 lines 3.8 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"
20#include <QDebug>
21#include <QDateTime>
23AppDrawerModel::AppDrawerModel(QObject *parent):
24 AppDrawerModelInterface(parent),
25 m_ual(new UalWrapper(this))
27 Q_FOREACH (const QString &appId, UalWrapper::installedApps()) {
28 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
29 if (!info.valid) {
30 qWarning() << "Failed to get app info for app" << appId;
31 continue;
32 }
33 LauncherItem* item = new LauncherItem(appId, info.name, info.icon, this);
34 item->setKeywords(info.keywords);
35 item->setPopularity(info.popularity);
36 m_list.append(item);
37 }
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);
45int AppDrawerModel::rowCount(const QModelIndex &parent) const
47 Q_UNUSED(parent)
48 return m_list.count();
51QVariant AppDrawerModel::data(const QModelIndex &index, int role) const
53 switch (role) {
54 case RoleAppId:
55 return m_list.at(index.row())->appId();
56 case RoleName:
57 return m_list.at(index.row())->name();
58 case RoleIcon:
59 return m_list.at(index.row())->icon();
60 case RoleKeywords:
61 return m_list.at(index.row())->keywords();
62 case RoleUsage:
63 return m_list.at(index.row())->popularity();
64 }
66 return QVariant();
69void AppDrawerModel::appAdded(const QString &appId)
71 UalWrapper::AppInfo info = UalWrapper::getApplicationInfo(appId);
72 if (!info.valid) {
73 qWarning() << "App added signal received but failed to get app info for app" << appId;
74 return;
75 }
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);
81 m_list.append(item);
82 endInsertRows();
85void AppDrawerModel::appRemoved(const QString &appId)
87 int idx = -1;
88 for (int i = 0; i < m_list.count(); i++) {
89 if (m_list.at(i)->appId() == appId) {
90 idx = i;
91 break;
92 }
93 }
94 if (idx < 0) {
95 qWarning() << "App removed signal received but app doesn't seem to be in the drawer model";
96 return;
97 }
98 beginRemoveRows(QModelIndex(), idx, idx);
99 m_list.takeAt(idx)->deleteLater();
100 endRemoveRows();
103void AppDrawerModel::appInfoChanged(const QString &appId)
105 LauncherItem *item = nullptr;
106 int idx = -1;
108 for(int i = 0; i < m_list.count(); i++) {
109 if (m_list.at(i)->appId() == appId) {
110 item = m_list.at(i);
111 idx = i;
112 break;
113 }
114 }
116 if (!item) {
117 return;
118 }
120 UalWrapper::AppInfo info = m_ual->getApplicationInfo(appId);
121 item->setPopularity(info.popularity);
122 Q_EMIT dataChanged(index(idx), index(idx), {AppDrawerModelInterface::RoleUsage});