/
launchermonitor.cpp
247 lines (210 loc) · 7.13 KB
/
launchermonitor.cpp
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
// This file is part of lipstick, a QML desktop library
//
// Copyright (c) 2012 Jolla Ltd.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation
// and appearing in the file LICENSE.LGPL included in the packaging
// of this file.
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
#include "launchermonitor.h"
#include "launcheritem.h"
#include <QDir>
/**
* Timeout (in milliseconds) to hold back sending updates, so that we can
* combine multiple updates to icons and desktop file in one go. Also to
* avoid doing extraneous updates in case files get added/changed/removed
* in quick succession.
**/
#define LAUNCHER_MONITOR_HOLDBACK_TIMEOUT_MS 2000
LauncherMonitor::LauncherMonitor()
: QObject()
, m_watcher()
, m_holdbackTimer()
, m_knownFiles()
, m_addedFiles()
, m_modifiedFiles()
, m_removedFiles()
{
initialize();
}
LauncherMonitor::LauncherMonitor(const QString &desktopFilesPath,
const QString &iconFilesPath)
: QObject()
, m_watcher()
, m_holdbackTimer()
, m_knownFiles()
, m_addedFiles()
, m_modifiedFiles()
, m_removedFiles()
{
initialize();
m_iconFilesPaths << iconFilesPath;
m_desktopFilesPaths << desktopFilesPath;
m_watcher.addPaths(m_iconFilesPaths);
m_watcher.addPaths(m_desktopFilesPaths);
// Force initial scan of directories
// Scan the desktop files first, so that the launcher items are already
// available by the time the icons will be processed
onDirectoryChanged(desktopFilesPath);
onDirectoryChanged(iconFilesPath);
}
void LauncherMonitor::initialize()
{
m_holdbackTimer.setSingleShot(true);
QObject::connect(&m_watcher, SIGNAL(directoryChanged(const QString &)),
this, SLOT(onDirectoryChanged(const QString &)));
QObject::connect(&m_watcher, SIGNAL(fileChanged(const QString &)),
this, SLOT(onFileChanged(const QString &)));
QObject::connect(&m_holdbackTimer, SIGNAL(timeout()),
this, SLOT(onHoldbackTimerTimeout()));
}
LauncherMonitor::~LauncherMonitor()
{
}
void LauncherMonitor::start()
{
// Manually force sending an update (this should be called when signals are
// hooked up and the model is ready to receive update events)
m_holdbackTimer.stop();
onHoldbackTimerTimeout();
}
QStringList LauncherMonitor::directories() const
{
return m_desktopFilesPaths;
}
void LauncherMonitor::setDirectories(const QStringList &dirs)
{
setDirectories(dirs, m_desktopFilesPaths);
}
QStringList LauncherMonitor::iconDirectories() const
{
return m_iconFilesPaths;
}
void LauncherMonitor::setIconDirectories(const QStringList &dirs)
{
setDirectories(dirs, m_iconFilesPaths);
}
void LauncherMonitor::setDirectories(const QStringList &newDirs, QStringList &targetDirs)
{
QStringList newPaths;
QStringList::ConstIterator it = newDirs.begin();
while (it != newDirs.end()) {
if (!targetDirs.contains(*it)) {
newPaths << *it;
} else {
targetDirs.removeAll(*it);
}
++it;
}
if (!targetDirs.isEmpty()) {
m_watcher.removePaths(targetDirs);
}
targetDirs = newDirs;
m_watcher.addPaths(newPaths);
foreach (QString path, newPaths)
onDirectoryChanged(path);
}
void LauncherMonitor::reset(const QStringList &dirs)
{
setDirectories(QStringList(), m_desktopFilesPaths);
m_knownFiles.clear();
setDirectories(dirs, m_desktopFilesPaths);
}
void LauncherMonitor::onDirectoryChanged(const QString &path)
{
QDir dir(path);
QStringList seen = dir.entryList();
QStringList added;
QStringList removed;
QStringList &knownFiles = m_knownFiles[path];
// Calculate added and removed files
foreach (const QString &filename, knownFiles) {
if (filename.startsWith(".")) {
continue;
}
if (!seen.contains(filename)) {
removed.append(dir.filePath(filename));
}
}
foreach (const QString &filename, seen) {
if (filename.startsWith(".")) {
continue;
}
if (!knownFiles.contains(filename)) {
added.append(dir.filePath(filename));
}
}
// First, stop monitoring all removed files and then tell interested
// parties that these files have gone.
// After that, do the same thing for added files.
if (!removed.isEmpty()) {
m_watcher.removePaths(removed);
foreach (const QString &filename, removed) {
m_modifiedFiles.removeAll(filename);
if (m_addedFiles.contains(filename)) {
// We have an "added" notification that's not sent out yet
// Just remove this notification, as if nothing happened
//
// (=> the file has been added and quickly removed again)
m_addedFiles.removeAll(filename);
} else {
m_removedFiles.append(filename);
}
}
}
if (!added.isEmpty()) {
m_watcher.addPaths(added);
foreach (const QString &filename, added) {
m_modifiedFiles.removeAll(filename);
if (m_removedFiles.contains(filename)) {
// We have a "removed" notification that's not sent out yet
// Just remove this notification, as if nothing happened
//
// (=> the file has vanished and re-appeared quickly)
m_removedFiles.removeAll(filename);
} else {
m_addedFiles.append(filename);
}
}
}
// Schedule updating the launcher icons
m_holdbackTimer.start(LAUNCHER_MONITOR_HOLDBACK_TIMEOUT_MS);
knownFiles = seen;
}
void LauncherMonitor::onFileChanged(const QString &path)
{
m_modifiedFiles.append(path);
// Schedule updating the launcher icons
m_holdbackTimer.start(LAUNCHER_MONITOR_HOLDBACK_TIMEOUT_MS);
}
void LauncherMonitor::onHoldbackTimerTimeout()
{
QStringList modifiedCandidates = m_modifiedFiles;
foreach (const QString &filename, modifiedCandidates) {
// This takes care of avoiding sending modifications for two cases:
// 1. The file was added and then modified
// 2. The file was modified and then removed
if (m_addedFiles.contains(filename) || m_removedFiles.contains(filename)) {
m_modifiedFiles.removeOne(filename);
}
}
if (m_addedFiles.isEmpty() && m_modifiedFiles.isEmpty() && m_removedFiles.isEmpty()) {
// Nothing to update
return;
}
LAUNCHER_DEBUG("=========");
LAUNCHER_DEBUG("Added:" << m_addedFiles);
LAUNCHER_DEBUG("Modified:" << m_modifiedFiles);
LAUNCHER_DEBUG("Removed:" << m_removedFiles);
LAUNCHER_DEBUG("=========");
emit filesUpdated(m_addedFiles, m_modifiedFiles, m_removedFiles);
m_addedFiles.clear();
m_modifiedFiles.clear();
m_removedFiles.clear();
}