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"
96 #include "subscriptionimportview.h"
99 MainWindow *mainWindowInstance;
102 MainWindow *MainWindow::instance() {
103 return mainWindowInstance;
106 MainWindow::MainWindow()
107 : aboutView(nullptr), downloadView(nullptr), regionsView(nullptr), mainToolBar(nullptr),
108 fullScreenActive(false), compactModeActive(false), initialized(false), toolbarMenu(nullptr),
110 mainWindowInstance = this;
113 views = new QStackedWidget();
114 setCentralWidget(views);
117 Extra::windowSetup(this);
120 messageLabel = new QLabel(this);
121 messageLabel->setWordWrap(false);
122 messageLabel->setStyleSheet("padding:5px;border:0;background:palette(window)");
123 messageLabel->setAlignment(Qt::AlignCenter);
124 messageLabel->hide();
125 adjustMessageLabelPosition();
126 messageTimer = new QTimer(this);
127 messageTimer->setInterval(5000);
128 messageTimer->setSingleShot(true);
129 connect(messageTimer, SIGNAL(timeout()), SLOT(hideMessage()));
132 homeView = new HomeView(this);
133 views->addWidget(homeView);
135 // TODO make this lazy
136 mediaView = MediaView::instance();
137 mediaView->setEnabled(false);
138 views->addWidget(mediaView);
147 // remove that useless menu/toolbar context menu
148 this->setContextMenuPolicy(Qt::NoContextMenu);
150 // event filter to block ugly toolbar tooltips
151 qApp->installEventFilter(this);
153 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
155 // restore window position
158 // fix stacked widget minimum size
159 for (int i = 0; i < views->count(); i++)
160 views->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
163 #ifdef APP_ACTIVATION
164 Activation::instance().initialCheck();
169 if (VideoAPI::impl() == VideoAPI::IV) {
170 Invidious::instance().initServers();
171 } else if (VideoAPI::impl() == VideoAPI::YT3) {
172 YT3::instance().initApiKeys();
173 } else if (VideoAPI::impl() == VideoAPI::JS) {
174 JS::instance().getNamFactory().setRequestHeaders(
175 {{"User-Agent", HttpUtils::stealthUserAgent()}});
176 JS::instance().initialize(QUrl(QLatin1String(Constants::WEBSITE) + "-ws/bundle2.js"));
177 /// JS::instance().initialize(QUrl("http://localhost:8000/bundle-test.js"));
178 Invidious::instance().initServers();
181 QTimer::singleShot(100, this, &MainWindow::lazyInit);
184 void MainWindow::lazyInit() {
185 mediaView->initialize();
187 qApp->processEvents();
190 if (qApp->arguments().size() > 1) {
191 QString query = qApp->arguments().at(1);
192 if (query.startsWith(QLatin1String("--"))) {
193 messageReceived(query);
196 SearchParams *searchParams = new SearchParams();
197 searchParams->setKeywords(query);
198 showMedia(searchParams);
201 showMessage(tr("Make yourself comfortable"));
204 GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
206 if (GnomeGlobalShortcutBackend::IsGsdAvailable())
207 shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
212 connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
213 connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
214 connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
215 connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
216 // connect(&shortcuts, SIGNAL(StopAfter()), getAction("stopafterthis"), SLOT(toggle()));
218 connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
219 SLOT(updateDownloadMessage(QString)));
220 connect(DownloadManager::instance(), SIGNAL(finished()), SLOT(downloadsFinished()));
222 setAcceptDrops(true);
224 fullscreenTimer = new QTimer(this);
225 fullscreenTimer->setInterval(3000);
226 fullscreenTimer->setSingleShot(true);
227 connect(fullscreenTimer, SIGNAL(timeout()), SLOT(hideFullscreenUI()));
229 JsFunctions::instance();
231 // Hack to give focus to searchlineedit
232 View *view = qobject_cast<View *>(views->currentWidget());
233 if (view == homeView) {
234 QMetaObject::invokeMethod(views->currentWidget(), "appear");
235 const QString &desc = view->getDescription();
236 if (!desc.isEmpty()) showMessage(desc);
239 ChannelAggregator::instance()->start();
242 Updater::instance().checkWithoutUI();
248 void MainWindow::changeEvent(QEvent *e) {
250 if (e->type() == QEvent::WindowStateChange) {
251 getAction("minimize")->setEnabled(!isMinimized());
254 if (messageLabel->isVisible()) {
255 if (e->type() == QEvent::ActivationChange || e->type() == QEvent::WindowStateChange ||
256 e->type() == QEvent::WindowDeactivate || e->type() == QEvent::ApplicationStateChange) {
260 QMainWindow::changeEvent(e);
263 bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
264 const QEvent::Type t = e->type();
267 static bool altPressed = false;
268 if (t == QEvent::KeyRelease && altPressed) {
270 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
271 if (ke->key() == Qt::Key_Alt) {
272 toggleMenuVisibility();
275 } else if (t == QEvent::KeyPress) {
276 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
277 altPressed = ke->key() == Qt::Key_Alt;
281 if (fullScreenActive && views->currentWidget() == mediaView && t == QEvent::MouseMove &&
282 obj->isWidgetType() && qobject_cast<QWidget *>(obj)->window() == this) {
283 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
285 bool toolBarVisible = mainToolBar && mainToolBar->isVisible();
286 bool sidebarVisible = mediaView->isSidebarVisible();
288 if (!sidebarVisible && !toolBarVisible) {
289 const int x = mouseEvent->pos().x();
290 if (x >= 0 && x < 5) {
292 SidebarWidget *sidebar = mediaView->getSidebar();
293 sidebar->resize(sidebar->width(), height());
295 mediaView->setSidebarVisibility(true);
296 sidebarVisible = true;
301 if (!toolBarVisible && !sidebarVisible) {
302 const int y = mouseEvent->pos().y();
303 if (y >= 0 && y < 5) {
304 mainToolBar->resize(width(), mainToolBar->sizeHint().height());
305 mainToolBar->setVisible(true);
310 // show the normal cursor
312 // then hide it again after a few seconds
313 fullscreenTimer->start();
316 if (t == QEvent::ToolTip) {
321 if (t == QEvent::Show && obj == toolbarMenu) {
323 int x = width() - toolbarMenu->sizeHint().width();
326 int x = toolbarMenuButton->x() + toolbarMenuButton->width() -
327 toolbarMenu->sizeHint().width();
328 int y = toolbarMenuButton->y() + toolbarMenuButton->height();
331 toolbarMenu->move(mapToGlobal(p));
334 if (obj == this && t == QEvent::StyleChange) {
335 qDebug() << "Style change detected";
336 qApp->paletteChanged(qApp->palette());
340 // standard event processing
341 return QMainWindow::eventFilter(obj, e);
344 void MainWindow::createActions() {
345 stopAct = new QAction(tr("&Stop"), this);
346 IconUtils::setIcon(stopAct, "media-playback-stop");
347 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
348 stopAct->setShortcuts(QList<QKeySequence>()
349 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
350 stopAct->setEnabled(false);
351 actionMap.insert("stop", stopAct);
352 connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
354 skipBackwardAct = new QAction(tr("P&revious"), this);
355 skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
356 skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
357 skipBackwardAct->setEnabled(false);
358 actionMap.insert("previous", skipBackwardAct);
359 connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
361 skipAct = new QAction(tr("S&kip"), this);
362 IconUtils::setIcon(skipAct, "media-skip-forward");
363 skipAct->setStatusTip(tr("Skip to the next video"));
364 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right)
365 << QKeySequence(Qt::Key_MediaNext));
366 skipAct->setEnabled(false);
367 actionMap.insert("skip", skipAct);
368 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
370 pauseAct = new QAction(tr("&Play"), this);
371 IconUtils::setIcon(pauseAct, "media-playback-start");
372 pauseAct->setStatusTip(tr("Resume playback"));
373 pauseAct->setShortcuts(QList<QKeySequence>()
374 << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
375 pauseAct->setEnabled(false);
376 actionMap.insert("pause", pauseAct);
377 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
379 fullscreenAct = new QAction(tr("&Full Screen"), this);
380 IconUtils::setIcon(fullscreenAct, "view-fullscreen");
381 fullscreenAct->setStatusTip(tr("Go full screen"));
382 QList<QKeySequence> fsShortcuts;
384 fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
386 fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
388 fullscreenAct->setShortcuts(fsShortcuts);
389 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
390 fullscreenAct->setPriority(QAction::LowPriority);
391 actionMap.insert("fullscreen", fullscreenAct);
392 connect(fullscreenAct, SIGNAL(triggered()), SLOT(toggleFullscreen()));
394 compactViewAct = new QAction(tr("&Compact Mode"), this);
395 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
396 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
397 compactViewAct->setCheckable(true);
398 compactViewAct->setChecked(false);
399 compactViewAct->setEnabled(false);
400 actionMap.insert("compactView", compactViewAct);
401 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
403 webPageAct = new QAction(tr("Open the &YouTube Page"), this);
404 webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
405 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
406 webPageAct->setEnabled(false);
407 actionMap.insert("webpage", webPageAct);
408 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
410 copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
411 IconUtils::setIcon(copyPageAct, "link");
412 copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
413 copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
414 copyPageAct->setEnabled(false);
415 actionMap.insert("pagelink", copyPageAct);
416 connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
418 copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
419 copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
420 copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
421 copyLinkAct->setEnabled(false);
422 actionMap.insert("videolink", copyLinkAct);
423 connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
425 findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
426 findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
427 findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
428 findVideoPartsAct->setEnabled(false);
429 connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
430 actionMap.insert("findVideoParts", findVideoPartsAct);
432 removeAct = new QAction(tr("&Remove"), this);
433 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
434 removeAct->setShortcuts(QList<QKeySequence>()
435 << QKeySequence("Del") << QKeySequence("Backspace"));
436 removeAct->setEnabled(false);
437 actionMap.insert("remove", removeAct);
438 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
440 moveUpAct = new QAction(tr("Move &Up"), this);
441 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
442 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
443 moveUpAct->setEnabled(false);
444 actionMap.insert("moveUp", moveUpAct);
445 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
447 moveDownAct = new QAction(tr("Move &Down"), this);
448 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
449 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
450 moveDownAct->setEnabled(false);
451 actionMap.insert("moveDown", moveDownAct);
452 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
454 clearAct = new QAction(tr("&Clear Recent Searches"), this);
455 clearAct->setMenuRole(QAction::ApplicationSpecificRole);
456 clearAct->setShortcuts(QList<QKeySequence>()
457 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
458 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
459 clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
460 clearAct->setEnabled(true);
461 actionMap.insert("clearRecentKeywords", clearAct);
462 connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
464 quitAct = new QAction(tr("&Quit"), this);
465 quitAct->setMenuRole(QAction::QuitRole);
466 quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
467 quitAct->setStatusTip(tr("Bye"));
468 actionMap.insert("quit", quitAct);
469 connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
471 siteAct = new QAction(tr("&Website"), this);
472 siteAct->setShortcut(QKeySequence::HelpContents);
473 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
474 actionMap.insert("site", siteAct);
475 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
477 #if !defined(APP_MAC) && !defined(APP_WIN)
478 donateAct = new QAction(tr("Make a &Donation"), this);
479 donateAct->setStatusTip(
480 tr("Please support the continued development of %1").arg(Constants::NAME));
481 actionMap.insert("donate", donateAct);
482 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
485 aboutAct = new QAction(tr("&About"), this);
486 aboutAct->setMenuRole(QAction::AboutRole);
487 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
488 actionMap.insert("about", aboutAct);
489 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
493 searchFocusAct = new QAction(this);
494 searchFocusAct->setShortcut(QKeySequence::Find);
495 searchFocusAct->setStatusTip(tr("Search"));
496 actionMap.insert("search", searchFocusAct);
497 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
498 addAction(searchFocusAct);
500 volumeUpAct = new QAction(this);
501 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
502 actionMap.insert("volumeUp", volumeUpAct);
503 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
504 addAction(volumeUpAct);
506 volumeDownAct = new QAction(this);
507 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
508 actionMap.insert("volumeDown", volumeDownAct);
509 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
510 addAction(volumeDownAct);
512 volumeMuteAct = new QAction(this);
513 IconUtils::setIcon(volumeMuteAct, "audio-volume-high");
514 volumeMuteAct->setStatusTip(tr("Mute volume"));
515 volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_M));
516 actionMap.insert("volumeMute", volumeMuteAct);
517 connect(volumeMuteAct, SIGNAL(triggered()), SLOT(toggleVolumeMute()));
518 addAction(volumeMuteAct);
520 QToolButton *definitionButton = new QToolButton(this);
521 definitionButton->setText(YT3::instance().maxVideoDefinition().getName());
522 IconUtils::setIcon(definitionButton, "video-display");
523 definitionButton->setIconSize(QSize(16, 16));
524 definitionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
525 definitionButton->setPopupMode(QToolButton::InstantPopup);
526 QMenu *definitionMenu = new QMenu(this);
527 QActionGroup *group = new QActionGroup(this);
528 for (auto &defName : VideoDefinition::getDefinitionNames()) {
529 QAction *a = new QAction(defName);
530 a->setCheckable(true);
531 a->setActionGroup(group);
532 a->setChecked(defName == YT3::instance().maxVideoDefinition().getName());
533 connect(a, &QAction::triggered, this, [this, defName, definitionButton] {
534 setDefinitionMode(defName);
535 definitionButton->setText(defName);
537 connect(&YT3::instance(), &YT3::maxVideoDefinitionChanged, this,
538 [defName, definitionButton](const QString &name) {
539 if (defName == name) definitionButton->setChecked(true);
541 definitionMenu->addAction(a);
543 definitionButton->setMenu(definitionMenu);
544 QWidgetAction *definitionAct = new QWidgetAction(this);
545 definitionAct->setDefaultWidget(definitionButton);
546 definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
547 actionMap.insert("definition", definitionAct);
548 addAction(definitionAct);
552 action = new QAction(tr("&Manually Start Playing"), this);
553 IconUtils::setIcon(action, "media-playback-start");
554 action->setStatusTip(tr("Manually start playing videos"));
555 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
556 action->setCheckable(true);
557 connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
558 actionMap.insert("manualplay", action);
560 action = new QAction(tr("&Downloads"), this);
561 IconUtils::setIcon(action, "document-save");
562 action->setStatusTip(tr("Show details about video downloads"));
563 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
564 action->setCheckable(true);
565 connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
566 actionMap.insert("downloads", action);
568 action = new QAction(tr("&Download"), this);
569 IconUtils::setIcon(action, "document-save");
570 action->setStatusTip(tr("Download the current video"));
571 action->setShortcut(QKeySequence::Save);
572 action->setEnabled(false);
573 action->setVisible(false);
574 action->setPriority(QAction::LowPriority);
575 connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
576 actionMap.insert("download", action);
579 action = new QAction(tr("Take &Snapshot"), this);
580 action->setShortcut(QKeySequence(Qt::Key_F9));
581 action->setEnabled(false);
582 actionMap.insert("snapshot", action);
583 connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
586 action = new QAction(tr("&Subscribe to Channel"), this);
587 action->setProperty("originalText", action->text());
588 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
589 action->setEnabled(false);
590 connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
591 actionMap.insert("subscribeChannel", action);
592 mediaView->updateSubscriptionActionForVideo(0, false);
594 QString shareTip = tr("Share the current video using %1");
596 action = new QAction("&Twitter", this);
597 IconUtils::setIcon(action, "twitter");
598 action->setStatusTip(shareTip.arg("Twitter"));
599 action->setEnabled(false);
600 actionMap.insert("twitter", action);
601 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
603 action = new QAction("&Facebook", this);
604 IconUtils::setIcon(action, "facebook");
605 action->setStatusTip(shareTip.arg("Facebook"));
606 action->setEnabled(false);
607 actionMap.insert("facebook", action);
608 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
610 action = new QAction(tr("&Email"), this);
611 IconUtils::setIcon(action, "email");
612 action->setStatusTip(shareTip.arg(tr("Email")));
613 action->setEnabled(false);
614 actionMap.insert("email", action);
615 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
617 action = new QAction(tr("&Close"), this);
618 action->setShortcut(QKeySequence(QKeySequence::Close));
619 actionMap.insert("close", action);
620 connect(action, SIGNAL(triggered()), SLOT(close()));
622 action = new QAction(Constants::NAME, this);
623 action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
624 actionMap.insert("restore", action);
625 connect(action, SIGNAL(triggered()), SLOT(restore()));
627 action = new QAction(tr("&Float on Top"), this);
628 IconUtils::setIcon(action, "go-top");
629 action->setCheckable(true);
630 actionMap.insert("ontop", action);
631 connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
633 action = new QAction(tr("&Stop After This Video"), this);
634 IconUtils::setIcon(action, "media-playback-stop");
635 action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
636 action->setCheckable(true);
637 action->setEnabled(false);
638 actionMap.insert("stopafterthis", action);
639 connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
641 action = new QAction(tr("&Report an Issue..."), this);
642 actionMap.insert("reportIssue", action);
643 connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
645 action = new QAction(tr("&Refine Search..."), this);
646 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
647 action->setCheckable(true);
648 action->setEnabled(false);
649 actionMap.insert("refineSearch", action);
651 action = new QAction(YTRegions::worldwideRegion().name, this);
652 actionMap.insert("worldwideRegion", action);
654 action = new QAction(YTRegions::localRegion().name, this);
655 actionMap.insert("localRegion", action);
657 action = new QAction(tr("More..."), this);
658 actionMap.insert("moreRegion", action);
660 action = new QAction(tr("&Related Videos"), this);
661 IconUtils::setIcon(action, "view-list");
662 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
663 action->setStatusTip(tr("Watch videos related to the current one"));
664 action->setEnabled(false);
665 action->setPriority(QAction::LowPriority);
666 connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
667 actionMap.insert("relatedVideos", action);
669 action = new QAction(tr("Open in &Browser..."), this);
670 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
671 action->setEnabled(false);
672 actionMap.insert("openInBrowser", action);
673 connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
675 action = new QAction(tr("Restricted Mode"), this);
676 IconUtils::setIcon(action, "safesearch");
677 action->setStatusTip(tr("Hide videos that may contain inappropriate content"));
678 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
679 action->setCheckable(true);
680 action->setVisible(VideoAPI::impl() != VideoAPI::IV);
681 actionMap.insert("safeSearch", action);
683 action = new QAction(tr("Toggle &Menu Bar"), this);
684 connect(action, SIGNAL(triggered()), SLOT(toggleMenuVisibilityWithMessage()));
685 actionMap.insert("toggleMenu", action);
687 action = new QAction(tr("Menu"), this);
688 IconUtils::setIcon(action, "open-menu");
689 connect(action, SIGNAL(triggered()), SLOT(toggleToolbarMenu()));
690 actionMap.insert("toolbarMenu", action);
692 action = new QAction(tr("Import Subscriptions..."), this);
693 action->setMenuRole(QAction::ApplicationSpecificRole);
694 connect(action, &QAction::triggered, this, [this] {
695 if (!subscriptionImportView) {
696 subscriptionImportView = new SubscriptionImportView(this);
697 views->addWidget(subscriptionImportView);
699 showView(subscriptionImportView);
701 actionMap.insert("importSubscriptions", action);
704 action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
705 actionMap.insert("appStore", action);
706 connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
709 #ifdef APP_ACTIVATION
710 ActivationView::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
713 // common action properties
714 for (QAction *action : qAsConst(actionMap)) {
715 // add actions to the MainWindow so that they work
716 // when the menu is hidden
722 void MainWindow::createMenus() {
723 fileMenu = menuBar()->addMenu(tr("&Application"));
724 #ifdef APP_ACTIVATION
725 QAction *buyAction = getAction("buy");
726 if (buyAction) fileMenu->addAction(buyAction);
728 fileMenu->addSeparator();
731 fileMenu->addAction(clearAct);
733 fileMenu->addSeparator();
735 fileMenu->addAction(quitAct);
737 QMenu *playbackMenu = menuBar()->addMenu(tr("&Playback"));
738 menuMap.insert("playback", playbackMenu);
739 playbackMenu->addAction(pauseAct);
740 playbackMenu->addAction(stopAct);
741 playbackMenu->addAction(getAction("stopafterthis"));
742 playbackMenu->addSeparator();
743 playbackMenu->addAction(skipAct);
744 playbackMenu->addAction(skipBackwardAct);
745 playbackMenu->addSeparator();
746 playbackMenu->addAction(getAction("manualplay"));
748 MacSupport::dockMenu(playbackMenu);
751 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
752 menuMap.insert("playlist", playlistMenu);
753 playlistMenu->addAction(removeAct);
754 playlistMenu->addSeparator();
755 playlistMenu->addAction(moveUpAct);
756 playlistMenu->addAction(moveDownAct);
757 playlistMenu->addSeparator();
758 playlistMenu->addAction(getAction("refineSearch"));
760 QMenu *videoMenu = menuBar()->addMenu(tr("&Video"));
761 menuMap.insert("video", videoMenu);
762 videoMenu->addAction(getAction("relatedVideos"));
763 videoMenu->addAction(findVideoPartsAct);
764 videoMenu->addSeparator();
765 videoMenu->addAction(getAction("subscribeChannel"));
767 videoMenu->addSeparator();
768 videoMenu->addAction(getAction("snapshot"));
770 videoMenu->addSeparator();
771 videoMenu->addAction(webPageAct);
772 videoMenu->addAction(copyLinkAct);
773 videoMenu->addAction(getAction("openInBrowser"));
774 videoMenu->addAction(getAction("download"));
776 QMenu *shareMenu = menuBar()->addMenu(tr("&Share"));
777 menuMap.insert("share", shareMenu);
778 shareMenu->addAction(copyPageAct);
779 shareMenu->addSeparator();
780 shareMenu->addAction(getAction("twitter"));
781 shareMenu->addAction(getAction("facebook"));
782 shareMenu->addSeparator();
783 shareMenu->addAction(getAction("email"));
785 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
786 menuMap.insert("view", viewMenu);
787 viewMenu->addAction(getAction("ontop"));
788 viewMenu->addAction(compactViewAct);
789 viewMenu->addSeparator();
790 viewMenu->addAction(fullscreenAct);
792 viewMenu->addSeparator();
793 viewMenu->addAction(getAction("toggleMenu"));
797 MacSupport::windowMenu(this);
800 helpMenu = menuBar()->addMenu(tr("&Help"));
801 menuMap.insert("help", helpMenu);
802 helpMenu->addAction(siteAct);
803 #if !defined(APP_MAC) && !defined(APP_WIN)
804 helpMenu->addAction(donateAct);
806 helpMenu->addAction(getAction("reportIssue"));
807 helpMenu->addAction(aboutAct);
809 helpMenu->addAction(Updater::instance().getAction());
813 helpMenu->addSeparator();
814 helpMenu->addAction(getAction("appStore"));
818 void MainWindow::createToolBar() {
820 currentTimeLabel = new QLabel("00:00", this);
822 seekSlider = new SeekSlider(this);
823 seekSlider->setEnabled(false);
824 seekSlider->setTracking(false);
825 seekSlider->setMaximum(1000);
826 volumeSlider = new SeekSlider(this);
827 volumeSlider->setValue(volumeSlider->maximum());
829 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
830 SearchWrapper *searchWrapper = new SearchWrapper(this);
831 toolbarSearch = searchWrapper->getSearchLineEdit();
833 toolbarSearch = new SearchLineEdit(this);
835 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize() * 15);
836 toolbarSearch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
837 toolbarSearch->setSuggester(new YTSuggester(this));
838 connect(toolbarSearch, SIGNAL(search(const QString &)), SLOT(search(const QString &)));
839 connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion *)),
840 SLOT(suggestionAccepted(Suggestion *)));
841 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
843 // Add widgets to toolbar
845 #ifdef APP_MAC_QMACTOOLBAR
846 currentTimeLabel->hide();
847 toolbarSearch->hide();
848 volumeSlider->hide();
850 MacToolbar::instance().createToolbar(this);
854 mainToolBar = new QToolBar(this);
855 mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
856 mainToolBar->setFloatable(false);
857 mainToolBar->setMovable(false);
859 mainToolBar->setIconSize(QSize(32, 32));
861 mainToolBar->addAction(stopAct);
862 QToolButton *stopToolButton =
863 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(stopAct));
864 if (stopToolButton) {
865 QMenu *stopMenu = new QMenu(this);
866 stopMenu->addAction(getAction("stopafterthis"));
867 stopToolButton->setMenu(stopMenu);
868 stopToolButton->setPopupMode(QToolButton::DelayedPopup);
870 mainToolBar->addAction(pauseAct);
871 mainToolBar->addAction(skipAct);
872 mainToolBar->addAction(getAction("relatedVideos"));
874 bool addFullScreenAct = true;
876 addFullScreenAct = !mac::CanGoFullScreen(winId());
878 if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
880 mainToolBar->addWidget(new Spacer());
882 currentTimeLabel->setFont(FontUtils::small());
883 currentTimeLabel->setMinimumWidth(currentTimeLabel->fontInfo().pixelSize() * 4);
884 currentTimeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
885 mainToolBar->addWidget(currentTimeLabel);
888 mainToolBar->addWidget(new Spacer(nullptr, 10));
891 seekSlider->setOrientation(Qt::Horizontal);
892 seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
893 seekSlider->setFocusPolicy(Qt::NoFocus);
894 mainToolBar->addWidget(seekSlider);
896 mainToolBar->addWidget(new Spacer());
898 mainToolBar->addAction(volumeMuteAct);
899 #ifndef APP_MAC_QMACTOOLBAR
900 QToolButton *volumeMuteButton =
901 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
902 volumeMuteButton->setIconSize(QSize(16, 16));
903 auto fixVolumeMuteIconSize = [volumeMuteButton] {
904 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
906 fixVolumeMuteIconSize();
907 volumeMuteButton->connect(volumeMuteAct, &QAction::changed, volumeMuteButton,
908 fixVolumeMuteIconSize);
911 volumeSlider->setStatusTip(
912 tr("Press %1 to raise the volume, %2 to lower it")
913 .arg(volumeUpAct->shortcut().toString(QKeySequence::NativeText),
914 volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
916 volumeSlider->setOrientation(Qt::Horizontal);
917 // this makes the volume slider smaller
918 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
919 volumeSlider->setFocusPolicy(Qt::NoFocus);
920 mainToolBar->addWidget(volumeSlider);
922 mainToolBar->addWidget(new Spacer());
924 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
925 mainToolBar->addWidget(searchWrapper);
927 mainToolBar->addWidget(toolbarSearch);
928 mainToolBar->addWidget(new Spacer(this, toolbarSearch->height() / 2));
930 QAction *toolbarMenuAction = getAction("toolbarMenu");
931 mainToolBar->addAction(toolbarMenuAction);
933 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(toolbarMenuAction));
936 addToolBar(mainToolBar);
939 void MainWindow::createStatusBar() {
940 statusToolBar = new QToolBar(statusBar());
941 statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
942 statusToolBar->setIconSize(QSize(16, 16));
944 regionAction = new QAction(this);
945 regionAction->setStatusTip(tr("Choose your content location"));
947 QAction *localAction = getAction("localRegion");
948 if (!localAction->text().isEmpty()) {
949 QMenu *regionMenu = new QMenu(this);
950 regionMenu->addAction(getAction("worldwideRegion"));
951 regionMenu->addAction(localAction);
952 regionMenu->addSeparator();
953 QAction *moreRegionsAction = getAction("moreRegion");
954 regionMenu->addAction(moreRegionsAction);
955 connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
956 regionAction->setMenu(regionMenu);
958 connect(regionAction, SIGNAL(triggered()), SLOT(showRegionsView()));
960 /* Stupid code that generates the QRC items
961 foreach(YTRegion r, YTRegions::list())
962 qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
965 statusBar()->addPermanentWidget(statusToolBar);
969 void MainWindow::showStopAfterThisInStatusBar(bool show) {
970 QAction *action = getAction("stopafterthis");
971 showActionsInStatusBar({action}, show);
974 void MainWindow::showActionsInStatusBar(const QVector<QAction *> &actions, bool show) {
976 Extra::fadeInWidget(statusBar(), statusBar());
978 for (auto action : actions) {
980 if (statusToolBar->actions().contains(action)) continue;
981 if (statusToolBar->actions().isEmpty()) {
982 statusToolBar->addAction(action);
984 statusToolBar->insertAction(statusToolBar->actions().at(0), action);
987 statusToolBar->removeAction(action);
992 if (statusBar()->isHidden() && !fullScreenActive) setStatusBarVisibility(true);
994 if (statusBar()->isVisible() && !needStatusBar()) setStatusBarVisibility(false);
998 void MainWindow::setStatusBarVisibility(bool show) {
999 if (statusBar()->isVisible() != show) {
1000 statusBar()->setVisible(show);
1001 if (views->currentWidget() == mediaView)
1002 QTimer::singleShot(0, mediaView, SLOT(adjustWindowSize()));
1006 void MainWindow::adjustStatusBarVisibility() {
1007 setStatusBarVisibility(needStatusBar());
1010 void MainWindow::hideToolbar() {
1012 mac::showToolBar(winId(), false);
1014 mainToolBar->hide();
1018 void MainWindow::showToolbar() {
1020 mac::showToolBar(winId(), true);
1022 mainToolBar->show();
1026 void MainWindow::readSettings() {
1028 QByteArray geometrySettings = settings.value("geometry").toByteArray();
1029 if (!geometrySettings.isEmpty()) {
1030 restoreGeometry(geometrySettings);
1032 const QRect desktopSize = QGuiApplication::primaryScreen()->availableGeometry();
1033 int w = desktopSize.width() * .9;
1034 int h = qMin(w / 2, desktopSize.height());
1036 QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, QSize(w, h), desktopSize));
1038 setDefinitionMode(settings.value("definition", YT3::instance().maxVideoDefinition().getName())
1040 getAction("manualplay")->setChecked(settings.value("manualplay", false).toBool());
1041 getAction("safeSearch")->setChecked(settings.value("safeSearch", false).toBool());
1043 menuBar()->setVisible(settings.value("menuBar", false).toBool());
1047 void MainWindow::writeSettings() {
1050 if (!isReallyFullScreen()) {
1051 settings.setValue("geometry", saveGeometry());
1052 if (mediaView) mediaView->saveSplitterState();
1055 settings.setValue("manualplay", getAction("manualplay")->isChecked());
1056 settings.setValue("safeSearch", getAction("safeSearch")->isChecked());
1058 settings.setValue("menuBar", menuBar()->isVisible());
1062 void MainWindow::goBack() {
1063 if (history.size() > 1) {
1065 showView(history.pop());
1069 void MainWindow::showView(View *view, bool transition) {
1070 if (!history.isEmpty() && view == history.top()) {
1071 qDebug() << "Attempting to show same view" << view;
1076 if (transition && !history.isEmpty()) CompositeFader::go(this, this->grab());
1079 if (compactViewAct->isChecked()) compactViewAct->toggle();
1081 // call hide method on the current view
1082 View *oldView = qobject_cast<View *>(views->currentWidget());
1084 oldView->willDisappear();
1085 oldView->disappear();
1086 oldView->setEnabled(false);
1087 oldView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
1089 qDebug() << "Cannot cast old view";
1092 view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1093 view->setEnabled(true);
1094 views->setCurrentWidget(view);
1097 if (oldView) oldView->didDisappear();
1099 QString title = view->getTitle();
1100 if (title.isEmpty())
1101 title = Constants::NAME;
1103 title += QLatin1String(" - ") + Constants::NAME;
1104 setWindowTitle(title);
1106 const bool isMediaView = view == mediaView;
1107 stopAct->setEnabled(isMediaView);
1108 compactViewAct->setEnabled(isMediaView);
1109 toolbarSearch->setEnabled(isMediaView);
1110 aboutAct->setEnabled(view != aboutView);
1111 getAction("downloads")->setChecked(view == downloadView);
1113 // dynamic view actions
1114 /* Not currently used by any view
1115 showActionsInStatusBar(viewActions, false);
1116 viewActions = newView->getViewActions();
1117 showActionsInStatusBar(viewActions, true);
1124 void MainWindow::about() {
1126 aboutView = new AboutView(this);
1127 views->addWidget(aboutView);
1129 showView(aboutView);
1132 void MainWindow::visitSite() {
1133 QUrl url(Constants::WEBSITE);
1134 showMessage(QString(tr("Opening %1").arg(url.toString())));
1135 QDesktopServices::openUrl(url);
1138 void MainWindow::donate() {
1139 QUrl url("https://" + QLatin1String(Constants::ORG_DOMAIN) + "/donate");
1140 showMessage(QString(tr("Opening %1").arg(url.toString())));
1141 QDesktopServices::openUrl(url);
1144 void MainWindow::reportIssue() {
1145 QUrl url("https://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
1146 QDesktopServices::openUrl(url);
1149 void MainWindow::quit() {
1151 if (!confirmQuit()) {
1155 // do not save geometry when in full screen or in compact mode
1156 if (!fullScreenActive && !compactViewAct->isChecked()) {
1162 // mediaView->stop();
1163 Temporary::deleteAll();
1164 ChannelAggregator::instance()->stop();
1165 ChannelAggregator::instance()->cleanup();
1166 Database::shutdown();
1170 void MainWindow::closeEvent(QCloseEvent *e) {
1172 mac::closeWindow(winId());
1175 if (!confirmQuit()) {
1179 QWidget::closeEvent(e);
1182 messageLabel->hide();
1185 void MainWindow::showEvent(QShowEvent *e) {
1186 QWidget::showEvent(e);
1192 bool MainWindow::confirmQuit() {
1193 if (DownloadManager::instance()->activeItems() > 0) {
1194 QMessageBox msgBox(this);
1195 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1197 tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1198 msgBox.setInformativeText(
1199 tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1200 msgBox.setModal(true);
1201 // make it a "sheet" on the Mac
1202 msgBox.setWindowModality(Qt::WindowModal);
1204 msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1205 QPushButton *waitButton =
1206 msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1210 if (msgBox.clickedButton() == waitButton) {
1217 void MainWindow::showHome() {
1219 currentTimeLabel->clear();
1220 seekSlider->setValue(0);
1223 void MainWindow::showMedia(SearchParams *searchParams) {
1224 showView(mediaView);
1225 if (getAction("safeSearch")->isChecked())
1226 searchParams->setSafeSearch(SearchParams::Strict);
1228 searchParams->setSafeSearch(SearchParams::None);
1229 mediaView->search(searchParams);
1232 void MainWindow::showMedia(VideoSource *videoSource) {
1233 showView(mediaView);
1234 mediaView->setVideoSource(videoSource);
1237 void MainWindow::stateChanged(Media::State newState) {
1238 qDebug() << newState;
1240 seekSlider->setEnabled(newState != Media::StoppedState);
1243 case Media::ErrorState:
1244 showMessage(tr("Error: %1").arg(media->errorString()));
1247 case Media::PlayingState:
1248 pauseAct->setEnabled(true);
1249 pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1250 pauseAct->setText(tr("&Pause"));
1251 pauseAct->setStatusTip(tr("Pause playback") + " (" +
1252 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1255 case Media::StoppedState:
1256 pauseAct->setEnabled(false);
1257 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1258 pauseAct->setText(tr("&Play"));
1259 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1260 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1263 case Media::PausedState:
1264 pauseAct->setEnabled(true);
1265 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1266 pauseAct->setText(tr("&Play"));
1267 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1268 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1271 case Media::BufferingState:
1272 pauseAct->setEnabled(false);
1273 pauseAct->setIcon(IconUtils::icon("content-loading"));
1274 pauseAct->setText(tr("&Loading..."));
1275 pauseAct->setStatusTip(QString());
1278 case Media::LoadingState:
1279 pauseAct->setEnabled(false);
1280 currentTimeLabel->clear();
1287 void MainWindow::stop() {
1292 void MainWindow::resizeEvent(QResizeEvent *e) {
1295 if (initialized && mac::CanGoFullScreen(winId())) {
1296 bool isFullscreen = mac::IsFullScreen(winId());
1297 if (isFullscreen != fullScreenActive) {
1298 if (compactViewAct->isChecked()) {
1299 compactViewAct->setChecked(false);
1302 fullScreenActive = isFullscreen;
1303 updateUIForFullscreen();
1307 #ifdef APP_MAC_QMACTOOLBAR
1308 int moreButtonWidth = 40;
1309 toolbarSearch->move(width() - toolbarSearch->width() - moreButtonWidth - 7, -34);
1314 void MainWindow::enterEvent(QEvent *e) {
1317 // Workaround cursor bug on macOS
1322 void MainWindow::leaveEvent(QEvent *e) {
1324 if (fullScreenActive) hideFullscreenUI();
1327 void MainWindow::toggleFullscreen() {
1328 if (compactViewAct->isChecked()) compactViewAct->toggle();
1331 WId handle = winId();
1332 if (mac::CanGoFullScreen(handle)) {
1333 if (mainToolBar) mainToolBar->setVisible(true);
1334 mac::ToggleFullScreen(handle);
1339 fullScreenActive = !fullScreenActive;
1341 if (fullScreenActive) {
1342 // Enter full screen
1344 maximizedBeforeFullScreen = isMaximized();
1346 // save geometry now, if the user quits when in full screen
1347 // geometry won't be saved
1351 MacSupport::enterFullScreen(this, views);
1353 menuVisibleBeforeFullScreen = menuBar()->isVisible();
1355 if (mainToolBar) mainToolBar->hide();
1363 MacSupport::exitFullScreen(this, views);
1365 menuBar()->setVisible(menuVisibleBeforeFullScreen);
1366 if (mainToolBar) mainToolBar->setVisible(views->currentWidget() == mediaView);
1367 if (maximizedBeforeFullScreen)
1373 // Make sure the window has focus
1377 qApp->processEvents();
1378 updateUIForFullscreen();
1381 void MainWindow::updateUIForFullscreen() {
1382 static QList<QKeySequence> fsShortcuts;
1383 static QString fsText;
1385 if (fullScreenActive) {
1386 fsShortcuts = fullscreenAct->shortcuts();
1387 fsText = fullscreenAct->text();
1388 if (fsText.isEmpty()) qDebug() << "[taking Empty!]";
1389 fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1390 << QKeySequence(Qt::Key_Escape));
1391 fullscreenAct->setText(tr("Leave &Full Screen"));
1392 fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1393 setStatusBarVisibility(false);
1396 removeToolBar(mainToolBar);
1397 mainToolBar->move(0, 0);
1400 mediaView->removeSidebar();
1403 fullscreenAct->setShortcuts(fsShortcuts);
1404 if (fsText.isEmpty()) fsText = "[Empty!]";
1405 fullscreenAct->setText(fsText);
1406 fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1408 if (needStatusBar()) setStatusBarVisibility(true);
1411 addToolBar(mainToolBar);
1414 mediaView->restoreSidebar();
1417 // No compact view action when in full screen
1418 compactViewAct->setVisible(!fullScreenActive);
1419 compactViewAct->setChecked(false);
1421 // Hide anything but the video
1422 mediaView->setSidebarVisibility(!fullScreenActive);
1424 if (fullScreenActive) {
1425 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1427 stopAct->setShortcuts(QList<QKeySequence>()
1428 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1432 MacSupport::fullScreenActions(actionMap, fullScreenActive);
1435 if (views->currentWidget() == mediaView) mediaView->setFocus();
1437 if (fullScreenActive) {
1438 if (views->currentWidget() == mediaView) hideFullscreenUI();
1440 fullscreenTimer->stop();
1445 bool MainWindow::isReallyFullScreen() {
1447 WId handle = winId();
1448 if (mac::CanGoFullScreen(handle))
1449 return mac::IsFullScreen(handle);
1451 return isFullScreen();
1453 return isFullScreen();
1457 void MainWindow::missingKeyWarning() {
1458 static bool shown = false;
1461 QMessageBox msgBox(this);
1462 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1463 msgBox.setText(QString("%1 was built without a Google API key.").arg(Constants::NAME));
1464 msgBox.setInformativeText(QString("It won't work unless you enter one."
1465 "<p>In alternative you can get %1 from the developer site.")
1466 .arg(Constants::NAME));
1467 msgBox.setModal(true);
1468 msgBox.setWindowModality(Qt::WindowModal);
1469 msgBox.addButton(QMessageBox::Close);
1470 QPushButton *enterKeyButton =
1471 msgBox.addButton(QString("Enter API key..."), QMessageBox::AcceptRole);
1472 QPushButton *devButton = msgBox.addButton(QString("Get from %1").arg(Constants::WEBSITE),
1473 QMessageBox::AcceptRole);
1474 QPushButton *helpButton = msgBox.addButton(QMessageBox::Help);
1478 if (msgBox.clickedButton() == helpButton) {
1479 QDesktopServices::openUrl(QUrl("https://github.com/flaviotordini/minitube/blob/master/"
1480 "README.md#google-api-key"));
1481 } else if (msgBox.clickedButton() == enterKeyButton) {
1483 QString text = QInputDialog::getText(this, QString(), "Google API key:", QLineEdit::Normal,
1485 if (ok && !text.isEmpty()) {
1487 settings.setValue("googleApiKey", text);
1488 YT3::instance().initApiKeys();
1490 } else if (msgBox.clickedButton() == devButton) {
1491 QDesktopServices::openUrl(QUrl(Constants::WEBSITE));
1496 void MainWindow::compactView(bool enable) {
1497 setUpdatesEnabled(false);
1499 compactModeActive = enable;
1501 static QList<QKeySequence> compactShortcuts;
1502 static QList<QKeySequence> stopShortcuts;
1504 const QString key = "compactGeometry";
1508 setMinimumSize(320, 180);
1510 mac::RemoveFullScreenWindow(winId());
1514 if (settings.contains(key))
1515 restoreGeometry(settings.value(key).toByteArray());
1519 #ifdef APP_MAC_QMACTOOLBAR
1520 mac::showToolBar(winId(), !enable);
1522 mainToolBar->setVisible(!enable);
1524 mediaView->setSidebarVisibility(!enable);
1525 statusBar()->hide();
1527 compactShortcuts = compactViewAct->shortcuts();
1528 stopShortcuts = stopAct->shortcuts();
1530 QList<QKeySequence> newStopShortcuts(stopShortcuts);
1531 newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1532 stopAct->setShortcuts(newStopShortcuts);
1533 compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts)
1534 << QKeySequence(Qt::Key_Escape));
1536 // ensure focus does not end up to the search box
1537 // as it would steal the Space shortcut
1538 mediaView->setFocus();
1541 settings.setValue(key, saveGeometry());
1543 // unset minimum size
1544 setMinimumSize(0, 0);
1547 mac::SetupFullScreenWindow(winId());
1549 #ifdef APP_MAC_QMACTOOLBAR
1550 mac::showToolBar(winId(), !enable);
1552 mainToolBar->setVisible(!enable);
1554 mediaView->setSidebarVisibility(!enable);
1555 if (needStatusBar()) setStatusBarVisibility(true);
1559 compactViewAct->setShortcuts(compactShortcuts);
1560 stopAct->setShortcuts(stopShortcuts);
1563 // auto float on top
1564 floatOnTop(enable, false);
1567 mac::compactMode(winId(), enable);
1570 menuVisibleBeforeCompactMode = menuBar()->isVisible();
1573 menuBar()->setVisible(menuVisibleBeforeCompactMode);
1577 setUpdatesEnabled(true);
1580 void MainWindow::toggleToolbarMenu() {
1581 if (!toolbarMenu) toolbarMenu = new ToolbarMenu(this);
1582 if (toolbarMenu->isVisible())
1583 toolbarMenu->hide();
1585 toolbarMenu->show();
1588 void MainWindow::searchFocus() {
1589 toolbarSearch->selectAll();
1590 toolbarSearch->setFocus();
1593 void MainWindow::initMedia() {
1595 qFatal("QtAV has a showstopper bug. Audio stops randomly. See bug "
1596 "https://github.com/wang-bin/QtAV/issues/1184");
1597 media = new MediaQtAV(this);
1598 #elif defined MEDIA_MPV
1599 media = new MediaMPV();
1601 qFatal("No media backend defined");
1604 media->setUserAgent(HttpUtils::stealthUserAgent());
1607 qreal volume = settings.value("volume", 1.).toReal();
1608 media->setVolume(volume);
1610 connect(media, &Media::error, this, &MainWindow::handleError);
1611 connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
1612 connect(media, &Media::positionChanged, this, &MainWindow::tick);
1614 connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
1615 // value : maxValue = posit ion : duration
1616 qint64 ms = (value * media->duration()) / seekSlider->maximum();
1617 qDebug() << "Seeking to" << ms;
1619 if (media->state() == Media::PausedState) media->play();
1621 connect(seekSlider, &QSlider::sliderPressed, this, [this]() {
1622 // value : maxValue = position : duration
1623 qint64 ms = (seekSlider->value() * media->duration()) / seekSlider->maximum();
1625 if (media->state() == Media::PausedState) media->play();
1627 connect(media, &Media::started, this, [this]() { seekSlider->setValue(0); });
1629 connect(media, &Media::volumeChanged, this, &MainWindow::volumeChanged);
1630 connect(media, &Media::volumeMutedChanged, this, &MainWindow::volumeMutedChanged);
1631 connect(volumeSlider, &QSlider::sliderMoved, this, [this](int value) {
1632 qreal volume = (qreal)value / volumeSlider->maximum();
1633 media->setVolume(volume);
1635 connect(volumeSlider, &QSlider::sliderPressed, this, [this]() {
1636 qreal volume = (qreal)volumeSlider->value() / volumeSlider->maximum();
1637 media->setVolume(volume);
1640 mediaView->setMedia(media);
1643 void MainWindow::tick(qint64 time) {
1645 bool isDown = seekSlider->property("down").isValid();
1647 bool isDown = seekSlider->isSliderDown();
1649 if (!isDown && media->state() == Media::PlayingState) {
1650 // value : maxValue = position : duration
1651 qint64 duration = media->duration();
1652 if (duration <= 0) return;
1653 int value = (seekSlider->maximum() * media->position()) / duration;
1654 seekSlider->setValue(value);
1657 const QString s = formatTime(time);
1658 if (s != currentTimeLabel->text()) {
1659 currentTimeLabel->setText(s);
1660 emit currentTimeChanged(s);
1663 const qint64 remainingTime = media->remainingTime();
1664 currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1668 QString MainWindow::formatTime(qint64 duration) {
1671 int seconds = (int)(duration % 60);
1673 int minutes = (int)(duration % 60);
1675 int hours = (int)(duration % 24);
1676 if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
1677 return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
1680 void MainWindow::volumeUp() {
1681 qreal newVolume = media->volume() + .1;
1682 if (newVolume > 1.) newVolume = 1.;
1683 media->setVolume(newVolume);
1686 void MainWindow::volumeDown() {
1687 qreal newVolume = media->volume() - .1;
1688 if (newVolume < 0) newVolume = 0;
1689 media->setVolume(newVolume);
1692 void MainWindow::toggleVolumeMute() {
1693 bool muted = media->volumeMuted();
1694 media->setVolumeMuted(!muted);
1697 void MainWindow::volumeChanged(qreal newVolume) {
1698 // automatically unmute when volume changes
1699 if (media->volumeMuted()) media->setVolumeMuted(false);
1700 showMessage(tr("Volume at %1%").arg((int)(newVolume * 100)));
1701 // newVolume : 1.0 = x : 1000
1702 int value = newVolume * volumeSlider->maximum();
1703 volumeSlider->blockSignals(true);
1704 volumeSlider->setValue(value);
1705 volumeSlider->blockSignals(false);
1708 void MainWindow::volumeMutedChanged(bool muted) {
1710 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1711 showMessage(tr("Volume is muted"));
1713 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1714 showMessage(tr("Volume is unmuted"));
1718 void MainWindow::setDefinitionMode(const QString &definitionName) {
1719 QAction *definitionAct = getAction("definition");
1720 definitionAct->setText(definitionName);
1721 definitionAct->setStatusTip(
1722 tr("Maximum video definition set to %1").arg(definitionAct->text()) + " (" +
1723 definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1724 showMessage(definitionAct->statusTip());
1725 YT3::instance().setMaxVideoDefinition(definitionName);
1726 if (views->currentWidget() == mediaView) {
1727 mediaView->reloadCurrentVideo();
1731 void MainWindow::toggleDefinitionMode() {
1732 const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
1733 const VideoDefinition ¤tDefinition = YT3::instance().maxVideoDefinition();
1735 int index = definitions.indexOf(currentDefinition);
1736 if (index != definitions.size() - 1) {
1741 setDefinitionMode(definitions.at(index).getName());
1744 void MainWindow::clearRecentKeywords() {
1746 settings.remove("recentKeywords");
1747 settings.remove("recentChannels");
1748 if (views->currentWidget() == homeView) {
1749 SearchView *searchView = homeView->getSearchView();
1750 searchView->updateRecentKeywords();
1751 searchView->updateRecentChannels();
1753 HttpUtils::clearCaches();
1754 showMessage(tr("Your privacy is now safe"));
1757 void MainWindow::setManualPlay(bool enabled) {
1759 settings.setValue("manualplay", QVariant::fromValue(enabled));
1760 if (views->currentWidget() == homeView &&
1761 homeView->currentWidget() == homeView->getSearchView())
1763 showActionsInStatusBar({getAction("manualplay")}, enabled);
1766 void MainWindow::updateDownloadMessage(const QString &message) {
1767 getAction("downloads")->setText(message);
1770 void MainWindow::downloadsFinished() {
1771 getAction("downloads")->setText(tr("&Downloads"));
1772 showMessage(tr("Downloads complete"));
1775 void MainWindow::toggleDownloads(bool show) {
1777 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1778 getAction("downloads")
1779 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1780 << QKeySequence(Qt::Key_Escape));
1782 getAction("downloads")
1783 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1784 stopAct->setShortcuts(QList<QKeySequence>()
1785 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1788 if (!downloadView) {
1789 downloadView = new DownloadView(this);
1790 views->addWidget(downloadView);
1793 showView(downloadView);
1798 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1799 search(suggestion->value);
1802 void MainWindow::search(const QString &query) {
1803 QString q = query.simplified();
1804 if (q.isEmpty()) return;
1805 SearchParams *searchParams = new SearchParams();
1806 searchParams->setKeywords(q);
1807 showMedia(searchParams);
1810 void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
1811 if (e->mimeData()->hasFormat("text/uri-list")) {
1812 QList<QUrl> urls = e->mimeData()->urls();
1813 if (urls.isEmpty()) return;
1814 const QUrl &url = urls.at(0);
1815 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1816 if (!videoId.isEmpty()) e->acceptProposedAction();
1820 void MainWindow::dropEvent(QDropEvent *e) {
1821 if (!toolbarSearch->isEnabled()) return;
1823 QList<QUrl> urls = e->mimeData()->urls();
1824 if (urls.isEmpty()) return;
1825 const QUrl &url = urls.at(0);
1826 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1827 if (!videoId.isEmpty()) {
1828 setWindowTitle(url.toString());
1829 SearchParams *searchParams = new SearchParams();
1830 searchParams->setKeywords(videoId);
1831 showMedia(searchParams);
1835 bool MainWindow::needStatusBar() {
1836 return !statusToolBar->actions().isEmpty();
1839 void MainWindow::adjustMessageLabelPosition() {
1840 if (messageLabel->parent() == this)
1841 messageLabel->move(0, height() - messageLabel->height());
1843 messageLabel->move(mapToGlobal(QPoint(0, height() - messageLabel->height())));
1846 void MainWindow::floatOnTop(bool onTop, bool showAction) {
1847 if (showAction) showActionsInStatusBar({getAction("ontop")}, onTop);
1849 mac::floatOnTop(winId(), onTop);
1852 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1855 setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1861 void MainWindow::restore() {
1863 mac::uncloseWindow(winId());
1867 void MainWindow::messageReceived(const QString &message) {
1868 if (message == QLatin1String("--toggle-playing")) {
1869 if (pauseAct->isEnabled()) pauseAct->trigger();
1870 } else if (message == QLatin1String("--next")) {
1871 if (skipAct->isEnabled()) skipAct->trigger();
1872 } else if (message == QLatin1String("--previous")) {
1873 if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1874 } else if (message == QLatin1String("--stop-after-this")) {
1875 getAction("stopafterthis")->toggle();
1876 } else if (message.startsWith("--")) {
1877 MainWindow::printHelp();
1878 } else if (!message.isEmpty()) {
1879 SearchParams *searchParams = new SearchParams();
1880 searchParams->setKeywords(message);
1881 showMedia(searchParams);
1885 void MainWindow::hideFullscreenUI() {
1886 if (views->currentWidget() != mediaView) return;
1887 setCursor(Qt::BlankCursor);
1889 QPoint p = mapFromGlobal(QCursor::pos());
1890 const int x = p.x();
1892 if (x > mediaView->getSidebar()->width()) mediaView->setSidebarVisibility(false);
1895 const int y = p.y();
1896 bool shouldHideToolbar = !toolbarSearch->hasFocus() && y > mainToolBar->height();
1897 if (shouldHideToolbar) mainToolBar->setVisible(false);
1901 void MainWindow::toggleMenuVisibility() {
1902 bool show = !menuBar()->isVisible();
1903 menuBar()->setVisible(show);
1906 void MainWindow::toggleMenuVisibilityWithMessage() {
1907 bool show = !menuBar()->isVisible();
1908 menuBar()->setVisible(show);
1910 QMessageBox msgBox(this);
1911 msgBox.setText(tr("You can still access the menu bar by pressing the ALT key"));
1912 msgBox.setModal(true);
1913 msgBox.setWindowModality(Qt::WindowModal);
1918 #ifdef APP_MAC_STORE
1919 void MainWindow::rateOnAppStore() {
1920 QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1921 "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1922 "?id=422006190&type=Purple+Software"));
1926 void MainWindow::printHelp() {
1927 QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1928 msg += "Usage: minitube [options]\n";
1929 msg += "Options:\n";
1930 msg += " --toggle-playing\t";
1931 msg += "Start or pause playback.\n";
1932 msg += " --next\t\t";
1933 msg += "Skip to the next video.\n";
1934 msg += " --previous\t\t";
1935 msg += "Go back to the previous video.\n";
1936 msg += " --stop-after-this\t";
1937 msg += "Stop playback at the end of the video.\n";
1938 std::cout << msg.toLocal8Bit().data();
1941 void MainWindow::setupAction(QAction *action) {
1942 // never autorepeat.
1943 // unexperienced users tend to keep keys pressed for a "long" time
1944 action->setAutoRepeat(false);
1946 // show keyboard shortcuts in the status bar
1947 if (!action->shortcut().isEmpty())
1948 action->setStatusTip(action->statusTip() + QLatin1String(" (") +
1949 action->shortcut().toString(QKeySequence::NativeText) +
1950 QLatin1String(")"));
1953 QAction *MainWindow::getAction(const char *name) {
1954 return actionMap.value(QByteArray::fromRawData(name, strlen(name)));
1957 void MainWindow::addNamedAction(const QByteArray &name, QAction *action) {
1958 actionMap.insert(name, action);
1961 QMenu *MainWindow::getMenu(const char *name) {
1962 return menuMap.value(QByteArray::fromRawData(name, strlen(name)));
1965 void MainWindow::showMessage(const QString &message) {
1966 if (!isVisible()) return;
1968 if (!mac::isVisible(winId())) return;
1970 if (statusBar()->isVisible())
1971 statusBar()->showMessage(message, 60000);
1972 else if (isActiveWindow()) {
1973 messageLabel->setText(message);
1974 QSize size = messageLabel->sizeHint();
1975 // round width to avoid flicker with fast changing messages (e.g. volume
1977 int w = size.width() + 10;
1978 const int multiple = 15;
1979 w = w + multiple / 2;
1982 messageLabel->resize(size);
1983 if (messageLabel->isHidden()) {
1984 adjustMessageLabelPosition();
1985 messageLabel->show();
1987 messageTimer->start();
1991 void MainWindow::hideMessage() {
1992 if (messageLabel->isVisible()) {
1993 messageLabel->hide();
1994 messageLabel->clear();
1998 void MainWindow::handleError(const QString &message) {
1999 qWarning() << message;
2000 showMessage(message);
2003 #ifdef APP_ACTIVATION
2004 void MainWindow::showActivationView() {
2005 View *activationView = ActivationView::instance();
2006 views->addWidget(activationView);
2007 if (views->currentWidget() != activationView) showView(activationView);
2011 void MainWindow::showRegionsView() {
2013 regionsView = new RegionsView(this);
2014 connect(regionsView, SIGNAL(regionChanged()), homeView->getStandardFeedsView(),
2016 views->addWidget(regionsView);
2018 showView(regionsView);