} 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());
}
}
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<VideoDefinition>& 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) {
}
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);
}
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<int, QString> 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;
// 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<int> 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<VideoDefinition>& 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));
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;
+}
+
#endif
#include <QtNetwork>
+class VideoDefinition;
+
class Video : public QObject {
Q_OBJECT
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;
#include "videodefinition.h"
-QStringList VideoDefinition::getDefinitionNames() {
- static QStringList definitionNames = QStringList() << "360p" << "720p" << "1080p";
- return definitionNames;
-}
+namespace {
+static const int kEmptyDefinitionCode = -1;
-QList<int> VideoDefinition::getDefinitionCodes() {
- static QList<int> definitionCodes = QList<int>() << 18 << 22 << 37;
- return definitionCodes;
-}
+static const VideoDefinition kEmptyDefinition(QString(), kEmptyDefinitionCode);
-QHash<QString, int> VideoDefinition::getDefinitions() {
- static QHash<QString, int> definitions;
- if (definitions.isEmpty()) {
- definitions.insert("360p", 18);
- definitions.insert("720p", 22);
- definitions.insert("1080p", 37);
+template <typename T, T (VideoDefinition::*Getter)() const>
+const VideoDefinition& getDefinitionForImpl(T matchValue) {
+ const QList<VideoDefinition>& 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>& VideoDefinition::getDefinitions() {
+ static QList<VideoDefinition> definitions = QList<VideoDefinition>()
+ << 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<const QString&, &VideoDefinition::getName>(name);
+}
+
+// static
+const VideoDefinition& VideoDefinition::getDefinitionFor(int code) {
+ return getDefinitionForImpl<int, &VideoDefinition::getCode>(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();
}
class VideoDefinition {
public:
- static QStringList getDefinitionNames();
- static QList<int> getDefinitionCodes();
- static QHash<QString, int> getDefinitions();
- static int getDefinitionCode(QString name);
- static QString getDefinitionName(int code);
+ static const QList<VideoDefinition>& 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