~ubuntu-branches/ubuntu/saucy/kgrubeditor/saucy

« back to all changes in this revision

Viewing changes to src/install.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Stalcup
  • Date: 2008-08-02 08:47:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080802084756-4doy9cyfc63kzaic
Tags: 0.7-0ubuntu1
* New upstream release
* bumped standards version to 3.8.0 (no change)
* Updated debian/cdbs for intrepid paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Konstantinos Smanis                             *
 
3
 *   kon.smanis@gmail.com                                                  *
 
4
 *                                                                         *
 
5
 *   This file is part of KGRUBEditor.                                     *
 
6
 *                                                                         *
 
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.                                   *
 
11
 *                                                                         *
 
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.                          *
 
16
 *                                                                         *
 
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
 ***************************************************************************/
 
23
 
 
24
//Own
 
25
#include "install.h"
 
26
 
 
27
//Qt
 
28
#include <qdir.h>
 
29
 
 
30
//KDE
 
31
#include <kmenu.h>
 
32
#include <kmessagebox.h>
 
33
#include <kprocess.h>
 
34
#include <kprogressdialog.h>
 
35
 
 
36
void HardDiskThread::setLocation( const QString &location )
 
37
{
 
38
        m_location = location;
 
39
}
 
40
void HardDiskThread::run()
 
41
{
 
42
        KProcess install;
 
43
        install.setProgram( "grub-install", QStringList( m_location ) );
 
44
 
 
45
        emit exitCode( install.execute() );
 
46
}
 
47
 
 
48
void RescueFloppyThread::setMenuFile( const QString &menuFile )
 
49
{
 
50
        m_menuFile = menuFile;
 
51
}
 
52
void RescueFloppyThread::run()
 
53
{
 
54
        emit currentProcess( i18nc( "@info", "Formatting Floppy..." ) );
 
55
        emit currentStep( 20 );
 
56
        if ( KProcess::execute( "mke2fs", QStringList( "/dev/fd0" ) ) != 0 )
 
57
        {
 
58
                emit exitCode( -1 );
 
59
                return;
 
60
        }
 
61
 
 
62
        emit currentProcess( i18nc( "@info", "Creating Mountpoint..." ) );
 
63
        emit currentStep( 40 );
 
64
        if ( !QDir().exists( "/mnt/rescue" ) )
 
65
        {
 
66
                if ( !QDir().mkpath( "/mnt/rescue" ) )
 
67
                {
 
68
                        emit exitCode( -2 );
 
69
                        return;
 
70
                }
 
71
        }
 
72
 
 
73
        emit currentProcess( i18nc( "@info", "Mounting Floppy..." ) );
 
74
        emit currentStep( 60 );
 
75
        if ( KProcess::execute( "mount", QStringList() << "-t" << "ext2" << "/dev/fd0" << "/mnt/rescue" ) != 0 )
 
76
        {
 
77
                emit exitCode( -3 );
 
78
                return;
 
79
        }
 
80
 
 
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 )
 
84
        {
 
85
                emit exitCode( -4 );
 
86
                return;
 
87
        }
 
88
 
 
89
        if ( !m_menuFile.isEmpty() )
 
90
        {
 
91
                emit currentProcess( i18nc( "@info", "Copying Menu File..." ) );
 
92
                emit currentStep( 100 );
 
93
                if ( !QFile::copy( m_menuFile, "/mnt/rescue/boot/grub/menu.lst" ) )
 
94
                {
 
95
                        emit exitCode( -5 );
 
96
                        return;
 
97
                }
 
98
        }
 
99
 
 
100
        emit exitCode( 0 );
 
101
}
 
102
 
 
103
InstallAssistant::InstallAssistant( const QVector<GRUB::Misc::Device> &devices, QWidget *parent ) : KAssistantDialog( parent )
 
104
{
 
105
//SETUP UI
 
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 ) );
 
111
 
 
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" ) );
 
124
//PROPERTY SETUP
 
125
        m_devices = new QVector<GRUB::Misc::Device>;
 
126
        m_devices = &devices;
 
127
 
 
128
        clearMountPoint();
 
129
//MISC UI SETUP
 
130
        ui_choice.label_iconHard->setPixmap( KIcon( "drive-harddisk" ).pixmap( 48, 48 ) );
 
131
        ui_choice.label_iconFloppy->setPixmap( KIcon( "media-floppy" ).pixmap( 48, 48 ) );
 
132
 
 
133
        ui_hdd.klineedit->setValidator( new QRegExpValidator( QRegExp( "(/dev/[sh]d[a-z]\\d?|hd\\d(,\\d)?|'\\(hd\\d(,\\d)?\\)')" ), this ) );
 
134
 
 
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 )
 
138
        {
 
139
                if ( !alreadyExists( device.device() ) )
 
140
                        menu_deviceSuggestions->addAction( device.device() )->setData( device.grubDevice() );
 
141
 
 
142
                menu_partitionSuggestions->addAction( device.partition() + QString( " (" ) + device.mountPoint() + QString( ")" ) )->setData( device.grubPartition() );
 
143
        }
 
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" ) );
 
149
//SETUP CONNECTIONS
 
150
        setupConnections();
 
151
}
 
152
InstallAssistant::~InstallAssistant()
 
153
{
 
154
        m_devices = 0;
 
155
        delete m_devices;
 
156
 
 
157
        clearMountPoint();
 
158
}
 
159
 
 
160
void InstallAssistant::setupConnections()
 
