[PC-BSD Commits] r19636 - pcbsd/current/src-qt4/pc-mounttray

svn at pcbsd.org svn at pcbsd.org
Wed Oct 10 12:03:22 PDT 2012


Author: kenmoore
Date: 2012-10-10 19:03:21 +0000 (Wed, 10 Oct 2012)
New Revision: 19636

Modified:
   pcbsd/current/src-qt4/pc-mounttray/menuItem.cpp
   pcbsd/current/src-qt4/pc-mounttray/menuItem.h
   pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.pro
   pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.qrc
Log:
Large checkpoint for the new Mounttray interface for 9.2
I have not seen anything worthy of being backported to 9.1 yet.



Modified: pcbsd/current/src-qt4/pc-mounttray/menuItem.cpp
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/menuItem.cpp	2012-10-10 17:56:03 UTC (rev 19635)
+++ pcbsd/current/src-qt4/pc-mounttray/menuItem.cpp	2012-10-10 19:03:21 UTC (rev 19636)
@@ -2,8 +2,10 @@
 #include <pcbsd-utils.h>
 #include "menuItem.h"
 
-MenuItem::MenuItem(QWidget* parent, QString newdevice) : QWidget(parent)
+MenuItem::MenuItem(QWidget* parent, QString newdevice, bool automount) : QWidget(parent)
 {
+  //Set the direcectory to mount devices in
+  MOUNTDIR="/media/";
   //Set the device
   device = newdevice;
   isSelected = FALSE;
@@ -13,24 +15,31 @@
   devLabel = new QLabel;
   devIcon = new QLabel;
   currentSpace = new QProgressBar;
+    currentSpace->setMinimum(0);
   pushMount = new QPushButton;
-  pushChangeLabel = new QPushButton;
   checkAutomount = new QCheckBox;
+    checkAutomount->setChecked(automount);
   //Add the widgets to the layout
+  layout->addWidget(devIcon,0,0,3,1);	
   layout->addWidget(devLabel,0,1);
-  layout->addWidget(devIcon,0,0);
-  layout->addWidget(currentSpace,1,0,1,2);
-  layout->addWidget(pushMount,2,0,1,2);
-  layout->addWidget(pushChangeLabel,3,0);
-  layout->addWidget(checkAutoMount,3,1);
+  layout->addWidget(pushMount,0,2);
+  layout->addWidget(currentSpace,1,1,1,2);
+  layout->addWidget(checkAutomount,2,1);
   //Set the layout
   this->setLayout(layout);
   
   //Connect the signals/slots
   connect(pushMount,SIGNAL(clicked()), this, SLOT(slotMountClicked()));
+  connect(checkAutomount,SIGNAL(toggled(bool)),this,SLOT(slotAutoMountToggled(bool)));
   
+  //Start the automount procedure if necessary
+  if(automount){
+    mountItem();
+  }
+  
   //Update the Item based upon current device status
   updateItem();
+
 }
 
 MenuItem::~MenuItem(){
@@ -52,25 +61,19 @@
   if( isConnected() ){
     if( isMounted() ){
       devIcon->setEnabled(TRUE);  //Make the icon full color
+      pushMount->setText(tr("Eject"));
+      pushMount->setIcon(QIcon(":icons/eject.png"));
     }else{	  
       devIcon->setEnabled(FALSE); //Grey out the icon
+      pushMount->setText(tr("Mount"));
+      pushMount->setIcon(QIcon(":icons/mount.png"));
     }
   }else{
-    emit itemRemoved(this);
+    emit itemRemoved(device);
     return;
   }
-  //Set visibility
-  if( isSelected() ){
-    currentSpace->setVisible(TRUE);
-    pushMount->setVisible(TRUE);
-    pushChangeLabel->setVisible(TRUE);
-    checkAutoMount->setVisible(TRUE);
-  }else{
-    currentSpace->setVisible(FALSE);
-    pushMount->setVisible(FALSE);
-    pushChangeLabel->setVisible(FALSE);
-    checkAutoMount->setVisible(FALSE);
-  }
+  //Set visibility and sizes on progressbar
+  updateSizes();
 }
 	
 //Getters
@@ -79,20 +82,100 @@
 }
 	
 //Device information
-bool isConnected(){
+bool MenuItem::isConnected(){
   if( QFile::exists(device) ){ return TRUE; }
   else{ return FALSE; }
 }
 
-bool isMounted(){
+bool MenuItem::isMounted(){
   //Check if device is mounted
-  QString chk = Utils::runShellCommandSearch("mount",device->whatsThis());  
-  if(chk.isEmpty() ){ chk = Utils::runShellCommandSearch("mount",device->text().replace(" ","-")); } 
+  QString chk = Utils::runShellCommandSearch("mount",device);  
+  if(chk.isEmpty() ){ chk = Utils::runShellCommandSearch("mount",devLabel->text().replace(" ","-")); } 
 
   if(chk.isEmpty() ){ return FALSE; }
   else{ return TRUE; }
 }
 	
+void MenuItem::getDevInfo(QString dev, QString* type, QString* label, QString* filesystem, QString* maxsize){
+  //Clear the output variables
+  type->clear(); label->clear(); filesystem->clear(); maxsize->clear();
+  //make sure to use the absolute path
+  if(!dev.startsWith("/dev/")){ dev.prepend("/dev/"); }
+  
+  //Check for valid device types
+  QStringList validdev, devtype;
+  validdev << "da" << "ad"    << "mmcsd" << "cd"         << "acd";
+  devtype << "USB"<<"SATA"<< "SD"        << "CD9660" << "CD9660";
+  QString node = dev.section("/",-1);
+  for(int i=0; i<validdev.length(); i++){
+    if(node.startsWith(validdev[i])){ type->append(devtype[i]); break; }
+  }
+  //Make sure we quit before running commands on any invalid device nodes
+  if(type->isEmpty()){return;}
+  
+  //Read the Device Info using "file -s <device>"
+  QString cmd = "file -s "+dev;
+  QString output = Utils::runShellCommand(cmd).join(" ");
+  //qDebug() << "File -s output:" << output;  
+  
+  // ** Get the max storage size **
+  QStringList tmp = output.split(",");
+  int kb;
+  //qDebug() << "tmp:" << tmp;
+  if( !tmp.filter("number of data blocks").isEmpty() ){
+    tmp = tmp.filter("number of data blocks");
+    kb = tmp[0].remove("number of data blocks").simplified().toInt();
+  }else if( !tmp.filter("number of blocks").isEmpty() ){
+    tmp = tmp.filter("number of blocks");
+    kb = tmp[0].remove("number of blocks").simplified().toInt();
+  }else if( !tmp.filter("sectors ").isEmpty()){
+    tmp = tmp.filter("sectors ");
+    kb = tmp[0].remove("sectors ").section("(",0,0).simplified().toInt();
+  }else{ kb = 1; }
+  maxsize->append( QString::number(kb) );
+  
+  
+  // ** Get the device label **
+  QString dlabel = output.section("label: \"",1,1).section("\"",0,0).simplified(); //device name
+  
+  if(dlabel.isEmpty()){
+    //Try using glabel to find the device label if it was not found
+    QStringList glout = Utils::runShellCommandSearch("glabel list "+dev,"Name:").split("\n");
+    for(int i=0; i<glout.length(); i++){
+      if(!glout[i].contains(dev)){
+        dlabel = glout[i].section("Name:",1,1).section("/",-1).simplified();
+	break;
+      }
+    }
+  }
+  //Set the Device Name
+  if(dlabel.isEmpty()){
+    dlabel = *type+"-Device";
+  }
+  label->append(dlabel);
+  
+  // - trim the label out of the output line for filesystem type detection
+  QString devFSsec = output.section("label:",0,0);
+  QString devFSsec2 = output.section("label:",1,3).section(",",1,1,QString::SectionSkipEmpty);
+  
+  // ** Find the filesystem type for the device **
+  QString filesys;
+  QStringList fsdetect, fslabel;
+  fsdetect << "FAT" << "NTFS" << "EXT" << "ISO 9660" << "Unix Fast File system" << "Reiser" << "XFS"; //string to match for a particular filesystem
+  fslabel << "FAT" << "NTFS" << "EXT" << "CD9660" << "UFS" << "REISERFS" << "XFS"; //internal labels for the filesystems
+  for(int i=0; i<fsdetect.length(); i++){
+    if(devFSsec.contains(fsdetect[i]) || devFSsec2.contains(fsdetect[i]) ){
+      filesys = fslabel[i]; 
+    }
+  }
+  //If the filesystem could not be detected or is not supported
+  if(filesys.isEmpty()){ filesys = "UNKNOWN"; }
+  //save the filesystem type
+  filesystem->append(filesys);
+  
+
+}
+
 /*
   PRIVATE FUNCTIONS
 */
@@ -104,6 +187,175 @@
       unmountItem();  
     }
   }else{
-    emit itemRemoved(this);	  
+    emit itemRemoved(device);	  
   }
 }
