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().initialize(QUrl(QLatin1String(Constants::WEBSITE) + "-ws/bundle2.js"));
175 // JS::instance().initialize(QUrl("http://localhost:8000/bundle-test.js"));
176 Invidious::instance().initServers();
179 connect(JsFunctions::instance(), &JsFunctions::ready, this, [] {
180 auto ua = JsFunctions::instance()->string("userAgent()").toUtf8();
181 JS::instance().getNamFactory().setRequestHeaders({{"User-Agent", ua}});
184 QTimer::singleShot(100, this, &MainWindow::lazyInit);
187 void MainWindow::lazyInit() {
188 mediaView->initialize();
190 qApp->processEvents();
193 if (qApp->arguments().size() > 1) {
194 QString query = qApp->arguments().at(1);
195 if (query.startsWith(QLatin1String("--"))) {
196 messageReceived(query);
199 SearchParams *searchParams = new SearchParams();
200 searchParams->setKeywords(query);
201 showMedia(searchParams);
204 showMessage(tr("Make yourself comfortable"));
207 GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
209 if (GnomeGlobalShortcutBackend::IsGsdAvailable())
210 shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
215 connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
216 connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
217 connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
218 connect(&shortcuts, SIGNAL(Previous()), skipBackwardAct, SLOT(trigger()));
219 // connect(&shortcuts, SIGNAL(StopAfter()), getAction("stopafterthis"), SLOT(toggle()));
221 connect(DownloadManager::instance(), SIGNAL(statusMessageChanged(QString)),
222 SLOT(updateDownloadMessage(QString)));
223 connect(DownloadManager::instance(), SIGNAL(finished()), SLOT(downloadsFinished()));
225 setAcceptDrops(true);
227 fullscreenTimer = new QTimer(this);
228 fullscreenTimer->setInterval(3000);
229 fullscreenTimer->setSingleShot(true);
230 connect(fullscreenTimer, SIGNAL(timeout()), SLOT(hideFullscreenUI()));
232 // Hack to give focus to searchlineedit
233 View *view = qobject_cast<View *>(views->currentWidget());
234 if (view == homeView) {
235 QMetaObject::invokeMethod(views->currentWidget(), "appear");
236 const QString &desc = view->getDescription();
237 if (!desc.isEmpty()) showMessage(desc);
240 ChannelAggregator::instance()->start();
243 Updater::instance().checkWithoutUI();
249 void MainWindow::changeEvent(QEvent *e) {
251 if (e->type() == QEvent::WindowStateChange) {
252 getAction("minimize")->setEnabled(!isMinimized());
255 if (messageLabel->isVisible()) {
256 if (e->type() == QEvent::ActivationChange || e->type() == QEvent::WindowStateChange ||
257 e->type() == QEvent::WindowDeactivate || e->type() == QEvent::ApplicationStateChange) {
261 QMainWindow::changeEvent(e);
264 bool MainWindow::eventFilter(QObject *obj, QEvent *e) {
265 const QEvent::Type t = e->type();
268 static bool altPressed = false;
269 if (t == QEvent::KeyRelease && altPressed) {
271 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
272 if (ke->key() == Qt::Key_Alt) {
273 toggleMenuVisibility();
276 } else if (t == QEvent::KeyPress) {
277 QKeyEvent *ke = static_cast<QKeyEvent *>(e);
278 altPressed = ke->key() == Qt::Key_Alt;
282 if (fullScreenActive && views->currentWidget() == mediaView && t == QEvent::MouseMove &&
283 obj->isWidgetType() && qobject_cast<QWidget *>(obj)->window() == this) {
284 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
286 bool toolBarVisible = mainToolBar && mainToolBar->isVisible();
287 bool sidebarVisible = mediaView->isSidebarVisible();
289 if (!sidebarVisible && !toolBarVisible) {
290 const int x = mouseEvent->pos().x();
291 if (x >= 0 && x < 5) {
293 SidebarWidget *sidebar = mediaView->getSidebar();
294 sidebar->resize(sidebar->width(), height());
296 mediaView->setSidebarVisibility(true);
297 sidebarVisible = true;
302 if (!toolBarVisible && !sidebarVisible) {
303 const int y = mouseEvent->pos().y();
304 if (y >= 0 && y < 5) {
305 mainToolBar->resize(width(), mainToolBar->sizeHint().height());
306 mainToolBar->setVisible(true);
311 // show the normal cursor
313 // then hide it again after a few seconds
314 fullscreenTimer->start();
317 if (t == QEvent::ToolTip) {
322 if (t == QEvent::Show && obj == toolbarMenu) {
324 int x = width() - toolbarMenu->sizeHint().width();
327 int x = toolbarMenuButton->x() + toolbarMenuButton->width() -
328 toolbarMenu->sizeHint().width();
329 int y = toolbarMenuButton->y() + toolbarMenuButton->height();
332 toolbarMenu->move(mapToGlobal(p));
335 if (obj == this && t == QEvent::StyleChange) {
336 qDebug() << "Style change detected";
337 qApp->paletteChanged(qApp->palette());
341 // standard event processing
342 return QMainWindow::eventFilter(obj, e);
345 void MainWindow::createActions() {
346 stopAct = new QAction(tr("&Stop"), this);
347 IconUtils::setIcon(stopAct, "media-playback-stop");
348 stopAct->setStatusTip(tr("Stop playback and go back to the search view"));
349 stopAct->setShortcuts(QList<QKeySequence>()
350 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
351 stopAct->setEnabled(false);
352 actionMap.insert("stop", stopAct);
353 connect(stopAct, SIGNAL(triggered()), SLOT(stop()));
355 skipBackwardAct = new QAction(tr("P&revious"), this);
356 skipBackwardAct->setStatusTip(tr("Go back to the previous track"));
357 skipBackwardAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
358 skipBackwardAct->setEnabled(false);
359 actionMap.insert("previous", skipBackwardAct);
360 connect(skipBackwardAct, SIGNAL(triggered()), mediaView, SLOT(skipBackward()));
362 skipAct = new QAction(tr("S&kip"), this);
363 IconUtils::setIcon(skipAct, "media-skip-forward");
364 skipAct->setStatusTip(tr("Skip to the next video"));
365 skipAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Right)
366 << QKeySequence(Qt::Key_MediaNext));
367 skipAct->setEnabled(false);
368 actionMap.insert("skip", skipAct);
369 connect(skipAct, SIGNAL(triggered()), mediaView, SLOT(skip()));
371 pauseAct = new QAction(tr("&Play"), this);
372 IconUtils::setIcon(pauseAct, "media-playback-start");
373 pauseAct->setStatusTip(tr("Resume playback"));
374 pauseAct->setShortcuts(QList<QKeySequence>()
375 << QKeySequence(Qt::Key_Space) << QKeySequence(Qt::Key_MediaPlay));
376 pauseAct->setEnabled(false);
377 actionMap.insert("pause", pauseAct);
378 connect(pauseAct, SIGNAL(triggered()), mediaView, SLOT(pause()));
380 fullscreenAct = new QAction(tr("&Full Screen"), this);
381 IconUtils::setIcon(fullscreenAct, "view-fullscreen");
382 fullscreenAct->setStatusTip(tr("Go full screen"));
383 QList<QKeySequence> fsShortcuts;
385 fsShortcuts << QKeySequence(Qt::CTRL + Qt::META + Qt::Key_F);
387 fsShortcuts << QKeySequence(Qt::Key_F11) << QKeySequence(Qt::ALT + Qt::Key_Return);
389 fullscreenAct->setShortcuts(fsShortcuts);
390 fullscreenAct->setShortcutContext(Qt::ApplicationShortcut);
391 fullscreenAct->setPriority(QAction::LowPriority);
392 actionMap.insert("fullscreen", fullscreenAct);
393 connect(fullscreenAct, SIGNAL(triggered()), SLOT(toggleFullscreen()));
395 compactViewAct = new QAction(tr("&Compact Mode"), this);
396 compactViewAct->setStatusTip(tr("Hide the playlist and the toolbar"));
397 compactViewAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
398 compactViewAct->setCheckable(true);
399 compactViewAct->setChecked(false);
400 compactViewAct->setEnabled(false);
401 actionMap.insert("compactView", compactViewAct);
402 connect(compactViewAct, SIGNAL(toggled(bool)), this, SLOT(compactView(bool)));
404 webPageAct = new QAction(tr("Open the &YouTube Page"), this);
405 webPageAct->setStatusTip(tr("Go to the YouTube video page and pause playback"));
406 webPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y));
407 webPageAct->setEnabled(false);
408 actionMap.insert("webpage", webPageAct);
409 connect(webPageAct, SIGNAL(triggered()), mediaView, SLOT(openWebPage()));
411 copyPageAct = new QAction(tr("Copy the YouTube &Link"), this);
412 IconUtils::setIcon(copyPageAct, "link");
413 copyPageAct->setStatusTip(tr("Copy the current video YouTube link to the clipboard"));
414 copyPageAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
415 copyPageAct->setEnabled(false);
416 actionMap.insert("pagelink", copyPageAct);
417 connect(copyPageAct, SIGNAL(triggered()), mediaView, SLOT(copyWebPage()));
419 copyLinkAct = new QAction(tr("Copy the Video Stream &URL"), this);
420 copyLinkAct->setStatusTip(tr("Copy the current video stream URL to the clipboard"));
421 copyLinkAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
422 copyLinkAct->setEnabled(false);
423 actionMap.insert("videolink", copyLinkAct);
424 connect(copyLinkAct, SIGNAL(triggered()), mediaView, SLOT(copyVideoLink()));
426 findVideoPartsAct = new QAction(tr("Find Video &Parts"), this);
427 findVideoPartsAct->setStatusTip(tr("Find other video parts hopefully in the right order"));
428 findVideoPartsAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
429 findVideoPartsAct->setEnabled(false);
430 connect(findVideoPartsAct, SIGNAL(triggered()), mediaView, SLOT(findVideoParts()));
431 actionMap.insert("findVideoParts", findVideoPartsAct);
433 removeAct = new QAction(tr("&Remove"), this);
434 removeAct->setStatusTip(tr("Remove the selected videos from the playlist"));
435 removeAct->setShortcuts(QList<QKeySequence>()
436 << QKeySequence("Del") << QKeySequence("Backspace"));
437 removeAct->setEnabled(false);
438 actionMap.insert("remove", removeAct);
439 connect(removeAct, SIGNAL(triggered()), mediaView, SLOT(removeSelected()));
441 moveUpAct = new QAction(tr("Move &Up"), this);
442 moveUpAct->setStatusTip(tr("Move up the selected videos in the playlist"));
443 moveUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
444 moveUpAct->setEnabled(false);
445 actionMap.insert("moveUp", moveUpAct);
446 connect(moveUpAct, SIGNAL(triggered()), mediaView, SLOT(moveUpSelected()));
448 moveDownAct = new QAction(tr("Move &Down"), this);
449 moveDownAct->setStatusTip(tr("Move down the selected videos in the playlist"));
450 moveDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
451 moveDownAct->setEnabled(false);
452 actionMap.insert("moveDown", moveDownAct);
453 connect(moveDownAct, SIGNAL(triggered()), mediaView, SLOT(moveDownSelected()));
455 clearAct = new QAction(tr("&Clear Recent Searches"), this);
456 clearAct->setMenuRole(QAction::ApplicationSpecificRole);
457 clearAct->setShortcuts(QList<QKeySequence>()
458 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Delete)
459 << QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Backspace));
460 clearAct->setStatusTip(tr("Clear the search history. Cannot be undone."));
461 clearAct->setEnabled(true);
462 actionMap.insert("clearRecentKeywords", clearAct);
463 connect(clearAct, SIGNAL(triggered()), SLOT(clearRecentKeywords()));
465 quitAct = new QAction(tr("&Quit"), this);
466 quitAct->setMenuRole(QAction::QuitRole);
467 quitAct->setShortcut(QKeySequence(QKeySequence::Quit));
468 quitAct->setStatusTip(tr("Bye"));
469 actionMap.insert("quit", quitAct);
470 connect(quitAct, SIGNAL(triggered()), SLOT(quit()));
472 siteAct = new QAction(tr("&Website"), this);
473 siteAct->setShortcut(QKeySequence::HelpContents);
474 siteAct->setStatusTip(tr("%1 on the Web").arg(Constants::NAME));
475 actionMap.insert("site", siteAct);
476 connect(siteAct, SIGNAL(triggered()), this, SLOT(visitSite()));
478 #if !defined(APP_MAC) && !defined(APP_WIN)
479 donateAct = new QAction(tr("Make a &Donation"), this);
480 donateAct->setStatusTip(
481 tr("Please support the continued development of %1").arg(Constants::NAME));
482 actionMap.insert("donate", donateAct);
483 connect(donateAct, SIGNAL(triggered()), this, SLOT(donate()));
486 aboutAct = new QAction(tr("&About"), this);
487 aboutAct->setMenuRole(QAction::AboutRole);
488 aboutAct->setStatusTip(tr("Info about %1").arg(Constants::NAME));
489 actionMap.insert("about", aboutAct);
490 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
494 searchFocusAct = new QAction(this);
495 searchFocusAct->setShortcut(QKeySequence::Find);
496 searchFocusAct->setStatusTip(tr("Search"));
497 actionMap.insert("search", searchFocusAct);
498 connect(searchFocusAct, SIGNAL(triggered()), this, SLOT(searchFocus()));
499 addAction(searchFocusAct);
501 volumeUpAct = new QAction(this);
502 volumeUpAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Plus));
503 actionMap.insert("volumeUp", volumeUpAct);
504 connect(volumeUpAct, SIGNAL(triggered()), this, SLOT(volumeUp()));
505 addAction(volumeUpAct);
507 volumeDownAct = new QAction(this);
508 volumeDownAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_Minus));
509 actionMap.insert("volumeDown", volumeDownAct);
510 connect(volumeDownAct, SIGNAL(triggered()), this, SLOT(volumeDown()));
511 addAction(volumeDownAct);
513 volumeMuteAct = new QAction(this);
514 IconUtils::setIcon(volumeMuteAct, "audio-volume-high");
515 volumeMuteAct->setStatusTip(tr("Mute volume"));
516 volumeMuteAct->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_M));
517 actionMap.insert("volumeMute", volumeMuteAct);
518 connect(volumeMuteAct, SIGNAL(triggered()), SLOT(toggleVolumeMute()));
519 addAction(volumeMuteAct);
521 QToolButton *definitionButton = new QToolButton(this);
522 definitionButton->setText(YT3::instance().maxVideoDefinition().getName());
523 IconUtils::setIcon(definitionButton, "video-display");
524 definitionButton->setIconSize(QSize(16, 16));
525 definitionButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
526 definitionButton->setPopupMode(QToolButton::InstantPopup);
527 QMenu *definitionMenu = new QMenu(this);
528 QActionGroup *group = new QActionGroup(this);
529 for (auto &defName : VideoDefinition::getDefinitionNames()) {
530 QAction *a = new QAction(defName);
531 a->setCheckable(true);
532 a->setActionGroup(group);
533 a->setChecked(defName == YT3::instance().maxVideoDefinition().getName());
534 connect(a, &QAction::triggered, this, [this, defName, definitionButton] {
535 setDefinitionMode(defName);
536 definitionButton->setText(defName);
538 connect(&YT3::instance(), &YT3::maxVideoDefinitionChanged, this,
539 [defName, definitionButton](const QString &name) {
540 if (defName == name) definitionButton->setChecked(true);
542 definitionMenu->addAction(a);
544 definitionButton->setMenu(definitionMenu);
545 QWidgetAction *definitionAct = new QWidgetAction(this);
546 definitionAct->setDefaultWidget(definitionButton);
547 definitionAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_D));
548 actionMap.insert("definition", definitionAct);
549 addAction(definitionAct);
553 action = new QAction(tr("&Manually Start Playing"), this);
554 IconUtils::setIcon(action, "media-playback-start");
555 action->setStatusTip(tr("Manually start playing videos"));
556 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_T));
557 action->setCheckable(true);
558 connect(action, SIGNAL(toggled(bool)), SLOT(setManualPlay(bool)));
559 actionMap.insert("manualplay", action);
561 action = new QAction(tr("&Downloads"), this);
562 IconUtils::setIcon(action, "document-save");
563 action->setStatusTip(tr("Show details about video downloads"));
564 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
565 action->setCheckable(true);
566 connect(action, SIGNAL(toggled(bool)), SLOT(toggleDownloads(bool)));
567 actionMap.insert("downloads", action);
569 action = new QAction(tr("&Download"), this);
570 IconUtils::setIcon(action, "document-save");
571 action->setStatusTip(tr("Download the current video"));
572 action->setShortcut(QKeySequence::Save);
573 action->setEnabled(false);
574 action->setVisible(false);
575 action->setPriority(QAction::LowPriority);
576 connect(action, SIGNAL(triggered()), mediaView, SLOT(downloadVideo()));
577 actionMap.insert("download", action);
580 action = new QAction(tr("Take &Snapshot"), this);
581 action->setShortcut(QKeySequence(Qt::Key_F9));
582 action->setEnabled(false);
583 actionMap.insert("snapshot", action);
584 connect(action, SIGNAL(triggered()), mediaView, SLOT(snapshot()));
587 action = new QAction(tr("&Subscribe to Channel"), this);
588 action->setProperty("originalText", action->text());
589 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
590 action->setEnabled(false);
591 connect(action, SIGNAL(triggered()), mediaView, SLOT(toggleSubscription()));
592 actionMap.insert("subscribeChannel", action);
593 mediaView->updateSubscriptionActionForVideo(0, false);
595 QString shareTip = tr("Share the current video using %1");
597 action = new QAction("&Twitter", this);
598 IconUtils::setIcon(action, "twitter");
599 action->setStatusTip(shareTip.arg("Twitter"));
600 action->setEnabled(false);
601 actionMap.insert("twitter", action);
602 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaTwitter()));
604 action = new QAction("&Facebook", this);
605 IconUtils::setIcon(action, "facebook");
606 action->setStatusTip(shareTip.arg("Facebook"));
607 action->setEnabled(false);
608 actionMap.insert("facebook", action);
609 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaFacebook()));
611 action = new QAction(tr("&Email"), this);
612 IconUtils::setIcon(action, "email");
613 action->setStatusTip(shareTip.arg(tr("Email")));
614 action->setEnabled(false);
615 actionMap.insert("email", action);
616 connect(action, SIGNAL(triggered()), mediaView, SLOT(shareViaEmail()));
618 action = new QAction(tr("&Close"), this);
619 action->setShortcut(QKeySequence(QKeySequence::Close));
620 actionMap.insert("close", action);
621 connect(action, SIGNAL(triggered()), SLOT(close()));
623 action = new QAction(Constants::NAME, this);
624 action->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_1));
625 actionMap.insert("restore", action);
626 connect(action, SIGNAL(triggered()), SLOT(restore()));
628 action = new QAction(tr("&Float on Top"), this);
629 IconUtils::setIcon(action, "go-top");
630 action->setCheckable(true);
631 actionMap.insert("ontop", action);
632 connect(action, SIGNAL(toggled(bool)), SLOT(floatOnTop(bool)));
634 action = new QAction(tr("&Stop After This Video"), this);
635 IconUtils::setIcon(action, "media-playback-stop");
636 action->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Escape));
637 action->setCheckable(true);
638 action->setEnabled(false);
639 actionMap.insert("stopafterthis", action);
640 connect(action, SIGNAL(toggled(bool)), SLOT(showStopAfterThisInStatusBar(bool)));
642 action = new QAction(tr("&Report an Issue..."), this);
643 actionMap.insert("reportIssue", action);
644 connect(action, SIGNAL(triggered()), SLOT(reportIssue()));
646 action = new QAction(tr("&Refine Search..."), this);
647 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
648 action->setCheckable(true);
649 action->setEnabled(false);
650 actionMap.insert("refineSearch", action);
652 action = new QAction(YTRegions::defaultRegion().name, this);
653 actionMap.insert("worldwideRegion", action);
655 action = new QAction(YTRegions::localRegion().name, this);
656 actionMap.insert("localRegion", action);
658 action = new QAction(tr("More..."), this);
659 actionMap.insert("moreRegion", action);
661 action = new QAction(tr("&Related Videos"), this);
662 IconUtils::setIcon(action, "view-list");
663 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
664 action->setStatusTip(tr("Watch videos related to the current one"));
665 action->setEnabled(false);
666 action->setPriority(QAction::LowPriority);
667 connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
668 actionMap.insert("relatedVideos", action);
670 action = new QAction(tr("Open in &Browser..."), this);
671 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));
672 action->setEnabled(false);
673 actionMap.insert("openInBrowser", action);
674 connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
676 action = new QAction(tr("Restricted Mode"), this);
677 IconUtils::setIcon(action, "safesearch");
678 action->setStatusTip(tr("Hide videos that may contain inappropriate content"));
679 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K));
680 action->setCheckable(true);
681 action->setVisible(VideoAPI::impl() != VideoAPI::IV);
682 actionMap.insert("safeSearch", action);
684 action = new QAction(tr("Toggle &Menu Bar"), this);
685 connect(action, SIGNAL(triggered()), SLOT(toggleMenuVisibilityWithMessage()));
686 actionMap.insert("toggleMenu", action);
688 action = new QAction(tr("Menu"), this);
689 IconUtils::setIcon(action, "open-menu");
690 connect(action, SIGNAL(triggered()), SLOT(toggleToolbarMenu()));
691 actionMap.insert("toolbarMenu", action);
693 action = new QAction(tr("Import Subscriptions..."), this);
694 action->setMenuRole(QAction::ApplicationSpecificRole);
695 connect(action, &QAction::triggered, this, [this] {
696 if (!subscriptionImportView) {
697 subscriptionImportView = new SubscriptionImportView(this);
698 views->addWidget(subscriptionImportView);
700 showView(subscriptionImportView);
702 actionMap.insert("importSubscriptions", action);
705 action = new QAction(tr("&Love %1? Rate it!").arg(Constants::NAME), this);
706 actionMap.insert("appStore", action);
707 connect(action, SIGNAL(triggered()), SLOT(rateOnAppStore()));
710 #ifdef APP_ACTIVATION
711 ActivationView::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
714 // common action properties
715 for (QAction *action : qAsConst(actionMap)) {
716 // add actions to the MainWindow so that they work
717 // when the menu is hidden
723 void MainWindow::createMenus() {
724 fileMenu = menuBar()->addMenu(tr("&Application"));
725 #ifdef APP_ACTIVATION
726 QAction *buyAction = getAction("buy");
727 if (buyAction) fileMenu->addAction(buyAction);
729 fileMenu->addSeparator();
732 fileMenu->addAction(clearAct);
734 fileMenu->addSeparator();
736 fileMenu->addAction(quitAct);
738 QMenu *playbackMenu = menuBar()->addMenu(tr("&Playback"));
739 menuMap.insert("playback", playbackMenu);
740 playbackMenu->addAction(pauseAct);
741 playbackMenu->addAction(stopAct);
742 playbackMenu->addAction(getAction("stopafterthis"));
743 playbackMenu->addSeparator();
744 playbackMenu->addAction(skipAct);
745 playbackMenu->addAction(skipBackwardAct);
746 playbackMenu->addSeparator();
747 playbackMenu->addAction(getAction("manualplay"));
749 MacSupport::dockMenu(playbackMenu);
752 playlistMenu = menuBar()->addMenu(tr("&Playlist"));
753 menuMap.insert("playlist", playlistMenu);
754 playlistMenu->addAction(removeAct);
755 playlistMenu->addSeparator();
756 playlistMenu->addAction(moveUpAct);
757 playlistMenu->addAction(moveDownAct);
758 playlistMenu->addSeparator();
759 playlistMenu->addAction(getAction("refineSearch"));
761 QMenu *videoMenu = menuBar()->addMenu(tr("&Video"));
762 menuMap.insert("video", videoMenu);
763 videoMenu->addAction(getAction("relatedVideos"));
764 videoMenu->addAction(findVideoPartsAct);
765 videoMenu->addSeparator();
766 videoMenu->addAction(getAction("subscribeChannel"));
768 videoMenu->addSeparator();
769 videoMenu->addAction(getAction("snapshot"));
771 videoMenu->addSeparator();
772 videoMenu->addAction(webPageAct);
773 videoMenu->addAction(copyLinkAct);
774 videoMenu->addAction(getAction("openInBrowser"));
775 videoMenu->addAction(getAction("download"));
777 QMenu *shareMenu = menuBar()->addMenu(tr("&Share"));
778 menuMap.insert("share", shareMenu);
779 shareMenu->addAction(copyPageAct);
780 shareMenu->addSeparator();
781 shareMenu->addAction(getAction("twitter"));
782 shareMenu->addAction(getAction("facebook"));
783 shareMenu->addSeparator();
784 shareMenu->addAction(getAction("email"));
786 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
787 menuMap.insert("view", viewMenu);
788 viewMenu->addAction(getAction("ontop"));
789 viewMenu->addAction(compactViewAct);
790 viewMenu->addSeparator();
791 viewMenu->addAction(fullscreenAct);
793 viewMenu->addSeparator();
794 viewMenu->addAction(getAction("toggleMenu"));
798 MacSupport::windowMenu(this);
801 helpMenu = menuBar()->addMenu(tr("&Help"));
802 menuMap.insert("help", helpMenu);
803 helpMenu->addAction(siteAct);
804 #if !defined(APP_MAC) && !defined(APP_WIN)
805 helpMenu->addAction(donateAct);
807 helpMenu->addAction(getAction("reportIssue"));
808 helpMenu->addAction(aboutAct);
810 helpMenu->addAction(Updater::instance().getAction());
814 helpMenu->addSeparator();
815 helpMenu->addAction(getAction("appStore"));
819 void MainWindow::createToolBar() {
821 currentTimeLabel = new QLabel("00:00", this);
823 seekSlider = new SeekSlider(this);
824 seekSlider->setEnabled(false);
825 seekSlider->setTracking(false);
826 seekSlider->setMaximum(1000);
827 volumeSlider = new SeekSlider(this);
828 volumeSlider->setValue(volumeSlider->maximum());
830 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
831 SearchWrapper *searchWrapper = new SearchWrapper(this);
832 toolbarSearch = searchWrapper->getSearchLineEdit();
834 toolbarSearch = new SearchLineEdit(this);
836 toolbarSearch->setMinimumWidth(toolbarSearch->fontInfo().pixelSize() * 15);
837 toolbarSearch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
838 toolbarSearch->setSuggester(new YTSuggester(this));
839 connect(toolbarSearch, SIGNAL(search(const QString &)), SLOT(search(const QString &)));
840 connect(toolbarSearch, SIGNAL(suggestionAccepted(Suggestion *)),
841 SLOT(suggestionAccepted(Suggestion *)));
842 toolbarSearch->setStatusTip(searchFocusAct->statusTip());
844 // Add widgets to toolbar
846 #ifdef APP_MAC_QMACTOOLBAR
847 currentTimeLabel->hide();
848 toolbarSearch->hide();
849 volumeSlider->hide();
851 MacToolbar::instance().createToolbar(this);
855 mainToolBar = new QToolBar(this);
856 mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
857 mainToolBar->setFloatable(false);
858 mainToolBar->setMovable(false);
860 mainToolBar->setIconSize(QSize(32, 32));
862 mainToolBar->addAction(stopAct);
863 QToolButton *stopToolButton =
864 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(stopAct));
865 if (stopToolButton) {
866 QMenu *stopMenu = new QMenu(this);
867 stopMenu->addAction(getAction("stopafterthis"));
868 stopToolButton->setMenu(stopMenu);
869 stopToolButton->setPopupMode(QToolButton::DelayedPopup);
871 mainToolBar->addAction(pauseAct);
872 mainToolBar->addAction(skipAct);
873 mainToolBar->addAction(getAction("relatedVideos"));
875 bool addFullScreenAct = true;
877 addFullScreenAct = !mac::CanGoFullScreen(winId());
879 if (addFullScreenAct) mainToolBar->addAction(fullscreenAct);
881 mainToolBar->addWidget(new Spacer());
883 currentTimeLabel->setFont(FontUtils::small());
884 currentTimeLabel->setMinimumWidth(currentTimeLabel->fontInfo().pixelSize() * 4);
885 currentTimeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
886 mainToolBar->addWidget(currentTimeLabel);
889 mainToolBar->addWidget(new Spacer(nullptr, 10));
892 seekSlider->setOrientation(Qt::Horizontal);
893 seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
894 seekSlider->setFocusPolicy(Qt::NoFocus);
895 mainToolBar->addWidget(seekSlider);
897 mainToolBar->addWidget(new Spacer());
899 mainToolBar->addAction(volumeMuteAct);
900 #ifndef APP_MAC_QMACTOOLBAR
901 QToolButton *volumeMuteButton =
902 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(volumeMuteAct));
903 volumeMuteButton->setIconSize(QSize(16, 16));
904 auto fixVolumeMuteIconSize = [volumeMuteButton] {
905 volumeMuteButton->setIcon(volumeMuteButton->icon().pixmap(16));
907 fixVolumeMuteIconSize();
908 volumeMuteButton->connect(volumeMuteAct, &QAction::changed, volumeMuteButton,
909 fixVolumeMuteIconSize);
912 volumeSlider->setStatusTip(
913 tr("Press %1 to raise the volume, %2 to lower it")
914 .arg(volumeUpAct->shortcut().toString(QKeySequence::NativeText),
915 volumeDownAct->shortcut().toString(QKeySequence::NativeText)));
917 volumeSlider->setOrientation(Qt::Horizontal);
918 // this makes the volume slider smaller
919 volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
920 volumeSlider->setFocusPolicy(Qt::NoFocus);
921 mainToolBar->addWidget(volumeSlider);
923 mainToolBar->addWidget(new Spacer());
925 #if defined(APP_MAC_SEARCHFIELD) && !defined(APP_MAC_QMACTOOLBAR)
926 mainToolBar->addWidget(searchWrapper);
928 mainToolBar->addWidget(toolbarSearch);
929 mainToolBar->addWidget(new Spacer(this, toolbarSearch->height() / 2));
931 QAction *toolbarMenuAction = getAction("toolbarMenu");
932 mainToolBar->addAction(toolbarMenuAction);
934 qobject_cast<QToolButton *>(mainToolBar->widgetForAction(toolbarMenuAction));
937 addToolBar(mainToolBar);
940 void MainWindow::createStatusBar() {
941 statusToolBar = new QToolBar(statusBar());
942 statusToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
943 statusToolBar->setIconSize(QSize(16, 16));
945 regionAction = new QAction(this);
946 regionAction->setStatusTip(tr("Choose your content location"));
948 QAction *localAction = getAction("localRegion");
949 if (!localAction->text().isEmpty()) {
950 QMenu *regionMenu = new QMenu(this);
951 regionMenu->addAction(getAction("worldwideRegion"));
952 regionMenu->addAction(localAction);
953 regionMenu->addSeparator();
954 QAction *moreRegionsAction = getAction("moreRegion");
955 regionMenu->addAction(moreRegionsAction);
956 connect(moreRegionsAction, SIGNAL(triggered()), SLOT(showRegionsView()));
957 regionAction->setMenu(regionMenu);
959 connect(regionAction, SIGNAL(triggered()), SLOT(showRegionsView()));
961 /* Stupid code that generates the QRC items
962 foreach(YTRegion r, YTRegions::list())
963 qDebug() << QString("<file>flags/%1.png</file>").arg(r.id.toLower());
966 statusBar()->addPermanentWidget(statusToolBar);
970 void MainWindow::showStopAfterThisInStatusBar(bool show) {
971 QAction *action = getAction("stopafterthis");
972 showActionsInStatusBar({action}, show);
975 void MainWindow::showActionsInStatusBar(const QVector<QAction *> &actions, bool show) {
977 Extra::fadeInWidget(statusBar(), statusBar());
979 for (auto action : actions) {
981 if (statusToolBar->actions().contains(action)) continue;
982 if (statusToolBar->actions().isEmpty()) {
983 statusToolBar->addAction(action);
985 statusToolBar->insertAction(statusToolBar->actions().at(0), action);
988 statusToolBar->removeAction(action);
993 if (statusBar()->isHidden() && !fullScreenActive) setStatusBarVisibility(true);
995 if (statusBar()->isVisible() && !needStatusBar()) setStatusBarVisibility(false);
999 void MainWindow::setStatusBarVisibility(bool show) {
1000 if (statusBar()->isVisible() != show) {
1001 statusBar()->setVisible(show);
1002 if (views->currentWidget() == mediaView)
1003 QTimer::singleShot(0, mediaView, SLOT(adjustWindowSize()));
1007 void MainWindow::adjustStatusBarVisibility() {
1008 setStatusBarVisibility(needStatusBar());
1011 void MainWindow::hideToolbar() {
1013 mac::showToolBar(winId(), false);
1015 mainToolBar->hide();
1019 void MainWindow::showToolbar() {
1021 mac::showToolBar(winId(), true);
1023 mainToolBar->show();
1027 void MainWindow::readSettings() {
1029 QByteArray geometrySettings = settings.value("geometry").toByteArray();
1030 if (!geometrySettings.isEmpty()) {
1031 restoreGeometry(geometrySettings);
1033 const QRect desktopSize = QGuiApplication::primaryScreen()->availableGeometry();
1034 int w = desktopSize.width() * .9;
1035 int h = qMin(w / 2, desktopSize.height());
1037 QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, QSize(w, h), desktopSize));
1039 setDefinitionMode(settings.value("definition", YT3::instance().maxVideoDefinition().getName())
1041 getAction("manualplay")->setChecked(settings.value("manualplay", false).toBool());
1042 getAction("safeSearch")->setChecked(settings.value("safeSearch", false).toBool());
1044 menuBar()->setVisible(settings.value("menuBar", false).toBool());
1048 void MainWindow::writeSettings() {
1051 if (!isReallyFullScreen()) {
1052 settings.setValue("geometry", saveGeometry());
1053 if (mediaView) mediaView->saveSplitterState();
1056 settings.setValue("manualplay", getAction("manualplay")->isChecked());
1057 settings.setValue("safeSearch", getAction("safeSearch")->isChecked());
1059 settings.setValue("menuBar", menuBar()->isVisible());
1063 void MainWindow::goBack() {
1064 if (history.size() > 1) {
1066 showView(history.pop());
1070 void MainWindow::showView(View *view, bool transition) {
1071 if (!history.isEmpty() && view == history.top()) {
1072 qDebug() << "Attempting to show same view" << view;
1077 if (transition && !history.isEmpty()) CompositeFader::go(this, this->grab());
1080 if (compactViewAct->isChecked()) compactViewAct->toggle();
1082 // call hide method on the current view
1083 View *oldView = qobject_cast<View *>(views->currentWidget());
1085 oldView->willDisappear();
1086 oldView->disappear();
1087 oldView->setEnabled(false);
1088 oldView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
1090 qDebug() << "Cannot cast old view";
1093 view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
1094 view->setEnabled(true);
1095 views->setCurrentWidget(view);
1098 if (oldView) oldView->didDisappear();
1100 QString title = view->getTitle();
1101 if (title.isEmpty())
1102 title = Constants::NAME;
1104 title += QLatin1String(" - ") + Constants::NAME;
1105 setWindowTitle(title);
1107 const bool isMediaView = view == mediaView;
1108 stopAct->setEnabled(isMediaView);
1109 compactViewAct->setEnabled(isMediaView);
1110 toolbarSearch->setEnabled(isMediaView);
1111 aboutAct->setEnabled(view != aboutView);
1112 getAction("downloads")->setChecked(view == downloadView);
1114 // dynamic view actions
1115 /* Not currently used by any view
1116 showActionsInStatusBar(viewActions, false);
1117 viewActions = newView->getViewActions();
1118 showActionsInStatusBar(viewActions, true);
1125 void MainWindow::about() {
1127 aboutView = new AboutView(this);
1128 views->addWidget(aboutView);
1130 showView(aboutView);
1133 void MainWindow::visitSite() {
1134 QUrl url(Constants::WEBSITE);
1135 showMessage(QString(tr("Opening %1").arg(url.toString())));
1136 QDesktopServices::openUrl(url);
1139 void MainWindow::donate() {
1140 QUrl url("https://" + QLatin1String(Constants::ORG_DOMAIN) + "/donate");
1141 showMessage(QString(tr("Opening %1").arg(url.toString())));
1142 QDesktopServices::openUrl(url);
1145 void MainWindow::reportIssue() {
1146 QUrl url("https://flavio.tordini.org/forums/forum/minitube-forums/minitube-troubleshooting");
1147 QDesktopServices::openUrl(url);
1150 void MainWindow::quit() {
1152 if (!confirmQuit()) {
1156 // do not save geometry when in full screen or in compact mode
1157 if (!fullScreenActive && !compactViewAct->isChecked()) {
1163 // mediaView->stop();
1164 Temporary::deleteAll();
1165 ChannelAggregator::instance()->stop();
1166 ChannelAggregator::instance()->cleanup();
1167 Database::shutdown();
1171 void MainWindow::closeEvent(QCloseEvent *e) {
1173 mac::closeWindow(winId());
1176 if (!confirmQuit()) {
1180 QWidget::closeEvent(e);
1183 messageLabel->hide();
1186 void MainWindow::showEvent(QShowEvent *e) {
1187 QWidget::showEvent(e);
1193 bool MainWindow::confirmQuit() {
1194 if (DownloadManager::instance()->activeItems() > 0) {
1195 QMessageBox msgBox(this);
1196 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1198 tr("Do you want to exit %1 with a download in progress?").arg(Constants::NAME));
1199 msgBox.setInformativeText(
1200 tr("If you close %1 now, this download will be cancelled.").arg(Constants::NAME));
1201 msgBox.setModal(true);
1202 // make it a "sheet" on the Mac
1203 msgBox.setWindowModality(Qt::WindowModal);
1205 msgBox.addButton(tr("Close and cancel download"), QMessageBox::RejectRole);
1206 QPushButton *waitButton =
1207 msgBox.addButton(tr("Wait for download to finish"), QMessageBox::ActionRole);
1211 if (msgBox.clickedButton() == waitButton) {
1218 void MainWindow::showHome() {
1220 currentTimeLabel->clear();
1221 seekSlider->setValue(0);
1224 void MainWindow::showMedia(SearchParams *searchParams) {
1225 showView(mediaView);
1226 if (getAction("safeSearch")->isChecked())
1227 searchParams->setSafeSearch(SearchParams::Strict);
1229 searchParams->setSafeSearch(SearchParams::None);
1230 mediaView->search(searchParams);
1233 void MainWindow::showMedia(VideoSource *videoSource) {
1234 showView(mediaView);
1235 mediaView->setVideoSource(videoSource);
1238 void MainWindow::stateChanged(Media::State newState) {
1239 qDebug() << newState;
1241 seekSlider->setEnabled(newState != Media::StoppedState);
1244 case Media::ErrorState:
1245 showMessage(tr("Error: %1").arg(media->errorString()));
1248 case Media::PlayingState:
1249 pauseAct->setEnabled(true);
1250 pauseAct->setIcon(IconUtils::icon("media-playback-pause"));
1251 pauseAct->setText(tr("&Pause"));
1252 pauseAct->setStatusTip(tr("Pause playback") + " (" +
1253 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1256 case Media::StoppedState:
1257 pauseAct->setEnabled(false);
1258 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1259 pauseAct->setText(tr("&Play"));
1260 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1261 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1264 case Media::PausedState:
1265 pauseAct->setEnabled(true);
1266 pauseAct->setIcon(IconUtils::icon("media-playback-start"));
1267 pauseAct->setText(tr("&Play"));
1268 pauseAct->setStatusTip(tr("Resume playback") + " (" +
1269 pauseAct->shortcut().toString(QKeySequence::NativeText) + ")");
1272 case Media::BufferingState:
1273 pauseAct->setEnabled(false);
1274 pauseAct->setIcon(IconUtils::icon("content-loading"));
1275 pauseAct->setText(tr("&Loading..."));
1276 pauseAct->setStatusTip(QString());
1279 case Media::LoadingState:
1280 pauseAct->setEnabled(false);
1281 currentTimeLabel->clear();
1288 void MainWindow::stop() {
1293 void MainWindow::resizeEvent(QResizeEvent *e) {
1296 if (initialized && mac::CanGoFullScreen(winId())) {
1297 bool isFullscreen = mac::IsFullScreen(winId());
1298 if (isFullscreen != fullScreenActive) {
1299 if (compactViewAct->isChecked()) {
1300 compactViewAct->setChecked(false);
1303 fullScreenActive = isFullscreen;
1304 updateUIForFullscreen();
1308 #ifdef APP_MAC_QMACTOOLBAR
1309 int moreButtonWidth = 40;
1310 toolbarSearch->move(width() - toolbarSearch->width() - moreButtonWidth - 7, -34);
1315 void MainWindow::enterEvent(QEvent *e) {
1318 // Workaround cursor bug on macOS
1323 void MainWindow::leaveEvent(QEvent *e) {
1325 if (fullScreenActive) hideFullscreenUI();
1328 void MainWindow::toggleFullscreen() {
1329 if (compactViewAct->isChecked()) compactViewAct->toggle();
1332 WId handle = winId();
1333 if (mac::CanGoFullScreen(handle)) {
1334 if (mainToolBar) mainToolBar->setVisible(true);
1335 mac::ToggleFullScreen(handle);
1340 fullScreenActive = !fullScreenActive;
1342 if (fullScreenActive) {
1343 // Enter full screen
1345 maximizedBeforeFullScreen = isMaximized();
1347 // save geometry now, if the user quits when in full screen
1348 // geometry won't be saved
1352 MacSupport::enterFullScreen(this, views);
1354 menuVisibleBeforeFullScreen = menuBar()->isVisible();
1356 if (mainToolBar) mainToolBar->hide();
1364 MacSupport::exitFullScreen(this, views);
1366 menuBar()->setVisible(menuVisibleBeforeFullScreen);
1367 if (mainToolBar) mainToolBar->setVisible(views->currentWidget() == mediaView);
1368 if (maximizedBeforeFullScreen)
1374 // Make sure the window has focus
1378 qApp->processEvents();
1379 updateUIForFullscreen();
1382 void MainWindow::updateUIForFullscreen() {
1383 static QList<QKeySequence> fsShortcuts;
1384 static QString fsText;
1386 if (fullScreenActive) {
1387 fsShortcuts = fullscreenAct->shortcuts();
1388 fsText = fullscreenAct->text();
1389 if (fsText.isEmpty()) qDebug() << "[taking Empty!]";
1390 fullscreenAct->setShortcuts(QList<QKeySequence>(fsShortcuts)
1391 << QKeySequence(Qt::Key_Escape));
1392 fullscreenAct->setText(tr("Leave &Full Screen"));
1393 fullscreenAct->setIcon(IconUtils::icon("view-restore"));
1394 setStatusBarVisibility(false);
1397 removeToolBar(mainToolBar);
1398 mainToolBar->move(0, 0);
1401 mediaView->removeSidebar();
1404 fullscreenAct->setShortcuts(fsShortcuts);
1405 if (fsText.isEmpty()) fsText = "[Empty!]";
1406 fullscreenAct->setText(fsText);
1407 fullscreenAct->setIcon(IconUtils::icon("view-fullscreen"));
1409 if (needStatusBar()) setStatusBarVisibility(true);
1412 addToolBar(mainToolBar);
1415 mediaView->restoreSidebar();
1418 // No compact view action when in full screen
1419 compactViewAct->setVisible(!fullScreenActive);
1420 compactViewAct->setChecked(false);
1422 // Hide anything but the video
1423 mediaView->setSidebarVisibility(!fullScreenActive);
1425 if (fullScreenActive) {
1426 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1428 stopAct->setShortcuts(QList<QKeySequence>()
1429 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1433 MacSupport::fullScreenActions(actionMap, fullScreenActive);
1436 if (views->currentWidget() == mediaView) mediaView->setFocus();
1438 if (fullScreenActive) {
1439 if (views->currentWidget() == mediaView) hideFullscreenUI();
1441 fullscreenTimer->stop();
1446 bool MainWindow::isReallyFullScreen() {
1448 WId handle = winId();
1449 if (mac::CanGoFullScreen(handle))
1450 return mac::IsFullScreen(handle);
1452 return isFullScreen();
1454 return isFullScreen();
1458 void MainWindow::missingKeyWarning() {
1459 static bool shown = false;
1462 QMessageBox msgBox(this);
1463 msgBox.setIconPixmap(IconUtils::pixmap(":/images/64x64/app.png", devicePixelRatioF()));
1464 msgBox.setText(QString("%1 was built without a Google API key.").arg(Constants::NAME));
1465 msgBox.setInformativeText(QString("It won't work unless you enter one."
1466 "<p>In alternative you can get %1 from the developer site.")
1467 .arg(Constants::NAME));
1468 msgBox.setModal(true);
1469 msgBox.setWindowModality(Qt::WindowModal);
1470 msgBox.addButton(QMessageBox::Close);
1471 QPushButton *enterKeyButton =
1472 msgBox.addButton(QString("Enter API key..."), QMessageBox::AcceptRole);
1473 QPushButton *devButton = msgBox.addButton(QString("Get from %1").arg(Constants::WEBSITE),
1474 QMessageBox::AcceptRole);
1475 QPushButton *helpButton = msgBox.addButton(QMessageBox::Help);
1479 if (msgBox.clickedButton() == helpButton) {
1480 QDesktopServices::openUrl(QUrl("https://github.com/flaviotordini/minitube/blob/master/"
1481 "README.md#google-api-key"));
1482 } else if (msgBox.clickedButton() == enterKeyButton) {
1484 QString text = QInputDialog::getText(this, QString(), "Google API key:", QLineEdit::Normal,
1486 if (ok && !text.isEmpty()) {
1488 settings.setValue("googleApiKey", text);
1489 YT3::instance().initApiKeys();
1491 } else if (msgBox.clickedButton() == devButton) {
1492 QDesktopServices::openUrl(QUrl(Constants::WEBSITE));
1497 void MainWindow::compactView(bool enable) {
1498 setUpdatesEnabled(false);
1500 compactModeActive = enable;
1502 static QList<QKeySequence> compactShortcuts;
1503 static QList<QKeySequence> stopShortcuts;
1505 const QString key = "compactGeometry";
1509 setMinimumSize(320, 180);
1511 mac::RemoveFullScreenWindow(winId());
1515 if (settings.contains(key))
1516 restoreGeometry(settings.value(key).toByteArray());
1520 #ifdef APP_MAC_QMACTOOLBAR
1521 mac::showToolBar(winId(), !enable);
1523 mainToolBar->setVisible(!enable);
1525 mediaView->setSidebarVisibility(!enable);
1526 statusBar()->hide();
1528 compactShortcuts = compactViewAct->shortcuts();
1529 stopShortcuts = stopAct->shortcuts();
1531 QList<QKeySequence> newStopShortcuts(stopShortcuts);
1532 newStopShortcuts.removeAll(QKeySequence(Qt::Key_Escape));
1533 stopAct->setShortcuts(newStopShortcuts);
1534 compactViewAct->setShortcuts(QList<QKeySequence>(compactShortcuts)
1535 << QKeySequence(Qt::Key_Escape));
1537 // ensure focus does not end up to the search box
1538 // as it would steal the Space shortcut
1539 mediaView->setFocus();
1542 settings.setValue(key, saveGeometry());
1544 // unset minimum size
1545 setMinimumSize(0, 0);
1548 mac::SetupFullScreenWindow(winId());
1550 #ifdef APP_MAC_QMACTOOLBAR
1551 mac::showToolBar(winId(), !enable);
1553 mainToolBar->setVisible(!enable);
1555 mediaView->setSidebarVisibility(!enable);
1556 if (needStatusBar()) setStatusBarVisibility(true);
1560 compactViewAct->setShortcuts(compactShortcuts);
1561 stopAct->setShortcuts(stopShortcuts);
1564 // auto float on top
1565 floatOnTop(enable, false);
1568 mac::compactMode(winId(), enable);
1571 menuVisibleBeforeCompactMode = menuBar()->isVisible();
1574 menuBar()->setVisible(menuVisibleBeforeCompactMode);
1578 setUpdatesEnabled(true);
1581 void MainWindow::toggleToolbarMenu() {
1582 if (!toolbarMenu) toolbarMenu = new ToolbarMenu(this);
1583 if (toolbarMenu->isVisible())
1584 toolbarMenu->hide();
1586 toolbarMenu->show();
1589 void MainWindow::searchFocus() {
1590 toolbarSearch->selectAll();
1591 toolbarSearch->setFocus();
1594 void MainWindow::initMedia() {
1596 qFatal("QtAV has a showstopper bug. Audio stops randomly. See bug "
1597 "https://github.com/wang-bin/QtAV/issues/1184");
1598 media = new MediaQtAV(this);
1599 #elif defined MEDIA_MPV
1600 media = new MediaMPV();
1602 qFatal("No media backend defined");
1605 media->setUserAgent(HttpUtils::stealthUserAgent());
1608 qreal volume = settings.value("volume", 1.).toReal();
1609 media->setVolume(volume);
1611 connect(media, &Media::error, this, &MainWindow::handleError);
1612 connect(media, &Media::stateChanged, this, &MainWindow::stateChanged);
1613 connect(media, &Media::positionChanged, this, &MainWindow::tick);
1615 connect(seekSlider, &QSlider::sliderMoved, this, [this](int value) {
1616 // value : maxValue = posit ion : duration
1617 qint64 ms = (value * media->duration()) / seekSlider->maximum();
1618 qDebug() << "Seeking to" << ms;
1620 if (media->state() == Media::PausedState) media->play();
1622 connect(seekSlider, &QSlider::sliderPressed, this, [this]() {
1623 // value : maxValue = position : duration
1624 qint64 ms = (seekSlider->value() * media->duration()) / seekSlider->maximum();
1626 if (media->state() == Media::PausedState) media->play();
1628 connect(media, &Media::started, this, [this]() { seekSlider->setValue(0); });
1630 connect(media, &Media::volumeChanged, this, &MainWindow::volumeChanged);
1631 connect(media, &Media::volumeMutedChanged, this, &MainWindow::volumeMutedChanged);
1632 connect(volumeSlider, &QSlider::sliderMoved, this, [this](int value) {
1633 qreal volume = (qreal)value / volumeSlider->maximum();
1634 media->setVolume(volume);
1636 connect(volumeSlider, &QSlider::sliderPressed, this, [this]() {
1637 qreal volume = (qreal)volumeSlider->value() / volumeSlider->maximum();
1638 media->setVolume(volume);
1641 mediaView->setMedia(media);
1644 void MainWindow::tick(qint64 time) {
1646 bool isDown = seekSlider->property("down").isValid();
1648 bool isDown = seekSlider->isSliderDown();
1650 if (!isDown && media->state() == Media::PlayingState) {
1651 // value : maxValue = position : duration
1652 qint64 duration = media->duration();
1653 if (duration <= 0) return;
1654 int value = (seekSlider->maximum() * media->position()) / duration;
1655 seekSlider->setValue(value);
1658 const QString s = formatTime(time);
1659 if (s != currentTimeLabel->text()) {
1660 currentTimeLabel->setText(s);
1661 emit currentTimeChanged(s);
1664 const qint64 remainingTime = media->remainingTime();
1665 currentTimeLabel->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
1669 QString MainWindow::formatTime(qint64 duration) {
1672 int seconds = (int)(duration % 60);
1674 int minutes = (int)(duration % 60);
1676 int hours = (int)(duration % 24);
1677 if (hours == 0) return res.sprintf("%02d:%02d", minutes, seconds);
1678 return res.sprintf("%02d:%02d:%02d", hours, minutes, seconds);
1681 void MainWindow::volumeUp() {
1682 qreal newVolume = media->volume() + .1;
1683 if (newVolume > 1.) newVolume = 1.;
1684 media->setVolume(newVolume);
1687 void MainWindow::volumeDown() {
1688 qreal newVolume = media->volume() - .1;
1689 if (newVolume < 0) newVolume = 0;
1690 media->setVolume(newVolume);
1693 void MainWindow::toggleVolumeMute() {
1694 bool muted = media->volumeMuted();
1695 media->setVolumeMuted(!muted);
1698 void MainWindow::volumeChanged(qreal newVolume) {
1699 // automatically unmute when volume changes
1700 if (media->volumeMuted()) media->setVolumeMuted(false);
1701 showMessage(tr("Volume at %1%").arg((int)(newVolume * 100)));
1702 // newVolume : 1.0 = x : 1000
1703 int value = newVolume * volumeSlider->maximum();
1704 volumeSlider->blockSignals(true);
1705 volumeSlider->setValue(value);
1706 volumeSlider->blockSignals(false);
1709 void MainWindow::volumeMutedChanged(bool muted) {
1711 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-muted"));
1712 showMessage(tr("Volume is muted"));
1714 volumeMuteAct->setIcon(IconUtils::icon("audio-volume-high"));
1715 showMessage(tr("Volume is unmuted"));
1719 void MainWindow::setDefinitionMode(const QString &definitionName) {
1720 QAction *definitionAct = getAction("definition");
1721 definitionAct->setText(definitionName);
1722 definitionAct->setStatusTip(
1723 tr("Maximum video definition set to %1").arg(definitionAct->text()) + " (" +
1724 definitionAct->shortcut().toString(QKeySequence::NativeText) + ")");
1725 showMessage(definitionAct->statusTip());
1726 YT3::instance().setMaxVideoDefinition(definitionName);
1727 if (views->currentWidget() == mediaView) {
1728 mediaView->reloadCurrentVideo();
1732 void MainWindow::toggleDefinitionMode() {
1733 const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
1734 const VideoDefinition ¤tDefinition = YT3::instance().maxVideoDefinition();
1736 int index = definitions.indexOf(currentDefinition);
1737 if (index != definitions.size() - 1) {
1742 setDefinitionMode(definitions.at(index).getName());
1745 void MainWindow::clearRecentKeywords() {
1747 settings.remove("recentKeywords");
1748 settings.remove("recentChannels");
1749 if (views->currentWidget() == homeView) {
1750 SearchView *searchView = homeView->getSearchView();
1751 searchView->updateRecentKeywords();
1752 searchView->updateRecentChannels();
1754 HttpUtils::clearCaches();
1755 showMessage(tr("Your privacy is now safe"));
1758 void MainWindow::setManualPlay(bool enabled) {
1760 settings.setValue("manualplay", QVariant::fromValue(enabled));
1761 if (views->currentWidget() == homeView &&
1762 homeView->currentWidget() == homeView->getSearchView())
1764 showActionsInStatusBar({getAction("manualplay")}, enabled);
1767 void MainWindow::updateDownloadMessage(const QString &message) {
1768 getAction("downloads")->setText(message);
1771 void MainWindow::downloadsFinished() {
1772 getAction("downloads")->setText(tr("&Downloads"));
1773 showMessage(tr("Downloads complete"));
1776 void MainWindow::toggleDownloads(bool show) {
1778 stopAct->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_MediaStop));
1779 getAction("downloads")
1780 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J)
1781 << QKeySequence(Qt::Key_Escape));
1783 getAction("downloads")
1784 ->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::CTRL + Qt::Key_J));
1785 stopAct->setShortcuts(QList<QKeySequence>()
1786 << QKeySequence(Qt::Key_Escape) << QKeySequence(Qt::Key_MediaStop));
1789 if (!downloadView) {
1790 downloadView = new DownloadView(this);
1791 views->addWidget(downloadView);
1794 showView(downloadView);
1799 void MainWindow::suggestionAccepted(Suggestion *suggestion) {
1800 search(suggestion->value);
1803 void MainWindow::search(const QString &query) {
1804 QString q = query.simplified();
1805 if (q.isEmpty()) return;
1806 SearchParams *searchParams = new SearchParams();
1807 searchParams->setKeywords(q);
1808 showMedia(searchParams);
1811 void MainWindow::dragEnterEvent(QDragEnterEvent *e) {
1812 if (e->mimeData()->hasFormat("text/uri-list")) {
1813 QList<QUrl> urls = e->mimeData()->urls();
1814 if (urls.isEmpty()) return;
1815 const QUrl &url = urls.at(0);
1816 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1817 if (!videoId.isEmpty()) e->acceptProposedAction();
1821 void MainWindow::dropEvent(QDropEvent *e) {
1822 if (!toolbarSearch->isEnabled()) return;
1824 QList<QUrl> urls = e->mimeData()->urls();
1825 if (urls.isEmpty()) return;
1826 const QUrl &url = urls.at(0);
1827 QString videoId = YTSearch::videoIdFromUrl(url.toString());
1828 if (!videoId.isEmpty()) {
1829 setWindowTitle(url.toString());
1830 SearchParams *searchParams = new SearchParams();
1831 searchParams->setKeywords(videoId);
1832 showMedia(searchParams);
1836 bool MainWindow::needStatusBar() {
1837 return !statusToolBar->actions().isEmpty();
1840 void MainWindow::adjustMessageLabelPosition() {
1841 if (messageLabel->parent() == this)
1842 messageLabel->move(0, height() - messageLabel->height());
1844 messageLabel->move(mapToGlobal(QPoint(0, height() - messageLabel->height())));
1847 void MainWindow::floatOnTop(bool onTop, bool showAction) {
1848 if (showAction) showActionsInStatusBar({getAction("ontop")}, onTop);
1850 mac::floatOnTop(winId(), onTop);
1853 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1856 setWindowFlags(windowFlags() ^ Qt::WindowStaysOnTopHint);
1862 void MainWindow::restore() {
1864 mac::uncloseWindow(winId());
1868 void MainWindow::messageReceived(const QString &message) {
1869 if (message == QLatin1String("--toggle-playing")) {
1870 if (pauseAct->isEnabled()) pauseAct->trigger();
1871 } else if (message == QLatin1String("--next")) {
1872 if (skipAct->isEnabled()) skipAct->trigger();
1873 } else if (message == QLatin1String("--previous")) {
1874 if (skipBackwardAct->isEnabled()) skipBackwardAct->trigger();
1875 } else if (message == QLatin1String("--stop-after-this")) {
1876 getAction("stopafterthis")->toggle();
1877 } else if (message.startsWith("--")) {
1878 MainWindow::printHelp();
1879 } else if (!message.isEmpty()) {
1880 SearchParams *searchParams = new SearchParams();
1881 searchParams->setKeywords(message);
1882 showMedia(searchParams);
1886 void MainWindow::hideFullscreenUI() {
1887 if (views->currentWidget() != mediaView) return;
1888 setCursor(Qt::BlankCursor);
1890 QPoint p = mapFromGlobal(QCursor::pos());
1891 const int x = p.x();
1893 if (x > mediaView->getSidebar()->width()) mediaView->setSidebarVisibility(false);
1896 const int y = p.y();
1897 bool shouldHideToolbar = !toolbarSearch->hasFocus() && y > mainToolBar->height();
1898 if (shouldHideToolbar) mainToolBar->setVisible(false);
1902 void MainWindow::toggleMenuVisibility() {
1903 bool show = !menuBar()->isVisible();
1904 menuBar()->setVisible(show);
1907 void MainWindow::toggleMenuVisibilityWithMessage() {
1908 bool show = !menuBar()->isVisible();
1909 menuBar()->setVisible(show);
1911 QMessageBox msgBox(this);
1912 msgBox.setText(tr("You can still access the menu bar by pressing the ALT key"));
1913 msgBox.setModal(true);
1914 msgBox.setWindowModality(Qt::WindowModal);
1919 #ifdef APP_MAC_STORE
1920 void MainWindow::rateOnAppStore() {
1921 QDesktopServices::openUrl(QUrl("macappstore://userpub.itunes.apple.com"
1922 "/WebObjects/MZUserPublishing.woa/wa/addUserReview"
1923 "?id=422006190&type=Purple+Software"));
1927 void MainWindow::printHelp() {
1928 QString msg = QString("%1 %2\n\n").arg(Constants::NAME, Constants::VERSION);
1929 msg += "Usage: minitube [options]\n";
1930 msg += "Options:\n";
1931 msg += " --toggle-playing\t";
1932 msg += "Start or pause playback.\n";
1933 msg += " --next\t\t";
1934 msg += "Skip to the next video.\n";
1935 msg += " --previous\t\t";
1936 msg += "Go back to the previous video.\n";
1937 msg += " --stop-after-this\t";
1938 msg += "Stop playback at the end of the video.\n";
1939 std::cout << msg.toLocal8Bit().data();
1942 void MainWindow::setupAction(QAction *action) {
1943 // never autorepeat.
1944 // unexperienced users tend to keep keys pressed for a "long" time
1945 action->setAutoRepeat(false);
1947 // show keyboard shortcuts in the status bar
1948 if (!action->shortcut().isEmpty())
1949 action->setStatusTip(action->statusTip() + QLatin1String(" (") +
1950 action->shortcut().toString(QKeySequence::NativeText) +
1951 QLatin1String(")"));
1954 QAction *MainWindow::getAction(const char *name) {
1955 return actionMap.value(QByteArray::fromRawData(name, strlen(name)));
1958 void MainWindow::addNamedAction(const QByteArray &name, QAction *action) {
1959 actionMap.insert(name, action);
1962 QMenu *MainWindow::getMenu(const char *name) {
1963 return menuMap.value(QByteArray::fromRawData(name, strlen(name)));
1966 void MainWindow::showMessage(const QString &message) {
1967 if (!isVisible()) return;
1969 if (!mac::isVisible(winId())) return;
1971 if (statusBar()->isVisible())
1972 statusBar()->showMessage(message, 60000);
1973 else if (isActiveWindow()) {
1974 messageLabel->setText(message);
1975 QSize size = messageLabel->sizeHint();
1976 // round width to avoid flicker with fast changing messages (e.g. volume
1978 int w = size.width() + 10;
1979 const int multiple = 15;
1980 w = w + multiple / 2;
1983 messageLabel->resize(size);
1984 if (messageLabel->isHidden()) {
1985 adjustMessageLabelPosition();
1986 messageLabel->show();
1988 messageTimer->start();
1992 void MainWindow::hideMessage() {
1993 if (messageLabel->isVisible()) {
1994 messageLabel->hide();
1995 messageLabel->clear();
1999 void MainWindow::handleError(const QString &message) {
2000 qWarning() << message;
2001 showMessage(message);
2004 #ifdef APP_ACTIVATION
2005 void MainWindow::showActivationView() {
2006 View *activationView = ActivationView::instance();
2007 views->addWidget(activationView);
2008 if (views->currentWidget() != activationView) showView(activationView);
2012 void MainWindow::showRegionsView() {
2014 regionsView = new RegionsView(this);
2015 connect(regionsView, SIGNAL(regionChanged()), homeView->getStandardFeedsView(),
2017 views->addWidget(regionsView);
2019 showView(regionsView);