3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "mainwindow.h"
23 #include "searchview.h"
24 #include "mediaview.h"
25 #include "aboutview.h"
26 #include "downloadview.h"
28 #include "constants.h"
29 #include "iconutils.h"
31 #include "videodefinition.h"
32 #include "fontutils.h"
33 #include "globalshortcuts.h"
34 #include "searchparams.h"
35 #include "videosource.h"
38 #include "gnomeglobalshortcutbackend.h"
41 #include "mac_startup.h"
42 #include "macfullscreen.h"
43 #include "macsupport.h"
46 #include "downloadmanager.h"
47 #include "ytsuggester.h"
48 #include "updatechecker.h"
49 #include "temporary.h"
51 #include "searchlineedit_mac.h"
53 #include "searchlineedit.h"
58 #include "updatedialog.h"
61 #include "activation.h"
62 #include "activationview.h"
63 #include "activationdialog.h"
65 #include "ytregions.h"
66 #include "regionsview.h"
67 #include "standardfeedsview.h"
68 #include "channelaggregator.h"
70 #include "videoareawidget.h"
71 #include "jsfunctions.h"
72 #include "seekslider.h"
78 static MainWindow *singleton = 0;
81 MainWindow* MainWindow::instance() {
85 MainWindow::MainWindow() :
100 Extra::windowSetup(this);
104 history = new QStack<QWidget*>();
105 views = new QStackedWidget();
107 setCentralWidget(views);
110 homeView = new HomeView();
111 views->addWidget(homeView);
113 // TODO make this lazy
114 mediaView = MediaView::instance();
115 mediaView->setEnabled(false);
116 views->addWidget(mediaView);
124 // remove that useless menu/toolbar context menu
125 this->setContextMenuPolicy(Qt::NoContextMenu);
127 // event filter to block ugly toolbar tooltips
128 qApp->installEventFilter(this);
130 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
132 // restore window position
135 // fix stacked widget minimum size
136 for (int i = 0; i < views->count(); i++)
137 views->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
140 // show the initial view
143 #ifdef APP_ACTIVATION
144 if (!Activation::instance().isActivated())
145 showActivationView(false);
150 qApp->processEvents();
151 QTimer::singleShot(50, this, SLOT(lazyInit()));
154 MainWindow::~MainWindow() {
158 void MainWindow::lazyInit() {
162 mediaView->initialize();
164 mediaView->setMediaObject(mediaObject);
166 qApp->processEvents();
169 if (qApp->arguments().size() > 1) {
170 QString query = qApp->arguments().at(1);
171 if (query.startsWith(QLatin1String("--"))) {
172 messageReceived(query);
175 SearchParams *searchParams = new SearchParams();
176 searchParams->setKeywords(query);
177 showMedia(searchParams);
182 GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
184 if (GnomeGlobalShortcutBackend::IsGsdAvailable())
185 shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
190 connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
191 connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
192 connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
193 connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
194 // connect(&shortcuts, SIGNAL(StopAfter()), The::globalActions()->value("stopafterthis"), SLOT(toggle()));
196 connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
197 SLOT(updateDownloadMessage(QString)));
198 connect(DownloadManager::instance(), SIGNAL(finished()),
199 SLOT(downloadsFinished()));
201 setAcceptDrops(true);
203 mouseTimer = new QTimer(this);
204 mouseTimer->setInterval(5000);
205 mouseTimer->setSingleShot(true);
206 connect(mouseTimer, SIGNAL(timeout()), SLOT(hideMouse()));
208 JsFunctions::instance();
213 void MainWindow::changeEvent(QEvent* event) {
215 if (event->type() == QEvent::WindowStateChange) {
216 The::globalActions()->value("minimize")->setEnabled(!isMinimized());
219 QMainWindow::changeEvent(event);
222 bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
224 if (m_fullscreen && event->type() == QEvent::MouseMove) {
226 QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
227 const int x = mouseEvent->pos().x();
228 const QString className = QString(obj->metaObject()->className());
229 const bool isHoveringVideo =
230 (className == QLatin1String("QGLWidget")) ||
231 (className == QLatin1String("VideoAreaWidget"));
233 // qDebug() << obj << mouseEvent->pos() << isHoveringVideo << mediaView->isPlaylistVisible();
235 if (mediaView && mediaView->isPlaylistVisible()) {
236 if (isHoveringVideo && x > 5) mediaView->setPlaylistVisible(false);
238 if (isHoveringVideo && x >= 0 && x < 5) mediaView->setPlaylistVisible(true);
242 const int y = mouseEvent->pos().y();
243 if (mainToolBar->isVisible()) {
244 if (isHoveringVideo && y > 5) mainToolBar->setVisible(false);
246 if (isHoveringVideo && y >= 0 && y < 5) mainToolBar->setVisible(true);
250 // show the normal cursor
252 // then hide it again after a few seconds
257 if (event->type() == QEvent::ToolTip) {
261 // standard event processing
262 return QMainWindow::eventFilter(obj, event);
265 void MainWindow::createActions() {
267 QHash<QString, QAction*> *actions = The::globalActions();
269 stopAct = new QAction(IconUtils::icon("media-playback-stop"), tr("&Stop"), this);
270 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
271 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
272 stopAct->setEnabled(false);
273 actions->insert("stop", stopAct);
274 connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
276 skipBackwardAct = new QAction(
277 IconUtils::icon("media-skip-backward"),
278 tr("P&revious"), this);
279 skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
280 skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
281 skipBackwardAct->setEnabled(false);
282 actions->insert("previous", skipBackwardAct);
283 connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
285 skipAct = new QAction(IconUtils::icon("media-skip-forward"), tr("S&kip"), this);
286 skipAct->setStatusTip(tr("Skip to the next video"));
287 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right) << QKeySequence(Qt::Key_MediaNext));
288 skipAct->setEnabled(false);
289 actions->insert("skip", skipAct);
290 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
292 pauseAct = new QAction(IconUtils::icon("media-playback-start"), tr("&Play"), this);
293 pauseAct->setStatusTip(tr("Resume playback"));
294 pauseAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
295 pauseAct->setEnabled(false);
296 actions->insert("pause", pauseAct);
297 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
299 fullscreenAct = new QAction(IconUtils::icon("view-fullscreen"), tr("&Full Screen"), this);
300 fullscreenAct->setStatusTip(tr("Go full screen"));
301 QList<QKeySequence> fsShortcuts;
303 fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
305 fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
307 fullscreenAct->setShortcuts(fsShortcuts);
308 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
309 fullscreenAct->setPriority(QAction::LowPriority);
310 actions->insert("fullscreen", fullscreenAct);
311 connect(fullscreenAct, SIGNAL(triggered()), this, SLOT(fullscreen()));
313 compactViewAct = new QAction(tr("&Compact Mode"), this);
314 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
316 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::META + Qt::Key_C));
318 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
320 compactViewAct->setCheckable(true);
321 compactViewAct->setChecked(false);
322 compactViewAct->setEnabled(false);
323 actions->insert("compactView", compactViewAct);
324 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
326 webPageAct = new QAction(tr("Open the &YouTube Page"), this);
327 webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
328 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
329 webPageAct->setEnabled(false);
330 actions->insert("webpage", webPageAct);
331 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
333 copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
334 copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
335 copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
336 copyPageAct->setEnabled(false);
337 actions->insert("pagelink", copyPageAct);
338 connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
340 copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
341 copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
342 copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
343 copyLinkAct->setEnabled(false);
344 actions->insert("videolink", copyLinkAct);
345 connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
347 findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
348 findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
349 findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
350 findVideoPartsAct->setEnabled(false);
351 connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
352 actions->insert("findVideoParts", findVideoPartsAct);
354 removeAct = new QAction(tr("&Remove"), this);
355 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
356 removeAct->setShortcuts(QList<QKeySequence>() << QKeySequence("Del") << QKeySequence("Backspace"));
357 removeAct->setEnabled(false);
358 actions->insert("remove", removeAct);
359 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
361 moveUpAct = new QAction(tr("Move &Up"), this);
362 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
363 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
364 moveUpAct->setEnabled(false);
365 actions->insert("moveUp", moveUpAct);
366 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
368 moveDownAct = new QAction(tr("Move &Down"), this);
369 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
370 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
371 moveDownAct->setEnabled(false);
372 actions->insert("moveDown", moveDownAct);
373 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
375 clearAct = new QAction(tr("&Clear Recent Searches"), this);
376 clearAct->setMenuRole(QAction::ApplicationSpecificRole);
377 clearAct->setShortcuts(QList<QKeySequence>()
378 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
379 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
380 clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
381 clearAct->setEnabled(true);
382 actions->insert("clearRecentKeywords", clearAct);
383 connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
385 quitAct = new QAction(tr("&Quit"), this);
386 quitAct->setMenuRole(QAction::QuitRole);
387 quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
388 quitAct->setStatusTip(tr("Bye"));
389 actions->insert("quit", quitAct);
390 connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
392 siteAct = new QAction(tr("&Website"), this);
393 siteAct->setShortcut(QKeySequence::HelpContents);
394 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
395 actions->insert("site", siteAct);
396 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
398 #if !defined(APP_MAC) && !defined(APP_WIN)
399 donateAct = new QAction(tr("Make a &Donation"), this);
400 donateAct->setStatusTip(tr("Please support the continued development of %1").arg(Constants::NAME));
401 actions->insert("donate", donateAct);
402 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
405 aboutAct = new QAction(tr("&About"), this);
406 aboutAct->setMenuRole(QAction::AboutRole);
407 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
408 actions->insert("about", aboutAct);
409 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
413 searchFocusAct = new QAction(this);
414 searchFocusAct->setShortcut(QKeySequence::Find);
415 searchFocusAct->setStatusTip(tr("Search"));
416 actions->insert("search", searchFocusAct);
417 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
418 addAction(searchFocusAct);
420 volumeUpAct = new QAction(this);
421 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
422 actions->insert("volume-up", volumeUpAct);
423 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
424 addAction(volumeUpAct);
426 volumeDownAct = new QAction(this);
427 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
428 actions->insert("volume-down", volumeDownAct);
429 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
430 addAction(volumeDownAct);
432 volumeMuteAct = new QAction(this);
433 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
434 volumeMuteAct->setStatusTip(tr("Mute volume"));
435 volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
436 actions->insert("volume-mute", volumeMuteAct);
437 connect(volumeMuteAct, SIGNAL(triggered()), SLOT(volumeMute()));
438 addAction(volumeMuteAct);
440 QAction *definitionAct = new QAction(this);
442 definitionAct->setIcon(IconUtils::tintedIcon("video-display", QColor(0, 0, 0),
443 QList<QSize>() << QSize(16, 16)));
445 definitionAct->setIcon(IconUtils::icon("video-display"));
447 definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
449 QMenu *definitionMenu = new QMenu(this);
450 foreach (QString definition, VideoDefinition::getDefinitionNames()) {
451 definitionMenu->addAction(definition);
453 definitionAct->setMenu(definitionMenu);
455 actions->insert("definition", definitionAct);
456 connect(definitionAct, SIGNAL(triggered()), SLOT(toggleDefinitionMode()));
457 addAction(definitionAct);
461 action = new QAction(IconUtils::icon("media-playback-start"), tr("&Manually Start Playing"), this);
462 action->setStatusTip(tr("Manually start playing videos"));
463 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
464 action->setCheckable(true);
465 connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
466 actions->insert("manualplay", action);
468 action = new QAction(tr("&Downloads"), this);
469 action->setStatusTip(tr("Show details about video downloads"));
470 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
471 action->setCheckable(true);
472 action->setIcon(IconUtils::icon("document-save"));
473 action->setVisible(false);
474 connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
475 actions->insert("downloads", action);
477 action = new QAction(tr("&Download"), this);
478 action->setStatusTip(tr("Download the current video"));
479 action->setShortcut(QKeySequence::Save);
480 action->setIcon(IconUtils::icon("document-save"));
481 action->setEnabled(false);
482 action->setVisible(false);
483 action->setPriority(QAction::LowPriority);
484 connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
485 actions->insert("download", action);
488 action = new QAction(tr("Take &Snapshot"), this);
489 action->setShortcut(QKeySequence(Qt::Key_F9));
490 action->setEnabled(false);
491 actions->insert("snapshot", action);
492 connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
495 action = new QAction(tr("&Subscribe to Channel"), this);
496 action->setProperty("originalText", action->text());
497 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
498 action->setEnabled(false);
499 connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
500 actions->insert("subscribe-channel", action);
501 mediaView->updateSubscriptionAction(0, false);
503 QString shareTip = tr("Share the current video using %1");
505 action = new QAction("&Twitter", this);
506 action->setStatusTip(shareTip.arg("Twitter"));
507 action->setEnabled(false);
508 actions->insert("twitter", action);
509 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
511 action = new QAction("&Facebook", this);
512 action->setStatusTip(shareTip.arg("Facebook"));
513 action->setEnabled(false);
514 actions->insert("facebook", action);
515 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
517 action = new QAction("&Buffer", this);
518 action->setStatusTip(shareTip.arg("Buffer"));
519 action->setEnabled(false);
520 actions->insert("buffer", action);
521 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaBuffer()));
523 action = new QAction(tr("&Email"), this);
524 action->setStatusTip(shareTip.arg(tr("Email")));
525 action->setEnabled(false);
526 actions->insert("email", action);
527 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
529 action = new QAction(tr("&Close"), this);
530 action->setShortcut(QKeySequence(QKeySequence::Close));
531 actions->insert("close", action);
532 connect(action, SIGNAL(triggered()), SLOT(close()));
534 action = new QAction(Constants::NAME, this);
535 action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
536 actions->insert("restore", action);
537 connect(action, SIGNAL(triggered()), SLOT(restore()));
539 action = new QAction(IconUtils::icon("go-top"), tr("&Float on Top"), this);
540 action->setCheckable(true);
541 actions->insert("ontop", action);
542 connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
544 action = new QAction(IconUtils::icon("media-playback-stop"), tr("&Stop After This Video"), this);
545 action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
546 action->setCheckable(true);
547 action->setEnabled(false);
548 actions->insert("stopafterthis", action);
549 connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
551 action = new QAction(tr("&Report an Issue..."), this);
552 actions->insert("report-issue", action);
553 connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
555 action = new QAction(tr("&Refine Search..."), this);
556 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
557 action->setCheckable(true);
558 action->setEnabled(false);
559 actions->insert("refine-search", action);
561 action = new QAction(YTRegions::worldwideRegion().name, this);
562 actions->insert("worldwide-region", action);
564 action = new QAction(YTRegions::localRegion().name, this);
565 actions->insert("local-region", action);
567 action = new QAction(tr("More..."), this);
568 actions->insert("more-region", action);
570 action = new QAction(IconUtils::icon("view-list"), tr("&Related Videos"), this);
571 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
572 action->setStatusTip(tr("Watch videos related to the current one"));
573 action->setEnabled(false);
574 action->setPriority(QAction::LowPriority);
575 connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
576 actions->insert("related-videos", action);
578 action = new QAction(tr("Open in &Browser..."), this);
579 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
580 action->setEnabled(false);
581 actions->insert("open-in-browser", action);
582 connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
585 action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
586 actions->insert("app-store", action);
587 connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
590 #ifdef APP_ACTIVATION
591 Extra::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
594 // common action properties
595 foreach (QAction *action, actions->values()) {
596 // add actions to the MainWindow so that they work
597 // when the menu is hidden
599 IconUtils::setupAction(action);
603 void MainWindow::createMenus() {
605 QHash<QString, QMenu*> *menus = The::globalMenus();
607 fileMenu = menuBar()->addMenu(tr("&Application"));
608 #ifdef APP_ACTIVATION
609 QAction *buyAction = The::globalActions()->value("buy");
610 if (buyAction) fileMenu->addAction(buyAction);
612 fileMenu->addSeparator();
615 fileMenu->addAction(clearAct);
617 fileMenu->addSeparator();
619 fileMenu->addAction(quitAct);
621 QMenu* playbackMenu = menuBar()->addMenu(tr("&Playback"));
622 menus->insert("playback", playbackMenu);
623 playbackMenu->addAction(pauseAct);
624 playbackMenu->addAction(stopAct);
625 playbackMenu->addAction(The::globalActions()->value("stopafterthis"));
626 playbackMenu->addSeparator();
627 playbackMenu->addAction(skipAct);
628 playbackMenu->addAction(skipBackwardAct);
629 playbackMenu->addSeparator();
630 playbackMenu->addAction(The::globalActions()->value("manualplay"));
632 MacSupport::dockMenu(playbackMenu);
635 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
636 menus->insert("playlist", playlistMenu);
637 playlistMenu->addAction(removeAct);
638 playlistMenu->addSeparator();
639 playlistMenu->addAction(moveUpAct);
640 playlistMenu->addAction(moveDownAct);
641 playlistMenu->addSeparator();
642 playlistMenu->addAction(The::globalActions()->value("refine-search"));
644 QMenu* videoMenu = menuBar()->addMenu(tr("&Video"));
645 menus->insert("video", videoMenu);
646 videoMenu->addAction(The::globalActions()->value("related-videos"));
647 videoMenu->addAction(findVideoPartsAct);
648 videoMenu->addSeparator();
649 videoMenu->addAction(The::globalActions()->value("subscribe-channel"));
651 videoMenu->addSeparator();
652 videoMenu->addAction(The::globalActions()->value("snapshot"));
654 videoMenu->addSeparator();
655 videoMenu->addAction(webPageAct);
656 videoMenu->addAction(copyLinkAct);
657 videoMenu->addAction(The::globalActions()->value("open-in-browser"));
658 videoMenu->addAction(The::globalActions()->value("download"));
660 QMenu* viewMenu = menuBar()->addMenu(tr("&View"));
661 menus->insert("view", viewMenu);
662 viewMenu->addAction(fullscreenAct);
663 viewMenu->addAction(compactViewAct);
664 viewMenu->addSeparator();
665 viewMenu->addAction(The::globalActions()->value("ontop"));
667 QMenu* shareMenu = menuBar()->addMenu(tr("&Share"));
668 menus->insert("share", shareMenu);
669 shareMenu->addAction(copyPageAct);
670 shareMenu->addSeparator();
671 shareMenu->addAction(The::globalActions()->value("twitter"));
672 shareMenu->addAction(The::globalActions()->value("facebook"));
673 shareMenu->addAction(The::globalActions()->value("buffer"));
674 shareMenu->addSeparator();
675 shareMenu->addAction(The::globalActions()->value("email"));
678 MacSupport::windowMenu(this);
681 helpMenu = menuBar()->addMenu(tr("&Help"));
682 helpMenu->addAction(siteAct);
683 #if !defined(APP_MAC) && !defined(APP_WIN)
684 helpMenu->addAction(donateAct);
686 helpMenu->addAction(The::globalActions()->value("report-issue"));
687 helpMenu->addAction(aboutAct);
690 helpMenu->addSeparator();
691 helpMenu->addAction(The::globalActions()->value("app-store"));
695 void MainWindow::createToolBars() {
697 setUnifiedTitleAndToolBarOnMac(true);
699 mainToolBar = new QToolBar(this);
700 mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
701 mainToolBar->setFloatable(false);
702 mainToolBar->setMovable(false);
704 #if defined(APP_MAC) | defined(APP_WIN)
705 mainToolBar->setIconSize(QSize(32, 32));
708 mainToolBar->addAction(stopAct);
709 mainToolBar->addAction(pauseAct);
710 mainToolBar->addAction(skipAct);
712 mainToolBar->addAction(The::globalActions()->value("related-videos"));
713 mainToolBar->addAction(The::globalActions()->value("download"));
715 bool addFullScreenAct = true;
717 addFullScreenAct = !mac::CanGoFullScreen(winId());
719 if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
721 mainToolBar->addWidget(new Spacer());
723 QFont smallerFont = FontUtils::small();
724 currentTime = new QLabel(mainToolBar);
725 currentTime->setFont(smallerFont);
726 mainToolBar->addWidget(currentTime);
728 mainToolBar->addWidget(new Spacer());
730 #ifdef APP_PHONON_SEEK
731 seekSlider = new Phonon::SeekSlider(this);
732 seekSlider->setTracking(true);
733 seekSlider->setIconVisible(false);
734 seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
735 mainToolBar->addWidget(seekSlider);
737 slider = new SeekSlider(this);
738 slider->setEnabled(false);
739 slider->setTracking(false);
740 slider->setMaximum(1000);
741 slider->setOrientation(Qt::Horizontal);
742 slider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
743 mainToolBar->addWidget(slider);
747 mainToolBar->addWidget(new Spacer());
748 totalTime = new QLabel(mainToolBar);
749 totalTime->setFont(smallerFont);
750 mainToolBar->addWidget(totalTime);
753 mainToolBar->addWidget(new Spacer());
754 mainToolBar->addAction(volumeMuteAct);
756 QToolButton *volumeMuteButton = qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
757 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
761 volumeSlider = new Phonon::VolumeSlider(this);
762 volumeSlider->setMuteVisible(false);
763 // qDebug() << volumeSlider->children();
764 // status tip for the volume slider
765 QSlider* volumeQSlider = volumeSlider->findChild<QSlider*>();
767 volumeQSlider->setStatusTip(tr("Press %1 to raise the volume, %2 to lower it").arg(
768 volumeUpAct->shortcut().toString(QKeySequence::NativeText), volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
769 // this makes the volume slider smaller
770 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
771 mainToolBar->addWidget(volumeSlider);
774 mainToolBar->addWidget(new Spacer());
777 SearchWrapper* searchWrapper = new SearchWrapper(this);
778 toolbarSearch = searchWrapper->getSearchLineEdit();
780 toolbarSearch = new SearchLineEdit(this);
782 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize()*15);
783 toolbarSearch->setSuggester(new YTSuggester(this));
784 connect(toolbarSearch, SIGNAL(search(const QString&)), SLOT(search(const QString&)));
785 connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
786 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
788 mainToolBar->addWidget(searchWrapper);
790 mainToolBar->addWidget(toolbarSearch);
791 mainToolBar->addWidget(new Spacer());
794 addToolBar(mainToolBar);
797 void MainWindow::createStatusBar() {
798 statusToolBar = new QToolBar(this);
799 statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
800 statusToolBar->setIconSize(QSize(16, 16));
801 statusToolBar->addAction(The::globalActions()->value("downloads"));
803 regionButton = new QToolButton(this);
804 regionButton->setStatusTip(tr("Choose your content location"));
805 regionButton->setIconSize(QSize(16, 16));
806 regionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
807 regionAction = statusToolBar->addWidget(regionButton);
808 regionAction->setVisible(false);
810 QAction *localAction = The::globalActions()->value("local-region");
811 if (!localAction->text().isEmpty()) {
812 regionButton->setPopupMode(QToolButton::InstantPopup);
813 QMenu *regionMenu = new QMenu(this);
814 regionMenu->addAction(The::globalActions()->value("worldwide-region"));
815 regionMenu->addAction(localAction);
816 regionMenu->addSeparator();
817 QAction *moreRegionsAction = The::globalActions()->value("more-region");
818 regionMenu->addAction(moreRegionsAction);
819 connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
820 regionButton->setMenu(regionMenu);
822 connect(regionButton, SIGNAL(clicked()), SLOT(showRegionsView()));
825 /* Stupid code that generates the QRC items
826 foreach(YTRegion r, YTRegions::list())
827 qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
830 statusToolBar->addAction(The::globalActions()->value("definition"));
832 statusBar()->addPermanentWidget(statusToolBar);
836 void MainWindow::showStopAfterThisInStatusBar(bool show) {
837 QAction* action = The::globalActions()->value("stopafterthis");
838 showActionInStatusBar(action, show);
841 void MainWindow::showActionInStatusBar(QAction* action, bool show) {
843 Extra::fadeInWidget(statusBar(), statusBar());
846 statusToolBar->insertAction(statusToolBar->actions().first(), action);
848 statusToolBar->removeAction(action);
852 void MainWindow::readSettings() {
854 if (settings.contains("geometry")) {
855 restoreGeometry(settings.value("geometry").toByteArray());
857 MacSupport::fixGeometry(this);
860 setGeometry(100, 100, 1000, 500);
862 const VideoDefinition& firstDefinition = VideoDefinition::getDefinitions().first();
863 setDefinitionMode(settings.value("definition", firstDefinition.getName()).toString());
864 The::globalActions()->value("manualplay")->setChecked(settings.value("manualplay", false).toBool());
867 void MainWindow::writeSettings() {
870 settings.setValue("geometry", saveGeometry());
871 mediaView->saveSplitterState();
874 if (audioOutput->volume() > 0.1)
875 settings.setValue("volume", audioOutput->volume());
876 // settings.setValue("volumeMute", audioOutput->isMuted());
879 settings.setValue("manualplay", The::globalActions()->value("manualplay")->isChecked());
882 void MainWindow::goBack() {
883 if ( history->size() > 1 ) {
885 QWidget *widget = history->pop();
890 void MainWindow::showWidget(QWidget* widget, bool transition) {
891 if (compactViewAct->isChecked())
892 compactViewAct->toggle();
894 setUpdatesEnabled(false);
896 // call hide method on the current view
897 View* oldView = dynamic_cast<View *> (views->currentWidget());
899 oldView->disappear();
900 views->currentWidget()->setEnabled(false);
901 } else qDebug() << "Cannot cast view";
903 // call show method on the new view
904 View* newView = dynamic_cast<View *> (widget);
906 widget->setEnabled(true);
907 QHash<QString,QVariant> metadata = newView->metadata();
908 QString title = metadata.value("title").toString();
909 if (title.isEmpty()) title = Constants::NAME;
910 else title += QLatin1String(" - ") + Constants::NAME;
911 setWindowTitle(title);
912 QString desc = metadata.value("description").toString();
913 if (!desc.isEmpty()) showMessage(desc);
916 // dynamic view actions
917 foreach (QAction* action, viewActions)
918 showActionInStatusBar(action, false);
919 viewActions = newView->getViewActions();
920 foreach (QAction* action, viewActions)
921 showActionInStatusBar(action, true);
925 const bool isMediaView = widget == mediaView;
927 stopAct->setEnabled(isMediaView);
928 compactViewAct->setEnabled(isMediaView);
929 toolbarSearch->setEnabled(widget == homeView || isMediaView || widget == downloadView);
931 aboutAct->setEnabled(widget != aboutView);
932 The::globalActions()->value("downloads")->setChecked(widget == downloadView);
934 QWidget *oldWidget = views->currentWidget();
936 oldWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
938 views->setCurrentWidget(widget);
939 widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
941 setUpdatesEnabled(true);
944 // if (transition && (oldWidget != mediaView || !mediaView->getVideoArea()->isVideoShown()))
946 Extra::fadeInWidget(oldWidget, widget);
948 Q_UNUSED(transition);
951 history->push(widget);
954 void MainWindow::about() {
956 aboutView = new AboutView(this);
957 views->addWidget(aboutView);
959 showWidget(aboutView);
962 void MainWindow::visitSite() {
963 QUrl url(Constants::WEBSITE);
964 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
965 QDesktopServices::openUrl(url);
968 void MainWindow::donate() {
969 QUrl url(QString(Constants::WEBSITE) + "#donate");
970 statusBar()->showMessage(QString(tr("Opening %1").arg(url.toString())));
971 QDesktopServices::openUrl(url);
974 void MainWindow::reportIssue() {
975 QUrl url("http://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
976 QDesktopServices::openUrl(url);
979 void MainWindow::quit() {
981 if (!confirmQuit()) {
985 // do not save geometry when in full screen or in compact mode
986 if (!m_fullscreen && !compactViewAct->isChecked()) {
989 // mediaView->stop();
990 Temporary::deleteAll();
991 ChannelAggregator::instance()->stop();
992 ChannelAggregator::instance()->cleanup();
993 Database::shutdown();
997 void MainWindow::closeEvent(QCloseEvent *event) {
999 mac::closeWindow(winId());
1002 if (!confirmQuit()) {
1006 QWidget::closeEvent(event);
1011 bool MainWindow::confirmQuit() {
1012 if (DownloadManager::instance()->activeItems() > 0) {
1013 QMessageBox msgBox(this);
1014 msgBox.setIconPixmap(QPixmap(":/images/app.png").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
1015 msgBox.setText(tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1016 msgBox.setInformativeText(tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1017 msgBox.setModal(true);
1018 // make it a "sheet" on the Mac
1019 msgBox.setWindowModality(Qt::WindowModal);
1021 msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1022 QPushButton *waitButton = msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1026 if (msgBox.clickedButton() == waitButton) {
1033 void MainWindow::showHome(bool transition) {
1034 showWidget(homeView, transition);
1035 currentTime->clear();
1036 // totalTime->clear();
1039 void MainWindow::showMedia(SearchParams *searchParams) {
1040 showWidget(mediaView);
1041 mediaView->search(searchParams);
1044 void MainWindow::showMedia(VideoSource *videoSource) {
1045 showWidget(mediaView);
1046 mediaView->setVideoSource(videoSource);
1050 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) {
1052 // qDebug() << "Phonon state: " << newState;
1056 case Phonon::ErrorState:
1057 if (mediaObject->errorType() == Phonon::FatalError) {
1058 // Do not display because we try to play incomplete video files and sometimes trigger this
1059 // We retry automatically (in MediaView) so no need to show it
1060 // statusBar()->showMessage(tr("Fatal error: %1").arg(mediaObject->errorString()));
1062 statusBar()->showMessage(tr("Error: %1").arg(mediaObject->errorString()));
1066 case Phonon::PlayingState:
1067 pauseAct->setEnabled(true);
1068 pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1069 pauseAct->setText(tr("&Pause"));
1070 pauseAct->setStatusTip(tr("Pause playback") + " (" + pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1071 // stopAct->setEnabled(true);
1074 case Phonon::StoppedState:
1075 pauseAct->setEnabled(false);
1076 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1077 pauseAct->setText(tr("&Play"));
1078 pauseAct->setStatusTip(tr("Resume playback") + " (" + pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1079 // stopAct->setEnabled(false);
1082 case Phonon::PausedState:
1083 pauseAct->setEnabled(true);
1084 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1085 pauseAct->setText(tr("&Play"));
1086 pauseAct->setStatusTip(tr("Resume playback") + " (" + pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1087 // stopAct->setEnabled(true);
1090 case Phonon::BufferingState:
1091 pauseAct->setEnabled(false);
1092 pauseAct->setIcon(IconUtils::icon("content-loading"));
1093 pauseAct->setText(tr("&Loading..."));
1094 pauseAct->setStatusTip(QString());
1097 case Phonon::LoadingState:
1098 pauseAct->setEnabled(false);
1099 currentTime->clear();
1100 // totalTime->clear();
1101 // stopAct->setEnabled(true);
1110 void MainWindow::stop() {
1115 void MainWindow::resizeEvent(QResizeEvent*) {
1117 if (mac::CanGoFullScreen(winId())) {
1118 bool isFullscreen = mac::IsFullScreen(winId());
1119 if (isFullscreen != m_fullscreen) {
1120 if (compactViewAct->isChecked()) {
1121 compactViewAct->setChecked(false);
1124 m_fullscreen = isFullscreen;
1125 updateUIForFullscreen();
1131 void MainWindow::fullscreen() {
1133 if (compactViewAct->isChecked())
1134 compactViewAct->toggle();
1137 WId handle = winId();
1138 if (mac::CanGoFullScreen(handle)) {
1139 mainToolBar->setVisible(true);
1140 mac::ToggleFullScreen(handle);
1145 m_fullscreen = !m_fullscreen;
1148 // Enter full screen
1150 m_maximized = isMaximized();
1152 // save geometry now, if the user quits when in full screen
1153 // geometry won't be saved
1157 MacSupport::enterFullScreen(this, views);
1159 mainToolBar->hide();
1167 MacSupport::exitFullScreen(this, views);
1169 mainToolBar->show();
1170 if (m_maximized) showMaximized();
1174 // Make sure the window has focus
1179 updateUIForFullscreen();
1183 void MainWindow::updateUIForFullscreen() {
1184 static QList<QKeySequence> fsShortcuts;
1185 static QString fsText;
1188 fsShortcuts = fullscreenAct->shortcuts();
1189 fsText = fullscreenAct->text();
1190 fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1191 << QKeySequence(Qt::Key_Escape));
1192 fullscreenAct->setText(tr("Leave &Full Screen"));
1193 fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1195 fullscreenAct->setShortcuts(fsShortcuts);
1196 fullscreenAct->setText(fsText);
1197 fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1200 // No compact view action when in full screen
1201 compactViewAct->setVisible(!m_fullscreen);
1202 compactViewAct->setChecked(false);
1204 // Hide anything but the video
1205 mediaView->setPlaylistVisible(!m_fullscreen);
1206 statusBar()->setVisible(!m_fullscreen);
1209 menuBar()->setVisible(!m_fullscreen);
1213 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1215 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1219 MacSupport::fullScreenActions(The::globalActions()->values(), m_fullscreen);
1222 if (views->currentWidget() == mediaView)
1223 mediaView->setFocus();
1233 bool MainWindow::isReallyFullScreen() {
1235 WId handle = winId();
1236 if (mac::CanGoFullScreen(handle)) return mac::IsFullScreen(handle);
1237 else return isFullScreen();
1239 return isFullScreen();
1243 void MainWindow::compactView(bool enable) {
1246 static QList<QKeySequence> compactShortcuts;
1247 static QList<QKeySequence> stopShortcuts;
1249 const static QString key = "compactGeometry";
1253 menuBar()->setVisible(!enable);
1257 setMinimumSize(320, 180);
1259 mac::RemoveFullScreenWindow(winId());
1263 if (settings.contains(key))
1264 restoreGeometry(settings.value(key).toByteArray());
1268 mainToolBar->setVisible(!enable);
1269 mediaView->setPlaylistVisible(!enable);
1270 statusBar()->setVisible(!enable);
1272 compactShortcuts = compactViewAct->shortcuts();
1273 stopShortcuts = stopAct->shortcuts();
1275 QList<QKeySequence> newStopShortcuts(stopShortcuts);
1276 newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1277 stopAct->setShortcuts(newStopShortcuts);
1278 compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts) << QKeySequence(Qt::Key_Escape));
1280 // ensure focus does not end up to the search box
1281 // as it would steal the Space shortcut
1282 mediaView->setFocus();
1285 // unset minimum size
1286 setMinimumSize(0, 0);
1288 mac::SetupFullScreenWindow(winId());
1290 settings.setValue(key, saveGeometry());
1291 mainToolBar->setVisible(!enable);
1292 mediaView->setPlaylistVisible(!enable);
1293 statusBar()->setVisible(!enable);
1296 compactViewAct->setShortcuts(compactShortcuts);
1297 stopAct->setShortcuts(stopShortcuts);
1300 // auto float on top
1304 mac::compactMode(winId(), enable);
1308 void MainWindow::searchFocus() {
1309 toolbarSearch->selectAll();
1310 toolbarSearch->setFocus();
1314 void MainWindow::initPhonon() {
1315 // Phonon initialization
1316 if (mediaObject) delete mediaObject;
1317 if (audioOutput) delete audioOutput;
1318 mediaObject = new Phonon::MediaObject(this);
1319 mediaObject->setTickInterval(100);
1320 connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
1321 SLOT(stateChanged(Phonon::State, Phonon::State)));
1322 connect(mediaObject, SIGNAL(tick(qint64)), SLOT(tick(qint64)));
1323 connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), SLOT(totalTimeChanged(qint64)));
1325 audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
1326 connect(audioOutput, SIGNAL(volumeChanged(qreal)), SLOT(volumeChanged(qreal)));
1327 connect(audioOutput, SIGNAL(mutedChanged(bool)), SLOT(volumeMutedChanged(bool)));
1328 Phonon::createPath(mediaObject, audioOutput);
1329 volumeSlider->setAudioOutput(audioOutput);
1331 #ifdef APP_PHONON_SEEK
1332 seekSlider->setMediaObject(mediaObject);
1336 audioOutput->setVolume(settings.value("volume", 1.).toReal());
1337 // audioOutput->setMuted(settings.value("volumeMute").toBool());
1339 mediaObject->stop();
1343 void MainWindow::tick(qint64 time) {
1345 // the "if" is important because tick is continually called
1346 // and we don't want to paint the toolbar every 100ms
1347 if (!currentTime->text().isEmpty()) currentTime->clear();
1351 currentTime->setText(formatTime(time));
1355 const qint64 remainingTime = mediaObject->remainingTime();
1356 currentTime->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1358 #ifndef APP_PHONON_SEEK
1359 const qint64 totalTime = mediaObject->totalTime();
1360 slider->blockSignals(true);
1361 // qWarning() << totalTime << time << time * 100 / totalTime;
1362 if (totalTime > 0 && time > 0 && !slider->isSliderDown() && mediaObject->state() == Phonon::PlayingState)
1363 slider->setValue(time * slider->maximum() / totalTime);
1364 slider->blockSignals(false);
1370 void MainWindow::totalTimeChanged(qint64 time) {
1372 // totalTime->clear();
1375 // totalTime->setText(formatTime(time));
1378 slider->blockSignals(true);
1379 slider->setMaximum(time/1000);
1380 slider->blockSignals(false);
1385 QString MainWindow::formatTime(qint64 time) {
1387 displayTime = displayTime.addMSecs(time);
1389 // 60 * 60 * 1000 = 3600000
1391 timeString = displayTime.toString("h:mm:ss");
1393 timeString = displayTime.toString("m:ss");
1397 void MainWindow::volumeUp() {
1399 qreal newVolume = volumeSlider->audioOutput()->volume() + .1;
1400 if (newVolume > volumeSlider->maximumVolume())
1401 newVolume = volumeSlider->maximumVolume();
1402 volumeSlider->audioOutput()->setVolume(newVolume);
1406 void MainWindow::volumeDown() {
1408 qreal newVolume = volumeSlider->audioOutput()->volume() - .1;
1411 volumeSlider->audioOutput()->setVolume(newVolume);
1415 void MainWindow::volumeMute() {
1417 bool muted = volumeSlider->audioOutput()->isMuted();
1418 volumeSlider->audioOutput()->setMuted(!muted);
1419 qApp->processEvents();
1420 if (muted && volumeSlider->audioOutput()->volume() == 0) {
1421 volumeSlider->audioOutput()->setVolume(volumeSlider->maximumVolume());
1423 qDebug() << volumeSlider->audioOutput()->isMuted() << volumeSlider->audioOutput()->volume();
1427 void MainWindow::volumeChanged(qreal newVolume) {
1429 // automatically unmute when volume changes
1430 if (volumeSlider->audioOutput()->isMuted()) volumeSlider->audioOutput()->setMuted(false);
1432 bool isZero = volumeSlider->property("zero").toBool();
1433 bool styleChanged = false;
1434 if (newVolume == 0. && !isZero) {
1435 volumeSlider->setProperty("zero", true);
1436 styleChanged = true;
1437 } else if (newVolume > 0. && isZero) {
1438 volumeSlider->setProperty("zero", false);
1439 styleChanged = true;
1442 QSlider* volumeQSlider = volumeSlider->findChild<QSlider*>();
1443 style()->unpolish(volumeQSlider);
1444 style()->polish(volumeQSlider);
1447 statusBar()->showMessage(tr("Volume at %1%").arg((int)(newVolume*100)));
1450 void MainWindow::volumeMutedChanged(bool muted) {
1452 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1453 statusBar()->showMessage(tr("Volume is muted"));
1455 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1456 statusBar()->showMessage(tr("Volume is unmuted"));
1459 QToolButton *volumeMuteButton = qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
1460 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
1464 void MainWindow::setDefinitionMode(const QString &definitionName) {
1465 QAction *definitionAct = The::globalActions()->value("definition");
1466 definitionAct->setText(definitionName);
1467 definitionAct->setStatusTip(tr("Maximum video definition set to %1").arg(definitionAct->text())
1468 + " (" + definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1469 statusBar()->showMessage(definitionAct->statusTip());
1471 settings.setValue("definition", definitionName);
1474 void MainWindow::toggleDefinitionMode() {
1475 const QString definitionName = QSettings().value("definition").toString();
1476 const QList<VideoDefinition>& definitions = VideoDefinition::getDefinitions();
1477 const VideoDefinition& currentDefinition = VideoDefinition::getDefinitionFor(definitionName);
1478 if (currentDefinition.isEmpty()) {
1479 setDefinitionMode(definitions.first().getName());
1483 int index = definitions.indexOf(currentDefinition);
1484 if (index != definitions.size() - 1) {
1489 // TODO: pass a VideoDefinition instead of QString.
1490 setDefinitionMode(definitions.at(index).getName());
1493 void MainWindow::showFullscreenToolbar(bool show) {
1494 if (!m_fullscreen) return;
1495 mainToolBar->setVisible(show);
1498 void MainWindow::showFullscreenPlaylist(bool show) {
1499 if (!m_fullscreen) return;
1500 mediaView->setPlaylistVisible(show);
1503 void MainWindow::clearRecentKeywords() {
1505 settings.remove("recentKeywords");
1506 settings.remove("recentChannels");
1507 if (views->currentWidget() == homeView) {
1508 SearchView *searchView = homeView->getSearchView();
1509 searchView->updateRecentKeywords();
1510 searchView->updateRecentChannels();
1512 QAbstractNetworkCache *cache = The::networkAccessManager()->cache();
1513 if (cache) cache->clear();
1514 showMessage(tr("Your privacy is now safe"));
1517 void MainWindow::setManualPlay(bool enabled) {
1519 settings.setValue("manualplay", QVariant::fromValue(enabled));
1520 showActionInStatusBar(The::globalActions()->value("manualplay"), enabled);
1523 void MainWindow::updateDownloadMessage(const QString &message) {
1524 The::globalActions()->value("downloads")->setText(message);
1527 void MainWindow::downloadsFinished() {
1528 The::globalActions()->value("downloads")->setText(tr("&Downloads"));
1529 statusBar()->showMessage(tr("Downloads complete"));
1532 void MainWindow::toggleDownloads(bool show) {
1535 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1536 The::globalActions()->value("downloads")->setShortcuts(
1537 QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1538 << QKeySequence(Qt::Key_Escape));
1540 The::globalActions()->value("downloads")->setShortcuts(
1541 QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1542 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1545 if (!downloadView) {
1546 downloadView = new DownloadView(this);
1547 views->addWidget(downloadView);
1549 if (show) showWidget(downloadView);
1553 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1554 search(suggestion->value);
1557 void MainWindow::search(const QString &query) {
1558 QString q = query.trimmed();
1559 if (q.length() == 0) return;
1560 SearchParams *searchParams = new SearchParams();
1561 searchParams->setKeywords(q);
1562 showMedia(searchParams);
1565 void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
1566 if (event->mimeData()->hasFormat("text/uri-list")) {
1567 QList<QUrl> urls = event->mimeData()->urls();
1568 if (urls.isEmpty()) return;
1569 QUrl url = urls.first();
1570 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1571 if (!videoId.isEmpty())
1572 event->acceptProposedAction();
1576 void MainWindow::dropEvent(QDropEvent *event) {
1577 if (!toolbarSearch->isEnabled()) return;
1579 QList<QUrl> urls = event->mimeData()->urls();
1582 QUrl url = urls.first();
1583 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1584 if (!videoId.isEmpty()) {
1585 setWindowTitle(url.toString());
1586 SearchParams *searchParams = new SearchParams();
1587 searchParams->setKeywords(videoId);
1588 showMedia(searchParams);
1592 void MainWindow::checkForUpdate() {
1593 static const QString updateCheckKey = "updateCheck";
1597 uint unixTime = QDateTime::currentDateTime().toTime_t();
1598 int lastCheck = settings.value(updateCheckKey).toInt();
1599 int secondsSinceLastCheck = unixTime - lastCheck;
1600 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
1601 if (secondsSinceLastCheck < 86400) return;
1604 if (updateChecker) delete updateChecker;
1605 updateChecker = new UpdateChecker();
1606 connect(updateChecker, SIGNAL(newVersion(QString)),
1607 this, SLOT(gotNewVersion(QString)));
1608 updateChecker->checkForUpdate();
1609 settings.setValue(updateCheckKey, unixTime);
1612 void MainWindow::gotNewVersion(const QString &version) {
1613 if (updateChecker) {
1614 delete updateChecker;
1619 QString checkedVersion = settings.value("checkedVersion").toString();
1620 if (checkedVersion == version) return;
1624 UpdateDialog *dialog = new UpdateDialog(version, this);
1628 simpleUpdateDialog(version);
1632 void MainWindow::simpleUpdateDialog(const QString &version) {
1633 QMessageBox msgBox(this);
1634 msgBox.setIconPixmap(
1635 QPixmap(":/images/app.png")
1636 .scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
1637 msgBox.setText(tr("%1 version %2 is now available.").arg(Constants::NAME, version));
1638 msgBox.setModal(true);
1639 msgBox.setWindowModality(Qt::WindowModal);
1640 msgBox.addButton(QMessageBox::Close);
1641 QPushButton* laterButton = msgBox.addButton(tr("Remind me later"), QMessageBox::RejectRole);
1642 QPushButton* updateButton = msgBox.addButton(tr("Update"), QMessageBox::AcceptRole);
1644 if (msgBox.clickedButton() != laterButton) {
1646 settings.setValue("checkedVersion", version);
1648 if (msgBox.clickedButton() == updateButton) visitSite();
1651 void MainWindow::floatOnTop(bool onTop) {
1652 showActionInStatusBar(The::globalActions()->value("ontop"), onTop);
1654 mac::floatOnTop(winId(), onTop);
1658 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1661 setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1666 void MainWindow::restore() {
1668 mac::uncloseWindow(window()->winId());
1672 void MainWindow::messageReceived(const QString &message) {
1673 if (message == QLatin1String("--toggle-playing")) {
1674 if (pauseAct->isEnabled()) pauseAct->trigger();
1675 } else if (message == QLatin1String("--next")) {
1676 if (skipAct->isEnabled()) skipAct->trigger();
1677 } else if (message == QLatin1String("--previous")) {
1678 if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1679 } else if (message == QLatin1String("--stop-after-this")) {
1680 The::globalActions()->value("stopafterthis")->toggle();
1681 } else if (message.startsWith("--")) {
1682 MainWindow::printHelp();
1683 } else if (!message.isEmpty()) {
1684 SearchParams *searchParams = new SearchParams();
1685 searchParams->setKeywords(message);
1686 showMedia(searchParams);
1690 void MainWindow::hideMouse() {
1691 setCursor(Qt::BlankCursor);
1692 mediaView->setPlaylistVisible(false);
1694 mainToolBar->setVisible(false);
1698 #ifdef APP_MAC_STORE
1699 void MainWindow::rateOnAppStore() {
1700 QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1701 "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1702 "?id=422006190&type=Purple+Software"));
1706 void MainWindow::printHelp() {
1707 QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1708 msg += "Usage: minitube [options]\n";
1709 msg += "Options:\n";
1710 msg += " --toggle-playing\t";
1711 msg += "Start or pause playback.\n";
1712 msg += " --next\t\t";
1713 msg += "Skip to the next video.\n";
1714 msg += " --previous\t\t";
1715 msg += "Go back to the previous video.\n";
1716 msg += " --stop-after-this\t";
1717 msg += "Stop playback at the end of the video.\n";
1718 std::cout << msg.toLocal8Bit().data();
1721 void MainWindow::showMessage(const QString &message) {
1722 statusBar()->showMessage(message, 60000);
1725 #ifdef APP_ACTIVATION
1726 void MainWindow::showActivationView(bool transition) {
1727 QWidget *activationView = ActivationView::instance();
1728 if (views->currentWidget() == activationView) {
1732 views->addWidget(activationView);
1733 showWidget(activationView, transition);
1736 void MainWindow::showActivationDialog() {
1737 QTimer::singleShot(0, new ActivationDialog(this), SLOT(show()));
1740 void MainWindow::buy() {
1744 void MainWindow::hideBuyAction() {
1745 QAction *action = The::globalActions()->value("buy");
1746 action->setVisible(false);
1747 action->setEnabled(false);
1751 void MainWindow::showRegionsView() {
1753 regionsView = new RegionsView(this);
1754 connect(regionsView, SIGNAL(regionChanged()),
1755 homeView->getStandardFeedsView(), SLOT(load()));
1756 views->addWidget(regionsView);
1758 showWidget(regionsView);