diff --git a/libimageviewer/unionimage/imageutils.cpp b/libimageviewer/unionimage/imageutils.cpp index eb854886..28d57562 100755 --- a/libimageviewer/unionimage/imageutils.cpp +++ b/libimageviewer/unionimage/imageutils.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace Libutils { @@ -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; } diff --git a/libimageviewer/viewpanel/viewpanel.cpp b/libimageviewer/viewpanel/viewpanel.cpp index 1146cadf..a5e4e83b 100644 --- a/libimageviewer/viewpanel/viewpanel.cpp +++ b/libimageviewer/viewpanel/viewpanel.cpp @@ -179,7 +179,6 @@ LibViewPanel::~LibViewPanel() void LibViewPanel::loadImage(const QString &path, QStringList paths) { // 初始化图像缓存目录 - Libutils::image::clearCacheImageFolder(); Libutils::image::initCacheImageFolder(); QFileInfo info(path); @@ -883,12 +882,13 @@ 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; @@ -896,21 +896,19 @@ void LibViewPanel::setWallpaper(const QImage &img) 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)