]> git.sur5r.net Git - minitube/blob - src/MediaView.cpp
new error signal when stream url cannot be determined
[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
7 namespace The {
8     QMap<QString, QAction*>* globalActions();
9     QMap<QString, QMenu*>* globalMenus();
10     QNetworkAccessManager* networkAccessManager();
11 }
12
13 MediaView::MediaView(QWidget *parent) : QWidget(parent) {
14
15     QBoxLayout *layout = new QHBoxLayout();
16     layout->setMargin(0);
17
18     splitter = new MiniSplitter(this);
19     splitter->setChildrenCollapsible(false);
20
21     sortBar = new THBlackBar(this);
22     mostRelevantAction = new THAction(tr("Most relevant"), this);
23     connect(mostRelevantAction, SIGNAL(triggered()), this, SLOT(searchMostRelevant()), Qt::QueuedConnection);
24     sortBar->addAction(mostRelevantAction);
25     mostRecentAction = new THAction(tr("Most recent"), this);
26     connect(mostRecentAction, SIGNAL(triggered()), this, SLOT(searchMostRecent()), Qt::QueuedConnection);
27     sortBar->addAction(mostRecentAction);
28     mostViewedAction = new THAction(tr("Most viewed"), this);
29     connect(mostViewedAction, SIGNAL(triggered()), this, SLOT(searchMostViewed()), Qt::QueuedConnection);
30     sortBar->addAction(mostViewedAction);
31
32     listView = new QListView(this);
33     listView->setItemDelegate(new Playlist::PrettyItemDelegate(this));
34     listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
35
36     // dragndrop
37     listView->setDragEnabled(true);
38     listView->setAcceptDrops(true);
39     listView->setDropIndicatorShown(true);
40     listView->setDragDropMode(QAbstractItemView::InternalMove);
41
42     // cosmetics
43     listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
44     listView->setFrameShape( QFrame::NoFrame );
45     listView->setAttribute(Qt::WA_MacShowFocusRect, false);
46     listView->setMinimumSize(320,240);
47
48     // respond to the user doubleclicking a playlist item
49     connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
50
51     listModel = new ListModel(this);
52     connect(listModel, SIGNAL(activeRowChanged(int)), this, SLOT(activeRowChanged(int)));
53     // needed to restore the selection after dragndrop
54     connect(listModel, SIGNAL(needSelectionFor(QList<Video*>)), this, SLOT(selectVideos(QList<Video*>)));
55     listView->setModel(listModel);
56
57     connect(listView->selectionModel(),
58             SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )),
59             this, SLOT(selectionChanged ( const QItemSelection & , const QItemSelection & )));
60
61     playlistWidget = new PlaylistWidget(this, sortBar, listView);
62
63     splitter->addWidget(playlistWidget);
64
65     videoAreaWidget = new VideoAreaWidget(this);
66     videoAreaWidget->setMinimumSize(320,240);
67
68     videoWidget = new Phonon::VideoWidget(this);
69     videoAreaWidget->setVideoWidget(videoWidget);
70
71     loadingWidget = new LoadingWidget(this);
72     videoAreaWidget->setLoadingWidget(loadingWidget);
73
74     splitter->addWidget(videoAreaWidget);
75
76     layout->addWidget(splitter);
77     setLayout(layout);
78
79 }
80
81 MediaView::~MediaView() {
82
83 }
84
85 void MediaView::initialize() {
86     connect(videoAreaWidget, SIGNAL(doubleClicked()), The::globalActions()->value("fullscreen"), SLOT(trigger()));
87     videoAreaWidget->setContextMenuPolicy(Qt::CustomContextMenu);
88     connect(videoAreaWidget, SIGNAL(customContextMenuRequested(QPoint)),
89             this, SLOT(showVideoContextMenu(QPoint)));
90 }
91
92 void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
93     this->mediaObject = mediaObject;
94     Phonon::createPath(this->mediaObject, videoWidget);
95     // connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
96     connect(mediaObject, SIGNAL(finished()), this, SLOT(skip()));
97     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
98             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
99     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
100             this, SLOT(currentSourceChanged(Phonon::MediaSource)));
101     connect(mediaObject, SIGNAL(bufferStatus(int)), loadingWidget, SLOT(bufferStatus(int)));
102 }
103
104 void MediaView::search(SearchParams *searchParams) {
105     this->searchParams = searchParams;
106
107     // start serching for videos
108     listModel->search(searchParams);
109
110     // this implies that the enum and the bar action order is the same
111     sortBar->setCheckedAction(searchParams->sortBy()-1);
112
113     listView->setFocus();
114
115     loadingWidget->clear();
116 }
117
118 void MediaView::disappear() {
119     timerPlayFlag = true;
120 }
121
122 void MediaView::stateChanged(Phonon::State newState, Phonon::State /*oldState*/)
123 {
124
125     // qDebug() << "Phonon state: " << newState << oldState;
126
127     switch (newState) {
128
129          case Phonon::ErrorState:
130         qDebug() << "Phonon error:" << mediaObject->errorString() << mediaObject->errorType();
131         // recover from errors by skipping to the next video
132         skip();
133         break;
134
135          case Phonon::PlayingState:
136         qDebug("playing");
137         videoAreaWidget->showVideo();
138         break;
139
140          case Phonon::StoppedState:
141         qDebug("stopped");
142         // play() has already been called when setting the source
143         // but Phonon on Linux needs a little more help to start playback
144         mediaObject->play();
145
146         // Workaround for Mac playback start problem
147         if (!timerPlayFlag) {
148             QTimer::singleShot(1000, this, SLOT(timerPlay()));
149         }
150
151         break;
152
153          case Phonon::PausedState:
154         qDebug("paused");
155         break;
156
157          case Phonon::BufferingState:
158         qDebug("buffering");
159         break;
160
161          case Phonon::LoadingState:
162         qDebug("loading");
163         break;
164
165          default:
166         ;
167     }
168 }
169
170 void MediaView::pause() {
171     switch( mediaObject->state() ) {
172     case Phonon::PlayingState:
173         mediaObject->pause();
174         break;
175     default:
176         mediaObject->play();
177         break;
178     }
179 }
180
181 void MediaView::stop() {
182     listModel->abortSearch();
183     mediaObject->stop();
184     mediaObject->clear();
185 }
186
187 void MediaView::activeRowChanged(int row) {
188     Video *video = listModel->videoAt(row);
189     if (!video) return;
190
191     // immediately show the loading widget
192     videoAreaWidget->showLoading(video);
193
194     // mediaObject->pause();
195
196     connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl)));
197     // TODO handle signal in a proper slot and impl item error status
198     connect(video, SIGNAL(errorStreamUrl()), SLOT(skip()));
199     video->loadStreamUrl();
200
201     // reset the timer flag
202     timerPlayFlag = false;
203
204     // video title in the statusbar
205     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
206     if (mainWindow) mainWindow->statusBar()->showMessage(video->title());
207
208     // see you in gotStreamUrl...
209
210 }
211
212 void MediaView::gotStreamUrl(QUrl streamUrl) {
213
214     // go!
215     mediaObject->setCurrentSource(streamUrl);
216     mediaObject->play();
217
218     // ensure we always have 10 videos ahead
219     listModel->searchNeeded();
220
221     // ensure active item is visible
222     int row = listModel->activeRow();
223     if (row != -1) {
224         QModelIndex index = listModel->index(row, 0, QModelIndex());
225         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
226     }
227
228 }
229
230 void MediaView::itemActivated(const QModelIndex &index) {
231     if (listModel->rowExists(index.row()))
232         listModel->setActiveRow(index.row());
233     // the user doucleclicked on the "Search More" item
234     else listModel->searchMore();
235 }
236
237 void MediaView::aboutToFinish() {
238     /*
239     int nextRow = listModel->nextRow();
240     if (nextRow == -1) return;
241     Video* video = listModel->videoAt(nextRow);
242     QUrl streamUrl = video->streamUrl();
243     qDebug() << "Enqueing" << streamUrl;
244     mediaObject->enqueue(streamUrl);
245     */
246 }
247
248 void MediaView::currentSourceChanged(const Phonon::MediaSource source) {
249     qDebug() << "Source changed:" << source.url();
250 }
251
252 void MediaView::skip() {
253     int nextRow = listModel->nextRow();
254     if (nextRow == -1) return;
255     listModel->setActiveRow(nextRow);
256 }
257
258 void MediaView::openWebPage() {
259     Video* video = listModel->activeVideo();
260     if (!video) return;
261     mediaObject->pause();
262     QDesktopServices::openUrl(video->webpage());
263 }
264
265 void MediaView::removeSelected() {
266     if (!listView->selectionModel()->hasSelection()) return;
267     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
268     listModel->removeIndexes(indexes);
269 }
270
271 void MediaView::selectVideos(QList<Video*> videos) {
272     foreach (Video *video, videos) {
273         QModelIndex index = listModel->indexForVideo(video);
274         listView->selectionModel()->select(index, QItemSelectionModel::Select);
275         listView->scrollTo(index, QAbstractItemView::EnsureVisible);
276     }
277 }
278
279 void MediaView::selectionChanged(const QItemSelection & /*selected*/, const QItemSelection & /*deselected*/) {
280     const bool gotSelection = listView->selectionModel()->hasSelection();
281     The::globalActions()->value("remove")->setEnabled(gotSelection);
282     The::globalActions()->value("moveUp")->setEnabled(gotSelection);
283     The::globalActions()->value("moveDown")->setEnabled(gotSelection);
284 }
285
286 void MediaView::moveUpSelected() {
287     if (!listView->selectionModel()->hasSelection()) return;
288     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
289     listModel->move(indexes, true);
290 }
291
292 void MediaView::moveDownSelected() {
293     if (!listView->selectionModel()->hasSelection()) return;
294     QModelIndexList indexes = listView->selectionModel()->selectedIndexes();
295     listModel->move(indexes, false);
296 }
297
298 void MediaView::showVideoContextMenu(QPoint point) {
299     The::globalMenus()->value("video")->popup(videoWidget->mapToGlobal(point));
300 }
301
302 void MediaView::searchMostRelevant() {
303     searchParams->setSortBy(SearchParams::SortByRelevance);
304     search(searchParams);
305 }
306
307 void MediaView::searchMostRecent() {
308     searchParams->setSortBy(SearchParams::SortByNewest);
309     search(searchParams);
310 }
311
312 void MediaView::searchMostViewed() {
313     searchParams->setSortBy(SearchParams::SortByViewCount);
314     search(searchParams);
315 }
316
317 void MediaView::setPlaylistVisible(bool visible) {
318     playlistWidget->setVisible(visible);
319 }
320
321 void MediaView::timerPlay() {
322     // qDebug() << mediaObject->currentTime();
323     // Workaround Phonon bug on Mac OSX
324     if (mediaObject->currentTime() <= 0 && mediaObject->state() == Phonon::PlayingState) {
325         mediaObject->pause();
326         mediaObject->play();
327     }
328 }