Commit be9cb5e1 authored by Carl Schwan's avatar Carl Schwan 🚴 Committed by Janet Blackquill
Browse files

Make the code look less like ugly Java

This is now following KDE conventions.
parent 70bc7c6f
set(SOURCES
Sources/Main.cpp
Sources/KISS.h
Resources/Resources.qrc
Sources/Main.cpp
Sources/Settings.cpp
Sources/KISS.cpp
Resources/Resources.qrc
)
qt5_add_dbus_interface(SOURCES
......@@ -13,9 +14,7 @@ qt5_add_dbus_interface(SOURCES
User
)
add_executable(org.kde.initialsystemsetup
${SOURCES}
)
add_executable(org.kde.initialsystemsetup ${SOURCES})
target_compile_definitions(org.kde.initialsystemsetup
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>
......
// SPDX-FileCopyrightText: 2021 Carson Black <uhhadd@gmail.com>
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "KISS.h"
KISS::KISS(QObject* parent)
: QObject(parent)
{
m_backend = backends[Settings::instance()->displayManager()]();
auto locs = KLocalizedString::availableDomainTranslations("plasmashell").values();
std::transform(locs.begin(), locs.end(), std::back_inserter(m_locales), [](const QString& locale) -> QVariant {
return QVariant::fromValue(Language {
.m_name = QLocale(locale).nativeLanguageName(),
.m_code = locale
});
});
std::sort(m_locales.begin(), m_locales.end(), [](const QVariant& lhs, const QVariant& rhs) -> bool {
return lhs.value<Language>().m_name < rhs.value<Language>().m_name;
});
m_accountsInterface = new OrgFreedesktopAccountsInterface(QStringLiteral("org.freedesktop.Accounts"), QStringLiteral("/org/freedesktop/Accounts"), QDBusConnection::systemBus(), this);
}
KISS::~KISS()
{}
QVariantList KISS::locales() const
{
return m_locales;
}
QStringList KISS::pages() const
{
return Settings::instance()->pages();
}
void KISS::disableSelf()
{
auto reply = m_accountsInterface->CreateUser(username(), realname(), admin() ? 1 : 0);
auto user = OrgFreedesktopAccountsUserInterface(QStringLiteral("org.freedesktop.Accounts"), reply.value().path(), QDBusConnection::systemBus(), this);
user.SetLanguage(targetLanguage());
user.SetPassword(password(), QString());
Systemd::instance()->disableService("org.kde.initialsystemsetup");
m_backend->yeetToSession(username());
Systemd::instance()->stopService("org.kde.initialsystemsetup");
}
QString KISS::checkPassword(const QString& username, const QString& realname, const QString& password)
{
auto usernameData = username.toLocal8Bit();
auto realnameData = realname.toLocal8Bit();
auto passwordData = password.toLocal8Bit();
usernameData.data();
auto data = FascistCheckUser(passwordData.data(), GetDefaultCracklibDict(), usernameData.data(), realnameData.data());
if (data == nullptr) {
return QString();
}
return QString::fromLocal8Bit(data);
}
......@@ -19,9 +19,10 @@
#include <crack.h>
#include <optional>
#include <memory>
static auto backends = QMap<QString,std::function<Backend*()>> {
{"sddm", []() -> Backend* { return new SDDMBackend; } }
static auto backends = QMap<QString,std::function<std::unique_ptr<Backend>()>> {
{"sddm", []() -> std::unique_ptr<Backend> {return std::unique_ptr<Backend>(new SDDMBackend); } }
};
struct Language
......@@ -39,45 +40,8 @@ public:
class KISS : public QObject
{
Q_OBJECT
QVariantList m_locales;
OrgFreedesktopAccountsInterface* m_accountsInterface;
Backend* m_backend;
QString m_name;
public: KISS(QObject* parent = nullptr) : QObject(parent)
{
m_backend = backends[Settings::instance()->displayManager()]();
auto locs = KLocalizedString::availableDomainTranslations("plasmashell").values();
std::transform(locs.begin(), locs.end(), std::back_inserter(m_locales), [](const QString& locale) -> QVariant {
return QVariant::fromValue(Language {
.m_name = QLocale(locale).nativeLanguageName(),
.m_code = locale
});
});
std::sort(m_locales.begin(), m_locales.end(), [](const QVariant& lhs, const QVariant& rhs) -> bool {
return lhs.value<Language>().m_name < rhs.value<Language>().m_name;
});
m_accountsInterface = new OrgFreedesktopAccountsInterface(QStringLiteral("org.freedesktop.Accounts"), QStringLiteral("/org/freedesktop/Accounts"), QDBusConnection::systemBus(), this);
}
public: ~KISS()
{
delete m_backend;
}
Q_PROPERTY(QVariantList locales READ locales CONSTANT)
public: QVariantList locales() const
{
return m_locales;
}
Q_PROPERTY(QStringList pages READ pages CONSTANT)
public: QStringList pages() const
{
return Settings::instance()->pages();
}
#define synth_prop(kind, name, default) Q_PROPERTY(kind name READ name WRITE set_ ## name RESET reset_ ## name NOTIFY name ## _changed) \
std::optional<kind> m_ ## name;\
......@@ -95,8 +59,6 @@ public: void reset_ ## name() {\
m_ ## name.reset(); Q_EMIT name ## _changed();\
}\
}
// The stuff that we may potentially use when setting stuff up
synth_prop(QString, targetLanguage, QStringLiteral("en_US"))
synth_prop(QString, username, QString())
......@@ -104,31 +66,18 @@ public: void reset_ ## name() {\
synth_prop(QString, password, QString())
synth_prop(bool, admin, true)
public: Q_INVOKABLE void disableSelf()
{
auto reply = m_accountsInterface->CreateUser(username(), realname(), admin() ? 1 : 0);
auto user = OrgFreedesktopAccountsUserInterface(QStringLiteral("org.freedesktop.Accounts"), reply.value().path(), QDBusConnection::systemBus(), this);
user.SetLanguage(targetLanguage());
user.SetPassword(password(), QString());
Systemd::instance()->disableService("org.kde.initialsystemsetup");
m_backend->yeetToSession(username());
Systemd::instance()->stopService("org.kde.initialsystemsetup");
}
public: Q_INVOKABLE QString checkPassword(const QString& username, const QString& realname, const QString& password)
{
auto usernameData = username.toLocal8Bit();
auto realnameData = realname.toLocal8Bit();
auto passwordData = password.toLocal8Bit();
usernameData.data();
auto data = FascistCheckUser(passwordData.data(), GetDefaultCracklibDict(), usernameData.data(), realnameData.data());
if (data == nullptr) {
return QString();
}
return QString::fromLocal8Bit(data);
}
public:
explicit KISS(QObject *parent = nullptr);
~KISS();
Q_INVOKABLE void disableSelf();
Q_INVOKABLE QString checkPassword(const QString& username, const QString& realname, const QString& password);
QVariantList locales() const;
QStringList pages() const;
private:
QVariantList m_locales;
OrgFreedesktopAccountsInterface* m_accountsInterface;
std::unique_ptr<Backend> m_backend;
};
// SPDX-FileCopyrightText: 2021 Carson Black <uhhadd@gmail.com>
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Settings.h"
Settings::Settings()
{
m_settings = g_settings_new("org.kde.initialsystemsetup");
GVariantIter *iter;
gchar *str;
m_pages << "locale";
g_settings_get(m_settings, "stages", "as", &iter);
while (g_variant_iter_loop (iter, "s", &str))
m_pages << QString::fromLocal8Bit(str);
m_pages << "ready";
g_variant_iter_free(iter);
auto session = g_settings_get_string(m_settings, "desktop-session");
m_session = QString::fromLocal8Bit(session);
g_free(session);
auto dm = g_settings_get_string(m_settings, "display-manager-backend");
m_dm = QString::fromLocal8Bit(dm);
g_free(dm);
}
Settings::~Settings()
{
g_free(m_settings);
}
Settings *Settings::instance()
{
Settings* inst = nullptr;
if (inst == nullptr) {
inst = new Settings;
}
return inst;
}
QString Settings::displayManager() const
{
return m_dm;
}
QString Settings::session() const
{
return m_session;
}
QStringList Settings::pages() const
{
return m_pages;
}
// SPDX-FileCopyrightText: 2021 Carson Black <> // TODO
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#undef signals
#include <gio/gio.h>
#include <QString>
#include <QStringList>
/// Settings for KISS
class Settings
{
private:
Settings();
~Settings();
public:
static Settings *instance();
QString displayManager() const;
QString session() const;
QStringList pages() const;
private:
GSettings* m_settings;
QStringList m_pages;
QString m_dm;
QString m_session;
private: Settings()
{
m_settings = g_settings_new("org.kde.initialsystemsetup");
GVariantIter *iter;
gchar *str;
m_pages << "locale";
g_settings_get(m_settings, "stages", "as", &iter);
while (g_variant_iter_loop (iter, "s", &str))
m_pages << QString::fromLocal8Bit(str);
m_pages << "ready";
g_variant_iter_free (iter);
auto session = g_settings_get_string(m_settings, "desktop-session");
m_session = QString::fromLocal8Bit(session);
g_free(session);
auto dm = g_settings_get_string(m_settings, "display-manager-backend");
m_dm = QString::fromLocal8Bit(dm);
g_free(dm);
}
private: ~Settings()
{
g_free(m_settings);
}
public: static Settings* instance()
{
Settings* inst = nullptr;
if (inst == nullptr) {
inst = new Settings;
}
return inst;
}
public: QString displayManager() const
{
return m_dm;
}
public: QString session() const
{
return m_session;
}
public: QStringList pages() const
{
return m_pages;
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment