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

« back to all changes in this revision

Viewing changes to src/core/filetransactions.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:
43
43
#include <kmessagebox.h>
44
44
#include <kdebug.h>
45
45
 
46
 
void FileTransactions::fileInput( const KUrl menulst, const KUrl devicemap, GRUB::ConfigFile::Settings *settings, QVector<GRUB::ConfigFile::Entry> *entries, QVector<GRUB::Misc::Device> *devices )
 
46
void FileTransactions::fileInput( const KUrl menulst, const KUrl devicemap, GRUB::ConfigFile::Settings *const settings, QVector<GRUB::ConfigFile::Entry> *const entries, QVector<GRUB::Misc::Device> *const devices )
47
47
{
48
48
        settings->clear();
49
49
        entries->clear();
53
53
        QRegExp space( "\\s+" );
54
54
        QRegExp separator( "(\\s+|=|\\s+=\\s+)" );
55
55
        QString line, output, tmpMenulst, tmpDevicemap;
 
56
        GRUB::Misc::Automagic tmp_automagic;
56
57
 
 
58
        kDebug() << "Reading input from" << menulst;
57
59
        if ( KIO::NetAccess::download( menulst, tmpMenulst, 0 ) )
58
60
        {
 
61
                kDebug() << "Created temporary copy of" << menulst;
59
62
                QFile fileMenulst( tmpMenulst );
60
63
                if ( fileMenulst.open( QIODevice::ReadOnly ) )
61
64
                {
 
65
                        kDebug() << "Opened temporary file";
62
66
                        stream.setDevice( &fileMenulst );
63
67
                        while ( !stream.atEnd() )
64
68
                        {
65
69
                                line = stream.readLine().trimmed();
66
 
                                //GRUB::ConfigFile::Settings identification
 
70
                                //Comments
 
71
                                if ( line.startsWith( "### BEGIN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) )
 
72
                                {
 
73
                                        tmp_automagic.setFirstEntry( entries->size() );
 
74
                                        tmp_automagic.appendComment( line );
 
75
                                        continue;
 
76
                                }
 
77
                                if ( line.startsWith( "### END DEBIAN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) )
 
78
                                {
 
79
                                        tmp_automagic.appendComment( line );
 
80
                                        tmp_automagic.setLastEntry( entries->size() - 1 );
 
81
                                        settings->setAutomagic( tmp_automagic );
 
82
                                        continue;
 
83
                                }
 
84
                                if ( line.startsWith( "#" ) )
 
85
                                {
 
86
                                        if ( !tmp_automagic.isEmpty() && ( !tmp_automagic.comments().last().startsWith( "## ## End Default Options ##", Qt::CaseInsensitive ) || !tmp_automagic.comments().last().startsWith( "### END DEBIAN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) ) )
 
87
                                                tmp_automagic.appendComment( line );
 
88
                                        continue;
 
89
                                }
 
90
                                if ( line.isEmpty() )
 
91
                                {
 
92
                                        if ( !tmp_automagic.isEmpty() && ( !tmp_automagic.comments().last().startsWith( "## ## End Default Options ##", Qt::CaseInsensitive ) || !tmp_automagic.comments().last().startsWith( "### END DEBIAN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) ) )
 
93
                                                tmp_automagic.appendComment( line );
 
94
                                        continue;
 
95
                                }
 
96
                                //*General* settings
67
97
                                if ( line.startsWith( "splashimage", Qt::CaseInsensitive ) )
68
98
                                {
69
99
                                        settings->setSplashImage( line.section( separator, 1 ) );
94
124
                                        settings->setHiddenMenu( true );
95
125
                                        continue;
96
126
                                }
 
127
                                //*Shared* settings (can be both *General* and *Entry*)
97
128
                                if ( line.startsWith( "map", Qt::CaseInsensitive ) )
98
129
                                {
99
 
                                        settings->addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
 
130
                                        if ( entries->isEmpty()  )
 
131
                                                settings->addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
 
132
                                        else
 
133
                                                entries->last().addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
100
134
                                        continue;
101
135
                                }
102
136
                                if ( line.startsWith( "color", Qt::CaseInsensitive ) )
103
137
                                {
104
 
                                        settings->setColor( line.section( separator, 1 ) );
 
138
                                        if ( entries->isEmpty()  )
 
139
                                                settings->setColor( line.section( separator, 1 ) );
 
140
                                        else
 
141
                                                entries->last().setColor( line.section( separator, 1 ) );
105
142
                                        continue;
106
143
                                }
107
144
                                if ( line.startsWith( "password", Qt::CaseInsensitive ) )
108
145
                                {
109
 
                                        settings->setPassword( line.section( separator, 1 ) );
 
146
                                        if ( entries->isEmpty()  )
 
147
                                                settings->setPassword( line.section( separator, 1 ) );
 
148
                                        else
 
149
                                                entries->last().setPassword( line.section( separator, 1 ) );
110
150
                                        continue;
111
151
                                }
112
 
                                //GRUB::ConfigFile::Entry identified
 
152
                                //*Entry* settings
113
153
                                if ( line.startsWith( "title", Qt::CaseInsensitive ) )
114
154
                                {
115
155
                                        GRUB::ConfigFile::Entry tmp_entry;
116
 
                                        tmp_entry.setTitle( line.section( separator, 1 ) );
117
 
 
118
 
                                        while ( !stream.atEnd() )
119
 
                                        {
120
 
                                                line = stream.readLine().trimmed();
121
 
                                                if ( line.startsWith( "title", Qt::CaseInsensitive ) )
122
 
                                                {
123
 
                                                        entries->append( tmp_entry );
124
 
                                                        tmp_entry.clear();
125
 
                                                        tmp_entry.setTitle( line.section( separator, 1 ) );
126
 
                                                }
127
 
                                                if ( line.startsWith( "lock", Qt::CaseInsensitive ) )
128
 
                                                {
129
 
                                                        tmp_entry.setLock( true );
130
 
                                                        continue;
131
 
                                                }
132
 
                                                if ( line.startsWith( "password", Qt::CaseInsensitive ) )
133
 
                                                {
134
 
                                                        tmp_entry.setPassword( line.section( separator, 1 ) );
135
 
                                                        continue;
136
 
                                                }
137
 
                                                if ( line.startsWith( "root", Qt::CaseInsensitive ) )
138
 
                                                {
139
 
                                                        tmp_entry.setRoot( line.section( separator, 1 ) );
140
 
                                                        continue;
141
 
                                                }
142
 
                                                if ( line.startsWith( "kernel", Qt::CaseInsensitive ) )
143
 
                                                {
144
 
                                                        tmp_entry.setKernel( line.section( separator, 1 ) );
145
 
                                                        continue;
146
 
                                                }
147
 
                                                if ( line.startsWith( "initrd", Qt::CaseInsensitive ) )
148
 
                                                {
149
 
                                                        tmp_entry.setInitrd( line.section( separator, 1 ) );
150
 
                                                        continue;
151
 
                                                }
152
 
                                                if ( line.startsWith( "map", Qt::CaseInsensitive ) )
153
 
                                                {
154
 
                                                        tmp_entry.addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
155
 
                                                        continue;
156
 
                                                }
157
 
                                                if ( line.startsWith( "color", Qt::CaseInsensitive ) )
158
 
                                                {
159
 
                                                        tmp_entry.setColor( line.section( separator, 1 ) );
160
 
                                                        continue;
161
 
                                                }
162
 
                                                if ( line.startsWith( "chainloader", Qt::CaseInsensitive ) )
163
 
                                                {
164
 
                                                        tmp_entry.setChainLoader( line.section( separator, 1 ) );
165
 
                                                        continue;
166
 
                                                }
167
 
                                                if ( line.startsWith( "savedefault", Qt::CaseInsensitive ) )
168
 
                                                {
169
 
                                                        tmp_entry.setSaveDefault( true );
170
 
                                                        continue;
171
 
                                                }
172
 
                                                if ( line.startsWith( "makeactive", Qt::CaseInsensitive ) )
173
 
                                                {
174
 
                                                        tmp_entry.setMakeActive( true );
175
 
                                                        continue;
176
 
                                                }
177
 
                                        }
178
156
                                        entries->append( tmp_entry );
179
 
                                        tmp_entry.clear();
 
157
 
 
158
                                        entries->last().setTitle( line.section( separator, 1 ) );
 
159
                                        continue;
 
160
                                }
 
161
                                if ( line.startsWith( "lock", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
162
                                {
 
163
                                        entries->last().setLock( true );
 
164
                                        continue;
 
165
                                }
 
166
                                if ( line.startsWith( "root", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
167
                                {
 
168
                                        entries->last().setRoot( line.section( separator, 1 ) );
 
169
                                        continue;
 
170
                                }
 
171
                                if ( line.startsWith( "kernel", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
172
                                {
 
173
                                        entries->last().setKernel( line.section( separator, 1 ) );
 
174
                                        continue;
 
175
                                }
 
176
                                if ( line.startsWith( "initrd", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
177
                                {
 
178
                                        entries->last().setInitrd( line.section( separator, 1 ) );
 
179
                                        continue;
 
180
                                }
 
181
                                if ( line.startsWith( "chainloader", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
182
                                {
 
183
                                        entries->last().setChainLoader( line.section( separator, 1 ) );
 
184
                                        continue;
 
185
                                }
 
186
                                if ( line.startsWith( "savedefault", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
187
                                {
 
188
                                        entries->last().setSaveDefault( true );
 
189
                                        continue;
 
190
                                }
 
191
                                if ( line.startsWith( "makeactive", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
 
192
                                {
 
193
                                        entries->last().setMakeActive( true );
 
194
                                        continue;
180
195
                                }
181
196
                        }
 
197
                        kDebug() << "Data extraction successful. Closing temporary file";
182
198
                        fileMenulst.close();
183
199
                }
184
200
                else
187
203
                        KMessageBox::error( 0, fileMenulst.errorString() );
188
204
                }
189
205
                KIO::NetAccess::removeTempFile( tmpMenulst );
 
206
                kDebug() << "Removed temporary file";
190
207
        }
191
208
        else
192
209
        {
195
212
        }
196
213
 
197
214
        KMountPoint::List mountPoints = KMountPoint::currentMountPoints();
198
 
        for( KMountPoint::List::ConstIterator it = mountPoints.begin(); it != mountPoints.end(); ++it )
 
215
        foreach( const KMountPoint::Ptr mp, mountPoints )
199
216
        {
200
 
                const KMountPoint::Ptr mp = ( *it );
201
217
                if ( mp->mountedFrom().startsWith( "/dev" ) )
202
218
                        devices->append( GRUB::Misc::Device( mp->mountedFrom().remove( QRegExp( "\\d" ) ), mp->mountedFrom(), mp->mountPoint() ) );
203
219
        }
204
220
 
 
221
        kDebug() << "Reading input from" << devicemap;
205
222
        if ( KIO::NetAccess::download( devicemap, tmpDevicemap, 0 ) )
206
223
        {
 
224
                kDebug() << "Created temporary copy of" << devicemap;
207
225
                QFile fileDevicemap( tmpDevicemap );
208
226
                if ( fileDevicemap.open( QIODevice::ReadOnly ) )
209
227
                {
 
228
                        kDebug() << "Opened temporary file";
210
229
                        stream.setDevice( &fileDevicemap );
211
230
                        while ( !stream.atEnd() )
212
231
                        {
216
235
                                        if ( devices->at( i ).device() == line.section( space, 1, 1 ) )
217
236
                                        {
218
237
                                                (*devices)[i].setGrubDevice( line.section( space, 0, 0 ) );
219
 
                                                (*devices)[i].setGrubPartition( (*devices)[i].grubDevice().replace( QString( ")" ), QString( "," ) + QString().setNum( (*devices)[i].partition().right( 1 ).toInt() - 1 ) + QString( ")" ) ) );
 
238
                                                (*devices)[i].setGrubPartition( QString( (*devices)[i].grubDevice() ).replace( QString( ")" ), QString( "," ) + QString().setNum( (*devices)[i].partition().right( 1 ).toInt() - 1 ) + QString( ")" ) ) );
220
239
                                        }
221
240
                                }
222
241
                        }
 
242
                        kDebug() << "Data extraction successful. Closing temporary file";
223
243
                        fileDevicemap.close();
224
244
                }
225
245
                else
228
248
                        KMessageBox::error( 0, fileDevicemap.errorString() );
229
249
                }
230
250
                KIO::NetAccess::removeTempFile( tmpDevicemap );
 
251
                kDebug() << "Removed temporary file";
231
252
        }
232
253
        else
233
254
        {
246
267
                        if ( devices->at( i ).partition() == output.section( "\n", j, j ).section( space, 9, 9 ).remove( "../.." ).prepend( "/dev" ) )
247
268
                        {
248
269
                                (*devices)[i].setUuid( output.section( "\n", j, j ).section( space, 7, 7 ) );
 
270
                                continue;
249
271
                        }
250
272
                }
251
273
        }
252
274
 
253
275
}
254
 
void FileTransactions::fileOutput( const KUrl menulst, const GRUB::ConfigFile::Settings *settings, const QVector<GRUB::ConfigFile::Entry> *entries )
 
276
bool FileTransactions::fileOutput( const KUrl menulst, const GRUB::ConfigFile::Settings &settings, const QVector<GRUB::ConfigFile::Entry> &entries )
255
277
{
 
278
        kDebug() << "Writing output to" << menulst;
256
279
        QString tmpMenulst;
257
280
        if ( menulst.isLocalFile() )
258
281
                tmpMenulst = menulst.path();
263
286
                {
264
287
                        kWarning() << tmp.errorString();
265
288
                        KMessageBox::error( 0, tmp.errorString() );
266
 
                        return;
 
289
                        return false;
267
290
                }
268
291
                tmpMenulst = tmp.fileName();
 
292
                kDebug() << "Created temporary copy of" << menulst;
269
293
        }
270
294
 
271
295
        KSaveFile fileMenulst( tmpMenulst );
273
297
        {
274
298
                kWarning() << fileMenulst.errorString();
275
299
                KMessageBox::error( 0, fileMenulst.errorString() );
276
 
                return;
 
300
                return false;
277
301
        }
 
302
        kDebug() << "Opened file for writing";
278
303
 
 
304
//Settings
279
305
        QTextStream stream( &fileMenulst );
280
 
        if ( !settings->splashImage().isEmpty() )
281
 
                        stream << "splashimage " << settings->splashImage() << endl;
282
 
        if ( !settings->gfxMenu().isEmpty() )
283
 
                        stream << "gfxmenu " << settings->gfxMenu() << endl;
284
 
        if ( settings->hiddenMenu() )
 
306
        if ( !settings.splashImage().isEmpty() )
 
307
                        stream << "splashimage " << settings.splashImage() << endl;
 
308
        if ( !settings.gfxMenu().isEmpty() )
 
309
                        stream << "gfxmenu " << settings.gfxMenu() << endl;
 
310
        if ( settings.hiddenMenu() )
285
311
                        stream << "hiddenmenu" << endl;
286
 
        if ( settings->_default() != -1 )
287
 
                        stream << "default " << ( settings->_default() != -2 ? QString().setNum( settings->_default() ) : "saved" ) << endl;
288
 
        if ( settings->fallback() != -1 )
289
 
                        stream << "fallback " << settings->fallback() << endl;
290
 
        if ( settings->timeout() != -1 )
291
 
                        stream << "timeout " << settings->timeout() << endl;
292
 
        if ( !settings->maps().isEmpty() )
293
 
                for ( int i = 0; i < settings->maps().size(); i++ )
294
 
                        stream << "map " << settings->maps().at( i ) << endl;
295
 
        if ( !settings->color().isEmpty() )
296
 
                        stream << "color " << settings->color() << endl;
297
 
        if ( !settings->password().isEmpty() )
298
 
                        stream << "password " << settings->password() << endl;
299
 
        for ( int i = 0; i < entries->size(); i++ )
 
312
        if ( settings._default() != -1 )
 
313
                        stream << "default " << ( settings._default() != -2 ? QString().setNum( settings._default() ) : "saved" ) << endl;
 
314
        if ( settings.fallback() != -1 )
 
315
                        stream << "fallback " << settings.fallback() << endl;
 
316
        if ( settings.timeout() != -1 )
 
317
                        stream << "timeout " << settings.timeout() << endl;
 
318
        if ( !settings.maps().isEmpty() )
 
319
                for ( int i = 0; i < settings.maps().size(); i++ )
 
320
                        stream << "map " << settings.maps().at( i ) << endl;
 
321
        if ( !settings.color().isEmpty() )
 
322
                        stream << "color " << settings.color() << endl;
 
323
        if ( !settings.password().isEmpty() )
 
324
                        stream << "password " << settings.password() << endl;
 
325
        stream << endl;
 
326
 
 
327
//Entries
 
328
        for ( int i = 0; i < entries.size(); i++ )
300
329
        {
 
330
                if ( !settings.automagic().isEmpty() && settings.automagic().firstEntry() == i )
 
331
                {
 
332
                        QStringList comments = settings.automagic().comments();
 
333
                        comments.removeLast();
 
334
                        while( comments.last().isEmpty() )
 
335
                                comments.removeLast();
 
336
 
 
337
                        foreach( const QString &comment, comments )
 
338
                                stream << comment << endl;
301
339
                        stream << endl;
302
 
                        stream << "title " << entries->at( i ).title() << endl;
303
 
                if ( entries->at( i ).lock() )
 
340
                }
 
341
 
 
342
                        stream << "title " << entries.at( i ).title() << endl;
 
343
                if ( entries.at( i ).lock() )
304
344
                        stream << "lock" << endl;
305
 
                if ( !entries->at( i ).password().isEmpty() )
306
 
                        stream << "password " << entries->at( i ).password() << endl;
307
 
                if ( !entries->at( i ).root().isEmpty() )
308
 
                        stream << "root " << entries->at( i ).root() << endl;
309
 
                if ( !entries->at( i ).kernel().isEmpty() )
310
 
                        stream << "kernel " << entries->at( i ).kernel() << endl;
311
 
                if ( !entries->at( i ).initrd().isEmpty() )
312
 
                        stream << "initrd " << entries->at( i ).initrd() << endl;
313
 
                if ( !entries->at( i ).maps().isEmpty() )
314
 
                        for ( int j = 0; j < entries->at( i ).maps().size(); j++ )
315
 
                                stream << "map " << entries->at( i ).maps().at( j ) << endl;
316
 
                if ( !entries->at( i ).color().isEmpty() )
317
 
                        stream << "color " << entries->at( i ).color() << endl;
318
 
                if (!entries->at( i ).chainLoader().isEmpty())
319
 
                        stream << "chainloader " << entries->at( i ).chainLoader() << endl;
320
 
                if ( entries->at( i ).saveDefault() )
 
345
                if ( !entries.at( i ).password().isEmpty() )
 
346
                        stream << "password " << entries.at( i ).password() << endl;
 
347
                if ( !entries.at( i ).root().isEmpty() )
 
348
                        stream << "root " << entries.at( i ).root() << endl;
 
349
                if ( !entries.at( i ).kernel().isEmpty() )
 
350
                        stream << "kernel " << entries.at( i ).kernel() << endl;
 
351
                if ( !entries.at( i ).initrd().isEmpty() )
 
352
                        stream << "initrd " << entries.at( i ).initrd() << endl;
 
353
                if ( !entries.at( i ).maps().isEmpty() )
 
354
                        for ( int j = 0; j < entries.at( i ).maps().size(); j++ )
 
355
                                stream << "map " << entries.at( i ).maps().at( j ) << endl;
 
356
                if ( !entries.at( i ).color().isEmpty() )
 
357
                        stream << "color " << entries.at( i ).color() << endl;
 
358
                if (!entries.at( i ).chainLoader().isEmpty())
 
359
                        stream << "chainloader " << entries.at( i ).chainLoader() << endl;
 
360
                if ( entries.at( i ).saveDefault() )
321
361
                        stream << "savedefault" << endl;
322
 
                if ( entries->at( i ).makeActive() )
 
362
                if ( entries.at( i ).makeActive() )
323
363
                        stream << "makeactive" << endl;
 
364
                stream << endl;
 
365
 
 
366
                if ( !settings.automagic().isEmpty() && settings.automagic().lastEntry() == i )
 
367
                {
 
368
                        stream << settings.automagic().comments().last() << endl;
 
369
                        stream << endl;
 
370
                }
324
371
        }
325
 
        stream.flush();
326
372
 
327
373
        if ( !fileMenulst.finalize() )
328
374
        {
329
375
                kWarning() << fileMenulst.errorString();
330
376
                KMessageBox::error( 0, fileMenulst.errorString() );
331
 
                return;
 
377
                return false;
332
378
        }
 
379
        kDebug() << "Successfully wrote and finalized file";
333
380
 
334
381
        if ( !menulst.isLocalFile() )
335
382
        {
337
384
                {
338
385
                        kWarning() << KIO::NetAccess::lastErrorString();
339
386
                        KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
340
 
                        return;
 
387
                        return false;
341
388
                }
 
389
                kDebug() << "Successfully copied temporary file's contents to" << menulst;
342
390
        }
 
391
        return true;
343
392
}
344
 
QString FileTransactions::view( const KUrl file )
 
393
const QString FileTransactions::view( const KUrl file )
345
394
{
 
395
        kDebug() << "Opening file" << file << "for viewing";
346
396
        QString tmpFile, contents;
347
397
        if ( KIO::NetAccess::download( file, tmpFile, 0 ) )
348
398
        {
 
399
                kDebug() << "Created temporary copy of" << file;
349
400
                QFile _file( tmpFile );
350
401
                if ( _file.open( QIODevice::ReadOnly ) )
351
402
                {
 
403
                        kDebug() << "Opened temporary file";
352
404
                        QTextStream stream( &_file );
353
405
                        contents = stream.readAll();
 
406
                        kDebug() << "Data extraction successful. Closing temporary file";
 
407
                        _file.close();
354
408
                }
355
409
                else
356
410
                {
359
413
                        return QString();
360
414
                }
361
415
                KIO::NetAccess::removeTempFile( tmpFile );
 
416
                kDebug() << "Removed temporary file";
362
417
                return contents;
363
418
        }
364
419
        else
371
426
 
372
427
void FileTransactions::backup( const KUrl source, const KUrl target )
373
428
{
 
429
        if ( !KIO::NetAccess::exists( source, false, 0 ) )
 
430
        {
 
431
                kWarning() << source << "does not exist!";
 
432
                KMessageBox::error( 0, i18nc( "@info", "<filename>%1</filename> does not exist!", source.path() ) );
 
433
                return;
 
434
        }
 
435
 
374
436
        if ( KIO::NetAccess::exists( target, false, 0 ) )
375
437
        {
376
 
                if ( KIO::NetAccess::del( target, 0 ) )
377
 
                {
378
 
                        if ( KIO::NetAccess::file_copy( source, target ) )
379
 
                        {
380
 
                                kDebug() << QString( "Operation was successfully completed! %1 has been backed up to %2." ).arg( source.path() ).arg( target.path() );
381
 
                                KMessageBox::information( 0, i18n( "Operation was successfully completed! %1 has been backed up to %2.", source.path(), target.path() ), i18n( "Success!" ) );
382
 
                        }
383
 
                        else
384
 
                        {
385
 
                                kWarning() << KIO::NetAccess::lastErrorString();
386
 
                                KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
387
 
                        }
388
 
                }
389
 
                else
 
438
                if ( !KIO::NetAccess::del( target, 0 ) )
390
439
                {
391
440
                        kWarning() << KIO::NetAccess::lastErrorString();
392
441
                        KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
 
442
                        return;
393
443
                }
394
444
        }
 
445
 
 
446
        if ( KIO::NetAccess::file_copy( source, target ) )
 
447
        {
 
448
                kDebug() << QString( "Operation was successfully completed! %1 has been backed up to %2." ).arg( source.path() ).arg( target.path() );
 
449
                KMessageBox::information( 0, i18nc( "@info", "Operation was successfully completed! <filename>%1</filename> has been backed up to <filename>%2</filename>.", source.path(), target.path() ), i18nc( "@title:window", "Success!" ) );
 
450
        }
395
451
        else
396
452
        {
397
 
                if ( KIO::NetAccess::file_copy( source, target ) )
398
 
                {
399
 
                        kDebug() << QString( "Operation was successfully completed! %1 has been backed up to %2." ).arg( source.path() ).arg( target.path() );
400
 
                        KMessageBox::information( 0, i18n( "Operation was successfully completed! %1 has been backed up to %2.", source.path(), target.path() ), i18n( "Success!" ) );
401
 
                }
402
 
                else
403
 
                {
404
 
                        kWarning() << KIO::NetAccess::lastErrorString();
405
 
                        KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
406
 
                }
 
453
                kWarning() << KIO::NetAccess::lastErrorString();
 
454
                KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
407
455
        }
408
456
}
409
 
void FileTransactions::restoreBackup( const KUrl source, const KUrl target)
 
457
void FileTransactions::restore( const KUrl source, const KUrl target )
410
458
{
411
 
        if ( KIO::NetAccess::del( target, 0 ) )
412
 
        {
413
 
                if ( KIO::NetAccess::file_copy( source, target ) )
414
 
                {
415
 
                        kDebug() << QString( "Operation was successfully completed! %1 has been restored to %2." ).arg( source.path() ).arg( target.path() );
416
 
                        KMessageBox::information( 0, i18n( "Operation was successfully completed! %1 has been restored to %2.", source.path(), target.path() ), i18n( "Success!" ) );
417
 
                }
418
 
                else
 
459
        if ( !KIO::NetAccess::exists( source, false, 0 ) )
 
460
        {
 
461
                kWarning() << source << "does not exist!";
 
462
                KMessageBox::error( 0, i18nc( "@info", "<filename>%1</filename> does not exist!", source.path() ) );
 
463
                return;
 
464
        }
 
465
 
 
466
        if ( KIO::NetAccess::exists( target, false, 0 ) )
 
467
        {
 
468
                if ( !KIO::NetAccess::del( target, 0 ) )
419
469
                {
420
470
                        kWarning() << KIO::NetAccess::lastErrorString();
421
471
                        KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
 
472
                        return;
422
473
                }
423
474
        }
 
475
 
 
476
        if ( KIO::NetAccess::file_copy( source, target ) )
 
477
        {
 
478
                kDebug() << QString( "Operation was successfully completed! %1 has been restored to %2." ).arg( source.path() ).arg( target.path() );
 
479
                KMessageBox::information( 0, i18nc( "@info", "Operation was successfully completed! <filename>%1</filename> has been restored to <filename>%2</filename>.", source.path(), target.path() ), i18nc( "@title:window", "Success!" ) );
 
480
        }
424
481
        else
425
482
        {
426
483
                kWarning() << KIO::NetAccess::lastErrorString();
433
490
        if ( KIO::NetAccess::del( backup, 0 ) )
434
491
        {
435
492
                kDebug() << backup << "was successfully deleted.";
436
 
                KMessageBox::information( 0, i18n( "Operation was successfully completed! %1 has been deleted.", backup.path() ) );
 
493
                KMessageBox::information( 0, i18nc( "@info", "Operation was successfully completed! <filename>%1</filename> has been deleted.", backup.path() ) );
437
494
        }
438
495
        else
439
496
        {
442
499
        }
443
500
}
444
501
 
445
 
void FileTransactions::moveEntry( const int source, const int target, const KUrl menulst, const GRUB::ConfigFile::Settings *settings, QVector<GRUB::ConfigFile::Entry> *entries )
446
 
{
447
 
        if ( source != target )
448
 
        {
449
 
                GRUB::ConfigFile::Entry tmp_entry = (*entries)[source];
450
 
                entries->remove( source );
451
 
                entries->insert( target, tmp_entry );
452
 
                fileOutput( menulst, settings, entries );
453
 
        }
454
 
}
455
 
 
456
 
QString FileTransactions::convertToGRUBPath( const QString path, const QVector<GRUB::Misc::Device> devices )
 
502
QString FileTransactions::convertToGRUBPath( const QString path, const QVector<GRUB::Misc::Device> &devices )
457
503
{
458
504
        if ( path.isEmpty() )
459
505
                return QString();
460
506
 
461
507
        if ( KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( path ) )
462
508
        {
463
 
                for ( int i = 0; i < devices.size() ; i++ )
 
509
                foreach( const GRUB::Misc::Device device, devices )
464
510
                {
465
 
                        if ( mp->mountPoint() == devices.at( i ).mountPoint() )
 
511
                        if ( mp->mountPoint() == device.mountPoint() )
466
512
                        {
467
 
                                if ( mp->mountPoint() == "/" )
468
 
                                        return QString( path ).prepend( devices.at( i ).grubPartition() );
 
513
                                if ( mp->mountPoint() == "/" ) //Unix-specific check
 
514
                                        return QString( path ).prepend( device.grubPartition() );
469
515
                                else
470
 
                                        return QString( path ).replace( mp->mountPoint(), devices.at( i ).grubPartition() );
 
516
                                        return QString( path ).replace( mp->mountPoint(), device.grubPartition() );
471
517
                        }
472
518
                }
473
519
        }
474
520
        return QString();
475
521
}
476
 
QString FileTransactions::convertToGenericPath( const QString path, const QVector<GRUB::Misc::Device> devices )
 
522
QString FileTransactions::convertToGenericPath( const QString path, const QVector<GRUB::Misc::Device> &devices )
477
523
{
478
524
        if ( path.isEmpty() )
479
525
                return QString();
480
526
 
481
 
        for ( int i = 0; i < devices.size() ; i++ )
 
527
        foreach( const GRUB::Misc::Device device, devices )
482
528
        {
483
 
                if ( path.startsWith( devices.at( i ).grubPartition() ) )
 
529
                if ( path.startsWith( device.grubPartition() ) )
484
530
                {
485
 
                        return QString( path ).replace( devices.at( i ).grubPartition(), devices.at( i ).mountPoint() );
 
531
                        if ( device.mountPoint() == "/" ) //Unix-specific check
 
532
                                return QString( path ).remove( device.grubPartition() );
 
533
                        else
 
534
                                return QString( path ).replace( device.grubPartition(), device.mountPoint() );
486
535
                }
487
536
        }
488
537
        return QString();