Debian Codegraph @

qt5/src/poppler-embeddedfile.cc

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