Coverage Report

Created: 2026-10-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poppler/qt6/src/poppler-media.cc
Line
Count
Source
1
/* poppler-media.cc: qt interface to poppler
2
 * Copyright (C) 2012 Guillermo A. Amaral B. <gamaral@kde.org>
3
 * Copyright (C) 2013, 2018, 2021, 2024, 2026 Albert Astals Cid <aacid@kde.org>
4
 * Copyright (C) 2025, 2026 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
5
 * Copyright (C) 2025 Arnav V <arnav0872@gmail.com>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2, or (at your option)
10
 * any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20
 */
21
22
#include "poppler-media.h"
23
24
#include "Rendition.h"
25
26
#include "poppler-private.h"
27
28
#include <QtCore/QBuffer>
29
30
constexpr int BUFFER_MAX = 4096;
31
32
namespace Poppler {
33
34
class MediaRenditionPrivate
35
{
36
public:
37
0
    explicit MediaRenditionPrivate(std::unique_ptr<::MediaRendition> &&renditionA) : rendition(std::move(renditionA)) { }
38
39
0
    ~MediaRenditionPrivate() = default;
40
41
    MediaRenditionPrivate(const MediaRenditionPrivate &) = delete;
42
    MediaRenditionPrivate &operator=(const MediaRenditionPrivate &) = delete;
43
44
    std::unique_ptr<::MediaRendition> rendition;
45
};
46
47
0
MediaRendition::MediaRendition(::MediaRendition *rendition) : MediaRendition(std::unique_ptr<::MediaRendition>(rendition)) { }
48
49
0
MediaRendition::MediaRendition(std::unique_ptr<::MediaRendition> &&rendition) : d_ptr(new MediaRenditionPrivate(std::move(rendition))) { }
50
51
MediaRendition::~MediaRendition()
52
0
{
53
0
    delete d_ptr;
54
0
}
55
56
bool MediaRendition::isValid() const
57
0
{
58
0
    Q_D(const MediaRendition);
59
0
    return d->rendition && d->rendition->isOk();
60
0
}
61
62
QString MediaRendition::contentType() const
63
0
{
64
0
    Q_ASSERT(isValid() && "Invalid media rendition.");
65
0
    Q_D(const MediaRendition);
66
0
    return UnicodeParsedString(d->rendition->getContentType());
67
0
}
68
69
QString MediaRendition::fileName() const
70
0
{
71
0
    Q_ASSERT(isValid() && "Invalid media rendition.");
72
0
    Q_D(const MediaRendition);
73
0
    return UnicodeParsedString(d->rendition->getFileName());
74
0
}
75
76
bool MediaRendition::isEmbedded() const
77
0
{
78
0
    Q_ASSERT(isValid() && "Invalid media rendition.");
79
0
    Q_D(const MediaRendition);
80
0
    return d->rendition->getIsEmbedded();
81
0
}
82
83
QByteArray MediaRendition::data() const
84
0
{
85
0
    Q_ASSERT(isValid() && "Invalid media rendition.");
86
0
    Q_D(const MediaRendition);
87
88
0
    Stream *s = d->rendition->getEmbbededStream();
89
0
    if (!s) {
90
0
        return QByteArray();
91
0
    }
92
93
0
    if (!s->rewind()) {
94
0
        return QByteArray {};
95
0
    }
96
97
0
    QBuffer buffer;
98
0
    unsigned char data[BUFFER_MAX];
99
0
    int bread;
100
101
0
    buffer.open(QIODevice::WriteOnly);
102
0
    while ((bread = s->doGetChars(BUFFER_MAX, data)) != 0) {
103
0
        buffer.write(reinterpret_cast<const char *>(data), bread);
104
0
    }
105
0
    buffer.close();
106
107
0
    return buffer.data();
108
0
}
109
110
bool MediaRendition::autoPlay() const
111
0
{
112
0
    Q_D(const MediaRendition);
113
0
    if (d->rendition->getBEParameters()) {
114
0
        return d->rendition->getBEParameters()->autoPlay;
115
0
    }
116
0
    if (d->rendition->getMHParameters()) {
117
0
        return d->rendition->getMHParameters()->autoPlay;
118
0
    }
119
0
    qDebug("No BE or MH parameters to reference!");
120
121
0
    return false;
122
0
}
123
124
bool MediaRendition::showControls() const
125
0
{
126
0
    Q_D(const MediaRendition);
127
0
    if (d->rendition->getBEParameters()) {
128
0
        return d->rendition->getBEParameters()->showControls;
129
0
    }
130
0
    if (d->rendition->getMHParameters()) {
131
0
        return d->rendition->getMHParameters()->showControls;
132
0
    }
133
0
    qDebug("No BE or MH parameters to reference!");
134
135
0
    return false;
136
0
}
137
138
float MediaRendition::repeatCount() const
139
0
{
140
0
    Q_D(const MediaRendition);
141
0
    if (d->rendition->getBEParameters()) {
142
0
        return d->rendition->getBEParameters()->repeatCount;
143
0
    }
144
0
    if (d->rendition->getMHParameters()) {
145
0
        return d->rendition->getMHParameters()->repeatCount;
146
0
    }
147
0
    qDebug("No BE or MH parameters to reference!");
148
149
0
    return 1.F;
150
0
}
151
152
QSize MediaRendition::size() const
153
0
{
154
0
    Q_D(const MediaRendition);
155
0
    std::optional<MediaParameters> mp;
156
157
0
    if (d->rendition->getBEParameters()) {
158
0
        mp = d->rendition->getBEParameters();
159
0
    } else if (d->rendition->getMHParameters()) {
160
0
        mp = d->rendition->getMHParameters();
161
0
    } else {
162
0
        qDebug("No BE or MH parameters to reference!");
163
0
    }
164
165
0
    if (mp) {
166
0
        return QSize(mp->windowParams.width, mp->windowParams.height);
167
0
    }
168
0
    return QSize();
169
0
}
170
171
}