forked from MadFishTheOne/qtpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktopapplications.cpp
302 lines (257 loc) · 6.43 KB
/
desktopapplications.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "desktopapplications.h"
#include <QtCore/QTimer>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QMutexLocker>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QProcess>
#include <QtGui/QIcon>
#include "iconloader.h"
#include "dpisupport.h"
bool DesktopApplication::init(const QString& path)
{
m_path = path;
QFile file(m_path);
if(!file.exists())
return false;
QFileInfo fileInfo(file);
m_lastUpdated = fileInfo.lastModified();
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream in(&file);
while(!in.atEnd())
{
QString line = in.readLine();
if(line[0] == '[')
{
if(line.contains("Desktop Entry"))
continue;
else
break; // We only process "Desktop Entry" here.
}
if(line[0] == '#')
continue;
QStringList list = line.split('=');
if(list.size() < 2)
continue;
QString key = list[0];
QString value = list[1];
if(key == "NoDisplay" && value == "true")
m_isNoDisplay = true;
if(key == "Name")
m_name = value;
if(key == "Exec")
m_exec = value;
if(key == "Icon")
{
m_iconName = value;
}
if(key == "Categories")
m_categories = value.split(";", QString::SkipEmptyParts);
}
return true;
}
bool DesktopApplication::exists() const
{
return QFile(m_path).exists();
}
bool DesktopApplication::needUpdate() const
{
return m_lastUpdated != QFileInfo(m_path).lastModified();
}
void DesktopApplication::launch() const
{
QString exec = m_exec;
// Handle special arguments.
for(;;)
{
int argPos = exec.indexOf('%');
if(argPos == -1)
break;
// For now, just remove them.
int spacePos = exec.indexOf(' ', argPos);
if(spacePos == -1)
exec.resize(argPos);
else
exec.remove(argPos, spacePos - argPos);
}
exec = exec.trimmed();
QStringList args = exec.split(' ');
QString process = args[0];
args.removeAt(0);
QProcess::startDetached(process, args, getenv("HOME"));
}
DesktopApplications* DesktopApplications::m_instance = NULL;
DesktopApplications::DesktopApplications()
: m_abortWorker(false)
{
m_instance = this;
qRegisterMetaType<DesktopApplication>();
m_updateTimer = new QTimer();
m_updateTimer->setSingleShot(true);
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(refresh()));
m_watcher = new QFileSystemWatcher();
connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(directoryChanged(QString)));
connect(m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(fileChanged(QString)));
// Start worker and trigger initial update.
start(QThread::IdlePriority);
refresh();
}
DesktopApplications::~DesktopApplications()
{
m_abortWorker = true;
m_tasksWaitCondition.wakeOne();
wait();
delete m_watcher;
delete m_updateTimer;
m_instance = NULL;
}
QList<DesktopApplication> DesktopApplications::applications()
{
QMutexLocker lock(&m_applicationsMutex);
return m_applications.values();
}
DesktopApplication DesktopApplications::applicationFromPath(const QString& path)
{
QMutexLocker lock(&m_applicationsMutex);
return m_applications[path];
}
void DesktopApplications::launch(const QString& path)
{
QMutexLocker lock(&m_applicationsMutex);
m_applications[path].launch();
}
void DesktopApplications::run()
{
forever
{
// Extract next task.
bool isImageTask = false;
QString path;
{
QMutexLocker lock(&m_tasksMutex);
if(m_fileTasks.isEmpty() && m_imageTasks.isEmpty())
m_tasksWaitCondition.wait(&m_tasksMutex);
if(m_abortWorker)
return;
if(!m_fileTasks.isEmpty())
{
path = m_fileTasks.first();
m_fileTasks.removeFirst();
}
else
{
isImageTask = true;
path = m_imageTasks.first();
m_imageTasks.removeFirst();
}
}
if(!isImageTask)
{
// File task.
m_applicationsMutex.lock();
bool needUpdate = (!m_applications.contains(path)) || (m_applications[path].needUpdate());
m_applicationsMutex.unlock();
if(needUpdate)
{
DesktopApplication app;
if(app.init(path))
{
m_applicationsMutex.lock();
m_applications[path] = app;
emit applicationUpdated(app);
m_applicationsMutex.unlock();
m_tasksMutex.lock();
if(!m_imageTasks.contains(path))
m_imageTasks.append(path);
m_tasksMutex.unlock();
}
}
}
else
{
// Image task.
m_applicationsMutex.lock();
QString iconName;
if(m_applications.contains(path))
iconName = m_applications[path].iconName();
m_applicationsMutex.unlock();
if(!iconName.isEmpty())
{
QImage iconImage = IconLoader::instance()->loadIcon(QIcon::themeName(), iconName, adjustHardcodedPixelSize(32));
m_applicationsMutex.lock();
if(m_applications.contains(path))
m_applications[path].setIconImage(iconImage);
emit applicationUpdated(m_applications[path]);
m_applicationsMutex.unlock();
}
}
}
}
void DesktopApplications::directoryChanged(const QString& path)
{
m_updateTimer->stop();
m_updateTimer->start();
}
void DesktopApplications::fileChanged(const QString& path)
{
QMutexLocker lock(&m_tasksMutex);
if(!m_fileTasks.contains(path))
m_fileTasks.append(path);
}
void DesktopApplications::refresh()
{
m_tasksMutex.lock();
m_fileTasks.clear();
m_imageTasks.clear();
if(!m_watcher->directories().isEmpty())
m_watcher->removePaths(m_watcher->directories());
if(!m_watcher->files().isEmpty())
m_watcher->removePaths(m_watcher->files());
QString xdgDataDirs;
char* xdgDataDirsEnv = getenv("XDG_DATA_DIRS");
if(xdgDataDirsEnv != NULL)
xdgDataDirs = xdgDataDirsEnv;
else
xdgDataDirs = "/usr/local/share/:/usr/share/";
QStringList dirs = xdgDataDirs.split(':');
foreach(const QString& path, dirs)
{
QDir dir(path);
if(dir.exists())
traverse(QDir(dir.absoluteFilePath("applications")));
}
QStringList removeList;
foreach(const DesktopApplication& app, m_applications)
{
if(!app.exists())
removeList.append(app.path());
}
foreach(const QString& path, removeList)
{
m_applications.remove(path);
emit applicationRemoved(path);
}
m_tasksMutex.unlock();
m_tasksWaitCondition.wakeOne();
}
void DesktopApplications::traverse(const QDir& dir)
{
if(!dir.exists())
return;
m_watcher->addPath(dir.canonicalPath());
QFileInfoList fileInfos = dir.entryInfoList(QStringList("*.desktop"), QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files);
foreach(const QFileInfo& fileInfo, fileInfos)
{
if(fileInfo.isDir())
{
traverse(fileInfo.canonicalFilePath());
}
else
{
m_watcher->addPath(fileInfo.canonicalFilePath());
m_fileTasks.append(fileInfo.canonicalFilePath());
}
}
}