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"
85 #include "mediaqtav.h"
96 MainWindow *mainWindowInstance;
99 MainWindow *MainWindow::instance() {
100 return mainWindowInstance;
103 MainWindow::MainWindow()
104 : aboutView(nullptr), downloadView(nullptr), regionsView(nullptr), mainToolBar(nullptr),
105 fullScreenActive(false), compactModeActive(false), initialized(false), toolbarMenu(nullptr),
107 mainWindowInstance = this;
110 views = new QStackedWidget();
111 setCentralWidget(views);
114 Extra::windowSetup(this);
117 messageLabel = new QLabel(this);
118 messageLabel->setWordWrap(false);
119 messageLabel->setStyleSheet("padding:5px;border:0;background:palette(window)");
120 messageLabel->setAlignment(Qt::AlignCenter);
121 messageLabel->hide();
122 adjustMessageLabelPosition();
123 messageTimer = new QTimer(this);
124 messageTimer->setInterval(5000);
125 messageTimer->setSingleShot(true);
126 connect(messageTimer, SIGNAL(timeout()), SLOT(hideMessage()));
129 homeView = new HomeView(this);
130 views->addWidget(homeView);
132 // TODO make this lazy
133 mediaView = MediaView::instance();
134 mediaView->setEnabled(false);
135 views->addWidget(mediaView);
144 // remove that useless menu/toolbar context menu
145 this->setContextMenuPolicy(Qt::NoContextMenu);
147 // event filter to block ugly toolbar tooltips
148 qApp->installEventFilter(this);
150 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
152 // restore window position
155 // fix stacked widget minimum size
156 for (int i = 0; i < views->count(); i++)
157 views->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
160 #ifdef APP_ACTIVATION
161 Activation::instance().initialCheck();
166 if (VideoAPI::impl() == VideoAPI::IV) {
167 Invidious::instance().initServers();
168 } else if (VideoAPI::impl() == VideoAPI::YT3) {
169 YT3::instance().initApiKeys();
172 QTimer::singleShot(100, this, &MainWindow::lazyInit);
175 void MainWindow::lazyInit() {
176 mediaView->initialize();
178 qApp->processEvents();
181 if (qApp->arguments().size() > 1) {
182 QString query = qApp->arguments().at(1);
183 if (query.startsWith(QLatin1String("--"))) {
184 messageReceived(query);
187 SearchParams *searchParams = new SearchParams();
188 searchParams->setKeywords(query);
189 showMedia(searchParams);
194 GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
196 if (GnomeGlobalShortcutBackend::IsGsdAvailable())
197 shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
202 connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
203 connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
204 connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
205 connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
206 // connect(&shortcuts, SIGNAL(StopAfter()), getAction("stopafterthis"), SLOT(toggle()));
208 connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
209 SLOT(updateDownloadMessage(QString)));
210 connect(DownloadManager::instance(), SIGNAL(finished()), SLOT(downloadsFinished()));
212 setAcceptDrops(true);
214 fullscreenTimer = new QTimer(this);
215 fullscreenTimer->setInterval(3000);
216 fullscreenTimer->setSingleShot(true);
217 connect(fullscreenTimer, SIGNAL(timeout()), SLOT(hideFullscreenUI()));
219 JsFunctions::instance();
221 // Hack to give focus to searchlineedit
222 View *view = qobject_cast<View *>(views->currentWidget());
223 if (view == homeView) {
224 QMetaObject::invokeMethod(views->currentWidget(), "appear");
225 const QString &desc = view->getDescription();
226 if (!desc.isEmpty()) showMessage(desc);
229 ChannelAggregator::instance()->start();
232 Updater::instance().checkWithoutUI();
238 void MainWindow::changeEvent(QEvent *e) {
240 if (e->type() == QEvent::WindowStateChange) {
241 getAction("minimize")->setEnabled(!isMinimized());
244 if (messageLabel->isVisible()) {
245 if (e->type() == QEvent::ActivationChange || e->type() == QEvent::WindowStateChange ||
246 e->type() == QEvent::WindowDeactivate || e->type() == QEvent::ApplicationStateChange) {
250 QMainWindow::changeEvent(e);
253 bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
254 const QEvent::Type t = e->type();
257 static bool altPressed = false;
258 if (t == QEvent::KeyRelease && altPressed) {
260 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
261 if (ke->key() == Qt::Key_Alt) {
262 toggleMenuVisibility();
265 } else if (t == QEvent::KeyPress) {
266 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
267 altPressed = ke->key() == Qt::Key_Alt;
271 if (fullScreenActive && views->currentWidget() == mediaView && t == QEvent::MouseMove &&
272 obj->isWidgetType() && qobject_cast<QWidget *>(obj)->window() == this) {
273 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
275 bool toolBarVisible = mainToolBar && mainToolBar->isVisible();
276 bool sidebarVisible = mediaView->isSidebarVisible();
278 if (!sidebarVisible && !toolBarVisible) {
279 const int x = mouseEvent->pos().x();
280 if (x >= 0 && x < 5) {
282 SidebarWidget *sidebar = mediaView->getSidebar();
283 sidebar->resize(sidebar->width(), height());
285 mediaView->setSidebarVisibility(true);
286 sidebarVisible = true;
291 if (!toolBarVisible && !sidebarVisible) {
292 const int y = mouseEvent->pos().y();
293 if (y >= 0 && y < 5) {
294 mainToolBar->resize(width(), mainToolBar->sizeHint().height());
295 mainToolBar->setVisible(true);
300 // show the normal cursor
302 // then hide it again after a few seconds
303 fullscreenTimer->start();
306 if (t == QEvent::ToolTip) {
311 if (t == QEvent::Show && obj == toolbarMenu) {
313 int x = width() - toolbarMenu->sizeHint().width();
316 int x = toolbarMenuButton->x() + toolbarMenuButton->width() -
317 toolbarMenu->sizeHint().width();
318 int y = toolbarMenuButton->y() + toolbarMenuButton->height();
321 toolbarMenu->move(mapToGlobal(p));
324 if (obj == this && t == QEvent::StyleChange) {
325 qDebug() << "Style change detected";
326 qApp->paletteChanged(qApp->palette());
330 // standard event processing
331 return QMainWindow::eventFilter(obj, e);
334 void MainWindow::createActions() {
335 stopAct = new QAction(tr("&Stop"), this);
336 IconUtils::setIcon(stopAct, "media-playback-stop");
337 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
338 stopAct->setShortcuts(QList<QKeySequence>()
339 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
340 stopAct->setEnabled(false);
341 actionMap.insert("stop", stopAct);
342 connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
344 skipBackwardAct = new QAction(tr("P&revious"), this);
345 skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
346 skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
347 skipBackwardAct->setEnabled(false);
348 actionMap.insert("previous", skipBackwardAct);
349 connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
351 skipAct = new QAction(tr("S&kip"), this);
352 IconUtils::setIcon(skipAct, "media-skip-forward");
353 skipAct->setStatusTip(tr("Skip to the next video"));
354 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right)
355 << QKeySequence(Qt::Key_MediaNext));
356 skipAct->setEnabled(false);
357 actionMap.insert("skip", skipAct);
358 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
360 pauseAct = new QAction(tr("&Play"), this);
361 IconUtils::setIcon(pauseAct, "media-playback-start");
362 pauseAct->setStatusTip(tr("Resume playback"));
363 pauseAct->setShortcuts(QList<QKeySequence>()
364 << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
365 pauseAct->setEnabled(false);
366 actionMap.insert("pause", pauseAct);
367 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
369 fullscreenAct = new QAction(tr("&Full Screen"), this);
370 IconUtils::setIcon(fullscreenAct, "view-fullscreen");
371 fullscreenAct->setStatusTip(tr("Go full screen"));
372 QList<QKeySequence> fsShortcuts;
374 fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
376 fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
378 fullscreenAct->setShortcuts(fsShortcuts);
379 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
380 fullscreenAct->setPriority(QAction::LowPriority);
381 actionMap.insert("fullscreen", fullscreenAct);
382 connect(fullscreenAct, SIGNAL(triggered()), SLOT(toggleFullscreen()));
384 compactViewAct = new QAction(tr("&Compact Mode"), this);
385 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
386 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
387 compactViewAct->setCheckable(true);
388 compactViewAct->setChecked(false);
389 compactViewAct->setEnabled(false);
390 actionMap.insert("compactView", compactViewAct);
391 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
393 webPageAct = new QAction(tr("Open the &YouTube Page"), this);
394 webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
395 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
396 webPageAct->setEnabled(false);
397 actionMap.insert("webpage", webPageAct);
398 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
400 copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
401 IconUtils::setIcon(copyPageAct, "link");
402 copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
403 copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
404 copyPageAct->setEnabled(false);
405 actionMap.insert("pagelink", copyPageAct);
406 connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
408 copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
409 copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
410 copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
411 copyLinkAct->setEnabled(false);
412 actionMap.insert("videolink", copyLinkAct);
413 connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
415 findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
416 findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
417 findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
418 findVideoPartsAct->setEnabled(false);
419 connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
420 actionMap.insert("findVideoParts", findVideoPartsAct);
422 removeAct = new QAction(tr("&Remove"), this);
423 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
424 removeAct->setShortcuts(QList<QKeySequence>()
425 << QKeySequence("Del") << QKeySequence("Backspace"));
426 removeAct->setEnabled(false);
427 actionMap.insert("remove", removeAct);
428 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
430 moveUpAct = new QAction(tr("Move &Up"), this);
431 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
432 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
433 moveUpAct->setEnabled(false);
434 actionMap.insert("moveUp", moveUpAct);
435 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
437 moveDownAct = new QAction(tr("Move &Down"), this);
438 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
439 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
440 moveDownAct->setEnabled(false);
441 actionMap.insert("moveDown", moveDownAct);
442 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
444 clearAct = new QAction(tr("&Clear Recent Searches"), this);
445 clearAct->setMenuRole(QAction::ApplicationSpecificRole);
446 clearAct->setShortcuts(QList<QKeySequence>()
447 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
448 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
449 clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
450 clearAct->setEnabled(true);
451 actionMap.insert("clearRecentKeywords", clearAct);
452 connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
454 quitAct = new QAction(tr("&Quit"), this);
455 quitAct->setMenuRole(QAction::QuitRole);
456 quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
457 quitAct->setStatusTip(tr("Bye"));
458 actionMap.insert("quit", quitAct);
459 connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
461 siteAct = new QAction(tr("&Website"), this);
462 siteAct->setShortcut(QKeySequence::HelpContents);
463 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
464 actionMap.insert("site", siteAct);
465 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
467 #if !defined(APP_MAC) && !defined(APP_WIN)
468 donateAct = new QAction(tr("Make a &Donation"), this);
469 donateAct->setStatusTip(
470 tr("Please support the continued development of %1").arg(Constants::NAME));
471 actionMap.insert("donate", donateAct);
472 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
475 aboutAct = new QAction(tr("&About"), this);
476 aboutAct->setMenuRole(QAction::AboutRole);
477 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
478 actionMap.insert("about", aboutAct);
479 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
483 searchFocusAct = new QAction(this);
484 searchFocusAct->setShortcut(QKeySequence::Find);
485 searchFocusAct->setStatusTip(tr("Search"));
486 actionMap.insert("search", searchFocusAct);
487 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
488 addAction(searchFocusAct);
490 volumeUpAct = new QAction(this);
491 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
492 actionMap.insert("volumeUp", volumeUpAct);
493 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
494 addAction(volumeUpAct);
496 volumeDownAct = new QAction(this);
497 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
498 actionMap.insert("volumeDown", volumeDownAct);
499 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
500 addAction(volumeDownAct);
502 volumeMuteAct = new QAction(this);
503 IconUtils::setIcon(volumeMuteAct, "audio-volume-high");
504 volumeMuteAct->setStatusTip(tr("Mute volume"));
505 volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_M));
506 actionMap.insert("volumeMute", volumeMuteAct);
507 connect(volumeMuteAct, SIGNAL(triggered()), SLOT(toggleVolumeMute()));
508 addAction(volumeMuteAct);
510 QToolButton *definitionButton = new QToolButton(this);
511 definitionButton->setText(YT3::instance().maxVideoDefinition().getName());
512 IconUtils::setIcon(definitionButton, "video-display");
513 definitionButton->setIconSize(QSize(16, 16));
514 definitionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
515 definitionButton->setPopupMode(QToolButton::InstantPopup);
516 QMenu *definitionMenu = new QMenu(this);
517 QActionGroup *group = new QActionGroup(this);
518 for (auto &defName : VideoDefinition::getDefinitionNames()) {
519 QAction *a = new QAction(defName);
520 a->setCheckable(true);
521 a->setActionGroup(group);
522 a->setChecked(defName == YT3::instance().maxVideoDefinition().getName());
523 connect(a, &QAction::triggered, this, [this, defName, definitionButton] {
524 setDefinitionMode(defName);
525 definitionButton->setText(defName);
527 connect(&YT3::instance(), &YT3::maxVideoDefinitionChanged, this,
528 [defName, definitionButton](const QString &name) {
529 if (defName == name) definitionButton->setChecked(true);
531 definitionMenu->addAction(a);
533 definitionButton->setMenu(definitionMenu);
534 QWidgetAction *definitionAct = new QWidgetAction(this);
535 definitionAct->setDefaultWidget(definitionButton);
536 definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
537 actionMap.insert("definition", definitionAct);
538 addAction(definitionAct);
542 action = new QAction(tr("&Manually Start Playing"), this);
543 IconUtils::setIcon(action, "media-playback-start");
544 action->setStatusTip(tr("Manually start playing videos"));
545 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
546 action->setCheckable(true);
547 connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
548 actionMap.insert("manualplay", action);
550 action = new QAction(tr("&Downloads"), this);
551 IconUtils::setIcon(action, "document-save");
552 action->setStatusTip(tr("Show details about video downloads"));
553 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
554 action->setCheckable(true);
555 connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
556 actionMap.insert("downloads", action);
558 action = new QAction(tr("&Download"), this);
559 IconUtils::setIcon(action, "document-save");
560 action->setStatusTip(tr("Download the current video"));
561 action->setShortcut(QKeySequence::Save);
562 action->setEnabled(false);
563 action->setVisible(false);
564 action->setPriority(QAction::LowPriority);
565 connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
566 actionMap.insert("download", action);
569 action = new QAction(tr("Take &Snapshot"), this);
570 action->setShortcut(QKeySequence(Qt::Key_F9));
571 action->setEnabled(false);
572 actionMap.insert("snapshot", action);
573 connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
576 action = new QAction(tr("&Subscribe to Channel"), this);
577 action->setProperty("originalText", action->text());
578 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
579 action->setEnabled(false);
580 connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
581 actionMap.insert("subscribeChannel", action);
582 mediaView->updateSubscriptionActionForVideo(0, false);
584 QString shareTip = tr("Share the current video using %1");
586 action = new QAction("&Twitter", this);
587 IconUtils::setIcon(action, "twitter");
588 action->setStatusTip(shareTip.arg("Twitter"));
589 action->setEnabled(false);
590 actionMap.insert("twitter", action);
591 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
593 action = new QAction("&Facebook", this);
594 IconUtils::setIcon(action, "facebook");
595 action->setStatusTip(shareTip.arg("Facebook"));
596 action->setEnabled(false);
597 actionMap.insert("facebook", action);
598 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
600 action = new QAction(tr("&Email"), this);
601 IconUtils::setIcon(action, "email");
602 action->setStatusTip(shareTip.arg(tr("Email")));
603 action->setEnabled(false);
604 actionMap.insert("email", action);
605 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
607 action = new QAction(tr("&Close"), this);
608 action->setShortcut(QKeySequence(QKeySequence::Close));
609 actionMap.insert("close", action);
610 connect(action, SIGNAL(triggered()), SLOT(close()));
612 action = new QAction(Constants::NAME, this);
613 action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
614 actionMap.insert("restore", action);
615 connect(action, SIGNAL(triggered()), SLOT(restore()));
617 action = new QAction(tr("&Float on Top"), this);
618 IconUtils::setIcon(action, "go-top");
619 action->setCheckable(true);
620 actionMap.insert("ontop", action);
621 connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
623 action = new QAction(tr("&Stop After This Video"), this);
624 IconUtils::setIcon(action, "media-playback-stop");
625 action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
626 action->setCheckable(true);
627 action->setEnabled(false);
628 actionMap.insert("stopafterthis", action);
629 connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
631 action = new QAction(tr("&Report an Issue..."), this);
632 actionMap.insert("reportIssue", action);
633 connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
635 action = new QAction(tr("&Refine Search..."), this);
636 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
637 action->setCheckable(true);
638 action->setEnabled(false);
639 actionMap.insert("refineSearch", action);
641 action = new QAction(YTRegions::worldwideRegion().name, this);
642 actionMap.insert("worldwideRegion", action);
644 action = new QAction(YTRegions::localRegion().name, this);
645 actionMap.insert("localRegion", action);
647 action = new QAction(tr("More..."), this);
648 actionMap.insert("moreRegion", action);
650 action = new QAction(tr("&Related Videos"), this);
651 IconUtils::setIcon(action, "view-list");
652 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
653 action->setStatusTip(tr("Watch videos related to the current one"));
654 action->setEnabled(false);
655 action->setPriority(QAction::LowPriority);
656 connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
657 actionMap.insert("relatedVideos", action);
659 action = new QAction(tr("Open in &Browser..."), this);
660 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
661 action->setEnabled(false);
662 actionMap.insert("openInBrowser", action);
663 connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
665 action = new QAction(tr("Restricted Mode"), this);
666 IconUtils::setIcon(action, "safesearch");
667 action->setStatusTip(tr("Hide videos that may contain inappropriate content"));
668 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
669 action->setCheckable(true);
670 action->setVisible(VideoAPI::impl() == VideoAPI::YT3);
671 actionMap.insert("safeSearch", action);
673 action = new QAction(tr("Toggle &Menu Bar"), this);
674 connect(action, SIGNAL(triggered()), SLOT(toggleMenuVisibilityWithMessage()));
675 actionMap.insert("toggleMenu", action);
677 action = new QAction(tr("Menu"), this);
678 IconUtils::setIcon(action, "open-menu");
679 connect(action, SIGNAL(triggered()), SLOT(toggleToolbarMenu()));
680 actionMap.insert("toolbarMenu", action);
683 action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
684 actionMap.insert("appStore", action);
685 connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
688 #ifdef APP_ACTIVATION
689 ActivationView::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
692 // common action properties
693 for (QAction *action : qAsConst(actionMap)) {
694 // add actions to the MainWindow so that they work
695 // when the menu is hidden
701 void MainWindow::createMenus() {
702 fileMenu = menuBar()->addMenu(tr("&Application"));
703 #ifdef APP_ACTIVATION
704 QAction *buyAction = getAction("buy");
705 if (buyAction) fileMenu->addAction(buyAction);
707 fileMenu->addSeparator();
710 fileMenu->addAction(clearAct);
712 fileMenu->addSeparator();
714 fileMenu->addAction(quitAct);
716 QMenu *playbackMenu = menuBar()->addMenu(tr("&Playback"));
717 menuMap.insert("playback", playbackMenu);
718 playbackMenu->addAction(pauseAct);
719 playbackMenu->addAction(stopAct);
720 playbackMenu->addAction(getAction("stopafterthis"));
721 playbackMenu->addSeparator();
722 playbackMenu->addAction(skipAct);
723 playbackMenu->addAction(skipBackwardAct);
724 playbackMenu->addSeparator();
725 playbackMenu->addAction(getAction("manualplay"));
727 MacSupport::dockMenu(playbackMenu);
730 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
731 menuMap.insert("playlist", playlistMenu);
732 playlistMenu->addAction(removeAct);
733 playlistMenu->addSeparator();
734 playlistMenu->addAction(moveUpAct);
735 playlistMenu->addAction(moveDownAct);
736 playlistMenu->addSeparator();
737 playlistMenu->addAction(getAction("refineSearch"));
739 QMenu *videoMenu = menuBar()->addMenu(tr("&Video"));
740 menuMap.insert("video", videoMenu);
741 videoMenu->addAction(getAction("relatedVideos"));
742 videoMenu->addAction(findVideoPartsAct);
743 videoMenu->addSeparator();
744 videoMenu->addAction(getAction("subscribeChannel"));
746 videoMenu->addSeparator();
747 videoMenu->addAction(getAction("snapshot"));
749 videoMenu->addSeparator();
750 videoMenu->addAction(webPageAct);
751 videoMenu->addAction(copyLinkAct);
752 videoMenu->addAction(getAction("openInBrowser"));
753 videoMenu->addAction(getAction("download"));
755 QMenu *shareMenu = menuBar()->addMenu(tr("&Share"));
756 menuMap.insert("share", shareMenu);
757 shareMenu->addAction(copyPageAct);
758 shareMenu->addSeparator();
759 shareMenu->addAction(getAction("twitter"));
760 shareMenu->addAction(getAction("facebook"));
761 shareMenu->addSeparator();
762 shareMenu->addAction(getAction("email"));
764 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
765 menuMap.insert("view", viewMenu);
766 viewMenu->addAction(getAction("ontop"));
767 viewMenu->addAction(compactViewAct);
768 viewMenu->addSeparator();
769 viewMenu->addAction(fullscreenAct);
771 viewMenu->addSeparator();
772 viewMenu->addAction(getAction("toggleMenu"));
776 MacSupport::windowMenu(this);
779 helpMenu = menuBar()->addMenu(tr("&Help"));
780 menuMap.insert("help", helpMenu);
781 helpMenu->addAction(siteAct);
782 #if !defined(APP_MAC) && !defined(APP_WIN)
783 helpMenu->addAction(donateAct);
785 helpMenu->addAction(getAction("reportIssue"));
786 helpMenu->addAction(aboutAct);
788 helpMenu->addAction(Updater::instance().getAction());
792 helpMenu->addSeparator();
793 helpMenu->addAction(getAction("appStore"));
797 void MainWindow::createToolBar() {
799 currentTimeLabel = new QLabel("00:00", this);
801 seekSlider = new SeekSlider(this);
802 seekSlider->setEnabled(false);
803 seekSlider->setTracking(false);
804 seekSlider->setMaximum(1000);
805 volumeSlider = new SeekSlider(this);
806 volumeSlider->setValue(volumeSlider->maximum());
808 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
809 SearchWrapper *searchWrapper = new SearchWrapper(this);
810 toolbarSearch = searchWrapper->getSearchLineEdit();
812 toolbarSearch = new SearchLineEdit(this);
814 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize() * 15);
815 toolbarSearch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
816 toolbarSearch->setSuggester(new YTSuggester(this));
817 connect(toolbarSearch, SIGNAL(search(const QString &)), SLOT(search(const QString &)));
818 connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion *)),
819 SLOT(suggestionAccepted(Suggestion *)));
820 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
822 // Add widgets to toolbar
824 #ifdef APP_MAC_QMACTOOLBAR
825 currentTimeLabel->hide();
826 toolbarSearch->hide();
827 volumeSlider->hide();
829 MacToolbar::instance().createToolbar(this);
833 mainToolBar = new QToolBar(this);
834 mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
835 mainToolBar->setFloatable(false);
836 mainToolBar->setMovable(false);
838 mainToolBar->setIconSize(QSize(32, 32));
840 mainToolBar->addAction(stopAct);
841 QToolButton *stopToolButton =
842 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(stopAct));
843 if (stopToolButton) {
844 QMenu *stopMenu = new QMenu(this);
845 stopMenu->addAction(getAction("stopafterthis"));
846 stopToolButton->setMenu(stopMenu);
847 stopToolButton->setPopupMode(QToolButton::DelayedPopup);
849 mainToolBar->addAction(pauseAct);
850 mainToolBar->addAction(skipAct);
851 mainToolBar->addAction(getAction("relatedVideos"));
853 bool addFullScreenAct = true;
855 addFullScreenAct = !mac::CanGoFullScreen(winId());
857 if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
859 mainToolBar->addWidget(new Spacer());
861 currentTimeLabel->setFont(FontUtils::small());
862 currentTimeLabel->setMinimumWidth(currentTimeLabel->fontInfo().pixelSize() * 4);
863 currentTimeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
864 mainToolBar->addWidget(currentTimeLabel);
867 mainToolBar->addWidget(new Spacer(nullptr, 10));
870 seekSlider->setOrientation(Qt::Horizontal);
871 seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
872 seekSlider->setFocusPolicy(Qt::NoFocus);
873 mainToolBar->addWidget(seekSlider);
875 mainToolBar->addWidget(new Spacer());
877 mainToolBar->addAction(volumeMuteAct);
878 #ifndef APP_MAC_QMACTOOLBAR
879 QToolButton *volumeMuteButton =
880 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
881 volumeMuteButton->setIconSize(QSize(16, 16));
882 auto fixVolumeMuteIconSize = [volumeMuteButton] {
883 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
885 fixVolumeMuteIconSize();
886 volumeMuteButton->connect(volumeMuteAct, &QAction::changed, volumeMuteButton,
887 fixVolumeMuteIconSize);
890 volumeSlider->setStatusTip(
891 tr("Press %1 to raise the volume, %2 to lower it")
892 .arg(volumeUpAct->shortcut().toString(QKeySequence::NativeText),
893 volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
895 volumeSlider->setOrientation(Qt::Horizontal);
896 // this makes the volume slider smaller
897 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
898 volumeSlider->setFocusPolicy(Qt::NoFocus);
899 mainToolBar->addWidget(volumeSlider);
901 mainToolBar->addWidget(new Spacer());
903 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
904 mainToolBar->addWidget(searchWrapper);
906 mainToolBar->addWidget(toolbarSearch);
907 mainToolBar->addWidget(new Spacer(this, toolbarSearch->height() / 2));
909 QAction *toolbarMenuAction = getAction("toolbarMenu");
910 mainToolBar->addAction(toolbarMenuAction);
912 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(toolbarMenuAction));
915 addToolBar(mainToolBar);
918 void MainWindow::createStatusBar() {
919 statusToolBar = new QToolBar(statusBar());
920 statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
921 statusToolBar->setIconSize(QSize(16, 16));
923 regionAction = new QAction(this);
924 regionAction->setStatusTip(tr("Choose your content location"));
926 QAction *localAction = getAction("localRegion");
927 if (!localAction->text().isEmpty()) {
928 QMenu *regionMenu = new QMenu(this);
929 regionMenu->addAction(getAction("worldwideRegion"));
930 regionMenu->addAction(localAction);
931 regionMenu->addSeparator();
932 QAction *moreRegionsAction = getAction("moreRegion");
933 regionMenu->addAction(moreRegionsAction);
934 connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
935 regionAction->setMenu(regionMenu);
937 connect(regionAction, SIGNAL(triggered()), SLOT(showRegionsView()));
939 /* Stupid code that generates the QRC items
940 foreach(YTRegion r, YTRegions::list())
941 qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
944 statusBar()->addPermanentWidget(statusToolBar);
948 void MainWindow::showStopAfterThisInStatusBar(bool show) {
949 QAction *action = getAction("stopafterthis");
950 showActionsInStatusBar({action}, show);
953 void MainWindow::showActionsInStatusBar(const QVector<QAction *> &actions, bool show) {
955 Extra::fadeInWidget(statusBar(), statusBar());
957 for (auto action : actions) {
959 if (statusToolBar->actions().contains(action)) continue;
960 if (statusToolBar->actions().isEmpty()) {
961 statusToolBar->addAction(action);
963 statusToolBar->insertAction(statusToolBar->actions().at(0), action);
966 statusToolBar->removeAction(action);
971 if (statusBar()->isHidden() && !fullScreenActive) setStatusBarVisibility(true);
973 if (statusBar()->isVisible() && !needStatusBar()) setStatusBarVisibility(false);
977 void MainWindow::setStatusBarVisibility(bool show) {
978 if (statusBar()->isVisible() != show) {
979 statusBar()->setVisible(show);
980 if (views->currentWidget() == mediaView)
981 QTimer::singleShot(0, mediaView, SLOT(adjustWindowSize()));
985 void MainWindow::adjustStatusBarVisibility() {
986 setStatusBarVisibility(needStatusBar());
989 void MainWindow::hideToolbar() {
991 mac::showToolBar(winId(), false);
997 void MainWindow::showToolbar() {
999 mac::showToolBar(winId(), true);
1001 mainToolBar->show();
1005 void MainWindow::readSettings() {
1007 QByteArray geometrySettings = settings.value("geometry").toByteArray();
1008 if (!geometrySettings.isEmpty()) {
1009 restoreGeometry(geometrySettings);
1011 const QRect desktopSize = QGuiApplication::primaryScreen()->availableGeometry();
1012 int w = desktopSize.width() * .9;
1013 int h = qMin(w / 2, desktopSize.height());
1015 QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, QSize(w, h), desktopSize));
1017 setDefinitionMode(settings.value("definition", YT3::instance().maxVideoDefinition().getName())
1019 getAction("manualplay")->setChecked(settings.value("manualplay", false).toBool());
1020 getAction("safeSearch")->setChecked(settings.value("safeSearch", false).toBool());
1022 menuBar()->setVisible(settings.value("menuBar", false).toBool());
1026 void MainWindow::writeSettings() {
1029 if (!isReallyFullScreen()) {
1030 settings.setValue("geometry", saveGeometry());
1031 if (mediaView) mediaView->saveSplitterState();
1034 settings.setValue("manualplay", getAction("manualplay")->isChecked());
1035 settings.setValue("safeSearch", getAction("safeSearch")->isChecked());
1037 settings.setValue("menuBar", menuBar()->isVisible());
1041 void MainWindow::goBack() {
1042 if (history.size() > 1) {
1044 showView(history.pop());
1048 void MainWindow::showView(View *view, bool transition) {
1049 if (!history.isEmpty() && view == history.top()) {
1050 qDebug() << "Attempting to show same view" << view;
1055 if (transition && !history.isEmpty()) CompositeFader::go(this, this->grab());
1058 if (compactViewAct->isChecked()) compactViewAct->toggle();
1060 // call hide method on the current view
1061 View *oldView = qobject_cast<View *>(views->currentWidget());
1063 oldView->disappear();
1064 oldView->setEnabled(false);
1065 oldView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
1067 qDebug() << "Cannot cast old view";
1069 view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1070 view->setEnabled(true);
1071 views->setCurrentWidget(view);
1074 QString title = view->getTitle();
1075 if (title.isEmpty())
1076 title = Constants::NAME;
1078 title += QLatin1String(" - ") + Constants::NAME;
1079 setWindowTitle(title);
1081 const bool isMediaView = view == mediaView;
1082 stopAct->setEnabled(isMediaView);
1083 compactViewAct->setEnabled(isMediaView);
1084 toolbarSearch->setEnabled(isMediaView);
1085 aboutAct->setEnabled(view != aboutView);
1086 getAction("downloads")->setChecked(view == downloadView);
1088 // dynamic view actions
1089 /* Not currently used by any view
1090 showActionsInStatusBar(viewActions, false);
1091 viewActions = newView->getViewActions();
1092 showActionsInStatusBar(viewActions, true);
1099 void MainWindow::about() {
1101 aboutView = new AboutView(this);
1102 views->addWidget(aboutView);
1104 showView(aboutView);
1107 void MainWindow::visitSite() {
1108 QUrl url(Constants::WEBSITE);
1109 showMessage(QString(tr("Opening %1").arg(url.toString())));
1110 QDesktopServices::openUrl(url);
1113 void MainWindow::donate() {
1114 QUrl url("https://" + QLatin1String(Constants::ORG_DOMAIN) + "/donate");
1115 showMessage(QString(tr("Opening %1").arg(url.toString())));
1116 QDesktopServices::openUrl(url);
1119 void MainWindow::reportIssue() {
1120 QUrl url("https://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
1121 QDesktopServices::openUrl(url);
1124 void MainWindow::quit() {
1126 if (!confirmQuit()) {
1130 // do not save geometry when in full screen or in compact mode
1131 if (!fullScreenActive && !compactViewAct->isChecked()) {
1137 // mediaView->stop();
1138 Temporary::deleteAll();
1139 ChannelAggregator::instance()->stop();
1140 ChannelAggregator::instance()->cleanup();
1141 Database::shutdown();
1145 void MainWindow::closeEvent(QCloseEvent *e) {
1147 mac::closeWindow(winId());
1150 if (!confirmQuit()) {
1154 QWidget::closeEvent(e);
1157 messageLabel->hide();
1160 void MainWindow::showEvent(QShowEvent *e) {
1161 QWidget::showEvent(e);
1167 bool MainWindow::confirmQuit() {
1168 if (DownloadManager::instance()->activeItems() > 0) {
1169 QMessageBox msgBox(this);
1170 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1172 tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1173 msgBox.setInformativeText(
1174 tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1175 msgBox.setModal(true);
1176 // make it a "sheet" on the Mac
1177 msgBox.setWindowModality(Qt::WindowModal);
1179 msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1180 QPushButton *waitButton =
1181 msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1185 if (msgBox.clickedButton() == waitButton) {
1192 void MainWindow::showHome() {
1194 currentTimeLabel->clear();
1195 seekSlider->setValue(0);
1198 void MainWindow::showMedia(SearchParams *searchParams) {
1199 showView(mediaView);
1200 if (getAction("safeSearch")->isChecked())
1201 searchParams->setSafeSearch(SearchParams::Strict);
1203 searchParams->setSafeSearch(SearchParams::None);
1204 mediaView->search(searchParams);
1207 void MainWindow::showMedia(VideoSource *videoSource) {
1208 showView(mediaView);
1209 mediaView->setVideoSource(videoSource);
1212 void MainWindow::stateChanged(Media::State newState) {
1213 qDebug() << newState;
1215 seekSlider->setEnabled(newState != Media::StoppedState);
1218 case Media::ErrorState:
1219 showMessage(tr("Error: %1").arg(media->errorString()));
1222 case Media::PlayingState:
1223 pauseAct->setEnabled(true);
1224 pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1225 pauseAct->setText(tr("&Pause"));
1226 pauseAct->setStatusTip(tr("Pause playback") + " (" +
1227 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1230 case Media::StoppedState:
1231 pauseAct->setEnabled(false);
1232 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1233 pauseAct->setText(tr("&Play"));
1234 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1235 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1238 case Media::PausedState:
1239 pauseAct->setEnabled(true);
1240 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1241 pauseAct->setText(tr("&Play"));
1242 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1243 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1246 case Media::BufferingState:
1247 pauseAct->setEnabled(false);
1248 pauseAct->setIcon(IconUtils::icon("content-loading"));
1249 pauseAct->setText(tr("&Loading..."));
1250 pauseAct->setStatusTip(QString());
1253 case Media::LoadingState:
1254 pauseAct->setEnabled(false);
1255 currentTimeLabel->clear();
1262 void MainWindow::stop() {
1267 void MainWindow::resizeEvent(QResizeEvent *e) {
1270 if (initialized && mac::CanGoFullScreen(winId())) {
1271 bool isFullscreen = mac::IsFullScreen(winId());
1272 if (isFullscreen != fullScreenActive) {
1273 if (compactViewAct->isChecked()) {
1274 compactViewAct->setChecked(false);
1277 fullScreenActive = isFullscreen;
1278 updateUIForFullscreen();
1282 #ifdef APP_MAC_QMACTOOLBAR
1283 int moreButtonWidth = 40;
1284 toolbarSearch->move(width() - toolbarSearch->width() - moreButtonWidth - 7, -34);
1289 void MainWindow::enterEvent(QEvent *e) {
1292 // Workaround cursor bug on macOS
1297 void MainWindow::leaveEvent(QEvent *e) {
1299 if (fullScreenActive) hideFullscreenUI();
1302 void MainWindow::toggleFullscreen() {
1303 if (compactViewAct->isChecked()) compactViewAct->toggle();
1306 WId handle = winId();
1307 if (mac::CanGoFullScreen(handle)) {
1308 if (mainToolBar) mainToolBar->setVisible(true);
1309 mac::ToggleFullScreen(handle);
1314 fullScreenActive = !fullScreenActive;
1316 if (fullScreenActive) {
1317 // Enter full screen
1319 maximizedBeforeFullScreen = isMaximized();
1321 // save geometry now, if the user quits when in full screen
1322 // geometry won't be saved
1326 MacSupport::enterFullScreen(this, views);
1328 menuVisibleBeforeFullScreen = menuBar()->isVisible();
1330 if (mainToolBar) mainToolBar->hide();
1338 MacSupport::exitFullScreen(this, views);
1340 menuBar()->setVisible(menuVisibleBeforeFullScreen);
1341 if (mainToolBar) mainToolBar->setVisible(views->currentWidget() == mediaView);
1342 if (maximizedBeforeFullScreen)
1348 // Make sure the window has focus
1352 qApp->processEvents();
1353 updateUIForFullscreen();
1356 void MainWindow::updateUIForFullscreen() {
1357 static QList<QKeySequence> fsShortcuts;
1358 static QString fsText;
1360 if (fullScreenActive) {
1361 fsShortcuts = fullscreenAct->shortcuts();
1362 fsText = fullscreenAct->text();
1363 if (fsText.isEmpty()) qDebug() << "[taking Empty!]";
1364 fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1365 << QKeySequence(Qt::Key_Escape));
1366 fullscreenAct->setText(tr("Leave &Full Screen"));
1367 fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1368 setStatusBarVisibility(false);
1371 removeToolBar(mainToolBar);
1372 mainToolBar->move(0, 0);
1375 mediaView->removeSidebar();
1378 fullscreenAct->setShortcuts(fsShortcuts);
1379 if (fsText.isEmpty()) fsText = "[Empty!]";
1380 fullscreenAct->setText(fsText);
1381 fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1383 if (needStatusBar()) setStatusBarVisibility(true);
1386 addToolBar(mainToolBar);
1389 mediaView->restoreSidebar();
1392 // No compact view action when in full screen
1393 compactViewAct->setVisible(!fullScreenActive);
1394 compactViewAct->setChecked(false);
1396 // Hide anything but the video
1397 mediaView->setSidebarVisibility(!fullScreenActive);
1399 if (fullScreenActive) {
1400 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1402 stopAct->setShortcuts(QList<QKeySequence>()
1403 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1407 MacSupport::fullScreenActions(actionMap, fullScreenActive);
1410 if (views->currentWidget() == mediaView) mediaView->setFocus();
1412 if (fullScreenActive) {
1413 if (views->currentWidget() == mediaView) hideFullscreenUI();
1415 fullscreenTimer->stop();
1420 bool MainWindow::isReallyFullScreen() {
1422 WId handle = winId();
1423 if (mac::CanGoFullScreen(handle))
1424 return mac::IsFullScreen(handle);
1426 return isFullScreen();
1428 return isFullScreen();
1432 void MainWindow::missingKeyWarning() {
1433 static bool shown = false;
1436 QMessageBox msgBox(this);
1437 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1438 msgBox.setText(QString("%1 was built without a Google API key.").arg(Constants::NAME));
1439 msgBox.setInformativeText(QString("It won't work unless you enter one."
1440 "<p>In alternative you can get %1 from the developer site.")
1441 .arg(Constants::NAME));
1442 msgBox.setModal(true);
1443 msgBox.setWindowModality(Qt::WindowModal);
1444 msgBox.addButton(QMessageBox::Close);
1445 QPushButton *enterKeyButton =
1446 msgBox.addButton(QString("Enter API key..."), QMessageBox::AcceptRole);
1447 QPushButton *devButton = msgBox.addButton(QString("Get from %1").arg(Constants::WEBSITE),
1448 QMessageBox::AcceptRole);
1449 QPushButton *helpButton = msgBox.addButton(QMessageBox::Help);
1453 if (msgBox.clickedButton() == helpButton) {
1454 QDesktopServices::openUrl(QUrl("https://github.com/flaviotordini/minitube/blob/master/"
1455 "README.md#google-api-key"));
1456 } else if (msgBox.clickedButton() == enterKeyButton) {
1458 QString text = QInputDialog::getText(this, QString(), "Google API key:", QLineEdit::Normal,
1460 if (ok && !text.isEmpty()) {
1462 settings.setValue("googleApiKey", text);
1463 YT3::instance().initApiKeys();
1465 } else if (msgBox.clickedButton() == devButton) {
1466 QDesktopServices::openUrl(QUrl(Constants::WEBSITE));
1471 void MainWindow::compactView(bool enable) {
1472 setUpdatesEnabled(false);
1474 compactModeActive = enable;
1476 static QList<QKeySequence> compactShortcuts;
1477 static QList<QKeySequence> stopShortcuts;
1479 const QString key = "compactGeometry";
1483 setMinimumSize(320, 180);
1485 mac::RemoveFullScreenWindow(winId());
1489 if (settings.contains(key))
1490 restoreGeometry(settings.value(key).toByteArray());
1494 #ifdef APP_MAC_QMACTOOLBAR
1495 mac::showToolBar(winId(), !enable);
1497 mainToolBar->setVisible(!enable);
1499 mediaView->setSidebarVisibility(!enable);
1500 statusBar()->hide();
1502 compactShortcuts = compactViewAct->shortcuts();
1503 stopShortcuts = stopAct->shortcuts();
1505 QList<QKeySequence> newStopShortcuts(stopShortcuts);
1506 newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1507 stopAct->setShortcuts(newStopShortcuts);
1508 compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts)
1509 << QKeySequence(Qt::Key_Escape));
1511 // ensure focus does not end up to the search box
1512 // as it would steal the Space shortcut
1513 mediaView->setFocus();
1516 settings.setValue(key, saveGeometry());
1518 // unset minimum size
1519 setMinimumSize(0, 0);
1522 mac::SetupFullScreenWindow(winId());
1524 #ifdef APP_MAC_QMACTOOLBAR
1525 mac::showToolBar(winId(), !enable);
1527 mainToolBar->setVisible(!enable);
1529 mediaView->setSidebarVisibility(!enable);
1530 if (needStatusBar()) setStatusBarVisibility(true);
1534 compactViewAct->setShortcuts(compactShortcuts);
1535 stopAct->setShortcuts(stopShortcuts);
1538 // auto float on top
1539 floatOnTop(enable, false);
1542 mac::compactMode(winId(), enable);
1545 menuVisibleBeforeCompactMode = menuBar()->isVisible();
1548 menuBar()->setVisible(menuVisibleBeforeCompactMode);
1552 setUpdatesEnabled(true);
1555 void MainWindow::toggleToolbarMenu() {
1556 if (!toolbarMenu) toolbarMenu = new ToolbarMenu(this);
1557 if (toolbarMenu->isVisible())
1558 toolbarMenu->hide();
1560 toolbarMenu->show();
1563 void MainWindow::searchFocus() {
1564 toolbarSearch->selectAll();
1565 toolbarSearch->setFocus();
1568 void MainWindow::initMedia() {
1570 qFatal("QtAV has a showstopper bug. Audio stops randomly. See bug "
1571 "https://github.com/wang-bin/QtAV/issues/1184");
1572 media = new MediaQtAV(this);
1573 #elif defined MEDIA_MPV
1574 media = new MediaMPV();
1576 qFatal("No media backend defined");
1579 media->setUserAgent(HttpUtils::stealthUserAgent());
1582 qreal volume = settings.value("volume", 1.).toReal();
1583 media->setVolume(volume);
1585 connect(media, &Media::error, this, &MainWindow::handleError);
1586 connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
1587 connect(media, &Media::positionChanged, this, &MainWindow::tick);
1589 connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
1590 // value : maxValue = posit ion : duration
1591 qint64 ms = (value * media->duration()) / seekSlider->maximum();
1592 qDebug() << "Seeking to" << ms;
1594 if (media->state() == Media::PausedState) media->play();
1596 connect(seekSlider, &QSlider::sliderPressed, this, [this]() {
1597 // value : maxValue = position : duration
1598 qint64 ms = (seekSlider->value() * media->duration()) / seekSlider->maximum();
1600 if (media->state() == Media::PausedState) media->play();
1602 connect(media, &Media::started, this, [this]() { seekSlider->setValue(0); });
1604 connect(media, &Media::volumeChanged, this, &MainWindow::volumeChanged);
1605 connect(media, &Media::volumeMutedChanged, this, &MainWindow::volumeMutedChanged);
1606 connect(volumeSlider, &QSlider::sliderMoved, this, [this](int value) {
1607 qreal volume = (qreal)value / volumeSlider->maximum();
1608 media->setVolume(volume);
1610 connect(volumeSlider, &QSlider::sliderPressed, this, [this]() {
1611 qreal volume = (qreal)volumeSlider->value() / volumeSlider->maximum();
1612 media->setVolume(volume);
1615 mediaView->setMedia(media);
1618 void MainWindow::tick(qint64 time) {
1620 bool isDown = seekSlider->property("down").isValid();
1622 bool isDown = seekSlider->isSliderDown();
1624 if (!isDown && media->state() == Media::PlayingState) {
1625 // value : maxValue = position : duration
1626 qint64 duration = media->duration();
1627 if (duration <= 0) return;
1628 int value = (seekSlider->maximum() * media->position()) / duration;
1629 seekSlider->setValue(value);
1632 const QString s = formatTime(time);
1633 if (s != currentTimeLabel->text()) {
1634 currentTimeLabel->setText(s);
1635 emit currentTimeChanged(s);
1638 const qint64 remainingTime = media->remainingTime();
1639 currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1643 QString MainWindow::formatTime(qint64 duration) {
1646 int seconds = (int)(duration % 60);
1648 int minutes = (int)(duration % 60);
1650 int hours = (int)(duration % 24);
1651 if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
1652 return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
1655 void MainWindow::volumeUp() {
1656 qreal newVolume = media->volume() + .1;
1657 if (newVolume > 1.) newVolume = 1.;
1658 media->setVolume(newVolume);
1661 void MainWindow::volumeDown() {
1662 qreal newVolume = media->volume() - .1;
1663 if (newVolume < 0) newVolume = 0;
1664 media->setVolume(newVolume);
1667 void MainWindow::toggleVolumeMute() {
1668 bool muted = media->volumeMuted();
1669 media->setVolumeMuted(!muted);
1672 void MainWindow::volumeChanged(qreal newVolume) {
1673 // automatically unmute when volume changes
1674 if (media->volumeMuted()) media->setVolumeMuted(false);
1675 showMessage(tr("Volume at %1%").arg((int)(newVolume * 100)));
1676 // newVolume : 1.0 = x : 1000
1677 int value = newVolume * volumeSlider->maximum();
1678 volumeSlider->blockSignals(true);
1679 volumeSlider->setValue(value);
1680 volumeSlider->blockSignals(false);
1683 void MainWindow::volumeMutedChanged(bool muted) {
1685 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1686 showMessage(tr("Volume is muted"));
1688 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1689 showMessage(tr("Volume is unmuted"));
1693 void MainWindow::setDefinitionMode(const QString &definitionName) {
1694 QAction *definitionAct = getAction("definition");
1695 definitionAct->setText(definitionName);
1696 definitionAct->setStatusTip(
1697 tr("Maximum video definition set to %1").arg(definitionAct->text()) + " (" +
1698 definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1699 showMessage(definitionAct->statusTip());
1700 YT3::instance().setMaxVideoDefinition(definitionName);
1701 if (views->currentWidget() == mediaView) {
1702 mediaView->reloadCurrentVideo();
1706 void MainWindow::toggleDefinitionMode() {
1707 const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
1708 const VideoDefinition ¤tDefinition = YT3::instance().maxVideoDefinition();
1710 int index = definitions.indexOf(currentDefinition);
1711 if (index != definitions.size() - 1) {
1716 setDefinitionMode(definitions.at(index).getName());
1719 void MainWindow::clearRecentKeywords() {
1721 settings.remove("recentKeywords");
1722 settings.remove("recentChannels");
1723 if (views->currentWidget() == homeView) {
1724 SearchView *searchView = homeView->getSearchView();
1725 searchView->updateRecentKeywords();
1726 searchView->updateRecentChannels();
1728 HttpUtils::clearCaches();
1729 showMessage(tr("Your privacy is now safe"));
1732 void MainWindow::setManualPlay(bool enabled) {
1734 settings.setValue("manualplay", QVariant::fromValue(enabled));
1735 if (views->currentWidget() == homeView &&
1736 homeView->currentWidget() == homeView->getSearchView())
1738 showActionsInStatusBar({getAction("manualplay")}, enabled);
1741 void MainWindow::updateDownloadMessage(const QString &message) {
1742 getAction("downloads")->setText(message);
1745 void MainWindow::downloadsFinished() {
1746 getAction("downloads")->setText(tr("&Downloads"));
1747 showMessage(tr("Downloads complete"));
1750 void MainWindow::toggleDownloads(bool show) {
1752 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1753 getAction("downloads")
1754 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1755 << QKeySequence(Qt::Key_Escape));
1757 getAction("downloads")
1758 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1759 stopAct->setShortcuts(QList<QKeySequence>()
1760 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1763 if (!downloadView) {
1764 downloadView = new DownloadView(this);
1765 views->addWidget(downloadView);
1768 showView(downloadView);
1773 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1774 search(suggestion->value);
1777 void MainWindow::search(const QString &query) {
1778 QString q = query.simplified();
1779 if (q.isEmpty()) return;
1780 SearchParams *searchParams = new SearchParams();
1781 searchParams->setKeywords(q);
1782 showMedia(searchParams);
1785 void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
1786 if (e->mimeData()->hasFormat("text/uri-list")) {
1787 QList<QUrl> urls = e->mimeData()->urls();
1788 if (urls.isEmpty()) return;
1789 const QUrl &url = urls.at(0);
1790 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1791 if (!videoId.isEmpty()) e->acceptProposedAction();
1795 void MainWindow::dropEvent(QDropEvent *e) {
1796 if (!toolbarSearch->isEnabled()) return;
1798 QList<QUrl> urls = e->mimeData()->urls();
1799 if (urls.isEmpty()) return;
1800 const QUrl &url = urls.at(0);
1801 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1802 if (!videoId.isEmpty()) {
1803 setWindowTitle(url.toString());
1804 SearchParams *searchParams = new SearchParams();
1805 searchParams->setKeywords(videoId);
1806 showMedia(searchParams);
1810 bool MainWindow::needStatusBar() {
1811 return !statusToolBar->actions().isEmpty();
1814 void MainWindow::adjustMessageLabelPosition() {
1815 if (messageLabel->parent() == this)
1816 messageLabel->move(0, height() - messageLabel->height());
1818 messageLabel->move(mapToGlobal(QPoint(0, height() - messageLabel->height())));
1821 void MainWindow::floatOnTop(bool onTop, bool showAction) {
1822 if (showAction) showActionsInStatusBar({getAction("ontop")}, onTop);
1824 mac::floatOnTop(winId(), onTop);
1827 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1830 setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1836 void MainWindow::restore() {
1838 mac::uncloseWindow(winId());
1842 void MainWindow::messageReceived(const QString &message) {
1843 if (message == QLatin1String("--toggle-playing")) {
1844 if (pauseAct->isEnabled()) pauseAct->trigger();
1845 } else if (message == QLatin1String("--next")) {
1846 if (skipAct->isEnabled()) skipAct->trigger();
1847 } else if (message == QLatin1String("--previous")) {
1848 if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1849 } else if (message == QLatin1String("--stop-after-this")) {
1850 getAction("stopafterthis")->toggle();
1851 } else if (message.startsWith("--")) {
1852 MainWindow::printHelp();
1853 } else if (!message.isEmpty()) {
1854 SearchParams *searchParams = new SearchParams();
1855 searchParams->setKeywords(message);
1856 showMedia(searchParams);
1860 void MainWindow::hideFullscreenUI() {
1861 if (views->currentWidget() != mediaView) return;
1862 setCursor(Qt::BlankCursor);
1864 QPoint p = mapFromGlobal(QCursor::pos());
1865 const int x = p.x();
1867 if (x > mediaView->getSidebar()->width()) mediaView->setSidebarVisibility(false);
1870 const int y = p.y();
1871 bool shouldHideToolbar = !toolbarSearch->hasFocus() && y > mainToolBar->height();
1872 if (shouldHideToolbar) mainToolBar->setVisible(false);
1876 void MainWindow::toggleMenuVisibility() {
1877 bool show = !menuBar()->isVisible();
1878 menuBar()->setVisible(show);
1881 void MainWindow::toggleMenuVisibilityWithMessage() {
1882 bool show = !menuBar()->isVisible();
1883 menuBar()->setVisible(show);
1885 QMessageBox msgBox(this);
1886 msgBox.setText(tr("You can still access the menu bar by pressing the ALT key"));
1887 msgBox.setModal(true);
1888 msgBox.setWindowModality(Qt::WindowModal);
1893 #ifdef APP_MAC_STORE
1894 void MainWindow::rateOnAppStore() {
1895 QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1896 "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1897 "?id=422006190&type=Purple+Software"));
1901 void MainWindow::printHelp() {
1902 QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1903 msg += "Usage: minitube [options]\n";
1904 msg += "Options:\n";
1905 msg += " --toggle-playing\t";
1906 msg += "Start or pause playback.\n";
1907 msg += " --next\t\t";
1908 msg += "Skip to the next video.\n";
1909 msg += " --previous\t\t";
1910 msg += "Go back to the previous video.\n";
1911 msg += " --stop-after-this\t";
1912 msg += "Stop playback at the end of the video.\n";
1913 std::cout << msg.toLocal8Bit().data();
1916 void MainWindow::setupAction(QAction *action) {
1917 // never autorepeat.
1918 // unexperienced users tend to keep keys pressed for a "long" time
1919 action->setAutoRepeat(false);
1921 // show keyboard shortcuts in the status bar
1922 if (!action->shortcut().isEmpty())
1923 action->setStatusTip(action->statusTip() + QLatin1String(" (") +
1924 action->shortcut().toString(QKeySequence::NativeText) +
1925 QLatin1String(")"));
1928 QAction *MainWindow::getAction(const char *name) {
1929 return actionMap.value(QByteArray::fromRawData(name, strlen(name)));
1932 void MainWindow::addNamedAction(const QByteArray &name, QAction *action) {
1933 actionMap.insert(name, action);
1936 QMenu *MainWindow::getMenu(const char *name) {
1937 return menuMap.value(QByteArray::fromRawData(name, strlen(name)));
1940 void MainWindow::showMessage(const QString &message) {
1941 if (!isVisible()) return;
1943 if (!mac::isVisible(winId())) return;
1945 if (statusBar()->isVisible())
1946 statusBar()->showMessage(message, 60000);
1947 else if (isActiveWindow()) {
1948 messageLabel->setText(message);
1949 QSize size = messageLabel->sizeHint();
1950 // round width to avoid flicker with fast changing messages (e.g. volume
1952 int w = size.width() + 10;
1953 const int multiple = 15;
1954 w = w + multiple / 2;
1957 messageLabel->resize(size);
1958 if (messageLabel->isHidden()) {
1959 adjustMessageLabelPosition();
1960 messageLabel->show();
1962 messageTimer->start();
1966 void MainWindow::hideMessage() {
1967 if (messageLabel->isVisible()) {
1968 messageLabel->hide();
1969 messageLabel->clear();
1973 void MainWindow::handleError(const QString &message) {
1974 qWarning() << message;
1975 showMessage(message);
1978 #ifdef APP_ACTIVATION
1979 void MainWindow::showActivationView() {
1980 View *activationView = ActivationView::instance();
1981 views->addWidget(activationView);
1982 if (views->currentWidget() != activationView) showView(activationView);
1986 void MainWindow::showRegionsView() {
1988 regionsView = new RegionsView(this);
1989 connect(regionsView, SIGNAL(regionChanged()), homeView->getStandardFeedsView(),
1991 views->addWidget(regionsView);
1993 showView(regionsView);