]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/places/placesitemlistwidget.cpp
Port away from KInit
[dolphin.git] / src / panels / places / placesitemlistwidget.cpp
index 4be7ac021ae0e8b8dda4ef36802f12a53efa1ff9..2d5111c56c5d0394ad31548c39d914994f018397 100644 (file)
@@ -6,17 +6,18 @@
 
 #include "placesitemlistwidget.h"
 
-#include <QDebug>
-
-#include <QDateTime>
-#include <QGraphicsView>
 #include <QStyleOption>
+#include <QPainter>
+
+#include <KColorScheme>
 
 #include <KIO/FileSystemFreeSpaceJob>
-#include <KMountPoint>
+#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) :
@@ -42,53 +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
-            )
-        ) {
-            // qDebug() << "url:" << url;
-            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;
-                    // qDebug() << "job.url:" << data().value("url").toUrl();
-                    // qDebug() << "job.size:" << m_freeSpaceInfo.size;
-                    // qDebug() << "job.used:" << m_freeSpaceInfo.used;
-                    // qDebug() << "job.ratio:" << m_freeSpaceInfo.usedRatio;
-                    // qDebug() << "job.draw:" << m_drawCapacityBar;
-
-                    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.setRemainingTime(0);
     m_freeSpaceInfo.size = 0;
     m_freeSpaceInfo.used = 0;
     m_freeSpaceInfo.usedRatio = 0;
@@ -105,12 +106,13 @@ 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();
 
-            QRect capacityRect(
+            const QRect capacityRect(
                 textInfo->pos.x(),
                 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
                 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
@@ -119,42 +121,23 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt
 
             const QPalette pal = palette();
             const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
-            // QColor bgColor = QColor::fromRgb(230, 230, 230);
-            // QColor outlineColor = QColor::fromRgb(208, 208, 208);
-            // QColor bgColor = QColor::fromRgb(0, 230, 0);
-            // QColor outlineColor = QColor::fromRgb(208, 0, 0, 127);
-            // QColor normalUsedColor = QColor::fromRgb(38, 160, 218);
-            // QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
-            // QColor bgColor = pal.base().color().darker(130);
-            // QColor outlineColor = pal.base().color().darker(150);
-
-            QPalette::ColorRole role;
-            // role = isSelected() ? QPalette::Highlight : QPalette::Window;
-            // QColor bgColor = styleOption().palette.color(group, role).darker(150);
-            // QColor outlineColor = styleOption().palette.color(group, role).darker(170);
-            QColor bgColor = isSelected()
-                ? styleOption().palette.color(group, QPalette::Highlight).darker(180)
-                : styleOption().palette.color(group, QPalette::Window).darker(120);
-
-            role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
-            QColor normalUsedColor = styleOption().palette.color(group, role);
-
-            QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
 
             // Background
-            painter->fillRect(capacityRect, bgColor);
+            const QColor bgColor = isSelected()
+                ? pal.color(group, QPalette::Highlight).darker(180)
+                : pal.color(group, QPalette::Window).darker(120);
 
-            // Outline
-            // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
-            // painter->setPen(outlineColor);
-            // painter->drawRect(outlineRect);
+            painter->fillRect(capacityRect, bgColor);
 
             // Fill
             const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
-            if (m_freeSpaceInfo.usedRatio < 0.95) { // Fill
-                painter->fillRect(fillRect, normalUsedColor);
-            } else {
+            if (m_freeSpaceInfo.usedRatio >= 0.95) { // More than 95% full!
+                const QColor dangerUsedColor = KColorScheme(group, KColorScheme::View).foreground(KColorScheme::NegativeText).color();
                 painter->fillRect(fillRect, dangerUsedColor);
+            } else {
+                const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
+                const QColor normalUsedColor = styleOption().palette.color(group, role);
+                painter->fillRect(fillRect, normalUsedColor);
             }
 
             painter->restore();