Add support to set the hostname

main
Rohan Garg 10 years ago
parent 89fe455163
commit b587a0ff00

@ -11,6 +11,7 @@ calamares_add_plugin( users
SetPasswordJob.cpp
UsersViewStep.cpp
UsersPage.cpp
SetHostNameJob.cpp
UI
page_usersetup.ui
RESOURCES

@ -0,0 +1,69 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2014 Rohan Garg <rohan@kde.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "SetHostNameJob.h"
#include "GlobalStorage.h"
#include "utils/Logger.h"
#include "JobQueue.h"
#include <QFile>
#include <QDir>
SetHostNameJob::SetHostNameJob(const QString& hostname)
: Calamares::Job()
, m_hostname(hostname)
{
}
QString SetHostNameJob::prettyName() const
{
return tr( "Set hostname %1" ).arg( m_hostname );
}
Calamares::JobResult SetHostNameJob::exec()
{
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
if ( !gs || !gs->contains( "rootMountPoint" ) )
{
cLog() << "No rootMountPoint in global storage";
return Calamares::JobResult::error( tr( "Internal Error" ) );
}
QString destDir = gs->value( "rootMountPoint" ).toString();
if ( !QDir( destDir ).exists() )
{
cLog() << "rootMountPoint points to a dir which does not exist";
return Calamares::JobResult::error( tr( "Internal Error" ) );
}
QFile hostfile(destDir + "/etc/hostname");
if (!hostfile.open(QFile::WriteOnly))
{
cLog() << "Can't write to hostname file";
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
}
QTextStream out(&hostfile);
out << m_hostname << "\n";
hostfile.close();
return Calamares::JobResult::ok();
}

@ -0,0 +1,37 @@
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2014 Rohan Garg <rohan@kde.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SETHOSTNAMEJOB_CPP_H
#define SETHOSTNAMEJOB_CPP_H
#include <Job.h>
class SetHostNameJob : public Calamares::Job
{
Q_OBJECT
public:
SetHostNameJob(const QString &hostname);
QString prettyName() const override;
Calamares::JobResult exec() override;
private:
const QString m_hostname;
};
#endif // SETHOSTNAMEJOB_CPP_H

@ -24,6 +24,7 @@
#include "ui_page_usersetup.h"
#include "CreateUserJob.h"
#include "SetPasswordJob.h"
#include "SetHostNameJob.h"
#include "JobQueue.h"
#include "GlobalStorage.h"
#include "utils/Logger.h"
@ -109,6 +110,9 @@ UsersPage::createJobs( const QString& defaultUserGroup, const QStringList& defau
ui->textBoxRootPassword->text() );
list.append( Calamares::job_ptr( j ) );
j = new SetHostNameJob( ui->textBoxHostname->text() );
list.append( Calamares::job_ptr( j ) );
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
gs->insert( "hostname", ui->textBoxHostname->text() );
if ( ui->checkBoxLoginAuto->isChecked() )
@ -270,11 +274,11 @@ UsersPage::validateHostnameText( const QString& textRef )
if ( pos >= 0 && pos < text.size() )
ui->labelHostnameError->setText(
tr( "Your username contains an invalid character '%1'" )
tr( "Your hostname contains an invalid character '%1'" )
.arg( text.at( pos ) ) );
else
ui->labelHostnameError->setText(
tr( "Your username contains invalid characters!" ) );
tr( "Your hostname contains invalid characters!" ) );
m_readyHostname = false;
}

Loading…
Cancel
Save