1
/***************************************************************************
2
* Copyright (C) 2008 by Konstantinos Smanis *
3
* kon.smanis@gmail.com *
5
* This file is part of KGRUBEditor. *
7
* This program is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation; either version 2 of the License, or *
10
* (at your option) any later version. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program; if not, write to the *
19
* Free Software Foundation, Inc., *
20
* 51 Franklin Street, Fifth Floor *
21
* Boston, MA 02111-1307, USA. *
22
***************************************************************************/
32
#include <kmessagebox.h>
34
#include <kprogressdialog.h>
36
void HardDiskThread::setLocation( const QString &location )
38
m_location = location;
40
void HardDiskThread::run()
43
install.setProgram( "grub-install", QStringList( m_location ) );
45
emit exitCode( install.execute() );
48
void RescueFloppyThread::setMenuFile( const QString &menuFile )
50
m_menuFile = menuFile;
52
void RescueFloppyThread::run()
54
emit currentProcess( i18nc( "@info", "Formatting Floppy..." ) );
55
emit currentStep( 20 );
56
if ( KProcess::execute( "mke2fs", QStringList( "/dev/fd0" ) ) != 0 )
62
emit currentProcess( i18nc( "@info", "Creating Mountpoint..." ) );
63
emit currentStep( 40 );
64
if ( !QDir().exists( "/mnt/rescue" ) )
66
if ( !QDir().mkpath( "/mnt/rescue" ) )
73
emit currentProcess( i18nc( "@info", "Mounting Floppy..." ) );
74
emit currentStep( 60 );
75
if ( KProcess::execute( "mount", QStringList() << "-t" << "ext2" << "/dev/fd0" << "/mnt/rescue" ) != 0 )
81
emit currentProcess( i18nc( "@info", "Installing GRUB..." ) );
82
emit currentStep( 80 );
83
if ( KProcess::execute( "grub-install", QStringList() << "--root-directory=/mnt/rescue" << "/dev/fd0" ) != 0 )
89
if ( !m_menuFile.isEmpty() )
91
emit currentProcess( i18nc( "@info", "Copying Menu File..." ) );
92
emit currentStep( 100 );
93
if ( !QFile::copy( m_menuFile, "/mnt/rescue/boot/grub/menu.lst" ) )
103
InstallAssistant::InstallAssistant( const QVector<GRUB::Misc::Device> &devices, QWidget *parent ) : KAssistantDialog( parent )
106
setCaption( i18nc( "@window:title", "Install Assistant" ) );
107
setWindowIcon( KIcon( "drive-harddisk" ) );
108
setAttribute( Qt::WA_DeleteOnClose );
109
showButton( KDialog::Help, false );
110
setInitialSize( QSize( 600, 400 ) );
112
QWidget *intro = new QWidget( this );
113
ui_intro.setupUi( intro );
114
addPage( intro, i18nc( "@title", "Introduction" ) );
115
QWidget *choice = new QWidget( this );
116
ui_choice.setupUi( choice );
117
choicePage = addPage( choice, i18nc( "@title", "Select where to install GRUB" ) );
118
QWidget *hdd = new QWidget( this );
119
ui_hdd.setupUi( hdd );
120
hardPage = addPage( hdd, i18nc( "@title", "Hard Disk Installation" ) );
121
QWidget *floppy = new QWidget( this );
122
ui_floppy.setupUi( floppy );
123
floppyPage = addPage( floppy, i18nc( "@title", "Floppy Disk Installation" ) );
125
m_devices = new QVector<GRUB::Misc::Device>;
126
m_devices = &devices;
130
ui_choice.label_iconHard->setPixmap( KIcon( "drive-harddisk" ).pixmap( 48, 48 ) );
131
ui_choice.label_iconFloppy->setPixmap( KIcon( "media-floppy" ).pixmap( 48, 48 ) );
133
ui_hdd.klineedit->setValidator( new QRegExpValidator( QRegExp( "(/dev/[sh]d[a-z]\\d?|hd\\d(,\\d)?|'\\(hd\\d(,\\d)?\\)')" ), this ) );
135
menu_deviceSuggestions = new KMenu( i18n( "Hard Disks" ), this );
136
menu_partitionSuggestions = new KMenu( i18n( "Partitions" ), this );
137
foreach( const GRUB::Misc::Device device, devices )
139
if ( !alreadyExists( device.device() ) )
140
menu_deviceSuggestions->addAction( device.device() )->setData( device.grubDevice() );
142
menu_partitionSuggestions->addAction( device.partition() + QString( " (" ) + device.mountPoint() + QString( ")" ) )->setData( device.grubPartition() );
144
menu_suggestions = new KMenu( this );
145
menu_suggestions->addMenu( menu_deviceSuggestions );
146
menu_suggestions->addMenu( menu_partitionSuggestions );
147
ui_hdd.kpushbutton_suggestions->setMenu( menu_suggestions );
148
ui_hdd.kpushbutton_suggestions->setIcon( KIcon( "tools-wizard" ) );
152
InstallAssistant::~InstallAssistant()
160
void InstallAssistant::setupConnections()
162
connect( menu_suggestions, SIGNAL( triggered( QAction * ) ), SLOT( suggestionTriggered( QAction * ) ) );
164
connect( ui_floppy.checkBox, SIGNAL( toggled( bool ) ), ui_floppy.kurlrequester, SLOT( setEnabled( bool ) ) );
166
bool InstallAssistant::alreadyExists( const QString &device )
168
foreach( QAction *action, menu_deviceSuggestions->actions() )
169
if ( action->text() == device )
173
void InstallAssistant::clearMountPoint()
175
if ( QDir().exists( "/mnt/rescue" ) )
177
KProcess::execute( "umount", QStringList( "/mnt/rescue" ) );
178
QDir().rmpath( "/mnt/rescue" );
182
void InstallAssistant::suggestionTriggered( QAction *action )
184
ui_hdd.klineedit->setText( action->data().toString().remove( "(" ).remove( ")" ) );
187
void InstallAssistant::hddResult( int exitCode )
192
KMessageBox::error( this, i18nc( "@info", "<para>The installation process failed to start.</para><para>Please check that <command>grub-install</command> is available and that you have sufficient permissions.</para>" ) );
195
KMessageBox::error( this, i18nc( "@info", "<para>The installation process crashed.</para>" ) );
198
KMessageBox::information( this, i18nc( "@info", "<para>The process was successfully completed.</para>" ) );
201
KMessageBox::sorry( this, i18nc( "@info", "The installation process returned exit code %1. An error could have occured.", exitCode ) );
205
( exitCode == 0 ? accept() : reject() );
208
void InstallAssistant::floppyResult( int exitCode )
213
KMessageBox::error( this, i18nc( "@info", "<para>The floppy formatting process failed.</para>" ) );
216
KMessageBox::error( this, i18nc( "@info", "<para>Unable to create temporary mountpoint.</para>" ) );
219
KMessageBox::error( this, i18nc( "@info", "<para>The floppy mounting process failed.</para>" ) );
222
KMessageBox::error( this, i18nc( "@info", "<para>The installation process failed.</para>" ) );
225
KMessageBox::error( this, i18nc( "@info", "<para>Copying the configuration file failed.</para>" ) );
228
KMessageBox::information( this, i18nc( "@info", "<para>The process was successfully completed.</para>" ) );
232
( exitCode == 0 ? accept() : reject() );
234
void InstallAssistant::setProgressLabelText( const QString &text )
236
if ( progressDialog )
237
progressDialog->setLabelText( text );
239
void InstallAssistant::setProgressValue( int value )
241
if ( progressDialog )
242
progressDialog->progressBar()->setValue( value );
245
void InstallAssistant::slotButtonClicked( int button )
248
if ( button == KDialog::User2 )
250
if ( currentPage() == choicePage )
252
setAppropriate( hardPage, ui_choice.radioButton_hard->isChecked() );
253
setAppropriate( floppyPage, ui_choice.radioButton_floppy->isChecked() );
255
KDialog::slotButtonClicked( button );
258
else if ( button == KDialog::User1 )
261
if ( currentPage() == hardPage )
263
if ( !ui_hdd.klineedit->hasAcceptableInput() )
265
KMessageBox::sorry( this, i18nc( "@info", "<para>Please enter a valid installation location.</para><para>If you are unsure what to enter, use the <interface>Suggestions</interface> list.</para>" ) );
270
HardDiskThread install;
271
install.setLocation( ui_hdd.klineedit->text() );
273
connect( &install, SIGNAL( exitCode( int ) ), SLOT( hddResult( int ) ) );
279
else if ( currentPage() == floppyPage )
281
RescueFloppyThread install;
282
if ( ui_floppy.checkBox->isChecked() && !ui_floppy.kurlrequester->url().isEmpty() )
283
install.setMenuFile( ui_floppy.kurlrequester->url().path() );
285
progressDialog = new KProgressDialog( this, i18nc( "@window:title", "Creating Rescue Floppy..." ), i18nc( "@label", "Starting..." ) );
286
progressDialog->setAutoClose( false );
287
progressDialog->setAllowCancel( false );
288
progressDialog->progressBar()->setMaximum( 100 );
290
connect( &install, SIGNAL( currentProcess( const QString & ) ), SLOT( setProgressLabelText( const QString & ) ) );
291
connect( &install, SIGNAL( currentStep( int ) ), SLOT( setProgressValue( int ) ) );
292
connect( &install, SIGNAL( finished() ), progressDialog, SLOT( close() ) );
293
connect( &install, SIGNAL( exitCode( int ) ), SLOT( floppyResult( int ) ) );
295
progressDialog->exec();
297
delete progressDialog;
301
KDialog::slotButtonClicked( button );