]> git.sur5r.net Git - minitube/blob - src/video.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / video.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "video.h"
22 #include "datautils.h"
23 #include "http.h"
24 #include "httputils.h"
25 #include "jsfunctions.h"
26 #include "playlistitemdelegate.h"
27 #include "videodefinition.h"
28 #include "ytvideo.h"
29
30 Video::Video()
31     : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
32       loadingThumbnail(false), ytVideo(nullptr) {}
33
34 Video::~Video() {
35     qDebug() << "Deleting" << id;
36 }
37
38 Video *Video::clone() {
39     Video *clone = new Video();
40     clone->title = title;
41     clone->description = description;
42     clone->channelTitle = channelTitle;
43     clone->channelId = channelId;
44     clone->webpage = webpage;
45     clone->streamUrl = streamUrl;
46     clone->thumbnail = thumbnail;
47     clone->thumbnailUrl = thumbnailUrl;
48     clone->mediumThumbnailUrl = mediumThumbnailUrl;
49     clone->duration = duration;
50     clone->formattedDuration = formattedDuration;
51     clone->published = published;
52     clone->formattedPublished = formattedPublished;
53     clone->viewCount = viewCount;
54     clone->formattedViewCount = formattedViewCount;
55     clone->id = id;
56     clone->definitionCode = definitionCode;
57     return clone;
58 }
59
60 const QString &Video::getWebpage() {
61     if (webpage.isEmpty() && !id.isEmpty())
62         webpage.append("https://www.youtube.com/watch?v=").append(id);
63     return webpage;
64 }
65
66 void Video::setWebpage(const QString &value) {
67     webpage = value;
68
69     // Get Video ID
70     if (id.isEmpty()) {
71         QRegExp re(JsFunctions::instance()->videoIdRE());
72         if (re.indexIn(webpage) == -1) {
73             qWarning() << QString("Cannot get video id for %1").arg(webpage);
74             // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
75             // loadingStreamUrl = false;
76             return;
77         }
78         id = re.cap(1);
79     }
80 }
81
82 void Video::loadThumbnail() {
83     if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
84     loadingThumbnail = true;
85     QObject *reply = HttpUtils::yt().get(thumbnailUrl);
86     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
87 }
88
89 void Video::setDuration(int value) {
90     duration = value;
91     formattedDuration = DataUtils::formatDuration(duration);
92 }
93
94 void Video::setViewCount(int value) {
95     viewCount = value;
96     formattedViewCount = DataUtils::formatCount(viewCount);
97 }
98
99 void Video::setPublished(const QDateTime &value) {
100     published = value;
101     formattedPublished = DataUtils::formatDateTime(published);
102 }
103
104 void Video::setThumbnail(const QByteArray &bytes) {
105     qreal ratio = qApp->devicePixelRatio();
106     thumbnail.loadFromData(bytes);
107     thumbnail.setDevicePixelRatio(ratio);
108     const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
109     if (thumbnail.width() > thumbWidth)
110         thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
111     emit gotThumbnail();
112     loadingThumbnail = false;
113 }
114
115 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
116     qDebug() << "Streams loaded";
117     definitionCode = ytVideo->getDefinitionCode();
118     this->streamUrl = streamUrl;
119     emit gotStreamUrl(streamUrl, audioUrl);
120     ytVideo->deleteLater();
121     ytVideo = nullptr;
122 }
123
124 void Video::loadStreamUrl() {
125     if (ytVideo) {
126         qDebug() << "Already loading" << id;
127         return;
128     }
129     ytVideo = new YTVideo(id, this);
130     connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
131     connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
132         emit errorStreamUrl(msg);
133         ytVideo->deleteLater();
134         ytVideo = nullptr;
135     });
136     ytVideo->loadStreamUrl();
137 }
138
139 void Video::abortLoadStreamUrl() {
140     if (ytVideo) {
141         ytVideo->disconnect(this);
142         ytVideo->deleteLater();
143         ytVideo = nullptr;
144     }
145 }