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