[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: [PATCH] Make KDiskFreeSpace more convenient -> KJob-like usage
From:       Sebastian =?iso-8859-1?q?Tr=FCg?= <strueg () mandriva ! com>
Date:       2008-07-25 7:32:31
Message-ID: 200807250932.31547.strueg () mandriva ! com
[Download RAW message or body]

On Thursday 24 July 2008 23:44:14 Alex Merry wrote:
> On Thursday 24 July 2008 22:03:06 Sebastian Tr=FCg wrote:
> > The attached patch reverts my previous patch and marks the whole
> > KDiskFreeSpace class deprecated (although that would not link here so I
> > marked the methods).
> > A new class takes its place that works 100% sync and has a rather nice
> > API.
> >
> > May I commit?

> Other than that (sorry, I'm a bit obsessive about apidocs), I think it
> looks great.

no need to be sorry. We want this to be right this time. :)

Updated patch attached.

Cheers,
Sebastian

["kdiskfreespace.diff" (text/x-diff)]

Index: kio/kfile/kdiskfreespace.h
===================================================================
--- kio/kfile/kdiskfreespace.h	(revision 837497)
+++ kio/kfile/kdiskfreespace.h	(working copy)
@@ -3,7 +3,6 @@
  *
  * Copyright 2007 David Faure <faure@kde.org>
  * Copyright 2008 Dirk Mueller <mueller@kde.org>
- * Copyright 2008 Sebastian Trug <trueg@kde.org>
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -28,36 +27,11 @@
 #include <QtCore/QString>
 
 #include <kio/kio_export.h>
-#include <kio/global.h>
 
-
 /**
- * \class KDiskFreeSpacePrivate kdiskfreespace.h KDiskFreeSpace
- *
- * \brief Determine the space left on an arbitrary partition.
- *
- * This class wraps around the system calls to detemermine the
- * free space left in a specific partition. It supports arbitrary
- * paths and the mount point of the partition in question.
- *
- * The class is intended to be used as a fire-and-forget object
- * very much like KJob. It is recommended to use the static
- * findUsageInfo method instead of creating ones own instance:
- *
- * \code
- * connect( KDiskFreeSpace::findUsageInfo( myPath ), \
                SIGNAL(finished(KDiskFreeSpace*)),
- *          this, SLOT(mySlot(KDiskFreeSpace*)) );
- *
- * void mySlot( KDiskFreeSpace* df ) {
- *     if (!df->error())
- *         doSomethingWithTheDFInfo( df->mountPoint(),
- *                                   df->size(),
- *                                   df->used(),
- *                                   df->avail() );
- * }
- * \code
+ * \deprecated Use KDiskFreeSpaceInfo
  */
-class KIO_EXPORT KDiskFreeSpace : public QObject // KDE 5 -> make it a KIO::Job
+class KIO_EXPORT KDiskFreeSpace : public QObject
 {
     Q_OBJECT
 
@@ -65,10 +39,8 @@
 
     /**
      * Constructor
-     *
-     * It is recommended to use findUsageInfo instead.
      */
-    explicit KDiskFreeSpace( QObject *parent = 0 );
+    KDE_DEPRECATED explicit KDiskFreeSpace( QObject *parent = 0 );
 
     /**
      * Destructor - this object autodeletes itself when it's done
@@ -76,56 +48,6 @@
     ~KDiskFreeSpace();
 
     /**
-     * The mount point the requested partition is mounted on.
-     *
-     * Only valid after a successful run of readDF() or findUsageInfo()
-     * (ie. error() returns 0).
-     *
-     * \since 4.2
-     */
-    QString mountPoint() const;
-
-    /**
-     * The size of the requested partition.
-     *
-     * Only valid after a successful run of readDF() or findUsageInfo()
-     * (ie. error() returns 0).
-     *
-     * \since 4.2
-     */
-    KIO::filesize_t size() const;
-
-    /**
-     * The used space of the requested partition.
-     *
-     * Only valid after a successful run of readDF() or findUsageInfo()
-     * (ie. error() returns 0).
-     *
-     * \since 4.2
-     */
-    KIO::filesize_t used() const;
-
-    /**
-     * The available space of the requested partition.
-     *
-     * Only valid after a successful run of readDF() or findUsageInfo()
-     * (ie. error() returns 0).
-     *
-     * \since 4.2
-     */
-    KIO::filesize_t avail() const;
-
-    /**
-     * Returns the error code, if there has been an error. 
-     * Only call this method from the slot connected to finished().
-     *
-     * \return the error code for this job, 0 is no error.
-     *
-     * \since 4.2
-     */
-    int error() const;
-
-    /**
      * Call this to fire a search on the disk usage information
      * for @p mountPoint.
      * The foundMountPoint() signal will be emitted
@@ -137,10 +59,8 @@
      * on a given instance of KDiskFreeSpace, given that it handles only
      * the request for one mount point and then auto-deletes itself.
      * Suicidal objects are not reusable...
-     *
-     * It is recommended to use findUsageInfo instead.
      */
-    bool readDF( const QString & mountPoint );
+    KDE_DEPRECATED bool readDF( const QString & mountPoint );
 
     /**
      * Call this to fire a search on the disk usage information
@@ -149,7 +69,7 @@
      * if this mount point is found, with the info requested.
      * The done() signal is emitted in any case.
      */
-    static KDiskFreeSpace * findUsageInfo( const QString & path ); // KDE 5 -> move \
this into the KIO namespace +     KDE_DEPRECATED static KDiskFreeSpace * \
findUsageInfo( const QString & path );  
 Q_SIGNALS:
     /**
@@ -158,24 +78,14 @@
      * @param kibSize the total size of the partition in KiB
      * @param kibUsed the amount of KiB being used on the partition
      * @param kibAvail the available space on the partition in KiB
-     * \deprecated use finished
      */
     void foundMountPoint( const QString & mountPoint, quint64 kibSize, quint64 \
kibUsed, quint64 kibAvail );  
     /**
      * Emitted when the request made via readDF is over, whether foundMountPoint was \
                emitted or not.
-     * \deprecated use finished
      */
     void done();
 
-    /**
-     * Emitted once the request is done. Check error() to see if the call was
-     * successful.
-     *
-     * \since 4.2
-     */
-    void finished( KDiskFreeSpace* );
-
 private:
     class Private;
     Private * const d;
Index: kio/kfile/kdiskfreespace.cpp
===================================================================
--- kio/kfile/kdiskfreespace.cpp	(revision 837497)
+++ kio/kfile/kdiskfreespace.cpp	(working copy)
@@ -21,37 +21,25 @@
  */
 
 #include "kdiskfreespace.h"
-#include <QtCore/QFile>
-#include <QtCore/QTextIStream>
+#include "kdiskfreespaceinfo.h"
 #include <QtCore/QTimer>
 
 #include <kdebug.h>
-#include <kprocess.h>
-#include <kmountpoint.h>
-#include <kio/global.h>
-#include <config-kfile.h>
 
-class KDiskFreeSpace::Private
+
+struct KDiskFreeSpace::Private
 {
-public:
     Private(KDiskFreeSpace *parent)
-        : m_parent(parent),
-          total(0),
-          avail(0),
-          error(-1)
+        : m_parent(parent)
     {}
 
     bool _k_calculateFreeSpace();
 
     KDiskFreeSpace   *m_parent;
     QString           m_path;
-
-    QString mountPoint;
-    KIO::filesize_t total;
-    KIO::filesize_t avail;
-    int error;
 };
 
+
 KDiskFreeSpace::KDiskFreeSpace(QObject *parent)
     : QObject(parent), d(new Private(this))
 {
@@ -64,90 +52,27 @@
 }
 
 
-QString KDiskFreeSpace::mountPoint() const
-{
-    return d->mountPoint;
-}
-
-
-KIO::filesize_t KDiskFreeSpace::size() const
-{
-    return d->total;
-}
-
-
-KIO::filesize_t KDiskFreeSpace::used() const
-{
-    return d->total - d->avail;
-}
-
-
-KIO::filesize_t KDiskFreeSpace::avail() const
-{
-    return d->avail;
-}
-
-
-int KDiskFreeSpace::error() const
-{
-    return d->error;
-}
-
-
 bool KDiskFreeSpace::readDF( const QString & mountPoint )
 {
     d->m_path = mountPoint;
     return d->_k_calculateFreeSpace();
 }
 
-#ifdef Q_OS_WIN
-#include <QtCore/QDir>
-#include <windows.h>
-#else
-#include <sys/statvfs.h>
-#endif
 
 bool KDiskFreeSpace::Private::_k_calculateFreeSpace()
 {
-    // determine the mount point
-    KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( m_path );
-    if (mp)
-        mountPoint = mp->mountPoint();
-
-    error = -1;
-
-#ifdef Q_OS_WIN
-    quint64 availUser;
-    QFileInfo fi(mountPoint);
-    QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath());
-
-    if(GetDiskFreeSpaceExW((LPCWSTR)dir.utf16(),
-                           (PULARGE_INTEGER)&availUser,
-                           (PULARGE_INTEGER)&total,
-                           (PULARGE_INTEGER)&avail) != 0) {
-        error = 0;
+    KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo( m_path );
+    if ( info.isValid() ) {
+        quint64 sizeKiB = info.size() / 1024;
+        quint64 availKiB = info.available() / 1024;
+        emit m_parent->foundMountPoint( info.mountPoint(), sizeKiB, \
sizeKiB-availKiB, availKiB );  }
-#else
-    struct statvfs statvfs_buf;
 
-    if (!statvfs(QFile::encodeName(m_path).constData(), &statvfs_buf)) {
-        avail = statvfs_buf.f_bavail * statvfs_buf.f_frsize;
-        total = statvfs_buf.f_blocks * statvfs_buf.f_frsize;
-        error = 0;
-    }
-#endif
-
-    if (!error) {
-        quint64 totalKib = total / 1024;
-        quint64 availKib = avail / 1024;
-        emit m_parent->foundMountPoint( mountPoint, totalKib, totalKib-availKib, \
                availKib );
-    }
-
     emit m_parent->done();
-    emit m_parent->finished( m_parent );
+
     m_parent->deleteLater();
 
-    return !error;
+    return info.isValid();
 }
 
 KDiskFreeSpace * KDiskFreeSpace::findUsageInfo( const QString & path )
Index: kio/kfile/kdiskfreespaceinfo.h
===================================================================
--- kio/kfile/kdiskfreespaceinfo.h	(revision 0)
+++ kio/kfile/kdiskfreespaceinfo.h	(revision 0)
@@ -0,0 +1,131 @@
+/*
+ *  kdiskfreespaceinfo.h
+ *
+ *  Copyright 2008 Sebastian Trug <trueg@kde.org>
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Library General Public
+ *  License version 2 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library General Public License
+ *  along with this library; see the file COPYING.LIB.  If not, write to
+ *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *  Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _KDISK_FREE_SPACE_INFO_H_
+#define _KDISK_FREE_SPACE_INFO_H_
+
+#include <QtCore/QSharedDataPointer>
+#include <QtCore/QString>
+
+#include <kio/kio_export.h>
+#include <kio/global.h>
+
+/**
+ * \class KDiskFreeSpaceInfo kdiskfreespaceinfo.h KDiskFreeSpaceInfo
+ *
+ * \brief Determine the space left on an arbitrary partition.
+ *
+ * This class determines the free space left on the partition that holds a given 
+ * path.  This path can be the mount point or any file or directory on the 
+ * partition.
+ *
+ * To find how much space is available on the partition containing @p path, 
+ * simply do the following:
+ *
+ * \code
+ * KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo( path );
+ * if( info.isValid() )
+ *    doSomething( info.available() );
+ * \code
+ *
+ * \author Sebastian Trueg <trueg@kde.org>
+ *
+ * \since 4.2
+ */
+class KIO_EXPORT KDiskFreeSpaceInfo
+{
+public:
+    /**
+     * Copy constructor
+     */
+    KDiskFreeSpaceInfo( const KDiskFreeSpaceInfo& );
+
+    /**
+     * Destructor
+     */
+    ~KDiskFreeSpaceInfo();
+
+    /**
+     * Assignment operator
+     */
+    KDiskFreeSpaceInfo& operator=( const KDiskFreeSpaceInfo& );
+
+    /**
+     * \return \p true if the available disk space was successfully
+     * determined and the values from mountPoint(), size(), available(),
+     * and used() are valid. \p false otherwise.
+     */
+    bool isValid() const;
+
+    /**
+     * The mount point of the partition the requested path points to
+     *
+     * Only valid if isValid() returns \p true.
+     */
+    QString mountPoint() const;
+
+    /**
+     * The total size of the partition mounted at mountPoint()
+     *
+     * Only valid if isValid() returns \p true.
+     *
+     * \return Total size of the requested partition in bytes.
+     */
+    KIO::filesize_t size() const;
+
+    /**
+     * The available space in the partition mounted at mountPoint()
+     *
+     * Only valid if isValid() returns \p true.
+     *
+     * \return Available space left on the requested partition in bytes.
+     */
+    KIO::filesize_t available() const;
+
+    /**
+     * The used space in the partition mounted at mountPoint()
+     *
+     * Only valid if isValid() returns \p true.
+     *
+     * \return Used space on the requested partition in bytes.
+     */
+    KIO::filesize_t used() const;
+
+    /**
+     * Static method used to determine the free disk space.
+     *
+     * \param path An arbitrary path. The available space will be
+     * determined for the partition containing path.
+     *
+     * Check isValid() to see if the process was successful. Then
+     * use mountPoint(), size(), available(), and used() to access
+     * the requested values.
+     */
+    static KDiskFreeSpaceInfo freeSpaceInfo( const QString& path );
+
+private:
+    KDiskFreeSpaceInfo();
+
+    class Private;
+    QSharedDataPointer<Private> d;
+};
+
+#endif
+
Index: kio/kfile/kdiskfreespaceinfo.cpp
===================================================================
--- kio/kfile/kdiskfreespaceinfo.cpp	(revision 0)
+++ kio/kfile/kdiskfreespaceinfo.cpp	(revision 0)
@@ -0,0 +1,141 @@
+/*
+ *  kdiskfreespaceinfo.h
+ *
+ *  Copyright 2008 Sebastian Trug <trueg@kde.org>
+ *
+ *  Based on kdiskfreespace.h
+ *  Copyright 2007 David Faure <faure@kde.org>
+ *  Copyright 2008 Dirk Mueller <mueller@kde.org>
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Library General Public
+ *  License version 2 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library General Public License
+ *  along with this library; see the file COPYING.LIB.  If not, write to
+ *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *  Boston, MA 02110-1301, USA.
+ */
+
+#include "kdiskfreespaceinfo.h"
+
+#include <QtCore/QSharedData>
+#include <QtCore/QFile>
+
+#include <kmountpoint.h>
+
+#ifdef Q_OS_WIN
+#include <QtCore/QDir>
+#include <windows.h>
+#else
+#include <sys/statvfs.h>
+#endif
+
+
+class KDiskFreeSpaceInfo::Private : public QSharedData
+{
+public:
+    Private()
+        : valid(false),
+          size(0),
+          available(0) {
+    }
+
+    bool valid;
+    QString mountPoint;
+    KIO::filesize_t size;
+    KIO::filesize_t available;
+};
+
+
+KDiskFreeSpaceInfo::KDiskFreeSpaceInfo()
+    : d(new Private())
+{
+}
+
+
+KDiskFreeSpaceInfo::KDiskFreeSpaceInfo( const KDiskFreeSpaceInfo& other )
+{
+    d = other.d;
+}
+
+
+KDiskFreeSpaceInfo::~KDiskFreeSpaceInfo()
+{
+}
+
+
+KDiskFreeSpaceInfo& KDiskFreeSpaceInfo::operator=( const KDiskFreeSpaceInfo& other )
+{
+    d = other.d;
+    return *this;
+}
+
+
+bool KDiskFreeSpaceInfo::isValid() const
+{
+    return d->valid;
+}
+
+
+QString KDiskFreeSpaceInfo::mountPoint() const
+{
+    return d->mountPoint;
+}
+
+
+KIO::filesize_t KDiskFreeSpaceInfo::size() const
+{
+    return d->size;
+}
+
+
+KIO::filesize_t KDiskFreeSpaceInfo::available() const
+{
+    return d->available;
+}
+
+
+KIO::filesize_t KDiskFreeSpaceInfo::used() const
+{
+    return d->size - d->available;
+}
+
+
+KDiskFreeSpaceInfo KDiskFreeSpaceInfo::freeSpaceInfo( const QString& path )
+{
+    KDiskFreeSpaceInfo info;
+
+    // determine the mount point
+    KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( path );
+    if (mp)
+        info.d->mountPoint = mp->mountPoint();
+
+#ifdef Q_OS_WIN
+    quint64 availUser;
+    QFileInfo fi(info.d->mountPoint);
+    QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath());
+
+    if(GetDiskFreeSpaceExW((LPCWSTR)dir.utf16(),
+                           (PULARGE_INTEGER)&availUser,
+                           (PULARGE_INTEGER)&info.d->size,
+                           (PULARGE_INTEGER)&info.d->available) != 0) {
+        info.d->valid = true;
+    }
+#else
+    struct statvfs statvfs_buf;
+
+    if (!statvfs(QFile::encodeName(path).constData(), &statvfs_buf)) {
+        info.d->available = statvfs_buf.f_bavail * statvfs_buf.f_frsize;
+        info.d->size = statvfs_buf.f_blocks * statvfs_buf.f_frsize;
+        info.d->valid = true;
+    }
+#endif
+
+    return info;
+}
Index: kio/CMakeLists.txt
===================================================================
--- kio/CMakeLists.txt	(revision 837497)
+++ kio/CMakeLists.txt	(working copy)
@@ -17,6 +17,9 @@
    ${CMAKE_CURRENT_BINARY_DIR}/kfile
    ${CMAKE_CURRENT_BINARY_DIR}      # e.g. for observer_stub.h
    ${CMAKE_SOURCE_DIR}/kwallet/client
+   ${CMAKE_SOURCE_DIR}/nepomuk
+   ${CMAKE_SOURCE_DIR}/nepomuk/core
+   ${CMAKE_SOURCE_DIR}/nepomuk/core/ontology
    ${KDE4_KDEUI_INCLUDES}
    ${CMAKE_SOURCE_DIR}/solid
    ${CMAKE_BINARY_DIR}/solid
@@ -36,6 +39,7 @@
 endif(WIN32)
 
 configure_file(kio/config-kdirwatch.h.cmake \
${CMAKE_CURRENT_BINARY_DIR}/kio/config-kdirwatch.h ) \
+configure_file(kio/config-kfileitem.h.cmake \
${CMAKE_CURRENT_BINARY_DIR}/kio/config-kfileitem.h )  
 configure_file(kssl/ksslconfig.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/kssl/ksslconfig.h \
)  #macro_additional_clean_files( ${CMAKE_CURRENT_BINARY_DIR}/kssl/ksslconfig.h )
@@ -157,6 +161,7 @@
   kfile/kdevicelistitem.cpp
   kfile/kdevicelistmodel.cpp
   kfile/kdiskfreespace.cpp
+  kfile/kdiskfreespaceinfo.cpp
   kfile/kencodingfiledialog.cpp
   kfile/kfile.cpp
   kfile/kfiledialog.cpp
@@ -246,6 +251,9 @@
 
 target_link_libraries(kio ${KDE4_KDEUI_LIBS} ${ZLIB_LIBRARY} \
${STRIGI_STREAMANALYZER_LIBRARY} ${STRIGI_STREAMS_LIBRARY} ${KDE4_SOLID_LIBS} \
${QT_QTNETWORK_LIBRARY} ${QT_QTXML_LIBRARY} ${X11_LIBRARIES})  
+if(HAVE_NEPOMUK)
+  target_link_libraries(kio nepomuk)
+endif(HAVE_NEPOMUK)
 
 set(SYS_INOTIFY_H_FOUND 0)
 if(SYS_INOTIFY_H_FOUND)
@@ -379,6 +387,7 @@
   kfile/kabstractfilewidget.h
   kfile/kdevicelistmodel.h
   kfile/kdiskfreespace.h
+  kfile/kdiskfreespaceinfo.h
   kfile/kencodingfiledialog.h
   kfile/kfile.h
   kfile/kfiledialog.h
Index: includes/KDiskFreeSpaceInfo
===================================================================
--- includes/KDiskFreeSpaceInfo	(revision 0)
+++ includes/KDiskFreeSpaceInfo	(revision 0)
@@ -0,0 +1 @@
+#include "../kdiskfreespaceinfo.h"
Index: includes/CMakeLists.txt
===================================================================
--- includes/CMakeLists.txt	(revision 837497)
+++ includes/CMakeLists.txt	(working copy)
@@ -147,6 +147,7 @@
   KDirSelectDialog
   KDirWatch
   KDiskFreeSpace
+  KDiskFreeSpaceInfo
   KDoubleNumInput
   KDoubleSpinBox
   KDoubleValidator



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic