forked from linuxdeepin/dde-launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappsmodel.cpp
More file actions
248 lines (217 loc) · 8.89 KB
/
appsmodel.cpp
File metadata and controls
248 lines (217 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "appsmodel.h"
#include "categoryutils.h"
#include "iconutils.h"
#include <QDebug>
#include <QStandardPaths>
#include <DConfig>
#include <DPinyin>
#include <DFileWatcherManager>
#include <appinfo.h>
#include <QFileInfo>
#include "appmgr.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(logModels)
DCORE_USE_NAMESPACE
static void updateAppItemFromAM(AppItem *appItem)
{
const QString id(appItem->freedesktopId());
qCDebug(logModels) << "Updating AppItem from AppMgr:" << id;
auto item = AppMgr::instance()->appItem(id);
if (!item) {
qCWarning(logModels) << "Not existing item in AppMgr for the desktopId" << id;
return;
}
qCDebug(logModels) << "Update AppItem property for the desktopId" << id;
appItem->setName(item->name);
appItem->setDisplayName(item->displayName);
appItem->setIconName(item->iconName);
appItem->setCategories(item->categories);
appItem->setInstalledTime(item->installedTime);
appItem->setLastLaunchedTime(item->lastLaunchedTime);
appItem->setLaunchedTimes(item->launchedTimes);
appItem->setIsAutoStart(item->isAutoStart);
}
AppsModel::AppsModel(QObject *parent)
: QStandardItemModel(parent)
, m_dconfig(DConfig::create("org.deepin.dde.shell", "org.deepin.ds.launchpad"))
, m_tmUpdateCache(new QTimer(this))
{
qCDebug(logModels) << "Initializing AppsModel";
Q_ASSERT_X(m_dconfig->isValid(), "DConfig", "DConfig file is missing or invalid");
m_excludedAppIdList = m_dconfig->value("excludeAppIdList", QStringList{}).toStringList();
QHash<int, QByteArray> defaultRoleNames = roleNames();
defaultRoleNames.insert({
{AppItem::DesktopIdRole, QByteArrayLiteral("desktopId")},
{AppItem::DDECategoryRole, QByteArrayLiteral("category")},
{AppItem::IconNameRole, QByteArrayLiteral("iconName")},
{AppItem::InstalledTimeRole, QByteArrayLiteral("installedTime")},
{AppItem::LastLaunchedTimeRole, QByteArrayLiteral("lastLaunchedTime")},
{AppItem::LaunchedTimesRole, QByteArrayLiteral("launchedTimes")},
{AppItem::IsAutoStartRole, QByteArrayLiteral("autoStart")},
{AppItem::VendorRole, QByteArrayLiteral("vendor")},
{AppItem::GenericNameRole, QByteArrayLiteral("genericName")},
{AppsModel::TransliteratedRole, QByteArrayLiteral("transliterated")}
});
setItemRoleNames(defaultRoleNames);
QList<AppItem *> items(allAppInfosShouldBeShown());
QList<AppItem *> duplicatedItems = addItems(items);
Q_ASSERT(duplicatedItems.isEmpty());
qDebug() << rowCount();
m_tmUpdateCache->setInterval(1000);
m_tmUpdateCache->setSingleShot(true);
connect(AppMgr::instance(), &AppMgr::changed, m_tmUpdateCache, qOverload<>(&QTimer::start));
connect(AppMgr::instance(), &AppMgr::itemDataChanged, this, [this](const QString &id) {
const auto appItem = this->itemFromDesktopId(id);
if (!appItem) {
qWarning() << "Not existing item in AppsModel for the desktopId" << id;
return;
}
updateAppItemFromAM(appItem);
});
m_fwIconCache = new DFileWatcherManager(this);
const QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
const QString suffix("/icons/hicolor/icon-theme.cache");
for (const QString &path : paths) {
if (QFileInfo::exists(path + suffix))
m_fwIconCache->add(path + suffix);
}
connect(m_fwIconCache, &DFileWatcherManager::fileModified, m_tmUpdateCache, qOverload<>(&QTimer::start));
connect(m_fwIconCache, &DFileWatcherManager::fileAttributeChanged, m_tmUpdateCache, qOverload<>(&QTimer::start));
connect(m_tmUpdateCache, &QTimer::timeout, this, &AppsModel::updateModelData);
}
void AppsModel::appendRows(const QList<AppItem *> items)
{
// TODO: preformance improvement?
for (AppItem * item : items) {
appendRow(item);
}
}
AppItem *AppsModel::itemFromDesktopId(const QString freedesktopId) const
{
QModelIndexList indexes = match(index(0, 0, QModelIndex()),
AppItem::DesktopIdRole, freedesktopId, 1, Qt::MatchExactly);
if (indexes.isEmpty()) return nullptr;
return static_cast<AppItem *>(itemFromIndex(indexes.at(0)));
}
// the model takes the ownership for the items that actually added to the model.
// won't try to update item if there are existing ones.
// return the duplicated ones
const QList<AppItem *> AppsModel::addItems(const QList<AppItem *> &items)
{
QList<AppItem *> append;
QList<AppItem *> duplicated;
for (AppItem * item : items) {
if (itemFromDesktopId(item->freedesktopId()) != nullptr) {
duplicated.append(item);
} else {
append.append(item);
}
}
appendRows(append);
return duplicated;
}
// try to update items, if not exist, add them to the model.
// the reference item that were used to update the existing one won't replace the existing one, thus
// they will be in the returned item list.
// return the ones were not added to the model.
const QList<AppItem *> AppsModel::updateItems(const QList<AppItem *> &items)
{
QList<AppItem *> append;
QList<AppItem *> duplicated;
for (AppItem * item : items) {
AppItem * existing = itemFromDesktopId(item->freedesktopId());
if (existing != nullptr) {
existing->updateData(item);
if (existing != item) {
duplicated.append(item);
}
} else {
append.append(item);
}
}
appendRows(append);
return duplicated;
}
QVariant AppsModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case AppsModel::TransliteratedRole: {
// TODO: 1. use icu::Transliterator for other locales
// 2. support polyphonic characters (e.g. Music: YinYue or YinLe)
const auto decodedDisplay = Dtk::Core::pinyin(index.data(Qt::DisplayRole).toString(), Dtk::Core::TS_NoneTone);
if (decodedDisplay.isEmpty()) return QString();
const QString transliterated = decodedDisplay.constFirst();
if (transliterated.isEmpty()) return transliterated;
const QChar & firstChar = transliterated.constData()[0];
if (firstChar.isDigit()) return QString("#%1").arg(transliterated);
else if (!firstChar.isLetter()) return QString("&%1").arg(transliterated);
return transliterated;
}
case AppsModel::AllTransliteratedRole: {
// it's useful to search(e.g. Music: YinYue or YinLe -> YinYueYinLe)
const auto decodedDisplay = Dtk::Core::pinyin(index.data(Qt::DisplayRole).toString(), Dtk::Core::TS_NoneTone);
const QString &transliterated = decodedDisplay.join(".");
return transliterated;
}
default:
break;
}
return QStandardItemModel::data(index, role);
}
void AppsModel::updateModelData()
{
qCDebug(logModels) << "Updating model data";
IconUtils::tryUpdateIconCache();
beginResetModel();
qCInfo(logModels) << "Resetting model";
QList<AppItem *> items(allAppInfosShouldBeShown());
cleanUpInvalidApps(items);
QList<AppItem *> duplicatedItems = updateItems(items);
for (AppItem * item : std::as_const(duplicatedItems)) {
delete item;
}
endResetModel();
qCInfo(logModels) << "Model data updated";
}
// the caller manage the return values' ownership (i.e. might need to free them)
QList<AppItem *> AppsModel::allAppInfosShouldBeShown() const
{
QList<AppItem *> items;
const auto list = AppMgr::instance()->allAppInfosShouldBeShown();
for (auto appItem : list) {
if (m_excludedAppIdList.contains(appItem->id)) {
continue;
}
auto item = new AppItem(appItem->id);
item->setName(appItem->name);
item->setDisplayName(appItem->displayName);
item->setIconName(appItem->iconName);
item->setCategories(appItem->categories);
item->setDDECategory(AppItem::DDECategories(CategoryUtils::parseBestMatchedCategory(appItem->categories)));
item->setInstalledTime(appItem->installedTime);
item->setLastLaunchedTime(appItem->lastLaunchedTime);
item->setLaunchedTimes(appItem->launchedTimes);
item->setIsAutoStart(appItem->isAutoStart);
item->setVendor(appItem->vendor);
item->setGenericName(appItem->genericName);
items.append(item);
}
return items;
}
// remove apps that are not in the \l knownExistedApps list
void AppsModel::cleanUpInvalidApps(const QList<AppItem *> knownExistedApps)
{
QSet<QString> existedApps;
for (const AppItem * app : knownExistedApps) {
existedApps.insert(app->freedesktopId());
}
for (int i = rowCount() - 1; i >= 0; i--) {
const QString & appId(data(index(i, 0), AppItem::DesktopIdRole).toString());
if (!existedApps.contains(appId)) {
removeRow(i);
}
}
}