kio Library API Documentation

kdiskfreesp.cpp

00001 /*
00002  * kdiskfreesp.cpp
00003  *
00004  * Copyright (c) 1999 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  *
00010  *  This library is free software; you can redistribute it and/or
00011  *  modify it under the terms of the GNU Library General Public
00012  *  License version 2 as published by the Free Software Foundation.
00013  *
00014  *  This library 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 GNU
00017  *  Library General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU Library General Public License
00020  *  along with this library; see the file COPYING.LIB.  If not, write to
00021  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  *  Boston, MA 02111-1307, USA.
00023  */
00024 
00025 #include "kdiskfreesp.h"
00026 #include <qfile.h>
00027 #include <qtextstream.h>
00028 
00029 #include <kdebug.h>
00030 #include <kprocess.h>
00031 #include <kio/global.h>
00032 #include <config-kfile.h>
00033 
00034 #include "kdiskfreesp.moc"
00035 
00036 #define DF_COMMAND    "df"
00037 #define DF_ARGS       "-k"
00038 #define NO_FS_TYPE    true
00039 
00040 #define BLANK ' '
00041 #define FULL_PERCENT 95.0
00042 
00043 /***************************************************************************
00044   * constructor
00045 **/
00046 KDiskFreeSp::KDiskFreeSp(QObject *parent, const char *name)
00047     : QObject(parent,name)
00048 {
00049     dfProc = new KProcess(); Q_CHECK_PTR(dfProc);
00050     dfProc->setEnvironment("LANGUAGE", "C");
00051     connect( dfProc, SIGNAL(receivedStdout(KProcess *, char *, int) ),
00052              this, SLOT (receivedDFStdErrOut(KProcess *, char *, int)) );
00053     connect(dfProc,SIGNAL(processExited(KProcess *) ),
00054             this, SLOT(dfDone() ) );
00055 
00056     readingDFStdErrOut=FALSE;
00057 }
00058 
00059 
00060 /***************************************************************************
00061   * destructor
00062 **/
00063 KDiskFreeSp::~KDiskFreeSp()
00064 {
00065     delete dfProc;
00066 };
00067 
00068 /***************************************************************************
00069   * is called, when the df-command writes on StdOut
00070 **/
00071 void KDiskFreeSp::receivedDFStdErrOut(KProcess *, char *data, int len)
00072 {
00073   QCString tmp(data,len+1);  // adds a zero-byte
00074   dfStringErrOut.append(tmp);
00075 }
00076 
00077 /***************************************************************************
00078   * reads the df-commands results
00079 **/
00080 int KDiskFreeSp::readDF( const QString & mountPoint )
00081 {
00082   if (readingDFStdErrOut || dfProc->isRunning())
00083     return -1;
00084   m_mountPoint = mountPoint;
00085   dfStringErrOut=""; // yet no data received
00086   dfProc->clearArguments();
00087   (*dfProc) << QString::fromLocal8Bit(DF_COMMAND) << QString::fromLocal8Bit(DF_ARGS);
00088   if (!dfProc->start( KProcess::NotifyOnExit, KProcess::AllOutput ))
00089      kdError() << "could not execute ["<< DF_COMMAND << "]" << endl;;
00090   return 1;
00091 }
00092 
00093 
00094 /***************************************************************************
00095   * is called, when the df-command has finished
00096 **/
00097 void KDiskFreeSp::dfDone()
00098 {
00099   readingDFStdErrOut=TRUE;
00100 
00101   QTextStream t (dfStringErrOut, IO_ReadOnly);
00102   QString s=t.readLine();
00103   if ( (s.isEmpty()) || ( s.left(10) != QString::fromLatin1("Filesystem") ) )
00104     kdError() << "Error running df command... got [" << s << "]" << endl;
00105   while ( !t.eof() ) {
00106     QString u,v;
00107     s=t.readLine();
00108     s=s.simplifyWhiteSpace();
00109     if ( !s.isEmpty() ) {
00110       //kdDebug(kfile_area) << "GOT: [" << s << "]" << endl;
00111 
00112       if (s.find(BLANK)<0)      // devicename was too long, rest in next line
00113     if ( !t.eof() ) {       // just appends the next line
00114             v=t.readLine();
00115             s=s.append(v);
00116             s=s.simplifyWhiteSpace();
00117             kdDebug(kfile_area) << "SPECIAL GOT: [" << s << "]" << endl;
00118      }//if silly linefeed
00119 
00120       kdDebug(kfile_area) << "[" << s << "]" << endl;
00121 
00122       //QString deviceName = s.left(s.find(BLANK));
00123       s=s.remove(0,s.find(BLANK)+1 );
00124       //kdDebug(kfile_area) << "    DeviceName:    [" << deviceName << "]" << endl;
00125 
00126       if (!NO_FS_TYPE)
00127           s=s.remove(0,s.find(BLANK)+1 ); // eat fs type
00128 
00129       u=s.left(s.find(BLANK));
00130       unsigned long kBSize = u.toULong();
00131       s=s.remove(0,s.find(BLANK)+1 );
00132       kdDebug(kfile_area) << "    Size:       [" << kBSize << "]" << endl;
00133 
00134       u=s.left(s.find(BLANK));
00135       unsigned long kBUsed = u.toULong();
00136       s=s.remove(0,s.find(BLANK)+1 );
00137       kdDebug(kfile_area) << "    Used:       [" << kBUsed << "]" << endl;
00138 
00139       u=s.left(s.find(BLANK));
00140       unsigned long kBAvail = u.toULong();
00141       s=s.remove(0,s.find(BLANK)+1 );
00142       kdDebug(kfile_area) << "    Avail:       [" << kBAvail << "]" << endl;
00143 
00144 
00145       s=s.remove(0,s.find(BLANK)+1 );  // delete the capacity 94%
00146       QString mountPoint = s.left(s.find(BLANK));
00147       s=s.remove(0,s.find(BLANK)+1 );
00148       kdDebug(kfile_area) << "    MountPoint:       [" << mountPoint << "]" << endl;
00149 
00150       if ( mountPoint == m_mountPoint )
00151       {
00152         kdDebug(kfile_area) << "Found mount point. Emitting" << endl;
00153         emit foundMountPoint( mountPoint, kBSize, kBUsed, kBAvail );
00154         emit foundMountPoint( kBSize, kBUsed, kBAvail, mountPoint ); // sic!
00155       }
00156     }//if not header
00157   }//while further lines available
00158 
00159   readingDFStdErrOut=FALSE;
00160   emit done();
00161   delete this;
00162 }
00163 
00164 KDiskFreeSp * KDiskFreeSp::findUsageInfo( const QString & path )
00165 {
00166     KDiskFreeSp * job = new KDiskFreeSp;
00167     QString mountPoint = KIO::findPathMountPoint( path );
00168     job->readDF( mountPoint );
00169     return job;
00170 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:29 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001