+
+void MenuItem::slotAutoMountToggled(){
+  if(checkAutomount->isChecked()){
+    emit itemAllowAutoMount( devLabel->text(), filesystem, maxSize);
+  }else{
+    emit itemDisallowAutoMount( devLabel->text(), filesystem, maxSize);	  
+  }
+}
+
+void MenuItem::mountItem(){
+  //Mount the device
+  
+  //Create the full path to the mountpoint
+  QString deviceName = devLabel->text();
+  QString mntpoint = MOUNTDIR + deviceName.replace(" ","-"); //take into account spaces in the name
+
+  //Create the fileystem specific command for mounting
+  QString fstype;
+  QString fsopts="";
+  if( filesystem == "FAT" ){ fstype = "mount -t msdosfs"; fsopts = "-o large,-m=644,-M=777"; }
+  else if(filesystem == "NTFS"){ fstype = "ntfs-3g"; }
+  else if(filesystem == "EXT"){ fstype = "mount -t ext2fs"; }
+  else if(filesystem == "CD9660"){ fstype = "mount -t cd9660"; }
+  else if(filesystem == "UFS"){ fstype = "mount -t ufs"; }
+  else if(filesystem == "REISERFS"){ fstype = "mount -t reiserfs"; }
+  else if(filesystem == "XFS"){ fstype = "mount -t xfs"; }
+  else{
+    qDebug() << "Unknown device filesystem:" << device << filesystem << " attempting mount_auto command";
+    fstype = "mount_auto";
+    //QMessageBox::warning(this,tr("Unknown Device Filesystem"),tr("The filesystem on this device is unknown and cannot be mounted at this time") );
+    //return FALSE;
+  }
+  //Make sure the mntpoint is available
+  QDir mpd(mntpoint);
+  if(mpd.exists()){
+    //Remove the existing directory (will work only if it is empty)
+    mpd.cdUp();
+    mpd.rmdir(mntpoint);
+  }
+  //Prepare the commands to run
+  QString cmd1 = "mkdir " + mntpoint;
+  QString cmd2 = fstype + " " +fsopts + " " + device + " " + mntpoint;
+  
+  qDebug() << "Mounting device" << device << "on" << mntpoint << "("<<filesystem<<")";
+  qDebug() << " - command:" << cmd2;
+  
+  bool ok = FALSE;
+  QString result;
+  //Run the mounting commands
+  QStringList output = Utils::runShellCommand(cmd1);
+  if( output.join(" ").simplified().isEmpty() ){
+    //directory created, run the next command
+    output = Utils::runShellCommand(cmd2);
+    if( output.join(" ").simplified().isEmpty() ){
+      result = QString( tr("Successfully mounted %1 at %2") ).arg(deviceName).arg(mntpoint);
+      ok = TRUE;
+    }else{
+      qDebug() << "pc-mounttray: Error mounting device:" << device;
+      qDebug() << " - Error message:" << output;
+      result = QString( tr("Error mounting %1 at %2") +"\n"+output.join(" ") ).arg(deviceName).arg(mntpoint);
+      //Remove the mount point just created
+      Utils::runShellCommand("rmdir "+mntpoint);
+    }
+  }else{
+    qDebug() << "pc-mounttray: Error creating mountpoint:" << mntpoint;
+    qDebug() << " - Error message:" << output;
+    result = QString( tr("Error mounting %1") +"/n"+ tr("Could not create mount point at %2") ).arg(deviceName).arg(mntpoint);
+  }
+  //Output the proper signals depending upon success
+  if(ok){
+    emit itemMounted(mntpoint);
+    mountpoint = mntpoint;
+  }else{
+    mountpoint.clear();
+  }
+  emit newMessage(result);
+  
+}
+
+void MenuItem::unmountItem(){
+  //Unmount the device
+
+  //Check to see if the current mountpoint exists or if it is somewhere else
+  if( !QFile::exists(mountpoint) ){
+    if( isMounted() ){  //double check that it is actually mounted
+      //mounted someplace else - find it
+      QString output = Utils::runShellCommandSearch("mount",device);
+      mountpoint = output.section(" on ",1,1).section(" (",0,0).replace(" ","-");
+    }else{
+      //it is not mounted to begin with
+      return;
+    }
+  }
+  
+  QString cmd1 = "umount " + mountpoint;
+  QString cmd2 = "rmdir " + mountpoint;
+  qDebug() << "Unmounting device from" << mountpoint;
+  //Run the commands
+  QStringList output;
+  QString result;
+  bool ok = FALSE;
+  output = Utils::runShellCommand(cmd1);
+  if(output.join(" ").simplified().isEmpty()){
+    //unmounting successful, remove the mount point directory
+    output = Utils::runShellCommand(cmd2);
+    if(!output.join(" ").simplified().isEmpty()){
+      qDebug() << "pc-mounttray: Error removing mountpoint:" << mountpoint;
+      qDebug() << " - Error message:" << output;
+    }
+    ok = TRUE;
+    result = QString( tr("%1 has been successfully unmounted.")+"\n"+tr("It is now safe to remove the device") ).arg(devLabel->text());
+  }else{
+    qDebug() << "pc-mounttray: Error unmounting mountpoint:" << mountpoint;
+    qDebug() << " - Error message:" << output;
+    result = QString( tr("Error: %1 could not be unmounted")+"\n"+output.join(" ") ).arg(devLabel->text());
+  }
+  //emit the proper signals
+  if(ok){
+    emit itemUnmounted(device);
+    mountpoint.clear();
+  }
+  emit newMessage(result);
+}
+
+void MenuItem::updateSizes(){
+  //this method only works if the device is currently mounted
+  bool ok = FALSE;
+  if(isMounted()){
+    QString cmd = "df "+device;
+    QStringList output = Utils::runShellCommand(cmd);
+    if(output.length() > 1){
+      //parse the output (1K blocks) and save them
+      QString line = output[1].replace("\t"," ");
+      maxSize = line.section(" ",1,1,QString::SectionSkipEmpty).simplified();
+      currentSize = line.section(" ",2,2,QString::SectionSkipEmpty).simplified();
+      ok=TRUE;
+    }else{
+      maxSize.clear();
+      currentSize.clear();	    
+    }
+  }else{
+    maxSize.clear();
+    currentSize.clear();
+  }
+  //Now setup the display progressbar display
+  if(ok){
+    currentSpace->setMaximum( maxSize.toInt() );
+    currentSpace->setValue( currentSize.toInt() );
+    currentSpace->setVisible(TRUE);
+    //display the actual size available in the tooltip
+    QString diskAvailable = getSizeDisplay( maxSize.toInt() - currentSize.toInt() );
+    currentSpace->setToolTip( QString( tr("%1 of disk space available") ).arg(diskAvailable) );
+  }else{
+    currentSpace->setVisible(FALSE);
+  }
+  	
+}
+
+QString MenuItem::getSizeDisplay(int kb){
+  //convert from KB to human readable output
+  if( kb > 1048576 ){ //display in GB
+    return QString::number( double(int( (kb*100)/1048576 )/100.0) )+"GB";
+  }else if( kb > 1024 ){ //display in MB
+    return QString::number( double(int( (kb*100)/1024 )/100.0) )+"MB";	  
+  }else{ //display in KB
+    return QString::number( kb )+"KB";	  
+  }
+	
+}

Modified: pcbsd/current/src-qt4/pc-mounttray/menuItem.h
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/menuItem.h	2012-10-10 17:56:03 UTC (rev 19635)
+++ pcbsd/current/src-qt4/pc-mounttray/menuItem.h	2012-10-10 19:03:21 UTC (rev 19636)
@@ -6,6 +6,7 @@
 #define MENU_ITEM_H
 
 #include <QWidget>
+#include <QDir>
 #include <QFile>
 #include <QPushButton>
 #include <QProgressBar>
@@ -19,12 +20,15 @@
 	Q_OBJECT
 
   public:
-	MenuItem(QWidget* parent = 0, QString newdevice);
+	MenuItem(QWidget* parent = 0, QString newdevice = "", bool automount = FALSE);
 	~MenuItem();
 	
 	QString device;
   	QString devType;
   	QString filesystem;
+        QString mountpoint;
+        QString maxSize;  //number in KB saved as a QString
+        QString currentSize; //number in KB saved as a QString
 	
   	//Setters
 	void setDeviceName(QString);
@@ -37,28 +41,35 @@
 	//Device information
 	bool isConnected();
 	bool isMounted();
+	//static function for getting a device's info
+	static void getDevInfo(QString, QString*, QString*, QString*, QString*);
         
   private:
