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 Albert Astals Cid <aacid@kde.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "poppler-media.h"
21 
22 #include "Rendition.h"
23 
24 #include "poppler-private.h"
25 
26 #include <QtCore/QBuffer>
27 
28 #define BUFFER_MAX 4096
29 
30 namespace Poppler {
31 
32 class MediaRenditionPrivate
33 {
34 public:
MediaRenditionPrivate(::MediaRendition * renditionA)35     explicit MediaRenditionPrivate(::MediaRendition *renditionA) : rendition(renditionA) { }
36 
~MediaRenditionPrivate()37     ~MediaRenditionPrivate() { delete rendition; }
38 
39     MediaRenditionPrivate(const MediaRenditionPrivate &) = delete;
40     MediaRenditionPrivate &operator=(const MediaRenditionPrivate &) = delete;
41 
42     ::MediaRendition *rendition;
43 };
44 
MediaRendition(::MediaRendition * rendition)45 MediaRendition::MediaRendition(::MediaRendition *rendition) : d_ptr(new MediaRenditionPrivate(rendition)) { }
46 
~MediaRendition()47 MediaRendition::~MediaRendition()
48 {
49     delete d_ptr;
50 }
51 
isValid() const52 bool MediaRendition::isValid() const
53 {
54     Q_D(const MediaRendition);
55     return d->rendition && d->rendition->isOk();
56 }
57 
contentType() const58 QString MediaRendition::contentType() const
59 {
60     Q_ASSERT(isValid() && "Invalid media rendition.");
61     Q_D(const MediaRendition);
62     return UnicodeParsedString(d->rendition->getContentType());
63 }
64 
fileName() const65 QString MediaRendition::fileName() const
66 {
67     Q_ASSERT(isValid() && "Invalid media rendition.");
68     Q_D(const MediaRendition);
69     return UnicodeParsedString(d->rendition->getFileName());
70 }
71 
isEmbedded() const72 bool MediaRendition::isEmbedded() const
73 {
74     Q_ASSERT(isValid() && "Invalid media rendition.");
75     Q_D(const MediaRendition);
76     return d->rendition->getIsEmbedded();
77 }
78 
data() const79 QByteArray MediaRendition::data() const
80 {
81     Q_ASSERT(isValid() && "Invalid media rendition.");
82     Q_D(const MediaRendition);
83 
84     Stream *s = d->rendition->getEmbbededStream();
85     if (!s)
86         return QByteArray();
87 
88     QBuffer buffer;
89     unsigned char data[BUFFER_MAX];
90     int bread;
91 
92     buffer.open(QIODevice::WriteOnly);
93     s->reset();
94     while ((bread = s->doGetChars(BUFFER_MAX, data)) != 0)
95         buffer.write(reinterpret_cast<const char *>(data), bread);
96     buffer.close();
97 
98     return buffer.data();
99 }
100 
autoPlay() const101 bool MediaRendition::autoPlay() const
102 {
103     Q_D(const MediaRendition);
104     if (d->rendition->getBEParameters()) {
105         return d->rendition->getBEParameters()->autoPlay;
106     } else if (d->rendition->getMHParameters()) {
107         return d->rendition->getMHParameters()->autoPlay;
108     } else
109         qDebug("No BE or MH parameters to reference!");
110     return false;
111 }
112 
showControls() const113 bool MediaRendition::showControls() const
114 {
115     Q_D(const MediaRendition);
116     if (d->rendition->getBEParameters()) {
117         return d->rendition->getBEParameters()->showControls;
118     } else if (d->rendition->getMHParameters()) {
119         return d->rendition->getMHParameters()->showControls;
120     } else
121         qDebug("No BE or MH parameters to reference!");
122     return false;
123 }
124 
repeatCount() const125 float MediaRendition::repeatCount() const
126 {
127     Q_D(const MediaRendition);
128     if (d->rendition->getBEParameters()) {
129         return d->rendition->getBEParameters()->repeatCount;
130     } else if (d->rendition->getMHParameters()) {
131         return d->rendition->getMHParameters()->repeatCount;
132     } else
133         qDebug("No BE or MH parameters to reference!");
134     return 1.f;
135 }
136 
size() const137 QSize MediaRendition::size() const
138 {
139     Q_D(const MediaRendition);
140     const MediaParameters *mp = nullptr;
141 
142     if (d->rendition->getBEParameters())
143         mp = d->rendition->getBEParameters();
144     else if (d->rendition->getMHParameters())
145         mp = d->rendition->getMHParameters();
146     else
147         qDebug("No BE or MH parameters to reference!");
148 
149     if (mp)
150         return QSize(mp->windowParams.width, mp->windowParams.height);
151     return QSize();
152 }
153 
154 }
155