Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
Please login to access the resource
openSUSE:Leap:15.0:Ports
sddm
0001-greeter-Use-Qt-command-line-parser.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0001-greeter-Use-Qt-command-line-parser.patch of Package sddm
From 66b764abc0f7fa1562cf2b9ef4d751fd5d5a686e Mon Sep 17 00:00:00 2001 From: Pier Luigi Fiorini <pierluigi.fiorini@liri.io> Date: Sun, 4 Mar 2018 04:12:55 +0100 Subject: [PATCH 1/2] greeter: Use Qt command line parser Do not reinvent the wheel with a command line parser. Restructure the code so that GreeterApp no longer need to parse arguments itself. --- src/greeter/GreeterApp.cpp | 211 +++++++++++++++++++++++++++------------------ src/greeter/GreeterApp.h | 30 +++++-- 2 files changed, 149 insertions(+), 92 deletions(-) diff --git a/src/greeter/GreeterApp.cpp b/src/greeter/GreeterApp.cpp index 5fb70ea..1230efa 100644 --- a/src/greeter/GreeterApp.cpp +++ b/src/greeter/GreeterApp.cpp @@ -1,5 +1,5 @@ /*************************************************************************** -* Copyright (c) 2015-2016 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com> +* Copyright (c) 2015-2018 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com> * Copyright (c) 2013 Abdurrahman AVCI <abdurrahmanavci@gmail.com> * * This program is free software; you can redistribute it and/or modify @@ -31,6 +31,7 @@ #include "MessageHandler.h" +#include <QCommandLineParser> #include <QGuiApplication> #include <QQuickItem> #include <QQuickView> @@ -42,98 +43,91 @@ #include <iostream> -namespace SDDM { - QString parameter(const QStringList &arguments, const QString &key, const QString &defaultValue) { - int index = arguments.indexOf(key); +#define TR(x) QT_TRANSLATE_NOOP("Command line parser", QStringLiteral(x)) - if ((index < 0) || (index >= arguments.size() - 1)) - return defaultValue; +static const QEvent::Type StartupEventType = static_cast<QEvent::Type>(QEvent::registerEventType()); - QString value = arguments.at(index + 1); +namespace SDDM { + GreeterApp::GreeterApp(QObject *parent) + : QObject(parent) + { + // Translations + // Components translation + m_components_tranlator = new QTranslator(); + if (m_components_tranlator->load(QLocale::system(), QString(), QString(), QStringLiteral(COMPONENTS_TRANSLATION_DIR))) + QCoreApplication::installTranslator(m_components_tranlator); - if (value.startsWith(QLatin1Char('-'))) - return defaultValue; - return value; + // Create models + m_sessionModel = new SessionModel(); + m_userModel = new UserModel(); + m_keyboard = new KeyboardModel(); } - GreeterApp *GreeterApp::self = nullptr; + bool GreeterApp::isTestModeEnabled() const + { + return m_testing; + } - GreeterApp::GreeterApp(int &argc, char **argv) : QGuiApplication(argc, argv) { - // point instance to this - self = this; + void GreeterApp::setTestModeEnabled(bool value) + { + m_testing = value; + } - // Parse arguments - bool testing = false; + QString GreeterApp::socketName() const + { + return m_socket; + } - if (arguments().contains(QStringLiteral("--test-mode"))) - testing = true; + void GreeterApp::setSocketName(const QString &name) + { + m_socket = name; + } - // get socket name - QString socket = parameter(arguments(), QStringLiteral("--socket"), QString()); + QString GreeterApp::themePath() const + { + return m_themePath; + } - // get theme path (fallback to internal theme) - m_themePath = parameter(arguments(), QStringLiteral("--theme"), QString()); + void GreeterApp::setThemePath(const QString &path) + { + m_themePath = path; if (m_themePath.isEmpty()) m_themePath = QLatin1String("qrc:/theme"); - // read theme metadata - m_metadata = new ThemeMetadata(QStringLiteral("%1/metadata.desktop").arg(m_themePath)); - - // Translations - // Components translation - m_components_tranlator = new QTranslator(); - if (m_components_tranlator->load(QLocale::system(), QString(), QString(), QStringLiteral(COMPONENTS_TRANSLATION_DIR))) - installTranslator(m_components_tranlator); - - // Theme specific translation - m_theme_translator = new QTranslator(); - if (m_theme_translator->load(QLocale::system(), QString(), QString(), - QStringLiteral("%1/%2/").arg(m_themePath, m_metadata->translationsDirectory()))) - installTranslator(m_theme_translator); + // Read theme metadata + const QString metadataPath = QStringLiteral("%1/metadata.desktop").arg(m_themePath); + if (m_metadata) + m_metadata->setTo(metadataPath); + else + m_metadata = new ThemeMetadata(metadataPath); - // get theme config file + // Get theme config file QString configFile = QStringLiteral("%1/%2").arg(m_themePath).arg(m_metadata->configFile()); - // read theme config - m_themeConfig = new ThemeConfig(configFile); + // Read theme config + if (m_themeConfig) + m_themeConfig->setTo(configFile); + else + m_themeConfig = new ThemeConfig(configFile); - // set default icon theme from greeter theme + // Set default icon theme from greeter theme if (m_themeConfig->contains(QStringLiteral("iconTheme"))) QIcon::setThemeName(m_themeConfig->value(QStringLiteral("iconTheme")).toString()); - // create models - - m_sessionModel = new SessionModel(); - m_userModel = new UserModel(); - m_proxy = new GreeterProxy(socket); - m_keyboard = new KeyboardModel(); - - if(!testing && !m_proxy->isConnected()) { - qCritical() << "Cannot connect to the daemon - is it running?"; - exit(EXIT_FAILURE); - } - - // Set numlock upon start - if (m_keyboard->enabled()) { - if (mainConfig.Numlock.get() == MainConfig::NUM_SET_ON) - m_keyboard->setNumLockState(true); - else if (mainConfig.Numlock.get() == MainConfig::NUM_SET_OFF) - m_keyboard->setNumLockState(false); - } - - m_proxy->setSessionModel(m_sessionModel); - - // create views - QList<QScreen *> screens = primaryScreen()->virtualSiblings(); - Q_FOREACH (QScreen *screen, screens) - addViewForScreen(screen); + // Theme specific translation + if (m_theme_translator) + m_theme_translator->deleteLater(); + m_theme_translator = new QTranslator(); + if (m_theme_translator->load(QLocale::system(), QString(), QString(), + QStringLiteral("%1/%2/").arg(m_themePath, m_metadata->translationsDirectory()))) + QCoreApplication::installTranslator(m_theme_translator); + } - // handle screens - connect(this, &GreeterApp::screenAdded, this, &GreeterApp::addViewForScreen); - connect(this, &GreeterApp::primaryScreenChanged, this, [this](QScreen *) { - activatePrimary(); - }); + void GreeterApp::customEvent(QEvent *event) + { + if (event->type() == StartupEventType) + startup(); } void GreeterApp::addViewForScreen(QScreen *screen) { @@ -149,7 +143,7 @@ namespace SDDM { // need to be careful here since Qt will move the view to // another screen before this signal is emitted so we // pass a pointer to the view to our slot - connect(this, &GreeterApp::screenRemoved, this, [view, this](QScreen *) { + connect(qGuiApp, &QGuiApplication::screenRemoved, this, [view, this](QScreen *) { removeViewForScreen(view); }); @@ -231,6 +225,38 @@ namespace SDDM { view->deleteLater(); } + void GreeterApp::startup() + { + // Connect to the daemon + m_proxy = new GreeterProxy(m_socket); + if (!m_testing && !m_proxy->isConnected()) { + qCritical() << "Cannot connect to the daemon - is it running?"; + QCoreApplication::exit(EXIT_FAILURE); + } + + // Set numlock upon start + if (m_keyboard->enabled()) { + if (mainConfig.Numlock.get() == MainConfig::NUM_SET_ON) + m_keyboard->setNumLockState(true); + else if (mainConfig.Numlock.get() == MainConfig::NUM_SET_OFF) + m_keyboard->setNumLockState(false); + } + + // Set session model on proxy + m_proxy->setSessionModel(m_sessionModel); + + // Create views + QList<QScreen *> screens = qGuiApp->primaryScreen()->virtualSiblings(); + Q_FOREACH (QScreen *screen, screens) + addViewForScreen(screen); + + // Handle screens + connect(qGuiApp, &QGuiApplication::screenAdded, this, &GreeterApp::addViewForScreen); + connect(qGuiApp, &QGuiApplication::primaryScreenChanged, this, [this](QScreen *) { + activatePrimary(); + }); + } + void GreeterApp::activatePrimary() { // activate and give focus to the window assigned to the primary screen Q_FOREACH (QQuickView *view, m_views) { @@ -240,10 +266,16 @@ namespace SDDM { } } } + + StartupEvent::StartupEvent() + : QEvent(StartupEventType) + { + } } -int main(int argc, char **argv) { - // install message handler +int main(int argc, char **argv) +{ + // Install message handler qInstallMessageHandler(SDDM::GreeterMessageHandler); // HiDPI @@ -259,22 +291,29 @@ int main(int argc, char **argv) { qDebug() << "High-DPI autoscaling not Enabled"; } - QStringList arguments; + QGuiApplication app(argc, argv); - for (int i = 0; i < argc; i++) - arguments << QString::fromLocal8Bit(argv[i]); + QCommandLineParser parser; + parser.setApplicationDescription(TR("SDDM greeter")); + parser.addHelpOption(); + parser.addVersionOption(); - if (arguments.contains(QStringLiteral("--help")) || arguments.contains(QStringLiteral("-h"))) { - std::cout << "Usage: " << argv[0] << " [options] [arguments]\n" - "Options: \n" - " --theme <theme path> Set greeter theme\n" - " --socket <socket name> Set socket name\n" - " --test-mode Start greeter in test mode" << std::endl; + QCommandLineOption testModeOption(QLatin1String("test-mode"), TR("Start greeter in test mode")); + parser.addOption(testModeOption); - return EXIT_FAILURE; - } + QCommandLineOption socketOption(QLatin1String("socket"), TR("Socket name"), TR("name")); + parser.addOption(socketOption); + + QCommandLineOption themeOption(QLatin1String("theme"), TR("Greeter theme"), TR("path")); + parser.addOption(themeOption); + + parser.process(app); - SDDM::GreeterApp app(argc, argv); + SDDM::GreeterApp *greeter = new SDDM::GreeterApp(); + greeter->setTestModeEnabled(parser.isSet(testModeOption)); + greeter->setSocketName(parser.value(socketOption)); + greeter->setThemePath(parser.value(themeOption)); + QCoreApplication::postEvent(greeter, new SDDM::StartupEvent()); return app.exec(); } diff --git a/src/greeter/GreeterApp.h b/src/greeter/GreeterApp.h index 1ebd981..ed63595 100644 --- a/src/greeter/GreeterApp.h +++ b/src/greeter/GreeterApp.h @@ -21,7 +21,7 @@ #ifndef GREETERAPP_H #define GREETERAPP_H -#include <QGuiApplication> +#include <QObject> #include <QScreen> #include <QQuickView> @@ -38,27 +38,38 @@ namespace SDDM { class KeyboardModel; - class GreeterApp : public QGuiApplication + class GreeterApp : public QObject { Q_OBJECT Q_DISABLE_COPY(GreeterApp) public: - explicit GreeterApp(int &argc, char **argv); + explicit GreeterApp(QObject *parent = nullptr); - static GreeterApp *instance() { return self; } + bool isTestModeEnabled() const; + void setTestModeEnabled(bool value); + + QString socketName() const; + void setSocketName(const QString &name); + + QString themePath() const; + void setThemePath(const QString &path); + + protected: + void customEvent(QEvent *event) override; private slots: void addViewForScreen(QScreen *screen); void removeViewForScreen(QQuickView *view); private: - static GreeterApp *self; + bool m_testing = false; + QString m_socket; + QString m_themePath; QList<QQuickView *> m_views; QTranslator *m_theme_translator { nullptr }, *m_components_tranlator { nullptr }; - QString m_themePath; ThemeMetadata *m_metadata { nullptr }; ThemeConfig *m_themeConfig { nullptr }; SessionModel *m_sessionModel { nullptr }; @@ -66,8 +77,15 @@ namespace SDDM { GreeterProxy *m_proxy { nullptr }; KeyboardModel *m_keyboard { nullptr }; + void startup(); void activatePrimary(); }; + + class StartupEvent : public QEvent + { + public: + StartupEvent(); + }; } -- 2.16.1
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor