summaryrefslogtreecommitdiff
path: root/raveos-calamares-module/raveusers/RaveUsersViewStep.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'raveos-calamares-module/raveusers/RaveUsersViewStep.cpp')
-rw-r--r--raveos-calamares-module/raveusers/RaveUsersViewStep.cpp253
1 files changed, 253 insertions, 0 deletions
diff --git a/raveos-calamares-module/raveusers/RaveUsersViewStep.cpp b/raveos-calamares-module/raveusers/RaveUsersViewStep.cpp
new file mode 100644
index 0000000..4e21e65
--- /dev/null
+++ b/raveos-calamares-module/raveusers/RaveUsersViewStep.cpp
@@ -0,0 +1,253 @@
+#include "RaveUsersViewStep.h"
+
+#include "RaveUsersBackend.h"
+#include "RaveUsersJobs.h"
+
+#include "GlobalStorage.h"
+#include "JobQueue.h"
+#include "utils/Logger.h"
+#include "utils/String.h"
+#include "utils/Variant.h"
+
+#include <QDir>
+#include <QFile>
+#include <QLocale>
+#include <QQmlContext>
+#include <QQmlEngine>
+#include <QQuickItem>
+#include <QQuickWidget>
+#include <QVBoxLayout>
+
+CALAMARES_PLUGIN_FACTORY_DEFINITION( RaveUsersViewStepFactory, registerPlugin< RaveUsersViewStep >(); )
+
+RaveUsersViewStep::RaveUsersViewStep( QObject* parent )
+ : Calamares::ViewStep( parent )
+ , m_backend( new RaveUsersBackend( this ) )
+{
+ connect( m_backend, &RaveUsersBackend::readyChanged, this, [this]() {
+ emit nextStatusChanged( m_backend->isReady() );
+ } );
+}
+
+RaveUsersViewStep::~RaveUsersViewStep()
+{
+ if ( m_widget && m_widget->parent() == nullptr )
+ {
+ delete m_widget;
+ }
+}
+
+QString
+RaveUsersViewStep::prettyName() const
+{
+ return tr( "User" );
+}
+
+void
+RaveUsersViewStep::buildWidget()
+{
+ m_widget = new QWidget();
+ auto* layout = new QVBoxLayout( m_widget );
+ layout->setContentsMargins( 0, 0, 0, 0 );
+
+ m_quickWidget = new QQuickWidget();
+ m_quickWidget->setResizeMode( QQuickWidget::SizeRootObjectToView );
+
+ m_quickWidget->setClearColor( QColor( 0x40, 0x40, 0x40 ) );
+
+ QString modulePath = QStringLiteral( "/usr/lib/calamares/modules/raveusers/ui" );
+ if ( !QDir( modulePath ).exists() )
+ {
+ modulePath = QDir::currentPath() + QStringLiteral( "/raveusers/ui" );
+ }
+
+ m_quickWidget->engine()->addImportPath( modulePath );
+ m_quickWidget->rootContext()->setContextProperty( "backend", m_backend );
+
+ m_locale = getLocale();
+ m_quickWidget->rootContext()->setContextProperty( "systemLocale", m_locale );
+
+ QString qmlPath = QStringLiteral( "/usr/lib/calamares/modules/raveusers/ui/raveusers.qml" );
+ if ( !QFile::exists( qmlPath ) )
+ {
+ qmlPath = QDir::currentPath() + QStringLiteral( "/raveusers/ui/raveusers.qml" );
+ }
+
+ m_quickWidget->setSource( QUrl::fromLocalFile( qmlPath ) );
+ layout->addWidget( m_quickWidget );
+}
+
+QWidget*
+RaveUsersViewStep::widget()
+{
+ if ( !m_widget )
+ {
+ buildWidget();
+ }
+ return m_widget;
+}
+
+bool
+RaveUsersViewStep::isNextEnabled() const
+{
+ return m_backend && m_backend->isReady();
+}
+
+bool
+RaveUsersViewStep::isBackEnabled() const
+{
+ return true;
+}
+
+bool
+RaveUsersViewStep::isAtBeginning() const
+{
+ return true;
+}
+
+bool
+RaveUsersViewStep::isAtEnd() const
+{
+ return true;
+}
+
+Calamares::JobList
+RaveUsersViewStep::jobs() const
+{
+ Calamares::JobList jobs;
+
+ if ( !m_backend || !m_backend->isReady() )
+ {
+ return jobs;
+ }
+
+ if ( !m_backend->sudoersGroup().isEmpty() )
+ {
+ jobs.append( Calamares::job_ptr(
+ new RaveUsersSetupSudoJob( m_backend->sudoersGroup(), m_backend->sudoersConfigureWithGroup() ) ) );
+ }
+
+ jobs.append( Calamares::job_ptr( new RaveUsersSetupGroupsJob( m_backend->defaultGroups() ) ) );
+ jobs.append( Calamares::job_ptr( new RaveUsersCreateUserJob( m_backend ) ) );
+ jobs.append( Calamares::job_ptr( new RaveUsersSetPasswordJob( m_backend->username(), m_backend->userPassword() ) ) );
+ jobs.append( Calamares::job_ptr( new RaveUsersSetPasswordJob( QStringLiteral( "root" ), m_backend->rootPassword() ) ) );
+ jobs.append( Calamares::job_ptr( new RaveUsersSetHostnameJob( m_backend->hostname() ) ) );
+
+ return jobs;
+}
+
+void
+RaveUsersViewStep::onActivate()
+{
+ const QString newLocale = getLocale();
+ if ( newLocale != m_locale )
+ {
+ m_locale = newLocale;
+ if ( m_quickWidget )
+ {
+ m_quickWidget->rootContext()->setContextProperty( "systemLocale", m_locale );
+ }
+ }
+
+ if ( m_quickWidget )
+ {
+ QQuickItem* root = m_quickWidget->rootObject();
+ if ( root )
+ {
+ QMetaObject::invokeMethod( root, "updateLanguage", Qt::DirectConnection );
+ }
+ }
+}
+
+void
+RaveUsersViewStep::onLeave()
+{
+ if ( !m_backend || !m_backend->isReady() )
+ {
+ return;
+ }
+
+ Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
+ if ( !gs )
+ {
+ return;
+ }
+
+ const QString username = m_backend->username();
+ gs->insert( "username", username );
+ gs->insert( "hostname", m_backend->hostname() );
+ gs->insert( "fullname", username );
+ gs->insert( "password", Calamares::String::obscure( m_backend->userPassword() ) );
+ gs->insert( "setRootPassword", m_backend->rootReuseVisible() );
+ gs->insert( "reuseRootPassword", m_backend->reuseRootPassword() );
+}
+
+void
+RaveUsersViewStep::setConfigurationMap( const QVariantMap& config )
+{
+ const QVariantMap userSettings = config.value( "user" ).toMap();
+ QString shell = Calamares::getString( userSettings, "shell" );
+ m_backend->setUserShell( shell.isEmpty() ? QStringLiteral( "/bin/bash" ) : shell );
+
+ QStringList forbiddenLogin = Calamares::getStringList( userSettings, "forbidden_names" );
+ forbiddenLogin << QStringLiteral( "root" ) << QStringLiteral( "nobody" );
+ forbiddenLogin.removeDuplicates();
+ m_backend->setForbiddenLoginNames( forbiddenLogin );
+
+ const QVariantMap hostnameSettings = config.value( "hostname" ).toMap();
+ QStringList forbiddenHost = Calamares::getStringList( hostnameSettings, "forbidden_names" );
+ forbiddenHost << QStringLiteral( "localhost" );
+ forbiddenHost.removeDuplicates();
+ m_backend->setForbiddenHostNames( forbiddenHost );
+
+ m_backend->setSudoersGroup( Calamares::getString( config, "sudoersGroup" ) );
+ m_backend->setSudoersConfigureWithGroup( Calamares::getBool( config, "sudoersConfigureWithGroup", true ) );
+ m_backend->setSetRootPassword( Calamares::getBool( config, "setRootPassword", true ) );
+
+ QStringList groups;
+ for ( const auto& v : config.value( "defaultGroups" ).toList() )
+ {
+ if ( v.typeId() == QMetaType::QString )
+ {
+ groups << v.toString();
+ }
+ else if ( v.typeId() == QMetaType::QVariantMap )
+ {
+ const QString name = v.toMap().value( "name" ).toString();
+ if ( !name.isEmpty() )
+ {
+ groups << name;
+ }
+ }
+ }
+ if ( !groups.isEmpty() )
+ {
+ m_backend->setDefaultGroups( groups );
+ }
+}
+
+QString
+RaveUsersViewStep::getLocale() const
+{
+ Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
+ if ( gs )
+ {
+ if ( gs->contains( "locale" ) )
+ {
+ const QString loc = gs->value( "locale" ).toString();
+ if ( !loc.isEmpty() )
+ {
+ return loc;
+ }
+ }
+ if ( gs->contains( "localeConf" ) )
+ {
+ const QString lang = gs->value( "localeConf" ).toMap().value( "LANG" ).toString();
+ if ( !lang.isEmpty() )
+ {
+ return lang;
+ }
+ }
+ }
+ return QLocale::system().name();
+}