File indexing completed on 2024-05-12 15:37:06

0001 /*
0002     SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "pngextractor.h"
0008 #include "propertyinfo.h"
0009 
0010 #include <QImageReader>
0011 
0012 using namespace KFileMetaData;
0013 
0014 // Keywords specified in https://www.w3.org/TR/PNG/#11keywords
0015 static const struct {
0016     QString key;
0017     Property::Property property;
0018 } s_textMapping[] = {
0019     // Short (one line) title or caption for image
0020     {QStringLiteral("Title"), Property::Title},
0021     // Name of image's creator
0022     {QStringLiteral("Author"), Property::Author},
0023     // Description of image (possibly long)
0024     // Unfortunately, QImage puts all text keys with spaces, such as
0025     // "Raw profile type exif", into the "Description" key,
0026     // (cf. qt_getImageTextFromDescription), overriding the actual
0027     // PNG description, and making it useless.
0028     //{QStringLiteral("Description"), Property::Description},
0029     // Copyright notice
0030     {QStringLiteral("Copyright"), Property::Copyright},
0031     // Time of original image creation
0032     {QStringLiteral("Creation Time"), Property::CreationDate},
0033     // Software used to create the image
0034     {QStringLiteral("Software"), Property::Generator},
0035     // Disclaimer - Legal disclaimer
0036     // Warning - Warning of nature of content
0037     // Source - Device used to create the image
0038     // Miscellaneous comment
0039     {QStringLiteral("Comment"), Property::Comment},
0040 };
0041 
0042 PngExtractor::PngExtractor(QObject* parent)
0043     : ExtractorPlugin(parent)
0044 {
0045 }
0046 
0047 QStringList PngExtractor::mimetypes() const
0048 {
0049     return {
0050         QStringLiteral("image/png")
0051     };
0052 }
0053 
0054 void PngExtractor::extract(ExtractionResult* result)
0055 {
0056     QImageReader reader(result->inputUrl(), "png");
0057     if (!reader.canRead()) {
0058         return;
0059     }
0060 
0061     result->addType(Type::Image);
0062 
0063     if (!result->inputFlags().testFlag(ExtractionResult::ExtractMetaData)) {
0064         return;
0065     }
0066 
0067     for (const auto &mapping : s_textMapping) {
0068         QString text = reader.text(mapping.key);
0069         if (text.isEmpty()) {
0070             // Spec says, keywords are case-sensitive but of course the real world looks different.
0071             text = reader.text(mapping.key.toLower());
0072         }
0073         if (text.isEmpty()) {
0074             continue;
0075         }
0076 
0077         const auto propertyInfo = PropertyInfo(mapping.property);
0078 
0079         if (propertyInfo.valueType() == QVariant::DateTime) {
0080             // "For the Creation Time keyword, the date format defined in section 5.2.14 of RFC 1123 is suggested"
0081             // which in turn references RFC822...
0082             const QDateTime dt = QDateTime::fromString(text, Qt::RFC2822Date);
0083 
0084             if (!dt.isValid()) {
0085                 continue;
0086             }
0087 
0088             result->add(mapping.property, dt);
0089             continue;
0090         }
0091 
0092         result->add(mapping.property, text);
0093     }
0094 }
0095 
0096 #include "moc_pngextractor.cpp"