This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
mountTray.cpp
452 lines (409 loc) · 16.4 KB
/
mountTray.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Qt */
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QImage>
#include <QMenu>
#include <QTranslator>
#include "mountTray.h"
#include <pcbsd-hardware.h>
#include <pcbsd-utils.h>
MountTray::~MountTray(){
}
void MountTray::programInit()
{
DCheck = new DevCheck(); //initialize class for checking devices
qDebug() << "pc-mounttray: starting up";
getInitialUsername(); //try to detect the non-root user who is running the program with root permissions
getDefaultFileManager(); //try to detect the default file-manager for opening the mount directory
loadSavedSettings();
trayIcon = new QSystemTrayIcon(this);
trayIconMenu = new QMenu();
//Generate the system menu options (these don't change)
sysMenu = new QMenu( tr("More Options") );
sysMenu->setIcon( QIcon(":icons/config.png") );
//Add the additional options
sysMenu->addAction( QIcon(":icons/folder.png"), tr("Open Media Directory"), this, SLOT(slotOpenMediaDir()) );
sysMenu->addAction( QIcon(":icons/harddrive.png"), tr("View Disk Usage"),this,SLOT(slotOpenFSDialog()) );
sysMenu->addAction( QIcon(":icons/refresh.png"),tr("Rescan Devices"), this, SLOT(slotRescan()) );
//Add the setting dialog option seperately
sysMenu->addSeparator();
sysMenu->addAction( QIcon(":icons/dvd.png"), tr("Load ISO File"), this, SLOT(slotOpenISO()) );
sysMenu->addAction( QIcon(":icons/config.png"), tr("Change Settings"), this, SLOT(slotOpenSettings()) );
//Add the Close button seperately
sysMenu->addSeparator();
sysMenu->addAction( QIcon(":icons/application-exit.png"), tr("Close Tray"), this, SLOT(closeTray()) );
// Tie the left-click signal to open the context menu
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotTrayActivated(QSystemTrayIcon::ActivationReason)) );
//Set the default Tray Icon (will change once tray menus are set)
trayIcon->setIcon(QIcon(":icons/CDdevices-inactive.png"));
trayIcon->show();
//Startup the devd watching process
qDebug() << "-Starting up the DEVD watcher";
devdTimer = new QTimer();
devdTimer->setSingleShot(TRUE);
connect(devdTimer,SIGNAL(timeout()),this,SLOT(slotDevChanges()));
startupDevdProc();
//Do an initial scan of the devices with dmesg
qDebug() << "-Performing initial device scan";
scanInitialDevices();
//Start up the filesystem watcher
diskWatcher = new FSWatcher();
connect(diskWatcher,SIGNAL(FSWarning(QString,QString)),this,SLOT(slotDisplayWarning(QString,QString)));
if(useDiskWatcher){
qDebug() << "-Starting up the disk space alert system";
diskWatcher->start(diskTimerMaxMS);
}
//Update the tray menu and icons
updateMenu();
qDebug() << "-Program now ready for use";
}
void MountTray::updateMenu(){
//Clear the menu
trayIconMenu->clear();
trayIconMenu->disconnect();
numAvail = 0;
numMount = 0;
//Iterate through all the devices and add them as necessary
for(int i=0; i<deviceList.length(); i++){
if(deviceList[i]->isConnected()) {
trayIconMenu->addAction(deviceList[i]);
numAvail++;
if(deviceList[i]->isMounted() ){ numMount++; }
deviceList[i]->updateItem(); //refresh the item now as well
}else{
//Remove any devices that are not connected
deviceList.removeAt(i);
i--; //roll back one to catch the new index values for the list
}
}
//Separate the extra options at the end
trayIconMenu->addSeparator();
trayIconMenu->addMenu(sysMenu);
//Apply the menu to the Tray
trayIcon->setContextMenu(trayIconMenu);
//Update the main icon based upon whether devices have been found
if(numAvail==0){
trayIcon->setIcon( QIcon(":icons/CDdevices-inactive.png") );
}else{
if(numMount==0){
trayIcon->setIcon( QIcon(":icons/CDdevices.png") );
}else{
trayIcon->setIcon( QIcon(":icons/CDdevices.png") );
}
}
}
void MountTray::scanInitialDevices(){
slotDevChanges(FALSE);
return;
}
int MountTray::findDeviceInList(QString newDev){
for(int i=0; i<deviceList.length(); i++){
if( deviceList[i]->device == newDev ){ return i; }
}
return -1;
}
bool MountTray::addDevice(QString dev, QString label, QString type, QString filesys){
if(!dev.startsWith(DEVICEDIR)){ dev.prepend(DEVICEDIR); }
//Check if the device is already in the list
int tot=0;
for(int i=0; i<deviceList.length(); i++){
if( deviceList[i]->device == dev ){ return false; } //already exists, do nothing
if( deviceList[i]->getDeviceName().startsWith(label) ){ tot++; }
}
//See if the label is unique as well, otherwise add a number to the end to make it unique
if(tot > 0 && !label.isEmpty()){ label.append("-"+QString::number(tot)); }
qDebug() << "Valid Device Connection:" << dev << type << label << filesys;
//Create the menu item (will automount if necessary)
MenuItem *tmp = new MenuItem(this, dev, label, type, filesys);
//connect the signals/slots
connect(tmp, SIGNAL(itemMounted(QString)), this, SLOT(openMediaDir(QString)) );
connect(tmp, SIGNAL(newMessage(QString,QString)), this, SLOT(slotDisplayPopup(QString,QString)) );
connect(tmp, SIGNAL(itemRemoved(QString)), this, SLOT(removeDevice(QString)) );
deviceList << tmp;
//Update the menu
updateMenu();
return true;
}
void MountTray::removeDevice(QString dev){
if(!dev.startsWith(DEVICEDIR)){ dev.prepend(DEVICEDIR); }
//Find the device in the list
int index = findDeviceInList(dev);
if( index == -1 ){ return; } //does not exist, do nothing
//Remove the menu entry from the list
deviceList[index]->cleanup(); //make sure it is unmounted with mountpoint removed
deviceList.removeAt(index);
//Update the menu
updateMenu();
qDebug() << "Valid Device Removal:" << dev;
}
void MountTray::slotTrayActivated(QSystemTrayIcon::ActivationReason reason) {
if(reason == QSystemTrayIcon::Trigger) {
//Make sure all the items are updated
for(int i=0; i<deviceList.length(); i++){
deviceList[i]->updateItem();
}
trayIcon->contextMenu()->popup(QCursor::pos());
}
}
void MountTray::startupDevdProc(){
devdProc = new QLocalSocket(this);
devdProc->connectToServer("/var/run/devd.pipe",QIODevice::ReadOnly | QIODevice::Text);
if( devdProc->waitForConnected(3000) ){ //Max wait of 3 sec
connect(devdProc, SIGNAL(readyRead()), this, SLOT(newDevdMessage()) );
}else{
qDebug() << " - Could not startup the devd watching process";
}
}
void MountTray::newDevdMessage(){
devdTimer->start(1500); //wait 1.5 seconds before checking for device changes
return;
}
void MountTray::slotDevChanges(bool showPopup){
//This function actually checks the system device list for changes
// and updates the available devices appropriately
if(DEBUG_MODE){ qDebug() << "Checking for Device Changes:"; }
//Get the current list of devices
QStringList nsd = DCheck->devChildren("");
//Remove all the currently managed devices
qDebug() << "Rescanning Device List:" << nsd;
for(int i=0; i<deviceList.length(); i++){
QString dev = deviceList[i]->device.section("/",-1);
if(DEBUG_MODE){ qDebug() << " - Check device:" << dev; }
int ni = nsd.indexOf(dev);
if(ni == -1){
//Device Removed
if(DEBUG_MODE){ qDebug() << " - Device no longer connected:" << dev; }
removeDevice(dev);
i--;
}else{
//Probe the device for validity if not currently mounted
if( !deviceList[i]->isMounted() ){
QString ja, jb, jc, jd; //junk variables
if( !DCheck->devInfo(dev,&ja,&jb,&jc,&jd) ){
if(DEBUG_MODE){ qDebug() << " - Device no longer valid:" << dev; }
//no longer valid device
removeDevice(dev);
i--;
}
}
nsd.removeAt(ni);
}
}
//Now Iterate through all available devices and probe them for validity
// (This should catch devices that do not "announce" their presence by creating a new device node)
for(int i=0; i<nsd.length(); i++){
//Check if it is a good device
QString dlabel, dtype, dfs, dsize; //additional output info
bool good = DCheck->devInfo(nsd[i],&dtype,&dlabel,&dfs,&dsize);
if(good){
//Now create a new entry for this device
bool added = addDevice(nsd[i],dlabel,dtype,dfs);
//Show a message bubble
if(showPopup && added){ //make sure this is not shown for previously added devices
QString title = tr("New Device");
QString message = QString( tr("%1 can now be accessed")).arg(dlabel);
slotDisplayPopup(title, message);
}
}
}
//Run the disk space check if appropriate
if(useDiskWatcher && useDiskTimerDevd && showPopup){ diskWatcher->checkFS(); }
}
void MountTray::closeTray(){
qDebug() << "pc-mounttray: closing down";
//Kill the devd watching process
qDebug() << " -Shutting down DEVD watcher";
devdProc->disconnectFromServer();
qDebug() << " -Unmounting managed devices and mount points";
for(int i=0; i<deviceList.length(); i++){
deviceList[i]->cleanup();
}
//Close down the application
exit(0);
}
void MountTray::getInitialUsername(){
//Get the original user who started the tray app
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
//qDebug() << "System Environment:" << env.toStringList();
QString username = env.value( "LOGNAME" );
//qDebug() << "First attempt user detected:" << username;
if(username=="root"){
username = env.value( "SUDO_USER" ); //try this environment variable instead
//qDebug() << "Second attempt user detected:" << username;
}
if(username=="root" || username.isEmpty() ){
//attempt another method of determining the username
QStringList uList = pcbsd::Utils::runShellCommand("who");
if( uList.length() > 0 ){
username = uList[0].section(" ",0,0,QString::SectionSkipEmpty);
for(int i=0; i<uList.length(); i++){
if( !uList[i].section(" ",0,0,QString::SectionSkipEmpty).contains(username) ){
username = "root"; //too many users logged in; this method will not work
break;
}
}
//qDebug() << "Third attempt user detected:" << username;
}
}
if(username=="root"){
QMessageBox::warning(this,tr("User Detection Error"),tr("Unable to determine the non-root user who started the application \nCan not open the file manager with root permissions") );
return;
}
USERNAME=username.simplified(); //set the global variable
if(DEBUG_MODE){ qDebug() << "-User detected:" << USERNAME; }
}
void MountTray::getDefaultFileManager(){
//Get the command to open the appropriate file manager
QString fmcmd = pcbsd::Utils::runShellCommand("de-info -fileman").join(" ");
//qDebug() << "de-info result:" << fmcmd;
fmcmd = fmcmd.remove("%s").simplified();
//qDebug() << "FM command found:" << fmcmd;
if( fmcmd.isEmpty() || fmcmd.contains("File ") ){fmcmd= "openwith"; } //Default to the "openwith" command
FILEMAN = fmcmd.simplified();
if(DEBUG_MODE){ qDebug() << "-File manager detected:" << FILEMAN; }
}
void MountTray::slotOpenMediaDir(){
openMediaDir(MOUNTDIR);
}
void MountTray::openMediaDir(QString dir){
//Open the default file-manager to the directory listed
if(dir.isEmpty()){ dir = MOUNTDIR; }
if(!dir.endsWith("/")){ dir.append("/"); } //make sure the filemanager knows it is a directory
//Make sure we can setup user permissions
if(USERNAME=="root"){
qDebug() << "Cannot open filemanager with root permissions";
return;
}
//Open the default file manager to the given directory as that user
qDebug() << "Opening the media directory with user permissions";
QString cmd = "su -m "+USERNAME+" -c \'"+FILEMAN+" \""+dir+"\"\' &";
if(DEBUG_MODE){ qDebug() << " -cmd:" << cmd; }
system( cmd.toUtf8() );
}
void MountTray::slotRescan(){
//Display a notification
qDebug() << "Re-scanning devices:";
slotDisplayPopup(tr("Please Wait"),tr("Rescanning devices attached to the system"));
//Rescan the device list for new devices
scanInitialDevices();
//Check that all the existing devices still exist
for(int i=0; i<deviceList.length(); i++){
deviceList[i]->updateItem();
}
//Run the disk check if appropriate
if(useDiskWatcher){ diskWatcher->checkFS(); }
}
void MountTray::slotOpenFSDialog(){
//Open up the Filesystem disk space monitoring dialog
diskDisplay = new FSDialog();
diskDisplay->show();
}
void MountTray::slotOpenSettings(){
//Stop the refresh timer on the watcher
diskWatcher->stop();
//Open up the settings window and apply changes as necessary
SettingsDialog *sdlg = new SettingsDialog();
sdlg->useDiskWatcher = useDiskWatcher;
sdlg->useDiskAutoTimer = useDiskTimerDevd;
sdlg->diskRefreshMS = diskTimerMaxMS;
sdlg->showDialog();
//Now parse the output and save if necessary
if(sdlg->SettingsChanged){
useDiskWatcher = sdlg->useDiskWatcher;
useDiskTimerDevd = sdlg->useDiskAutoTimer;
diskTimerMaxMS = sdlg->diskRefreshMS;
qDebug() << "INFO: Saving updated settings to file";
saveCurrentSettings(); //update the saved settings
}
//Now restart the disk watcher if enabled
if(useDiskWatcher){ diskWatcher->start(diskTimerMaxMS); }
}
void MountTray::slotOpenISO(){
//prompt for the user to select a file
QString file = QFileDialog::getOpenFileName( this, tr("Select ISO File"), QDir::homePath(), tr("ISO Files (*.iso)") );
if(file.isEmpty()){ return; } //cancelled
//check for available device node number /dev/md<number>
int num = 1;
while( QFile::exists("/dev/md"+QString::number(num)) ){ num++; }
//add it to the device tree (will automatically get picked up by the device detection method)
QString cmd = "mdconfig -a -f "+file+" -u "+QString::number(num);
system(cmd.toUtf8());
}
void MountTray::slotSingleInstance()
{
trayIcon->show();
}
void MountTray::slotDisplayPopup(QString title, QString msg){
//Display a popup bubble with the given message for 3 seconds
trayIcon->contextMenu()->hide(); //close the menu list
disconnect(trayIcon, SIGNAL(messageClicked()),0,0); //make sure only one signal/slot connection
connect(trayIcon,SIGNAL(messageClicked()),this,SLOT(slotOpenMediaDir()) );
trayIcon->showMessage(title, msg , QSystemTrayIcon::NoIcon,3000 );
}
void MountTray::slotDisplayWarning(QString title, QString msg){
//Display a popup bubble with the given message for 5 seconds
trayIcon->contextMenu()->hide(); //close the menu list
disconnect(trayIcon, SIGNAL(messageClicked()),0,0); //make sure only one signal/slot connection
connect(trayIcon,SIGNAL(messageClicked()),this,SLOT(slotOpenFSDialog()) );
trayIcon->showMessage(title, msg , QSystemTrayIcon::Warning,5000 );
}
void MountTray::loadSavedSettings(){
//The saved settings file
QString filename = QDir::homePath()+"/.mounttray.settings";
//Set the defaults
useDiskWatcher=TRUE; useDiskTimerDevd=TRUE;
diskTimerMaxMS=3600000; //1 hour refresh timer
//Now load the file
QFile file(filename);
if(file.exists()){
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "-Could not open settings file: using defaults";
return;
}
QTextStream in(&file);
while(!in.atEnd()){
QString line = in.readLine();
if(!line.startsWith("#")){ //skip comment lines
QString var = line.section(")",0,0,QString::SectionSkipEmpty).simplified();
QString val = line.section(")",1,30,QString::SectionSkipEmpty).simplified();
if(var=="UseDiskSpaceMonitoring"){
if(val.toLower() == "true"){ useDiskWatcher = TRUE;}
else{ useDiskWatcher = FALSE; }
}else if(var=="UseDiskSpaceDevdTiming"){
if(val.toLower() == "true"){ useDiskTimerDevd = TRUE;}
else{ useDiskTimerDevd = FALSE; }
}else if(var=="DiskSpaceTimingMaxMilliseconds"){
diskTimerMaxMS = val.toInt();
}
}
}
file.close();
}else{
qDebug() << "-Creating new settings file with defaults";
saveCurrentSettings();
}
}
void MountTray::saveCurrentSettings(){
//The saved settings file
QString filename = QDir::homePath()+"/.mounttray.settings";
//Now write the current values to the file
QFile file(filename);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
qDebug() << "ERROR: Could not open file to save settings:"<<filename;
return;
}
QTextStream out(&file);
out << "#pc-mounttray saved settings file\n";
out << "# DO NOT EDIT: Use the settings dialog in the application instead!!\n";
//Save the settings
out << "UseDiskSpaceMonitoring)";
if(useDiskWatcher){ out << "true\n";}
else{ out << "false\n"; }
out << "UseDiskSpaceDevdTiming)";
if(useDiskTimerDevd){ out << "true\n";}
else{ out << "false\n"; }
out << "DiskSpaceTimingMaxMilliseconds)"+QString::number(diskTimerMaxMS)+"\n";
//Now close the file
file.close();
}