File indexing completed on 2025-01-05 04:02:14

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "thumbnailwriter.h"
0023 
0024 // Local
0025 #include "gwenview_lib_debug.h"
0026 #include "gwenviewconfig.h"
0027 
0028 // Qt
0029 #include <QImage>
0030 #include <QTemporaryFile>
0031 
0032 namespace Gwenview
0033 {
0034 #undef ENABLE_LOG
0035 #undef LOG
0036 // #define ENABLE_LOG
0037 #ifdef ENABLE_LOG
0038 #define LOG(x) // qCDebug(GWENVIEW_LIB_LOG) << x
0039 #else
0040 #define LOG(x) ;
0041 #endif
0042 
0043 static void storeThumbnailToDiskCache(const QString &path, const QImage &image)
0044 {
0045     if (GwenviewConfig::lowResourceUsageMode()) {
0046         return;
0047     }
0048 
0049     LOG(path);
0050     QTemporaryFile tmp(path + QStringLiteral(".gwenview.tmpXXXXXX.png"));
0051     if (!tmp.open()) {
0052         qCWarning(GWENVIEW_LIB_LOG) << "Could not create a temporary file.";
0053         return;
0054     }
0055 
0056     if (!image.save(tmp.fileName(), "png")) {
0057         qCWarning(GWENVIEW_LIB_LOG) << "Could not save thumbnail";
0058         return;
0059     }
0060 
0061     QFile::rename(tmp.fileName(), path);
0062 }
0063 
0064 void ThumbnailWriter::queueThumbnail(const QString &path, const QImage &image)
0065 {
0066     if (GwenviewConfig::lowResourceUsageMode()) {
0067         return;
0068     }
0069 
0070     LOG(path);
0071     QMutexLocker locker(&mMutex);
0072     mCache.insert(path, image);
0073     start();
0074 }
0075 
0076 void ThumbnailWriter::run()
0077 {
0078     QMutexLocker locker(&mMutex);
0079     while (!mCache.isEmpty() && !isInterruptionRequested()) {
0080         Cache::ConstIterator it = mCache.constBegin();
0081         const QString path = it.key();
0082         const QImage image = it.value();
0083 
0084         // This part of the thread is the most time consuming but it does not
0085         // depend on mCache so we can unlock here. This way other thumbnails
0086         // can be added or queried
0087         locker.unlock();
0088         storeThumbnailToDiskCache(path, image);
0089         locker.relock();
0090 
0091         mCache.remove(path);
0092     }
0093 }
0094 
0095 QImage ThumbnailWriter::value(const QString &path) const
0096 {
0097     QMutexLocker locker(&mMutex);
0098     return mCache.value(path);
0099 }
0100 
0101 bool ThumbnailWriter::isEmpty() const
0102 {
0103     QMutexLocker locker(&mMutex);
0104     return mCache.isEmpty();
0105 }
0106 
0107 } // namespace
0108 
0109 #include "moc_thumbnailwriter.cpp"