]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/places/placesitemlistwidget.cpp
fix padding in places view
[dolphin.git] / src / panels / places / placesitemlistwidget.cpp
index 12292ad95be9726790e57cfc4bfde2b19a4f6093..2d5111c56c5d0394ad31548c39d914994f018397 100644 (file)
@@ -6,16 +6,18 @@
 
 #include "placesitemlistwidget.h"
 
-#include <QDateTime>
 #include <QStyleOption>
 #include <QPainter>
 
 #include <KColorScheme>
 
 #include <KIO/FileSystemFreeSpaceJob>
+#include <Solid/Device>
+#include <Solid/NetworkShare>
 
 #define CAPACITYBAR_HEIGHT 2
 #define CAPACITYBAR_MARGIN 2
+#define CAPACITYBAR_CACHE_TTL 60000
 
 
 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
@@ -41,49 +43,53 @@ QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
 
 void PlacesItemListWidget::updateCapacityBar()
 {
-    const bool isDevice = !data().value("udi").toString().isEmpty();
-    const QUrl url = data().value("url").toUrl();
-    if (isDevice && url.isLocalFile()) {
-        if (!m_freeSpaceInfo.job
-            && (
-                !m_freeSpaceInfo.lastUpdated.isValid()
-                || m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
-            )
-        ) {
-            m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
-            connect(
-                m_freeSpaceInfo.job,
-                &KIO::FileSystemFreeSpaceJob::result,
-                this,
-                [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
-                    // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
-                    m_freeSpaceInfo.lastUpdated = QDateTime::currentDateTimeUtc();
-
-                    if (job->error()) {
-                        return;
-                    }
-
-                    m_freeSpaceInfo.size = size;
-                    m_freeSpaceInfo.used = size - available;
-                    m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
-                    m_drawCapacityBar = size > 0;
-
-                    update();
-                }
-            );
-        } else {
-            // Job running or cache is still valid.
-        }
-    } else {
+    const QString udi = data().value("udi").toString();
+    if (udi.isEmpty()) {
         resetCapacityBar();
+        return;
     }
+    const Solid::Device device = Solid::Device(udi);
+    if (device.isDeviceInterface(Solid::DeviceInterface::NetworkShare)
+            || device.isDeviceInterface(Solid::DeviceInterface::OpticalDrive)
+            || device.isDeviceInterface(Solid::DeviceInterface::OpticalDisc)) {
+        resetCapacityBar();
+        return;
+    }
+    const QUrl url = data().value("url").toUrl();
+
+    if (url.isEmpty() || m_freeSpaceInfo.job || !m_freeSpaceInfo.lastUpdated.hasExpired()) {
+        // No url, job running or cache is still valid.
+        return;
+    }
+
+    m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
+    connect(
+        m_freeSpaceInfo.job,
+        &KIO::FileSystemFreeSpaceJob::result,
+        this,
+        [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
+            // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
+            m_freeSpaceInfo.lastUpdated.setRemainingTime(CAPACITYBAR_CACHE_TTL);
+
+            if (job->error()) {
+                return;
+            }
+
+            m_freeSpaceInfo.size = size;
+            m_freeSpaceInfo.used = size - available;
+            m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
+            m_drawCapacityBar = size > 0;
+
+            update();
+        }
+    );
 }
 
 void PlacesItemListWidget::resetCapacityBar()
 {
     m_drawCapacityBar = false;
     delete m_freeSpaceInfo.job;
-    m_freeSpaceInfo.lastUpdated = QDateTime();
+    m_freeSpaceInfo.lastUpdated.setRemainingTime(0);
     m_freeSpaceInfo.size = 0;
     m_freeSpaceInfo.used = 0;
     m_freeSpaceInfo.usedRatio = 0;
@@ -100,7 +106,8 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt
 {
     KStandardItemListWidget::paint(painter, option, widget);
 
-    if (m_drawCapacityBar) {
+    // We check if option=nullptr since it is null when the place is dragged (Bug #430441)
+    if (m_drawCapacityBar && option) {
         const TextInfo* textInfo = m_textInfo.value("text");
         if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
             painter->save();