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