-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathworker.cpp
More file actions
189 lines (149 loc) · 6.68 KB
/
worker.cpp
File metadata and controls
189 lines (149 loc) · 6.68 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
// system includes
#include <atomic>
#include <iostream>
#include <deque>
// library includes
#include <QDebug>
#include <QFile>
#include <QObject>
#include <QSysInfo>
#include <QTimer>
#include <QThreadPool>
#include <QMutexLocker>
#include <appimage/appimage.h>
// local includes
#include "worker.h"
#include "shared.h"
namespace {
enum OP_TYPE {
INTEGRATE = 0,
UNINTEGRATE = 1,
};
typedef std::pair<QString, OP_TYPE> Operation;
}
namespace appimagelauncher::daemon {
Q_LOGGING_CATEGORY(workerCat, "appimagelauncher.daemon.worker")
class Worker::PrivateData {
public:
QTimer deferredOperationsTimer;
static constexpr int TIMEOUT = 15 * 1000;
// std::set is unordered, therefore using std::deque to keep the order of the operations
std::deque<Operation> deferredOperations;
class OperationTask : public QRunnable {
private:
Operation operation;
QMutex* mutex;
public:
OperationTask(const Operation& operation, QMutex* mutex) : operation(operation), mutex(mutex) {}
void run() override {
const auto& path = operation.first;
const auto& type = operation.second;
const auto exists = QFile::exists(path);
const auto appImageType = appimage_get_type(path.toStdString().c_str(), false);
const auto isAppImage = 0 < appImageType && appImageType <= 2;
if (type == INTEGRATE) {
{ // Scope for Output Mutex Locker
QMutexLocker mutexLocker(mutex);
std::cout << "Integrating: " << path.toStdString() << std::endl;
if (!exists) {
std::cout << "ERROR: file does not exist, cannot integrate" << std::endl;
return;
}
if (!isAppImage) {
std::cout << "ERROR: not an AppImage, skipping" << std::endl;
return;
}
}
// check for X-AppImage-Integrate=false
const auto shallNotBeIntegrated = appimage_shall_not_be_integrated(path.toStdString().c_str());
if (shallNotBeIntegrated > 0) {
QMutexLocker mutexLocker(mutex);
std::cout << "WARNING: AppImage shall not be integrated, skipping" << std::endl;
return;
}
if (shallNotBeIntegrated < 0) {
QMutexLocker mutexLocker(mutex);
std::cout << "WARNING: appimage_shall_not_be_integrated failed, continuing with fallback capable integration" << std::endl;
}
if (!installDesktopFileAndIcons(path)) {
QMutexLocker mutexLocker(mutex);
std::cout << "ERROR: Failed to register AppImage in system" << std::endl;
return;
}
} else if (type == UNINTEGRATE) {
// nothing to do
}
}
};
public:
PrivateData() {
deferredOperationsTimer.setSingleShot(true);
deferredOperationsTimer.setInterval(TIMEOUT);
}
public:
// in addition to a simple duplicate check, this function is context sensitive
// it starts with the last element, and checks for duplicates until an opposite action is found
// for instance, when the element shall integrated, it will check for duplicates until an unintegration operation
// is found
bool isDuplicate(Operation operation) {
for (auto it = deferredOperations.rbegin(); it != deferredOperations.rend(); ++it) {
if ((*it).first == operation.first) {
// if operation type is different, then the operation is new, and should be added to the list
// if it is equal, it's a duplicate
// in either case, the loop can be aborted here
return (*it).second == operation.second;
}
}
return false;
}
};
Worker::Worker(QObject* parent) : QObject(parent) {
d = std::make_shared<PrivateData>();
connect(this, &Worker::startTimer, this, &Worker::startTimerIfNecessary, Qt::QueuedConnection);
connect(&d->deferredOperationsTimer, &QTimer::timeout, this, &Worker::executeDeferredOperations);
}
void Worker::executeDeferredOperations() {
if (d->deferredOperations.empty()) {
qCDebug(workerCat) << "No deferred operations to execute";
return;
}
std::cout << "Executing deferred operations" << std::endl;
QMutex outputMutex;
while (!d->deferredOperations.empty()) {
auto operation = d->deferredOperations.front();
d->deferredOperations.pop_front();
QThreadPool::globalInstance()->start(new PrivateData::OperationTask(operation, &outputMutex));
}
// wait until all AppImages have been integrated
QThreadPool::globalInstance()->waitForDone();
std::cout << "Cleaning up old desktop integration files" << std::endl;
if (!cleanUpOldDesktopIntegrationResources(true)) {
std::cout << "Failed to clean up old desktop integration files" << std::endl;
}
// make sure the icons in the launcher are refreshed
std::cout << "Updating desktop database and icon caches" << std::endl;
if (!updateDesktopDatabaseAndIconCaches())
std::cout << "Failed to update desktop database and icon caches" << std::endl;
std::cout << "Done" << std::endl;
}
void Worker::scheduleForIntegration(const QString& path) {
auto operation = std::make_pair(path, INTEGRATE);
if (!d->isDuplicate(operation)) {
std::cout << "Scheduling for (re-)integration: " << path.toStdString() << std::endl;
d->deferredOperations.push_back(operation);
emit startTimer();
}
}
void Worker::scheduleForUnintegration(const QString& path) {
auto operation = std::make_pair(path, UNINTEGRATE);
if (!d->isDuplicate(operation)) {
std::cout << "Scheduling for unintegration: " << path.toStdString() << std::endl;
d->deferredOperations.push_back(operation);
emit startTimer();
}
}
void Worker::startTimerIfNecessary() {
if (!d->deferredOperationsTimer.isActive())
QMetaObject::invokeMethod(&d->deferredOperationsTimer, "start");
}
}