161
{
 
162
        connect( menu_suggestions, SIGNAL( triggered( QAction * ) ), SLOT( suggestionTriggered( QAction * ) ) );
 
163
 
 
164
        connect( ui_floppy.checkBox, SIGNAL( toggled( bool ) ), ui_floppy.kurlrequester, SLOT( setEnabled( bool ) ) );
 
165
}
 
166
bool InstallAssistant::alreadyExists( const QString &device )
 
167
{
 
168
        foreach( QAction *action, menu_deviceSuggestions->actions() )
 
169
                if ( action->text() == device )
 
170
                        return true;
 
171
        return false;
 
172
}
 
173
void InstallAssistant::clearMountPoint()
 
174
{
 
175
        if ( QDir().exists( "/mnt/rescue" ) )
 
176
        {
 
177
                KProcess::execute( "umount", QStringList( "/mnt/rescue" ) );
 
178
                QDir().rmpath( "/mnt/rescue" );
 
179
        }
 
180
}
 
181
 
 
182
void InstallAssistant::suggestionTriggered( QAction *action )
 
183
{
 
184
        ui_hdd.klineedit->setText( action->data().toString().remove( "(" ).remove( ")" ) );
 
185
}
 
186
 
 
187
void InstallAssistant::hddResult( int exitCode )
 
188
{
 
189
        switch( exitCode )
 
190
        {
 
191
                case -2:
 
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>" ) );
 
193
                break;
 
194
                case -1:
 
195
                        KMessageBox::error( this, i18nc( "@info", "<para>The installation process crashed.</para>" ) );
 
196
                break;
 
197
                case 0:
 
198
                        KMessageBox::information( this, i18nc( "@info", "<para>The process was successfully completed.</para>" ) );
 
199
                break;
 
200
                default:
 
201
                        KMessageBox::sorry( this, i18nc( "@info", "The installation process returned exit code %1. An error could have occured.", exitCode ) );
 
202
                break;
 
203
        }
 
204
 
 
205
        ( exitCode == 0 ? accept() : reject() );
 
206
}
 
207
 
 
208
void InstallAssistant::floppyResult( int exitCode )
 
209
{
 
210
        switch( exitCode )
 
211
        {
 
212
                case -1:
 
213
                        KMessageBox::error( this, i18nc( "@info", "<para>The floppy formatting process failed.</para>" ) );
 
214
                break;
 
215
                case -2:
 
216
                        KMessageBox::error( this, i18nc( "@info", "<para>Unable to create temporary mountpoint.</para>" ) );
 
217
                break;
 
218
                case -3:
 
219
                        KMessageBox::error( this, i18nc( "@info", "<para>The floppy mounting process failed.</para>" ) );
 
220
                break;
 
221
                case -4:
 
222
                        KMessageBox::error( this, i18nc( "@info", "<para>The installation process failed.</para>" ) );
 
223
                break;
 
224
                case -5:
 
225
                        KMessageBox::error( this, i18nc( "@info", "<para>Copying the configuration file failed.</para>" ) );
 
226
                break;
 
227
                case 0:
 
228
                        KMessageBox::information( this, i18nc( "@info", "<para>The process was successfully completed.</para>" ) );
 
229
                break;
 
230
        }
 
231
 
 
232
        ( exitCode == 0 ? accept() : reject() );
 
233
}
 
234
void InstallAssistant::setProgressLabelText( const QString &text )
 
235
{
 
236
        if ( progressDialog )
 
237
                progressDialog->setLabelText( text );
 
238
}
 
239
void InstallAssistant::setProgressValue( int value )
 
240
{
 
241
        if ( progressDialog )
 
242
                progressDialog->progressBar()->setValue( value );
 
243
}
 
244
 
 
245
void InstallAssistant::slotButtonClicked( int button )
 
246
{
 
247
        //Next
 
248
        if ( button == KDialog::User2 )
 
249
        {
 
250
                if ( currentPage() == choicePage )
 
251
                {
 
252
                        setAppropriate( hardPage, ui_choice.radioButton_hard->isChecked() );
 
253
                        setAppropriate( floppyPage, ui_choice.radioButton_floppy->isChecked() );
 
254
                }
 
255
                KDialog::slotButtonClicked( button );
 
256
        }
 
257
        //Finish
 
258
        else if ( button == KDialog::User1 )
 
259
        {
 
260
                //hard disk
 
261
                if ( currentPage() == hardPage )
 
262
                {
 
263
                        if ( !ui_hdd.klineedit->hasAcceptableInput() )
 
264
                        {
 
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>" ) );
 
266
                                return;
 
267
                        }
 
268
                        else
 
269
                        {
 
270
                                HardDiskThread install;
 
271
                                install.setLocation( ui_hdd.klineedit->text() );
 
272
 
 
273
                                connect( &install, SIGNAL( exitCode( int ) ), SLOT( hddResult( int ) ) );
 
274
 
 
275
                                install.start();
 
276
                                install.wait();
 
277
                        }
 
278
                }
 
279
                else if ( currentPage() == floppyPage )
 
280
                {
 
281
                        RescueFloppyThread install;
 
282
                        if ( ui_floppy.checkBox->isChecked() && !ui_floppy.kurlrequester->url().isEmpty() )
 
283
                                install.setMenuFile( ui_floppy.kurlrequester->url().path() );
 
284
 
 
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 );
 
289
 
 
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 ) ) );
 
294
                        install.start();
 
295
                        progressDialog->exec();
 
296
 
 
297
                        delete progressDialog;
 
298
                }
 
299
        }
 
300
        else
 
301
                KDialog::slotButtonClicked( button );
 
302
}