1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2006-02-20
7  * Description : a widget to display Standard Exif metadata
8  *
9  * Copyright (C) 2006-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "exifwidget.h"
25 
26 // Qt includes
27 
28 #include <QMap>
29 #include <QFile>
30 #include <QScopedPointer>
31 
32 // KDE includes
33 
34 #include <klocalizedstring.h>
35 
36 // Local includes
37 
38 #include "dmetadata.h"
39 #include "metadatalistview.h"
40 
41 namespace
42 {
43 
44 /**
45  * Standard Exif entry list from the less important to the most important for photograph.
46  */
47 static const char* StandardExifEntryList[] =
48 {
49     "Iop",
50     "Thumbnail",
51     "SubImage1",
52     "SubImage2",
53     "Image",
54     "Photo",
55     "GPSInfo",
56     "-1"
57 };
58 
59 } // namepsace
60 
61 namespace Digikam
62 {
63 
ExifWidget(QWidget * const parent,const QString & name)64 ExifWidget::ExifWidget(QWidget* const parent, const QString& name)
65     : MetadataWidget(parent, name)
66 {
67     setup();
68 
69     for (int i = 0 ; QLatin1String(StandardExifEntryList[i]) != QLatin1String("-1") ; ++i)
70     {
71         m_keysFilter << QLatin1String(StandardExifEntryList[i]);
72     }
73 }
74 
~ExifWidget()75 ExifWidget::~ExifWidget()
76 {
77 }
78 
getMetadataTitle() const79 QString ExifWidget::getMetadataTitle() const
80 {
81     return i18n("Standard Exif Tags");
82 }
83 
loadFromURL(const QUrl & url)84 bool ExifWidget::loadFromURL(const QUrl& url)
85 {
86     setFileName(url.toLocalFile());
87 
88     if (url.isEmpty())
89     {
90         setMetadata();
91         return false;
92     }
93     else
94     {
95         QScopedPointer<DMetadata> metadata(new DMetadata(url.toLocalFile()));
96 
97         if (!metadata->hasExif())
98         {
99             setMetadata();
100             return false;
101         }
102         else
103         {
104             setMetadata(*metadata);
105         }
106     }
107 
108     return true;
109 }
110 
decodeMetadata()111 bool ExifWidget::decodeMetadata()
112 {
113     QScopedPointer<DMetadata> data(new DMetadata(getMetadata()->data()));
114 
115     if (!data->hasExif())
116     {
117         return false;
118     }
119 
120     // Update all metadata contents.
121 
122     setMetadataMap(data->getExifTagsDataList(QStringList(),
123                                              false,
124                                              false)); // Do not extract binary data which can introduce time latency in GUI.
125 
126     return true;
127 }
128 
buildView()129 void ExifWidget::buildView()
130 {
131     switch (getMode())
132     {
133         case CUSTOM:
134         {
135             setIfdList(getMetadataMap(), m_keysFilter, getTagsFilter());
136             break;
137         }
138 
139         case PHOTO:
140         {
141             setIfdList(getMetadataMap(), m_keysFilter, QStringList() << QLatin1String("FULL"));
142             break;
143         }
144 
145         default: // NONE
146         {
147             setIfdList(getMetadataMap(), m_keysFilter, QStringList());
148             break;
149         }
150     }
151 
152     MetadataWidget::buildView();
153 }
154 
getTagTitle(const QString & key)155 QString ExifWidget::getTagTitle(const QString& key)
156 {
157     QScopedPointer<DMetadata> metadataIface(new DMetadata);
158     QString title = metadataIface->getExifTagTitle(key.toLatin1().constData());
159 
160     if (title.isEmpty())
161     {
162         return key.section(QLatin1Char('.'), -1);
163     }
164 
165     return title;
166 }
167 
getTagDescription(const QString & key)168 QString ExifWidget::getTagDescription(const QString& key)
169 {
170     QScopedPointer<DMetadata> metadataIface(new DMetadata);
171     QString desc = metadataIface->getExifTagDescription(key.toLatin1().constData());
172 
173     if (desc.isEmpty())
174     {
175         return i18n("No description available");
176     }
177 
178     return desc;
179 }
180 
slotSaveMetadataToFile()181 void ExifWidget::slotSaveMetadataToFile()
182 {
183     QUrl url = saveMetadataToFile(i18n("EXIF File to Save"),
184                                   QString(QLatin1String("*.exif|") + i18n("EXIF binary Files (*.exif)")));
185 
186     storeMetadataToFile(url, getMetadata()->getExifEncoded());
187 }
188 
189 } // namespace Digikam
190