summaryrefslogtreecommitdiff
path: root/qt5/src/poppler-embeddedfile.cc
blob: 9fe62c77f34537461662046132e3ff0233019e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* poppler-document.cc: qt interface to poppler
 * Copyright (C) 2005, 2008, 2009, 2012, 2013, 2018, 2022, Albert Astals Cid <aacid@kde.org>
 * Copyright (C) 2005, Brad Hards <bradh@frogmouth.net>
 * Copyright (C) 2008, 2011, Pino Toscano <pino@kde.org>
 * 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
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "poppler-qt5.h"

#include <QtCore/QString>
#include <QtCore/QDateTime>

#include "Object.h"
#include "Stream.h"
#include "Catalog.h"

#include "poppler-private.h"
#include "poppler-embeddedfile-private.h"

namespace Poppler {

EmbeddedFileData::EmbeddedFileData(std::unique_ptr<FileSpec> &&fs) : filespec(std::move(fs)) { }

EmbFile *EmbeddedFileData::embFile() const
{
    return filespec->isOk() ? filespec->getEmbeddedFile() : nullptr;
}

EmbeddedFile::EmbeddedFile(EmbFile *embfile) : m_embeddedFile(nullptr)
{
    assert(!"You must not use this private constructor!");
}

EmbeddedFile::EmbeddedFile(EmbeddedFileData &dd) : m_embeddedFile(&dd) { }

EmbeddedFile::~EmbeddedFile()
{
    delete m_embeddedFile;
}

QString EmbeddedFile::name() const
{
    const GooString *goo = m_embeddedFile->filespec->getFileName();
    return goo ? UnicodeParsedString(goo) : QString();
}

QString EmbeddedFile::description() const
{
    const GooString *goo = m_embeddedFile->filespec->getDescription();
    return goo ? UnicodeParsedString(goo) : QString();
}

int EmbeddedFile::size() const
{
    return m_embeddedFile->embFile() ? m_embeddedFile->embFile()->size() : -1;
}

QDateTime EmbeddedFile::modDate() const
{
    const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->modDate() : nullptr;
    return goo ? convertDate(goo->c_str()) : QDateTime();
}

QDateTime EmbeddedFile::createDate() const
{
    const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->createDate() : nullptr;
    return goo ? convertDate(goo->c_str()) : QDateTime();
}

QByteArray EmbeddedFile::checksum() const
{
    const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->checksum() : nullptr;
    return goo ? QByteArray::fromRawData(goo->c_str(), goo->getLength()) : QByteArray();
}

QString EmbeddedFile::mimeType() const
{
    const GooString *goo = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->mimeType() : nullptr;
    return goo ? QString(goo->c_str()) : QString();
}

QByteArray EmbeddedFile::data()
{
    if (!isValid()) {
        return QByteArray();
    }
    Stream *stream = m_embeddedFile->embFile() ? m_embeddedFile->embFile()->stream() : nullptr;
    if (!stream) {
        return QByteArray();
    }

    stream->reset();
    auto data = stream->toUnsignedChars();
    return QByteArray(reinterpret_cast<const char *>(data.data()), data.size());
}

bool EmbeddedFile::isValid() const
{
    return m_embeddedFile->filespec->isOk();
}

}