Changeset View
Changeset View
Standalone View
Standalone View
src/extractors/postscriptdscextractor.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Lesser General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Lesser General Public | ||||
15 | License along with this library; if not, write to the Free Software | ||||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | | ||||
20 | #include "postscriptdscextractor.h" | ||||
21 | #include "kfilemetadata_debug.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QFile> | ||||
25 | | ||||
26 | namespace KFileMetaData | ||||
27 | { | ||||
28 | | ||||
29 | DscExtractor::DscExtractor(QObject* parent) | ||||
30 | : ExtractorPlugin(parent) | ||||
31 | { | ||||
32 | | ||||
33 | } | ||||
34 | | ||||
35 | QStringList DscExtractor::mimetypes() const | ||||
36 | { | ||||
37 | QStringList list; | ||||
38 | list << QStringLiteral("application/postscript") | ||||
39 | << QStringLiteral("image/x-eps"); | ||||
40 | | ||||
41 | return list; | ||||
42 | } | ||||
43 | | ||||
44 | void DscExtractor::extract(ExtractionResult* result) | ||||
45 | { | ||||
46 | auto flags = result->inputFlags(); | ||||
47 | if (!(flags & ExtractionResult::ExtractMetaData)) { | ||||
48 | return; | ||||
49 | } | ||||
50 | | ||||
51 | QFile file(result->inputUrl()); | ||||
52 | if (!file.open(QIODevice::ReadOnly)) { | ||||
53 | qCWarning(KFILEMETADATA_LOG) << "Document is not a valid file"; | ||||
54 | return; | ||||
55 | } | ||||
56 | | ||||
57 | // A little bit heuristic - assume EPS files are images, PS complete documents | ||||
58 | if (result->inputMimetype() == QLatin1String("application/postscript")) { | ||||
59 | result->addType(Type::Document); | ||||
60 | } else { | ||||
61 | result->addType(Type::Image); | ||||
62 | } | ||||
63 | | ||||
64 | // Try to find some DSC (PostScript Language Document Structuring Conventions) conforming data | ||||
65 | QTextStream stream(&file); | ||||
66 | QString line; | ||||
67 | | ||||
68 | while (stream.readLineInto(&line)) { | ||||
69 | if (!line.startsWith(QLatin1String("%%"))) { | ||||
70 | continue; | ||||
71 | } | ||||
72 | | ||||
73 | if (line.startsWith(QLatin1String("%%Pages:"))) { | ||||
74 | bool ok = false; | ||||
75 | int pages = line.midRef(8).toInt(&ok, 10); | ||||
76 | if (ok) { | ||||
77 | result->add(Property::PageCount, pages); | ||||
78 | } | ||||
79 | | ||||
80 | } else if (line.startsWith(QLatin1String("%%Title:"))) { | ||||
81 | QStringRef title = line.midRef(8); | ||||
82 | title = title.trimmed(); | ||||
83 | if (title.startsWith(QLatin1Char('(')) && title.endsWith(QLatin1Char(')'))) { | ||||
84 | title = title.mid(1, title.size() - 2); | ||||
85 | } | ||||
86 | if (!title.isEmpty()) { | ||||
87 | result->add(Property::Title, title.toString()); | ||||
88 | } | ||||
89 | | ||||
90 | } else if (line.startsWith(QLatin1String("%%CreationDate:"))) { | ||||
91 | // "Neither the date nor time need be in any standard format." | ||||
92 | QStringRef date = line.midRef(15); | ||||
93 | date = date.trimmed(); | ||||
94 | if (date.startsWith(QLatin1Char('(')) && date.endsWith(QLatin1Char(')'))) { | ||||
95 | date = date.mid(1, date.size() - 2); | ||||
96 | date = date.trimmed(); | ||||
97 | } | ||||
98 | if (date.startsWith(QLatin1String("D:")) && date.size() >= 23) { | ||||
99 | // Standard PDF date format, ASN.1 like - (D:YYYYMMDDHHmmSSOHH'mm') | ||||
100 | auto dt = QDateTime::fromString(date.mid(2, 14).toString(), QLatin1String("yyyyMMddhhmmss")); | ||||
101 | auto offset = QTime::fromString(date.mid(17, 5).toString(), QLatin1String("hh'\\''mm")); | ||||
102 | if (date.mid(16,1) == QLatin1String("+")) { | ||||
103 | dt.setOffsetFromUtc(QTime(0, 0).secsTo(offset)); | ||||
104 | } else { | ||||
105 | dt.setOffsetFromUtc(-1 * QTime(0, 0).secsTo(offset)); | ||||
106 | } | ||||
107 | result->add(Property::CreationDate, dt); | ||||
108 | } else { | ||||
109 | auto dt = QDateTime::fromString(date.toString()); | ||||
110 | if (dt.isValid()) { | ||||
111 | result->add(Property::CreationDate, dt); | ||||
112 | } | ||||
113 | } | ||||
114 | | ||||
115 | } else if (line.startsWith(QLatin1String("%%EndComments"))) { | ||||
116 | break; | ||||
117 | } | ||||
118 | } | ||||
119 | } | ||||
120 | | ||||
121 | } // namespace KFileMetaData |