1 /*
2     SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
3     SPDX-FileCopyrightText: 2012 Guillermo A. Amaral B. <gamaral@kde.org>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "movie.h"
9 
10 // qt/kde includes
11 #include <QDir>
12 #include <QImage>
13 #include <QString>
14 #include <QTemporaryFile>
15 
16 #include <QDebug>
17 
18 #include "debug_p.h"
19 
20 using namespace Okular;
21 
22 class Movie::Private
23 {
24 public:
Private(const QString & url)25     explicit Private(const QString &url)
26         : m_url(url)
27         , m_rotation(Rotation0)
28         , m_playMode(PlayLimited)
29         , m_playRepetitions(1.0)
30         , m_tmp(nullptr)
31         , m_showControls(false)
32         , m_autoPlay(false)
33         , m_showPosterImage(false)
34     {
35     }
36 
37     QString m_url;
38     QSize m_aspect;
39     Rotation m_rotation;
40     PlayMode m_playMode;
41     double m_playRepetitions;
42     QTemporaryFile *m_tmp;
43     QImage m_posterImage;
44     bool m_showControls : 1;
45     bool m_autoPlay : 1;
46     bool m_showPosterImage : 1;
47 };
48 
Movie(const QString & fileName)49 Movie::Movie(const QString &fileName)
50     : d(new Private(fileName))
51 {
52 }
53 
Movie(const QString & fileName,const QByteArray & data)54 Movie::Movie(const QString &fileName, const QByteArray &data)
55     : d(new Private(fileName))
56 {
57     /* Store movie data as temporary file.
58      *
59      * Originally loaded movie data directly using a QBuffer, but sadly phonon
60      * fails to play on a few of my test systems (I think it's the Phonon
61      * GStreamer backend). Storing the data in a temporary file works fine
62      * though, not to mention, it releases much needed memory. (gamaral)
63      */
64     d->m_tmp = new QTemporaryFile(QStringLiteral("%1/okrXXXXXX").arg(QDir::tempPath()));
65     if (d->m_tmp->open()) {
66         d->m_tmp->write(data);
67         d->m_tmp->flush();
68     } else
69         qCDebug(OkularCoreDebug) << "Failed to create temporary file for video data.";
70 }
71 
~Movie()72 Movie::~Movie()
73 {
74     delete d->m_tmp;
75     delete d;
76 }
77 
url() const78 QString Movie::url() const
79 {
80     if (d->m_tmp)
81         return d->m_tmp->fileName();
82     else
83         return d->m_url;
84 }
85 
setSize(const QSize & aspect)86 void Movie::setSize(const QSize &aspect) // clazy:exclude=function-args-by-value TODO remove the & when we do a BIC change elsewhere
87 {
88     d->m_aspect = aspect;
89 }
90 
size() const91 QSize Movie::size() const
92 {
93     return d->m_aspect;
94 }
95 
setRotation(Rotation rotation)96 void Movie::setRotation(Rotation rotation)
97 {
98     d->m_rotation = rotation;
99 }
100 
rotation() const101 Rotation Movie::rotation() const
102 {
103     return d->m_rotation;
104 }
105 
setShowControls(bool show)106 void Movie::setShowControls(bool show)
107 {
108     d->m_showControls = show;
109 }
110 
showControls() const111 bool Movie::showControls() const
112 {
113     return d->m_showControls;
114 }
115 
setPlayMode(Movie::PlayMode mode)116 void Movie::setPlayMode(Movie::PlayMode mode)
117 {
118     d->m_playMode = mode;
119 }
120 
playMode() const121 Movie::PlayMode Movie::playMode() const
122 {
123     return d->m_playMode;
124 }
125 
setPlayRepetitions(double repetitions)126 void Movie::setPlayRepetitions(double repetitions)
127 {
128     d->m_playRepetitions = repetitions;
129 }
130 
playRepetitions() const131 double Movie::playRepetitions() const
132 {
133     return d->m_playRepetitions;
134 }
135 
setAutoPlay(bool autoPlay)136 void Movie::setAutoPlay(bool autoPlay)
137 {
138     d->m_autoPlay = autoPlay;
139 }
140 
autoPlay() const141 bool Movie::autoPlay() const
142 {
143     return d->m_autoPlay;
144 }
145 
setShowPosterImage(bool show)146 void Movie::setShowPosterImage(bool show)
147 {
148     d->m_showPosterImage = show;
149 }
150 
showPosterImage() const151 bool Movie::showPosterImage() const
152 {
153     return d->m_showPosterImage;
154 }
155 
setPosterImage(const QImage & image)156 void Movie::setPosterImage(const QImage &image)
157 {
158     d->m_posterImage = image;
159 }
160 
posterImage() const161 QImage Movie::posterImage() const
162 {
163     return d->m_posterImage;
164 }
165