KIO
kdiskfreespaceinfo.cpp
Go to the documentation of this file.
00001 /* 00002 * kdiskfreespaceinfo.h 00003 * 00004 * Copyright 2008 Sebastian Trug <trueg@kde.org> 00005 * 00006 * Based on kdiskfreespace.h 00007 * Copyright 2007 David Faure <faure@kde.org> 00008 * Copyright 2008 Dirk Mueller <mueller@kde.org> 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 "kdiskfreespaceinfo.h" 00026 00027 #include <QtCore/QSharedData> 00028 #include <QtCore/QFile> 00029 00030 #include <kmountpoint.h> 00031 00032 #ifdef Q_OS_WIN 00033 #include <QtCore/QDir> 00034 #include <windows.h> 00035 #else 00036 #include <sys/statvfs.h> 00037 #endif 00038 00039 00040 class KDiskFreeSpaceInfo::Private : public QSharedData 00041 { 00042 public: 00043 Private() 00044 : valid(false), 00045 size(0), 00046 available(0) { 00047 } 00048 00049 bool valid; 00050 QString mountPoint; 00051 KIO::filesize_t size; 00052 KIO::filesize_t available; 00053 }; 00054 00055 00056 KDiskFreeSpaceInfo::KDiskFreeSpaceInfo() 00057 : d(new Private()) 00058 { 00059 } 00060 00061 00062 KDiskFreeSpaceInfo::KDiskFreeSpaceInfo( const KDiskFreeSpaceInfo& other ) 00063 { 00064 d = other.d; 00065 } 00066 00067 00068 KDiskFreeSpaceInfo::~KDiskFreeSpaceInfo() 00069 { 00070 } 00071 00072 00073 KDiskFreeSpaceInfo& KDiskFreeSpaceInfo::operator=( const KDiskFreeSpaceInfo& other ) 00074 { 00075 d = other.d; 00076 return *this; 00077 } 00078 00079 00080 bool KDiskFreeSpaceInfo::isValid() const 00081 { 00082 return d->valid; 00083 } 00084 00085 00086 QString KDiskFreeSpaceInfo::mountPoint() const 00087 { 00088 return d->mountPoint; 00089 } 00090 00091 00092 KIO::filesize_t KDiskFreeSpaceInfo::size() const 00093 { 00094 return d->size; 00095 } 00096 00097 00098 KIO::filesize_t KDiskFreeSpaceInfo::available() const 00099 { 00100 return d->available; 00101 } 00102 00103 00104 KIO::filesize_t KDiskFreeSpaceInfo::used() const 00105 { 00106 return d->size - d->available; 00107 } 00108 00109 00110 KDiskFreeSpaceInfo KDiskFreeSpaceInfo::freeSpaceInfo( const QString& path ) 00111 { 00112 KDiskFreeSpaceInfo info; 00113 00114 // determine the mount point 00115 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( path ); 00116 if (mp) 00117 info.d->mountPoint = mp->mountPoint(); 00118 00119 #ifdef Q_OS_WIN 00120 quint64 availUser; 00121 QFileInfo fi(info.d->mountPoint); 00122 QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath()); 00123 00124 if(GetDiskFreeSpaceExW((LPCWSTR)dir.utf16(), 00125 (PULARGE_INTEGER)&availUser, 00126 (PULARGE_INTEGER)&info.d->size, 00127 (PULARGE_INTEGER)&info.d->available) != 0) { 00128 info.d->valid = true; 00129 } 00130 #else 00131 struct statvfs statvfs_buf; 00132 00133 // Prefer mountPoint if available, so that it even works with non-existing files. 00134 const QString pathArg = info.d->mountPoint.isEmpty() ? path : info.d->mountPoint; 00135 if (!statvfs(QFile::encodeName(pathArg).constData(), &statvfs_buf)) { 00136 const quint64 blksize = quint64(statvfs_buf.f_frsize); // cast to avoid overflow 00137 info.d->available = statvfs_buf.f_bavail * blksize; 00138 info.d->size = statvfs_buf.f_blocks * blksize; 00139 info.d->valid = true; 00140 } 00141 #endif 00142 00143 return info; 00144 }
KDE 4.6 API Reference