00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00062
00063 KDiskFreeSp::~KDiskFreeSp()
00064 {
00065
delete dfProc;
00066 }
00067
00068
00069
00070
00071
void KDiskFreeSp::receivedDFStdErrOut(
KProcess *,
char *data,
int len)
00072 {
00073
QCString tmp(data,len+1);
00074 dfStringErrOut.
append(tmp);
00075 }
00076
00077
00078
00079
00080 int KDiskFreeSp::readDF(
const QString & mountPoint )
00081 {
00082
if (readingDFStdErrOut || dfProc->
isRunning())
00083
return -1;
00084 m_mountPoint = mountPoint;
00085 dfStringErrOut=
"";
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
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
00111
00112
if (s.
find(BLANK)<0)
00113
if ( !t.eof() ) {
00114 v=t.readLine();
00115 s=s.
append(v);
00116 s=s.
simplifyWhiteSpace();
00117
00118 }
00119
00120
00121
00122
00123 s=s.
remove(0,s.
find(BLANK)+1 );
00124
00125
00126
if (!NO_FS_TYPE)
00127 s=s.
remove(0,s.
find(BLANK)+1 );
00128
00129 u=s.
left(s.
find(BLANK));
00130
unsigned long kBSize = u.
toULong();
00131 s=s.
remove(0,s.
find(BLANK)+1 );
00132
00133
00134 u=s.
left(s.
find(BLANK));
00135
unsigned long kBUsed = u.
toULong();
00136 s=s.
remove(0,s.
find(BLANK)+1 );
00137
00138
00139 u=s.
left(s.
find(BLANK));
00140
unsigned long kBAvail = u.
toULong();
00141 s=s.
remove(0,s.
find(BLANK)+1 );
00142
00143
00144
00145 s=s.
remove(0,s.
find(BLANK)+1 );
00146
QString mountPoint = s.
stripWhiteSpace();
00147
00148
00149
if ( mountPoint == m_mountPoint )
00150 {
00151
00152 emit foundMountPoint( mountPoint, kBSize, kBUsed, kBAvail );
00153 emit foundMountPoint( kBSize, kBUsed, kBAvail, mountPoint );
00154 }
00155 }
00156 }
00157
00158 readingDFStdErrOut=
false;
00159 emit done();
00160
delete this;
00161 }
00162
00163 KDiskFreeSp *
KDiskFreeSp::findUsageInfo(
const QString & path )
00164 {
00165
KDiskFreeSp * job =
new KDiskFreeSp;
00166
QString mountPoint =
KIO::findPathMountPoint( path );
00167 job->
readDF( mountPoint );
00168
return job;
00169 }