]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
Fix build without APP_SNAPSHOT
[minitube] / src / mediaview.cpp
index 08798bf481ddbb7cf3c4ea9647e358602b754bb2..302c77a1ea478d6ca0c91329c42eb3fba6b48f86 100644 (file)
@@ -64,12 +64,13 @@ MediaView* MediaView::instance() {
     return i;
 }
 
-MediaView::MediaView(QWidget *parent) : QWidget(parent)
+MediaView::MediaView(QWidget *parent) : View(parent)
   , stopped(false)
   , downloadItem(0)
   #ifdef APP_SNAPSHOT
   , snapshotSettings(0)
   #endif
+  , pauseTime(0)
 { }
 
 void MediaView::initialize() {
@@ -189,7 +190,7 @@ void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
 SearchParams* MediaView::getSearchParams() {
     VideoSource *videoSource = playlistModel->getVideoSource();
     if (videoSource && videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
-        YTSearch *search = dynamic_cast<YTSearch *>(videoSource);
+        YTSearch *search = qobject_cast<YTSearch *>(videoSource);
         return search->getSearchParams();
     }
     return 0;
@@ -322,9 +323,13 @@ void MediaView::handleError(const QString &message) {
 
 #ifdef APP_PHONON
 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
-    if (newState == Phonon::PlayingState)
+    if (pauseTime > 0 && (newState == Phonon::PlayingState || newState == Phonon::BufferingState)) {
+        mediaObject->seek(pauseTime);
+        pauseTime = 0;
+    }
+    if (newState == Phonon::PlayingState) {
         videoAreaWidget->showVideo();
-    else if (newState == Phonon::ErrorState) {
+    else if (newState == Phonon::ErrorState) {
         qWarning() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
         if (mediaObject->errorType() == Phonon::FatalError)
             handleError(mediaObject->errorString());
@@ -337,9 +342,14 @@ void MediaView::pause() {
     switch( mediaObject->state() ) {
     case Phonon::PlayingState:
         mediaObject->pause();
+        pauseTimer.start();
         break;
     default:
-        mediaObject->play();
+        if (pauseTimer.hasExpired(60000)) {
+            pauseTimer.invalidate();
+            connect(playlistModel->activeVideo(), SIGNAL(gotStreamUrl(QUrl)), SLOT(resumeWithNewStreamUrl(QUrl)));
+            playlistModel->activeVideo()->loadStreamUrl();
+        } else mediaObject->play();
         break;
     }
 #endif
@@ -390,12 +400,16 @@ void MediaView::stop() {
     QSlider *slider = MainWindow::instance()->getSlider();
     slider->setEnabled(false);
     slider->setValue(0);
+#else
+    Phonon::SeekSlider *slider = MainWindow::instance()->getSeekSlider();
 #endif
 
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
         snapshotSettings = 0;
     }
+#endif
 }
 
 const QString & MediaView::getCurrentVideoId() {
@@ -466,11 +480,13 @@ void MediaView::activeRowChanged(int row) {
     slider->setValue(0);
 #endif
 
+#ifdef APP_SNAPSHOT
     if (snapshotSettings) {
         delete snapshotSettings;
         snapshotSettings = 0;
         MainWindow::instance()->adjustStatusBarVisibility();
     }
+#endif
 
     // see you in gotStreamUrl...
 }
@@ -634,7 +650,7 @@ void MediaView::aboutToFinish() {
 #ifdef APP_PHONON
     qint64 currentTime = mediaObject->currentTime();
     qint64 totalTime = mediaObject->totalTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
+    // qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
     if (totalTime < 1 || currentTime + 10000 < totalTime) {
         // QTimer::singleShot(500, this, SLOT(playbackResume()));
         mediaObject->seek(currentTime);
@@ -649,7 +665,7 @@ void MediaView::playbackFinished() {
 #ifdef APP_PHONON
     const qint64 totalTime = mediaObject->totalTime();
     const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
+    // qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
     // add 10 secs for imprecise Phonon backends (VLC, Xine)
     if (currentTime > 0 && currentTime + 10000 < totalTime) {
         // mediaObject->seek(currentTime);
@@ -667,7 +683,7 @@ void MediaView::playbackResume() {
     if (stopped) return;
 #ifdef APP_PHONON
     const qint64 currentTime = mediaObject->currentTime();
-    qDebug() << __PRETTY_FUNCTION__ << currentTime;
+    // qDebug() << __PRETTY_FUNCTION__ << currentTime;
     if (currentTime > 0)
         mediaObject->seek(currentTime);
     mediaObject->play();
@@ -680,7 +696,8 @@ void MediaView::openWebPage() {
 #ifdef APP_PHONON
     mediaObject->pause();
 #endif
-    QDesktopServices::openUrl(video->webpage());
+    QString url = video->webpage() + QLatin1String("&t=") + QString::number(mediaObject->currentTime() / 1000);
+    QDesktopServices::openUrl(url);
 }
 
 void MediaView::copyWebPage() {
@@ -785,7 +802,7 @@ void MediaView::demoMessage() {
 #endif
 
     QMessageBox msgBox(this);
-    msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
+    msgBox.setIconPixmap(IconUtils::pixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
     msgBox.setModal(true);
@@ -832,7 +849,7 @@ void MediaView::downloadVideo() {
     Video* video = playlistModel->activeVideo();
     if (!video) return;
     DownloadManager::instance()->addItem(video);
-    The::globalActions()->value("downloads")->setVisible(true);
+    MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("downloads"), true);
     QString message = tr("Downloading %1").arg(video->title());
     MainWindow::instance()->showMessage(message);
 }
@@ -905,6 +922,19 @@ void MediaView::startDownloading() {
     downloadItem->start();
 }
 
+void MediaView::resumeWithNewStreamUrl(const QUrl &streamUrl) {
+    pauseTime = mediaObject->currentTime();
+    mediaObject->setCurrentSource(streamUrl);
+    mediaObject->play();
+
+    Video *video = static_cast<Video *>(sender());
+    if (!video) {
+        qDebug() << "Cannot get sender in" << __PRETTY_FUNCTION__;
+        return;
+    }
+    video->disconnect(this);
+}
+
 void MediaView::maybeAdjustWindowSize() {
     QSettings settings;
     if (settings.value("adjustWindowSize", true).toBool())
@@ -1137,8 +1167,13 @@ void MediaView::toggleSubscription() {
     QString userId = video->channelId();
     if (userId.isEmpty()) return;
     bool subscribed = YTChannel::isSubscribed(userId);
-    if (subscribed) YTChannel::unsubscribe(userId);
-    else YTChannel::subscribe(userId);
+    if (subscribed) {
+        YTChannel::unsubscribe(userId);
+        MainWindow::instance()->showMessage(tr("Unsubscribed from %1").arg(video->channelTitle()));
+    } else {
+        YTChannel::subscribe(userId);
+        MainWindow::instance()->showMessage(tr("Subscribed to %1").arg(video->channelTitle()));
+    }
     updateSubscriptionAction(video, !subscribed);
 }