1 /* poppler-media.cc: qt interface to poppler
2 * Copyright (C) 2012 Guillermo A. Amaral B. <gamaral@kde.org>
3 * Copyright (C) 2013 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
33 class MediaRenditionPrivate
34 {
35 public:
36
MediaRenditionPrivate(::MediaRendition * rendition)37 MediaRenditionPrivate(::MediaRendition *rendition)
38 : rendition(rendition)
39 {
40 }
41
~MediaRenditionPrivate()42 ~MediaRenditionPrivate()
43 {
44 delete rendition;
45 }
46
47 ::MediaRendition *rendition;
48 };
49
MediaRendition(::MediaRendition * rendition)50 MediaRendition::MediaRendition(::MediaRendition *rendition)
51 : d_ptr(new MediaRenditionPrivate(rendition))
52 {
53 }
54
~MediaRendition()55 MediaRendition::~MediaRendition()
56 {
57 delete d_ptr;
58 }
59
60 bool
isValid() const61 MediaRendition::isValid() const
62 {
63 Q_D( const MediaRendition );
64 return d->rendition && d->rendition->isOk();
65 }
66
67 QString
contentType() const68 MediaRendition::contentType() const
69 {
70 Q_ASSERT(isValid() && "Invalid media rendition.");
71 Q_D( const MediaRendition );
72 return UnicodeParsedString(d->rendition->getContentType());
73 }
74
75 QString
fileName() const76 MediaRendition::fileName() const
77 {
78 Q_ASSERT(isValid() && "Invalid media rendition.");
79 Q_D( const MediaRendition );
80 return UnicodeParsedString(d->rendition->getFileName());
81 }
82
83 bool
isEmbedded() const84 MediaRendition::isEmbedded() const
85 {
86 Q_ASSERT(isValid() && "Invalid media rendition.");
87 Q_D( const MediaRendition );
88 return d->rendition->getIsEmbedded();
89 }
90
91 QByteArray
data() const92 MediaRendition::data() const
93 {
94 Q_ASSERT(isValid() && "Invalid media rendition.");
95 Q_D( const MediaRendition );
96
97 Stream *s = d->rendition->getEmbbededStream();
98 if (!s)
99 return QByteArray();
100
101 QBuffer buffer;
102 Guchar data[BUFFER_MAX];
103 int bread;
104
105 buffer.open(QIODevice::WriteOnly);
106 s->reset();
107 while ((bread = s->doGetChars(BUFFER_MAX, data)) != 0)
108 buffer.write(reinterpret_cast<const char *>(data), bread);
109 buffer.close();
110
111 return buffer.data();
112 }
113
114 bool
autoPlay() const115 MediaRendition::autoPlay() const
116 {
117 Q_D( const MediaRendition );
118 if (d->rendition->getBEParameters()) {
119 return d->rendition->getBEParameters()->autoPlay;
120 } else if (d->rendition->getMHParameters()) {
121 return d->rendition->getMHParameters()->autoPlay;
122 } else qDebug("No BE or MH parameters to reference!");
123 return false;
124 }
125
126 bool
showControls() const127 MediaRendition::showControls() const
128 {
129 Q_D( const MediaRendition );
130 if (d->rendition->getBEParameters()) {
131 return d->rendition->getBEParameters()->showControls;
132 } else if (d->rendition->getMHParameters()) {
133 return d->rendition->getMHParameters()->showControls;
134 } else qDebug("No BE or MH parameters to reference!");
135 return false;
136 }
137
138 float
repeatCount() const139 MediaRendition::repeatCount() const
140 {
141 Q_D( const MediaRendition );
142 if (d->rendition->getBEParameters()) {
143 return d->rendition->getBEParameters()->repeatCount;
144 } else if (d->rendition->getMHParameters()) {
145 return d->rendition->getMHParameters()->repeatCount;
146 } else qDebug("No BE or MH parameters to reference!");
147 return 1.f;
148 }
149
150 QSize
size() const151 MediaRendition::size() const
152 {
153 Q_D( const MediaRendition );
154 MediaParameters *mp = 0;
155
156 if (d->rendition->getBEParameters())
157 mp = d->rendition->getBEParameters();
158 else if (d->rendition->getMHParameters())
159 mp = d->rendition->getMHParameters();
160 else qDebug("No BE or MH parameters to reference!");
161
162 if (mp)
163 return QSize(mp->windowParams.width, mp->windowParams.height);
164 return QSize();
165 }
166
167 }
168
169