1 | /* poppler-document.cc: qt interface to poppler |
2 | * Copyright (C) 2005, 2008, 2009, 2012, 2013, 2018, 2022, Albert Astals Cid <aacid@kde.org> |
3 | * Copyright (C) 2005, Brad Hards <bradh@frogmouth.net> |
4 | * Copyright (C) 2008, 2011, Pino Toscano <pino@kde.org> |
5 | * Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich |
6 | * Copyright (C) 2023 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk> |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "poppler-qt6.h" |
24 | |
25 | #include <QtCore/QString> |
26 | #include <QtCore/QDateTime> |
27 | |
28 | #include "Object.h" |
29 | #include "Stream.h" |
30 | #include "Catalog.h" |
31 | |
32 | #include "poppler-private.h" |
33 | #include "poppler-embeddedfile-private.h" |
34 | |
35 | namespace Poppler { |
36 | |
37 | EmbeddedFileData::EmbeddedFileData(std::unique_ptr<FileSpec> &&fs) : filespec(std::move(fs)) { } |
38 | |
39 | EmbFile *EmbeddedFileData::embFile() const |
40 | { |
41 | return filespec->isOk() ? filespec->getEmbeddedFile() : nullptr; |
42 | } |
43 | |
44 | EmbeddedFile::EmbeddedFile(EmbFile *embfile) : m_embeddedFile(nullptr) |
45 | { |
46 | assert(!"You must not use this private constructor!" ); |
47 | } |
48 | |
49 | EmbeddedFile::EmbeddedFile(EmbeddedFileData &dd) : m_embeddedFile(&dd) { } |
50 | |
51 | EmbeddedFile::~EmbeddedFile() |
52 | { |
53 | delete m_embeddedFile; |
54 | } |
55 | |
56 | QString EmbeddedFile::name() const |
57 | { |
58 | const GooString *goo = m_embeddedFile->filespec->getFileName(); |
59 | return goo ? UnicodeParsedString(s1: goo) : QString(); |
60 | } |
61 | |
62 | QString EmbeddedFile::description() const |
63 | { |
64 | const GooString *goo = m_embeddedFile->filespec->getDescription(); |
65 | return goo ? UnicodeParsedString(s1: goo) : QString(); |
66 | } |
67 | |
68 | int EmbeddedFile::size() const |
69 | { |
70 | return m_embeddedFile->embFile() ? m_embeddedFile->embFile()->size() : -1; |
71 | } |
72 | |
73 | QDateTime EmbeddedFile::modDate() const |
74 | { |
75 | const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->modDate() : nullptr; |
76 | return goo ? convertDate(dateString: goo->c_str()) : QDateTime(); |
77 | } |
78 | |
79 | QDateTime EmbeddedFile::createDate() const |
80 | { |
81 | const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->createDate() : nullptr; |
82 | return goo ? convertDate(dateString: goo->c_str()) : QDateTime(); |
83 | } |
84 | |
85 | QByteArray EmbeddedFile::checksum() const |
86 | { |
87 | const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->checksum() : nullptr; |
88 | return goo ? QByteArray::fromRawData(data: goo->c_str(), size: goo->getLength()) : QByteArray(); |
89 | } |
90 | |
91 | QString EmbeddedFile::mimeType() const |
92 | { |
93 | const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->mimeType() : nullptr; |
94 | return goo ? QString(goo->c_str()) : QString(); |
95 | } |
96 | |
97 | QByteArray EmbeddedFile::data() |
98 | { |
99 | if (!isValid()) { |
100 | return QByteArray(); |
101 | } |
102 | Stream *stream = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->stream() : nullptr; |
103 | if (!stream) { |
104 | return QByteArray(); |
105 | } |
106 | |
107 | stream->reset(); |
108 | auto data = stream->toUnsignedChars(); |
109 | return QByteArray(reinterpret_cast<const char *>(data.data()), data.size()); |
110 | } |
111 | |
112 | bool EmbeddedFile::isValid() const |
113 | { |
114 | return m_embeddedFile->filespec->isOk(); |
115 | } |
116 | |
117 | } |
118 | |