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

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

Hi,

please find attached a patch for KDiskFreeSpace. I found it to be very 
inconvenient to use. You would have to connect to two signals of a one-shot 
object. That is really not nice. So the patch transforms the usage to 
something very similar to KJob: a finished signal which has the 
KDiskFreeSpace as parameter. You then check for success and get the actual 
values from the object. Afterwards it is deleted as before. 
Oh, and it now uses byte values instead of Kib. I don't really see the reason 
there. Also, we are in KIO, so I thought KIO::filesize_t was appropriate.

BTW: a one-shot-object that you can create yourself? IMHO it should only 
delete itself when used through the static method.

May I commit? BC is no problem, old API is marked deprecated (in documentation 
that is).

Cheers,
Sebastian

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

Index: kdiskfreespace.h
===================================================================
--- kdiskfreespace.h	(revision 836355)
+++ kdiskfreespace.h	(working copy)
@@ -3,6 +3,7 @@
  *
  * 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
@@ -27,12 +28,35 @@
 #include <QtCore/QString>
 
 #include <kio/kio_export.h>
+#include <kio/global.h>
 
 class KDiskFreeSpacePrivate;
 
 /**
- * This class parses the output of "df" to find the disk usage
- * information for a given partition (mount point).
+ * \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
  */
 class KIO_EXPORT KDiskFreeSpace : public QObject
 {
@@ -42,6 +66,8 @@
 
     /**
      * Constructor
+     *
+     * It is recommended to use findUsageInfo instead.
      */
     explicit KDiskFreeSpace( QObject *parent = 0 );
 
@@ -51,6 +77,46 @@
     ~KDiskFreeSpace();
 
     /**
+     * The mount point the requested partition is mounted on.
+     *
+     * Only valid after a successful run of readDF() or findUsageInfo()
+     * (ie. error() returns 0).
+     */
+    QString mountPoint() const;
+
+    /**
+     * The size of the requested partition.
+     *
+     * Only valid after a successful run of readDF() or findUsageInfo()
+     * (ie. error() returns 0).
+     */
+    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).
+     */
+    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).
+     */
+    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.
+     */
+    int error() const;
+
+    /**
      * Call this to fire a search on the disk usage information
      * for @p mountPoint.
      * The foundMountPoint() signal will be emitted
@@ -62,6 +128,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 );
 
@@ -81,14 +149,22 @@
      * @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.
+     */
+    void finished( KDiskFreeSpace* );
+
 private:
     class Private;
     Private * const d;
Index: kdiskfreespace.cpp
===================================================================
--- kdiskfreespace.cpp	(revision 836355)
+++ kdiskfreespace.cpp	(working copy)
@@ -3,6 +3,7 @@
  *
  * 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
@@ -34,13 +35,21 @@
 {
 public:
     Private(KDiskFreeSpace *parent)
-        : m_parent(parent)
+        : m_parent(parent),
+          total(0),
+          avail(0),
+          error(-1)
     {}
 
     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)
@@ -54,6 +63,37 @@
     delete d;
 }
 
+
+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;
@@ -70,15 +110,14 @@
 bool KDiskFreeSpace::Private::_k_calculateFreeSpace()
 {
     // determine the mount point
-    QString mountPoint;
-
     KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( m_path );
     if (mp)
         mountPoint = mp->mountPoint();
 
-    quint64 availUser, total, avail;
-    bool bRet = false;
+    error = -1;
+
 #ifdef Q_OS_WIN
+    quint64 availUser;
     QFileInfo fi(mountPoint);
     QString dir = QDir::toNativeSeparators(fi.absoluteDir().canonicalPath());
 
@@ -86,27 +125,29 @@
                            (PULARGE_INTEGER)&availUser,
                            (PULARGE_INTEGER)&total,
                            (PULARGE_INTEGER)&avail) != 0) {
-        availUser = availUser / 1024;
-        total = total / 1024;
-        avail = avail / 1024;
-        emit m_parent->foundMountPoint( mountPoint, total, total-avail, avail );
-        bRet = true;
+        error = 0;
     }
 #else
     struct statvfs statvfs_buf;
 
     if (!statvfs(QFile::encodeName(m_path).constData(), &statvfs_buf)) {
-        avail = statvfs_buf.f_bavail * statvfs_buf.f_frsize / 1024;
-        total = statvfs_buf.f_blocks * statvfs_buf.f_frsize / 1024;
-        emit m_parent->foundMountPoint( mountPoint, total, total-avail, avail );
-        bRet = true;
+        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 bRet;
+    return !error;
 }
 
 KDiskFreeSpace * KDiskFreeSpace::findUsageInfo( const QString & path )



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

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