+	QString MOUNTDIR;
   	QLabel* devLabel;
   	QLabel* devIcon;
   	QProgressBar* currentSpace;
   	QPushButton* pushMount;
-  	QPushButton* pushChangeLabel;
   	QCheckBox* checkAutomount;
   	bool isSelected;
 
 	void mountItem();
   	void unmountItem();
+        void updateSizes();
+        QString getSizeDisplay(int);
   	
   private slots:
   	void slotMountClicked();
+        void slotAutoMountToggled();
 	
   signals:
 	//Emits these signals whenever needed
-	void itemMounted(QString mountDir);
-	void itemUnmounted(QString deviceNode);
-	void itemChangeLabel(MenuItem);
-	void itemRemoved(MenuItem);
+	void itemMounted(QString);  	//device node (/dev/da*)
+	void itemUnmounted(QString); 	//device node
+	void itemRemoved(QString);  	//device node
+        void itemAllowAutoMount(QString,QString,QString); 	//device label, filesystem, and size in kB (in that order)
+        void itemDisallowAutoMount(QString,QString,QString); 	// device label, filesystem, and size in kB (in that order)
+        void newMessage(QString);	//message to be displayed
 
 };
 #endif

Modified: pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.pro
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.pro	2012-10-10 17:56:03 UTC (rev 19635)
+++ pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.pro	2012-10-10 19:03:21 UTC (rev 19636)
@@ -6,9 +6,9 @@
 
 LIBS    += -lQtSolutions_SingleApplication-head -lpcbsd
 
-HEADERS	+= mountTray.h
+HEADERS	+= mountTray.h menuItem.h
 
-SOURCES	+= main.cpp mountTray.cpp
+SOURCES	+= main.cpp mountTray.cpp menuItem.cpp
 
 RESOURCES += pc-mounttray.qrc
 

Modified: pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.qrc
===================================================================
--- pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.qrc	2012-10-10 17:56:03 UTC (rev 19635)
+++ pcbsd/current/src-qt4/pc-mounttray/pc-mounttray.qrc	2012-10-10 19:03:21 UTC (rev 19636)
@@ -7,5 +7,7 @@
     <file>icons/CDdevices-inactive.png</file>
     <file>icons/folder.png</file>
     <file>icons/application-exit.png</file>
+    <file>icons/eject.png</file>
+    <file>icons/mount.png</file>
   </qresource>
 </RCC>



More information about the Commits mailing list