File indexing completed on 2025-04-27 03:58:34

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-02-20
0007  * Description : a widget to display Standard Exif metadata
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "exifwidget.h"
0016 
0017 // Qt includes
0018 
0019 #include <QMap>
0020 #include <QFile>
0021 #include <QScopedPointer>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 
0027 // Local includes
0028 
0029 #include "dmetadata.h"
0030 #include "metadatalistview.h"
0031 
0032 namespace
0033 {
0034 
0035 /**
0036  * Standard Exif entry list from the less important to the most important for photograph.
0037  */
0038 static const char* StandardExifEntryList[] =
0039 {
0040     "Iop",
0041     "Thumbnail",
0042     "SubImage1",
0043     "SubImage2",
0044     "Image",
0045     "Photo",
0046     "GPSInfo",
0047     "-1"
0048 };
0049 
0050 } // namespace
0051 
0052 namespace Digikam
0053 {
0054 
0055 ExifWidget::ExifWidget(QWidget* const parent, const QString& name)
0056     : MetadataWidget(parent, name)
0057 {
0058     setup();
0059 
0060     for (int i = 0 ; QLatin1String(StandardExifEntryList[i]) != QLatin1String("-1") ; ++i)
0061     {
0062         m_keysFilter << QLatin1String(StandardExifEntryList[i]);
0063     }
0064 }
0065 
0066 ExifWidget::~ExifWidget()
0067 {
0068 }
0069 
0070 QString ExifWidget::getMetadataTitle() const
0071 {
0072     return i18n("Standard Exif Tags");
0073 }
0074 
0075 bool ExifWidget::loadFromURL(const QUrl& url)
0076 {
0077     setFileName(url.toLocalFile());
0078 
0079     if (url.isEmpty())
0080     {
0081         setMetadata();
0082         return false;
0083     }
0084     else
0085     {
0086         QScopedPointer<DMetadata> metadata(new DMetadata(url.toLocalFile()));
0087 
0088         if (!metadata->hasExif())
0089         {
0090             setMetadata();
0091             return false;
0092         }
0093         else
0094         {
0095             setMetadata(*metadata);
0096         }
0097     }
0098 
0099     return true;
0100 }
0101 
0102 bool ExifWidget::decodeMetadata()
0103 {
0104     QScopedPointer<DMetadata> data(new DMetadata(getMetadata()->data()));
0105 
0106     if (!data->hasExif())
0107     {
0108         return false;
0109     }
0110 
0111     // Update all metadata contents.
0112 
0113     setMetadataMap(data->getExifTagsDataList(QStringList(),
0114                                              false,
0115                                              false)); // Do not extract binary data which can introduce time latency in GUI.
0116 
0117     return true;
0118 }
0119 
0120 void ExifWidget::buildView()
0121 {
0122     switch (getMode())
0123     {
0124         case CUSTOM:
0125         {
0126             setIfdList(getMetadataMap(), m_keysFilter, getTagsFilter());
0127             break;
0128         }
0129 
0130         case PHOTO:
0131         {
0132             setIfdList(getMetadataMap(), m_keysFilter, QStringList() << QLatin1String("FULL"));
0133             break;
0134         }
0135 
0136         default: // NONE
0137         {
0138             setIfdList(getMetadataMap(), m_keysFilter, QStringList());
0139             break;
0140         }
0141     }
0142 
0143     MetadataWidget::buildView();
0144 }
0145 
0146 QString ExifWidget::getTagTitle(const QString& key)
0147 {
0148     QScopedPointer<DMetadata> metadataIface(new DMetadata);
0149     QString title = metadataIface->getExifTagTitle(key.toLatin1().constData());
0150 
0151     if (title.isEmpty())
0152     {
0153         return key.section(QLatin1Char('.'), -1);
0154     }
0155 
0156     return title;
0157 }
0158 
0159 QString ExifWidget::getTagDescription(const QString& key)
0160 {
0161     QScopedPointer<DMetadata> metadataIface(new DMetadata);
0162     QString desc = metadataIface->getExifTagDescription(key.toLatin1().constData());
0163 
0164     if (desc.isEmpty())
0165     {
0166         return i18n("No description available");
0167     }
0168 
0169     return desc;
0170 }
0171 
0172 void ExifWidget::slotSaveMetadataToFile()
0173 {
0174     QUrl url = saveMetadataToFile(i18n("EXIF File to Save"),
0175                                   QString(QLatin1String("*.exif|") + i18n("EXIF binary Files (*.exif)")));
0176 
0177     storeMetadataToFile(url, getMetadata()->getExifEncoded());
0178 }
0179 
0180 } // namespace Digikam
0181 
0182 #include "moc_exifwidget.cpp"