Debian Codegraph @

qt5/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 QBuffer buffer;
97 unsigned char data[BUFFER_MAX];
98 int bread;
99 buffer.open(QIODevice::WriteOnly);
100 while ((bread = s->doGetChars(BUFFER_MAX, data)) != 0) {
101 buffer.write(reinterpret_cast<const char *>(data), bread);
102 }
103 buffer.close();
104
105 return buffer.data();
106 }
107
108 bool MediaRendition::autoPlay() const
109 {
110 Q_D(const MediaRendition);
111 if (d->rendition->getBEParameters()) {
112 return d->rendition->getBEParameters()->autoPlay;
113 } else if (d->rendition->getMHParameters()) {
114 return d->rendition->getMHParameters()->autoPlay;
115 } else {
116 qDebug("No BE or MH parameters to reference!");
117 }
118 return false;
119 }
120
121 bool MediaRendition::showControls() const
122 {
123 Q_D(const MediaRendition);
124 if (d->rendition->getBEParameters()) {
125 return d->rendition->getBEParameters()->showControls;
126 } else if (d->rendition->getMHParameters()) {
127 return d->rendition->getMHParameters()->showControls;
128 } else {
129 qDebug("No BE or MH parameters to reference!");
130 }
131 return false;
132 }
133
134 float MediaRendition::repeatCount() const
135 {
136 Q_D(const MediaRendition);
137 if (d->rendition->getBEParameters()) {
138 return d->rendition->getBEParameters()->repeatCount;
139 } else if (d->rendition->getMHParameters()) {
140 return d->rendition->getMHParameters()->repeatCount;
141 } else {
142 qDebug("No BE or MH parameters to reference!");
143 }
144 return 1.f;
145 }
146
147 QSize MediaRendition::size() const
148 {
149 Q_D(const MediaRendition);
150 const MediaParameters *mp = nullptr;
151
152 if (d->rendition->getBEParameters()) {
153 mp = d->rendition->getBEParameters();
154 } else if (d->rendition->getMHParameters()) {
155 mp = d->rendition->getMHParameters();
156 } else {
157 qDebug("No BE or MH parameters to reference!");
158 }
159
160 if (mp) {
161 return QSize(mp->windowParams.width, mp->windowParams.height);
162 }
163 return QSize();
164 }
165
166 }