]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
c9ae47f90b4ae4f2246ea55f4e31cfee6356831f
[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(30000);
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(playbackFinished()));
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         // also hide sidebar
188         playlistWidget->hide();
189     } else playlistWidget->show();
190     // tr("You're watching \"%1\"").arg(searchParams->keywords())
191
192 }
193
194 void MediaView::disappear() {
195     timerPlayFlag = true;
196 }
197
198 void MediaView::handleError(QString message) {
199     if (message.indexOf("movie atom") != -1 || message.indexOf("Could not open") != -1) {
200         QTimer::singleShot(1000, this, SLOT(startPlaying()));
201         return;
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 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
211 {
212
213     // qDebug() << "Phonon state: " << newState << oldState;
214     // slider->setEnabled(newState == Phonon::PlayingState);
215
216     switch (newState) {
217
218     case Phonon::ErrorState:
219         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
220         if (mediaObject->errorType() == Phonon::FatalError)
221             handleError(mediaObject->errorString());
222         break;
223
224     case Phonon::PlayingState:
225         // qDebug("playing");
226         videoAreaWidget->showVideo();
227         break;
228
229     case Phonon::StoppedState:
230         // qDebug("stopped");
231         // play() has already been called when setting the source
232         // but Phonon on Linux needs a little more help to start playback
233         // if (!reallyStopped) mediaObject->play();
234
235 #ifdef APP_MAC
236         // Workaround for Mac playback start problem
237         if (!timerPlayFlag) {
238             // workaroundTimer->start();
239         }
240 #endif
241
242         break;
243
244          case Phonon::PausedState:
245         qDebug("paused");
246         break;
247
248          case Phonon::BufferingState:
249         qDebug("buffering");
250         break;
251
252          case Phonon::LoadingState:
253         qDebug("loading");
254         break;
255
256          default:
257         ;
258     }
259 }
260
261 void MediaView::pause() {
262     // qDebug() << "pause() called" << mediaObject->state();
263     switch( mediaObject->state() ) {
264     case Phonon::PlayingState:
265         mediaObject->pause();
266         break;
267     default:
268         mediaObject->play();
269         break;
270     }
271 }
272
273 void MediaView::stop() {
274     listModel->abortSearch();
275     reallyStopped = true;
276     mediaObject->stop();
277     videoAreaWidget->clear();
278     workaroundTimer->stop();
279     errorTimer->stop();
280     listView->selectionModel()->clearSelection();
281     if (downloadItem) {
282         downloadItem->stop();
283         delete downloadItem;
284         downloadItem = 0;
285     }
286 }
287
288 void MediaView::activeRowChanged(int row) {
289     if (reallyStopped) return;
290
291     Video *video = listModel->videoAt(row);
292     if (!video) return;
293
294     // now that we have a new video to play
295     // stop all the timers
296     workaroundTimer->stop();
297     errorTimer->stop();
298
299     mediaObject->pause();
300     if (downloadItem) {
301         downloadItem->stop();
302         delete downloadItem;
303         downloadItem = 0;
304     }
305     // slider->setMinimum(0);
306
307     // immediately show the loading widget
308     videoAreaWidget->showLoading(video);
309
310     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
311     // TODO handle signal in a proper slot and impl item error status
312     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
313
314     video->loadStreamUrl();
315
316     // reset the timer flag
317     timerPlayFlag = false;
318
319     // video title in the statusbar
320     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
321     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
322
323     The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
324
325     // see you in gotStreamUrl...
326
327 }
328
329 void MediaView::gotStreamUrl(QUrl streamUrl) {
330     if (reallyStopped) return;
331
332     Video *video = static_cast<Video *>(sender());
333     if (!video) {
334         qDebug() << "Cannot get sender";
335         return;
336     }
337     video->disconnect(this);
338
339
340     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
341 #ifdef Q_WS_X11
342     QString tempFile = tempDir + "/minitube-" + getenv("USERNAME") + ".mp4";
343 #else
344     QString tempFile = tempDir + "/minitube.mp4";
345 #endif
346     if (QFile::exists(tempFile) && !QFile::remove(tempFile)) {
347         qDebug() << "Cannot remove temp file";
348     }
349
350     Video *videoCopy = video->clone();
351     if (downloadItem) {
352         downloadItem->stop();
353         delete downloadItem;
354     }
355     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
356     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
357     // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
358     connect(downloadItem, SIGNAL(bufferProgress(int)), loadingWidget, SLOT(bufferStatus(int)));
359     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
360     connect(video, SIGNAL(errorStreamUrl(QString)), SLOT(handleError(QString)));
361     connect(downloadItem, SIGNAL(error(QString)), SLOT(handleError(QString)));
362     downloadItem->start();
363
364 }
365
366 /*
367 void MediaView::downloadProgress(int percent) {
368     MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
369
370     mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
371         "border: 1px solid #999999;"
372         // "border-left: 50px solid rgba(255, 0, 0, 128);"
373         "height: 8px;"
374         "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
375         + QString::number(percent/100.0) +
376
377         " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
378         "margin: 2px 0;"
379     "}"
380     "QSlider::handle:horizontal {"
381         "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
382         "border: 1px solid #5c5c5c;"
383         "width: 16px;"
384         "height: 16px;"
385         "margin: -2px 0;"
386         "border-radius: 8px;"
387     "}"
388
389     );
390 }
391
392 */
393
394 void MediaView::downloadStatusChanged() {
395     switch(downloadItem->status()) {
396     case Downloading:
397         startPlaying();
398         break;
399     case Starting:
400         // qDebug() << "Starting";
401         break;
402     case Finished:
403         // qDebug() << "Finished" << mediaObject->state();
404         // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
405         break;
406     case Failed:
407         // qDebug() << "Failed";
408     case Idle:
409         // qDebug() << "Idle";
410         break;
411     }
412 }
413
414 void MediaView::startPlaying() {
415     if (reallyStopped) return;
416
417     // go!
418     qDebug() << "Playing" << downloadItem->currentFilename();
419     mediaObject->setCurrentSource(downloadItem->currentFilename());
420     mediaObject->play();
421
422     // ensure we always have 10 videos ahead
423     listModel->searchNeeded();
424
425     // ensure active item is visible
426     int row = listModel->activeRow();
427     if (row != -1) {
428         QModelIndex index = listModel->index(row, 0, QModelIndex());
429         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
430     }
431
432 #ifdef APP_DEMO
433     demoTimer->start(30000);
434 #endif
435
436 }
437
438 void MediaView::itemActivated(const QModelIndex &index) {
439     if (listModel->rowExists(index.row()))
440         listModel->setActiveRow(index.row());
441     // the user doubleclicked on the "Search More" item
442     else listModel->searchMore();
443 }
444
445 void MediaView::currentSourceChanged(const Phonon::MediaSource /* source */ ) {
446
447 }
448
449 void MediaView::skipVideo() {
450     // skippedVideo is useful for DELAYED skip operations
451     // in order to be sure that we're skipping the video we wanted
452     // and not another one
453     if (skippedVideo) {
454         if (listModel->activeVideo() != skippedVideo) {
455             qDebug() << "Skip of video canceled";
456             return;
457         }
458         int nextRow = listModel->rowForVideo(skippedVideo);
459         nextRow++;
460         if (nextRow == -1) return;
461         listModel->setActiveRow(nextRow);
462     }
463 }
464
465 void MediaView::skip() {
466     int nextRow = listModel->nextRow();
467     if (nextRow == -1) return;
468     listModel->setActiveRow(nextRow);
469 }
470
471 void MediaView::playbackFinished() {
472     // qDebug() << "finished" << mediaObject->currentTime() << mediaObject->totalTime();
473     // add 10 secs for imprecise Phonon backends (VLC, Xine)
474     if (mediaObject->currentTime() + 10000 < mediaObject->totalTime()) {
475         // mediaObject->seek(mediaObject->currentTime());
476         QTimer::singleShot(3000, this, SLOT(playbackResume()));
477     } else skip();
478 }
479
480 void MediaView::playbackResume() {
481     mediaObject->seek(mediaObject->currentTime());
482     mediaObject->play();
483 }
484
485 void MediaView::openWebPage() {
486     Video* video = listModel->activeVideo();
487     if (!video) return;
488     mediaObject->pause();
489     QDesktopServices::openUrl(video->webpage());
490 }
491
492 void MediaView::copyWebPage() {
493     Video* video = listModel->activeVideo();
494     if (!video) return;
495     QString address = video->webpage().toString();
496     address.remove("&feature=youtube_gdata");
497     QApplication::clipboard()->setText(address);
498     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
499     QString message = tr("You can now paste the YouTube link into another application");
500     if (mainWindow) mainWindow->statusBar()->showMessage(message);
501 }
502
503 void MediaView::copyVideoLink() {
504     Video* video = listModel->activeVideo();
505     if (!video) return;
506     QApplication::clipboard()->setText(video->getStreamUrl().toEncoded());
507     QString message = tr("You can now paste the video stream URL into another application")
508                       + ". " + tr("The link will be valid only for a limited time.");
509     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
510     if (mainWindow) mainWindow->statusBar()->showMessage(message);
511 }
512
513 void MediaView::removeSelected() {
514     if (!listView->selectionModel()->hasSelection()) return;
515     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
516     listModel->removeIndexes(indexes);
517 }
518
519 void MediaView::selectVideos(QList<Video*> videos) {
520     foreach (Video *video, videos) {
521         QModelIndex index = listModel->indexForVideo(video);
522         listView->selectionModel()->select(index, QItemSelectionModel::Select);
523         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
524     }
525 }
526
527 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
528     const bool gotSelection = listView->selectionModel()->hasSelection();
529     The::globalActions()->value("remove")->setEnabled(gotSelection);
530     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
531     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
532 }
533
534 void MediaView::moveUpSelected() {
535     if (!listView->selectionModel()->hasSelection()) return;
536
537     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
538     qStableSort(indexes.begin(), indexes.end());
539     listModel->move(indexes, true);
540
541     // set current index after row moves to something more intuitive
542     int row = indexes.first().row();
543     listView->selectionModel()->setCurrentIndex(listModel->index(row>1?row:1), QItemSelectionModel::NoUpdate);
544 }
545
546 void MediaView::moveDownSelected() {
547     if (!listView->selectionModel()->hasSelection()) return;
548
549     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
550     qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
551     listModel->move(indexes, false);
552
553     // set current index after row moves to something more intuitive (respect 1 static item on bottom)
554     int row = indexes.first().row()+1, max = listModel->rowCount() - 2;
555     listView->selectionModel()->setCurrentIndex(listModel->index(row>max?max:row), QItemSelectionModel::NoUpdate);
556 }
557
558 void MediaView::showVideoContextMenu(QPoint point) {
559     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
560 }
561
562 void MediaView::searchMostRelevant() {
563     searchParams->setSortBy(SearchParams::SortByRelevance);
564     search(searchParams);
565 }
566
567 void MediaView::searchMostRecent() {
568     searchParams->setSortBy(SearchParams::SortByNewest);
569     search(searchParams);
570 }
571
572 void MediaView::searchMostViewed() {
573     searchParams->setSortBy(SearchParams::SortByViewCount);
574     search(searchParams);
575 }
576
577 void MediaView::setPlaylistVisible(bool visible) {
578     playlistWidget->setVisible(visible);
579 }
580
581 void MediaView::timerPlay() {
582     // Workaround Phonon bug on Mac OSX
583     // qDebug() << mediaObject->currentTime();
584     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
585         // qDebug() << "Mac playback workaround";
586         mediaObject->pause();
587         // QTimer::singleShot(1000, mediaObject, SLOT(play()));
588         mediaObject->play();
589     }
590 }
591
592 void MediaView::saveSplitterState() {
593     QSettings settings;
594     settings.setValue("splitter", splitter->saveState());
595 }
596
597 #ifdef APP_DEMO
598 void MediaView::demoMessage() {
599     if (mediaObject->state() != Phonon::PlayingState) return;
600     mediaObject->pause();
601
602     QMessageBox msgBox;
603     msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
604     msgBox.setText(tr("This is just the demo version of %1.").arg(Constants::APP_NAME));
605     msgBox.setInformativeText(tr("It allows you to test the application and see if it works for you."));
606     msgBox.setModal(true);
607
608     QPushButton *quitButton = msgBox.addButton(tr("Continue"), QMessageBox::RejectRole);
609     QPushButton *buyButton = msgBox.addButton(tr("Get the full version"), QMessageBox::ActionRole);
610
611     msgBox.exec();
612
613     if (msgBox.clickedButton() == buyButton) {
614         QDesktopServices::openUrl(QString(Constants::WEBSITE) + "#download");
615     } else {
616         mediaObject->play();
617         demoTimer->start(300000);
618     }
619 }
620 #endif
621
622 void MediaView::downloadVideo() {
623     Video* video = listModel->activeVideo();
624     if (!video) return;
625
626     DownloadManager::instance()->addItem(video);
627
628     // TODO animate
629
630     The::globalActions()->value("downloads")->setVisible(true);
631
632     // The::globalActions()->value("download")->setEnabled(DownloadManager::instance()->itemForVideo(video) == 0);
633
634     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
635     QString message = tr("Downloading %1").arg(video->title());
636     if (mainWindow) mainWindow->statusBar()->showMessage(message);
637 }
638
639 void MediaView::fullscreen() {
640     videoAreaWidget->setParent(0);
641     videoAreaWidget->showFullScreen();
642 }
643
644 /*
645 void MediaView::setSlider(QSlider *slider) {
646     this->slider = slider;
647     // slider->setEnabled(false);
648     slider->setTracking(false);
649     // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
650 }
651
652 void MediaView::sliderMoved(int value) {
653     qDebug() << __func__;
654     int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
655     qDebug() << slider->minimum() << value << slider->maximum();
656     if (sliderPercent <= downloadItem->currentPercent()) {
657         qDebug() << sliderPercent << downloadItem->currentPercent();
658         mediaObject->seek(value);
659     } else {
660         seekTo(value);
661     }
662 }
663
664 void MediaView::seekTo(int value) {
665     qDebug() << __func__;
666     mediaObject->pause();
667     workaroundTimer->stop();
668     errorTimer->stop();
669     // mediaObject->clear();
670
671     QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
672     QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
673     if (!QFile::remove(tempFile)) {
674         qDebug() << "Cannot remove temp file";
675     }
676     Video *videoCopy = downloadItem->getVideo()->clone();
677     QUrl streamUrl = videoCopy->getStreamUrl();
678     streamUrl.addQueryItem("begin", QString::number(value));
679     if (downloadItem) delete downloadItem;
680     downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
681     connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
682     // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
683     downloadItem->start();
684
685     // slider->setMinimum(value);
686
687 }
688
689 */