]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
Imported Upstream version 1.9
[minitube] / src / MediaView.cpp
1 #include "MediaView.h"
2 #include "playlistview.h"
3 #include "playlist/PrettyItemDelegate.h"
4 #include "networkaccess.h"
5 #include "videowidget.h"
6 #include "minisplitter.h"
7 #include "constants.h"
8 #include "downloadmanager.h"
9 #include "downloaditem.h"
10 #include "MainWindow.h"
11 #include "temporary.h"
12 #include "sidebarwidget.h"
13 #include "playlistwidget.h"
14 #include "refinesearchwidget.h"
15 #include "sidebarwidget.h"
16 #ifdef APP_MAC
17 #include "macfullscreen.h"
18 #endif
19
20 namespace The {
21 NetworkAccess* http();
22 }
23
24 namespace The {
25 QMap<QString, QAction*>* globalActions();
26 QMap<QString, QMenu*>* globalMenus();
27 QNetworkAccessManager* networkAccessManager();
28 }
29
30 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
31
32     reallyStopped = false;
33     downloadItem = 0;
34
35     QBoxLayout *layout = new QVBoxLayout();
36     layout->setMargin(0);
37
38     splitter = new MiniSplitter(this);
39     splitter->setChildrenCollapsible(false);
40
41     listView = new PlaylistView(this);
42     listView->setItemDelegate(new PrettyItemDelegate(this));
43     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
44
45     // dragndrop
46     listView->setDragEnabled(true);
47     listView->setAcceptDrops(true);
48     listView->setDropIndicatorShown(true);
49     listView->setDragDropMode(QAbstractItemView::DragDrop);
50
51     // cosmetics
52     listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
53     listView->setFrameShape( QFrame::NoFrame );
54     listView->setAttribute(Qt::WA_MacShowFocusRect, false);
55     listView->setMinimumSize(320,240);
56     listView->setUniformItemSizes(true);
57
58     // respond to the user doubleclicking a playlist item
59     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
60
61     listModel = new ListModel(this);
62     connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
63     // needed to restore the selection after dragndrop
64     connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
65     listView->setModel(listModel);
66
67     connect(listView->selectionModel(),
68             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
69             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
70
71     connect(listView, SIGNAL(authorPushed(QModelIndex)), SLOT(authorPushed(QModelIndex)));
72
73     sidebar = new SidebarWidget(this);
74     sidebar->setPlaylist(listView);
75     connect(sidebar->getRefineSearchWidget(), SIGNAL(searchRefined()),
76             SLOT(searchAgain()));
77     connect(listModel, SIGNAL(haveSuggestions(const QStringList &)),
78             sidebar, SLOT(showSuggestions(const QStringList &)));
79     connect(sidebar, SIGNAL(suggestionAccepted(QString)),
80             MainWindow::instance(), SLOT(startToolbarSearch(QString)));
81     splitter->addWidget(sidebar);
82
83     videoAreaWidget = new VideoAreaWidget(this);
84     // videoAreaWidget->setMinimumSize(320,240);
85     videoWidget = new Phonon::VideoWidget(this);
86     videoAreaWidget->setVideoWidget(videoWidget);
87     videoAreaWidget->setListModel(listModel);
88
89     loadingWidget = new LoadingWidget(this);
90     videoAreaWidget->setLoadingWidget(loadingWidget);
91
92     splitter->addWidget(videoAreaWidget);
93
94     layout->addWidget(splitter);
95     setLayout(layout);
96
97     splitter->setStretchFactor(0, 1);
98     splitter->setStretchFactor(1, 6);
99
100     // restore splitter state
101     QSettings settings;
102     splitter->restoreState(settings.value("splitter").toByteArray());
103
104     errorTimer = new QTimer(this);
105     errorTimer->setSingleShot(true);
106     errorTimer->setInterval(3000);
107     connect(errorTimer, SIGNAL(timeout()), SLOT(skipVideo()));
108
109     workaroundTimer = new QTimer(this);
110     workaroundTimer->setSingleShot(true);
111     workaroundTimer->setInterval(3000);
112     connect(workaroundTimer, SIGNAL(timeout()), SLOT(timerPlay()));
113
114 #ifdef APP_DEMO
115     demoTimer = new QTimer(this);
116     demoTimer->setSingleShot(true);
117     connect(demoTimer, SIGNAL(timeout()), SLOT(demoMessage()));
118 #endif
119
120 }
121
122 void MediaView::initialize() {
123     connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
124
125     /*
126     videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
127     connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
128             this, SLOT(showVideoContextMenu(QPoint)));
129             */
130
131     QAction* refineSearchAction = The::globalActions()->value("refine-search");
132     connect(refineSearchAction, SIGNAL(toggled(bool)),
133             sidebar, SLOT(toggleRefineSearch(bool)));
134 }
135
136 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
137     this->mediaObject = mediaObject;
138     Phonon::createPath(this->mediaObject, videoWidget);
139     connect(mediaObject, SIGNAL(finished()), this, SLOT(playbackFinished()));
140     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
141             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
142     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
143             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
144     // connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
145     connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(aboutToFinish()));
146 }
147
148 void MediaView::search(SearchParams *searchParams) {
149     reallyStopped = false;
150
151 #ifdef APP_DEMO
152     demoTimer->stop();
153 #endif
154     workaroundTimer->stop();
155     errorTimer->stop();
156
157     this->searchParams = searchParams;
158
159     // start serching for videos
160     listModel->search(searchParams);
161
162     sidebar->showPlaylist();
163     listView->setFocus();
164
165     QString keyword = searchParams->keywords();
166     QString display = keyword;
167     if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
168         int separator = keyword.indexOf("|");
169         if (separator > 0 && separator + 1 < keyword.length()) {
170             display = keyword.mid(separator+1);
171         }
172     }
173
174     sidebar->getRefineSearchWidget()->setSearchParams(searchParams);
175     sidebar->hideSuggestions();
176
177 }
178
179 void MediaView::searchAgain() {
180     search(searchParams);
181 }
182
183 void MediaView::appear() {
184     listView->setFocus();
185 }
186
187 void MediaView::disappear() {
188     timerPlayFlag = true;
189 }
190
191 void MediaView::handleError(QString /* message */) {
192
193     QTimer::singleShot(500, this, SLOT(startPlaying()));
194
195     /*
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
203 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
204     // qDebug() << "Phonon state: " << newState;
205     // slider->setEnabled(newState == Phonon::PlayingState);
206
207     switch (newState) {
208
209     case Phonon::ErrorState:
210         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
211         if (mediaObject->errorType() == Phonon::FatalError)
212             handleError(mediaObject->errorString());
213         break;
214
215     case Phonon::PlayingState:
216         // qDebug("playing");
217         videoAreaWidget->showVideo();
218         break;
219
220     case Phonon::StoppedState:
221         // qDebug("stopped");
222         // play() has already been called when setting the source
223         // but Phonon on Linux needs a little more help to start playback
224         // if (!reallyStopped) mediaObject->play();
225
226 #ifdef APP_MAC
227         // Workaround for Mac playback start problem
228         if (!timerPlayFlag) {
229             // workaroundTimer->start();
230         }
231 #endif
232
233         break;
234
235     case Phonon::PausedState:
236         qDebug("paused");
237         break;
238
239     case Phonon::BufferingState:
240         qDebug("buffering");
241         break;
242
243     case Phonon::LoadingState:
244         qDebug("loading");
245         break;
246
247     }
248 }
249
250 void MediaView::pause() {
251     // qDebug() << "pause() called" << mediaObject->state();
252
253     switch( mediaObject->state() ) {
254     case Phonon::PlayingState:
255         mediaObject->pause();
256         break;
257     default:
258         mediaObject->play();
259         break;
260     }
261
262 }
263
264 QRegExp MediaView::wordRE(QString s) {
265     return QRegExp("\\W" + s + "\\W?", Qt::CaseInsensitive);
266 }
267
268 void MediaView::stop() {
269     listModel->abortSearch();
270     reallyStopped = true;
271     mediaObject->stop();
272     videoAreaWidget->clear();
273     workaroundTimer->stop();
274     errorTimer->stop();
275     listView->selectionModel()->clearSelection();
276     if (downloadItem) {
277         downloadItem->stop();
278         delete downloadItem;
279         downloadItem = 0;
280     }
281     The::globalActions()->value("refine-search")->setChecked(false);
282 }
283
284 void MediaView::activeRowChanged(int row) {
285     if (reallyStopped) return;
286
287     Video *video = listModel->videoAt(row);
288     if (!video) return;
289
290     // now that we have a new video to play
291     // stop all the timers
292     workaroundTimer->stop();
293     errorTimer->stop();
294
295     mediaObject->stop();
296     if (downloadItem) {
297         downloadItem->stop();
298         delete downloadItem;
299         downloadItem = 0;
300     }
301     // slider->setMinimum(0);
302
303     // immediately show the loading widget
304     videoAreaWidget->showLoading(video);
305
306     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)), Qt::UniqueConnection);
307     // TODO handle signal in a proper slot and impl item error status
308     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
309
310     video->loadStreamUrl();
311
312     // reset the timer flag
313     timerPlayFlag = false;
314
315     // video title in the statusbar
316     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
317     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
318
319
320     // ensure active item is visible
321     // int row = listModel->activeRow();
322     if (row != -1) {
323         QModelIndex index = listModel->index(row, 0, QModelIndex());
324         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
325     }
326
327     // enable/disable actions
328     The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
329     The::globalActions()->value("skip")->setEnabled(true);
330     The::globalActions()->value("previous")->setEnabled(row > 0);
331     The::globalActions()->value("stopafterthis")->setEnabled(true);
332
333     // see you in gotStreamUrl...
334
335 }
336
337 void MediaView::gotStreamUrl(QUrl streamUrl) {
338     if (reallyStopped) return;
339
340     Video *video = static_cast<Video *>(sender());
341     if (!video) {
342         qDebug() << "Cannot get sender";
343         return;
344     }
345     video->disconnect(this);
346
347     QString tempFile = Temporary::filename();
348
349     Video *videoCopy = video->clone();
350     if (downloadItem) {
351         downloadItem->stop();
352         delete downloadItem;
353     }
354     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
355     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()), Qt::UniqueConnection);
356     // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
357     connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
358     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
359     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
360     connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)), Qt::UniqueConnection);
361     downloadItem->start();
362
363 }
364
365 /*
366 void MediaView::downloadProgress(int percent) {
367     MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
368
369     mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
370         "border: 1px solid #999999;"
371         // "border-left: 50px solid rgba(255, 0, 0, 128);"
372         "height: 8px;"
373         "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
374         + QString::number(percent/100.0) +
375
376         " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
377         "margin: 2px 0;"
378     "}"
379     "QSlider::handle:horizontal {"
380         "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
381         "border: 1px solid #5c5c5c;"
382         "width: 16px;"
383         "height: 16px;"
384         "margin: -2px 0;"
385         "border-radius: 8px;"
386     "}"
387
388     );
389 }
390
391 */
392
393 void MediaView::downloadStatusChanged() {
394     switch(downloadItem->status()) {
395     case Downloading:
396         startPlaying();
397         break;
398     case Starting:
399         // qDebug() << "Starting";
400         break;
401     case Finished:
402         // qDebug() << "Finished" << mediaObject->state();
403         // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
404 #ifdef Q_WS_X11
405         seekSlider->setEnabled(mediaObject->isSeekable());
406 #endif
407         break;
408     case Failed:
409         // qDebug() << "Failed";
410     case Idle:
411         // qDebug() << "Idle";
412         break;
413     }
414 }
415
416 void MediaView::startPlaying() {
417     if (reallyStopped) return;
418     if (!downloadItem) {
419         skip();
420         return;
421     }
422
423     // go!
424     QString source = downloadItem->currentFilename();
425     qDebug() << "Playing" << source;
426     mediaObject->setCurrentSource(source);
427     mediaObject->play();
428 #ifdef Q_WS_X11
429     seekSlider->setEnabled(false);
430 #endif
431
432     // ensure we always have 10 videos ahead
433     listModel->searchNeeded();
434
435     // ensure active item is visible
436     int row = listModel->activeRow();
437     if (row != -1) {
438         QModelIndex index = listModel->index(row, 0, QModelIndex());
439         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
440     }
441
442 #ifdef APP_DEMO
443     demoTimer->start(60000);
444 #endif
445
446 }
447
448 void MediaView::itemActivated(const QModelIndex &index) {
449     if (listModel->rowExists(index.row())) {
450
451         // if it's the current video, just rewind and play
452         Video *activeVideo = listModel->activeVideo();
453         Video *video = listModel->videoAt(index.row());
454         if (activeVideo && video && activeVideo == video) {
455             mediaObject->seek(0);
456             mediaObject->play();
457         } else listModel->setActiveRow(index.row());
458
459     // the user doubleclicked on the "Search More" item
460     } else {
461         listModel->searchMore();
462         listView->selectionModel()->clearSelection();
463     }
464 }
465
466 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
467
468 }
469
470 void MediaView::skipVideo() {
471     // skippedVideo is useful for DELAYED skip operations
472     // in order to be sure that we're skipping the video we wanted
473     // and not another one
474     if (skippedVideo) {
475         if (listModel->activeVideo() != skippedVideo) {
476             qDebug() << "Skip of video canceled";
477             return;
478         }
479         int nextRow = listModel->rowForVideo(skippedVideo);
480         nextRow++;
481         if (nextRow == -1) return;
482         listModel->setActiveRow(nextRow);
483     }
484 }
485
486 void MediaView::skip() {
487     int nextRow = listModel->nextRow();
488     if (nextRow == -1) return;
489     listModel->setActiveRow(nextRow);
490 }
491
492 void MediaView::skipBackward() {
493     int prevRow = listModel->previousRow();
494     if (prevRow == -1) return;
495     listModel->setActiveRow(prevRow);
496 }
497
498 void MediaView::aboutToFinish() {
499     qint64 currentTime = mediaObject->currentTime();
500     qDebug() << __PRETTY_FUNCTION__ << currentTime;
501     if (currentTime + 10000 < mediaObject->totalTime()) {
502         // mediaObject->seek(mediaObject->currentTime());
503         // QTimer::singleShot(500, this, SLOT(playbackResume()));
504         mediaObject->seek(currentTime);
505         mediaObject->play();
506     }
507 }
508
509 void MediaView::playbackFinished() {
510     qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime();
511     // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
512     // add 10 secs for imprecise Phonon backends (VLC, Xine)
513     if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
514         // mediaObject->seek(mediaObject->currentTime());
515         QTimer::singleShot(500, this, SLOT(playbackResume()));
516     } else {
517         QAction* stopAfterThisAction = The::globalActions()->value("stopafterthis");
518         if (stopAfterThisAction->isChecked()) {
519             stopAfterThisAction->setChecked(false);
520         } else skip();
521     }
522 }
523
524 void MediaView::playbackResume() {
525     qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime();
526     mediaObject->seek(mediaObject->currentTime());
527     mediaObject->play();
528 }
529
530 void MediaView::openWebPage() {
531     Video* video = listModel->activeVideo();
532     if (!video) return;
533     mediaObject->pause();
534     QDesktopServices::openUrl(video->webpage());
535 }
536
537 void MediaView::copyWebPage() {
538     Video* video = listModel->activeVideo();
539     if (!video) return;
540     QString address = video->webpage().toString();
541     QApplication::clipboard()->setText(address);
542     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
543     QString message = tr("You can now paste the YouTube link into another application");
544     if (mainWindow) mainWindow->statusBar()->showMessage(message);
545 }
546
547 void MediaView::copyVideoLink() {
548     Video* video = listModel->activeVideo();
549     if (!video) return;
550     QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
551     QString message = tr("You can now paste the video stream URL into another application")
552             + ". " + tr("The link will be valid only for a limited time.");
553     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
554     if (mainWindow) mainWindow->statusBar()->showMessage(message);
555 }
556
557 void MediaView::removeSelected() {
558     if (!listView->selectionModel()->hasSelection()) return;
559     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
560     listModel->removeIndexes(indexes);
561 }
562
563 void MediaView::selectVideos(QList<Video*> videos) {
564     foreach (Video *video, videos) {
565         QModelIndex index = listModel->indexForVideo(video);
566         listView->selectionModel()->select(index, QItemSelectionModel::Select);
567         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
568     }
569 }
570
571 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
572     const bool gotSelection = listView->selectionModel()->hasSelection();
573     The::globalActions()->value("remove")->setEnabled(gotSelection);
574     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
575     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
576 }
577
578 void MediaView::moveUpSelected() {
579     if (!listView->selectionModel()->hasSelection()) return;
580
581     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
582     qStableSort(indexes.begin(), indexes.end());
583     listModel->move(indexes, true);
584
585     // set current index after row moves to something more intuitive
586     int row = indexes.first().row();
587     listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
588 }
589
590 void MediaView::moveDownSelected() {
591     if (!listView->selectionModel()->hasSelection()) return;
592
593     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
594     qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
595     listModel->move(indexes, false);
596
597     // set current index after row moves to something more intuitive (respect 1 static item on bottom)
598     int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
599     listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
600 }
601
602 void MediaView::showVideoContextMenu(QPoint point) {
603     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
604 }
605
606 void MediaView::searchMostRelevant() {
607     searchParams->setSortBy(SearchParams::SortByRelevance);
608     search(searchParams);
609 }
610
611 void MediaView::searchMostRecent() {
612     searchParams->setSortBy(SearchParams::SortByNewest);
613     search(searchParams);
614 }
615
616 void MediaView::searchMostViewed() {
617     searchParams->setSortBy(SearchParams::SortByViewCount);
618     search(searchParams);
619 }
620
621 void MediaView::setPlaylistVisible(bool visible) {
622     if (splitter->widget(0)->isVisible() == visible) return;
623     splitter->widget(0)->setVisible(visible);
624     listView->setFocus();
625 }
626
627 bool MediaView::isPlaylistVisible() {
628     return splitter->widget(0)->isVisible();
629 }
630
631 void MediaView::timerPlay() {
632     // Workaround Phonon bug on Mac OSX
633     // qDebug() << mediaObject->currentTime();
634     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
635         // qDebug() << "Mac playback workaround";
636         mediaObject->pause();
637         // QTimer::singleShot(1000, mediaObject, SLOT(play()));
638         mediaObject->play();
639     }
640 }
641
642 void MediaView::saveSplitterState() {
643     QSettings settings;
644     settings.setValue("splitter", splitter->saveState());
645 }
646
647 #ifdef APP_DEMO
648
649 static QPushButton *continueButton;
650
651 void MediaView::demoMessage() {
652     if (mediaObject->state() != Phonon::PlayingState) return;
653     mediaObject->pause();
654
655     QMessageBox msgBox(this);
656     msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
657     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::NAME));
658     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
659     msgBox.setModal(true);
660     // make it a "sheet" on the Mac
661     msgBox.setWindowModality(Qt::WindowModal);
662
663     continueButton = msgBox.addButton("5", QMessageBox::RejectRole);
664     continueButton->setEnabled(false);
665     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
666
667     QTimeLine *timeLine = new QTimeLine(6000, this);
668     timeLine->setCurveShape(QTimeLine::LinearCurve);
669     timeLine->setFrameRange(5, 0);
670     connect(timeLine, SIGNAL(frameChanged(int)), SLOT(updateContinueButton(int)));
671     timeLine->start();
672
673     msgBox.exec();
674
675     if (msgBox.clickedButton() == buyButton) {
676         QDesktopServices::openUrl(QUrl(QString(Constants::WEBSITE) + "#download"));
677     } else {
678         mediaObject->play();
679         demoTimer->start(600000);
680     }
681
682     delete timeLine;
683
684 }
685
686 void MediaView::updateContinueButton(int value) {
687     if (value == 0) {
688         continueButton->setText(tr("Continue"));
689         continueButton->setEnabled(true);
690     } else {
691         continueButton->setText(QString::number(value));
692     }
693 }
694
695 #endif
696
697 void MediaView::downloadVideo() {
698     Video* video = listModel->activeVideo();
699     if (!video) return;
700
701     DownloadManager::instance()->addItem(video);
702
703     // TODO animate
704
705     The::globalActions()->value("downloads")->setVisible(true);
706
707     // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
708
709     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
710     QString message = tr("Downloading %1").arg(video->title());
711     if (mainWindow) mainWindow->statusBar()->showMessage(message);
712 }
713
714 void MediaView::snapshot() {
715     QImage image = videoWidget->snapshot();
716     qDebug() << image.size();
717
718     const QPixmap& pixmap = QPixmap::grabWindow(videoWidget->winId());
719     // qDebug() << pixmap.size();
720     videoAreaWidget->showSnapshotPreview(pixmap);
721 }
722
723 void MediaView::fullscreen() {
724     videoAreaWidget->setParent(0);
725     videoAreaWidget->showFullScreen();
726 }
727
728 /*
729 void MediaView::setSlider(QSlider *slider) {
730     this->slider = slider;
731     // slider->setEnabled(false);
732     slider->setTracking(false);
733     // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
734 }
735
736 void MediaView::sliderMoved(int value) {
737     qDebug() << __func__;
738     int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
739     qDebug() << slider->minimum() << value << slider->maximum();
740     if (sliderPercent <= downloadItem->currentPercent()) {
741         qDebug() << sliderPercent << downloadItem->currentPercent();
742         mediaObject->seek(value);
743     } else {
744         seekTo(value);
745     }
746 }
747
748 void MediaView::seekTo(int value) {
749     qDebug() << __func__;
750     mediaObject->pause();
751     workaroundTimer->stop();
752     errorTimer->stop();
753     // mediaObject->clear();
754
755     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
756     QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
757     if (!QFile::remove(tempFile)) {
758         qDebug() << "Cannot remove temp file";
759     }
760     Video *videoCopy = downloadItem->getVideo()->clone();
761     QUrl streamUrl = videoCopy->getStreamUrl();
762     streamUrl.addQueryItem("begin", QString::number(value));
763     if (downloadItem) delete downloadItem;
764     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
765     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
766     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
767     downloadItem->start();
768
769     // slider->setMinimum(value);
770
771 }
772
773 */
774
775 void MediaView::findVideoParts() {
776
777     // parts
778     Video* video = listModel->activeVideo();
779     if (!video) return;
780
781     QString query = video->title();
782
783     static QString optionalSpace = "\\s*";
784     static QString staticCounterSeparators = "[\\/\\-]";
785     QString counterSeparators = "( of | " +
786             tr("of", "Used in video parts, as in '2 of 3'") +
787             " |" + staticCounterSeparators + ")";
788
789     // numbers from 1 to 15
790     static QString counterNumber = "([1-9]|1[0-5])";
791
792     // query.remove(QRegExp(counterSeparators + optionalSpace + counterNumber));
793     query.remove(QRegExp(counterNumber + optionalSpace + counterSeparators + optionalSpace + counterNumber));
794     query.remove(wordRE("pr?t\\.?" + optionalSpace + counterNumber));
795     query.remove(wordRE("ep\\.?" + optionalSpace + counterNumber));
796     query.remove(wordRE("part" + optionalSpace + counterNumber));
797     query.remove(wordRE("episode" + optionalSpace + counterNumber));
798     query.remove(wordRE(tr("part", "This is for video parts, as in 'Cool video - part 1'") +
799                         optionalSpace + counterNumber));
800     query.remove(wordRE(tr("episode", "This is for video parts, as in 'Cool series - episode 1'") +
801                         optionalSpace + counterNumber));
802     query.remove(QRegExp("[\\(\\)\\[\\]]"));
803
804 #define NUMBERS "one|two|three|four|five|six|seven|eight|nine|ten"
805
806     QRegExp englishNumberRE = QRegExp(QLatin1String(".*(") + NUMBERS + ").*", Qt::CaseInsensitive);
807     // bool numberAsWords = englishNumberRE.exactMatch(query);
808     query.remove(englishNumberRE);
809
810     QRegExp localizedNumberRE = QRegExp(QLatin1String(".*(") + tr(NUMBERS) + ").*", Qt::CaseInsensitive);
811     // if (!numberAsWords) numberAsWords = localizedNumberRE.exactMatch(query);
812     query.remove(localizedNumberRE);
813
814     SearchParams *searchParams = new SearchParams();
815     searchParams->setTransient(true);
816     searchParams->setKeywords(query);
817     searchParams->setAuthor(video->author());
818
819     /*
820     if (!numberAsWords) {
821         qDebug() << "We don't have number as words";
822         // searchParams->setSortBy(SearchParams::SortByNewest);
823         // TODO searchParams->setReverseOrder(true);
824         // TODO searchParams->setMax(50);
825     }
826     */
827
828     search(searchParams);
829
830 }
831
832 void MediaView::shareViaTwitter() {
833     Video* video = listModel->activeVideo();
834     if (!video) return;
835     QUrl url("https://twitter.com/intent/tweet");
836     url.addQueryItem("via", "minitubeapp");
837     url.addQueryItem("text", video->title());
838     url.addQueryItem("url", video->webpage().toString());
839     QDesktopServices::openUrl(url);
840 }
841
842 void MediaView::shareViaFacebook() {
843     Video* video = listModel->activeVideo();
844     if (!video) return;
845     QUrl url("https://www.facebook.com/sharer.php");
846     url.addQueryItem("t", video->title());
847     url.addQueryItem("u", video->webpage().toString());
848     QDesktopServices::openUrl(url);
849 }
850
851 void MediaView::shareViaBuffer() {
852     Video* video = listModel->activeVideo();
853     if (!video) return;
854     QUrl url("http://bufferapp.com/add");
855     url.addQueryItem("via", "minitubeapp");
856     url.addQueryItem("text", video->title());
857     url.addQueryItem("url", video->webpage().toString());
858     if (!video->thumbnailUrls().isEmpty())
859         url.addQueryItem("picture", video->thumbnailUrls().first().toString());
860     QDesktopServices::openUrl(url);
861 }
862
863 void MediaView::shareViaEmail() {
864     Video* video = listModel->activeVideo();
865     if (!video) return;
866     QUrl url("mailto:");
867     url.addQueryItem("subject", video->title());
868     QString body = video->title() + "\n" +
869             video->webpage().toString() + "\n\n" +
870             tr("Sent from %1").arg(Constants::NAME) + "\n" +
871             Constants::WEBSITE;
872     url.addQueryItem("body", body);
873     QDesktopServices::openUrl(url);
874 }
875
876 void MediaView::authorPushed(QModelIndex index) {
877     Video* video = listModel->videoAt(index.row());
878     if (!video) return;
879
880     QString channel = video->author();
881     if (channel.isEmpty()) return;
882
883     SearchParams *searchParams = new SearchParams();
884     searchParams->setAuthor(channel);
885     searchParams->setSortBy(SearchParams::SortByNewest);
886
887     // go!
888     search(searchParams);
889 }