Debian Codegraph @

qt6/src/poppler-media.cc

raw
1 /* poppler-media.cc: qt interface to poppler
2 * Copyright (C) 2012 Guillermo A. Amaral B. <[email protected]>
3 * Copyright (C) 2013, 2018, 2021, 2024 Albert Astals Cid <[email protected]>
4 * Copyright (C) 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
5 * Copyright (C) 2025 Arnav V <[email protected]>
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 #define BUFFER_MAX 4096
31
32 namespace Poppler {
33
34 class MediaRenditionPrivate
35 {
36 public:
37 explicit MediaRenditionPrivate(std::unique_ptr<::MediaRendition> &&renditionA) : rendition(std::move(renditionA)) { }
38
39 ~MediaRenditionPrivate() = default;
40
41 MediaRenditionPrivate(const MediaRenditionPrivate &) = delete;
42 MediaRenditionPrivate &operator=(const MediaRenditionPrivate &) = delete;
43
44 std::unique_ptr<::MediaRendition> rendition;
45 };
46
47 MediaRendition::MediaRendition(::MediaRendition *rendition) : MediaRendition(std::unique_ptr<::MediaRendition>(rendition)) { }
48
49 MediaRendition::MediaRendition(std::unique_ptr<::MediaRendition> &&rendition) : d_ptr(new MediaRenditionPrivate(std::move(rendition))) { }
50
51 MediaRendition::~MediaRendition()
52 {
53 delete d_ptr;
54 }
55
56 bool MediaRendition::isValid() const
57 {
58 Q_D(const MediaRendition);
59 return d->rendition && d->rendition->isOk();
60 }
61
62 QString MediaRendition::contentType() const
63 {
64 Q_ASSERT(isValid() && "Invalid media rendition.");
65 Q_D(const MediaRendition);
66 return UnicodeParsedString(d->rendition->getContentType());
67 }
68
69 QString MediaRendition::fileName() const
70 {
71 Q_ASSERT(isValid() && "Invalid media rendition.");
72 Q_D(const MediaRendition);
73 return UnicodeParsedString(d->rendition->getFileName());
74 }
75
76 bool MediaRendition::isEmbedded() const
77 {
78 Q_ASSERT(isValid() && "Invalid media rendition.");
79 Q_D(const MediaRendition);
80 return d->rendition->getIsEmbedded();
81 }
82
83 QByteArray MediaRendition::data() const
84 {
85 Q_ASSERT(isValid() && "Invalid media rendition.");
86 Q_D(const MediaRendition);
87
88 Stream *s = d->rendition->getEmbbededStream();
89 if (!s) {
90 return QByteArray();
91 }
92
93 if (!s->rewind()) {
94 return QByteArray {};
95 }
96
97 QBuffer buffer;
98 unsigned char data[BUFFER_MAX];
99 int bread;
100
101 buffer.open(QIODevice::WriteOnly);
102 while ((bread = s->doGetChars(BUFFER_MAX, data)) != 0) {
103 buffer.write(reinterpret_cast<const char *>(data), bread);
104 }
105 buffer.close();
106
107 return buffer.data();
108 }
109
110 bool MediaRendition::autoPlay() const
111 {
112 Q_D(const MediaRendition);
113 if (d->rendition->getBEParameters()) {
114 return d->rendition->getBEParameters()->autoPlay;
115 } else if (d->rendition->getMHParameters()) {
116 return d->rendition->getMHParameters()->autoPlay;
117 } else {
118 qDebug("No BE or MH parameters to reference!");
119 }
120 return false;
121 }
122
123 bool MediaRendition::showControls() const
124 {
125 Q_D(const MediaRendition);
126 if (d->rendition->getBEParameters()) {
127 return d->rendition->getBEParameters()->showControls;
128 } else if (d->rendition->getMHParameters()) {
129 return d->rendition->getMHParameters()->showControls;
130 } else {
131 qDebug("No BE or MH parameters to reference!");
132 }
133 return false;
134 }
135
136 float MediaRendition::repeatCount() const
137 {
138 Q_D(const MediaRendition);
139 if (d->rendition->getBEParameters()) {
140 return d->rendition->getBEParameters()->repeatCount;
141 } else if (d->rendition->getMHParameters()) {
142 return d->rendition->getMHParameters()->repeatCount;
143 } else {
144 qDebug("No BE or MH parameters to reference!");
145 }
146 return 1.f;
147 }
148
149 QSize MediaRendition::size() const
150 {
151 Q_D(const MediaRendition);
152 const MediaParameters *mp = nullptr;
153
154 if (d->rendition->getBEParameters()) {
155 mp = d->rendition->getBEParameters();
156 } else if (d->rendition->getMHParameters()) {
157 mp = d->rendition->getMHParameters();
158 } else {
159 qDebug("No BE or MH parameters to reference!");
160 }
161
162 if (mp) {
163 return QSize(mp->windowParams.width, mp->windowParams.height);
164 }
165 return QSize();
166 }
167
168 }