pkg://klilo2-0.3.3.tar.gz:341088/
klilo2-0.3.3/
klilo2/liloconfig.cpp
downloads
#include "liloconfig.h"
LiloConfig::LiloConfig() { init(); }
LiloConfig::~LiloConfig() { _osList.clear(); }
//LiloConfig::LiloConfig(const LiloConfig &config){}
QStringList LiloConfig::getImageList() {
QStringList imagelist;
for(ImageConfig *item=_osList.first();item!=NULL;item=_osList.next()) {
imagelist << item->getName();
}
return imagelist;
}
bool LiloConfig::removeImage(unsigned int index) {
// Removes an Image from the QList
return _osList.remove(index);
}
void LiloConfig::appendImage(ImageConfig& image) {
// Adds an image to the QList
_osList.append(new ImageConfig);
*_osList.current() = image;
}
ImageConfig LiloConfig::getImage(unsigned int index) {
// Get an Image to edit it
return *_osList.at(index);
}
bool LiloConfig::setDefaultImage(unsigned int index) {
// Sets an image as default. First entry in the QList is default
ImageConfig* image = _osList.take(index);
return _osList.insert(0, image);
}
void LiloConfig::setImage(ImageConfig &image, unsigned int index) {
// Writes back an image to the QList
*_osList.at(index) = image;
}
void LiloConfig::clearImageList() {
// Clears the QList
if(!_osList.isEmpty()) {
_osList.clear();
init();
}
}
LiloConfig& LiloConfig::operator=(const LiloConfig &config){
if(this == &config) return *this;
_bootPartition = config._bootPartition;
_bootMessage = config._bootMessage;
_vga = config._vga;
_rfs = config._rfs;
_backup = config._backup;
_menutitle = config._menutitle;
_menuscheme = config._menuscheme;
_globalPasswd = config._globalPasswd;
_LiloVersion = config._LiloVersion;
_enableMenu = config._enableMenu;
// _enableBackup = config._enableBackup;
// _enablePasswd = config._enablePasswd;
_linear = config._linear;
_lba = config._lba;
_compact = config._compact;
_prompt = config._prompt;
_changerules = config._changerules;
_nowarn = config._nowarn;
_forcebackup = config._forcebackup;
_fixtable = config._fixtable;
_ignoretable = config._ignoretable;
_verbose = config._verbose;
_verb = config._verb;
_bootDelay = config._bootDelay;
_osList.clear();
QList <ImageConfig> list = config._osList;
for(ImageConfig *item=list.first(); item!=NULL; item=list.next()) {
_osList.append(new ImageConfig);
*_osList.current() = *item;
}
return *this;
}
QString LiloConfig::SplitLine(QString line, int pos) const {
line = line.mid(pos+1,line.length()-pos+1);
line = line.simplifyWhiteSpace();
return line;
}
QString LiloConfig::getVersion() const {
// For some reasons I didn't want to use KProcess... for just
// reading a single line
if(system("lilo -V > /dev/null") == 0) {
QString qstr;
system("lilo -V > version.tmp");
QFile f("version.tmp");
if ( f.open(IO_ReadOnly) ) { // file opened successfully
QTextStream t( &f ); // use a text stream
qstr = t.readLine(); // line of text excluding '\n'
f.close();
}
int pos = qstr.find("2");
qstr = SplitLine(qstr, pos-1);
system("rm version.tmp");
return qstr;
}
else {
KMessageBox::error(0,"Error runing LILO! Please check your configuration",i18n("Lilo error"), false);
return "";
}
}
bool LiloConfig::enableMenu(){
int pos;
QString line = _LiloVersion;
if( line.contains("21") ) {
if( (pos=line.find(".") ) > 0) line = SplitLine(line,pos);
bool ok=false;
unsigned int number = line.toUInt(&ok,10);
if( number >= 4 ) return true;
else
return false;
}
else
return false;
}
bool LiloConfig::readConfig(QString filename) {
clearImageList();
QString line;
int pos;
QFile f( filename );
if(!f.open(IO_ReadOnly)) {
qWarning( "Could not open file " + filename );
return false;
}
QTextStream stream(&f);
while(!stream.eof()) {
line = stream.readLine();
line = line.simplifyWhiteSpace();
if((pos=line.find("#")) > -1) line.truncate(pos);
if(line.isEmpty()) continue;
// start of global section
if(line.contains( "linear",false )) { _linear = true; continue; }
if(line.contains( "prompt",false )) { _prompt = true; continue; }
if(line.contains( "compact",false )) { _compact = true; continue; }
if(line.contains( "lba32",false )) { _lba = true; continue; }
if(line.contains( "read-only",false )) { _rfs = "read-only"; continue; }
if(line.contains( "nowarn",false )) { _nowarn = true; continue; }
if(line.contains( "fix-table",false )) { _fixtable = true; continue; }
if(line.contains( "ignore-table",false )) { _ignoretable = true; continue; }
if(line.contains( "change-rules",false )) { _changerules = true; continue; }
if((pos=line.find("=")) > 0) {
if(line.contains("vga",false) > 0) {
_vga = SplitLine(line,pos);
continue;
}
if((line.contains("boot",false) > 0)&&(line.contains("dev") > 0)) {
_bootPartition = SplitLine(line,pos);
continue;
}
if(line.contains("message",false) > 0) {
_bootMessage = SplitLine(line,pos);
continue;
}
if(line.contains("timeout",false) > 0) {
_bootDelay = SplitLine(line,pos).toInt();
continue;
}
if(line.contains("force-backup",false) > 0) {
_forcebackup = true;
// _backup = SplitLine(line,pos);
continue;
}
if(line.contains("backup",false) > 0) {
_backup = SplitLine(line,pos);
continue;
}
/*
if(line.contains("verbose",false) > 0) {
cout << "line(8): " << "detected: verbose=" << SplitLine(line,pos) << endl;
_verbose = true;
_verb = SplitLine(line,pos).toInt();
continue;
}
*/
if(line.contains("menu-title",false) > 0) {
_menutitle = SplitLine(line,pos);
_enableMenu = true;
continue;
}
if(line.contains("menu-scheme",false) > 0) {
_menuscheme = SplitLine(line,pos);
_enableMenu = true;
continue;
}
// end of global section
// start of per-image section
if((line.contains("image",false) > 0) || (line.contains("other",false) > 0)) {
if(line.contains("image",false) > 0) {
_osList.append(new ImageConfig);
_osList.current()->setKernel(SplitLine(line,pos));
_osList.current()->setIsLinux(true);
}
else if(line.contains("other",false) > 0) {
_osList.append(new ImageConfig);
_osList.current()->setPartition(SplitLine(line,pos));
_osList.current()->setIsLinux(false);
}
while(!stream.eof()) {
line = stream.readLine();
line = line.simplifyWhiteSpace();
if((pos=line.find("#")) > -1) line.truncate(pos);
if(line.isEmpty()) continue;
pos=line.find("=");
if(line.contains("image",false) > 0) {
_osList.append(new ImageConfig);
_osList.current()->setKernel(SplitLine(line,pos));
_osList.current()->setIsLinux(true);
continue;
}
else if(line.contains("other",false) > 0) {
_osList.append(new ImageConfig);
_osList.current()->setPartition(SplitLine(line,pos));
_osList.current()->setIsLinux(false);
continue;
}
else if(line.contains("root",false) > 0) {
_osList.current()->setPartition(SplitLine(line,pos));
continue;
}
else if(line.contains("label",false) > 0) {
_osList.current()->setName(SplitLine(line,pos));
continue;
}
else if(line.contains("append",false) > 0) {
_osList.current()->setAppend(SplitLine(line,pos));
continue;
}
else if(line.contains("alias",false) > 0) {
_osList.current()->setAlias(SplitLine(line,pos));
continue;
}
else if(line.contains("password",false) > 0) {
_osList.current()->setPassword(SplitLine(line,pos));
continue;
}
else if(line.contains("ramdisk",false) > 0) {
_osList.current()->setRamDisk(SplitLine(line,pos).toInt());
continue;
}
else if(line.contains("optional",false) > 0) {
_osList.current()->setIsOptional(true);
continue;
}
} // while
} // if
// end of per-image section
}
}
_modified = false;
return true;
}
bool LiloConfig::writeConfig() {
QFile f("/etc/lilo.conf");
if(!f.open(IO_WriteOnly)) {
qWarning( "Could not open file lilo.conf" );
return false;
}
QTextStream stream(&f);
stream << "# This file was generated by KLilo2 V" << VERSION << endl;
stream << "boot = " << _bootPartition << endl;
stream << "prompt" << endl;
stream << _rfs << endl;
stream << "vga = " << _vga << endl;
stream << "timeout = " << _bootDelay << endl;
if( _bootMessage != "" ) stream << "message=" << _bootMessage << endl;
if( _lba ) stream << "lba32" << endl;
if( _compact ) stream << "compact" << endl;
if( _linear ) stream << "linear" << endl;
if( _changerules ) stream << "change-rules" << endl;
if( _nowarn ) stream << "nowarn" << endl;
if( _fixtable ) stream << "fix-table" << endl;
if( _ignoretable ) stream << "ignore-table" << endl;
// if( _verbose) stream << "verbose = " << _verb << endl;
if( _backup != "" ) stream << "backup = " << _backup << endl;
// if( _backup == "" && _forcebackup) stream << "force-backup = " << "/boot/boot.old" << endl;
if( _forcebackup ) stream << "force-backup = " << "/boot/boot.old" << endl;
if( _globalPasswd != "" ) stream << "password = " << _globalPasswd << endl;
if( _enableMenu )
stream << "menu-title = " << _menutitle << "\nmenu-scheme = "
<< _menuscheme << endl;
stream << "\n# End LILO global Section" << endl;
ImageConfig *item;
for(item = _osList.first(); item != 0; item = _osList.next()) {
if(item->isLinux()) {
stream << "#" << endl;
stream << " image = " << item->getKernel() << endl;
if(item->getPartition() != "")
stream << " root = " << item->getPartition() << endl;
stream << " label = " << item->getName() << endl;
if( item->isOptional() ) stream << " optional" << endl;
if( item->getAlias() != "" ) stream << " alias = " << item->getAlias() << endl;
if( item->getAppend() != "" ) stream << " append = " << item->getAppend() << endl;
if( item->getPassword() != "" ) stream << " password = " << item->getPassword() << endl;
if(( item->getPassword() != "" ) && item->isRestricted()) stream << " restricted" << endl;
if( item->getRamDisk()) stream << " ramdisk = " << item->getRamDisk() << endl;
}
else {
stream << "#" << endl;
stream << " other = " << item->getPartition() << endl;
stream << " label = " << item->getName() << endl;
stream << " table = " << getMBR() << endl;
}
}
stream << "\n# End LILO Configuration" << endl;
_modified = false;
return true;
}
QString LiloConfig::getMBR() const {
// This function searches for the Master Boot Record on
// '/dev/hda' or on '/dev/sda' (IDE or SCSI).
// Return value: MBR of type QString
// Fault: if MBR not found --> NULL-pointer
QString MBR = "/dev/hda";
QFile f(MBR);
if(f.open(IO_ReadOnly)) return MBR;
else {
MBR = "/dev/sda";
f.setName(MBR);
if(f.open(IO_ReadOnly)) return MBR;
else
return NULL;
}
}
QString LiloConfig::getRootPartition() const {
// This function searches for the root partition in the file
// fstab which is placed in '/etc' by using the strtok function
// Return value: root device of type QString
// Fault: if file not found --> NULL-pointer
QFile in("/etc/fstab");
QTextStream stream(&in);
QString line, device, mountpoint;
if(!in.open(IO_ReadOnly)) {
qWarning( "Could not open file /etc/fstab!" );
return NULL;
}
while(!stream.eof()) {
line = stream.readLine();
device = strtok((char*)(line.data()), "\t ");
mountpoint = strtok(0, "\t ");
if(mountpoint == "/") return device;
}
return NULL; // mount point not found
}
void LiloConfig::init() {
_bootPartition = "";
_bootMessage = "";
_vga = "";
_rfs = "";
_backup = "";
_menutitle = "";
_menuscheme = "";
_globalPasswd = "";
_LiloVersion = getVersion();
_enableMenu = enableMenu();
_linear = false;
_lba = false;
_compact = false;
_prompt = false;
_changerules = false;
_nowarn = false;
_forcebackup = false;
_fixtable = false;
_ignoretable = false;
_verbose = false;
_verb = 1;
_bootDelay = 100;
_osList.setAutoDelete(true);
_modified = false;
}