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;
58
kDebug() << "Reading input from" << menulst;
57
59
if ( KIO::NetAccess::download( menulst, tmpMenulst, 0 ) )
61
kDebug() << "Created temporary copy of" << menulst;
59
62
QFile fileMenulst( tmpMenulst );
60
63
if ( fileMenulst.open( QIODevice::ReadOnly ) )
65
kDebug() << "Opened temporary file";
62
66
stream.setDevice( &fileMenulst );
63
67
while ( !stream.atEnd() )
65
69
line = stream.readLine().trimmed();
66
//GRUB::ConfigFile::Settings identification
71
if ( line.startsWith( "### BEGIN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) )
73
tmp_automagic.setFirstEntry( entries->size() );
74
tmp_automagic.appendComment( line );
77
if ( line.startsWith( "### END DEBIAN AUTOMAGIC KERNELS LIST", Qt::CaseInsensitive ) )
79
tmp_automagic.appendComment( line );
80
tmp_automagic.setLastEntry( entries->size() - 1 );
81
settings->setAutomagic( tmp_automagic );
84
if ( line.startsWith( "#" ) )
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 );
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 );
67
97
if ( line.startsWith( "splashimage", Qt::CaseInsensitive ) )
69
99
settings->setSplashImage( line.section( separator, 1 ) );
94
124
settings->setHiddenMenu( true );
127
//*Shared* settings (can be both *General* and *Entry*)
97
128
if ( line.startsWith( "map", Qt::CaseInsensitive ) )
99
settings->addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
130
if ( entries->isEmpty() )
131
settings->addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
133
entries->last().addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
102
136
if ( line.startsWith( "color", Qt::CaseInsensitive ) )
104
settings->setColor( line.section( separator, 1 ) );
138
if ( entries->isEmpty() )
139
settings->setColor( line.section( separator, 1 ) );
141
entries->last().setColor( line.section( separator, 1 ) );
107
144
if ( line.startsWith( "password", Qt::CaseInsensitive ) )
109
settings->setPassword( line.section( separator, 1 ) );
146
if ( entries->isEmpty() )
147
settings->setPassword( line.section( separator, 1 ) );
149
entries->last().setPassword( line.section( separator, 1 ) );
112
//GRUB::ConfigFile::Entry identified
113
153
if ( line.startsWith( "title", Qt::CaseInsensitive ) )
115
155
GRUB::ConfigFile::Entry tmp_entry;
116
tmp_entry.setTitle( line.section( separator, 1 ) );
118
while ( !stream.atEnd() )
120
line = stream.readLine().trimmed();
121
if ( line.startsWith( "title", Qt::CaseInsensitive ) )
123
entries->append( tmp_entry );
125
tmp_entry.setTitle( line.section( separator, 1 ) );
127
if ( line.startsWith( "lock", Qt::CaseInsensitive ) )
129
tmp_entry.setLock( true );
132
if ( line.startsWith( "password", Qt::CaseInsensitive ) )
134
tmp_entry.setPassword( line.section( separator, 1 ) );
137
if ( line.startsWith( "root", Qt::CaseInsensitive ) )
139
tmp_entry.setRoot( line.section( separator, 1 ) );
142
if ( line.startsWith( "kernel", Qt::CaseInsensitive ) )
144
tmp_entry.setKernel( line.section( separator, 1 ) );
147
if ( line.startsWith( "initrd", Qt::CaseInsensitive ) )
149
tmp_entry.setInitrd( line.section( separator, 1 ) );
152
if ( line.startsWith( "map", Qt::CaseInsensitive ) )
154
tmp_entry.addMap( GRUB::ComplexCommand::Map( line.section( separator, 1 ) ) );
157
if ( line.startsWith( "color", Qt::CaseInsensitive ) )
159
tmp_entry.setColor( line.section( separator, 1 ) );
162
if ( line.startsWith( "chainloader", Qt::CaseInsensitive ) )
164
tmp_entry.setChainLoader( line.section( separator, 1 ) );
167
if ( line.startsWith( "savedefault", Qt::CaseInsensitive ) )
169
tmp_entry.setSaveDefault( true );
172
if ( line.startsWith( "makeactive", Qt::CaseInsensitive ) )
174
tmp_entry.setMakeActive( true );
178
156
entries->append( tmp_entry );
158
entries->last().setTitle( line.section( separator, 1 ) );
161
if ( line.startsWith( "lock", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
163
entries->last().setLock( true );
166
if ( line.startsWith( "root", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
168
entries->last().setRoot( line.section( separator, 1 ) );
171
if ( line.startsWith( "kernel", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
173
entries->last().setKernel( line.section( separator, 1 ) );
176
if ( line.startsWith( "initrd", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
178
entries->last().setInitrd( line.section( separator, 1 ) );
181
if ( line.startsWith( "chainloader", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
183
entries->last().setChainLoader( line.section( separator, 1 ) );
186
if ( line.startsWith( "savedefault", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
188
entries->last().setSaveDefault( true );
191
if ( line.startsWith( "makeactive", Qt::CaseInsensitive ) && !entries->last().title().isEmpty() )
193
entries->last().setMakeActive( true );
197
kDebug() << "Data extraction successful. Closing temporary file";
182
198
fileMenulst.close();
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 )
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() ) );
221
kDebug() << "Reading input from" << devicemap;
205
222
if ( KIO::NetAccess::download( devicemap, tmpDevicemap, 0 ) )
224
kDebug() << "Created temporary copy of" << devicemap;
207
225
QFile fileDevicemap( tmpDevicemap );
208
226
if ( fileDevicemap.open( QIODevice::ReadOnly ) )
228
kDebug() << "Opened temporary file";
210
229
stream.setDevice( &fileDevicemap );
211
230
while ( !stream.atEnd() )
274
298
kWarning() << fileMenulst.errorString();
275
299
KMessageBox::error( 0, fileMenulst.errorString() );
302
kDebug() << "Opened file for writing";
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;
328
for ( int i = 0; i < entries.size(); i++ )
330
if ( !settings.automagic().isEmpty() && settings.automagic().firstEntry() == i )
332
QStringList comments = settings.automagic().comments();
333
comments.removeLast();
334
while( comments.last().isEmpty() )
335
comments.removeLast();
337
foreach( const QString &comment, comments )
338
stream << comment << endl;
302
stream << "title " << entries->at( i ).title() << endl;
303
if ( entries->at( i ).lock() )
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;
366
if ( !settings.automagic().isEmpty() && settings.automagic().lastEntry() == i )
368
stream << settings.automagic().comments().last() << endl;
327
373
if ( !fileMenulst.finalize() )
329
375
kWarning() << fileMenulst.errorString();
330
376
KMessageBox::error( 0, fileMenulst.errorString() );
379
kDebug() << "Successfully wrote and finalized file";
334
381
if ( !menulst.isLocalFile() )
372
427
void FileTransactions::backup( const KUrl source, const KUrl target )
429
if ( !KIO::NetAccess::exists( source, false, 0 ) )
431
kWarning() << source << "does not exist!";
432
KMessageBox::error( 0, i18nc( "@info", "<filename>%1</filename> does not exist!", source.path() ) );
374
436
if ( KIO::NetAccess::exists( target, false, 0 ) )
376
if ( KIO::NetAccess::del( target, 0 ) )
378
if ( KIO::NetAccess::file_copy( source, target ) )
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!" ) );
385
kWarning() << KIO::NetAccess::lastErrorString();
386
KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
438
if ( !KIO::NetAccess::del( target, 0 ) )
391
440
kWarning() << KIO::NetAccess::lastErrorString();
392
441
KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
446
if ( KIO::NetAccess::file_copy( source, target ) )
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!" ) );
397
if ( KIO::NetAccess::file_copy( source, target ) )
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!" ) );
404
kWarning() << KIO::NetAccess::lastErrorString();
405
KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
453
kWarning() << KIO::NetAccess::lastErrorString();
454
KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
409
void FileTransactions::restoreBackup( const KUrl source, const KUrl target)
457
void FileTransactions::restore( const KUrl source, const KUrl target )
411
if ( KIO::NetAccess::del( target, 0 ) )
413
if ( KIO::NetAccess::file_copy( source, target ) )
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!" ) );
459
if ( !KIO::NetAccess::exists( source, false, 0 ) )
461
kWarning() << source << "does not exist!";
462
KMessageBox::error( 0, i18nc( "@info", "<filename>%1</filename> does not exist!", source.path() ) );
466
if ( KIO::NetAccess::exists( target, false, 0 ) )
468
if ( !KIO::NetAccess::del( target, 0 ) )
420
470
kWarning() << KIO::NetAccess::lastErrorString();
421
471
KMessageBox::error( 0, KIO::NetAccess::lastErrorString() );
476
if ( KIO::NetAccess::file_copy( source, target ) )
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!" ) );
426
483
kWarning() << KIO::NetAccess::lastErrorString();
445
void FileTransactions::moveEntry( const int source, const int target, const KUrl menulst, const GRUB::ConfigFile::Settings *settings, QVector<GRUB::ConfigFile::Entry> *entries )
447
if ( source != target )
449
GRUB::ConfigFile::Entry tmp_entry = (*entries)[source];
450
entries->remove( source );
451
entries->insert( target, tmp_entry );
452
fileOutput( menulst, settings, entries );
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 )
458
504
if ( path.isEmpty() )
459
505
return QString();
461
507
if ( KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath( path ) )
463
for ( int i = 0; i < devices.size() ; i++ )
509
foreach( const GRUB::Misc::Device device, devices )
465
if ( mp->mountPoint() == devices.at( i ).mountPoint() )
511
if ( mp->mountPoint() == device.mountPoint() )
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() );
470
return QString( path ).replace( mp->mountPoint(), devices.at( i ).grubPartition() );
516
return QString( path ).replace( mp->mountPoint(), device.grubPartition() );
474
520
return QString();
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 )
478
524
if ( path.isEmpty() )
479
525
return QString();
481
for ( int i = 0; i < devices.size() ; i++ )
527
foreach( const GRUB::Misc::Device device, devices )
483
if ( path.startsWith( devices.at( i ).grubPartition() ) )
529
if ( path.startsWith( device.grubPartition() ) )
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() );
534
return QString( path ).replace( device.grubPartition(), device.mountPoint() );
488
537
return QString();