Sophie

Sophie

distrib > Mageia > 9 > aarch64 > by-pkgid > 7d6809a8f5d4e472c9107f6868e4297a > files > 1

pcmanfm-qt-1.3.0-2.mga9.src.rpm

From b4ed393399063c8a9eb65b42dcbc1145879cfa79 Mon Sep 17 00:00:00 2001
From: Tsu Jan <tsujan2000@gmail.com>
Date: Tue, 18 Apr 2023 21:42:49 +0330
Subject: [PATCH] Check if wallpaper cache is up-to-date on reading Desktop
 settings

The modification time of the wallpaper file is checked to know if the cache is up-to-date, but only when Desktop settings are read (i.e., at startup or on applying Desktop Preferences).

This is needed when the wallpaper is changed but its path and name are not. It fixes https://github.com/lxqt/pcmanfm-qt/issues/1764
---
 pcmanfm/desktopwindow.cpp | 60 ++++++++++++++++++++++++---------------
 pcmanfm/desktopwindow.h   |  4 +--
 2 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/pcmanfm/desktopwindow.cpp b/pcmanfm/desktopwindow.cpp
index 74879c9f..4ee1f1cf 100644
--- a/pcmanfm/desktopwindow.cpp
+++ b/pcmanfm/desktopwindow.cpp
@@ -569,7 +569,8 @@ QImage DesktopWindow::getWallpaperImage() const {
     return image;
 }
 
-QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) {
+QImage DesktopWindow::loadWallpaperFile(QSize requiredSize, bool checkMTime) {
+   static const QString timeFormat(QLatin1String("yyyy-MM-dd-hh:mm:ss.zzz"));
     // NOTE: for ease of programming, we only use the cache for the primary screen.
     bool useCache = (screenNum_ == -1 || screenNum_ == 0);
     QFile info;
@@ -586,35 +587,45 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) {
         cacheFileName += QLatin1String("/wallpaper.cache");
 
         // read info file
-        QString origin;
+        QString origin, mtime;
         info.setFileName(cacheFileName + QStringLiteral(".info"));
         if(info.open(QIODevice::ReadOnly)) {
-            // FIXME: we need to compare mtime to see if the cache is out of date
             origin = QString::fromLocal8Bit(info.readLine());
+            if(origin.endsWith(QLatin1Char('\n'))) {
+                origin.chop(1);
+                if(checkMTime) {
+                    mtime = QString::fromLocal8Bit(info.readLine());
+                }
+            }
             info.close();
-            if(!origin.isEmpty()) {
-                // try to see if we can get the size of the cached image.
-                QImageReader reader(cacheFileName);
-                reader.setAutoDetectImageFormat(true);
-                QSize cachedSize = reader.size();
-                qDebug() << "size of cached file" << cachedSize << ", requiredSize:" << requiredSize;
-                if(cachedSize.isValid()) {
-                    if(cachedSize == requiredSize) { // see if the cached wallpaper has the size we want
-                        QImage image = reader.read(); // return the loaded image
-                        qDebug() << "origin" << origin;
-                        if(origin == wallpaperFile_) {
+            if(!origin.isEmpty() && origin == wallpaperFile_) {
+                // if needed, check whether the cache is up-to-date
+                bool isUptodate = true;
+                if(checkMTime) {
+                    isUptodate = (mtime == QFileInfo(wallpaperFile_).lastModified().toString(timeFormat));
+                }
+                if(isUptodate) {
+                    // try to see if we can get the size of the cached image.
+                    QImageReader reader(cacheFileName);
+                    reader.setAutoDetectImageFormat(true);
+                    QSize cachedSize = reader.size();
+                    //qDebug() << "size of cached file" << cachedSize << ", requiredSize:" << requiredSize;
+                    if(cachedSize.isValid()) {
+                        if(cachedSize == requiredSize) { // see if the cached wallpaper has the size we want
+                            QImage image = reader.read(); // return the loaded image
+                            //qDebug() << "origin" << origin;
                             return image;
                         }
                     }
                 }
             }
         }
-        qDebug() << "no cached wallpaper. generate a new one!";
+        //qDebug() << "no cached wallpaper. generate a new one!";
     }
 
     // we don't have a cached scaled image, load the original file
     QImage image = getWallpaperImage();
-    qDebug() << "size of original image" << image.size();
+    //qDebug() << "size of original image" << image.size();
     if(image.isNull() || image.size() == requiredSize) { // if the original size is what we want
         return image;
     }
@@ -624,9 +635,12 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) {
     // FIXME: should we save the scaled image if its size is larger than the original image?
 
     if(useCache) {
-        // write the path of the original image to the .info file
+        // write the path and modification time of the original image to the .info file
         if(info.open(QIODevice::WriteOnly)) {
-            info.write(wallpaperFile_.toLocal8Bit());
+            QTextStream out(&info);
+            out << wallpaperFile_
+                << QLatin1Char('\n')
+                << QFileInfo(wallpaperFile_).lastModified().toString(timeFormat);
             info.close();
 
             // write the scaled cache image to disk
@@ -639,14 +653,14 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) {
             }
             scaled.save(cacheFileName, format);
         }
-        qDebug() << "wallpaper cached saved to " << cacheFileName;
+        //qDebug() << "wallpaper cached saved to " << cacheFileName;
         // FIXME: we might delay the write of the cached image?
     }
     return scaled;
 }
 
 // really generate the background pixmap according to current settings and apply it.
-void DesktopWindow::updateWallpaper() {
+void DesktopWindow::updateWallpaper(bool checkMTime) {
     if(wallpaperMode_ != WallpaperNone) {  // use wallpaper
         auto screen = getDesktopScreen();
         if(screen == nullptr) {
@@ -700,7 +714,7 @@ void DesktopWindow::updateWallpaper() {
                 pixmap.setDevicePixelRatio(DPRatio);
             }
             else {
-                image = loadWallpaperFile(pixmapSize);
+                image = loadWallpaperFile(pixmapSize, checkMTime);
                 pixmap = QPixmap::fromImage(image);
                 pixmap.setDevicePixelRatio(DPRatio);
             }
@@ -795,7 +809,7 @@ void DesktopWindow::updateWallpaper() {
                         QSize desiredSize = origSize;
                         Qt::AspectRatioMode mode = (wallpaperMode_ == WallpaperFit ? Qt::KeepAspectRatio : Qt::KeepAspectRatioByExpanding);
                         desiredSize.scale(pixmapSize, mode);
-                        image = loadWallpaperFile(desiredSize); // load the scaled image
+                        image = loadWallpaperFile(desiredSize, checkMTime); // load the scaled image
                     }
                 }
                 if(!image.isNull()) {
@@ -963,7 +977,7 @@ void DesktopWindow::updateFromSettings(Settings& settings, bool changeSlide) {
         wallpaperTimer_ = nullptr;
     }
 
-    updateWallpaper();
+    updateWallpaper(true);
     update();
 
     if(wallpaperTimer_) {
diff --git a/pcmanfm/desktopwindow.h b/pcmanfm/desktopwindow.h
index 9595de93..666cc59d 100644
--- a/pcmanfm/desktopwindow.h
+++ b/pcmanfm/desktopwindow.h
@@ -72,7 +72,7 @@ class DesktopWindow : public View {
     void setWallpaperRandomize(bool randomize);
 
     // void setWallpaperAlpha(qreal alpha);
-    void updateWallpaper();
+    void updateWallpaper(bool checkMTime = false);
     bool pickWallpaper();
     void nextWallpaper();
     void updateFromSettings(Settings& settings, bool changeSlide = true);
@@ -99,7 +99,7 @@ class DesktopWindow : public View {
     void retrieveCustomPos();
     void storeCustomPos();
 
-    QImage loadWallpaperFile(QSize requiredSize);
+    QImage loadWallpaperFile(QSize requiredSize, bool checkMTime);
 
     virtual bool event(QEvent* event) override;
     virtual bool eventFilter(QObject* watched, QEvent* event) override;