[PC-BSD Commits] r16605 - pcbsd-projects/pc-mounttray
svn at pcbsd.org
svn at pcbsd.org
Mon Apr 30 15:03:12 PDT 2012
Author: kenmoore
Date: 2012-04-30 22:03:12 +0000 (Mon, 30 Apr 2012)
New Revision: 16605
Modified:
pcbsd-projects/pc-mounttray/main.cpp
pcbsd-projects/pc-mounttray/mountTray.cpp
pcbsd-projects/pc-mounttray/mountTray.h
Log:
pc-mounttray now works fairly well, just needs more testing with additional filesystems to work out any bugs.
Modified: pcbsd-projects/pc-mounttray/main.cpp
===================================================================
--- pcbsd-projects/pc-mounttray/main.cpp 2012-04-30 20:38:38 UTC (rev 16604)
+++ pcbsd-projects/pc-mounttray/main.cpp 2012-04-30 22:03:12 UTC (rev 16605)
@@ -11,7 +11,8 @@
#include <qtsingleapplication.h>
#include "mountTray.h"
-#include "../config.h"
+//#include "../config.h"
+#define PREFIX QString("/usr/local")
int main(int argc, char ** argv)
{
Modified: pcbsd-projects/pc-mounttray/mountTray.cpp
===================================================================
--- pcbsd-projects/pc-mounttray/mountTray.cpp 2012-04-30 20:38:38 UTC (rev 16604)
+++ pcbsd-projects/pc-mounttray/mountTray.cpp 2012-04-30 22:03:12 UTC (rev 16605)
@@ -26,7 +26,8 @@
trayIcon->setIcon(QIcon(":icons/USBgrey.png"));
trayIcon->show();
- //Startup the devd wathing process
+ //Startup the devd watching process
+ startupDevdProc();
//Do an initial scan of the devices with dmesg
initialDeviceScan();
@@ -34,10 +35,11 @@
qDebug() << "pc-mounttray: starting up";
//Update the tray icon
- showDeviceNotification(0,QAction("",this));
+ QAction* junk = new QAction("",this);
+ showDeviceNotification(0,junk);
}
-void MountTray::showDeviceNotification(int ident){
+void MountTray::showDeviceNotification(int ident, QAction* device){
//ident: 0-refresh icon only, 1-device connected, 2-device mounted, 3-device unmounted, 4-device disconnected
if(ident==0){
@@ -48,7 +50,7 @@
}else if(ident==2){
//Device mounted
- trayIcon->showMessage( tr("Device Mounted"), tr("Device ")+"\""+device->text()+"\""+tr(" has been mounted at ")+"/media/"+device, QSystemTrayIcon::NoIcon,3000 );
+ trayIcon->showMessage( tr("Device Mounted"), tr("Device ")+"\""+device->text()+"\""+tr(" has been mounted at ")+"/media/"+device->text(), QSystemTrayIcon::NoIcon,3000 );
}else if(ident==3){
//Device unmounted
@@ -77,25 +79,30 @@
}
-QAction MountTray::newDeviceAction(QString deviceLocation, QString deviceName){
+QAction* MountTray::newDeviceAction(QString deviceLocation, QString deviceName){
//Turn a device location into a QAction for use in this program
- QAction newdev = new QAction(this);
- newdev.setWhatsThis(deviceLocation);
- newdev.setCheckable(TRUE);
+ QAction* newdev = new QAction(this);
+ newdev->setWhatsThis(deviceLocation);
+ newdev->setCheckable(FALSE);
+ //Create a generic device name if none given
+ if(deviceName.isEmpty()){
+ int num = 0;
+ for(int i=0; i<devList.length(); i++){
+ if( devList[i]->text().contains("USB_Device_") ){
+ num++;
+ }
+ }
+ deviceName = "USB_Device_"+QString::number(num);
+ }
- newdev.setText(deviceName);
+ newdev->setText(deviceName);
- //Check if device is mounted
- if(QFile::exists("/dev/"+deviceName) ){
- newdev.setChecked(TRUE);
- }else{
- newdev.setChecked(FALSE);
- }
+ return newdev;
}
-bool MountTray::updateDeviceList(int act, QAction device){
+bool MountTray::updateDeviceList(int act, QAction* device){
//act: 0-verify device 1-add device 2-mount device 3-unmount device 4-remove device
bool status = FALSE;
@@ -104,7 +111,7 @@
if( isConnected(device) ){
status = TRUE;
}else{
- updateDeviceList(4, device);
+ status = updateDeviceList(4, device);
}
}else if(act==1){
@@ -120,7 +127,6 @@
}else if(act==2){
//Mount device
if( mountDevice(device) ){
- device->setChecked(TRUE);
device->setIcon( QIcon(":icons/USBred.png") );
device->setStatusTip( tr("Mounted")+" - /media/"+device->text() );
numMount++;
@@ -130,7 +136,6 @@
}else if(act==3){
//Unmount device
if( unmountDevice(device) ){
- device->setChecked(FALSE);
device->setIcon( QIcon(":icons/USBblue.png") );
device->setStatusTip(tr("Unmounted - May be removed"));
numMount--;
@@ -154,7 +159,7 @@
return status;
}
-int MountTray::findDeviceInList(QAction device){
+int MountTray::findDeviceInList(QAction* device){
for(int i=0; i<devList.length(); i++){
if( devList[i]->whatsThis() == device->whatsThis() ){
return i;
@@ -163,7 +168,7 @@
return -1;
}
-bool MountTray::mountDevice(QAction devAct){
+bool MountTray::mountDevice(QAction* devAct){
//Mount the device
bool status = FALSE;
@@ -171,21 +176,30 @@
QString mntpoint = "/media/" + devAct->text();
QString fstype = "msdosfs"; //make this dynamic later
+ //Create the fileystem specific command for mounting
+ QString fstypeandopts;
+ if( fstype == "msdosfs" ){ fstypeandopts = "-t msdosfs -o large,-m=644,-M=777"; }
+ else if(fstype == "ntfs"){ fstypeandopts = "-t ntfs-3g"; }
+
QString cmd1 = "mkdir " + mntpoint;
- QString cmd2 = "mount -t " + fstype + " -o -m=644 " + dev + " " + mntpoint;
+ QString cmd2 = "mount "+ fstypeandopts + " " + dev + " " + mntpoint;
//Run the mounting commands
- QStringList output;
- output = Utils::runShellCommandList(cmd1);
- if( ouput.isEmpty() ){
+ //qDebug() << "cmd1: " << cmd1;
+ //qDebug() << "cmd2: " << cmd2;
+
+ QStringList output = Utils::runShellCommand(cmd1);
+ if( output.join(" ").simplified().isEmpty() ){
//directory created, run the next command
- output = Utils::runShellCommandList(cmd2);
- if(output.isEmpty()){
+ output = Utils::runShellCommand(cmd2);
+ if( output.join(" ").simplified().isEmpty() ){
//Mounted successfully
status = TRUE;
}else{
qDebug() << "pc-mounttray: Error mounting device:" << dev;
qDebug() << " - Error message:" << output;
+ //Remove the mount point just created
+ Utils::runShellCommand("rmdir "+mntpoint);
}
}else{
qDebug() << "pc-mounttray: Error creating mountpoint:" << mntpoint;
@@ -195,7 +209,7 @@
return status;
}
-bool MountTray::unmountDevice(QAction devAct){
+bool MountTray::unmountDevice(QAction* devAct){
//Unmount the device
bool status = FALSE;
QString mntpoint = "/media/" + devAct->text();
@@ -205,11 +219,11 @@
//Run the commands
QStringList output;
- output = Utils::runShellCommandList(cmd1);
- if(output.isEmpty()){
+ output = Utils::runShellCommand(cmd1);
+ if(output.join(" ").simplified().isEmpty()){
//unmounting successful, remove the mount point directory
- output = Utils::runShellCommandList(cmd2);
- if(output.isEmpty()){
+ output = Utils::runShellCommand(cmd2);
+ if(output.join(" ").simplified().isEmpty()){
//Directory removed
status = TRUE;
}else{
@@ -224,15 +238,19 @@
return status;
}
-bool MountTray::isMounted(QAction device){
- if(device->isChecked() ){
+bool MountTray::isMounted(QAction* device){
+ //Check if device is mounted
+ QString chk = Utils::runShellCommandSearch("mount",device->whatsThis()+" "+device->text());
+ if(chk.isEmpty() ){
+ device->setChecked(FALSE);
+ return FALSE;
+ }else{
+ device->setChecked(TRUE);
return TRUE;
- }else{
- return FALSE;
}
}
-bool MountTray::isConnected(QAction device){
+bool MountTray::isConnected(QAction* device){
if( QFile::exists( device->whatsThis() ) ){
return TRUE;
}else{
@@ -245,21 +263,21 @@
//Load all the devices plugged in before the program started up
trayIconMenu = new QMenu;
trayIconMenu->clear();
- devList->clear();
+ devList.clear();
numAvail = 0;
numMount = 0;
//Scan for all the "umass" devices and add them to the device list
- qDebug() << "Still need to add USB device detection";
+ //qDebug() << "Still need to add USB device detection";
//Parse out the "dmesg | grep umass" command - does not find the device names though....
- QStringList output = Utils::runShellCommandSearch("dmesg","umass");
- QSringList devs;
+ QStringList output = Utils::listShellCommandSearch("dmesg","umass");
+ QStringList devs;
for(int i=0; i<output.length(); i++){
QString line = output[i];
if( !line.startsWith("umass") ){
if(line.contains("removing device") || line.contains("lost device")){
//Device Removed (format: line=([device]:...... )
- QString dev = line.section(":",0,0).section("(",1,1)
+ QString dev = line.section(":",0,0).section("(",1,1);
devs.removeAll(dev);
}else if(line.contains("at umass") ){
QString dev = line.section(" ",0,0,QString::SectionSkipEmpty);
@@ -270,14 +288,19 @@
//If there are any devices connected, create actions for them and add them to the list
for(int i=0; i<devs.length(); i++){
- QAction newdevice = newDeviceAction(devs[i], "USB_Device_"+i);
-
+ QAction* newdevice = newDeviceAction(devs[i], "");
devList << newdevice;
}
+ updateTrayMenu();
+}
+
+void MountTray::updateTrayMenu(){
+ trayIconMenu->clear();
+
//Add all the elements in the saved list to the menu
for(int i=0; i<devList.length(); i++){
- trayIconMenu->addAction( devlist[i] );
+ trayIconMenu->addAction( devList[i] );
}
//Add the "close tray" entry to the list
@@ -286,13 +309,14 @@
//Setup the signal/slot connection
trayIconMenu->disconnect(); //clear any existing connections
- connect(trayIconMenu, SIGNAL(triggered(QAction)), this, SLOT(menuDeviceToggled(QAction)) );
+ connect(trayIconMenu, SIGNAL(triggered(QAction*)), this, SLOT(menuDeviceToggled(QAction*)) );
//Apply the menu to the Tray
trayIcon->setContextMenu(trayIconMenu);
//Update the tray icon
- showDeviceNotification(0,QAction("",this));
+ QAction* junk = new QAction("",this);
+ showDeviceNotification(0,junk);
}
@@ -314,13 +338,15 @@
//Since messages generally come in groups, save all the output for a couple seconds before doing anything
QString line = devdProc->readAllStandardOutput();
if(devdOutput.isEmpty()){
- QTimer::singleShot(3000,this,SLOT(parseDevdOutput()) ); //Wait 3 seconds to catch all the output from devd
+ QTimer::singleShot(1500,this,SLOT(parseDevdOutput()) ); //Wait 1.5 seconds to catch all the output from devd
}
devdOutput << line.split("\n");
+ //qDebug() << "New devd.pipe message:" << line;
}
-void MoutTray::parseDevdOutput(){
+void MountTray::parseDevdOutput(){
+ //qDebug() << "Devd.pipe output:" << devdOutput;
//Parse the output to find whether a device has been added/removed
bool isAttached = FALSE;
bool isUmass = FALSE;
@@ -331,12 +357,16 @@
isUmass = TRUE;
}else if(devdOutput[i].startsWith("-umass") ){
isAttached = FALSE;
- isUMass = TRUE;
+ isUmass = TRUE;
}
if(devdOutput[i].startsWith("!system=") ){
- cdev << devdOutput[i].section("cdev=",1,1).simplified();
+ cdev << devdOutput[i].section("cdev=",1,1).section(" vendor=",0,0).simplified();
}
}
+
+ //Finished with the saved devd Output, clear it to prepare for another signal
+ devdOutput.clear();
+ //qDebug() << "cdev=" << cdev;
//If a USB mass storage device found
if(isUmass){
QString dev, devname, fstype;
@@ -344,13 +374,14 @@
//Try to find the device, device name, and filesystem type
for(int i=0; i<cdev.length(); i++){
if( !cdev[i].startsWith("usb/") && !cdev.startsWith("ugen") && !cdev.startsWith("pass") ){
- if( cdev[i].contains("/") && !QFile::exists("/dev/"+cdev[i].section("/",0,0)) ){
+ //qDebug() << cdev[i];
+ if( cdev[i].contains("/") ){
fstype = cdev[i].section("/",0,0,QString::SectionSkipEmpty);
devname = cdev[i].section("/",1,1,QString::SectionSkipEmpty);
- }else if( !cdev.contains("/") ){
+ }else{
if(isAttached && QFile::exists("/dev/"+cdev[i]) ){
dev = cdev[i];
- }else if( !isAttached && (findDeviceInList(newDeviceAction(cdev[i],"")) != -1) ){
+ }else if( !isAttached && (findDeviceInList(newDeviceAction("/dev/"+cdev[i],"")) != -1) ){
dev = cdev[i];
}
}
@@ -358,36 +389,43 @@
}
//Create the action for the device if a device detected
- if(!dev.isEmpty() ){
+ if(!dev.isEmpty() ){
+ dev.prepend("/dev/"); //Add the device location now
+ QAction* newdev = newDeviceAction(dev,devname);
if(isAttached){
- if( updateDeviceList( 1 ,newDeviceAction(dev,devname) ) ){
- showDeviceNotification(1);
+ qDebug() << "New device detected:" << dev << devname << fstype;
+ if( updateDeviceList( 1 , newdev ) ){
+ showDeviceNotification(1,newdev);
}
}else{
- if( updateDeviceList( 4 ,newDeviceAction(dev,devname) ) ){
- showDeviceNotification(4);
+ qDebug() << "Device removal detected:" << dev << devname << fstype;
+ if( updateDeviceList( 4 , newdev ) ){
+ showDeviceNotification(4,newdev);
}
}
+ //Update the Tray menu
+ updateTrayMenu();
}
} // end if umass detected
- //Finished with the saved devd Output, clear it to prepare for another signal
- devdOutput.clear();
}
-void MountTray::menuDeviceToggled(QAction device){
+void MountTray::menuDeviceToggled(QAction* device){
+ //convert QAction to a pointer
+
+
int index = findDeviceInList(device);
if( isMounted(device) ){
//Unmount the device
if( updateDeviceList(3,devList[index]) ){
- showDeviceNotification(3);
+ showDeviceNotification(3,device);
}
}else{
//Mount the device
if( updateDeviceList(2,devList[index]) ){
- showDeviceNotification(2);
+ showDeviceNotification(2,device);
}
}
Modified: pcbsd-projects/pc-mounttray/mountTray.h
===================================================================
--- pcbsd-projects/pc-mounttray/mountTray.h 2012-04-30 20:38:38 UTC (rev 16604)
+++ pcbsd-projects/pc-mounttray/mountTray.h 2012-04-30 22:03:12 UTC (rev 16605)
@@ -25,7 +25,9 @@
private slots:
void closeTray();
void slotSingleInstance();
- void MenuDeviceToggled(QAction);
+ void menuDeviceToggled(QAction*);
+ void newDevdMessage();
+ void parseDevdOutput();
//void slotTrayActivated(QSystemTrayIcon::ActivationReason);
private:
@@ -34,16 +36,17 @@
int numMount, numAvail;
QSystemTrayIcon* trayIcon;
QMenu* trayIconMenu;
- QList<QAction> devList;
- void showDeviceNotification(int);
+ QList<QAction*> devList;
+ void showDeviceNotification(int, QAction*);
void initialDeviceScan();
- bool isMounted(QAction);
- bool isConnected(QAction);
- bool updateDeviceList(int, QAction);
- int findDeviceInList(QAction);
- bool mountDevice(QAction);
- bool unmountDevice(QAction);
- QAction newDeviceAction(QString,QString);
+ bool isMounted(QAction*);
+ bool isConnected(QAction*);
+ bool updateDeviceList(int, QAction*);
+ int findDeviceInList(QAction*);
+ bool mountDevice(QAction*);
+ bool unmountDevice(QAction*);
+ QAction* newDeviceAction(QString,QString);
+ void updateTrayMenu();
void startupDevdProc();
};
More information about the Commits
mailing list