KIO
kdiskfreespace.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kdiskfreespace.h"
00023 #include <QtCore/QFile>
00024 #include <QtCore/QTextIStream>
00025 #include <QtCore/QTimer>
00026
00027 #include <kdebug.h>
00028 #include <kprocess.h>
00029 #include <kmountpoint.h>
00030 #include <kio/global.h>
00031 #include <config-kfile.h>
00032
00033 class KDiskFreeSpace::Private
00034 {
00035 public:
00036 Private(KDiskFreeSpace *parent)
00037 : m_parent(parent)
00038 {}
00039
00040 bool _k_calculateFreeSpace();
00041
00042 KDiskFreeSpace *m_parent;
00043 QString m_path;
00044 };
00045
00046 KDiskFreeSpace::KDiskFreeSpace(QObject *parent)
00047 : QObject(parent), d(new Private(this))
00048 {
00049 }
00050
00051
00052 KDiskFreeSpace::~KDiskFreeSpace()
00053 {
00054 delete d;
00055 }
00056
00057 bool KDiskFreeSpace::readDF( const QString & mountPoint )
00058 {
00059 d->m_path = mountPoint;
00060 return d->_k_calculateFreeSpace();
00061 }
00062
00063 #ifdef Q_OS_WIN
00064 #include <QtCore/QDir>
00065 #include <windows.h>
00066 #else
00067 #include <sys/statvfs.h>
00068 #endif
00069
00070 bool KDiskFreeSpace::Private::_k_calculateFreeSpace()
00071 {
00072
00073 QString mountPoint;
00074
00075 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( m_path );
00076 if (mp)
00077 mountPoint = mp->mountPoint();
00078
00079 quint64 availUser, total, avail;
00080 bool bRet = false;
00081 #ifdef Q_OS_WIN
00082 QFileInfo fi(mountPoint);
00083 QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath());
00084
00085 if(GetDiskFreeSpaceExW((LPCWSTR)dir.utf16(),
00086 (PULARGE_INTEGER)&availUser,
00087 (PULARGE_INTEGER)&total,
00088 (PULARGE_INTEGER)&avail) != 0) {
00089 availUser = availUser / 1024;
00090 total = total / 1024;
00091 avail = avail / 1024;
00092 emit m_parent->foundMountPoint( mountPoint, total, total-avail, avail );
00093 bRet = true;
00094 }
00095 #else
00096 struct statvfs statvfs_buf;
00097
00098 if (!statvfs(QFile::encodeName(m_path).constData(), &statvfs_buf)) {
00099 avail = statvfs_buf.f_bavail * statvfs_buf.f_frsize / 1024;
00100 total = statvfs_buf.f_blocks * statvfs_buf.f_frsize / 1024;
00101 emit m_parent->foundMountPoint( mountPoint, total, total-avail, avail );
00102 bRet = true;
00103 }
00104 #endif
00105
00106 emit m_parent->done();
00107 m_parent->deleteLater();
00108
00109 return bRet;
00110 }
00111
00112 KDiskFreeSpace * KDiskFreeSpace::findUsageInfo( const QString & path )
00113 {
00114 KDiskFreeSpace * job = new KDiskFreeSpace;
00115 job->d->m_path = path;
00116 QTimer::singleShot(0, job, SLOT(_k_calculateFreeSpace()));
00117 return job;
00118 }
00119
00120 #include "kdiskfreespace.moc"
00121