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 "aboutview.h"
24 #include "downloadview.h"
26 #include "mediaview.h"
27 #include "regionsview.h"
28 #include "searchview.h"
29 #include "standardfeedsview.h"
31 #include "constants.h"
32 #include "fontutils.h"
33 #include "globalshortcuts.h"
34 #include "iconutils.h"
35 #include "searchparams.h"
37 #include "videodefinition.h"
38 #include "videosource.h"
41 #include "gnomeglobalshortcutbackend.h"
44 #include "mac_startup.h"
45 #include "macfullscreen.h"
46 #include "macsupport.h"
49 #include "downloadmanager.h"
50 #include "temporary.h"
51 #include "ytsuggester.h"
52 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
53 #include "searchlineedit_mac.h"
55 #include "searchlineedit.h"
57 #ifdef APP_MAC_QMACTOOLBAR
58 #include "mactoolbar.h"
62 #include "compositefader.h"
64 #include "updatedialog.h"
67 #include "activation.h"
68 #include "activationview.h"
70 #include "channelaggregator.h"
72 #include "httputils.h"
73 #include "jsfunctions.h"
74 #include "seekslider.h"
75 #include "sidebarwidget.h"
76 #include "toolbarmenu.h"
77 #include "videoarea.h"
79 #include "ytregions.h"
81 #include "invidious.h"
86 #include "mediaqtav.h"
97 MainWindow *mainWindowInstance;
100 MainWindow *MainWindow::instance() {
101 return mainWindowInstance;
104 MainWindow::MainWindow()
105 : aboutView(nullptr), downloadView(nullptr), regionsView(nullptr), mainToolBar(nullptr),
106 fullScreenActive(false), compactModeActive(false), initialized(false), toolbarMenu(nullptr),
108 mainWindowInstance = this;
111 views = new QStackedWidget();
112 setCentralWidget(views);
115 Extra::windowSetup(this);
118 messageLabel = new QLabel(this);
119 messageLabel->setWordWrap(false);
120 messageLabel->setStyleSheet("padding:5px;border:0;background:palette(window)");
121 messageLabel->setAlignment(Qt::AlignCenter);
122 messageLabel->hide();
123 adjustMessageLabelPosition();
124 messageTimer = new QTimer(this);
125 messageTimer->setInterval(5000);
126 messageTimer->setSingleShot(true);
127 connect(messageTimer, SIGNAL(timeout()), SLOT(hideMessage()));
130 homeView = new HomeView(this);
131 views->addWidget(homeView);
133 // TODO make this lazy
134 mediaView = MediaView::instance();
135 mediaView->setEnabled(false);
136 views->addWidget(mediaView);
145 // remove that useless menu/toolbar context menu
146 this->setContextMenuPolicy(Qt::NoContextMenu);
148 // event filter to block ugly toolbar tooltips
149 qApp->installEventFilter(this);
151 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
153 // restore window position
156 // fix stacked widget minimum size
157 for (int i = 0; i < views->count(); i++)
158 views->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
161 #ifdef APP_ACTIVATION
162 Activation::instance().initialCheck();
167 if (VideoAPI::impl() == VideoAPI::IV) {
168 Invidious::instance().initServers();
169 } else if (VideoAPI::impl() == VideoAPI::YT3) {
170 YT3::instance().initApiKeys();
171 } else if (VideoAPI::impl() == VideoAPI::JS) {
173 Invidious::instance().initServers();
176 QTimer::singleShot(100, this, &MainWindow::lazyInit);
179 void MainWindow::lazyInit() {
180 mediaView->initialize();
182 qApp->processEvents();
185 if (qApp->arguments().size() > 1) {
186 QString query = qApp->arguments().at(1);
187 if (query.startsWith(QLatin1String("--"))) {
188 messageReceived(query);
191 SearchParams *searchParams = new SearchParams();
192 searchParams->setKeywords(query);
193 showMedia(searchParams);
198 GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
200 if (GnomeGlobalShortcutBackend::IsGsdAvailable())
201 shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
206 connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
207 connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
208 connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
209 connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
210 // connect(&shortcuts, SIGNAL(StopAfter()), getAction("stopafterthis"), SLOT(toggle()));
212 connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
213 SLOT(updateDownloadMessage(QString)));
214 connect(DownloadManager::instance(), SIGNAL(finished()), SLOT(downloadsFinished()));
216 setAcceptDrops(true);
218 fullscreenTimer = new QTimer(this);
219 fullscreenTimer->setInterval(3000);
220 fullscreenTimer->setSingleShot(true);
221 connect(fullscreenTimer, SIGNAL(timeout()), SLOT(hideFullscreenUI()));
223 JsFunctions::instance();
225 // Hack to give focus to searchlineedit
226 View *view = qobject_cast<View *>(views->currentWidget());
227 if (view == homeView) {
228 QMetaObject::invokeMethod(views->currentWidget(), "appear");
229 const QString &desc = view->getDescription();
230 if (!desc.isEmpty()) showMessage(desc);
233 ChannelAggregator::instance()->start();
236 Updater::instance().checkWithoutUI();
242 void MainWindow::changeEvent(QEvent *e) {
244 if (e->type() == QEvent::WindowStateChange) {
245 getAction("minimize")->setEnabled(!isMinimized());
248 if (messageLabel->isVisible()) {
249 if (e->type() == QEvent::ActivationChange || e->type() == QEvent::WindowStateChange ||
250 e->type() == QEvent::WindowDeactivate || e->type() == QEvent::ApplicationStateChange) {
254 QMainWindow::changeEvent(e);
257 bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
258 const QEvent::Type t = e->type();
261 static bool altPressed = false;
262 if (t == QEvent::KeyRelease && altPressed) {
264 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
265 if (ke->key() == Qt::Key_Alt) {
266 toggleMenuVisibility();
269 } else if (t == QEvent::KeyPress) {
270 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
271 altPressed = ke->key() == Qt::Key_Alt;
275 if (fullScreenActive && views->currentWidget() == mediaView && t == QEvent::MouseMove &&
276 obj->isWidgetType() && qobject_cast<QWidget *>(obj)->window() == this) {
277 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
279 bool toolBarVisible = mainToolBar && mainToolBar->isVisible();
280 bool sidebarVisible = mediaView->isSidebarVisible();
282 if (!sidebarVisible && !toolBarVisible) {
283 const int x = mouseEvent->pos().x();
284 if (x >= 0 && x < 5) {
286 SidebarWidget *sidebar = mediaView->getSidebar();
287 sidebar->resize(sidebar->width(), height());
289 mediaView->setSidebarVisibility(true);
290 sidebarVisible = true;
295 if (!toolBarVisible && !sidebarVisible) {
296 const int y = mouseEvent->pos().y();
297 if (y >= 0 && y < 5) {
298 mainToolBar->resize(width(), mainToolBar->sizeHint().height());
299 mainToolBar->setVisible(true);
304 // show the normal cursor
306 // then hide it again after a few seconds
307 fullscreenTimer->start();
310 if (t == QEvent::ToolTip) {
315 if (t == QEvent::Show && obj == toolbarMenu) {
317 int x = width() - toolbarMenu->sizeHint().width();
320 int x = toolbarMenuButton->x() + toolbarMenuButton->width() -
321 toolbarMenu->sizeHint().width();
322 int y = toolbarMenuButton->y() + toolbarMenuButton->height();
325 toolbarMenu->move(mapToGlobal(p));
328 if (obj == this && t == QEvent::StyleChange) {
329 qDebug() << "Style change detected";
330 qApp->paletteChanged(qApp->palette());
334 // standard event processing
335 return QMainWindow::eventFilter(obj, e);
338 void MainWindow::createActions() {
339 stopAct = new QAction(tr("&Stop"), this);
340 IconUtils::setIcon(stopAct, "media-playback-stop");
341 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
342 stopAct->setShortcuts(QList<QKeySequence>()
343 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
344 stopAct->setEnabled(false);
345 actionMap.insert("stop", stopAct);
346 connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
348 skipBackwardAct = new QAction(tr("P&revious"), this);
349 skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
350 skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
351 skipBackwardAct->setEnabled(false);
352 actionMap.insert("previous", skipBackwardAct);
353 connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
355 skipAct = new QAction(tr("S&kip"), this);
356 IconUtils::setIcon(skipAct, "media-skip-forward");
357 skipAct->setStatusTip(tr("Skip to the next video"));
358 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right)
359 << QKeySequence(Qt::Key_MediaNext));
360 skipAct->setEnabled(false);
361 actionMap.insert("skip", skipAct);
362 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
364 pauseAct = new QAction(tr("&Play"), this);
365 IconUtils::setIcon(pauseAct, "media-playback-start");
366 pauseAct->setStatusTip(tr("Resume playback"));
367 pauseAct->setShortcuts(QList<QKeySequence>()
368 << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
369 pauseAct->setEnabled(false);
370 actionMap.insert("pause", pauseAct);
371 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
373 fullscreenAct = new QAction(tr("&Full Screen"), this);
374 IconUtils::setIcon(fullscreenAct, "view-fullscreen");
375 fullscreenAct->setStatusTip(tr("Go full screen"));
376 QList<QKeySequence> fsShortcuts;
378 fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
380 fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
382 fullscreenAct->setShortcuts(fsShortcuts);
383 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
384 fullscreenAct->setPriority(QAction::LowPriority);
385 actionMap.insert("fullscreen", fullscreenAct);
386 connect(fullscreenAct, SIGNAL(triggered()), SLOT(toggleFullscreen()));
388 compactViewAct = new QAction(tr("&Compact Mode"), this);
389 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
390 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
391 compactViewAct->setCheckable(true);
392 compactViewAct->setChecked(false);
393 compactViewAct->setEnabled(false);
394 actionMap.insert("compactView", compactViewAct);
395 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
397 webPageAct = new QAction(tr("Open the &YouTube Page"), this);
398 webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
399 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
400 webPageAct->setEnabled(false);
401 actionMap.insert("webpage", webPageAct);
402 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
404 copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
405 IconUtils::setIcon(copyPageAct, "link");
406 copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
407 copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
408 copyPageAct->setEnabled(false);
409 actionMap.insert("pagelink", copyPageAct);
410 connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
412 copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
413 copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
414 copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
415 copyLinkAct->setEnabled(false);
416 actionMap.insert("videolink", copyLinkAct);
417 connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
419 findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
420 findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
421 findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
422 findVideoPartsAct->setEnabled(false);
423 connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
424 actionMap.insert("findVideoParts", findVideoPartsAct);
426 removeAct = new QAction(tr("&Remove"), this);
427 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
428 removeAct->setShortcuts(QList<QKeySequence>()
429 << QKeySequence("Del") << QKeySequence("Backspace"));
430 removeAct->setEnabled(false);
431 actionMap.insert("remove", removeAct);
432 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
434 moveUpAct = new QAction(tr("Move &Up"), this);
435 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
436 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
437 moveUpAct->setEnabled(false);
438 actionMap.insert("moveUp", moveUpAct);
439 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
441 moveDownAct = new QAction(tr("Move &Down"), this);
442 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
443 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
444 moveDownAct->setEnabled(false);
445 actionMap.insert("moveDown", moveDownAct);
446 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
448 clearAct = new QAction(tr("&Clear Recent Searches"), this);
449 clearAct->setMenuRole(QAction::ApplicationSpecificRole);
450 clearAct->setShortcuts(QList<QKeySequence>()
451 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
452 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
453 clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
454 clearAct->setEnabled(true);
455 actionMap.insert("clearRecentKeywords", clearAct);
456 connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
458 quitAct = new QAction(tr("&Quit"), this);
459 quitAct->setMenuRole(QAction::QuitRole);
460 quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
461 quitAct->setStatusTip(tr("Bye"));
462 actionMap.insert("quit", quitAct);
463 connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
465 siteAct = new QAction(tr("&Website"), this);
466 siteAct->setShortcut(QKeySequence::HelpContents);
467 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
468 actionMap.insert("site", siteAct);
469 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
471 #if !defined(APP_MAC) && !defined(APP_WIN)
472 donateAct = new QAction(tr("Make a &Donation"), this);
473 donateAct->setStatusTip(
474 tr("Please support the continued development of %1").arg(Constants::NAME));
475 actionMap.insert("donate", donateAct);
476 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
479 aboutAct = new QAction(tr("&About"), this);
480 aboutAct->setMenuRole(QAction::AboutRole);
481 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
482 actionMap.insert("about", aboutAct);
483 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
487 searchFocusAct = new QAction(this);
488 searchFocusAct->setShortcut(QKeySequence::Find);
489 searchFocusAct->setStatusTip(tr("Search"));
490 actionMap.insert("search", searchFocusAct);
491 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
492 addAction(searchFocusAct);
494 volumeUpAct = new QAction(this);
495 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
496 actionMap.insert("volumeUp", volumeUpAct);
497 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
498 addAction(volumeUpAct);
500 volumeDownAct = new QAction(this);
501 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
502 actionMap.insert("volumeDown", volumeDownAct);
503 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
504 addAction(volumeDownAct);
506 volumeMuteAct = new QAction(this);
507 IconUtils::setIcon(volumeMuteAct, "audio-volume-high");
508 volumeMuteAct->setStatusTip(tr("Mute volume"));
509 volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_M));
510 actionMap.insert("volumeMute", volumeMuteAct);
511 connect(volumeMuteAct, SIGNAL(triggered()), SLOT(toggleVolumeMute()));
512 addAction(volumeMuteAct);
514 QToolButton *definitionButton = new QToolButton(this);
515 definitionButton->setText(YT3::instance().maxVideoDefinition().getName());
516 IconUtils::setIcon(definitionButton, "video-display");
517 definitionButton->setIconSize(QSize(16, 16));
518 definitionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
519 definitionButton->setPopupMode(QToolButton::InstantPopup);
520 QMenu *definitionMenu = new QMenu(this);
521 QActionGroup *group = new QActionGroup(this);
522 for (auto &defName : VideoDefinition::getDefinitionNames()) {
523 QAction *a = new QAction(defName);
524 a->setCheckable(true);
525 a->setActionGroup(group);
526 a->setChecked(defName == YT3::instance().maxVideoDefinition().getName());
527 connect(a, &QAction::triggered, this, [this, defName, definitionButton] {
528 setDefinitionMode(defName);
529 definitionButton->setText(defName);
531 connect(&YT3::instance(), &YT3::maxVideoDefinitionChanged, this,
532 [defName, definitionButton](const QString &name) {
533 if (defName == name) definitionButton->setChecked(true);
535 definitionMenu->addAction(a);
537 definitionButton->setMenu(definitionMenu);
538 QWidgetAction *definitionAct = new QWidgetAction(this);
539 definitionAct->setDefaultWidget(definitionButton);
540 definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
541 actionMap.insert("definition", definitionAct);
542 addAction(definitionAct);
546 action = new QAction(tr("&Manually Start Playing"), this);
547 IconUtils::setIcon(action, "media-playback-start");
548 action->setStatusTip(tr("Manually start playing videos"));
549 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
550 action->setCheckable(true);
551 connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
552 actionMap.insert("manualplay", action);
554 action = new QAction(tr("&Downloads"), this);
555 IconUtils::setIcon(action, "document-save");
556 action->setStatusTip(tr("Show details about video downloads"));
557 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
558 action->setCheckable(true);
559 connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
560 actionMap.insert("downloads", action);
562 action = new QAction(tr("&Download"), this);
563 IconUtils::setIcon(action, "document-save");
564 action->setStatusTip(tr("Download the current video"));
565 action->setShortcut(QKeySequence::Save);
566 action->setEnabled(false);
567 action->setVisible(false);
568 action->setPriority(QAction::LowPriority);
569 connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
570 actionMap.insert("download", action);
573 action = new QAction(tr("Take &Snapshot"), this);
574 action->setShortcut(QKeySequence(Qt::Key_F9));
575 action->setEnabled(false);
576 actionMap.insert("snapshot", action);
577 connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
580 action = new QAction(tr("&Subscribe to Channel"), this);
581 action->setProperty("originalText", action->text());
582 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
583 action->setEnabled(false);
584 connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
585 actionMap.insert("subscribeChannel", action);
586 mediaView->updateSubscriptionActionForVideo(0, false);
588 QString shareTip = tr("Share the current video using %1");
590 action = new QAction("&Twitter", this);
591 IconUtils::setIcon(action, "twitter");
592 action->setStatusTip(shareTip.arg("Twitter"));
593 action->setEnabled(false);
594 actionMap.insert("twitter", action);
595 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
597 action = new QAction("&Facebook", this);
598 IconUtils::setIcon(action, "facebook");
599 action->setStatusTip(shareTip.arg("Facebook"));
600 action->setEnabled(false);
601 actionMap.insert("facebook", action);
602 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
604 action = new QAction(tr("&Email"), this);
605 IconUtils::setIcon(action, "email");
606 action->setStatusTip(shareTip.arg(tr("Email")));
607 action->setEnabled(false);
608 actionMap.insert("email", action);
609 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
611 action = new QAction(tr("&Close"), this);
612 action->setShortcut(QKeySequence(QKeySequence::Close));
613 actionMap.insert("close", action);
614 connect(action, SIGNAL(triggered()), SLOT(close()));
616 action = new QAction(Constants::NAME, this);
617 action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
618 actionMap.insert("restore", action);
619 connect(action, SIGNAL(triggered()), SLOT(restore()));
621 action = new QAction(tr("&Float on Top"), this);
622 IconUtils::setIcon(action, "go-top");
623 action->setCheckable(true);
624 actionMap.insert("ontop", action);
625 connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
627 action = new QAction(tr("&Stop After This Video"), this);
628 IconUtils::setIcon(action, "media-playback-stop");
629 action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
630 action->setCheckable(true);
631 action->setEnabled(false);
632 actionMap.insert("stopafterthis", action);
633 connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
635 action = new QAction(tr("&Report an Issue..."), this);
636 actionMap.insert("reportIssue", action);
637 connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
639 action = new QAction(tr("&Refine Search..."), this);
640 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
641 action->setCheckable(true);
642 action->setEnabled(false);
643 actionMap.insert("refineSearch", action);
645 action = new QAction(YTRegions::worldwideRegion().name, this);
646 actionMap.insert("worldwideRegion", action);
648 action = new QAction(YTRegions::localRegion().name, this);
649 actionMap.insert("localRegion", action);
651 action = new QAction(tr("More..."), this);
652 actionMap.insert("moreRegion", action);
654 action = new QAction(tr("&Related Videos"), this);
655 IconUtils::setIcon(action, "view-list");
656 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
657 action->setStatusTip(tr("Watch videos related to the current one"));
658 action->setEnabled(false);
659 action->setPriority(QAction::LowPriority);
660 connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
661 actionMap.insert("relatedVideos", action);
663 action = new QAction(tr("Open in &Browser..."), this);
664 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
665 action->setEnabled(false);
666 actionMap.insert("openInBrowser", action);
667 connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
669 action = new QAction(tr("Restricted Mode"), this);
670 IconUtils::setIcon(action, "safesearch");
671 action->setStatusTip(tr("Hide videos that may contain inappropriate content"));
672 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
673 action->setCheckable(true);
674 action->setVisible(VideoAPI::impl() != VideoAPI::IV);
675 actionMap.insert("safeSearch", action);
677 action = new QAction(tr("Toggle &Menu Bar"), this);
678 connect(action, SIGNAL(triggered()), SLOT(toggleMenuVisibilityWithMessage()));
679 actionMap.insert("toggleMenu", action);
681 action = new QAction(tr("Menu"), this);
682 IconUtils::setIcon(action, "open-menu");
683 connect(action, SIGNAL(triggered()), SLOT(toggleToolbarMenu()));
684 actionMap.insert("toolbarMenu", action);
687 action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
688 actionMap.insert("appStore", action);
689 connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
692 #ifdef APP_ACTIVATION
693 ActivationView::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
696 // common action properties
697 for (QAction *action : qAsConst(actionMap)) {
698 // add actions to the MainWindow so that they work
699 // when the menu is hidden
705 void MainWindow::createMenus() {
706 fileMenu = menuBar()->addMenu(tr("&Application"));
707 #ifdef APP_ACTIVATION
708 QAction *buyAction = getAction("buy");
709 if (buyAction) fileMenu->addAction(buyAction);
711 fileMenu->addSeparator();
714 fileMenu->addAction(clearAct);
716 fileMenu->addSeparator();
718 fileMenu->addAction(quitAct);
720 QMenu *playbackMenu = menuBar()->addMenu(tr("&Playback"));
721 menuMap.insert("playback", playbackMenu);
722 playbackMenu->addAction(pauseAct);
723 playbackMenu->addAction(stopAct);
724 playbackMenu->addAction(getAction("stopafterthis"));
725 playbackMenu->addSeparator();
726 playbackMenu->addAction(skipAct);
727 playbackMenu->addAction(skipBackwardAct);
728 playbackMenu->addSeparator();
729 playbackMenu->addAction(getAction("manualplay"));
731 MacSupport::dockMenu(playbackMenu);
734 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
735 menuMap.insert("playlist", playlistMenu);
736 playlistMenu->addAction(removeAct);
737 playlistMenu->addSeparator();
738 playlistMenu->addAction(moveUpAct);
739 playlistMenu->addAction(moveDownAct);
740 playlistMenu->addSeparator();
741 playlistMenu->addAction(getAction("refineSearch"));
743 QMenu *videoMenu = menuBar()->addMenu(tr("&Video"));
744 menuMap.insert("video", videoMenu);
745 videoMenu->addAction(getAction("relatedVideos"));
746 videoMenu->addAction(findVideoPartsAct);
747 videoMenu->addSeparator();
748 videoMenu->addAction(getAction("subscribeChannel"));
750 videoMenu->addSeparator();
751 videoMenu->addAction(getAction("snapshot"));
753 videoMenu->addSeparator();
754 videoMenu->addAction(webPageAct);
755 videoMenu->addAction(copyLinkAct);
756 videoMenu->addAction(getAction("openInBrowser"));
757 videoMenu->addAction(getAction("download"));
759 QMenu *shareMenu = menuBar()->addMenu(tr("&Share"));
760 menuMap.insert("share", shareMenu);
761 shareMenu->addAction(copyPageAct);
762 shareMenu->addSeparator();
763 shareMenu->addAction(getAction("twitter"));
764 shareMenu->addAction(getAction("facebook"));
765 shareMenu->addSeparator();
766 shareMenu->addAction(getAction("email"));
768 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
769 menuMap.insert("view", viewMenu);
770 viewMenu->addAction(getAction("ontop"));
771 viewMenu->addAction(compactViewAct);
772 viewMenu->addSeparator();
773 viewMenu->addAction(fullscreenAct);
775 viewMenu->addSeparator();
776 viewMenu->addAction(getAction("toggleMenu"));
780 MacSupport::windowMenu(this);
783 helpMenu = menuBar()->addMenu(tr("&Help"));
784 menuMap.insert("help", helpMenu);
785 helpMenu->addAction(siteAct);
786 #if !defined(APP_MAC) && !defined(APP_WIN)
787 helpMenu->addAction(donateAct);
789 helpMenu->addAction(getAction("reportIssue"));
790 helpMenu->addAction(aboutAct);
792 helpMenu->addAction(Updater::instance().getAction());
796 helpMenu->addSeparator();
797 helpMenu->addAction(getAction("appStore"));
801 void MainWindow::createToolBar() {
803 currentTimeLabel = new QLabel("00:00", this);
805 seekSlider = new SeekSlider(this);
806 seekSlider->setEnabled(false);
807 seekSlider->setTracking(false);
808 seekSlider->setMaximum(1000);
809 volumeSlider = new SeekSlider(this);
810 volumeSlider->setValue(volumeSlider->maximum());
812 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
813 SearchWrapper *searchWrapper = new SearchWrapper(this);
814 toolbarSearch = searchWrapper->getSearchLineEdit();
816 toolbarSearch = new SearchLineEdit(this);
818 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize() * 15);
819 toolbarSearch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
820 toolbarSearch->setSuggester(new YTSuggester(this));
821 connect(toolbarSearch, SIGNAL(search(const QString &)), SLOT(search(const QString &)));
822 connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion *)),
823 SLOT(suggestionAccepted(Suggestion *)));
824 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
826 // Add widgets to toolbar
828 #ifdef APP_MAC_QMACTOOLBAR
829 currentTimeLabel->hide();
830 toolbarSearch->hide();
831 volumeSlider->hide();
833 MacToolbar::instance().createToolbar(this);
837 mainToolBar = new QToolBar(this);
838 mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
839 mainToolBar->setFloatable(false);
840 mainToolBar->setMovable(false);
842 mainToolBar->setIconSize(QSize(32, 32));
844 mainToolBar->addAction(stopAct);
845 QToolButton *stopToolButton =
846 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(stopAct));
847 if (stopToolButton) {
848 QMenu *stopMenu = new QMenu(this);
849 stopMenu->addAction(getAction("stopafterthis"));
850 stopToolButton->setMenu(stopMenu);
851 stopToolButton->setPopupMode(QToolButton::DelayedPopup);
853 mainToolBar->addAction(pauseAct);
854 mainToolBar->addAction(skipAct);
855 mainToolBar->addAction(getAction("relatedVideos"));
857 bool addFullScreenAct = true;
859 addFullScreenAct = !mac::CanGoFullScreen(winId());
861 if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
863 mainToolBar->addWidget(new Spacer());
865 currentTimeLabel->setFont(FontUtils::small());
866 currentTimeLabel->setMinimumWidth(currentTimeLabel->fontInfo().pixelSize() * 4);
867 currentTimeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
868 mainToolBar->addWidget(currentTimeLabel);
871 mainToolBar->addWidget(new Spacer(nullptr, 10));
874 seekSlider->setOrientation(Qt::Horizontal);
875 seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
876 seekSlider->setFocusPolicy(Qt::NoFocus);
877 mainToolBar->addWidget(seekSlider);
879 mainToolBar->addWidget(new Spacer());
881 mainToolBar->addAction(volumeMuteAct);
882 #ifndef APP_MAC_QMACTOOLBAR
883 QToolButton *volumeMuteButton =
884 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
885 volumeMuteButton->setIconSize(QSize(16, 16));
886 auto fixVolumeMuteIconSize = [volumeMuteButton] {
887 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
889 fixVolumeMuteIconSize();
890 volumeMuteButton->connect(volumeMuteAct, &QAction::changed, volumeMuteButton,
891 fixVolumeMuteIconSize);
894 volumeSlider->setStatusTip(
895 tr("Press %1 to raise the volume, %2 to lower it")
896 .arg(volumeUpAct->shortcut().toString(QKeySequence::NativeText),
897 volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
899 volumeSlider->setOrientation(Qt::Horizontal);
900 // this makes the volume slider smaller
901 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
902 volumeSlider->setFocusPolicy(Qt::NoFocus);
903 mainToolBar->addWidget(volumeSlider);
905 mainToolBar->addWidget(new Spacer());
907 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
908 mainToolBar->addWidget(searchWrapper);
910 mainToolBar->addWidget(toolbarSearch);
911 mainToolBar->addWidget(new Spacer(this, toolbarSearch->height() / 2));
913 QAction *toolbarMenuAction = getAction("toolbarMenu");
914 mainToolBar->addAction(toolbarMenuAction);
916 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(toolbarMenuAction));
919 addToolBar(mainToolBar);
922 void MainWindow::createStatusBar() {
923 statusToolBar = new QToolBar(statusBar());
924 statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
925 statusToolBar->setIconSize(QSize(16, 16));
927 regionAction = new QAction(this);
928 regionAction->setStatusTip(tr("Choose your content location"));
930 QAction *localAction = getAction("localRegion");
931 if (!localAction->text().isEmpty()) {
932 QMenu *regionMenu = new QMenu(this);
933 regionMenu->addAction(getAction("worldwideRegion"));
934 regionMenu->addAction(localAction);
935 regionMenu->addSeparator();
936 QAction *moreRegionsAction = getAction("moreRegion");
937 regionMenu->addAction(moreRegionsAction);
938 connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
939 regionAction->setMenu(regionMenu);
941 connect(regionAction, SIGNAL(triggered()), SLOT(showRegionsView()));
943 /* Stupid code that generates the QRC items
944 foreach(YTRegion r, YTRegions::list())
945 qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
948 statusBar()->addPermanentWidget(statusToolBar);
952 void MainWindow::showStopAfterThisInStatusBar(bool show) {
953 QAction *action = getAction("stopafterthis");
954 showActionsInStatusBar({action}, show);
957 void MainWindow::showActionsInStatusBar(const QVector<QAction *> &actions, bool show) {
959 Extra::fadeInWidget(statusBar(), statusBar());
961 for (auto action : actions) {
963 if (statusToolBar->actions().contains(action)) continue;
964 if (statusToolBar->actions().isEmpty()) {
965 statusToolBar->addAction(action);
967 statusToolBar->insertAction(statusToolBar->actions().at(0), action);
970 statusToolBar->removeAction(action);
975 if (statusBar()->isHidden() && !fullScreenActive) setStatusBarVisibility(true);
977 if (statusBar()->isVisible() && !needStatusBar()) setStatusBarVisibility(false);
981 void MainWindow::setStatusBarVisibility(bool show) {
982 if (statusBar()->isVisible() != show) {
983 statusBar()->setVisible(show);
984 if (views->currentWidget() == mediaView)
985 QTimer::singleShot(0, mediaView, SLOT(adjustWindowSize()));
989 void MainWindow::adjustStatusBarVisibility() {
990 setStatusBarVisibility(needStatusBar());
993 void MainWindow::hideToolbar() {
995 mac::showToolBar(winId(), false);
1001 void MainWindow::showToolbar() {
1003 mac::showToolBar(winId(), true);
1005 mainToolBar->show();
1009 void MainWindow::readSettings() {
1011 QByteArray geometrySettings = settings.value("geometry").toByteArray();
1012 if (!geometrySettings.isEmpty()) {
1013 restoreGeometry(geometrySettings);
1015 const QRect desktopSize = QGuiApplication::primaryScreen()->availableGeometry();
1016 int w = desktopSize.width() * .9;
1017 int h = qMin(w / 2, desktopSize.height());
1019 QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, QSize(w, h), desktopSize));
1021 setDefinitionMode(settings.value("definition", YT3::instance().maxVideoDefinition().getName())
1023 getAction("manualplay")->setChecked(settings.value("manualplay", false).toBool());
1024 getAction("safeSearch")->setChecked(settings.value("safeSearch", false).toBool());
1026 menuBar()->setVisible(settings.value("menuBar", false).toBool());
1030 void MainWindow::writeSettings() {
1033 if (!isReallyFullScreen()) {
1034 settings.setValue("geometry", saveGeometry());
1035 if (mediaView) mediaView->saveSplitterState();
1038 settings.setValue("manualplay", getAction("manualplay")->isChecked());
1039 settings.setValue("safeSearch", getAction("safeSearch")->isChecked());
1041 settings.setValue("menuBar", menuBar()->isVisible());
1045 void MainWindow::goBack() {
1046 if (history.size() > 1) {
1048 showView(history.pop());
1052 void MainWindow::showView(View *view, bool transition) {
1053 if (!history.isEmpty() && view == history.top()) {
1054 qDebug() << "Attempting to show same view" << view;
1059 if (transition && !history.isEmpty()) CompositeFader::go(this, this->grab());
1062 if (compactViewAct->isChecked()) compactViewAct->toggle();
1064 // call hide method on the current view
1065 View *oldView = qobject_cast<View *>(views->currentWidget());
1067 oldView->disappear();
1068 oldView->setEnabled(false);
1069 oldView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
1071 qDebug() << "Cannot cast old view";
1073 view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1074 view->setEnabled(true);
1075 views->setCurrentWidget(view);
1078 QString title = view->getTitle();
1079 if (title.isEmpty())
1080 title = Constants::NAME;
1082 title += QLatin1String(" - ") + Constants::NAME;
1083 setWindowTitle(title);
1085 const bool isMediaView = view == mediaView;
1086 stopAct->setEnabled(isMediaView);
1087 compactViewAct->setEnabled(isMediaView);
1088 toolbarSearch->setEnabled(isMediaView);
1089 aboutAct->setEnabled(view != aboutView);
1090 getAction("downloads")->setChecked(view == downloadView);
1092 // dynamic view actions
1093 /* Not currently used by any view
1094 showActionsInStatusBar(viewActions, false);
1095 viewActions = newView->getViewActions();
1096 showActionsInStatusBar(viewActions, true);
1103 void MainWindow::about() {
1105 aboutView = new AboutView(this);
1106 views->addWidget(aboutView);
1108 showView(aboutView);
1111 void MainWindow::visitSite() {
1112 QUrl url(Constants::WEBSITE);
1113 showMessage(QString(tr("Opening %1").arg(url.toString())));
1114 QDesktopServices::openUrl(url);
1117 void MainWindow::donate() {
1118 QUrl url("https://" + QLatin1String(Constants::ORG_DOMAIN) + "/donate");
1119 showMessage(QString(tr("Opening %1").arg(url.toString())));
1120 QDesktopServices::openUrl(url);
1123 void MainWindow::reportIssue() {
1124 QUrl url("https://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
1125 QDesktopServices::openUrl(url);
1128 void MainWindow::quit() {
1130 if (!confirmQuit()) {
1134 // do not save geometry when in full screen or in compact mode
1135 if (!fullScreenActive && !compactViewAct->isChecked()) {
1141 // mediaView->stop();
1142 Temporary::deleteAll();
1143 ChannelAggregator::instance()->stop();
1144 ChannelAggregator::instance()->cleanup();
1145 Database::shutdown();
1149 void MainWindow::closeEvent(QCloseEvent *e) {
1151 mac::closeWindow(winId());
1154 if (!confirmQuit()) {
1158 QWidget::closeEvent(e);
1161 messageLabel->hide();
1164 void MainWindow::showEvent(QShowEvent *e) {
1165 QWidget::showEvent(e);
1171 bool MainWindow::confirmQuit() {
1172 if (DownloadManager::instance()->activeItems() > 0) {
1173 QMessageBox msgBox(this);
1174 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1176 tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1177 msgBox.setInformativeText(
1178 tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1179 msgBox.setModal(true);
1180 // make it a "sheet" on the Mac
1181 msgBox.setWindowModality(Qt::WindowModal);
1183 msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1184 QPushButton *waitButton =
1185 msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1189 if (msgBox.clickedButton() == waitButton) {
1196 void MainWindow::showHome() {
1198 currentTimeLabel->clear();
1199 seekSlider->setValue(0);
1202 void MainWindow::showMedia(SearchParams *searchParams) {
1203 showView(mediaView);
1204 if (getAction("safeSearch")->isChecked())
1205 searchParams->setSafeSearch(SearchParams::Strict);
1207 searchParams->setSafeSearch(SearchParams::None);
1208 mediaView->search(searchParams);
1211 void MainWindow::showMedia(VideoSource *videoSource) {
1212 showView(mediaView);
1213 mediaView->setVideoSource(videoSource);
1216 void MainWindow::stateChanged(Media::State newState) {
1217 qDebug() << newState;
1219 seekSlider->setEnabled(newState != Media::StoppedState);
1222 case Media::ErrorState:
1223 showMessage(tr("Error: %1").arg(media->errorString()));
1226 case Media::PlayingState:
1227 pauseAct->setEnabled(true);
1228 pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1229 pauseAct->setText(tr("&Pause"));
1230 pauseAct->setStatusTip(tr("Pause playback") + " (" +
1231 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1234 case Media::StoppedState:
1235 pauseAct->setEnabled(false);
1236 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1237 pauseAct->setText(tr("&Play"));
1238 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1239 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1242 case Media::PausedState:
1243 pauseAct->setEnabled(true);
1244 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1245 pauseAct->setText(tr("&Play"));
1246 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1247 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1250 case Media::BufferingState:
1251 pauseAct->setEnabled(false);
1252 pauseAct->setIcon(IconUtils::icon("content-loading"));
1253 pauseAct->setText(tr("&Loading..."));
1254 pauseAct->setStatusTip(QString());
1257 case Media::LoadingState:
1258 pauseAct->setEnabled(false);
1259 currentTimeLabel->clear();
1266 void MainWindow::stop() {
1271 void MainWindow::resizeEvent(QResizeEvent *e) {
1274 if (initialized && mac::CanGoFullScreen(winId())) {
1275 bool isFullscreen = mac::IsFullScreen(winId());
1276 if (isFullscreen != fullScreenActive) {
1277 if (compactViewAct->isChecked()) {
1278 compactViewAct->setChecked(false);
1281 fullScreenActive = isFullscreen;
1282 updateUIForFullscreen();
1286 #ifdef APP_MAC_QMACTOOLBAR
1287 int moreButtonWidth = 40;
1288 toolbarSearch->move(width() - toolbarSearch->width() - moreButtonWidth - 7, -34);
1293 void MainWindow::enterEvent(QEvent *e) {
1296 // Workaround cursor bug on macOS
1301 void MainWindow::leaveEvent(QEvent *e) {
1303 if (fullScreenActive) hideFullscreenUI();
1306 void MainWindow::toggleFullscreen() {
1307 if (compactViewAct->isChecked()) compactViewAct->toggle();
1310 WId handle = winId();
1311 if (mac::CanGoFullScreen(handle)) {
1312 if (mainToolBar) mainToolBar->setVisible(true);
1313 mac::ToggleFullScreen(handle);
1318 fullScreenActive = !fullScreenActive;
1320 if (fullScreenActive) {
1321 // Enter full screen
1323 maximizedBeforeFullScreen = isMaximized();
1325 // save geometry now, if the user quits when in full screen
1326 // geometry won't be saved
1330 MacSupport::enterFullScreen(this, views);
1332 menuVisibleBeforeFullScreen = menuBar()->isVisible();
1334 if (mainToolBar) mainToolBar->hide();
1342 MacSupport::exitFullScreen(this, views);
1344 menuBar()->setVisible(menuVisibleBeforeFullScreen);
1345 if (mainToolBar) mainToolBar->setVisible(views->currentWidget() == mediaView);
1346 if (maximizedBeforeFullScreen)
1352 // Make sure the window has focus
1356 qApp->processEvents();
1357 updateUIForFullscreen();
1360 void MainWindow::updateUIForFullscreen() {
1361 static QList<QKeySequence> fsShortcuts;
1362 static QString fsText;
1364 if (fullScreenActive) {
1365 fsShortcuts = fullscreenAct->shortcuts();
1366 fsText = fullscreenAct->text();
1367 if (fsText.isEmpty()) qDebug() << "[taking Empty!]";
1368 fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1369 << QKeySequence(Qt::Key_Escape));
1370 fullscreenAct->setText(tr("Leave &Full Screen"));
1371 fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1372 setStatusBarVisibility(false);
1375 removeToolBar(mainToolBar);
1376 mainToolBar->move(0, 0);
1379 mediaView->removeSidebar();
1382 fullscreenAct->setShortcuts(fsShortcuts);
1383 if (fsText.isEmpty()) fsText = "[Empty!]";
1384 fullscreenAct->setText(fsText);
1385 fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1387 if (needStatusBar()) setStatusBarVisibility(true);
1390 addToolBar(mainToolBar);
1393 mediaView->restoreSidebar();
1396 // No compact view action when in full screen
1397 compactViewAct->setVisible(!fullScreenActive);
1398 compactViewAct->setChecked(false);
1400 // Hide anything but the video
1401 mediaView->setSidebarVisibility(!fullScreenActive);
1403 if (fullScreenActive) {
1404 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1406 stopAct->setShortcuts(QList<QKeySequence>()
1407 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1411 MacSupport::fullScreenActions(actionMap, fullScreenActive);
1414 if (views->currentWidget() == mediaView) mediaView->setFocus();
1416 if (fullScreenActive) {
1417 if (views->currentWidget() == mediaView) hideFullscreenUI();
1419 fullscreenTimer->stop();
1424 bool MainWindow::isReallyFullScreen() {
1426 WId handle = winId();
1427 if (mac::CanGoFullScreen(handle))
1428 return mac::IsFullScreen(handle);
1430 return isFullScreen();
1432 return isFullScreen();
1436 void MainWindow::missingKeyWarning() {
1437 static bool shown = false;
1440 QMessageBox msgBox(this);
1441 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1442 msgBox.setText(QString("%1 was built without a Google API key.").arg(Constants::NAME));
1443 msgBox.setInformativeText(QString("It won't work unless you enter one."
1444 "<p>In alternative you can get %1 from the developer site.")
1445 .arg(Constants::NAME));
1446 msgBox.setModal(true);
1447 msgBox.setWindowModality(Qt::WindowModal);
1448 msgBox.addButton(QMessageBox::Close);
1449 QPushButton *enterKeyButton =
1450 msgBox.addButton(QString("Enter API key..."), QMessageBox::AcceptRole);
1451 QPushButton *devButton = msgBox.addButton(QString("Get from %1").arg(Constants::WEBSITE),
1452 QMessageBox::AcceptRole);
1453 QPushButton *helpButton = msgBox.addButton(QMessageBox::Help);
1457 if (msgBox.clickedButton() == helpButton) {
1458 QDesktopServices::openUrl(QUrl("https://github.com/flaviotordini/minitube/blob/master/"
1459 "README.md#google-api-key"));
1460 } else if (msgBox.clickedButton() == enterKeyButton) {
1462 QString text = QInputDialog::getText(this, QString(), "Google API key:", QLineEdit::Normal,
1464 if (ok && !text.isEmpty()) {
1466 settings.setValue("googleApiKey", text);
1467 YT3::instance().initApiKeys();
1469 } else if (msgBox.clickedButton() == devButton) {
1470 QDesktopServices::openUrl(QUrl(Constants::WEBSITE));
1475 void MainWindow::compactView(bool enable) {
1476 setUpdatesEnabled(false);
1478 compactModeActive = enable;
1480 static QList<QKeySequence> compactShortcuts;
1481 static QList<QKeySequence> stopShortcuts;
1483 const QString key = "compactGeometry";
1487 setMinimumSize(320, 180);
1489 mac::RemoveFullScreenWindow(winId());
1493 if (settings.contains(key))
1494 restoreGeometry(settings.value(key).toByteArray());
1498 #ifdef APP_MAC_QMACTOOLBAR
1499 mac::showToolBar(winId(), !enable);
1501 mainToolBar->setVisible(!enable);
1503 mediaView->setSidebarVisibility(!enable);
1504 statusBar()->hide();
1506 compactShortcuts = compactViewAct->shortcuts();
1507 stopShortcuts = stopAct->shortcuts();
1509 QList<QKeySequence> newStopShortcuts(stopShortcuts);
1510 newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1511 stopAct->setShortcuts(newStopShortcuts);
1512 compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts)
1513 << QKeySequence(Qt::Key_Escape));
1515 // ensure focus does not end up to the search box
1516 // as it would steal the Space shortcut
1517 mediaView->setFocus();
1520 settings.setValue(key, saveGeometry());
1522 // unset minimum size
1523 setMinimumSize(0, 0);
1526 mac::SetupFullScreenWindow(winId());
1528 #ifdef APP_MAC_QMACTOOLBAR
1529 mac::showToolBar(winId(), !enable);
1531 mainToolBar->setVisible(!enable);
1533 mediaView->setSidebarVisibility(!enable);
1534 if (needStatusBar()) setStatusBarVisibility(true);
1538 compactViewAct->setShortcuts(compactShortcuts);
1539 stopAct->setShortcuts(stopShortcuts);
1542 // auto float on top
1543 floatOnTop(enable, false);
1546 mac::compactMode(winId(), enable);
1549 menuVisibleBeforeCompactMode = menuBar()->isVisible();
1552 menuBar()->setVisible(menuVisibleBeforeCompactMode);
1556 setUpdatesEnabled(true);
1559 void MainWindow::toggleToolbarMenu() {
1560 if (!toolbarMenu) toolbarMenu = new ToolbarMenu(this);
1561 if (toolbarMenu->isVisible())
1562 toolbarMenu->hide();
1564 toolbarMenu->show();
1567 void MainWindow::searchFocus() {
1568 toolbarSearch->selectAll();
1569 toolbarSearch->setFocus();
1572 void MainWindow::initMedia() {
1574 qFatal("QtAV has a showstopper bug. Audio stops randomly. See bug "
1575 "https://github.com/wang-bin/QtAV/issues/1184");
1576 media = new MediaQtAV(this);
1577 #elif defined MEDIA_MPV
1578 media = new MediaMPV();
1580 qFatal("No media backend defined");
1583 media->setUserAgent(HttpUtils::stealthUserAgent());
1586 qreal volume = settings.value("volume", 1.).toReal();
1587 media->setVolume(volume);
1589 connect(media, &Media::error, this, &MainWindow::handleError);
1590 connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
1591 connect(media, &Media::positionChanged, this, &MainWindow::tick);
1593 connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
1594 // value : maxValue = posit ion : duration
1595 qint64 ms = (value * media->duration()) / seekSlider->maximum();
1596 qDebug() << "Seeking to" << ms;
1598 if (media->state() == Media::PausedState) media->play();
1600 connect(seekSlider, &QSlider::sliderPressed, this, [this]() {
1601 // value : maxValue = position : duration
1602 qint64 ms = (seekSlider->value() * media->duration()) / seekSlider->maximum();
1604 if (media->state() == Media::PausedState) media->play();
1606 connect(media, &Media::started, this, [this]() { seekSlider->setValue(0); });
1608 connect(media, &Media::volumeChanged, this, &MainWindow::volumeChanged);
1609 connect(media, &Media::volumeMutedChanged, this, &MainWindow::volumeMutedChanged);
1610 connect(volumeSlider, &QSlider::sliderMoved, this, [this](int value) {
1611 qreal volume = (qreal)value / volumeSlider->maximum();
1612 media->setVolume(volume);
1614 connect(volumeSlider, &QSlider::sliderPressed, this, [this]() {
1615 qreal volume = (qreal)volumeSlider->value() / volumeSlider->maximum();
1616 media->setVolume(volume);
1619 mediaView->setMedia(media);
1622 void MainWindow::tick(qint64 time) {
1624 bool isDown = seekSlider->property("down").isValid();
1626 bool isDown = seekSlider->isSliderDown();
1628 if (!isDown && media->state() == Media::PlayingState) {
1629 // value : maxValue = position : duration
1630 qint64 duration = media->duration();
1631 if (duration <= 0) return;
1632 int value = (seekSlider->maximum() * media->position()) / duration;
1633 seekSlider->setValue(value);
1636 const QString s = formatTime(time);
1637 if (s != currentTimeLabel->text()) {
1638 currentTimeLabel->setText(s);
1639 emit currentTimeChanged(s);
1642 const qint64 remainingTime = media->remainingTime();
1643 currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1647 QString MainWindow::formatTime(qint64 duration) {
1650 int seconds = (int)(duration % 60);
1652 int minutes = (int)(duration % 60);
1654 int hours = (int)(duration % 24);
1655 if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
1656 return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
1659 void MainWindow::volumeUp() {
1660 qreal newVolume = media->volume() + .1;
1661 if (newVolume > 1.) newVolume = 1.;
1662 media->setVolume(newVolume);
1665 void MainWindow::volumeDown() {
1666 qreal newVolume = media->volume() - .1;
1667 if (newVolume < 0) newVolume = 0;
1668 media->setVolume(newVolume);
1671 void MainWindow::toggleVolumeMute() {
1672 bool muted = media->volumeMuted();
1673 media->setVolumeMuted(!muted);
1676 void MainWindow::volumeChanged(qreal newVolume) {
1677 // automatically unmute when volume changes
1678 if (media->volumeMuted()) media->setVolumeMuted(false);
1679 showMessage(tr("Volume at %1%").arg((int)(newVolume * 100)));
1680 // newVolume : 1.0 = x : 1000
1681 int value = newVolume * volumeSlider->maximum();
1682 volumeSlider->blockSignals(true);
1683 volumeSlider->setValue(value);
1684 volumeSlider->blockSignals(false);
1687 void MainWindow::volumeMutedChanged(bool muted) {
1689 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1690 showMessage(tr("Volume is muted"));
1692 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1693 showMessage(tr("Volume is unmuted"));
1697 void MainWindow::setDefinitionMode(const QString &definitionName) {
1698 QAction *definitionAct = getAction("definition");
1699 definitionAct->setText(definitionName);
1700 definitionAct->setStatusTip(
1701 tr("Maximum video definition set to %1").arg(definitionAct->text()) + " (" +
1702 definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1703 showMessage(definitionAct->statusTip());
1704 YT3::instance().setMaxVideoDefinition(definitionName);
1705 if (views->currentWidget() == mediaView) {
1706 mediaView->reloadCurrentVideo();
1710 void MainWindow::toggleDefinitionMode() {
1711 const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
1712 const VideoDefinition ¤tDefinition = YT3::instance().maxVideoDefinition();
1714 int index = definitions.indexOf(currentDefinition);
1715 if (index != definitions.size() - 1) {
1720 setDefinitionMode(definitions.at(index).getName());
1723 void MainWindow::clearRecentKeywords() {
1725 settings.remove("recentKeywords");
1726 settings.remove("recentChannels");
1727 if (views->currentWidget() == homeView) {
1728 SearchView *searchView = homeView->getSearchView();
1729 searchView->updateRecentKeywords();
1730 searchView->updateRecentChannels();
1732 HttpUtils::clearCaches();
1733 showMessage(tr("Your privacy is now safe"));
1736 void MainWindow::setManualPlay(bool enabled) {
1738 settings.setValue("manualplay", QVariant::fromValue(enabled));
1739 if (views->currentWidget() == homeView &&
1740 homeView->currentWidget() == homeView->getSearchView())
1742 showActionsInStatusBar({getAction("manualplay")}, enabled);
1745 void MainWindow::updateDownloadMessage(const QString &message) {
1746 getAction("downloads")->setText(message);
1749 void MainWindow::downloadsFinished() {
1750 getAction("downloads")->setText(tr("&Downloads"));
1751 showMessage(tr("Downloads complete"));
1754 void MainWindow::toggleDownloads(bool show) {
1756 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1757 getAction("downloads")
1758 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1759 << QKeySequence(Qt::Key_Escape));
1761 getAction("downloads")
1762 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1763 stopAct->setShortcuts(QList<QKeySequence>()
1764 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1767 if (!downloadView) {
1768 downloadView = new DownloadView(this);
1769 views->addWidget(downloadView);
1772 showView(downloadView);
1777 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1778 search(suggestion->value);
1781 void MainWindow::search(const QString &query) {
1782 QString q = query.simplified();
1783 if (q.isEmpty()) return;
1784 SearchParams *searchParams = new SearchParams();
1785 searchParams->setKeywords(q);
1786 showMedia(searchParams);
1789 void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
1790 if (e->mimeData()->hasFormat("text/uri-list")) {
1791 QList<QUrl> urls = e->mimeData()->urls();
1792 if (urls.isEmpty()) return;
1793 const QUrl &url = urls.at(0);
1794 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1795 if (!videoId.isEmpty()) e->acceptProposedAction();
1799 void MainWindow::dropEvent(QDropEvent *e) {
1800 if (!toolbarSearch->isEnabled()) return;
1802 QList<QUrl> urls = e->mimeData()->urls();
1803 if (urls.isEmpty()) return;
1804 const QUrl &url = urls.at(0);
1805 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1806 if (!videoId.isEmpty()) {
1807 setWindowTitle(url.toString());
1808 SearchParams *searchParams = new SearchParams();
1809 searchParams->setKeywords(videoId);
1810 showMedia(searchParams);
1814 bool MainWindow::needStatusBar() {
1815 return !statusToolBar->actions().isEmpty();
1818 void MainWindow::adjustMessageLabelPosition() {
1819 if (messageLabel->parent() == this)
1820 messageLabel->move(0, height() - messageLabel->height());
1822 messageLabel->move(mapToGlobal(QPoint(0, height() - messageLabel->height())));
1825 void MainWindow::floatOnTop(bool onTop, bool showAction) {
1826 if (showAction) showActionsInStatusBar({getAction("ontop")}, onTop);
1828 mac::floatOnTop(winId(), onTop);
1831 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1834 setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1840 void MainWindow::restore() {
1842 mac::uncloseWindow(winId());
1846 void MainWindow::messageReceived(const QString &message) {
1847 if (message == QLatin1String("--toggle-playing")) {
1848 if (pauseAct->isEnabled()) pauseAct->trigger();
1849 } else if (message == QLatin1String("--next")) {
1850 if (skipAct->isEnabled()) skipAct->trigger();
1851 } else if (message == QLatin1String("--previous")) {
1852 if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1853 } else if (message == QLatin1String("--stop-after-this")) {
1854 getAction("stopafterthis")->toggle();
1855 } else if (message.startsWith("--")) {
1856 MainWindow::printHelp();
1857 } else if (!message.isEmpty()) {
1858 SearchParams *searchParams = new SearchParams();
1859 searchParams->setKeywords(message);
1860 showMedia(searchParams);
1864 void MainWindow::hideFullscreenUI() {
1865 if (views->currentWidget() != mediaView) return;
1866 setCursor(Qt::BlankCursor);
1868 QPoint p = mapFromGlobal(QCursor::pos());
1869 const int x = p.x();
1871 if (x > mediaView->getSidebar()->width()) mediaView->setSidebarVisibility(false);
1874 const int y = p.y();
1875 bool shouldHideToolbar = !toolbarSearch->hasFocus() && y > mainToolBar->height();
1876 if (shouldHideToolbar) mainToolBar->setVisible(false);
1880 void MainWindow::toggleMenuVisibility() {
1881 bool show = !menuBar()->isVisible();
1882 menuBar()->setVisible(show);
1885 void MainWindow::toggleMenuVisibilityWithMessage() {
1886 bool show = !menuBar()->isVisible();
1887 menuBar()->setVisible(show);
1889 QMessageBox msgBox(this);
1890 msgBox.setText(tr("You can still access the menu bar by pressing the ALT key"));
1891 msgBox.setModal(true);
1892 msgBox.setWindowModality(Qt::WindowModal);
1897 #ifdef APP_MAC_STORE
1898 void MainWindow::rateOnAppStore() {
1899 QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1900 "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1901 "?id=422006190&type=Purple+Software"));
1905 void MainWindow::printHelp() {
1906 QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1907 msg += "Usage: minitube [options]\n";
1908 msg += "Options:\n";
1909 msg += " --toggle-playing\t";
1910 msg += "Start or pause playback.\n";
1911 msg += " --next\t\t";
1912 msg += "Skip to the next video.\n";
1913 msg += " --previous\t\t";
1914 msg += "Go back to the previous video.\n";
1915 msg += " --stop-after-this\t";
1916 msg += "Stop playback at the end of the video.\n";
1917 std::cout << msg.toLocal8Bit().data();
1920 void MainWindow::setupAction(QAction *action) {
1921 // never autorepeat.
1922 // unexperienced users tend to keep keys pressed for a "long" time
1923 action->setAutoRepeat(false);
1925 // show keyboard shortcuts in the status bar
1926 if (!action->shortcut().isEmpty())
1927 action->setStatusTip(action->statusTip() + QLatin1String(" (") +
1928 action->shortcut().toString(QKeySequence::NativeText) +
1929 QLatin1String(")"));
1932 QAction *MainWindow::getAction(const char *name) {
1933 return actionMap.value(QByteArray::fromRawData(name, strlen(name)));
1936 void MainWindow::addNamedAction(const QByteArray &name, QAction *action) {
1937 actionMap.insert(name, action);
1940 QMenu *MainWindow::getMenu(const char *name) {
1941 return menuMap.value(QByteArray::fromRawData(name, strlen(name)));
1944 void MainWindow::showMessage(const QString &message) {
1945 if (!isVisible()) return;
1947 if (!mac::isVisible(winId())) return;
1949 if (statusBar()->isVisible())
1950 statusBar()->showMessage(message, 60000);
1951 else if (isActiveWindow()) {
1952 messageLabel->setText(message);
1953 QSize size = messageLabel->sizeHint();
1954 // round width to avoid flicker with fast changing messages (e.g. volume
1956 int w = size.width() + 10;
1957 const int multiple = 15;
1958 w = w + multiple / 2;
1961 messageLabel->resize(size);
1962 if (messageLabel->isHidden()) {
1963 adjustMessageLabelPosition();
1964 messageLabel->show();
1966 messageTimer->start();
1970 void MainWindow::hideMessage() {
1971 if (messageLabel->isVisible()) {
1972 messageLabel->hide();
1973 messageLabel->clear();
1977 void MainWindow::handleError(const QString &message) {
1978 qWarning() << message;
1979 showMessage(message);
1982 #ifdef APP_ACTIVATION
1983 void MainWindow::showActivationView() {
1984 View *activationView = ActivationView::instance();
1985 views->addWidget(activationView);
1986 if (views->currentWidget() != activationView) showView(activationView);
1990 void MainWindow::showRegionsView() {
1992 regionsView = new RegionsView(this);
1993 connect(regionsView, SIGNAL(regionChanged()), homeView->getStandardFeedsView(),
1995 views->addWidget(regionsView);
1997 showView(regionsView);