Warning, file /multimedia/plasmatube/src/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-or-later
0004 
0005 #include "config.h"
0006 #include "controllers/plasmatube.h"
0007 #include "controllers/subscriptioncontroller.h"
0008 #include "models/videolistmodel.h"
0009 #include "models/videomodel.h"
0010 
0011 #ifdef Q_OS_ANDROID
0012 #include <QGuiApplication>
0013 #else
0014 #include <QApplication>
0015 #endif
0016 #include <QCommandLineParser>
0017 #include <QIcon>
0018 #include <QQmlApplicationEngine>
0019 #include <QQuickStyle>
0020 #include <QQuickWindow>
0021 
0022 #include <KLocalizedContext>
0023 #include <KAboutData>
0024 #include <KLocalizedString>
0025 
0026 #ifdef HAVE_KDBUSADDONS
0027 #include <KDBusService>
0028 #include <KWindowSystem>
0029 #endif
0030 
0031 #include "controllers/windowcontroller.h"
0032 #include "plasmatube-version.h"
0033 
0034 std::optional<QString> parseVideoString(const QString &vid)
0035 {
0036     const QRegularExpression exp(QStringLiteral(R"(https:\/\/[www.]*youtube.com\/watch\?v=(.*))"));
0037     const auto match = exp.match(vid);
0038     if (match.hasMatch()) {
0039         return match.captured(1);
0040     }
0041 
0042     return std::nullopt;
0043 }
0044 
0045 int main(int argc, char **argv)
0046 {
0047 #ifdef Q_OS_ANDROID
0048     QGuiApplication app(argc, argv);
0049     QQuickStyle::setStyle(QStringLiteral("org.kde.breeze"));
0050 #else
0051     QApplication app(argc, argv);
0052 
0053     if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
0054         QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
0055     }
0056 #endif
0057 
0058     KLocalizedString::setApplicationDomain("plasmatube");
0059     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0060 
0061     KAboutData about(QStringLiteral("plasmatube"),
0062                      i18n("PlasmaTube"),
0063                      QStringLiteral(PLASMATUBE_VERSION_STRING),
0064                      i18n("YouTube client"),
0065                      KAboutLicense::GPL_V3,
0066                      i18n("© Linus Jahn"));
0067     about.addAuthor(i18n("Linus Jahn"), i18n("Creator"), QStringLiteral("lnj@kaidan.im"));
0068     about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0069     about.setOrganizationDomain("kde.org");
0070     about.setBugAddress("https://bugs.kde.org/describecomponents.cgi?product=plasmatube");
0071 
0072     KAboutData::setApplicationData(about);
0073     QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.plasmatube")));
0074 
0075     QQmlApplicationEngine engine;
0076 
0077 #ifdef HAVE_KDBUSADDONS
0078     KDBusService service(KDBusService::Unique);
0079     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine](const QStringList &arguments, const QString & /*workingDirectory*/) {
0080         const auto rootObjects = engine.rootObjects();
0081         for (auto obj : rootObjects) {
0082             if (auto view = qobject_cast<QQuickWindow *>(obj)) {
0083                 KWindowSystem::updateStartupId(view);
0084                 KWindowSystem::activateWindow(view);
0085 
0086                 if (arguments.isEmpty()) {
0087                     return;
0088                 }
0089 
0090                 auto args = arguments;
0091                 args.removeFirst();
0092 
0093                 if (arguments.length() >= 1) {
0094                     if (const auto videoUrl = parseVideoString(args[0])) {
0095                         PlasmaTube::instance().openVideo(*videoUrl);
0096                     }
0097                 }
0098 
0099                 return;
0100             }
0101         }
0102     });
0103 #endif
0104 
0105     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0106 
0107     PlasmaTubeSettings settings(KSharedConfig::openConfig(QStringLiteral("plasmatuberc"), KConfig::SimpleConfig, QStandardPaths::AppConfigLocation));
0108     qmlRegisterSingletonInstance<PlasmaTubeSettings>("org.kde.plasmatube.private", 1, 0, "Settings", &settings);
0109     QObject::connect(&app, &QCoreApplication::aboutToQuit, &settings, &PlasmaTubeSettings::save);
0110 
0111     qmlRegisterType<MpvObject>("org.kde.plasmatube.private", 1, 0, "MpvObject");
0112 
0113     QCommandLineParser parser;
0114     parser.setApplicationDescription(i18n("YouTube client"));
0115 
0116     parser.addPositionalArgument(QStringLiteral("video-url"), QStringLiteral("YouTube video URL to play"));
0117 
0118     about.setupCommandLine(&parser);
0119     parser.process(app);
0120     about.processCommandLine(&parser);
0121 
0122     engine.loadFromModule(QStringLiteral("org.kde.plasmatube"), QStringLiteral("Main"));
0123 
0124     if (engine.rootObjects().isEmpty())
0125         return -1;
0126 
0127     if (QCoreApplication::arguments().length() > 1) {
0128         if (const auto videoUrl = parseVideoString(app.arguments()[1])) {
0129             PlasmaTube::instance().openVideo(*videoUrl);
0130         }
0131     }
0132 
0133 #ifdef HAVE_KDBUSADDONS
0134     QQuickWindow *window = nullptr;
0135 
0136     const auto rootObjects = engine.rootObjects();
0137     for (auto obj : rootObjects) {
0138         auto view = qobject_cast<QQuickWindow *>(obj);
0139         if (view) {
0140             window = view;
0141             break;
0142         }
0143     }
0144 
0145     if (window != nullptr) {
0146         auto controller = engine.singletonInstance<WindowController *>(QStringLiteral("org.kde.plasmatube"), QStringLiteral("WindowController"));
0147         controller->setWindow(window);
0148         controller->restoreGeometry();
0149     }
0150 #endif
0151 
0152     return app.exec();
0153 }