]> git.sur5r.net Git - minitube/blobdiff - src/MediaView.cpp
Imported Upstream version 1.4.3
[minitube] / src / MediaView.cpp
index 8890acebddf2abeee5818edca4debedc2999cd14..66fc5ec1bcd681e7689651befa701a4250f4d807 100644 (file)
@@ -124,7 +124,6 @@ MediaView::MediaView(QWidget *parent) : QWidget(parent) {
 #ifdef APP_DEMO
     demoTimer = new QTimer(this);
     demoTimer->setSingleShot(true);
-    demoTimer->setInterval(60000);
     connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
 #endif
 
@@ -140,12 +139,12 @@ void MediaView::initialize() {
 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
     this->mediaObject = mediaObject;
     Phonon::createPath(this->mediaObject, videoWidget);
-    connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
+    connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
-    connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
+    // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
 }
 
 void MediaView::search(SearchParams *searchParams) {
@@ -183,7 +182,10 @@ void MediaView::search(SearchParams *searchParams) {
         if (separator > 0 && separator + 1 < keyword.length()) {
             display = keyword.mid(separator+1);
         }
-    }
+
+        // also hide sidebar
+        playlistWidget->hide();
+    } else playlistWidget->show();
     // tr("You're watching \"%1\"").arg(searchParams->keywords())
 
 }
@@ -193,10 +195,17 @@ void MediaView::disappear() {
 }
 
 void MediaView::handleError(QString message) {
+    // if (message.indexOf("movie atom") != -1 || message.indexOf("Could not open") != -1) {
+        QTimer::singleShot(1000, this, SLOT(startPlaying()));
+        return;
+    // }
+
+    /*
     videoAreaWidget->showError(message);
     skippedVideo = listModel->activeVideo();
     // recover from errors by skipping to the next video
     errorTimer->start(2000);
+    */
 }
 
 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
@@ -214,35 +223,35 @@ void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
         break;
 
     case Phonon::PlayingState:
-        //qDebug("playing");
+        // qDebug("playing");
         videoAreaWidget->showVideo();
         break;
 
     case Phonon::StoppedState:
-        //qDebug("stopped");
+        // qDebug("stopped");
         // play() has already been called when setting the source
         // but Phonon on Linux needs a little more help to start playback
-        if (!reallyStopped) mediaObject->play();
+        // if (!reallyStopped) mediaObject->play();
 
 #ifdef APP_MAC
         // Workaround for Mac playback start problem
         if (!timerPlayFlag) {
-            workaroundTimer->start();
+            // workaroundTimer->start();
         }
 #endif
 
         break;
 
          case Phonon::PausedState:
-        //qDebug("paused");
+        qDebug("paused");
         break;
 
          case Phonon::BufferingState:
-        //qDebug("buffering");
+        qDebug("buffering");
         break;
 
          case Phonon::LoadingState:
-        //qDebug("loading");
+        qDebug("loading");
         break;
 
          default:
@@ -271,6 +280,7 @@ void MediaView::stop() {
     errorTimer->stop();
     listView->selectionModel()->clearSelection();
     if (downloadItem) {
+        downloadItem->stop();
         delete downloadItem;
         downloadItem = 0;
     }
@@ -289,6 +299,7 @@ void MediaView::activeRowChanged(int row) {
 
     mediaObject->pause();
     if (downloadItem) {
+        downloadItem->stop();
         delete downloadItem;
         downloadItem = 0;
     }
@@ -326,18 +337,29 @@ void MediaView::gotStreamUrl(QUrl streamUrl) {
     }
     video->disconnect(this);
 
+
     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
+#ifdef Q_WS_X11
+    QString tempFile = tempDir + "/minitube-" + getenv("USERNAME") + ".mp4";
+#else
     QString tempFile = tempDir + "/minitube.mp4";
-    if (!QFile::remove(tempFile)) {
+#endif
+    if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
         qDebug() << "Cannot remove temp file";
     }
 
     Video *videoCopy = video->clone();
-    if (downloadItem) delete downloadItem;
+    if (downloadItem) {
+        downloadItem->stop();
+        delete downloadItem;
+    }
     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
     // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
+    connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)));
     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
+    connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
+    connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)));
     downloadItem->start();
 
 }
@@ -376,21 +398,26 @@ void MediaView::downloadStatusChanged() {
         startPlaying();
         break;
     case Starting:
-        qDebug() << "Starting";
+        // qDebug() << "Starting";
         break;
     case Finished:
-        qDebug() << "Finished";
+        // qDebug() << "Finished" << mediaObject->state();
+        // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
         break;
     case Failed:
-        qDebug() << "Failed";
+        // qDebug() << "Failed";
     case Idle:
-        qDebug() << "Idle";
+        // qDebug() << "Idle";
         break;
     }
 }
 
 void MediaView::startPlaying() {
     if (reallyStopped) return;
+    if (!downloadItem) {
+        skip();
+        return;
+    }
 
     // go!
     qDebug() << "Playing" << downloadItem->currentFilename();
@@ -408,7 +435,7 @@ void MediaView::startPlaying() {
     }
 
 #ifdef APP_DEMO
-    demoTimer->start();
+    demoTimer->start(30000);
 #endif
 
 }
@@ -446,6 +473,20 @@ void MediaView::skip() {
     listModel->setActiveRow(nextRow);
 }
 
+void MediaView::playbackFinished() {
+    // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
+    // add 10 secs for imprecise Phonon backends (VLC, Xine)
+    if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
+        // mediaObject->seek(mediaObject->currentTime());
+        QTimer::singleShot(3000, this, SLOT(playbackResume()));
+    } else skip();
+}
+
+void MediaView::playbackResume() {
+    mediaObject->seek(mediaObject->currentTime());
+    mediaObject->play();
+}
+
 void MediaView::openWebPage() {
     Video* video = listModel->activeVideo();
     if (!video) return;
@@ -563,11 +604,13 @@ void MediaView::demoMessage() {
     if (mediaObject->state() != Phonon::PlayingState) return;
     mediaObject->pause();
 
-    QMessageBox msgBox;
+    QMessageBox msgBox(this);
     msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
     msgBox.setModal(true);
+    // make it a "sheet" on the Mac
+    msgBox.setWindowModality(Qt::WindowModal);
 
     QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
@@ -578,6 +621,7 @@ void MediaView::demoMessage() {
         QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
     } else {
         mediaObject->play();
+        demoTimer->start(300000);
     }
 }
 #endif