File indexing completed on 2024-09-22 05:02:10

0001 /*
0002     SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org>
0003     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "mediametadatafinder.h"
0009 
0010 #include <QFile>
0011 #include <QImageReader>
0012 
0013 #include "config-KExiv2.h"
0014 #if HAVE_KExiv2
0015 #include <KExiv2/KExiv2>
0016 #endif
0017 
0018 MediaMetadataFinder::MediaMetadataFinder(const QString &path, QObject *parent)
0019     : QObject(parent)
0020     , m_path(path)
0021 {
0022 }
0023 
0024 void MediaMetadataFinder::run()
0025 {
0026     MediaMetadata metadata;
0027 
0028     const QImageReader reader(m_path);
0029     metadata.resolution = reader.size();
0030 
0031 #if HAVE_KExiv2
0032     KExiv2Iface::KExiv2 exivImage(m_path);
0033 
0034     // Extract title from XPTitle
0035     {
0036         const QByteArray titleByte = exivImage.getExifTagData("Exif.Image.XPTitle");
0037         metadata.title = QString::fromUtf8(titleByte).chopped(std::min<qsizetype>(titleByte.size(), 1));
0038     }
0039 
0040     // Use documentName as title
0041     if (metadata.title.isEmpty()) {
0042         const QByteArray titleByte = exivImage.getExifTagData("Exif.Image.DocumentName");
0043         metadata.title = QString::fromUtf8(titleByte).chopped(std::min<qsizetype>(titleByte.size(), 1));
0044     }
0045 
0046     // Use description as title
0047     if (metadata.title.isEmpty()) {
0048         const QByteArray titleByte = exivImage.getExifTagData("Exif.Image.ImageDescription");
0049         metadata.title = QString::fromUtf8(titleByte).chopped(std::min<qsizetype>(titleByte.size(), 1));
0050     }
0051 
0052     // Extract author from artist
0053     {
0054         const QByteArray authorByte = exivImage.getExifTagData("Exif.Image.Artist");
0055         metadata.author = QString::fromUtf8(authorByte).chopped(std::min<qsizetype>(authorByte.size(), 1));
0056     }
0057 
0058     // Extract author from XPAuthor
0059     if (metadata.author.isEmpty()) {
0060         const QByteArray authorByte = exivImage.getExifTagData("Exif.Image.XPAuthor");
0061         metadata.author = QString::fromUtf8(authorByte).chopped(std::min<qsizetype>(authorByte.size(), 1));
0062     }
0063 
0064     // Extract author from copyright
0065     if (metadata.author.isEmpty()) {
0066         const QByteArray authorByte = exivImage.getExifTagData("Exif.Image.Copyright");
0067         metadata.author = QString::fromUtf8(authorByte).chopped(std::min<qsizetype>(authorByte.size(), 1));
0068     }
0069 #endif
0070 
0071     Q_EMIT metadataFound(m_path, metadata);
0072 }