• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kfile
kdiskfreespaceinfo.cpp
Go to the documentation of this file.
1 /*
2  * kdiskfreespaceinfo.h
3  *
4  * Copyright 2008 Sebastian Trug <trueg@kde.org>
5  *
6  * Based on kdiskfreespace.h
7  * Copyright 2007 David Faure <faure@kde.org>
8  * Copyright 2008 Dirk Mueller <mueller@kde.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License version 2 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB. If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 #include "kdiskfreespaceinfo.h"
26 
27 #include <QtCore/QSharedData>
28 #include <QtCore/QFile>
29 
30 #include <kmountpoint.h>
31 
32 #ifdef Q_OS_WIN
33 #include <QtCore/QDir>
34 #include <windows.h>
35 #else
36 #include <sys/statvfs.h>
37 #endif
38 
39 
40 class KDiskFreeSpaceInfo::Private : public QSharedData
41 {
42 public:
43  Private()
44  : valid(false),
45  size(0),
46  available(0) {
47  }
48 
49  bool valid;
50  QString mountPoint;
51  KIO::filesize_t size;
52  KIO::filesize_t available;
53 };
54 
55 
56 KDiskFreeSpaceInfo::KDiskFreeSpaceInfo()
57  : d(new Private())
58 {
59 }
60 
61 
62 KDiskFreeSpaceInfo::KDiskFreeSpaceInfo( const KDiskFreeSpaceInfo& other )
63 {
64  d = other.d;
65 }
66 
67 
68 KDiskFreeSpaceInfo::~KDiskFreeSpaceInfo()
69 {
70 }
71 
72 
73 KDiskFreeSpaceInfo& KDiskFreeSpaceInfo::operator=( const KDiskFreeSpaceInfo& other )
74 {
75  d = other.d;
76  return *this;
77 }
78 
79 
80 bool KDiskFreeSpaceInfo::isValid() const
81 {
82  return d->valid;
83 }
84 
85 
86 QString KDiskFreeSpaceInfo::mountPoint() const
87 {
88  return d->mountPoint;
89 }
90 
91 
92 KIO::filesize_t KDiskFreeSpaceInfo::size() const
93 {
94  return d->size;
95 }
96 
97 
98 KIO::filesize_t KDiskFreeSpaceInfo::available() const
99 {
100  return d->available;
101 }
102 
103 
104 KIO::filesize_t KDiskFreeSpaceInfo::used() const
105 {
106  return d->size - d->available;
107 }
108 
109 
110 KDiskFreeSpaceInfo KDiskFreeSpaceInfo::freeSpaceInfo( const QString& path )
111 {
112  KDiskFreeSpaceInfo info;
113 
114  // determine the mount point
115  KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( path );
116  if (mp)
117  info.d->mountPoint = mp->mountPoint();
118 
119 #ifdef Q_OS_WIN
120  quint64 availUser;
121  QFileInfo fi(info.d->mountPoint);
122  QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath());
123 
124  if(GetDiskFreeSpaceExW((LPCWSTR)dir.utf16(),
125  (PULARGE_INTEGER)&availUser,
126  (PULARGE_INTEGER)&info.d->size,
127  (PULARGE_INTEGER)&info.d->available) != 0) {
128  info.d->valid = true;
129  }
130 #else
131  struct statvfs statvfs_buf;
132 
133  // Prefer mountPoint if available, so that it even works with non-existing files.
134  const QString pathArg = info.d->mountPoint.isEmpty() ? path : info.d->mountPoint;
135  if (!statvfs(QFile::encodeName(pathArg).constData(), &statvfs_buf)) {
136  const quint64 blksize = quint64(statvfs_buf.f_frsize); // cast to avoid overflow
137  info.d->available = statvfs_buf.f_bavail * blksize;
138  info.d->size = statvfs_buf.f_blocks * blksize;
139  info.d->valid = true;
140  }
141 #endif
142 
143  return info;
144 }
KSharedPtr
Definition: kprotocolmanager.h:31
QDir::toNativeSeparators
QString toNativeSeparators(const QString &pathName)
KIO::filesize_t
qulonglong filesize_t
64-bit file size
Definition: global.h:57
KSharedPtr::d
T * d
kmountpoint.h
KMountPoint::currentMountPoints
static List currentMountPoints(DetailsNeededFlags infoNeeded=BasicInfoNeeded)
QString::size
int size() const
QSharedData
kdiskfreespaceinfo.h
QString::isEmpty
bool isEmpty() const
QString
QFileInfo
QString::utf16
const ushort * utf16() const
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
KMountPoint::List::findByPath
Ptr findByPath(const QString &path) const
quint64
QFile::encodeName
QByteArray encodeName(const QString &fileName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal