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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
//===========================================
// Lumina desktop source code
// Copyright (c) 2017, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
// Internal, OS-agnostic functionality for managing the object itself
//===========================================
#include <framework-OSInterface.h>
#include <QtConcurrent>
#include <QQmlEngine>
OSInterface::OSInterface(QObject *parent) : QObject(parent){
watcher = 0;
iodevice = 0;
netman = 0;
}
OSInterface::~OSInterface(){
if(watcher!=0){
QStringList paths; paths << watcher->files() << watcher->directories();
if(!paths.isEmpty()){ watcher->removePaths(paths); }
watcher->deleteLater();
}
if(iodevice!=0){
if(iodevice->isOpen()){ iodevice->close(); }
iodevice->deleteLater();
}
if(netman!=0){
netman->deleteLater();
}
}
OSInterface* OSInterface::instance(){
static OSInterface* m_os_object = 0;
if(m_os_object==0){
m_os_object = new OSInterface();
}
return m_os_object;
}
void OSInterface::RegisterType(){
static bool done = false;
if(done){ return; }
done=true;
qmlRegisterType<OSInterface>("Lumina.Backend.OSInterface", 2, 0, "OSInterface");
}
//Start/stop interface systems
void OSInterface::start(){
if(!mediaDirectories().isEmpty()){ setupMediaWatcher(); }//will create/connect the filesystem watcher automatically
setupNetworkManager(); //will create/connect the network monitor automatically
if(batteryAvailable()){ setupBatteryMonitor(30000, 1); } //30 second updates, 1 ms init delay
if(brightnessSupported()){ setupBrightnessMonitor(60000, 1); } //1 minute updates, 1 ms init delay
if(volumeSupported()){ setupVolumeMonitor(60000, 2); } //1 minute updates, 2 ms init delay
if(updatesSupported()){ setupUpdateMonitor(12*60*60*1000, 5*60*1000); } //12-hour updates, 5 minute delay
if(cpuSupported()){ setupCpuMonitor(2000, 20); } //2 second updates, 20 ms init delay
if(memorySupported()){ setupMemoryMonitor(2000, 21); } //2 second updates, 21 ms init delay
if(diskSupported()){ setupDiskMonitor(60000, 25); } //1 minute updates, 25 ms init delay
}
void OSInterface::stop(){
if(watcher!=0){ watcher->deleteLater(); watcher=0; }
if(batteryTimer!=0){ batteryTimer->stop(); disconnect(batteryTimer); }
if(brightnessTimer!=0){ brightnessTimer->stop(); disconnect(brightnessTimer); }
if(volumeTimer!=0){ volumeTimer->stop(); disconnect(volumeTimer); }
if(updateTimer!=0){ updateTimer->stop(); disconnect(updateTimer); }
if(cpuTimer!=0){ cpuTimer->stop(); disconnect(cpuTimer); }
if(memTimer!=0){ memTimer->stop(); disconnect(memTimer); }
if(diskTimer!=0){ diskTimer->stop(); disconnect(diskTimer); }
if(netman!=0){ disconnect(netman); netman->deleteLater(); netman = 0; }
}
bool OSInterface::isRunning(){ return _started; } //status of the object - whether it has been started yet
void OSInterface::connectWatcher(){
if(watcher==0){ return; }
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(watcherFileChanged(QString)) );
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(watcherDirChanged(QString)) );
}
void OSInterface::connectIodevice(){
if(iodevice==0){ return; }
connect(iodevice, SIGNAL(readyRead()), this, SLOT(iodeviceReadyRead()) );
}
void OSInterface::connectNetman(){
if(netman==0){ return; }
connect(netman, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(netAccessChanged(QNetworkAccessManager::NetworkAccessibility)) );
connect(netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(netRequestFinished(QNetworkReply*)) );
connect(netman, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), this, SLOT(netSslErrors(QNetworkReply*, const QList<QSslError>&)) );
}
bool OSInterface::verifyAppOrBin(QString chk){
bool valid = !chk.isEmpty();
if(valid && chk.endsWith(".desktop")){
chk = LUtils::AppToAbsolute(chk);
valid = QFile::exists(chk);
}else if(valid){
valid = LUtils::isValidBinary(chk);
}
return valid;
}
// ===========================
// OS SPECIFIC EXISTANCE CHECKS
// ===========================
bool OSInterface::hasControlPanel(){
return verifyAppOrBin(controlPanelShortcut());
}
bool OSInterface::hasAudioMixer(){
return verifyAppOrBin(audioMixerShortcut());
}
bool OSInterface::hasAppStore(){
return verifyAppOrBin(appStoreShortcut());
}
// ========================
// MEDIA DIRECTORIES
// ========================
// External Media Management (if system uses *.desktop shortcuts)
void OSInterface::setupMediaWatcher(){
//Create/connect the watcher if needed
if(watcher == 0){ watcher = new QFileSystemWatcher(); connectWatcher(); }
QStringList dirs = this->mediaDirectories();
if(dirs.isEmpty()){ return; } //nothing to do
//Make sure each directory is scanned **right now** (if it exists)
for(int i=0; i<dirs.length(); i++){
if(QFile::exists(dirs[i])){
handleMediaDirChange(dirs[i]);
}
}
}
bool OSInterface::handleMediaDirChange(QString dir){ //returns true if directory was handled
if( !this->mediaDirectories().contains(dir) ){ return false; } //not a media directory
QDir qdir(dir);
QStringList files = qdir.entryList(QStringList() << "*.desktop", QDir::Files, QDir::Name);
for(int i=0; i<files.length(); i++){ files[i] = qdir.absoluteFilePath(files[i]); }
QString key = "media_files/"+dir;
if(files.isEmpty() && INFO.contains(key)){ INFO.remove(key); emit mediaShortcutsChanged(); } //no files for this directory at the moment
else{ INFO.insert("media_files/"+dir, files); emit mediaShortcutsChanged(); } //save these file paths for later
//Make sure the directory is still watched (sometimes the dir is removed/recreated on modification)
if(!watcher->directories().contains(dir)){ watcher->addPath(dir); }
return true;
}
QStringList OSInterface::autoHandledMediaFiles(){
QStringList files;
QStringList keys = INFO.keys().filter("media_files/");
for(int i=0; i<keys.length(); i++){
if(keys[i].startsWith("media_files/")){ files << INFO[keys[i]].toStringList(); }
}
return files;
}
// =============================
// NETWORK INTERFACE FUNCTIONS
// =============================
// Qt-based NetworkAccessManager usage
void OSInterface::setupNetworkManager(){
if(netman==0){
netman = new QNetworkAccessManager(this);
connectNetman();
}
//Load the initial state of the network accessibility
netAccessChanged(netman->networkAccessible());
}
bool OSInterface::networkAvailable(){
if(INFO.contains("netaccess/available")){ return INFO.value("netaccess/available").toBool(); }
return false;
}
QString OSInterface::networkType(){
if(INFO.contains("netaccess/type")){ return INFO.value("netaccess/type").toString(); } //"wifi", "wired", or "cell"
return "";
}
QString OSInterface::networkHostname(){
return QHostInfo::localHostName();
}
QHostAddress OSInterface::networkAddress(){
QString addr;
if(INFO.contains("netaccess/address")){ addr = INFO.value("netaccess/address").toString(); }
return QHostAddress(addr);
}
bool OSInterface::hasNetworkManager(){
return verifyAppOrBin(networkManagerUtility());
}
//NetworkAccessManager slots
void OSInterface::netAccessChanged(QNetworkAccessManager::NetworkAccessibility stat){
INFO.insert("netaccess/available", stat== QNetworkAccessManager::Accessible);
//Update all the other network status info at the same time
QNetworkConfiguration active = netman->activeConfiguration();
//Type of connection
QString type;
switch(active.bearerTypeFamily()){
case QNetworkConfiguration::BearerEthernet: type="wired"; break;
case QNetworkConfiguration::BearerWLAN: type="wifi"; break;
case QNetworkConfiguration::Bearer2G: type="cell-2G"; break;
case QNetworkConfiguration::Bearer3G: type="cell-3G"; break;
case QNetworkConfiguration::Bearer4G: type="cell-4G"; break;
default: type=networkTypeFromDeviceName(active.name()); //could not be auto-determined - run the OS-specific routine
}
INFO.insert("netaccess/type", type);
//qDebug() << "Detected Device Status:" << active.identifier() << type << stat;
QNetworkInterface iface = QNetworkInterface::interfaceFromName(active.name());
//qDebug() << " - Configuration: Name:" << active.name() << active.bearerTypeName() << active.identifier();
//qDebug() << " - Interface: MAC Address:" << iface.hardwareAddress() << "Name:" << iface.name() << iface.humanReadableName() << iface.isValid();
QList<QNetworkAddressEntry> addressList = iface.addressEntries();
QStringList address;
//NOTE: There are often 2 addresses, IPv4 and IPv6
for(int i=0; i<addressList.length(); i++){
address << addressList[i].ip().toString();
}
//qDebug() << " - IP Address:" << address;
//qDebug() << " - Hostname:" << networkHostname();
INFO.insert("netaccess/address", address.join(", "));
emit networkStatusChanged();
}
// ========================
// TIMER-BASED MONITORS
// ========================
//Timer slots
void OSInterface::BatteryTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncBatteryInfo, this, &INFO, batteryTimer);
}
void OSInterface::UpdateTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncUpdateInfo, this, &INFO, updateTimer);
}
void OSInterface::BrightnessTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncBrightnessInfo, this, &INFO, brightnessTimer);
}
void OSInterface::VolumeTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncVolumeInfo, this, &INFO, volumeTimer);
}
void OSInterface::CpuTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncCpuInfo, this, &INFO, cpuTimer);
}
void OSInterface::MemTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncMemoryInfo, this, &INFO, memTimer);
}
void OSInterface::DiskTimerUpdate(){
QtConcurrent::run(this, &OSInterface::syncDiskInfo, this, &INFO, diskTimer);
}
// Timer Setup functions
void OSInterface::setupBatteryMonitor(int update_ms, int delay_ms){
batteryTimer = new QTimer(this);
batteryTimer->setSingleShot(true);
batteryTimer->setInterval(update_ms);
connect(batteryTimer, SIGNAL(timeout()), this, SLOT(BatteryTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(BatteryTimerUpdate()) );
}
void OSInterface::setupUpdateMonitor(int update_ms, int delay_ms){
updateTimer = new QTimer(this);
updateTimer->setSingleShot(true);
updateTimer->setInterval(update_ms);
connect(updateTimer, SIGNAL(timeout()), this, SLOT(UpdateTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(UpdateTimerUpdate()) );
}
void OSInterface::setupBrightnessMonitor(int update_ms, int delay_ms){
brightnessTimer = new QTimer(this);
brightnessTimer->setSingleShot(true);
brightnessTimer->setInterval(update_ms);
connect(brightnessTimer, SIGNAL(timeout()), this, SLOT(BrightnessTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(BrightnessTimerUpdate()) );
}
void OSInterface::setupVolumeMonitor(int update_ms, int delay_ms){
volumeTimer = new QTimer(this);
volumeTimer->setSingleShot(true);
volumeTimer->setInterval(update_ms);
connect(volumeTimer, SIGNAL(timeout()), this, SLOT(VolumeTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(VolumeTimerUpdate()) );
}
void OSInterface::setupCpuMonitor(int update_ms, int delay_ms){
cpuTimer = new QTimer(this);
cpuTimer->setSingleShot(true);
cpuTimer->setInterval(update_ms);
connect(cpuTimer, SIGNAL(timeout()), this, SLOT(CpuTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(CpuTimerUpdate()) );
}
void OSInterface::setupMemoryMonitor(int update_ms, int delay_ms){
memTimer = new QTimer(this);
memTimer->setSingleShot(true);
memTimer->setInterval(update_ms);
connect(memTimer, SIGNAL(timeout()), this, SLOT(MemTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(MemTimerUpdate()) );
}
void OSInterface::setupDiskMonitor(int update_ms, int delay_ms){
diskTimer = new QTimer(this);
diskTimer->setSingleShot(true);
diskTimer->setInterval(update_ms);
connect(diskTimer, SIGNAL(timeout()), this, SLOT(DiskTimerUpdate()) );
QTimer::singleShot(delay_ms, this, SLOT(DiskTimerUpdate()) );
}
// Timer-based monitor update routines (NOTE: these are all run in a separate thread!!)
void OSInterface::syncBatteryInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncUpdateInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncBrightnessInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncVolumeInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncCpuInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncMemoryInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
void OSInterface::syncDiskInfo(OSInterface *os, QHash<QString, QVariant> *hash, QTimer *timer){
QTimer::singleShot(0, timer, SLOT(start()));
}
|