1 |
From b4ed393399063c8a9eb65b42dcbc1145879cfa79 Mon Sep 17 00:00:00 2001 |
2 |
From: Tsu Jan <tsujan2000@gmail.com> |
3 |
Date: Tue, 18 Apr 2023 21:42:49 +0330 |
4 |
Subject: [PATCH] Check if wallpaper cache is up-to-date on reading Desktop |
5 |
settings |
6 |
|
7 |
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). |
8 |
|
9 |
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 |
10 |
--- |
11 |
pcmanfm/desktopwindow.cpp | 60 ++++++++++++++++++++++++--------------- |
12 |
pcmanfm/desktopwindow.h | 4 +-- |
13 |
2 files changed, 39 insertions(+), 25 deletions(-) |
14 |
|
15 |
diff --git a/pcmanfm/desktopwindow.cpp b/pcmanfm/desktopwindow.cpp |
16 |
index 74879c9f..4ee1f1cf 100644 |
17 |
--- a/pcmanfm/desktopwindow.cpp |
18 |
+++ b/pcmanfm/desktopwindow.cpp |
19 |
@@ -569,7 +569,8 @@ QImage DesktopWindow::getWallpaperImage() const { |
20 |
return image; |
21 |
} |
22 |
|
23 |
-QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) { |
24 |
+QImage DesktopWindow::loadWallpaperFile(QSize requiredSize, bool checkMTime) { |
25 |
+ static const QString timeFormat(QLatin1String("yyyy-MM-dd-hh:mm:ss.zzz")); |
26 |
// NOTE: for ease of programming, we only use the cache for the primary screen. |
27 |
bool useCache = (screenNum_ == -1 || screenNum_ == 0); |
28 |
QFile info; |
29 |
@@ -586,35 +587,45 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) { |
30 |
cacheFileName += QLatin1String("/wallpaper.cache"); |
31 |
|
32 |
// read info file |
33 |
- QString origin; |
34 |
+ QString origin, mtime; |
35 |
info.setFileName(cacheFileName + QStringLiteral(".info")); |
36 |
if(info.open(QIODevice::ReadOnly)) { |
37 |
- // FIXME: we need to compare mtime to see if the cache is out of date |
38 |
origin = QString::fromLocal8Bit(info.readLine()); |
39 |
+ if(origin.endsWith(QLatin1Char('\n'))) { |
40 |
+ origin.chop(1); |
41 |
+ if(checkMTime) { |
42 |
+ mtime = QString::fromLocal8Bit(info.readLine()); |
43 |
+ } |
44 |
+ } |
45 |
info.close(); |
46 |
- if(!origin.isEmpty()) { |
47 |
- // try to see if we can get the size of the cached image. |
48 |
- QImageReader reader(cacheFileName); |
49 |
- reader.setAutoDetectImageFormat(true); |
50 |
- QSize cachedSize = reader.size(); |
51 |
- qDebug() << "size of cached file" << cachedSize << ", requiredSize:" << requiredSize; |
52 |
- if(cachedSize.isValid()) { |
53 |
- if(cachedSize == requiredSize) { // see if the cached wallpaper has the size we want |
54 |
- QImage image = reader.read(); // return the loaded image |
55 |
- qDebug() << "origin" << origin; |
56 |
- if(origin == wallpaperFile_) { |
57 |
+ if(!origin.isEmpty() && origin == wallpaperFile_) { |
58 |
+ // if needed, check whether the cache is up-to-date |
59 |
+ bool isUptodate = true; |
60 |
+ if(checkMTime) { |
61 |
+ isUptodate = (mtime == QFileInfo(wallpaperFile_).lastModified().toString(timeFormat)); |
62 |
+ } |
63 |
+ if(isUptodate) { |
64 |
+ // try to see if we can get the size of the cached image. |
65 |
+ QImageReader reader(cacheFileName); |
66 |
+ reader.setAutoDetectImageFormat(true); |
67 |
+ QSize cachedSize = reader.size(); |
68 |
+ //qDebug() << "size of cached file" << cachedSize << ", requiredSize:" << requiredSize; |
69 |
+ if(cachedSize.isValid()) { |
70 |
+ if(cachedSize == requiredSize) { // see if the cached wallpaper has the size we want |
71 |
+ QImage image = reader.read(); // return the loaded image |
72 |
+ //qDebug() << "origin" << origin; |
73 |
return image; |
74 |
} |
75 |
} |
76 |
} |
77 |
} |
78 |
} |
79 |
- qDebug() << "no cached wallpaper. generate a new one!"; |
80 |
+ //qDebug() << "no cached wallpaper. generate a new one!"; |
81 |
} |
82 |
|
83 |
// we don't have a cached scaled image, load the original file |
84 |
QImage image = getWallpaperImage(); |
85 |
- qDebug() << "size of original image" << image.size(); |
86 |
+ //qDebug() << "size of original image" << image.size(); |
87 |
if(image.isNull() || image.size() == requiredSize) { // if the original size is what we want |
88 |
return image; |
89 |
} |
90 |
@@ -624,9 +635,12 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) { |
91 |
// FIXME: should we save the scaled image if its size is larger than the original image? |
92 |
|
93 |
if(useCache) { |
94 |
- // write the path of the original image to the .info file |
95 |
+ // write the path and modification time of the original image to the .info file |
96 |
if(info.open(QIODevice::WriteOnly)) { |
97 |
- info.write(wallpaperFile_.toLocal8Bit()); |
98 |
+ QTextStream out(&info); |
99 |
+ out << wallpaperFile_ |
100 |
+ << QLatin1Char('\n') |
101 |
+ << QFileInfo(wallpaperFile_).lastModified().toString(timeFormat); |
102 |
info.close(); |
103 |
|
104 |
// write the scaled cache image to disk |
105 |
@@ -639,14 +653,14 @@ QImage DesktopWindow::loadWallpaperFile(QSize requiredSize) { |
106 |
} |
107 |
scaled.save(cacheFileName, format); |
108 |
} |
109 |
- qDebug() << "wallpaper cached saved to " << cacheFileName; |
110 |
+ //qDebug() << "wallpaper cached saved to " << cacheFileName; |
111 |
// FIXME: we might delay the write of the cached image? |
112 |
} |
113 |
return scaled; |
114 |
} |
115 |
|
116 |
// really generate the background pixmap according to current settings and apply it. |
117 |
-void DesktopWindow::updateWallpaper() { |
118 |
+void DesktopWindow::updateWallpaper(bool checkMTime) { |
119 |
if(wallpaperMode_ != WallpaperNone) { // use wallpaper |
120 |
auto screen = getDesktopScreen(); |
121 |
if(screen == nullptr) { |
122 |
@@ -700,7 +714,7 @@ void DesktopWindow::updateWallpaper() { |
123 |
pixmap.setDevicePixelRatio(DPRatio); |
124 |
} |
125 |
else { |
126 |
- image = loadWallpaperFile(pixmapSize); |
127 |
+ image = loadWallpaperFile(pixmapSize, checkMTime); |
128 |
pixmap = QPixmap::fromImage(image); |
129 |
pixmap.setDevicePixelRatio(DPRatio); |
130 |
} |
131 |
@@ -795,7 +809,7 @@ void DesktopWindow::updateWallpaper() { |
132 |
QSize desiredSize = origSize; |
133 |
Qt::AspectRatioMode mode = (wallpaperMode_ == WallpaperFit ? Qt::KeepAspectRatio : Qt::KeepAspectRatioByExpanding); |
134 |
desiredSize.scale(pixmapSize, mode); |
135 |
- image = loadWallpaperFile(desiredSize); // load the scaled image |
136 |
+ image = loadWallpaperFile(desiredSize, checkMTime); // load the scaled image |
137 |
} |
138 |
} |
139 |
if(!image.isNull()) { |
140 |
@@ -963,7 +977,7 @@ void DesktopWindow::updateFromSettings(Settings& settings, bool changeSlide) { |
141 |
wallpaperTimer_ = nullptr; |
142 |
} |
143 |
|
144 |
- updateWallpaper(); |
145 |
+ updateWallpaper(true); |
146 |
update(); |
147 |
|
148 |
if(wallpaperTimer_) { |
149 |
diff --git a/pcmanfm/desktopwindow.h b/pcmanfm/desktopwindow.h |
150 |
index 9595de93..666cc59d 100644 |
151 |
--- a/pcmanfm/desktopwindow.h |
152 |
+++ b/pcmanfm/desktopwindow.h |
153 |
@@ -72,7 +72,7 @@ class DesktopWindow : public View { |
154 |
void setWallpaperRandomize(bool randomize); |
155 |
|
156 |
// void setWallpaperAlpha(qreal alpha); |
157 |
- void updateWallpaper(); |
158 |
+ void updateWallpaper(bool checkMTime = false); |
159 |
bool pickWallpaper(); |
160 |
void nextWallpaper(); |
161 |
void updateFromSettings(Settings& settings, bool changeSlide = true); |
162 |
@@ -99,7 +99,7 @@ class DesktopWindow : public View { |
163 |
void retrieveCustomPos(); |
164 |
void storeCustomPos(); |
165 |
|
166 |
- QImage loadWallpaperFile(QSize requiredSize); |
167 |
+ QImage loadWallpaperFile(QSize requiredSize, bool checkMTime); |
168 |
|
169 |
virtual bool event(QEvent* event) override; |
170 |
virtual bool eventFilter(QObject* watched, QEvent* event) override; |