From c8eea5b3b1df42c79c5048ed3cad387863fbe93f Mon Sep 17 00:00:00 2001 From: "dreamer.dead" Date: Fri, 24 Apr 2015 20:06:47 +0300 Subject: [PATCH] Change VideoDefinition class interface. --- src/mainwindow.cpp | 27 ++++++++++------ src/playlistitemdelegate.cpp | 2 +- src/video.cpp | 48 +++++++++++++---------------- src/video.h | 3 ++ src/videodefinition.cpp | 60 +++++++++++++++++++++++++----------- src/videodefinition.h | 24 ++++++++++++--- 6 files changed, 104 insertions(+), 60 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 08a20a6..29794cf 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -859,7 +859,8 @@ void MainWindow::readSettings() { } else { setGeometry(100, 100, 1000, 500); } - setDefinitionMode(settings.value("definition", VideoDefinition::getDefinitionNames().first()).toString()); + const VideoDefinition& firstDefinition = VideoDefinition::getDefinitions().first(); + setDefinitionMode(settings.value("definition", firstDefinition.getName()).toString()); The::globalActions()->value("manualplay")->setChecked(settings.value("manualplay", false).toBool()); } @@ -1470,16 +1471,22 @@ void MainWindow::setDefinitionMode(QString definitionName) { } void MainWindow::toggleDefinitionMode() { - QSettings settings; - QString currentDefinition = settings.value("definition").toString(); - QStringList definitionNames = VideoDefinition::getDefinitionNames(); - int currentIndex = definitionNames.indexOf(currentDefinition); - int nextIndex = 0; - if (currentIndex != definitionNames.size() - 1) { - nextIndex = currentIndex + 1; + const QString definitionName = QSettings().value("definition").toString(); + const QList& definitions = VideoDefinition::getDefinitions(); + const VideoDefinition& currentDefinition = VideoDefinition::getDefinitionFor(definitionName); + if (currentDefinition.isEmpty()) { + setDefinitionMode(definitions.first().getName()); + return; + } + + int index = definitions.indexOf(currentDefinition); + if (index != definitions.size() - 1) { + index++; + } else { + index = 0; } - QString nextDefinition = definitionNames.at(nextIndex); - setDefinitionMode(nextDefinition); + // TODO: pass a VideoDefinition instead of QString. + setDefinitionMode(definitions.at(index).getName()); } void MainWindow::showFullscreenToolbar(bool show) { diff --git a/src/playlistitemdelegate.cpp b/src/playlistitemdelegate.cpp index 717c7e5..d29d973 100644 --- a/src/playlistitemdelegate.cpp +++ b/src/playlistitemdelegate.cpp @@ -214,7 +214,7 @@ void PlaylistItemDelegate::paintBody( QPainter* painter, } if (downloadInfo) { - QString definitionString = VideoDefinition::getDefinitionName(video->getDefinitionCode()); + const QString definitionString = VideoDefinition::getDefinitionFor(video->getDefinitionCode()).getName(); textLoc.setX(textLoc.x() + stringSize.width() + PADDING); stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, definitionString ) ); QRect viewCountTextBox(textLoc , stringSize); diff --git a/src/video.cpp b/src/video.cpp index af63afc..4e2192d 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -195,16 +195,15 @@ void Video::gotVideoInfo(QByteArray data) { } void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) { - QSettings settings; - QString definitionName = settings.value("definition", "360p").toString(); - int definitionCode = VideoDefinition::getDefinitionCode(definitionName); + const QString definitionName = QSettings().value("definition", "360p").toString(); + const VideoDefinition& definition = VideoDefinition::getDefinitionFor(definitionName); // qDebug() << "fmtUrlMap" << fmtUrlMap; - QStringList formatUrls = fmtUrlMap.split(',', QString::SkipEmptyParts); + const QStringList formatUrls = fmtUrlMap.split(',', QString::SkipEmptyParts); QHash urlMap; foreach(QString formatUrl, formatUrls) { // qDebug() << "formatUrl" << formatUrl; - QStringList urlParams = formatUrl.split('&', QString::SkipEmptyParts); + const QStringList urlParams = formatUrl.split('&', QString::SkipEmptyParts); // qDebug() << "urlParams" << urlParams; int format = -1; @@ -263,37 +262,25 @@ void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) { // qWarning() << url; - if (format == definitionCode) { + if (format == definition.getCode()) { // qDebug() << "Found format" << definitionCode; - QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode); - m_streamUrl = videoUrl; - this->definitionCode = definitionCode; - emit gotStreamUrl(videoUrl); - loadingStreamUrl = false; + saveDefinitionForUrl(url, definition); return; } urlMap.insert(format, url); } - QList definitionCodes = VideoDefinition::getDefinitionCodes(); - int currentIndex = definitionCodes.indexOf(definitionCode); - int previousIndex = 0; - while (currentIndex >= 0) { - previousIndex = currentIndex - 1; - if (previousIndex < 0) previousIndex = 0; - int definitionCode = definitionCodes.at(previousIndex); - if (urlMap.contains(definitionCode)) { + const QList& definitions = VideoDefinition::getDefinitions(); + int previousIndex = std::max(definitions.indexOf(definition) - 1, 0); + for (; previousIndex >= 0; previousIndex--) { + const VideoDefinition& previousDefinition = definitions.at(previousIndex); + if (urlMap.contains(previousDefinition.getCode())) { // qDebug() << "Found format" << definitionCode; - QString url = urlMap.value(definitionCode); - QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode); - m_streamUrl = videoUrl; - this->definitionCode = definitionCode; - emit gotStreamUrl(videoUrl); - loadingStreamUrl = false; + saveDefinitionForUrl(urlMap.value(previousDefinition.getCode()), + previousDefinition); return; } - currentIndex--; } emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage)); @@ -489,3 +476,12 @@ QString Video::formattedDuration() const { QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss"; return QTime().addSecs(m_duration).toString(format); } + +void Video::saveDefinitionForUrl(const QString& url, const VideoDefinition& definition) { + const QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode); + m_streamUrl = videoUrl; + definitionCode = definition.getCode(); + emit gotStreamUrl(videoUrl); + loadingStreamUrl = false; +} + diff --git a/src/video.h b/src/video.h index 1645442..de2c1b5 100644 --- a/src/video.h +++ b/src/video.h @@ -27,6 +27,8 @@ $END_LICENSE */ #endif #include +class VideoDefinition; + class Video : public QObject { Q_OBJECT @@ -106,6 +108,7 @@ private: void captureFunction(const QString &name, const QString &js); void captureObject(const QString &name, const QString &js); QString decryptSignature(const QString &s); + void saveDefinitionForUrl(const QString& url, const VideoDefinition& definition); QString m_title; QString m_description; diff --git a/src/videodefinition.cpp b/src/videodefinition.cpp index 28e3024..443f171 100644 --- a/src/videodefinition.cpp +++ b/src/videodefinition.cpp @@ -20,30 +20,54 @@ $END_LICENSE */ #include "videodefinition.h" -QStringList VideoDefinition::getDefinitionNames() { - static QStringList definitionNames = QStringList() << "360p" << "720p" << "1080p"; - return definitionNames; -} +namespace { +static const int kEmptyDefinitionCode = -1; -QList VideoDefinition::getDefinitionCodes() { - static QList definitionCodes = QList() << 18 << 22 << 37; - return definitionCodes; -} +static const VideoDefinition kEmptyDefinition(QString(), kEmptyDefinitionCode); -QHash VideoDefinition::getDefinitions() { - static QHash definitions; - if (definitions.isEmpty()) { - definitions.insert("360p", 18); - definitions.insert("720p", 22); - definitions.insert("1080p", 37); +template +const VideoDefinition& getDefinitionForImpl(T matchValue) { + const QList& definitions = VideoDefinition::getDefinitions(); + const int size = definitions.size(); + for (int ii = 0; ii < size; ++ii) { + const VideoDefinition& def = definitions.at(ii); + if ((def.*Getter)() == matchValue) + return def; } + + return kEmptyDefinition; +} +} + +// static +const QList& VideoDefinition::getDefinitions() { + static QList definitions = QList() + << VideoDefinition(QLatin1String("360p"), 18) + << VideoDefinition(QLatin1String("720p"), 22) + << VideoDefinition(QLatin1String("1080p"), 37); return definitions; } -int VideoDefinition::getDefinitionCode(QString name) { - return VideoDefinition::getDefinitions().value(name); +// static +const VideoDefinition& VideoDefinition::getDefinitionFor(const QString& name) { + return getDefinitionForImpl(name); +} + +// static +const VideoDefinition& VideoDefinition::getDefinitionFor(int code) { + return getDefinitionForImpl(code); +} + +VideoDefinition::VideoDefinition(const QString& name, int code) : + m_name(name), + m_code(code) { +} + +VideoDefinition::VideoDefinition(const VideoDefinition& other) : + m_name(other.m_name), + m_code(other.m_code) { } -QString VideoDefinition::getDefinitionName(int code) { - return getDefinitions().key(code); +bool VideoDefinition::isEmpty() const { + return m_code == kEmptyDefinitionCode && m_name.isEmpty(); } diff --git a/src/videodefinition.h b/src/videodefinition.h index 809fafb..13001c1 100644 --- a/src/videodefinition.h +++ b/src/videodefinition.h @@ -26,12 +26,26 @@ $END_LICENSE */ class VideoDefinition { public: - static QStringList getDefinitionNames(); - static QList getDefinitionCodes(); - static QHash getDefinitions(); - static int getDefinitionCode(QString name); - static QString getDefinitionName(int code); + static const QList& getDefinitions(); + static const VideoDefinition& getDefinitionFor(const QString& name); + static const VideoDefinition& getDefinitionFor(int code); + VideoDefinition(const QString& name, int code); + VideoDefinition(const VideoDefinition& other); + + const QString& getName() const { return m_name; } + int getCode() const { return m_code; } + bool isEmpty() const; + +private: + VideoDefinition& operator=(const VideoDefinition&); + + const QString m_name; + const int m_code; }; +inline bool operator==(const VideoDefinition& lhs, const VideoDefinition& rhs) { + return lhs.getCode() == rhs.getCode() && lhs.getName() == rhs.getName(); +} + #endif // VIDEODEFINITION_H -- 2.39.5