From 5103900bfeb0feced5b58d68924479bfc73d8ec5 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sun, 4 May 2014 12:56:35 +0200 Subject: Lots of fixes, cleanup, refactoring * Added command line option to set the base path where images can be accessed locally, in case NFS/CIFS is used. * Sorted command line options in source file to make it easier to add new options (prevent accidental shortopt collisions). * Disable "My Courses" button if the list is empty. * Added some TODO notes :> --- src/vsession.cpp | 166 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 88 insertions(+), 78 deletions(-) (limited to 'src/vsession.cpp') diff --git a/src/vsession.cpp b/src/vsession.cpp index c2b1aef..7656868 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -41,9 +41,9 @@ void VSession::addNodeWithAttribute(const QString& nodeName, QString VSession::icon() const { QString icon(getAttribute("icon")); if (icon.isEmpty()) { - if (imgtype() == VMWARE) icon = "vmware"; - else if (imgtype() == VBOX) icon = "virtualbox"; - else icon = "none"; + if (imgtype() == VMWARE) icon = "vmware"; + else if (imgtype() == VBOX) icon = "virtualbox"; + else icon = "none"; } return icon; } @@ -70,34 +70,34 @@ QString VSession::getAttribute(const QString &nodeName, } QList VSession::keywords() const { - return this->keywords_; + return this->keywords_; } void VSession::readKeywords() { - QDomNode keywordsNode = this->doc_.namedItem("eintrag").namedItem("keywords"); - for (QDomElement el(keywordsNode.firstChildElement("keyword")); - !el.isNull(); - el = el.nextSiblingElement("keyword")) { - this->keywords_.append(el.text()); - } + QDomNode keywordsNode = this->doc_.namedItem("eintrag").namedItem("keywords"); + for (QDomElement el(keywordsNode.firstChildElement("keyword")); + !el.isNull(); + el = el.nextSiblingElement("keyword")) { + this->keywords_.append(el.text()); + } } bool VSession::containsKeywords(const QList& keywords) const { - for (int j = 0; j < keywords.length(); ++j) { - bool keywordFlag = true; - if (!this->shortDescription().contains(keywords[j], Qt::CaseInsensitive) - && !this->description().contains(keywords[j], Qt::CaseInsensitive) - && !this->getAttribute("creator", "param").contains(keywords[j], Qt::CaseInsensitive)) { - keywordFlag = false; - for (int i = 0; i < this->keywords().length(); ++i) { - if (this->keywords()[i].contains(keywords[j], Qt::CaseInsensitive)) { - keywordFlag = true; - } - } - if (!keywordFlag) return false; - } - } - return true; + for (int j = 0; j < keywords.length(); ++j) { + bool keywordFlag = true; + if (!this->shortDescription().contains(keywords[j], Qt::CaseInsensitive) + && !this->description().contains(keywords[j], Qt::CaseInsensitive) + && !this->getAttribute("creator", "param").contains(keywords[j], Qt::CaseInsensitive)) { + keywordFlag = false; + for (int i = 0; i < this->keywords().length(); ++i) { + if (this->keywords()[i].contains(keywords[j], Qt::CaseInsensitive)) { + keywordFlag = true; + } + } + if (!keywordFlag) return false; + } + } + return true; } QString VSession::getNodeText(const QString& nodeName) const { @@ -120,6 +120,7 @@ bool VSession::isActive() const { QString value(getAttribute("active")); if (value.compare("false") == 0) { + if (debugMode) qDebug() << "'" << shortDescription() << "' not active. Reason: active == false"; return false; } else if (value.count("/") == 1) { // try to interpret value as date range @@ -140,13 +141,13 @@ bool VSession::isActive() const { if (fromDate.isValid() && fromDate > today) { // fromDate is in the future - if (debugMode) qDebug() << "Not active. Reason: fromDate is in the future"; + if (debugMode) qDebug() << "'" << shortDescription() << "' not active. Reason: fromDate is in the future"; return false; } if (tillDate.isValid() && tillDate < today) { // tillDate is in the past - if (debugMode) qDebug() << "Not active. Reason: tillDate is in the past"; + if (debugMode) qDebug() << "'" << shortDescription() << "' not active. Reason: tillDate is in the past"; return false; } } @@ -155,7 +156,7 @@ bool VSession::isActive() const { QStringList pools = getAttribute("pools").split("\\s*,\\s*"); if (!pools.isEmpty() && !pools.contains(pool)) { // pools does not contain pool - if (debugMode) qDebug() << "Not active. Reason: vsession is not part of active env"; + if (debugMode) qDebug() << "'" << shortDescription() << "' not active. Reason: '" << pool << "' is not part of active pool list (" << pools << ")"; return false; } } @@ -184,8 +185,12 @@ void VSession::addUserAndHostname() { // Qt >= 4.7 has // QString hostname(QHostInfo::localHostName()); char hname[HOST_NAME_MAX + 1]; - gethostname(hname, HOST_NAME_MAX); - QString hostname(hname); + QString hostname; + if (gethostname(hname, HOST_NAME_MAX) == 0) { + hostname = QString::fromUtf8(hname); + } else { + hostname = QString::fromUtf8("unknown-%1").arg(qrand()); + } this->addNodeWithAttribute("hostname", hostname); QString image(this->getAttribute("image_name")); @@ -194,13 +199,13 @@ void VSession::addUserAndHostname() { this->addNodeWithAttribute("image_path", this->baseDirPath_ + "/" + image); } else { - this->addNodeWithAttribute("image_path", image); + this->addNodeWithAttribute("image_path", image); } // insert computername as the first child of // bootpgm needs computername within the first 500 bytes QDomElement computername(doc_.createElement("computername")); - computername.setAttribute("param", hostname); + computername.setAttribute("param", hostname.append("-%1").arg(qrand())); this->doc_.namedItem("eintrag").insertBefore(computername, QDomNode()); } @@ -251,9 +256,9 @@ void VSession::mergePoolXml() { } bool VSession::run() const { - if (debugMode) { - qDebug() << "Sarting session " << this->getAttribute("short_description", "param") << " ..."; - } + if (debugMode) { + qDebug() << "Sarting session " << this->getAttribute("short_description", "param") << " ..."; + } QString command = getAttribute("command"); if (!command.isEmpty()) { @@ -296,62 +301,67 @@ QList VSession::readXmlFile(const QString& filepath) { if (!file.open(QIODevice::ReadOnly)) { if (debugMode) { - qDebug() << "Cannot read file: " << file.fileName(); + qDebug() << "Cannot read file: " << file.fileName(); } return retval; } if (!doc.setContent(&file)) { - if (debugMode) { - qDebug() << "XML file not valid: " << file.fileName(); - } - - file.close(); - - // try to use backup file - if (!backup_file.open(QIODevice::ReadOnly)) { - if (debugMode) { - qDebug() << "Cannot read backup file " << xml_filename << " either"; - } - return retval; - } - - if (!doc.setContent(&backup_file)) { - if (debugMode) { - qDebug() << "XML file not valid: " << backup_file.fileName(); - } - backup_file.close(); - return retval; - } - - if (debugMode) { - qDebug() << "Used backup file " << xml_filename; - } - - backup_file.close(); + if (debugMode) { + qDebug() << "XML file not valid: " << file.fileName(); + } + + file.close(); + + // try to use backup file + if (!backup_file.open(QIODevice::ReadOnly)) { + if (debugMode) { + qDebug() << "Cannot read backup file " << xml_filename << " either"; + } + return retval; + } + + if (!doc.setContent(&backup_file)) { + if (debugMode) { + qDebug() << "XML file not valid: " << backup_file.fileName(); + } + backup_file.close(); + return retval; + } + + if (debugMode) { + qDebug() << "Used backup file " << xml_filename; + } + + backup_file.close(); } else { - file.close(); - - // file is valid --> create backup file - QFile::remove(xml_filename); - QFile::rename(filepath, xml_filename); - if (!QFile::setPermissions(xml_filename, QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther)) { - if (debugMode) { - qDebug() << "Could not change permissions of file: " << news_backup_filename; - } - } + file.close(); + + // file is valid --> create backup file + QFile::remove(xml_filename); + QFile::rename(filepath, xml_filename); + if (!QFile::setPermissions(xml_filename, QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther)) { + if (debugMode) { + qDebug() << "Could not change permissions of file: " << news_backup_filename; + } + } } - QString dirName(QFileInfo(xml_filename).dir().absolutePath()); + QString dirName; + if (basePath.isEmpty()) { + dirName = QFileInfo(xml_filename).dir().absolutePath(); + } else { + dirName = basePath; + } QDomElement settingsNode = doc.firstChildElement("settings"); for (QDomElement el(settingsNode.firstChildElement("eintrag")); - !el.isNull(); - el = el.nextSiblingElement("eintrag")) { + !el.isNull(); + el = el.nextSiblingElement("eintrag")) { QDomDocument dummy; dummy.appendChild(dummy.importNode(el, true)); VSession* e = new VSession; - if (e->init(dummy.toString(), dirName)) { - e->readKeywords(); + if (e->init(dummy.toString(), dirName) && e->isActive()) { + e->readKeywords(); retval.append(e); } } -- cgit v1.2.3-55-g7522