Debian Codegraph @

core/libs/threadimageio/thumb/thumbnailcreator_freedesktop.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2007-07-20
7 * Description : Loader for thumbnails - Freedesktop.org standard implementation
8 *
9 * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
10 * SPDX-FileCopyrightText: 2003-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "thumbnailcreator_p.h"
18
19 namespace Digikam
20 {
21
22 ThumbnailInfo ThumbnailCreator::fileThumbnailInfo(const QString& path)
23 {
24 ThumbnailInfo info;
25
26 if (path.isEmpty())
27 {
28 return info;
29 }
30
31 info.filePath = path;
32 QFileInfo fileInfo(path);
33 info.isAccessible = fileInfo.exists();
34 info.fileName = fileInfo.fileName();
35 QString suffix = fileInfo.suffix().toUpper();
36
37 QMimeDatabase mimeDB;
38 QString mimeType(mimeDB.mimeTypeForFile(path).name());
39
40 if (
41 mimeType.startsWith(QLatin1String("image/")) ||
42 (suffix == QLatin1String("PGF")) ||
43 (suffix == QLatin1String("JXL")) ||
44 (suffix == QLatin1String("AVIF")) ||
45 (suffix == QLatin1String("KRA")) ||
46 (suffix == QLatin1String("CR3")) ||
47 (suffix == QLatin1String("HIF")) ||
48 (suffix == QLatin1String("HEIC")) ||
49 (suffix == QLatin1String("HEIF"))
50 )
51 {
52 info.mimeType = QLatin1String("image");
53 }
54 else if (mimeType.startsWith(QLatin1String("video/")))
55 {
56 info.mimeType = QLatin1String("video");
57 }
58
59 if (!info.isAccessible)
60 {
61 return info;
62 }
63
64 info.modificationDate = fileInfo.lastModified();
65
66 return info;
67 }
68
69 ThumbnailImage ThumbnailCreator::loadFreedesktop(const ThumbnailInfo& info) const
70 {
71 QString path;
72
73 if (!info.customIdentifier.isNull())
74 {
75 path = info.customIdentifier;
76 }
77 else
78 {
79 path = info.filePath;
80 }
81
82 QString uri = thumbnailUri(path);
83 QString thumbPath = thumbnailPath(path);
84 QImage qimage = loadPNG(thumbPath);
85 Q_UNUSED(uri);
86
87 // NOTE: if thumbnail have not been generated by digiKam (konqueror for example),
88 // force to recompute it, else we use it.
89
90 if (!qimage.isNull())
91 {
92 if ((qimage.text(QLatin1String("Thumb::MTime")) == QString::number(info.modificationDate.toSecsSinceEpoch())) &&
93 (qimage.text(QLatin1String("Software")) == d->digiKamFingerPrint))
94 {
95 ThumbnailImage imgInfo;
96 imgInfo.qimage = qimage;
97
98 // is stored rotated. Not needed to rotate.
99
100 imgInfo.exifOrientation = DMetadata::ORIENTATION_NORMAL;
101
102 return imgInfo;
103 }
104 }
105
106 return ThumbnailImage();
107 }
108
109 void ThumbnailCreator::storeFreedesktop(const ThumbnailInfo& info, const ThumbnailImage& image) const
110 {
111 QImage qimage = image.qimage;
112
113 QString path;
114
115 if (!info.customIdentifier.isNull())
116 {
117 path = info.customIdentifier;
118 }
119 else
120 {
121 path = info.filePath;
122 }
123
124 QString uri = thumbnailUri(path);
125 QString thumbPath = thumbnailPath(path);
126
127 // required by spec
128
129 if (qimage.format() != QImage::Format_ARGB32)
130 {
131 qimage = qimage.convertToFormat(QImage::Format_ARGB32);
132 }
133
134 qimage.setText(QLatin1String("Thumb::URI"), uri);
135 qimage.setText(QLatin1String("Thumb::MTime"), QString::number(info.modificationDate.toSecsSinceEpoch()));
136 qimage.setText(QLatin1String("Software"), d->digiKamFingerPrint);
137
138 QTemporaryFile temp;
139 temp.setFileTemplate(thumbPath + QLatin1String("-digikam-") + QLatin1String("XXXXXX") + QLatin1String(".png"));
140 temp.setAutoRemove(false);
141
142 if (temp.open())
143 {
144 QString tempFileName = temp.fileName();
145
146 if (qimage.save(tempFileName, "PNG", 0))
147 {
148 Q_ASSERT(!tempFileName.isEmpty());
149
150 temp.close();
151
152 // remove thumbPath file if it exist
153
154 if ((tempFileName != thumbPath) && QFile::exists(tempFileName) && QFile::exists(thumbPath))
155 {
156 QFile::remove(thumbPath);
157 }
158
159 if (!QFile::rename(tempFileName, thumbPath))
160 {
161 qCDebug(DIGIKAM_GENERAL_LOG) << "Cannot rename thumb file (" << tempFileName << ")";
162 qCDebug(DIGIKAM_GENERAL_LOG) << "to (" << thumbPath << ")...";
163 }
164 }
165 }
166 }
167
168 void ThumbnailCreator::deleteFromDiskFreedesktop(const QString& filePath) const
169 {
170 QFile smallThumb(thumbnailPath(filePath, normalThumbnailDir()));
171 QFile largeThumb(thumbnailPath(filePath, largeThumbnailDir()));
172
173 smallThumb.remove();
174 largeThumb.remove();
175 }
176
177 } // namespace Digikam