summaryrefslogtreecommitdiffstats
path: root/src/vsession.cpp
diff options
context:
space:
mode:
authorSimon Rettberg2014-05-04 12:56:35 +0200
committerSimon Rettberg2014-05-04 12:56:35 +0200
commit5103900bfeb0feced5b58d68924479bfc73d8ec5 (patch)
tree34a0d8145b6f3585e3cc4ae3a2f5a70e1a5db984 /src/vsession.cpp
parentshut up eclipse about missing break (diff)
downloadvmchooser2-5103900bfeb0feced5b58d68924479bfc73d8ec5.tar.gz
vmchooser2-5103900bfeb0feced5b58d68924479bfc73d8ec5.tar.xz
vmchooser2-5103900bfeb0feced5b58d68924479bfc73d8ec5.zip
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 :>
Diffstat (limited to 'src/vsession.cpp')
-rw-r--r--src/vsession.cpp166
1 files changed, 88 insertions, 78 deletions
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<QString> 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<QString>& 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 <QHostInfo>
// 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 <eintrag>
// 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<Session*> 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);
}
}