]> git.sur5r.net Git - minitube/blob - src/MainWindow.cpp
Added multimedia hotkeys support for stop, pause and skip actions
[minitube] / src / MainWindow.cpp
1 #include "MainWindow.h"
2 #include "spacer.h"
3 #include "Constants.h"
4 #include "iconloader/qticonloader.h"
5 #include "global.h"
6
7 MainWindow::MainWindow() {
8
9     m_fullscreen = false;
10     mediaObject = 0;
11     audioOutput = 0;
12
13     // views mechanism
14     history = new QStack<QWidget*>();
15     views = new QStackedWidget(this);
16
17     // views
18     searchView = new SearchView(this);
19     connect(searchView, SIGNAL(search(QString)), this, SLOT(showMedia(QString)));
20     views->addWidget(searchView);
21     mediaView = new MediaView(this);
22     views->addWidget(mediaView);
23
24     // lazily initialized views
25     aboutView = 0;
26     settingsView = 0;
27
28     toolbarSearch = new SearchLineEdit(this);
29     toolbarSearch->setFont(qApp->font());
30     connect(toolbarSearch, SIGNAL(search(const QString&)), searchView, SLOT(watch(const QString&)));
31
32     // build ui
33     createActions();
34     createMenus();
35     createToolBars();
36     createStatusBar();
37
38     // remove that useless menu/toolbar context menu
39     this->setContextMenuPolicy(Qt::NoContextMenu);
40
41     // mediaView init stuff thats needs actions
42     mediaView->initialize();
43
44     // restore window position
45     readSettings();
46
47     // show the initial view
48     showWidget(searchView);
49
50     setCentralWidget(views);
51 }
52
53 MainWindow::~MainWindow() {
54     delete history;
55 }
56
57 void MainWindow::createActions() {
58
59     QMap<QString, QAction*> *actions = The::globalActions();
60
61     /*
62     settingsAct = new QAction(tr("&Preferences..."), this);
63     settingsAct->setStatusTip(tr(QString("Configure ").append(Constants::APP_NAME).toUtf8()));
64     // Mac integration
65     settingsAct->setMenuRole(QAction::PreferencesRole);
66     actions->insert("settings", settingsAct);
67     connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
68     */
69     
70     backAct = new QAction(QIcon(":/images/go-previous.png"), tr("&Back"), this);
71     backAct->setEnabled(false);
72     backAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Left));
73     backAct->setStatusTip(tr("Go to the previous view"));
74     actions->insert("back", backAct);
75     connect(backAct, SIGNAL(triggered()), this, SLOT(goBack()));
76
77     stopAct = new QAction(QtIconLoader::icon("media-stop", QIcon(":/images/stop.png")), tr("&Stop"), this);
78     stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
79     stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
80     actions->insert("stop", stopAct);
81     connect(stopAct, SIGNAL(triggered()), this, SLOT(stop()));
82
83     skipAct = new QAction(QtIconLoader::icon("media-skip-forward", QIcon(":/images/skip.png")), tr("S&kip"), this);
84     skipAct->setStatusTip(tr("Skip to the next video"));
85     skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right) << QKeySequence(Qt::Key_MediaNext));
86     skipAct->setEnabled(false);
87     actions->insert("skip", skipAct);
88     connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
89
90     pauseAct = new QAction(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")), tr("&Pause"), this);
91     pauseAct->setStatusTip(tr("Pause playback"));
92     pauseAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
93     pauseAct->setEnabled(false);
94     actions->insert("pause", pauseAct);
95     connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
96
97     fullscreenAct = new QAction(QtIconLoader::icon("view-fullscreen", QIcon(":/images/view-fullscreen.png")), tr("&Full Screen"), this);
98     fullscreenAct->setStatusTip(tr("Go full screen"));
99     fullscreenAct->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Return));
100     fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
101     actions->insert("fullscreen", fullscreenAct);
102     connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
103
104     compactViewAct = new QAction(tr("&Compact mode"), this);
105     compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
106     compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
107     compactViewAct->setCheckable(true);
108     compactViewAct->setChecked(false);
109     compactViewAct->setEnabled(false);
110     actions->insert("compactView", compactViewAct);
111     connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
112
113     /*
114     // icon should be document-save but it is ugly
115     downloadAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("&Download"), this);
116     downloadAct->setStatusTip(tr("Download this video"));
117     downloadAct->setShortcut(tr("Ctrl+S"));
118     downloadAct->setEnabled(false);
119     actions->insert("download", downloadAct);
120     connect(downloadAct, SIGNAL(triggered()), this, SLOT(download()));
121     */
122
123     webPageAct = new QAction(QtIconLoader::icon("internet-web-browser", QIcon(":/images/internet-web-browser.png")), tr("&YouTube"), this);
124     webPageAct->setStatusTip(tr("Open the YouTube video page"));
125     webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
126     webPageAct->setEnabled(false);
127     actions->insert("webpage", webPageAct);
128     connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
129
130     removeAct = new QAction(tr("&Remove"), this);
131     removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
132     removeAct->setShortcuts(QList<QKeySequence>() << QKeySequence("Del") << QKeySequence("Backspace"));
133     removeAct->setEnabled(false);
134     actions->insert("remove", removeAct);
135     connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
136
137     moveUpAct = new QAction(QtIconLoader::icon("go-up", QIcon(":/images/go-up.png")), tr("Move &Up"), this);
138     moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
139     moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
140     moveUpAct->setEnabled(false);
141     actions->insert("moveUp", moveUpAct);
142     connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
143
144     moveDownAct = new QAction(QtIconLoader::icon("go-down", QIcon(":/images/go-down.png")), tr("Move &Down"), this);
145     moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
146     moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
147     moveDownAct->setEnabled(false);
148     actions->insert("moveDown", moveDownAct);
149     connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
150
151     quitAct = new QAction(tr("&Quit"), this);
152     quitAct->setMenuRole(QAction::QuitRole);
153     quitAct->setShortcut(tr("Ctrl+Q"));
154     quitAct->setStatusTip(tr("Bye"));
155     actions->insert("quit", quitAct);
156     connect(quitAct, SIGNAL(triggered()), this, SLOT(quit()));
157
158     siteAct = new QAction(tr("&Website"), this);
159     siteAct->setShortcut(QKeySequence::HelpContents);
160     siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::APP_NAME));
161     actions->insert("site", siteAct);
162     connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
163
164     donateAct = new QAction(tr("&Donate via PayPal"), this);
165     donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::APP_NAME));
166     actions->insert("donate", donateAct);
167     connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
168
169     aboutAct = new QAction(tr("&About"), this);
170     aboutAct->setMenuRole(QAction::AboutRole);
171     aboutAct->setStatusTip(tr("Info about %1").arg(Constants::APP_NAME));
172     actions->insert("about", aboutAct);
173     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
174
175     // Invisible actions
176
177     searchFocusAct = new QAction(this);
178     searchFocusAct->setShortcut(QKeySequence::Find);
179     actions->insert("search", searchFocusAct);
180     connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
181     addAction(searchFocusAct);
182
183     volumeUpAct = new QAction(this);
184     volumeUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus));
185     actions->insert("volume-up", volumeUpAct);
186     connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
187     addAction(volumeUpAct);
188
189     volumeDownAct = new QAction(this);
190     volumeDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus));
191     actions->insert("volume-down", volumeDownAct);
192     connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
193     addAction(volumeDownAct);
194
195     volumeMuteAct = new QAction(this);
196     volumeMuteAct->setShortcut(tr("Ctrl+M"));
197     actions->insert("volume-mute", volumeMuteAct);
198     connect(volumeMuteAct, SIGNAL(triggered()), this, SLOT(volumeMute()));
199     addAction(volumeMuteAct);
200
201     // common action properties
202     foreach (QAction *action, actions->values()) {
203
204         // add actions to the MainWindow so that they work
205         // when the menu is hidden
206         addAction(action);
207
208         // never autorepeat.
209         // unexperienced users tend to keep keys pressed for a "long" time
210         action->setAutoRepeat(false);
211         action->setToolTip(action->statusTip());
212
213         // make the actions work when video is fullscreen
214         action->setShortcutContext(Qt::ApplicationShortcut);
215
216 #ifdef Q_WS_MAC
217         // OSX does not use icons in menus
218         action->setIconVisibleInMenu(false);
219 #endif
220
221     }
222
223 }
224
225 void MainWindow::createMenus() {
226
227     QMap<QString, QMenu*> *menus = The::globalMenus();
228
229     fileMenu = menuBar()->addMenu(tr("&Application"));
230     // menus->insert("file", fileMenu);
231     // fileMenu->addAction(settingsAct);
232     fileMenu->addSeparator();
233     fileMenu->addAction(quitAct);
234
235     playlistMenu = menuBar()->addMenu(tr("&Playlist"));
236     menus->insert("playlist", playlistMenu);
237     playlistMenu->addAction(removeAct);
238     playlistMenu->addSeparator();
239     playlistMenu->addAction(moveUpAct);
240     playlistMenu->addAction(moveDownAct);
241
242     viewMenu = menuBar()->addMenu(tr("&Video"));
243     menus->insert("video", viewMenu);
244     // viewMenu->addAction(backAct);
245     viewMenu->addAction(stopAct);
246     viewMenu->addAction(pauseAct);
247     viewMenu->addAction(skipAct);
248     viewMenu->addSeparator();
249     viewMenu->addAction(webPageAct);
250     viewMenu->addSeparator();
251     // viewMenu->addAction(downloadAct);
252     viewMenu->addAction(compactViewAct);
253     viewMenu->addAction(fullscreenAct);
254
255     helpMenu = menuBar()->addMenu(tr("&Help"));
256     helpMenu->addAction(siteAct);
257     helpMenu->addAction(donateAct);
258     helpMenu->addAction(aboutAct);
259 }
260
261 void MainWindow::createToolBars() {
262
263     setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
264
265     mainToolBar = new QToolBar(this);
266     mainToolBar->setFloatable(false);
267     mainToolBar->setMovable(false);
268
269     QFont smallerFont;
270     smallerFont.setPointSize(smallerFont.pointSize()*.85);
271     mainToolBar->setFont(smallerFont);
272
273     mainToolBar->setIconSize(QSize(32,32));
274     // mainToolBar->addAction(backAct);
275     mainToolBar->addAction(stopAct);
276     mainToolBar->addAction(pauseAct);
277     mainToolBar->addAction(skipAct);
278     mainToolBar->addAction(fullscreenAct);
279
280     seekSlider = new Phonon::SeekSlider(this);
281     seekSlider->setIconVisible(false);
282     Spacer *seekSliderSpacer = new Spacer(mainToolBar, seekSlider);
283     seekSliderSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
284     mainToolBar->addWidget(seekSliderSpacer);
285
286     volumeSlider = new Phonon::VolumeSlider(this);
287     // this makes the volume slider smaller
288     volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
289     mainToolBar->addWidget(new Spacer(mainToolBar, volumeSlider));
290
291     mainToolBar->addWidget(new Spacer(mainToolBar, toolbarSearch));
292
293     addToolBar(mainToolBar);
294 }
295
296 void MainWindow::createStatusBar() {
297     currentTime = new QLabel(this);
298     statusBar()->addPermanentWidget(currentTime);
299
300     totalTime = new QLabel(this);
301     statusBar()->addPermanentWidget(totalTime);
302
303     // remove ugly borders on OSX
304     statusBar()->setStyleSheet("::item{border:0 solid}");
305
306     statusBar()->show();
307 }
308
309 void MainWindow::readSettings() {
310     QSettings settings;
311     restoreGeometry(settings.value("geometry").toByteArray());
312 }
313
314 void MainWindow::writeSettings() {
315     // do not save geometry when in full screen
316     if (m_fullscreen)
317         return;
318     QSettings settings;
319     settings.setValue("geometry", saveGeometry());
320 }
321
322 void MainWindow::goBack() {
323     if ( history->size() > 1 ) {
324         history->pop();
325         QWidget *widget = history->pop();
326         showWidget(widget);
327     }
328 }
329
330 void MainWindow::showWidget ( QWidget* widget ) {
331
332     setUpdatesEnabled(false);
333
334     // call hide method on the current view
335     View* oldView = dynamic_cast<View *> (views->currentWidget());
336     if (oldView != NULL) {
337         oldView->disappear();
338     }
339
340     // call show method on the new view
341     View* newView = dynamic_cast<View *> (widget);
342     if (newView != NULL) {
343         newView->appear();
344         QMap<QString,QVariant> metadata = newView->metadata();
345         QString windowTitle = metadata.value("title").toString();
346         if (windowTitle.length())
347             windowTitle += " - ";
348         setWindowTitle(windowTitle + Constants::APP_NAME);
349         statusBar()->showMessage((metadata.value("description").toString()));
350
351     }
352
353     // backAct->setEnabled(history->size() > 1);
354     // settingsAct->setEnabled(widget != settingsView);
355     stopAct->setEnabled(widget == mediaView);
356     fullscreenAct->setEnabled(widget == mediaView);
357     compactViewAct->setEnabled(widget == mediaView);
358     webPageAct->setEnabled(widget == mediaView);
359     aboutAct->setEnabled(widget != aboutView);
360
361     /*
362     // this is not the best place to enable downloads, but the user is informed
363     // if there really is no video is playing
364     downloadAct->setEnabled(widget == mediaView);
365     */
366
367     // cool toolbar on the Mac
368     // setUnifiedTitleAndToolBarOnMac(widget == mediaView);
369
370     // toolbar only for the mediaView
371     mainToolBar->setVisible(widget == mediaView && !compactViewAct->isChecked());
372
373     history->push(widget);
374
375 #ifdef Q_WS_MAC
376     // crossfade only on OSX
377     // where we can be sure of video performance
378     fadeInWidget(views->currentWidget(), widget);
379 #endif
380
381     views->setCurrentWidget(widget);
382
383     setUpdatesEnabled(true);
384 }
385
386 void MainWindow::fadeInWidget(QWidget *oldWidget, QWidget *newWidget) {
387     if (faderWidget) faderWidget->close();
388     if (!oldWidget || !newWidget || oldWidget == mediaView || newWidget == mediaView) return;
389     QPixmap frozenView = QPixmap::grabWidget(oldWidget);
390     faderWidget = new FaderWidget(newWidget);
391     faderWidget->start(frozenView);
392 }
393
394 void MainWindow::about() {
395     if (!aboutView) {
396         aboutView = new AboutView(this);
397         views->addWidget(aboutView);
398     }
399     showWidget(aboutView);
400 }
401
402 void MainWindow::visitSite() {
403     QUrl url(Constants::WEBSITE);
404     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
405     QDesktopServices::openUrl(url);
406 }
407
408 void MainWindow::donate() {
409     QUrl url(QString(Constants::WEBSITE) + "#donate");
410     statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
411     QDesktopServices::openUrl(url);
412 }
413
414 void MainWindow::quit() {
415     writeSettings();
416     qApp->quit();
417 }
418
419 void MainWindow::closeEvent(QCloseEvent *event) {
420     quit();
421     QWidget::closeEvent(event);
422 }
423
424 void MainWindow::showSettings() {
425     if (!settingsView) {
426         settingsView = new SettingsView(this);
427         views->addWidget(settingsView);
428     }
429     showWidget(settingsView);
430 }
431
432 void MainWindow::showSearch() {
433     showWidget(searchView);
434     currentTime->clear();
435     totalTime->clear();
436 }
437
438 void MainWindow::showMedia(QString query) {
439     initPhonon();
440     mediaView->setMediaObject(mediaObject);
441     SearchParams *searchParams = new SearchParams();
442     searchParams->setKeywords(query);
443     mediaView->search(searchParams);
444     showWidget(mediaView);
445 }
446
447 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
448
449     // qDebug() << "Phonon state: " << newState;
450
451     switch (newState) {
452
453          case Phonon::ErrorState:
454         if (mediaObject->errorType() == Phonon::FatalError) {
455             statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
456         } else {
457             statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
458         }
459         break;
460
461          case Phonon::PlayingState:
462         pauseAct->setEnabled(true);
463         pauseAct->setIcon(QtIconLoader::icon("media-pause", QIcon(":/images/pause.png")));
464         pauseAct->setText(tr("&Pause"));
465         pauseAct->setStatusTip(tr("Pause playback"));
466         skipAct->setEnabled(true);
467         break;
468
469          case Phonon::StoppedState:
470         pauseAct->setEnabled(false);
471         skipAct->setEnabled(false);
472         break;
473
474          case Phonon::PausedState:
475         skipAct->setEnabled(true);
476         pauseAct->setEnabled(true);
477         pauseAct->setIcon(QtIconLoader::icon("media-play", QIcon(":/images/play.png")));
478         pauseAct->setText(tr("&Play"));
479         pauseAct->setStatusTip(tr("Resume playback"));
480         break;
481
482          case Phonon::BufferingState:
483          case Phonon::LoadingState:
484         skipAct->setEnabled(true);
485         pauseAct->setEnabled(false);
486         currentTime->clear();
487         totalTime->clear();
488         break;
489
490          default:
491         ;
492     }
493 }
494
495 void MainWindow::stop() {
496     mediaView->stop();
497     showSearch();
498 }
499
500 void MainWindow::fullscreen() {
501
502     setUpdatesEnabled(false);
503
504     if (m_fullscreen) {
505         // use setShortucs instead of setShortcut
506         // the latter seems not to work
507         fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::ALT + Qt::Key_Return));
508         fullscreenAct->setText(tr("&Full Screen"));
509         stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
510         if (m_maximized) showMaximized();
511         else showNormal();
512     } else {
513         stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
514         fullscreenAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::ALT + Qt::Key_Return));
515         fullscreenAct->setText(tr("Exit &Full Screen"));
516         m_maximized = isMaximized();
517
518         // save geometry now, if the user quits when in full screen
519         // geometry won't be saved
520         writeSettings();
521
522         showFullScreen();
523     }
524
525     // No compact view action when in full screen
526     compactViewAct->setVisible(m_fullscreen);
527     // Also no Youtube action since it opens a new window
528     webPageAct->setVisible(m_fullscreen);
529
530     // Hide anything but the video
531     mediaView->setPlaylistVisible(m_fullscreen);
532     mainToolBar->setVisible(m_fullscreen);
533     statusBar()->setVisible(m_fullscreen);
534     menuBar()->setVisible(m_fullscreen);
535
536     m_fullscreen = !m_fullscreen;
537
538     setUpdatesEnabled(true);
539 }
540
541 void MainWindow::compactView(bool enable) {
542
543     setUpdatesEnabled(false);
544
545     // setUnifiedTitleAndToolBarOnMac(!enable);
546     mediaView->setPlaylistVisible(!enable);
547     mainToolBar->setVisible(!enable);
548     statusBar()->setVisible(!enable);
549
550     // ensure focus does not end up to the search box
551     // as it would steal the Space shortcut
552     toolbarSearch->setEnabled(!enable);
553
554     if (enable) {
555         stopAct->setShortcut(QString(""));
556         QList<QKeySequence> shortcuts;
557         // for some reason it is important that ESC comes first
558         shortcuts << QKeySequence(Qt::CTRL + Qt::Key_Return) << QKeySequence(Qt::Key_Escape);
559         compactViewAct->setShortcuts(shortcuts);
560     } else {
561         compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
562         stopAct->setShortcut(QKeySequence(Qt::Key_Escape));
563     }
564
565     setUpdatesEnabled(true);
566 }
567
568 void MainWindow::searchFocus() {
569     QWidget *view = views->currentWidget();
570     if (view == mediaView) {
571         toolbarSearch->setFocus();
572     }
573 }
574
575 void MainWindow::initPhonon() {
576     // Phonon initialization
577     if (mediaObject) delete mediaObject;
578     if (audioOutput) delete audioOutput;
579     mediaObject = new Phonon::MediaObject(this);
580     mediaObject->setTickInterval(100);
581     connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
582             this, SLOT(stateChanged(Phonon::State, Phonon::State)));
583     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
584     connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
585     seekSlider->setMediaObject(mediaObject);
586     audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
587     connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal)));
588     connect(audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(volumeMutedChanged(bool)));
589     volumeSlider->setAudioOutput(audioOutput);
590     Phonon::createPath(mediaObject, audioOutput);
591 }
592
593 void MainWindow::tick(qint64 time) {
594     if (time <= 0) {
595         currentTime->clear();
596         return;
597     }
598     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
599     currentTime->setText(displayTime.toString("mm:ss"));
600     // qDebug() << "currentTime" << time << displayTime.toString("mm:ss");
601 }
602
603 void MainWindow::totalTimeChanged(qint64 time) {
604     if (time <= 0) {
605         totalTime->clear();
606         return;
607     }
608     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
609     totalTime->setText(displayTime.toString("/ mm:ss"));
610     // qDebug() << "totalTime" << time << displayTime.toString("mm:ss");
611 }
612
613 void MainWindow::volumeUp() {
614     qreal newVolume = volumeSlider->audioOutput()->volume() + .1;
615     if (newVolume > volumeSlider->maximumVolume())
616         newVolume = volumeSlider->maximumVolume();
617     volumeSlider->audioOutput()->setVolume(newVolume);
618 }
619
620 void MainWindow::volumeDown() {
621     qreal newVolume = volumeSlider->audioOutput()->volume() - .1;
622     if (newVolume < 0)
623         newVolume = 0;
624     volumeSlider->audioOutput()->setVolume(newVolume);
625 }
626
627 void MainWindow::volumeMute() {
628     volumeSlider->audioOutput()->setMuted(!volumeSlider->audioOutput()->isMuted());
629 }
630
631 void MainWindow::volumeChanged(qreal newVolume) {
632     // automatically unmute when volume changes
633     if (volumeSlider->audioOutput()->isMuted())
634         volumeSlider->audioOutput()->setMuted(false);
635     statusBar()->showMessage(tr("Volume at %1%").arg(newVolume*100));
636 }
637
638 void MainWindow::volumeMutedChanged(bool muted) {
639     if (muted)
640         statusBar()->showMessage(tr("Volume is muted"));
641     else
642         statusBar()->showMessage(tr("Volume is unmuted"));
643 }
644
645 /*
646 void MainWindow::abortDownload() {
647     QProgressDialog* dlg = dynamic_cast<QProgressDialog*>(this->sender());
648     QMap<QNetworkReply*, DownloadResource>::iterator cur;
649     QMap<QNetworkReply*, DownloadResource>::iterator end;
650     // locate the DownloadResource by its dialog address and trigger abortion
651     for(cur=m_downloads.begin(), end=m_downloads.end(); cur!=end; cur++){
652         if(cur.value().dialog == dlg) cur.key()->abort();
653     }
654 }
655
656 void MainWindow::download() {
657     if(mediaObject == NULL || mediaObject->currentSource().url().isEmpty()){
658         // complain unless video source apperas to be valid
659         QMessageBox::critical(this, tr("No Video playing"), tr("You must first play the video you intent to download !"));
660         return;
661     }
662     QString filename = QFileDialog::getSaveFileName(this,
663                                                     tr("Save video as..."),
664                                                     tr("minitube video.mp4"),
665                                                     "Video File(*.avi *.mp4)"
666                                                     );
667     if(!filename.isNull()) {
668         // open destination file and initialize download
669         DownloadResource res;
670         res.file = new QFile(filename);
671         if(res.file->open(QFile::WriteOnly) == true) {
672             res.dialog = new QProgressDialog(tr("Downloading: ") + res.file->fileName(),
673                                              tr("Abort Download"), 0, 100, this);
674             connect(res.dialog, SIGNAL(canceled()), this, SLOT(abortDownload()));
675             download(mediaObject->currentSource().url(), res);
676         }else{
677             QMessageBox::critical(this, tr("File creation failed"), res.file->errorString());
678             delete res.file;
679         }
680     }
681 }
682
683 void MainWindow::download(const QUrl& url, const DownloadResource& res) {
684     // create and store request and connect the reply signals
685     QNetworkReply *r = The::networkAccessManager()->get(QNetworkRequest(url));
686     m_downloads.insert(r, res);
687     connect(r, SIGNAL(finished()), this, SLOT(replyFinished()));
688     connect(r, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
689     connect(r, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(replyError(QNetworkReply::NetworkError)));
690     connect(r, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(replyDownloadProgress(qint64,qint64)));
691     connect(r, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
692 }
693
694 void MainWindow::replyReadyRead() {
695     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
696     m_downloads[r].file->write(r->readAll());
697 }
698
699 void MainWindow::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
700     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
701     if (bytesTotal > 0 && bytesReceived >0)
702         m_downloads[r].dialog->setValue( double(100.0/bytesTotal)*bytesReceived );  // pssst :-X
703 }
704
705 void MainWindow::replyError(QNetworkReply::NetworkError code) {
706     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
707     QMessageBox::critical(this, tr("Download failed"), r->errorString());
708 }
709
710 void MainWindow::replyFinished() {
711     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
712     m_downloads[r].dialog->close();
713     m_downloads[r].file->close();
714     delete m_downloads[r].file;
715     m_downloads.remove(r);
716 }
717
718 void MainWindow::replyMetaDataChanged() {
719     QNetworkReply* r = dynamic_cast<QNetworkReply*>(this->sender());
720     QUrl url = r->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
721     if(url.isValid()) {
722         // redirect - request new url, but keep the resources
723         qDebug() << "redirecting to: " << url.toString();
724         download(url, m_downloads[r]);
725         m_downloads.remove(r);
726     }
727 }
728
729 */