]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
Imported Upstream version 1.3
[minitube] / src / MediaView.cpp
1 #include "MediaView.h"
2 #include "playlist/PrettyItemDelegate.h"
3 #include "networkaccess.h"
4 #include "videowidget.h"
5 #include "minisplitter.h"
6 #include "constants.h"
7 #include "downloadmanager.h"
8 #include "downloaditem.h"
9 #include "MainWindow.h"
10
11 namespace The {
12     NetworkAccess* http();
13 }
14
15 namespace The {
16     QMap<QString, QAction*>* globalActions();
17     QMap<QString, QMenu*>* globalMenus();
18     QNetworkAccessManager* networkAccessManager();
19 }
20
21 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
22
23     reallyStopped = false;
24     downloadItem = 0;
25
26     QBoxLayout *layout = new QHBoxLayout();
27     layout->setMargin(0);
28
29     splitter = new MiniSplitter(this);
30     splitter->setChildrenCollapsible(false);
31
32     sortBar = new THBlackBar(this);
33     mostRelevantAction = new QAction(tr("Most relevant"), this);
34     QKeySequence keySequence(Qt::CTRL + Qt::Key_1);
35     mostRelevantAction->setShortcut(keySequence);
36     mostRelevantAction->setStatusTip(mostRelevantAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
37     addAction(mostRelevantAction);
38     connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
39     sortBar->addAction(mostRelevantAction);
40     mostRecentAction = new QAction(tr("Most recent"), this);
41     keySequence = QKeySequence(Qt::CTRL + Qt::Key_2);
42     mostRecentAction->setShortcut(keySequence);
43     mostRecentAction->setStatusTip(mostRecentAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
44     addAction(mostRecentAction);
45     connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
46     sortBar->addAction(mostRecentAction);
47     mostViewedAction = new QAction(tr("Most viewed"), this);
48     keySequence = QKeySequence(Qt::CTRL + Qt::Key_3);
49     mostViewedAction->setShortcut(keySequence);
50     mostViewedAction->setStatusTip(mostViewedAction->text() + " (" + keySequence.toString(QKeySequence::NativeText) + ")");
51     addAction(mostViewedAction);
52     connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
53     sortBar->addAction(mostViewedAction);
54
55     listView = new QListView(this);
56     listView->setItemDelegate(new PrettyItemDelegate(this));
57     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
58
59     // dragndrop
60     listView->setDragEnabled(true);
61     listView->setAcceptDrops(true);
62     listView->setDropIndicatorShown(true);
63     listView->setDragDropMode(QAbstractItemView::DragDrop);
64
65     // cosmetics
66     listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
67     listView->setFrameShape( QFrame::NoFrame );
68     listView->setAttribute(Qt::WA_MacShowFocusRect, false);
69     listView->setMinimumSize(320,240);
70     listView->setUniformItemSizes(true);
71
72     // respond to the user doubleclicking a playlist item
73     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
74
75     listModel = new ListModel(this);
76     connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
77     // needed to restore the selection after dragndrop
78     connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
79     listView->setModel(listModel);
80
81     connect(listView->selectionModel(),
82             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
83             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
84
85     playlistWidget = new PlaylistWidget(this, sortBar, listView);
86
87     splitter->addWidget(playlistWidget);
88
89     videoAreaWidget = new VideoAreaWidget(this);
90     videoAreaWidget->setMinimumSize(320,240);
91
92 #ifdef APP_MAC
93     // mouse autohide does not work on the Mac (no mouseMoveEvent)
94     videoWidget = new Phonon::VideoWidget(this);
95 #else
96     videoWidget = new VideoWidget(this);
97 #endif
98
99     videoAreaWidget->setVideoWidget(videoWidget);
100     videoAreaWidget->setListModel(listModel);
101
102     loadingWidget = new LoadingWidget(this);
103     videoAreaWidget->setLoadingWidget(loadingWidget);
104
105     splitter->addWidget(videoAreaWidget);
106
107     layout->addWidget(splitter);
108     setLayout(layout);
109
110     // restore splitter state
111     QSettings settings;
112     splitter->restoreState(settings.value("splitter").toByteArray());
113
114     errorTimer = new QTimer(this);
115     errorTimer->setSingleShot(true);
116     errorTimer->setInterval(3000);
117     connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
118
119     workaroundTimer = new QTimer(this);
120     workaroundTimer->setSingleShot(true);
121     workaroundTimer->setInterval(3000);
122     connect(workaroundTimer, SIGNAL(timeout()), SLOT(timerPlay()));
123
124 #ifdef APP_DEMO
125     demoTimer = new QTimer(this);
126     demoTimer->setSingleShot(true);
127     demoTimer->setInterval(60000);
128     connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
129 #endif
130
131 }
132
133 void MediaView::initialize() {
134     connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
135     videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
136     connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
137             this, SLOT(showVideoContextMenu(QPoint)));
138 }
139
140 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
141     this->mediaObject = mediaObject;
142     Phonon::createPath(this->mediaObject, videoWidget);
143     connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
144     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
145             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
146     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
147             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
148     connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
149 }
150
151 void MediaView::search(SearchParams *searchParams) {
152     reallyStopped = false;
153
154 #ifdef APP_DEMO
155     demoTimer->stop();
156 #endif
157
158     videoAreaWidget->clear();
159     workaroundTimer->stop();
160     errorTimer->stop();
161
162     mediaObject->pause();
163     if (downloadItem) {
164         delete downloadItem;
165         downloadItem = 0;
166     }
167
168     this->searchParams = searchParams;
169
170     // start serching for videos
171     listModel->search(searchParams);
172
173     // this implies that the enum and the bar action order is the same
174     sortBar->setCheckedAction(searchParams->sortBy()-1);
175
176     listView->setFocus();
177
178
179     QString keyword = searchParams->keywords();
180     QString display = keyword;
181     if (keyword.startsWith("http://")) {
182         int separator = keyword.indexOf("|");
183         if (separator > 0 && separator + 1 < keyword.length()) {
184             display = keyword.mid(separator+1);
185         }
186     }
187     // tr("You're watching \"%1\"").arg(searchParams->keywords())
188
189 }
190
191 void MediaView::disappear() {
192     timerPlayFlag = true;
193 }
194
195 void MediaView::handleError(QString message) {
196     videoAreaWidget->showError(message);
197     skippedVideo = listModel->activeVideo();
198     // recover from errors by skipping to the next video
199     errorTimer->start(2000);
200 }
201
202 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
203 {
204
205     // qDebug() << "Phonon state: " << newState << oldState;
206     // slider->setEnabled(newState == Phonon::PlayingState);
207
208     switch (newState) {
209
210     case Phonon::ErrorState:
211         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
212         if (mediaObject->errorType() == Phonon::FatalError)
213             handleError(mediaObject->errorString());
214         break;
215
216     case Phonon::PlayingState:
217         //qDebug("playing");
218         videoAreaWidget->showVideo();
219         break;
220
221     case Phonon::StoppedState:
222         //qDebug("stopped");
223         // play() has already been called when setting the source
224         // but Phonon on Linux needs a little more help to start playback
225         if (!reallyStopped) mediaObject->play();
226
227 #ifdef APP_MAC
228         // Workaround for Mac playback start problem
229         if (!timerPlayFlag) {
230             workaroundTimer->start();
231         }
232 #endif
233
234         break;
235
236          case Phonon::PausedState:
237         //qDebug("paused");
238         break;
239
240          case Phonon::BufferingState:
241         //qDebug("buffering");
242         break;
243
244          case Phonon::LoadingState:
245         //qDebug("loading");
246         break;
247
248          default:
249         ;
250     }
251 }
252
253 void MediaView::pause() {
254     // qDebug() << "pause() called" << mediaObject->state();
255     switch( mediaObject->state() ) {
256     case Phonon::PlayingState:
257         mediaObject->pause();
258         break;
259     default:
260         mediaObject->play();
261         break;
262     }
263 }
264
265 void MediaView::stop() {
266     listModel->abortSearch();
267     reallyStopped = true;
268     mediaObject->stop();
269     videoAreaWidget->clear();
270     workaroundTimer->stop();
271     errorTimer->stop();
272     listView->selectionModel()->clearSelection();
273     if (downloadItem) {
274         delete downloadItem;
275         downloadItem = 0;
276     }
277 }
278
279 void MediaView::activeRowChanged(int row) {
280     if (reallyStopped) return;
281
282     Video *video = listModel->videoAt(row);
283     if (!video) return;
284
285     // now that we have a new video to play
286     // stop all the timers
287     workaroundTimer->stop();
288     errorTimer->stop();
289
290     mediaObject->pause();
291     if (downloadItem) {
292         delete downloadItem;
293         downloadItem = 0;
294     }
295     // slider->setMinimum(0);
296
297     // immediately show the loading widget
298     videoAreaWidget->showLoading(video);
299
300     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
301     // TODO handle signal in a proper slot and impl item error status
302     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
303
304     video->loadStreamUrl();
305
306     // reset the timer flag
307     timerPlayFlag = false;
308
309     // video title in the statusbar
310     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
311     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
312
313     The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
314
315     // see you in gotStreamUrl...
316
317 }
318
319 void MediaView::gotStreamUrl(QUrl streamUrl) {
320     if (reallyStopped) return;
321
322     Video *video = static_cast<Video *>(sender());
323     if (!video) {
324         qDebug() << "Cannot get sender";
325         return;
326     }
327     video->disconnect(this);
328
329     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
330     QString tempFile = tempDir + "/minitube.mp4";
331     if (!QFile::remove(tempFile)) {
332         qDebug() << "Cannot remove temp file";
333     }
334
335     Video *videoCopy = video->clone();
336     if (downloadItem) delete downloadItem;
337     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
338     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
339     // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
340     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
341     downloadItem->start();
342
343 }
344
345 /*
346 void MediaView::downloadProgress(int percent) {
347     MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
348
349     mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
350         "border: 1px solid #999999;"
351         // "border-left: 50px solid rgba(255, 0, 0, 128);"
352         "height: 8px;"
353         "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
354         + QString::number(percent/100.0) +
355
356         " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
357         "margin: 2px 0;"
358     "}"
359     "QSlider::handle:horizontal {"
360         "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
361         "border: 1px solid #5c5c5c;"
362         "width: 16px;"
363         "height: 16px;"
364         "margin: -2px 0;"
365         "border-radius: 8px;"
366     "}"
367
368     );
369 }
370
371 */
372
373 void MediaView::downloadStatusChanged() {
374     switch(downloadItem->status()) {
375     case Downloading:
376         startPlaying();
377         break;
378     case Starting:
379         qDebug() << "Starting";
380         break;
381     case Finished:
382         qDebug() << "Finished";
383         break;
384     case Failed:
385         qDebug() << "Failed";
386     case Idle:
387         qDebug() << "Idle";
388         break;
389     }
390 }
391
392 void MediaView::startPlaying() {
393     if (reallyStopped) return;
394
395     // go!
396     qDebug() << "Playing" << downloadItem->currentFilename();
397     mediaObject->setCurrentSource(downloadItem->currentFilename());
398     mediaObject->play();
399
400     // ensure we always have 10 videos ahead
401     listModel->searchNeeded();
402
403     // ensure active item is visible
404     int row = listModel->activeRow();
405     if (row != -1) {
406         QModelIndex index = listModel->index(row, 0, QModelIndex());
407         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
408     }
409
410 #ifdef APP_DEMO
411     demoTimer->start();
412 #endif
413
414 }
415
416 void MediaView::itemActivated(const QModelIndex &index) {
417     if (listModel->rowExists(index.row()))
418         listModel->setActiveRow(index.row());
419     // the user doubleclicked on the "Search More" item
420     else listModel->searchMore();
421 }
422
423 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
424
425 }
426
427 void MediaView::skipVideo() {
428     // skippedVideo is useful for DELAYED skip operations
429     // in order to be sure that we're skipping the video we wanted
430     // and not another one
431     if (skippedVideo) {
432         if (listModel->activeVideo() != skippedVideo) {
433             qDebug() << "Skip of video canceled";
434             return;
435         }
436         int nextRow = listModel->rowForVideo(skippedVideo);
437         nextRow++;
438         if (nextRow == -1) return;
439         listModel->setActiveRow(nextRow);
440     }
441 }
442
443 void MediaView::skip() {
444     int nextRow = listModel->nextRow();
445     if (nextRow == -1) return;
446     listModel->setActiveRow(nextRow);
447 }
448
449 void MediaView::openWebPage() {
450     Video* video = listModel->activeVideo();
451     if (!video) return;
452     mediaObject->pause();
453     QDesktopServices::openUrl(video->webpage());
454 }
455
456 void MediaView::copyWebPage() {
457     Video* video = listModel->activeVideo();
458     if (!video) return;
459     QString address = video->webpage().toString();
460     address.remove("&feature=youtube_gdata");
461     QApplication::clipboard()->setText(address);
462     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
463     QString message = tr("You can now paste the YouTube link into another application");
464     if (mainWindow) mainWindow->statusBar()->showMessage(message);
465 }
466
467 void MediaView::copyVideoLink() {
468     Video* video = listModel->activeVideo();
469     if (!video) return;
470     QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
471     QString message = tr("You can now paste the video stream URL into another application")
472                       + ". " + tr("The link will be valid only for a limited time.");
473     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
474     if (mainWindow) mainWindow->statusBar()->showMessage(message);
475 }
476
477 void MediaView::removeSelected() {
478     if (!listView->selectionModel()->hasSelection()) return;
479     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
480     listModel->removeIndexes(indexes);
481 }
482
483 void MediaView::selectVideos(QList<Video*> videos) {
484     foreach (Video *video, videos) {
485         QModelIndex index = listModel->indexForVideo(video);
486         listView->selectionModel()->select(index, QItemSelectionModel::Select);
487         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
488     }
489 }
490
491 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
492     const bool gotSelection = listView->selectionModel()->hasSelection();
493     The::globalActions()->value("remove")->setEnabled(gotSelection);
494     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
495     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
496 }
497
498 void MediaView::moveUpSelected() {
499     if (!listView->selectionModel()->hasSelection()) return;
500
501     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
502     qStableSort(indexes.begin(), indexes.end());
503     listModel->move(indexes, true);
504
505     // set current index after row moves to something more intuitive
506     int row = indexes.first().row();
507     listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
508 }
509
510 void MediaView::moveDownSelected() {
511     if (!listView->selectionModel()->hasSelection()) return;
512
513     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
514     qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
515     listModel->move(indexes, false);
516
517     // set current index after row moves to something more intuitive (respect 1 static item on bottom)
518     int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
519     listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
520 }
521
522 void MediaView::showVideoContextMenu(QPoint point) {
523     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
524 }
525
526 void MediaView::searchMostRelevant() {
527     searchParams->setSortBy(SearchParams::SortByRelevance);
528     search(searchParams);
529 }
530
531 void MediaView::searchMostRecent() {
532     searchParams->setSortBy(SearchParams::SortByNewest);
533     search(searchParams);
534 }
535
536 void MediaView::searchMostViewed() {
537     searchParams->setSortBy(SearchParams::SortByViewCount);
538     search(searchParams);
539 }
540
541 void MediaView::setPlaylistVisible(bool visible) {
542     playlistWidget->setVisible(visible);
543 }
544
545 void MediaView::timerPlay() {
546     // Workaround Phonon bug on Mac OSX
547     // qDebug() << mediaObject->currentTime();
548     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
549         // qDebug() << "Mac playback workaround";
550         mediaObject->pause();
551         // QTimer::singleShot(1000, mediaObject, SLOT(play()));
552         mediaObject->play();
553     }
554 }
555
556 void MediaView::saveSplitterState() {
557     QSettings settings;
558     settings.setValue("splitter", splitter->saveState());
559 }
560
561 #ifdef APP_DEMO
562 void MediaView::demoMessage() {
563     if (mediaObject->state() != Phonon::PlayingState) return;
564     mediaObject->pause();
565
566     QMessageBox msgBox;
567     msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
568     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
569     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
570     msgBox.setModal(true);
571
572     QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
573     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
574
575     msgBox.exec();
576
577     if (msgBox.clickedButton() == buyButton) {
578         QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
579     } else {
580         mediaObject->play();
581     }
582 }
583 #endif
584
585 void MediaView::downloadVideo() {
586     Video* video = listModel->activeVideo();
587     if (!video) return;
588
589     DownloadManager::instance()->addItem(video);
590
591     // TODO animate
592
593     The::globalActions()->value("downloads")->setVisible(true);
594
595     // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
596
597     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
598     QString message = tr("Downloading %1").arg(video->title());
599     if (mainWindow) mainWindow->statusBar()->showMessage(message);
600 }
601
602 void MediaView::fullscreen() {
603     videoAreaWidget->setParent(0);
604     videoAreaWidget->showFullScreen();
605 }
606
607 /*
608 void MediaView::setSlider(QSlider *slider) {
609     this->slider = slider;
610     // slider->setEnabled(false);
611     slider->setTracking(false);
612     // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
613 }
614
615 void MediaView::sliderMoved(int value) {
616     qDebug() << __func__;
617     int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
618     qDebug() << slider->minimum() << value << slider->maximum();
619     if (sliderPercent <= downloadItem->currentPercent()) {
620         qDebug() << sliderPercent << downloadItem->currentPercent();
621         mediaObject->seek(value);
622     } else {
623         seekTo(value);
624     }
625 }
626
627 void MediaView::seekTo(int value) {
628     qDebug() << __func__;
629     mediaObject->pause();
630     workaroundTimer->stop();
631     errorTimer->stop();
632     // mediaObject->clear();
633
634     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
635     QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
636     if (!QFile::remove(tempFile)) {
637         qDebug() << "Cannot remove temp file";
638     }
639     Video *videoCopy = downloadItem->getVideo()->clone();
640     QUrl streamUrl = videoCopy->getStreamUrl();
641     streamUrl.addQueryItem("begin", QString::number(value));
642     if (downloadItem) delete downloadItem;
643     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
644     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
645     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
646     downloadItem->start();
647
648     // slider->setMinimum(value);
649
650 }
651
652 */