Skip to content

Commit

Permalink
fix: Wrong clean cache folder
Browse files Browse the repository at this point in the history
之前设计的缓存目录名重复,在多实例下, 其它实例退出
会导致缓存目录被误删除. 调整缓存目录设计,改到/tmp
目录且每个实例唯一.

Log: 修复误删除缓存目录的问题
Bug: https://pms.uniontech.com/bug-view-247289.html
Influence: SetWallpaper
  • Loading branch information
rb-union committed Mar 18, 2024
1 parent f6a4a91 commit f1d5b49
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
54 changes: 45 additions & 9 deletions libimageviewer/unionimage/imageutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QReadWriteLock>
#include <QUrl>
#include <QApplication>
#include <QTemporaryDir>

namespace Libutils {

Expand Down Expand Up @@ -824,32 +825,67 @@ bool isCanRemove(const QString &path)
return bRet;
}

// 缓存文件路径及互斥锁
static QMutex s_CachePathMutex;
static QString s_CachePath;

/**
* @brief 取得图像缓存文件夹路径
@brief 取得图像缓存文件夹路径,未成功初始化将自动创建
@threadsafe
*/
QString getCacheImagePath()
{
return QDir::homePath() + "/.cache/deepin/deepin-image-viewer/cache_image";
QMutexLocker _locker(&s_CachePathMutex);
if (s_CachePath.isEmpty()) {
_locker.unlock();
initCacheImageFolder();
_locker.relock();
}

return s_CachePath;
}

/**
* @brief 初始化图像缓存文件夹并返回是否初始化成功
@brief 初始化图像临时缓存文件夹(/tmp/image-viewer-cache_XXXXXX)并返回是否初始化成功,仅在加载图片时调用
看图为多实例进程,不同实例创建缓存位置不同
@threadsafe
*/
bool initCacheImageFolder()
{
QDir homeDir(QDir::homePath());
return homeDir.mkpath(".cache/deepin/deepin-image-viewer/cache_image");
QMutexLocker _locker(&s_CachePathMutex);
if (!s_CachePath.isEmpty()) {
return true;
}

QTemporaryDir dir(QDir::tempPath() + QDir::separator() + "image-viewer-cache_XXXXXX");
dir.setAutoRemove(false);
if (dir.isValid()) {
s_CachePath = dir.path();
return true;
}

qWarning() << QString("Create cache image folder failed, %1:%2").arg(dir.path()).arg(dir.errorString());
return false;
}

/**
* @brief 清空图像缓存文件夹并返回是否清理成功,
* 在程序启动或打开文件时调用
@brief 清空图像缓存文件夹并返回是否清理成功,在程序退出时调用。
@threadsafe
*/
bool clearCacheImageFolder()
{
QDir cacheDir(getCacheImagePath());
QMutexLocker _locker(&s_CachePathMutex);
if (s_CachePath.isEmpty()) {
return false;
}

QDir cacheDir(s_CachePath);
if (cacheDir.exists()) {
return cacheDir.removeRecursively();
bool ret = cacheDir.removeRecursively();
if (ret) {
s_CachePath.clear();
}
return ret;
}
return false;
}
Expand Down
28 changes: 13 additions & 15 deletions libimageviewer/viewpanel/viewpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ LibViewPanel::~LibViewPanel()
void LibViewPanel::loadImage(const QString &path, QStringList paths)
{
// 初始化图像缓存目录
Libutils::image::clearCacheImageFolder();
Libutils::image::initCacheImageFolder();

QFileInfo info(path);
Expand Down Expand Up @@ -883,34 +882,33 @@ static void setWallpaperWithDBus(const QString &path)

void LibViewPanel::setWallpaper(const QImage &img)
{
QThread *th1 = QThread::create([ = ]() {
if (!img.isNull()) {
if (!img.isNull()) {
QString tempPathTemplate = Libutils::image::getCacheImagePath() + QDir::separator() + "XXXXXX_Wallpaper.png";
QThread *th1 = QThread::create([ = ]() {
// 设置锁屏壁纸不能使用相同名称,且临时文件不能立即删除(调用DBus接口拷贝需要时间),保留至缓存目录,程序退出自动清理
QTemporaryFile tmpImage;
tmpImage.setAutoRemove(false);
tmpImage.setFileTemplate(Libutils::image::getCacheImagePath() + QDir::separator() + "XXXXXX_Wallpaper.png");
tmpImage.setFileTemplate(tempPathTemplate);
if (!tmpImage.open() || !img.save(tmpImage.fileName(), "PNG")) {
qWarning() << QString("Copy image set wallpaper failed! path: %1").arg(tmpImage.fileName());
return;
}
qInfo() << QString("Copy image set wallpaper, path: %1").arg(tmpImage.fileName());

setWallpaperWithDBus(tmpImage.fileName());
}
});
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
});
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
}
}

void LibViewPanel::setWallpaper(const QString &imgPath)
{
QThread *th1 = QThread::create([ = ]() {
if (!imgPath.isNull()) {
setWallpaperWithDBus(imgPath);
}
});
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
if (!imgPath.isEmpty()) {
QThread *th1 = QThread::create([ = ]() { setWallpaperWithDBus(imgPath); });
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
}
}

bool LibViewPanel::startdragImage(const QStringList &paths, const QString &firstPath)
Expand Down

0 comments on commit f1d5b49

Please sign in to comment.