[PC-BSD Commits] r17300 - pcbsd/current/src-qt4/pc-mounttray
svn at pcbsd.org
svn at pcbsd.org
Mon Jun 18 10:52:59 PDT 2012
Author: kenmoore
Date: 2012-06-18 17:52:56 +0000 (Mon, 18 Jun 2012)
New Revision: 17300
Modified:
pcbsd/current/src-qt4/pc-mounttray/mountTray.cpp
pcbsd/current/src-qt4/pc-mounttray/mountTray.h
Log:
Add automounting to the pc-mounttray as well as a submenu for each device entry (rather than just a tooltip). Automounting can now be toggled on/off from this submenu and appears to work properly
Modified: pcbsd/current/src-qt4/pc-mounttray/mountTray.cpp
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/mountTray.cpp 2012-06-18 14:07:48 UTC (rev 17299)
+++ pcbsd/current/src-qt4/pc-mounttray/mountTray.cpp 2012-06-18 17:52:56 UTC (rev 17300)
@@ -22,6 +22,7 @@
qDebug() << "pc-mounttray: starting up";
MOUNTDIR = "/media/"; //Directory for all the devices to be mounted
DEVICEDIR = "/dev/"; //Directory that new devices are discovered
+ AMFILE = QDir::homePath() + "/.pc-automounttray"; //File to save/load all the devices to be automounted
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
@@ -30,9 +31,13 @@
// 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();
+ //Load the automount file and populate the list
+ loadAutoMountFile();
+
//Startup the devd watching process
startupDevdProc();
@@ -177,7 +182,7 @@
}
bool MountTray::updateDeviceList(int act, QAction* device){
- //act: 0-verify device 1-add device 2-mount device 3-unmount device 4-remove device
+ //act: 0-verify device 1-add device 2-mount device 3-unmount device 4-remove device 5-mount silently
bool status = FALSE;
if(act==0){
@@ -221,7 +226,15 @@
numAvail--;
status = TRUE;
}
-
+
+ }else if(act==5){
+ //Mount device silently
+ if( mountDevice(device,FALSE) ){
+ device->setToolTip( device->whatsThis()+" "+tr("mounted")+" - "+MOUNTDIR+device->text().replace(" ","-") );
+ numMount++;
+ status = TRUE;
+ }
+
}else{
//Unknown identifier
qDebug() << "pc-mounttray: unknown device update identifier";
@@ -242,7 +255,7 @@
return -1;
}
-bool MountTray::mountDevice(QAction* devAct){
+bool MountTray::mountDevice(QAction* devAct,bool openDir){
if(isMounted(devAct)){
//Device is already mounted
return TRUE;
@@ -282,7 +295,7 @@
output = Utils::runShellCommand(cmd2);
if( output.join(" ").simplified().isEmpty() ){
//Mounted successfully
- openMediaDir(mntpoint); //open the mountpoint directory
+ if(openDir){ openMediaDir(mntpoint); }//open the mountpoint directory
status = TRUE;
}else{
qDebug() << "pc-mounttray: Error mounting device:" << dev;
@@ -412,15 +425,15 @@
for(int i=0; i<devs.length(); i++){
if(i==0){ qDebug() << "Existing devices discovered:"; }
QAction* newdevice = newDeviceAction( devs[i].section(":::",0,0), devs[i].section(":::",1,1) );
- devList << newdevice;
- numAvail++;
+ qDebug() << " - "<<newdevice->whatsThis()<<" -> "<<newdevice->text()<<", "<<newdevice->statusTip();
+ updateDeviceList(1, newdevice);
if(isMounted(newdevice)){
numMount++;
newdevice->setToolTip( tr("Mounted")+" - "+MOUNTDIR+newdevice->text().replace(" ","-") );
}else{
newdevice->setToolTip(tr("Unmounted - May be removed"));
}
- qDebug() << " - "<<newdevice->whatsThis()<<" -> "<<newdevice->text()<<", "<<newdevice->statusTip();
+ autoMount(newdevice,TRUE);
}
//Update the Tray Menu
@@ -436,12 +449,10 @@
//Add all the elements in the saved list to the menu
for(int i=0; i<devList.length(); i++){
+ //Create the submenu for this action and fill it
+ setSubMenu(devList[i]);
+ //Add this item to the main menu
trayIconMenu->addAction( devList[i] );
- if( isMounted(devList[i]) ){
- //trayIconMenu->addAction( tr("Mounted")+" - "+tr("Click to unmount device") );
- }else{
- //trayIconMenu->addAction( tr("Unmounted")+" - "+tr("Click to mount device") );
- }
}
trayIconMenu->addSeparator();
@@ -450,10 +461,6 @@
//Add the "close tray" entry to the list
trayIconMenu->addAction( QIcon(":icons/application-exit.png"), tr("Close Tray"), this, SLOT(closeTray()) );
- //Setup the signal/slot connections
- connect(trayIconMenu, SIGNAL(triggered(QAction*)), this, SLOT(menuDeviceToggled(QAction*)) );
- connect(trayIconMenu, SIGNAL(hovered(QAction*)), this, SLOT(menuDeviceHovered(QAction*)) );
-
//Apply the menu to the Tray
trayIcon->setContextMenu(trayIconMenu);
@@ -518,6 +525,7 @@
if( !newdev->statusTip().isEmpty() ){ //make sure the device is valid
if( updateDeviceList( 1 , newdev ) ){
showDeviceNotification(1,newdev);
+ autoMount(newdev,FALSE);
}
}else{
qDebug() << " - Ignoring device (Unknown filesystem)";
@@ -534,21 +542,33 @@
} // end loop over all devd output lines
}
-void MountTray::menuDeviceToggled(QAction* device){
+void MountTray::menuDeviceToggled(QAction* currentAct){
+ //Get the parent action (parentAct->submenu->currentAct)
+ //qDebug() << currentAct->whatsThis();
+ QAction* device = newDeviceAction( currentAct->whatsThis() );
+ //qDebug() << device->whatsThis() << device->text() << device->statusTip();
int index = findDeviceInList(device);
- if(index != -1){
- if( isMounted(device) ){
+ if(index == -1){ return;} //invalid index
+
+ if(currentAct->isCheckable()){ //toggling auto-mount
+ qDebug() << "Auto-mount toggled for device: "<< currentAct->whatsThis()<<currentAct->isChecked();
+ setAutoMount(devList[index],currentAct->isChecked());
+ autoMount(devList[index],FALSE);
+ }else{ //mount or unmount
+ if( isMounted(devList[index]) ){
//Unmount the device
if( updateDeviceList(3,devList[index]) ){
- showDeviceNotification(3,device);
+ showDeviceNotification(3,devList[index]);
}
}else{
//Mount the device
if( updateDeviceList(2,devList[index]) ){
- showDeviceNotification(2,device);
+ showDeviceNotification(2,devList[index]);
}
}
}
+ //Refresh the menu
+ updateTrayMenu();
}
void MountTray::menuDeviceHovered(QAction* device){
@@ -606,7 +626,7 @@
//qDebug() << "de-info result:" << fmcmd;
fmcmd = fmcmd.remove("%s").simplified();
//qDebug() << "FM command found:" << fmcmd;
- if( fmcmd.isEmpty() ){fmcmd= "openwith"; } //Default to the "openwith" command
+ if( fmcmd.isEmpty() || fmcmd.contains("File ") ){fmcmd= "openwith"; } //Default to the "openwith" command
FILEMAN = fmcmd.simplified();
qDebug() << "File manager detected:" << FILEMAN;
@@ -635,3 +655,89 @@
{
trayIcon->show();
}
+
+void MountTray::loadAutoMountFile(){
+ QFile file(AMFILE);
+ if(file.exists()){
+ if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
+ qDebug() << "Could not open file for reading:" << AMFILE;
+ }else{
+ QTextStream in(&file);
+ while(!in.atEnd()){
+ AMList << in.readLine();
+ }
+ }
+ file.close();
+ }
+}
+
+void MountTray::setAutoMount(QAction* device, bool enable){
+ QString listchk = device->text()+":::"+device->statusTip();
+ bool changed=FALSE;
+ if(AMList.contains(listchk) ){
+ if(enable){}//do nothing, already enabled
+ else{
+ AMList.removeAll(listchk);
+ changed=TRUE;
+ } //Remove this entry
+ }else{
+ if(enable){
+ AMList << listchk;
+ changed=TRUE;
+ }else{} //do nothing, not enabled already;
+ }
+ //Save the changes to file if needed
+ if(changed){
+ QFile file(AMFILE);
+ if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
+ qDebug() << "Could not open file for writing:" << AMFILE;
+ }else{
+ QTextStream out(&file);
+ for(int i=0; i<AMList.length(); i++){
+ out << AMList[i] << "\n";
+ }
+ }
+ file.close();
+ }
+
+}
+
+void MountTray::autoMount(QAction* device,bool onStartup){
+ if( isAutoMountEnabled(device) && !isMounted(device) ){
+ if(onStartup){ updateDeviceList(5,device); } //Mount device silently
+ else{ updateDeviceList(2, device); }//mount the device (and open dir)
+ }
+}
+
+bool MountTray::isAutoMountEnabled(QAction* device){
+ QString listchk = device->text()+":::"+device->statusTip();
+ if(AMList.contains(listchk)){ return TRUE; }
+ else{ return FALSE; }
+}
+
+void MountTray::setSubMenu(QAction* device){
+ QMenu* submenu = new QMenu;
+ QString dev = device->whatsThis().section("/",-1);
+ // - mount/unmount
+ QAction* mount = new QAction(this);
+ mount->setWhatsThis(dev);
+ if( isMounted(device) ){
+ mount->setText( tr("Unmount")+" "+dev );
+ }else{
+ mount->setText( tr("Mount")+" "+dev );
+ }
+ submenu->addAction(mount);
+ // - device automount
+ QAction* amchk = new QAction(tr("Auto-mount"),this);
+ amchk->setWhatsThis(dev);
+ amchk->setCheckable(TRUE);
+ amchk->setChecked( isAutoMountEnabled(device) );
+ submenu->addAction(amchk);
+ //Setup the signal/slot connections
+ connect(submenu, SIGNAL(triggered(QAction*)), this, SLOT(menuDeviceToggled(QAction*)) );
+ //connect(submenu, SIGNAL(hovered(QAction*)), this, SLOT(menuDeviceHovered(QAction*)) );
+ //Add the submenu
+ device->setMenu(submenu);
+
+}
+
Modified: pcbsd/current/src-qt4/pc-mounttray/mountTray.h
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/mountTray.h 2012-06-18 14:07:48 UTC (rev 17299)
+++ pcbsd/current/src-qt4/pc-mounttray/mountTray.h 2012-06-18 17:52:56 UTC (rev 17300)
@@ -33,9 +33,9 @@
private:
- QString MOUNTDIR, DEVICEDIR, USERNAME, FILEMAN;
+ QString MOUNTDIR, DEVICEDIR, USERNAME, FILEMAN, AMFILE;
QProcess *devdProc;
- QStringList devdOutput;
+ QStringList AMList;
int numMount, numAvail;
QSystemTrayIcon* trayIcon;
QMenu* trayIconMenu;
@@ -46,7 +46,7 @@
bool isConnected(QAction*);
bool updateDeviceList(int, QAction*);
int findDeviceInList(QAction*);
- bool mountDevice(QAction*);
+ bool mountDevice(QAction*,bool openDir=TRUE);
bool unmountDevice(QAction*);
QAction* newDeviceAction(QString,QString type="USB");
void updateTrayMenu();
@@ -55,5 +55,9 @@
void setDeviceIcon(QAction*);
void getInitialUsername();
void getDefaultFileManager();
-
+ void loadAutoMountFile();
+ void setAutoMount(QAction*, bool);
+ void autoMount(QAction*,bool onstartup);
+ bool isAutoMountEnabled(QAction*);
+ void setSubMenu(QAction*);
};
More information about the Commits
mailing list