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., 51 Franklin Street, Fifth Floor, 00022 * Boston, MA 02110-1301, USA. 00023 */ 00024 00025 #include "kdiskfreesp.h" 00026 #include <tqfile.h> 00027 #include <tqtextstream.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(TQObject *parent, const char *name) 00047 : TQObject(parent,name) 00048 { 00049 dfProc = new KProcess(); Q_CHECK_PTR(dfProc); 00050 dfProc->setEnvironment("LANGUAGE", "C"); 00051 connect( dfProc, TQT_SIGNAL(receivedStdout(KProcess *, char *, int) ), 00052 this, TQT_SLOT (receivedDFStdErrOut(KProcess *, char *, int)) ); 00053 connect(dfProc,TQT_SIGNAL(processExited(KProcess *) ), 00054 this, TQT_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 TQCString 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 TQString & 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) << TQString::fromLocal8Bit(DF_COMMAND) << TQString::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 TQTextStream t (dfStringErrOut, IO_ReadOnly); 00102 t.setEncoding(TQTextStream::Locale); 00103 TQString s=t.readLine(); 00104 if ( (s.isEmpty()) || ( s.left(10) != TQString::fromLatin1("Filesystem") ) ) 00105 kdError() << "Error running df command... got [" << s << "]" << endl; 00106 while ( !t.eof() ) { 00107 TQString u,v; 00108 s=t.readLine(); 00109 s=s.simplifyWhiteSpace(); 00110 if ( !s.isEmpty() ) { 00111 //kdDebug(kfile_area) << "GOT: [" << s << "]" << endl; 00112 00113 if (s.find(BLANK)<0) // devicename was too long, rest in next line 00114 if ( !t.eof() ) { // just appends the next line 00115 v=t.readLine(); 00116 s=s.append(v); 00117 s=s.simplifyWhiteSpace(); 00118 //kdDebug(kfile_area) << "SPECIAL GOT: [" << s << "]" << endl; 00119 }//if silly linefeed 00120 00121 //kdDebug(kfile_area) << "[" << s << "]" << endl; 00122 00123 //TQString deviceName = s.left(s.find(BLANK)); 00124 s=s.remove(0,s.find(BLANK)+1 ); 00125 //kdDebug(kfile_area) << " DeviceName: [" << deviceName << "]" << endl; 00126 00127 if (!NO_FS_TYPE) 00128 s=s.remove(0,s.find(BLANK)+1 ); // eat fs type 00129 00130 u=s.left(s.find(BLANK)); 00131 unsigned long kBSize = u.toULong(); 00132 s=s.remove(0,s.find(BLANK)+1 ); 00133 //kdDebug(kfile_area) << " Size: [" << kBSize << "]" << endl; 00134 00135 u=s.left(s.find(BLANK)); 00136 unsigned long kBUsed = u.toULong(); 00137 s=s.remove(0,s.find(BLANK)+1 ); 00138 //kdDebug(kfile_area) << " Used: [" << kBUsed << "]" << endl; 00139 00140 u=s.left(s.find(BLANK)); 00141 unsigned long kBAvail = u.toULong(); 00142 s=s.remove(0,s.find(BLANK)+1 ); 00143 //kdDebug(kfile_area) << " Avail: [" << kBAvail << "]" << endl; 00144 00145 00146 s=s.remove(0,s.find(BLANK)+1 ); // delete the capacity 94% 00147 TQString mountPoint = s.stripWhiteSpace(); 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 TQString & path ) 00165 { 00166 KDiskFreeSp * job = new KDiskFreeSp; 00167 TQString mountPoint = KIO::findPathMountPoint( path ); 00168 job->readDF( mountPoint ); 00169 return job; 00170 }