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

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since b103b6c was b103b6c, checked in by Ken Moore <ken@…>, 16 months ago

Add a fix for differentiating between USB and SCSI devices (still needs testing with a SCSI device, but it is a relatively minor change and should work)

  • Property mode set to 100644
File size: 13.7 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( slotMountClicked() ));
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::slotAutoMountToggled(bool checked){
154  qDebug() << "Auto-mount toggled for:" << device << checked;
155  QString entry = devLabel->text()+":::"+devType+":::"+filesystem;
156  if(checked){
157    //Add this entry to the auto-mount file
158    QString cmd = "echo \""+entry+"\" >> "+AMFILE;
159    system( cmd.toUtf8() );
160  }else{
161    //Remove this entry from the automount file
162    QString tmpFile = AMFILE+".tmp";
163    QString cmd = "cat "+AMFILE+" | grep -v "+entry+" > "+tmpFile+"; mv "+tmpFile+" "+AMFILE;
164    system( cmd.toUtf8() );
165  }
166}
167
168bool MenuItem::checkSavedAutoMount(){
169  if(QFile::exists(AMFILE)){
170    QString cmd = "cat "+AMFILE;
171    QString search = devLabel->text() +" "+ devType +" "+ filesystem;
172    QString chk = pcbsd::Utils::runShellCommandSearch(cmd, search);
173    if( chk.isEmpty() ){ return FALSE; }
174    else{ return TRUE; }
175  }else{
176    return FALSE;
177  }
178}
179
180void MenuItem::mountItem(){
181  //Mount the device
182 
183  //Create the full path to the mountpoint
184  QString deviceName = devLabel->text();
185  QString mntpoint = MOUNTDIR + deviceName.replace(" ","-"); //take into account spaces in the name
186
187  //Create the fileystem specific command for mounting
188  QString fstype;
189  QString fsopts="";
190  if( filesystem == "FAT" ){ fstype = "mount -t msdosfs"; fsopts = "-o large,longnames,-m=644,-M=777"; }
191  else if(filesystem == "NTFS"){ fstype = "ntfs-3g"; }
192  else if(filesystem == "EXT"){ fstype = "mount -t ext2fs"; }
193  else if(filesystem == "CD9660"){ fstype = "mount -t cd9660"; }
194  else if(filesystem == "UFS"){ fstype = "mount -t ufs"; }
195  else if(filesystem == "REISERFS"){ fstype = "mount -t reiserfs"; }
196  else if(filesystem == "XFS"){ fstype = "mount -t xfs"; }
197  else{
198    qDebug() << "Unknown device filesystem:" << device << filesystem << " attempting mount_auto command";
199    fstype = "mount_auto";
200    //QMessageBox::warning(this,tr("Unknown Device Filesystem"),tr("The filesystem on this device is unknown and cannot be mounted at this time") );
201    //return FALSE;
202  }
203  //Make sure the mntpoint is available
204  QDir mpd(mntpoint);
205  if(mpd.exists()){
206    //Remove the existing directory (will work only if it is empty)
207    mpd.cdUp();
208    mpd.rmdir(mntpoint);
209  }
210  //Prepare the commands to run
211  QString cmd1 = "mkdir " + mntpoint;
212  QString cmd2 = fstype + " " +fsopts + " " + device + " " + mntpoint;
213  QString cmd3 = "chmod 777 " + mntpoint; //to set full user/root access
214 
215  qDebug() << "Mounting device" << device << "on" << mntpoint << "("<<filesystem<<")";
216  if(DEBUG_MODE){ qDebug() << " - command:" << cmd2; }
217 
218  bool ok = FALSE;
219  QString result, title;
220  //Run the mounting commands
221  QStringList output = pcbsd::Utils::runShellCommand(cmd1);
222  if( output.join(" ").simplified().isEmpty() ){
223    //directory created, run the next commands
224    system(cmd3.toUtf8()); //set directory permissions before mounting device
225    output = pcbsd::Utils::runShellCommand(cmd2);
226    if( output.join(" ").simplified().isEmpty() ){
227      title = tr("Success");
228      result = QString( tr("%1 mounted at %2") ).arg(deviceName).arg(mntpoint);
229      ok = TRUE;
230    }else{
231      qDebug() << "pc-mounttray: Error mounting device:" << device;
232      qDebug() << " - Error message:" << output;
233      title = QString( tr("Error mounting %1 at %2") ).arg(deviceName).arg(mntpoint);
234      result =  output.join(" ");
235      //Remove the mount point just created
236      pcbsd::Utils::runShellCommand("rmdir "+mntpoint);
237    }
238  }else{
239    qDebug() << "pc-mounttray: Error creating mountpoint:" << mntpoint;
240    qDebug() << " - Error message:" << output;
241    title = QString( tr("Error mounting %1") ).arg(deviceName);
242    result =  QString( tr("Could not create mount point at %1") ).arg(mntpoint);
243  }
244  //Output the proper signals depending upon success
245  if(ok){
246    emit itemMounted(mntpoint);
247    mountpoint = mntpoint;
248  }else{
249    mountpoint.clear();
250  }
251  if( !checkAutomount->isChecked() ){
252    emit newMessage(title, result); //suppress the output message if it was automounted
253  }
254 
255}
256
257void MenuItem::unmountItem(bool force){
258  //Unmount the device
259       
260  //Check to see if the current mountpoint exists or if it is somewhere else
261  if( !QFile::exists(mountpoint) ){
262    if( !isMounted() ){  //double check that it is actually mounted (and fix the mountpoint)
263      //it is not mounted to begin with
264      return;
265    }
266  }
267  //Make sure there are no spaces in the mounpoint path
268  QString cmd1 = "umount \"" + mountpoint +"\"";
269  if(force){ cmd1.replace("umount ","umount -f "); }
270  QString cmd2 = "rmdir \"" + mountpoint +"\"";
271  qDebug() << "Unmounting device from" << mountpoint;
272  //Run the commands
273  QStringList output;
274  QString result, title;
275  bool ok = FALSE;
276  output = pcbsd::Utils::runShellCommand(cmd1);
277  if(output.join(" ").simplified().isEmpty()){
278    //unmounting successful, remove the mount point directory
279    output = pcbsd::Utils::runShellCommand(cmd2);
280    if(!output.join(" ").simplified().isEmpty()){
281      qDebug() << "pc-mounttray: Error removing mountpoint:" << mountpoint;
282      qDebug() << " - Error message:" << output;
283    }
284    ok = TRUE;
285    title = QString( tr("%1 has been successfully unmounted.") ).arg(devLabel->text());
286    if(devType == "ISO"){
287      result = tr("The ISO file has been completely detached from the system.");
288    }else{
289      result = tr("It is now safe to remove the device");
290    }
291  }else{
292    if(!force){
293      if(QMessageBox::Yes == QMessageBox::question(0,tr("Device Busy"),
294                 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."),
295                 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
296        unmountItem(true); //force the unmount recursively
297        return;
298      }
299    }
300    qDebug() << "pc-mounttray: Error unmounting mountpoint:" << mountpoint;
301    qDebug() << " - Error message:" << output;
302    title = QString( tr("Error: %1 could not be unmounted") ).arg(devLabel->text());
303    result = output.join(" ");
304  }
305  //emit the proper signals
306  if(ok){
307    mountpoint.clear();
308    if(devType=="ISO" && device.section("/",-1).startsWith("md") ){
309      //Get the md number
310      QString num = device.section("/md",-1).simplified();
311      //also remove the MD device from the system using "mdconfig"
312      qDebug() << "Detaching Memory Disk:" << num;
313      QString cmd = "mdconfig -d -u "+num;
314      system(cmd.toUtf8());
315    }
316    emit itemUnmounted(device);
317  }
318  emit newMessage(title, result);
319}
320
321void MenuItem::updateSizes(){
322  //this method only works if the device is currently mounted
323  bool ok = FALSE;
324  if(isMounted()){
325    QString cmd = "df \""+mountpoint+"\"";
326    QStringList output = systemCMD(cmd); //make sure we use the one with a 1K blocksize
327    if(output.length() > 1){
328      //parse the output (1K blocks) and save them
329      QString line = output[1].replace("\t"," ");
330      //qDebug() << "df output:" << output << cmd;
331      maxSize = line.section(" ",1,1,QString::SectionSkipEmpty).simplified();
332      currentSize = line.section(" ",2,2,QString::SectionSkipEmpty).simplified();
333      ok=TRUE;
334    }else{
335      maxSize.clear();
336      currentSize.clear();         
337    }
338  }else{
339    maxSize.clear();
340    currentSize.clear();
341  }
342  //Now setup the display progressbar display
343  if(ok){
344    currentSpace->setMaximum( maxSize.toInt() );
345    currentSpace->setValue( currentSize.toInt() );
346    currentSpace->setVisible(TRUE);
347    //display the actual size available in the tooltip
348    QString diskAvailable = getSizeDisplay( maxSize.toInt() - currentSize.toInt() );
349    //qDebug() << "MaxSize:" << maxSize << maxSize.toInt();
350    //qDebug() << "CurrentSize:" << currentSize << currentSize.toInt();
351    //qDebug() << "Disk Available:" << diskAvailable;
352    currentSpace->setToolTip( QString( tr("%1 of disk space available") ).arg(diskAvailable) );
353  }else{
354    currentSpace->setVisible(FALSE);
355  }
356       
357}
358
359QString MenuItem::getSizeDisplay(int kb){
360  //convert from KB to human readable output
361  if( kb > 1048576 ){ //display in GB
362    return QString::number( double(int( (kb*100)/1048576 )/100.0) )+"GB";
363  }else if( kb > 1024 ){ //display in MB
364    return QString::number( double(int( (kb*100)/1024 )/100.0) )+"MB";   
365  }else{ //display in KB
366    return QString::number( kb )+"KB";   
367  }
368       
369}
370
371QStringList MenuItem::systemCMD(QString command){ 
372   QProcess p;
373   QString outstr;
374   //Make sure we use the system environment to properly read system variables, etc.
375   QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
376   penv.insert("BLOCKSIZE","K"); //make sure we use a 1KB block size
377   p.setProcessEnvironment(penv);
378   //Merge the output channels to retrieve all output possible
379   p.setProcessChannelMode(QProcess::MergedChannels);   
380   p.start(command);
381   while(p.state()==QProcess::Starting || p.state() == QProcess::Running){
382     p.waitForFinished(200);
383     QCoreApplication::processEvents();
384   }
385   QString tmp = p.readAllStandardOutput();
386   outstr.append(tmp);
387   if(outstr.endsWith("\n")){outstr.chop(1);} //remove the newline at the end
388   QStringList out = outstr.split("\n");
389   return out;
390}
Note: See TracBrowser for help on using the repository browser.