source: src-qt4/pc-mounttray/menuItem.cpp @ c69425e

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since c69425e was c69425e, checked in by Ken Moore <ken@…>, 6 months ago

Fix a bug with the mount tray where memory disks mounted outside the tray are automatically unmounted by the tray.
TRAC Ticket: https://trac.pcbsd.org/ticket/773

  • Property mode set to 100644
File size: 14.1 KB
Line 
1#include <pcbsd-utils.h>
2#include "menuItem.h"
3
4
5MenuItem::MenuItem(QWidget* parent, QString newdevice, QString newlabel, QString newtype, QString newfs) : QWidgetAction(parent)
6{
7  AMFILE= QDir::homePath() + "/.pc-automounttray";   //File to save/load all the devices to be automounted
8  //Set the device info variables
9  if( !newdevice.startsWith(DEVICEDIR) ){ newdevice.prepend(DEVICEDIR); }
10  device = newdevice;
11  devType = newtype;
12  filesystem = newfs;
13  //Create the layout
14  QGridLayout* layout = new QGridLayout();
15  QHBoxLayout* hlayout = new QHBoxLayout();
16  //Create the gui items
17  devLabel = new QLabel;
18    devLabel->setToolTip(device);
19    devLabel->setText(newlabel);
20   
21  devIcon = new QLabel;
22    devIcon->setToolTip(device);
23  currentSpace = new QProgressBar;
24    currentSpace->setMinimum(0);
25  pushMount = new QPushButton;
26  checkAutomount = new QCheckBox;
27    checkAutomount->setChecked( checkSavedAutoMount() );
28    checkAutomount->setText( tr("Auto-mount this device") );
29  //Add the widgets to the layout
30  layout->addWidget(devIcon,0,0,3,1);   
31  hlayout->addWidget(devLabel);
32  hlayout->addWidget(pushMount);
33  layout->addLayout(hlayout,0,1);
34  layout->addWidget(currentSpace,1,1);
35  layout->addWidget(checkAutomount,2,1);
36  //Set the layout
37  frame = new QWidget();
38  frame->setLayout(layout);
39  this->setDefaultWidget(frame);
40
41  //Connect the signals/slots
42  connect(pushMount,SIGNAL(clicked()), this, SLOT(slotMountClicked()));
43  connect(checkAutomount,SIGNAL(toggled(bool)),this,SLOT(slotAutoMountToggled(bool)));
44
45  //Setup the device Icon based on the type
46  if(devType == "USB"){ devIcon->setPixmap(QPixmap(":icons/usb.png")); }
47  else if(devType == "SATA"){ devIcon->setPixmap(QPixmap(":icons/harddrive.png")); }
48  else if(devType == "SD"){ devIcon->setPixmap(QPixmap(":icons/sdcard.png")); }
49  else if(devType == "CD9660"){ devIcon->setPixmap(QPixmap(":icons/dvd.png")); }
50  else if(devType == "ISO"){devIcon->setPixmap(QPixmap(":icons/dvd.png")); }
51  else if(devType == "SCSI"){devIcon->setPixmap(QPixmap(":icons/harddrive.png")); }
52  //Start the automount procedure if necessary
53  if(checkAutomount->isChecked() || devType=="ISO"){
54    QTimer::singleShot(500,this,SLOT( slotAutoMount() ));
55  }
56  //Update the Item based upon current device status
57  updateItem();
58
59}
60
61MenuItem::~MenuItem(){
62}
63
64/*
65  PUBLIC FUNCTIONS
66*/
67
68void MenuItem::updateItem(){
69  //Update the item visuals, based upon current device status
70  if( isConnected() ){
71    if( isMounted() ){
72      if(mountpoint.isEmpty()){
73        //detect the current mountpoint
74        QString output = pcbsd::Utils::runShellCommandSearch("mount",device);
75        mountpoint = output.section(" on ",1,1).section(" (",0,0).replace(" ","-");
76      }
77      devIcon->setEnabled(TRUE);  //Make the icon full color
78      devIcon->setToolTip(device+"\n"+QString(tr("Mounted at %1")).arg(mountpoint));
79      pushMount->setText(tr("Eject"));
80      pushMount->setIcon(QIcon(":icons/eject.png"));
81      if(devType != "ISO"){ checkAutomount->setVisible(TRUE); }
82      else{ checkAutomount->setVisible(FALSE); }
83    }else{       
84      devIcon->setEnabled(FALSE); //Grey out the icon if not mounted
85      devIcon->setToolTip(device);
86      pushMount->setText(tr("Mount"));
87      pushMount->setIcon(QIcon(":icons/mount.png"));
88      checkAutomount->setVisible(FALSE);
89    }
90  }else{
91    emit itemRemoved(device);
92    return;
93  }
94  //Set visibility and sizes on progressbar
95  updateSizes();
96}
97       
98//Getters
99QString MenuItem::getDeviceName(){
100  return devLabel->text();     
101}
102       
103//Device information
104bool MenuItem::isConnected(){
105  if( QFile::exists(device) ){ return TRUE; }
106  else{ return FALSE; }
107}
108
109bool MenuItem::isMounted(){
110  //Check if device is mounted
111  QStringList chk = pcbsd::Utils::runShellCommand("mount");
112  bool mounted=false;
113  for(int i=0; i<chk.length(); i++){
114    mounted = chk[i].contains(device) || chk[i].contains(devLabel->text()) || chk[i].contains(devLabel->text().replace(" ","-"));
115    if(mounted){ 
116      //Save the mountpoint if it is mounted
117      mountpoint = chk[i].section(" on ",1,10).section("(",0,0).simplified();
118      break; 
119    }
120  }
121  return mounted;
122}
123
124//Cleanup function
125void MenuItem::cleanup(){
126  if( isMounted() ){
127    unmountItem(); //unmount and remove mountpoint
128  }else{
129    //Just check for mountpoint removal
130    if(QFile::exists(mountpoint)){
131      qDebug() << "Removing old mountpoint:" << mountpoint;
132      QString output = pcbsd::Utils::runShellCommand("rmdir "+mountpoint).join(" ");
133      if(!output.isEmpty()){ qDebug() << " -Error:" <<output; }
134    }
135  }
136}
137/*
138  PRIVATE FUNCTIONS
139*/
140void MenuItem::slotMountClicked(){
141  if( isConnected() ){
142    if( !isMounted() ){
143      mountItem();
144    }else{
145      unmountItem(); 
146    }
147  }else{
148    emit itemRemoved(device);     
149  }
150  updateItem();
151}
152
153void MenuItem::slotAutoMount(){
154//Just like slotMountClicked, but will only mount the device if appropriate (no removals);
155  if( isConnected() ){
156    if( !isMounted() ){
157      mountItem();
158    }
159  }else{
160    emit itemRemoved(device);     
161  }
162  updateItem(); 
163}
164
165void MenuItem::slotAutoMountToggled(bool checked){
166  qDebug() << "Auto-mount toggled for:" << device << checked;
167  QString entry = devLabel->text()+":::"+devType+":::"+filesystem;
168  if(checked){
169    //Add this entry to the auto-mount file
170    QString cmd = "echo \""+entry+"\" >> "+AMFILE;
171    system( cmd.toUtf8() );
172  }else{
173    //Remove this entry from the automount file
174    QString tmpFile = AMFILE+".tmp";
175    QString cmd = "cat "+AMFILE+" | grep -v "+entry+" > "+tmpFile+"; mv "+tmpFile+" "+AMFILE;
176    system( cmd.toUtf8() );
177  }
178}
179
180bool MenuItem::checkSavedAutoMount(){
181  if(QFile::exists(AMFILE)){
182    QString cmd = "cat "+AMFILE;
183    QString search = devLabel->text() +" "+ devType +" "+ filesystem;
184    QString chk = pcbsd::Utils::runShellCommandSearch(cmd, search);
185    if( chk.isEmpty() ){ return FALSE; }
186    else{ return TRUE; }
187  }else{
188    return FALSE;
189  }
190}
191
192void MenuItem::mountItem(){
193  //Mount the device
194 
195  //Create the full path to the mountpoint
196  QString deviceName = devLabel->text();
197  QString mntpoint = MOUNTDIR + deviceName.replace(" ","-"); //take into account spaces in the name
198
199  //Create the fileystem specific command for mounting
200  QString fstype;
201  QString fsopts="";
202  if( filesystem == "FAT" ){ fstype = "mount -t msdosfs"; fsopts = "-o large,longnames,-m=644,-M=777"; }
203  else if(filesystem == "NTFS"){ fstype = "ntfs-3g"; }
204  else if(filesystem == "EXT"){ fstype = "mount -t ext2fs"; }
205  else if(filesystem == "CD9660"){ fstype = "mount -t cd9660"; }
206  else if(filesystem == "UFS"){ fstype = "mount -t ufs"; }
207  else if(filesystem == "REISERFS"){ fstype = "mount -t reiserfs"; }
208  else if(filesystem == "XFS"){ fstype = "mount -t xfs"; }
209  else{
210    qDebug() << "Unknown device filesystem:" << device << filesystem << " attempting mount_auto command";
211    fstype = "mount_auto";
212    //QMessageBox::warning(this,tr("Unknown Device Filesystem"),tr("The filesystem on this device is unknown and cannot be mounted at this time") );
213    //return FALSE;
214  }
215  //Make sure the mntpoint is available
216  QDir mpd(mntpoint);
217  if(mpd.exists()){
218    //Remove the existing directory (will work only if it is empty)
219    mpd.cdUp();
220    mpd.rmdir(mntpoint);
221  }
222  //Prepare the commands to run
223  QString cmd1 = "mkdir " + mntpoint;
224  QString cmd2 = fstype + " " +fsopts + " " + device + " " + mntpoint;
225  QString cmd3 = "chmod 777 " + mntpoint; //to set full user/root access
226 
227  qDebug() << "Mounting device" << device << "on" << mntpoint << "("<<filesystem<<")";
228  if(DEBUG_MODE){ qDebug() << " - command:" << cmd2; }
229 
230  bool ok = FALSE;
231  QString result, title;
232  //Run the mounting commands
233  QStringList output = pcbsd::Utils::runShellCommand(cmd1);
234  if( output.join(" ").simplified().isEmpty() ){
235    //directory created, run the next commands
236    system(cmd3.toUtf8()); //set directory permissions before mounting device
237    output = pcbsd::Utils::runShellCommand(cmd2);
238    if( output.join(" ").simplified().isEmpty() ){
239      title = tr("Success");
240      result = QString( tr("%1 mounted at %2") ).arg(deviceName).arg(mntpoint);
241      ok = TRUE;
242    }else{
243      qDebug() << "pc-mounttray: Error mounting device:" << device;
244      qDebug() << " - Error message:" << output;
245      title = QString( tr("Error mounting %1 at %2") ).arg(deviceName).arg(mntpoint);
246      result =  output.join(" ");
247      //Remove the mount point just created
248      pcbsd::Utils::runShellCommand("rmdir "+mntpoint);
249    }
250  }else{
251    qDebug() << "pc-mounttray: Error creating mountpoint:" << mntpoint;
252    qDebug() << " - Error message:" << output;
253    title = QString( tr("Error mounting %1") ).arg(deviceName);
254    result =  QString( tr("Could not create mount point at %1") ).arg(mntpoint);
255  }
256  //Output the proper signals depending upon success
257  if(ok){
258    emit itemMounted(mntpoint);
259    mountpoint = mntpoint;
260  }else{
261    mountpoint.clear();
262  }
263  if( !checkAutomount->isChecked() ){
264    emit newMessage(title, result); //suppress the output message if it was automounted
265  }
266 
267}
268
269void MenuItem::unmountItem(bool force){
270  //Unmount the device
271       
272  //Check to see if the current mountpoint exists or if it is somewhere else
273  if( !QFile::exists(mountpoint) ){
274    if( !isMounted() ){  //double check that it is actually mounted (and fix the mountpoint)
275      //it is not mounted to begin with
276      return;
277    }
278  }
279  //Make sure there are no spaces in the mounpoint path
280  QString cmd1 = "umount \"" + mountpoint +"\"";
281  if(force){ cmd1.replace("umount ","umount -f "); }
282  QString cmd2 = "rmdir \"" + mountpoint +"\"";
283  qDebug() << "Unmounting device from" << mountpoint;
284  //Run the commands
285  QStringList output;
286  QString result, title;
287  bool ok = FALSE;
288  output = pcbsd::Utils::runShellCommand(cmd1);
289  if(output.join(" ").simplified().isEmpty()){
290    //unmounting successful, remove the mount point directory
291    if(mountpoint != "/mnt" && mountpoint != "/media"){ //make sure not to remove base directories
292      output = pcbsd::Utils::runShellCommand(cmd2);
293    }
294    if(!output.join(" ").simplified().isEmpty()){
295      qDebug() << "pc-mounttray: Error removing mountpoint:" << mountpoint;
296      qDebug() << " - Error message:" << output;
297    }
298    ok = TRUE;
299    title = QString( tr("%1 has been successfully unmounted.") ).arg(devLabel->text());
300    if(devType == "ISO"){
301      result = tr("The ISO file has been completely detached from the system.");
302    }else{
303      result = tr("It is now safe to remove the device");
304    }
305  }else{
306    if(!force){
307      if(QMessageBox::Yes == QMessageBox::question(0,tr("Device Busy"),
308                 tr("The device appears to be busy. Would you like to unmount it anyway?")+"\n\n"+tr("NOTE: This is generally not recommended unless you are sure that you don't have any applications using the device."),
309                 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
310        unmountItem(true); //force the unmount recursively
311        return;
312      }
313    }
314    qDebug() << "pc-mounttray: Error unmounting mountpoint:" << mountpoint;
315    qDebug() << " - Error message:" << output;
316    title = QString( tr("Error: %1 could not be unmounted") ).arg(devLabel->text());
317    result = output.join(" ");
318  }
319  //emit the proper signals
320  if(ok){
321    mountpoint.clear();
322    if(devType=="ISO" && device.section("/",-1).startsWith("md") ){
323      //Get the md number
324      QString num = device.section("/md",-1).simplified();
325      //also remove the MD device from the system using "mdconfig"
326      qDebug() << "Detaching Memory Disk:" << num;
327      QString cmd = "mdconfig -d -u "+num;
328      system(cmd.toUtf8());
329    }
330    emit itemUnmounted(device);
331  }
332  emit newMessage(title, result);
333}
334
335void MenuItem::updateSizes(){
336  //this method only works if the device is currently mounted
337  bool ok = FALSE;
338  if(isMounted()){
339    QString cmd = "df \""+mountpoint+"\"";
340    QStringList output = systemCMD(cmd); //make sure we use the one with a 1K blocksize
341    if(output.length() > 1){
342      //parse the output (1K blocks) and save them
343      QString line = output[1].replace("\t"," ");
344      //qDebug() << "df output:" << output << cmd;
345      maxSize = line.section(" ",1,1,QString::SectionSkipEmpty).simplified();
346      currentSize = line.section(" ",2,2,QString::SectionSkipEmpty).simplified();
347      ok=TRUE;
348    }else{
349      maxSize.clear();
350      currentSize.clear();         
351    }
352  }else{
353    maxSize.clear();
354    currentSize.clear();
355  }
356  //Now setup the display progressbar display
357  if(ok){
358    currentSpace->setMaximum( maxSize.toInt() );
359    currentSpace->setValue( currentSize.toInt() );
360    currentSpace->setVisible(TRUE);
361    //display the actual size available in the tooltip
362    QString diskAvailable = getSizeDisplay( maxSize.toInt() - currentSize.toInt() );
363    //qDebug() << "MaxSize:" << maxSize << maxSize.toInt();
364    //qDebug() << "CurrentSize:" << currentSize << currentSize.toInt();
365    //qDebug() << "Disk Available:" << diskAvailable;
366    currentSpace->setToolTip( QString( tr("%1 of disk space available") ).arg(diskAvailable) );
367  }else{
368    currentSpace->setVisible(FALSE);
369  }
370       
371}
372
373QString MenuItem::getSizeDisplay(int kb){
374  //convert from KB to human readable output
375  if( kb > 1048576 ){ //display in GB
376    return QString::number( double(int( (kb*100)/1048576 )/100.0) )+"GB";
377  }else if( kb > 1024 ){ //display in MB
378    return QString::number( double(int( (kb*100)/1024 )/100.0) )+"MB";   
379  }else{ //display in KB
380    return QString::number( kb )+"KB";   
381  }
382       
383}
384
385QStringList MenuItem::systemCMD(QString command){ 
386   QProcess p;
387   QString outstr;
388   //Make sure we use the system environment to properly read system variables, etc.
389   QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
390   penv.insert("BLOCKSIZE","K"); //make sure we use a 1KB block size
391   p.setProcessEnvironment(penv);
392   //Merge the output channels to retrieve all output possible
393   p.setProcessChannelMode(QProcess::MergedChannels);   
394   p.start(command);
395   while(p.state()==QProcess::Starting || p.state() == QProcess::Running){
396     p.waitForFinished(200);
397     QCoreApplication::processEvents();
398   }
399   QString tmp = p.readAllStandardOutput();
400   outstr.append(tmp);
401   if(outstr.endsWith("\n")){outstr.chop(1);} //remove the newline at the end
402   QStringList out = outstr.split("\n");
403   return out;
404}
Note: See TracBrowser for help on using the repository browser.