File indexing completed on 2025-03-09 03:55:10

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-02-23
0007  * Description : item metadata interface - Exiftool helpers.
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  * SPDX-FileCopyrightText: 2011      by Leif Huhn <leif at dkstat dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "dmetadata.h"
0018 
0019 // Qt includes
0020 
0021 #include <QString>
0022 #include <QFileInfo>
0023 #include <QMimeDatabase>
0024 #include <QScopedPointer>
0025 
0026 // Local includes
0027 
0028 #include "digikam_config.h"
0029 #include "digikam_debug.h"
0030 #include "exiftoolparser.h"
0031 
0032 namespace Digikam
0033 {
0034 
0035 bool DMetadata::loadUsingExifTool(const QString& filePath, bool merge)
0036 {
0037     QMimeDatabase mimeDB;
0038     QFileInfo info(filePath);
0039 
0040     QString mimeType  = mimeDB.mimeTypeForFile(info).name();
0041     bool    copyToAll = (mimeType.startsWith(QLatin1String("video/"))     ||
0042                          (info.suffix().toUpper() == QLatin1String("FITS")));
0043 
0044     QScopedPointer<ExifToolParser> const parser(new ExifToolParser(nullptr));
0045 
0046     if (!parser->exifToolAvailable())
0047     {
0048         qCWarning(DIGIKAM_METAENGINE_LOG) << "ExifTool is not available to load metadata...";
0049 
0050         return false;
0051     }
0052 
0053     if (!parser->loadChunk(filePath, copyToAll))
0054     {
0055         qCCritical(DIGIKAM_METAENGINE_LOG) << "Load metadata using ExifTool failed...";
0056 
0057         return false;
0058     }
0059 
0060     ExifToolParser::ExifToolData chunk = parser->currentData();
0061 
0062     qCDebug(DIGIKAM_METAENGINE_LOG) << "Metadata chunk loaded with ExifTool";
0063 
0064     ExifToolParser::ExifToolData::iterator it = chunk.find(QLatin1String("EXV"));
0065 
0066     if (it == chunk.end())
0067     {
0068         qCWarning(DIGIKAM_METAENGINE_LOG) << "Metadata chunk loaded with ExifTool is empty";
0069 
0070         return false;
0071     }
0072 
0073     QVariantList varLst = it.value();
0074     QByteArray exv      = varLst[0].toByteArray();
0075 
0076     if (exv.isEmpty())
0077     {
0078         qCDebug(DIGIKAM_METAENGINE_LOG) << "Metadata chunk loaded with ExifTool has no data";
0079 
0080         return false;
0081     }
0082 
0083     if (merge)
0084     {
0085         QStringList exclude;
0086 
0087         // Remove Exif.Image.Orientation from the ExifTool container,
0088         // we already got the correct orientation with the FFmpeg backend.
0089 
0090         exclude << QLatin1String("Exif.Image.Orientation");
0091 
0092         loadFromDataAndMerge(exv, exclude);
0093     }
0094     else
0095     {
0096         loadFromData(exv);
0097     }
0098 
0099     // Restore file path.
0100 
0101     setFilePath(filePath);
0102     loadFromSidecarAndMerge(filePath);
0103 
0104     return true;
0105 }
0106 
0107 } // namespace Digikam