1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <QtCore/qurl.h> |
41 | #include <QtCore/qvariant.h> |
42 | #include <QtCore/QPointer> |
43 | |
44 | #include <qmediaplaylist.h> |
45 | #include "qmediacontent.h" |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | static void qRegisterMediaContentMetaTypes() |
50 | { |
51 | qRegisterMetaType<QMediaContent>(); |
52 | } |
53 | |
54 | Q_CONSTRUCTOR_FUNCTION(qRegisterMediaContentMetaTypes) |
55 | |
56 | |
57 | class QMediaContentPrivate : public QSharedData |
58 | { |
59 | public: |
60 | QMediaContentPrivate(): |
61 | isPlaylistOwned(false) |
62 | {} |
63 | |
64 | #if QT_DEPRECATED_SINCE(6, 0) |
65 | QMediaContentPrivate(const QMediaResourceList &r): |
66 | isPlaylistOwned(false) |
67 | { |
68 | for (auto &item : r) |
69 | requests << item.request(); |
70 | } |
71 | #endif |
72 | |
73 | QMediaContentPrivate(const QNetworkRequest &r): |
74 | isPlaylistOwned(false) |
75 | { |
76 | requests << r; |
77 | } |
78 | |
79 | QMediaContentPrivate(const QMediaContentPrivate &other): |
80 | QSharedData(other), |
81 | requests(other.requests), |
82 | playlist(other.playlist), |
83 | isPlaylistOwned(false) |
84 | {} |
85 | |
86 | QMediaContentPrivate(QMediaPlaylist *pls, const QUrl &url, bool isOwn): |
87 | playlist(pls), |
88 | isPlaylistOwned(isOwn) |
89 | { |
90 | requests << QNetworkRequest(url); |
91 | } |
92 | |
93 | ~QMediaContentPrivate() |
94 | { |
95 | if (isPlaylistOwned && !playlist.isNull()) |
96 | playlist.data()->deleteLater(); |
97 | } |
98 | |
99 | bool operator ==(const QMediaContentPrivate &other) const |
100 | { |
101 | return requests == other.requests && playlist == other.playlist; |
102 | } |
103 | |
104 | QList<QNetworkRequest> requests; |
105 | QPointer<QMediaPlaylist> playlist; |
106 | bool isPlaylistOwned; |
107 | private: |
108 | QMediaContentPrivate& operator=(const QMediaContentPrivate &other); |
109 | }; |
110 | |
111 | |
112 | /*! |
113 | \class QMediaContent |
114 | |
115 | \brief The QMediaContent class provides access to the resource relating to a media content. |
116 | |
117 | \inmodule QtMultimedia |
118 | \ingroup multimedia |
119 | \ingroup multimedia_playback |
120 | |
121 | QMediaContent is used within the multimedia framework as the logical handle |
122 | to media content. A QMediaContent object contains a \l {QNetworkRequest} |
123 | which provides the URL of the content. |
124 | |
125 | A non-null QMediaContent will always have a reference to |
126 | the content available through the request() method. |
127 | |
128 | Alternatively QMediaContent can represent a playlist and contain a pointer to a |
129 | valid QMediaPlaylist object. In this case URL is optional and can either be empty |
130 | or point to the playlist URL. |
131 | */ |
132 | |
133 | |
134 | /*! |
135 | Constructs a null QMediaContent. |
136 | */ |
137 | |
138 | QMediaContent::QMediaContent() |
139 | { |
140 | } |
141 | |
142 | /*! |
143 | Constructs a media content with \a url providing a reference to the content. |
144 | */ |
145 | |
146 | QMediaContent::QMediaContent(const QUrl &url): |
147 | d(new QMediaContentPrivate) |
148 | { |
149 | d->requests << QNetworkRequest(url); |
150 | } |
151 | |
152 | /*! |
153 | Constructs a media content with \a request providing a reference to the content. |
154 | |
155 | This constructor can be used to reference media content via network protocols such as HTTP. |
156 | This may include additional information required to obtain the resource, such as Cookies or HTTP headers. |
157 | */ |
158 | |
159 | QMediaContent::QMediaContent(const QNetworkRequest &request): |
160 | d(new QMediaContentPrivate) |
161 | { |
162 | d->requests << request; |
163 | } |
164 | |
165 | #if QT_DEPRECATED_SINCE(6, 0) |
166 | /*! |
167 | \obsolete |
168 | |
169 | Constructs a media content with \a resource providing a reference to the content. |
170 | */ |
171 | |
172 | QMediaContent::QMediaContent(const QMediaResource &resource): |
173 | d(new QMediaContentPrivate) |
174 | { |
175 | d->requests << resource.request(); |
176 | } |
177 | |
178 | /*! |
179 | \obsolete |
180 | |
181 | Constructs a media content with \a resources providing a reference to the content. |
182 | */ |
183 | |
184 | QMediaContent::QMediaContent(const QMediaResourceList &resources): |
185 | d(new QMediaContentPrivate(resources)) |
186 | { |
187 | } |
188 | #endif |
189 | |
190 | /*! |
191 | Constructs a copy of the media content \a other. |
192 | */ |
193 | |
194 | QMediaContent::QMediaContent(const QMediaContent &other): |
195 | d(other.d) |
196 | { |
197 | } |
198 | |
199 | /*! |
200 | Constructs a media content with \a playlist. |
201 | |
202 | \a contentUrl of a playlist is an optional parameter and can be empty. |
203 | |
204 | Set \a takeOwnership to true if you want QMediaContent to take ownership of the playlist. |
205 | \a takeOwnership is set to false by default. |
206 | */ |
207 | |
208 | QMediaContent::QMediaContent(QMediaPlaylist *playlist, const QUrl &contentUrl, bool takeOwnership): |
209 | d(new QMediaContentPrivate(playlist, contentUrl, takeOwnership)) |
210 | { |
211 | } |
212 | |
213 | /*! |
214 | Destroys the media content object. |
215 | */ |
216 | |
217 | QMediaContent::~QMediaContent() |
218 | { |
219 | } |
220 | |
221 | /*! |
222 | Assigns the value of \a other to this media content. |
223 | */ |
224 | |
225 | QMediaContent& QMediaContent::operator=(const QMediaContent &other) |
226 | { |
227 | d = other.d; |
228 | return *this; |
229 | } |
230 | |
231 | /*! |
232 | Returns true if \a other is equivalent to this media content; false otherwise. |
233 | */ |
234 | |
235 | bool QMediaContent::operator==(const QMediaContent &other) const |
236 | { |
237 | return (d.constData() == 0 && other.d.constData() == nullptr) || |
238 | (d.constData() != 0 && other.d.constData() != nullptr && |
239 | *d.constData() == *other.d.constData()); |
240 | } |
241 | |
242 | /*! |
243 | Returns true if \a other is not equivalent to this media content; false otherwise. |
244 | */ |
245 | |
246 | bool QMediaContent::operator!=(const QMediaContent &other) const |
247 | { |
248 | return !(*this == other); |
249 | } |
250 | |
251 | /*! |
252 | Returns true if this media content is null (uninitialized); false otherwise. |
253 | */ |
254 | |
255 | bool QMediaContent::isNull() const |
256 | { |
257 | return d.constData() == nullptr; |
258 | } |
259 | |
260 | /*! |
261 | \since 5.14 |
262 | |
263 | Returns a QNetworkRequest that represents the resource for this media content. |
264 | */ |
265 | |
266 | QNetworkRequest QMediaContent::request() const |
267 | { |
268 | return (d && !d->requests.isEmpty()) ? d->requests.first() : QNetworkRequest(); |
269 | } |
270 | |
271 | #if QT_DEPRECATED_SINCE(6, 0) |
272 | /*! |
273 | \obsolete |
274 | |
275 | Returns a QUrl that represents that canonical resource for this media content. |
276 | */ |
277 | |
278 | QUrl QMediaContent::canonicalUrl() const |
279 | { |
280 | return request().url(); |
281 | } |
282 | |
283 | /*! |
284 | \obsolete |
285 | |
286 | Returns a QNetworkRequest that represents that canonical resource for this media content. |
287 | */ |
288 | |
289 | QNetworkRequest QMediaContent::canonicalRequest() const |
290 | { |
291 | return request(); |
292 | } |
293 | |
294 | /*! |
295 | \obsolete |
296 | |
297 | Returns a QMediaResource that represents that canonical resource for this media content. |
298 | */ |
299 | |
300 | QMediaResource QMediaContent::canonicalResource() const |
301 | { |
302 | return (d && !d->requests.isEmpty()) ? d->requests.first() : QMediaResource(); |
303 | } |
304 | |
305 | /*! |
306 | \obsolete |
307 | |
308 | Returns a list of alternative resources for this media content. The first item in this list |
309 | is always the canonical resource. |
310 | */ |
311 | |
312 | QMediaResourceList QMediaContent::resources() const |
313 | { |
314 | QMediaResourceList list; |
315 | if (d) { |
316 | for (auto &item : d->requests) |
317 | list << item; |
318 | } |
319 | return list; |
320 | } |
321 | #endif // #if QT_DEPRECATED_SINCE(6, 0) |
322 | |
323 | /*! |
324 | Returns a playlist for this media content or 0 if this QMediaContent is not a playlist. |
325 | */ |
326 | |
327 | QMediaPlaylist *QMediaContent::playlist() const |
328 | { |
329 | return d.constData() != nullptr |
330 | ? d->playlist.data() |
331 | : nullptr; |
332 | } |
333 | |
334 | QT_END_NAMESPACE |
335 | |
336 | |