KFileMetaData

pngextractor.cpp
1 /*
2  SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "pngextractor.h"
8 #include "propertyinfo.h"
9 
10 #include <QImageReader>
11 
12 using namespace KFileMetaData;
13 
14 // Keywords specified in https://www.w3.org/TR/PNG/#11keywords
15 static const struct {
16  QString key;
17  Property::Property property;
18 } s_textMapping[] = {
19  // Short (one line) title or caption for image
20  {QStringLiteral("Title"), Property::Title},
21  // Name of image's creator
22  {QStringLiteral("Author"), Property::Author},
23  // Description of image (possibly long)
24  // Unfortunately, QImage puts all text keys with spaces, such as
25  // "Raw profile type exif", into the "Description" key,
26  // (cf. qt_getImageTextFromDescription), overriding the actual
27  // PNG description, and making it useless.
28  //{QStringLiteral("Description"), Property::Description},
29  // Copyright notice
30  {QStringLiteral("Copyright"), Property::Copyright},
31  // Time of original image creation
32  {QStringLiteral("Creation Time"), Property::CreationDate},
33  // Software used to create the image
34  {QStringLiteral("Software"), Property::Generator},
35  // Disclaimer - Legal disclaimer
36  // Warning - Warning of nature of content
37  // Source - Device used to create the image
38  // Miscellaneous comment
39  {QStringLiteral("Comment"), Property::Comment},
40 };
41 
42 PngExtractor::PngExtractor(QObject* parent)
43  : ExtractorPlugin(parent)
44 {
45 }
46 
47 QStringList PngExtractor::mimetypes() const
48 {
49  return {
50  QStringLiteral("image/png")
51  };
52 }
53 
54 void PngExtractor::extract(ExtractionResult* result)
55 {
56  QImageReader reader(result->inputUrl(), "png");
57  if (!reader.canRead()) {
58  return;
59  }
60 
61  result->addType(Type::Image);
62 
63  if (!result->inputFlags().testFlag(ExtractionResult::ExtractMetaData)) {
64  return;
65  }
66 
67  for (const auto &mapping : s_textMapping) {
68  QString text = reader.text(mapping.key);
69  if (text.isEmpty()) {
70  // Spec says, keywords are case-sensitive but of course the real world looks different.
71  text = reader.text(mapping.key.toLower());
72  }
73  if (text.isEmpty()) {
74  continue;
75  }
76 
77  const auto propertyInfo = PropertyInfo(mapping.property);
78 
79  if (propertyInfo.valueType() == QVariant::DateTime) {
80  // "For the Creation Time keyword, the date format defined in section 5.2.14 of RFC 1123 is suggested"
81  // which in turn references RFC822...
83 
84  if (!dt.isValid()) {
85  continue;
86  }
87 
88  result->add(mapping.property, dt);
89  continue;
90  }
91 
92  result->add(mapping.property, text);
93  }
94 }
95 
96 #include "moc_pngextractor.cpp"
virtual void addType(Type::Type type)=0
This function is called by the plugins.
The ExtractionResult class is where all the data extracted by the indexer is saved....
bool testFlag(Enum flag) const const
QString inputUrl() const
The input url which the plugins will use to locate the file.
Property
The Property enum contains all files property types that KFileMetaData manipulates.
Definition: properties.h:25
Flags inputFlags() const
The flags which the extraction plugin should considering following when extracting metadata from the ...
bool isEmpty() const const
QDateTime fromString(const QString &string, Qt::DateFormat format)
virtual void add(Property::Property property, const QVariant &value)=0
This function is called by the plugins when they wish to add a key value pair which should be indexed...
bool isValid() const const
The ExtractorPlugin is the base class for all file metadata extractors. It is responsible for extract...
RFC2822Date
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 03:48:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.