• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kdf

disks.cpp

Go to the documentation of this file.
00001 /*
00002  * disks.cpp
00003  *
00004  * Copyright (c) 1998 Michael Kropfberger <michael.kropfberger@gmx.net>
00005  *
00006  * Requires the Qt widget libraries, available at no cost at
00007  * http://www.troll.no/
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  */
00023 
00024 #include "disks.h"
00025 
00026 #include <QtCore/QFileInfo>
00027 #include <QtCore/QDir>
00028 
00029 #include <kglobal.h>
00030 #include <kdebug.h>
00031 #include <k3process.h>
00032 
00033 #include "disks.moc"
00034 
00035 /****************************************************/
00036 /********************* DiskEntry ********************/
00037 /****************************************************/
00038 
00042 void DiskEntry::init(const char *name)
00043 {
00044   setObjectName(name);
00045   device="";
00046   type="";
00047   mountedOn="";
00048   options="";
00049   size=0;
00050   used=0;
00051   avail=0;
00052   isMounted=false;
00053   mntcmd="";
00054   umntcmd="";
00055   iconSetByUser=false;
00056   icoName="";
00057 
00058 
00059  // BackgroundProcesses ****************************************
00060 
00061  sysProc = new K3ShellProcess(); Q_CHECK_PTR(sysProc);
00062  connect( sysProc, SIGNAL(receivedStdout(K3Process *, char *, int) ),
00063         this, SLOT (receivedSysStdErrOut(K3Process *, char *, int)) );
00064  connect( sysProc, SIGNAL(receivedStderr(K3Process *, char *, int) ),
00065         this, SLOT (receivedSysStdErrOut(K3Process *, char *, int)) );
00066  readingSysStdErrOut=false;
00067 
00068 
00069 }
00070 
00071 DiskEntry::DiskEntry(QObject *parent, const char *name)
00072  : QObject (parent)
00073 {
00074   init(name);
00075 }
00076 
00077 DiskEntry::DiskEntry(const QString & deviceName, QObject *parent, const char *name)
00078  : QObject (parent)
00079 {
00080   init(name);
00081 
00082   setDeviceName(deviceName);
00083 }
00084 DiskEntry::~DiskEntry()
00085 {
00086   disconnect(this);
00087   delete sysProc;
00088 }
00089 
00090 int DiskEntry::toggleMount()
00091 {
00092   if (!mounted())
00093       return mount();
00094   else
00095       return umount();
00096 }
00097 
00098 int DiskEntry::mount()
00099 {
00100   QString cmdS=mntcmd;
00101   if (cmdS.isEmpty()) // generate default mount cmd
00102     if (getuid()!=0 ) // user mountable
00103       {
00104       cmdS="mount %d";
00105       }
00106     else  // root mounts with all params/options
00107       {
00108       // FreeBSD's mount(8) is picky: -o _must_ go before
00109       // the device and mountpoint.
00110       cmdS=QString::fromLatin1("mount -t%t -o%o %d %m");
00111       }
00112 
00113   cmdS.replace(QString::fromLatin1("%d"),deviceName());
00114   cmdS.replace(QString::fromLatin1("%m"),mountPoint());
00115   cmdS.replace(QString::fromLatin1("%t"),fsType());
00116   cmdS.replace(QString::fromLatin1("%o"),mountOptions());
00117 
00118   kDebug() << "mount-cmd: [" << cmdS << "]" ;
00119   int e=sysCall(cmdS);
00120   if (!e) setMounted(true);
00121   kDebug() << "mount-cmd: e=" << e ;
00122   return e;
00123 }
00124 
00125 int DiskEntry::umount()
00126 {
00127   kDebug() << "umounting" ;
00128   QString cmdS=umntcmd;
00129   if (cmdS.isEmpty()) // generate default umount cmd
00130       cmdS="umount %d";
00131 
00132   cmdS.replace(QString::fromLatin1("%d"),deviceName());
00133   cmdS.replace(QString::fromLatin1("%m"),mountPoint());
00134 
00135   kDebug() << "umount-cmd: [" << cmdS << "]" ;
00136   int e=sysCall(cmdS);
00137   if (!e) setMounted(false);
00138   kDebug() << "umount-cmd: e=" << e ;
00139 
00140   return e;
00141 }
00142 
00143 int DiskEntry::remount()
00144 {
00145   if (mntcmd.isEmpty() && umntcmd.isEmpty() // default mount/umount commands
00146       && (getuid()==0)) // you are root
00147     {
00148     QString oldOpt=options;
00149     if (options.isEmpty())
00150        options="remount";
00151     else
00152        options+=",remount";
00153     int e=mount();
00154     options=oldOpt;
00155     return e;
00156    } else {
00157     if (int e=umount())
00158       return mount();
00159    else return e;
00160   }
00161 }
00162 
00163 void DiskEntry::setMountCommand(const QString & mnt)
00164 {
00165   mntcmd=mnt;
00166 }
00167 
00168 void DiskEntry::setUmountCommand(const QString & umnt)
00169 {
00170   umntcmd=umnt;
00171 }
00172 
00173 void DiskEntry::setIconName(const QString & iconName)
00174 {
00175   iconSetByUser=true;
00176   icoName=iconName;
00177   if (icoName.right(6) == "_mount")
00178      icoName.truncate(icoName.length()-6);
00179   else if (icoName.right(8) == "_unmount")
00180      icoName.truncate(icoName.length()-8);
00181 
00182   emit iconNameChanged();
00183 }
00184 
00185 QString DiskEntry::iconName()
00186 {
00187   QString iconName=icoName;
00188   if (iconSetByUser) {
00189     mounted() ? iconName+="_mount" : iconName+="_unmount";
00190    return iconName;
00191   } else
00192    return guessIconName();
00193 }
00194 
00195 //TODO: Need porting to solid
00196 QString DiskEntry::guessIconName()
00197 {
00198   QString iconName;
00199     // try to be intelligent
00200     if (mountPoint().contains("cdrom",Qt::CaseInsensitive)) iconName+="media-optical";
00201     else if (deviceName().contains("cdrom",Qt::CaseInsensitive)) iconName+="media-optical";
00202     else if (mountPoint().contains("writer",Qt::CaseInsensitive)) iconName+="media-optical-recordable";
00203     else if (deviceName().contains("writer",Qt::CaseInsensitive)) iconName+="media-optical-recordable";
00204     else if (mountPoint().contains("mo",Qt::CaseInsensitive)) iconName+="mo"; //TODO
00205     else if (deviceName().contains("mo",Qt::CaseInsensitive)) iconName+="mo"; //TODO
00206     else if (deviceName().contains("fd",Qt::CaseInsensitive)) {
00207             if (deviceName().contains("360",Qt::CaseInsensitive)) iconName+="5floppy"; //TODO
00208             if (deviceName().contains("1200",Qt::CaseInsensitive)) iconName+="5floppy"; //TODO
00209             else iconName+="media-floppy";
00210      }
00211     else if (mountPoint().contains("floppy",Qt::CaseInsensitive)) iconName+="media-floppy";
00212     else if (mountPoint().contains("zip",Qt::CaseInsensitive)) iconName+="zip"; //TODO
00213     else if (fsType().contains("nfs",Qt::CaseInsensitive)) iconName+="nfs"; //TODO
00214     else iconName+="drive-harddisk";
00216 //    if ( !mountOptions().contains("user",Qt::CaseInsensitive) )
00217 //      iconName.prepend("root_"); // special root icon, normal user can't mount
00218 
00219     //debug("device %s is %s",deviceName().latin1(),iconName.latin1());
00220 
00221     //emit iconNameChanged();
00222   return iconName;
00223 }
00224 
00225 
00226 /***************************************************************************
00227   * starts a command on the underlying system via /bin/sh
00228 **/
00229 int DiskEntry::sysCall(const QString & command)
00230 {
00231   if (readingSysStdErrOut || sysProc->isRunning() )  return -1;
00232 
00233   sysStringErrOut=i18n("Called: %1\n\n", command); // put the called command on ErrOut
00234   sysProc->clearArguments();
00235   (*sysProc) << command;
00236     if (!sysProc->start( K3Process::Block, K3Process::AllOutput ))
00237      kFatal() << i18n("could not execute %1", command.toLocal8Bit().data()) ;
00238 
00239   if (sysProc->exitStatus()!=0) emit sysCallError(this, sysProc->exitStatus());
00240 
00241   return (sysProc->exitStatus());
00242 }
00243 
00244 
00245 /***************************************************************************
00246   * is called, when the Sys-command writes on StdOut or StdErr
00247 **/
00248 void DiskEntry::receivedSysStdErrOut(K3Process *, char *data, int len)
00249 {
00250   QString tmp = QString::fromLocal8Bit(data, len);
00251   sysStringErrOut.append(tmp);
00252 }
00253 
00254 float DiskEntry::percentFull() const
00255 {
00256    if (size != 0) {
00257       return 100 - ( ((float)avail / (float)size) * 100 );
00258    } else {
00259       return -1;
00260    }
00261 }
00262 
00263 void DiskEntry::setDeviceName(const QString & deviceName)
00264 {
00265  device=deviceName;
00266  emit deviceNameChanged();
00267 }
00268 
00269 QString DiskEntry::deviceRealName() const
00270 {
00271  QFileInfo inf( device );
00272  QDir dir( inf.absolutePath() );
00273  QString relPath = inf.fileName();
00274  if ( inf.isSymLink() ) {
00275   QString link = inf.readLink();
00276   if ( link.startsWith( '/' ) )
00277     return link;
00278   relPath = link;
00279  }
00280  return dir.canonicalPath() + '/' + relPath;
00281 }
00282 
00283 void DiskEntry::setMountPoint(const QString & mountPoint)
00284 {
00285   mountedOn=mountPoint;
00286  emit mountPointChanged();
00287 }
00288 
00289 QString DiskEntry::realMountPoint() const
00290 {
00291  QDir dir( mountedOn );
00292  return dir.canonicalPath();
00293 }
00294 
00295 void DiskEntry::setMountOptions(const QString & mountOptions)
00296 {
00297  options=mountOptions;
00298  emit mountOptionsChanged();
00299 }
00300 
00301 void DiskEntry::setFsType(const QString & fsType)
00302 {
00303   type=fsType;
00304   emit fsTypeChanged();
00305 }
00306 
00307 void DiskEntry::setMounted(bool nowMounted)
00308 {
00309   isMounted=nowMounted;
00310   emit mountedChanged();
00311 }
00312 
00313 void DiskEntry::setKBSize(int kb_size)
00314 {
00315   size=kb_size;
00316   emit kBSizeChanged();
00317 }
00318 
00319 void DiskEntry::setKBUsed(int kb_used)
00320 {
00321   used=kb_used;
00322   if ( size < (used+avail) ) {  //adjust kBAvail
00323      kWarning() << "device " << device << ": kBAvail(" << avail << ")+*kBUsed(" << used << ") exceeds kBSize(" << size << ")" ;
00324      setKBAvail(size-used);
00325   }
00326   emit kBUsedChanged();
00327 }
00328 
00329 void DiskEntry::setKBAvail(int kb_avail)
00330 {
00331   avail=kb_avail;
00332   if ( size < (used+avail) ) {  //adjust kBUsed
00333      kWarning() << "device " << device << ": *kBAvail(" << avail << ")+kBUsed(" << used << ") exceeds kBSize(" << size << ")" ;
00334      setKBUsed(size-avail);
00335   }
00336   emit kBAvailChanged();
00337 }
00338 
00339 

kdf

Skip menu "kdf"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal