KFileMetaData

postscriptdscextractor.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7
8#include "postscriptdscextractor.h"
9#include "kfilemetadata_debug.h"
10
11#include <QFile>
12#include <QTimeZone>
13
14namespace KFileMetaData
15{
16
17DscExtractor::DscExtractor(QObject* parent)
18 : ExtractorPlugin(parent)
19{
20
21}
22
23QStringList DscExtractor::mimetypes() const
24{
26 list << QStringLiteral("application/postscript")
27 << QStringLiteral("image/x-eps");
28
29 return list;
30}
31
32void DscExtractor::extract(ExtractionResult* result)
33{
34 QFile file(result->inputUrl());
35 if (!file.open(QIODevice::ReadOnly)) {
36 qCWarning(KFILEMETADATA_LOG) << "Document is not a valid file";
37 return;
38 }
39
40 // A little bit heuristic - assume EPS files are images, PS complete documents
41 if (result->inputMimetype() == QLatin1String("application/postscript")) {
42 result->addType(Type::Document);
43 } else {
44 result->addType(Type::Image);
45 }
46
47 if (!(result->inputFlags() & ExtractionResult::ExtractMetaData)) {
48 return;
49 }
50 // Try to find some DSC (PostScript Language Document Structuring Conventions) conforming data
51 QTextStream stream(&file);
52 QString line;
53
54 while (stream.readLineInto(&line)) {
55 if (!line.startsWith(QLatin1String("%%"))) {
56 continue;
57 }
58
59 if (const auto tag = QLatin1String("%%Pages:"); line.startsWith(tag)) {
60 bool ok = false;
61 int pages = QStringView(line).mid(tag.size()).toInt(&ok, 10);
62 if (ok) {
63 result->add(Property::PageCount, pages);
64 }
65
66 } else if (const auto tag = QLatin1String("%%Title:"); line.startsWith(tag)) {
67 QStringView title = QStringView(line).mid(tag.size()).trimmed();
68 if (title.startsWith(QLatin1Char('(')) && title.endsWith(QLatin1Char(')'))) {
69 title = title.mid(1, title.size() - 2);
70 }
71 if (!title.isEmpty()) {
72 result->add(Property::Title, title.toString());
73 }
74
75 } else if (const auto tag = QLatin1String("%%CreationDate:"); line.startsWith(tag)) {
76 // "Neither the date nor time need be in any standard format."
77 QStringView date = QStringView(line).mid(tag.size()).trimmed();
78 if (date.startsWith(QLatin1Char('(')) && date.endsWith(QLatin1Char(')'))) {
79 date = date.mid(1, date.size() - 2);
80 date = date.trimmed();
81 }
82 if (date.startsWith(QLatin1String("D:")) && date.size() >= 23) {
83 // Standard PDF date format, ASN.1 like - (D:YYYYMMDDHHmmSSOHH'mm')
84 auto dt = QDateTime::fromString(date.mid(2, 14).toString(), QLatin1String("yyyyMMddhhmmss"));
85 auto offset = QTime::fromString(date.mid(17, 5).toString(), QLatin1String("hh'\\''mm"));
86 if (date.at(16) == QLatin1Char('+')) {
87 dt.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(QTime(0, 0).secsTo(offset)));
88 } else {
89 dt.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(-1 * QTime(0, 0).secsTo(offset)));
90 }
91 result->add(Property::CreationDate, dt);
92 } else {
93 auto dt = QDateTime::fromString(date.toString());
94 if (dt.isValid()) {
95 result->add(Property::CreationDate, dt);
96 }
97 }
98
99 } else if (line.startsWith(QLatin1String("%%EndComments"))) {
100 break;
101 }
102 }
103}
104
105} // namespace KFileMetaData
106
107#include "moc_postscriptdscextractor.cpp"
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QDateTime fromString(const QString &string, Qt::DateFormat format)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QChar at(qsizetype n) const const
bool endsWith(QStringView str, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QStringView mid(qsizetype start) const const
qsizetype size() const const
bool startsWith(QStringView str, Qt::CaseSensitivity cs) const const
QString toString() const const
QStringView trimmed() const const
QTime fromString(const QString &string, Qt::DateFormat format)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:44:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.