#include <QtXml>
#include <QDir>
#include <QApplication>
#include <QProcess>
#include <QDate>
#include <QThread>
#include <QStringList>
#include <QIcon>
#include <QMessageBox>
#if 0
#include <QHostInfo> // available since Qt 4.7
#endif
#include <limits> // for HOST_NAME_MAX
#include <unistd.h> // for gethostname(), getuid()
#include <sys/types.h> // for getuid, getpwuid
#include <pwd.h> // for getpwuid
#include "globals.h"
#include "vsession.h"
#include "sessionsiconholder.h"
bool VSession::init(const QString& xml, const QString& baseDirPath) {
this->baseDirPath_ = baseDirPath;
_process = new QProcess();
return this->doc_.setContent(xml);
}
void VSession::addNodeWithAttribute(const QString& nodeName,
const QString& value,
const QString& attribute,
bool replace) {
QDomElement node =
this->doc_.namedItem("eintrag").namedItem(nodeName).toElement();
if (replace == false || node.isNull()) {
// create a new node
node = this->doc_.createElement(nodeName);
this->doc_.namedItem("eintrag").appendChild(node);
}
node.setAttribute(attribute, value);
}
QIcon VSession::icon() const {
QString icon(getAttribute("icon"));
SessionsIconHolder *iconHolder = SessionsIconHolder::get();
if (icon.startsWith("http://")) {
// try to load icon from url
QIcon url_icon(iconHolder->getIcon(QUrl(icon)));
if (!url_icon.isNull()) {
return url_icon;
}
}
if (!icon.isEmpty()) {
QIcon res_icon(iconHolder->getIcon(icon));
if (!res_icon.isNull()) {
return res_icon;
}
}
// Everything failed, try to guess the OS
QString os(getAttribute("os", "param").toLower());
if (!os.isEmpty()) {
QIcon osi = iconHolder->getIcon(os);
if (!osi.isNull())
return osi;
// These match vmware guestOS keywords mostly, extend for vbox...
if (os == "dos")
return iconHolder->getIcon("dos");
if (os.startsWith("windows7"))
return iconHolder->getIcon("win7");
if (os.startsWith("win31"))
return iconHolder->getIcon("win311");
if (os.startsWith("windows8"))
return iconHolder->getIcon("win8");
if (os.startsWith("win2000"))
return iconHolder->getIcon("win2000");
if (os.startsWith("winxp"))
return iconHolder->getIcon("winxp");
if (os.startsWith("debian"))
return iconHolder->getIcon("debian");
if (os.startsWith("ubuntu"))
return iconHolder->getIcon("ubuntu");
if (os.startsWith("win"))
return iconHolder->getIcon("windows");
if (os.contains("linux"))
return iconHolder->getIcon("linux");
}
// TODO: Maybe parse title of entry for an OS guess?
// Fallback to generic virtualizer icon
if (imgtype() == VMWARE)
return iconHolder->getIcon("vmware");
if (imgtype() == VBOX)
return iconHolder->getIcon("virtualbox");
return QIcon();
}
QString VSession::toXml() const {
QDomDocument doc;
doc.appendChild(doc.createElement("settings"));
doc.firstChild().appendChild(doc.importNode(doc_.documentElement(), true));
QDomNode xmlNode = doc.createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.insertBefore(xmlNode, doc.firstChild());
return doc.toString();
}
QString VSession::getAttribute(const QString &nodeName,
const QString &attribute) const {
QDomDocument doc = this->doc_;
QDomNode n = doc.namedItem("eintrag").namedItem(nodeName);
return this->doc_.namedItem("eintrag").namedItem(nodeName).toElement()
.attribute(attribute);
}
QList<QString> VSession::keywords() const {
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());
}
}
bool VSession::containsKeywords(const QList<QString>& keywords) const {
for (int j = 0; j < keywords.length(); ++j) {
if (!this->shortDescription().contains(keywords[j], Qt::CaseInsensitive)
&& !this->description().contains(keywords[j], Qt::CaseInsensitive)
&& !this->getAttribute("creator", "param").contains(keywords[j], Qt::CaseInsensitive)) {
bool match = false;
for (int i = 0; i < this->keywords().length(); ++i) {
if (this->keywords()[i].contains(keywords[j], Qt::CaseInsensitive)) {
match = true;
break;
}
}
if (!match)
return false;
}
}
return true;
}
QString VSession::getNodeText(const QString& nodeName) const {
return this->doc_.namedItem(nodeName).toText().data();
}
ImgType VSession::imgtype() const {
QString s(getAttribute("virtualmachine"));
if (s.compare("vmware") == 0) {
return VMWARE;
} else if (s.compare("virtualbox") == 0 || s.compare("vbox") == 0) {
return VBOX;
} else {
return OTHER;
}
}
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
// [YYYY-MM-DD]/[YYYY-MM-DD]
// eg. "1970-01-01/1971-01-01" from Jan 1st 1970 till Jan 1st 1971
// "/1971-01-01" till Jan 1st 1971
// "1970-01-01/" from Jan 1st 1970
// "/" allways
// note: invalid dates are treated as empty dates
QStringList list(value.split("/"));
QString from(list.value(0));
QString till(list.value(1));
QDate fromDate(QDate::fromString(from, Qt::ISODate));
QDate tillDate(QDate::fromString(till, Qt::ISODate));
QDate today(QDate::currentDate());
if (fromDate.isValid() && fromDate > today) {
// 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() << "'" << shortDescription() << "' not active. Reason: tillDate is in the past";
return false;
}
}
if (!pool.isEmpty()) {
QStringList pools = getAttribute("pools").split("\\s*,\\s*");
if (!pools.isEmpty() && !pools.contains(pool)) {
// pools does not contain pool
if (debugMode) qDebug() << "'" << shortDescription() << "' not active. Reason: '" << pool << "' is not part of active pool list (" << pools << ")";
return false;
}
}
return true;
}
bool VSession::isLocked() const {
// default to false
return getAttribute("locked").compare("true") == 0;
}
bool VSession::isValid() const {
// default to false
return QFile::exists(QString(this->baseDirPath_).append("/").append(getAttribute("image_name")));
}
int VSession::priority() const {
int prio = getAttribute("priority").toInt();
if (g_templateHandling == TEMPLATES_BUMP && isTemplate()) {
prio -= 500;
}
if (g_forLocationHandling != LOCATION_IGNORE && isForLocation()) {
prio -= 1000;
}
return prio;
}
void VSession::addUserAndHostname() {
QString username(getpwuid(geteuid())->pw_name);
this->addNodeWithAttribute("username", username);
// Qt >= 4.7 has <QHostInfo>
// QString hostname(QHostInfo::localHostName());
char hname[HOST_NAME_MAX + 1];
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"));
if (QFileInfo(image).isRelative()) {
// make path to image absolute
this->addNodeWithAttribute("image_path",
this->baseDirPath_ + "/" + image);
} else {
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.append("-%1").arg(qrand()));
this->doc_.namedItem("eintrag").insertBefore(computername, QDomNode());
}
void VSession::mergePoolXml() {
QDomDocument doc;
QString poolXmlFile = etcPath + "/vmchooser-" + pool + ".xml";
QFile file(poolXmlFile);
if (!file.open(QIODevice::ReadOnly)) {
return;
}
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
for (QDomElement envNode(doc.firstChildElement("environment"));
!envNode.isNull();
envNode = envNode.nextSiblingElement()) {
if (envNode.attribute("param") != pool) continue;
for (QDomElement typeNode(envNode.firstChildElement());
!typeNode.isNull();
typeNode = typeNode.nextSiblingElement()) {
QString type = typeNode.nodeName();
if (type != "shared_folders" &&
type != "printers" &&
type != "scanners") continue;
QDomElement destinationNode =
this->doc_.namedItem("eintrag").namedItem(type).toElement();
if (destinationNode.isNull()) {
// create new node
destinationNode = this->doc_.createElement(type);
this->doc_.namedItem("eintrag").appendChild(destinationNode);
}
for (QDomElement el(typeNode.firstChildElement());
!el.isNull();
el = el.nextSiblingElement()) {
destinationNode.appendChild(this->doc_.importNode(el, true));
}
}
}
}
bool VSession::run() const {
if (debugMode) {
qDebug() << "Sarting session " << this->getAttribute("short_description", "param") << " ...";
}
if (g_noVtx && is64Bit()) {
QMessageBox::warning(NULL, QObject::trUtf8("Warning"), QObject::trUtf8("The selected session is based on a 64 bit operating system, but this computer doesn't seem to support this (VT-x/AMD-V not supported by CPU, or disabled in BIOS). You will probably get an error message while the virtualizer is initializing."));
}
QString command = getAttribute("command");
if (!command.isEmpty()) {
return QProcess::startDetached(command);
}
VSession session = *this;
session.addUserAndHostname();
session.mergePoolXml();
// write xml to temporary file
QTemporaryFile tmpfile(QDir::tempPath() + "/vmchooser-XXXXXX.xml");
if (!tmpfile.open() ||
tmpfile.write(session.toXml().toUtf8()) == -1) {
return false;
}
tmpfile.close();
tmpfile.setAutoRemove(false);
_process->start(runVmScript, QStringList(tmpfile.fileName()));
QObject::connect(_process, SIGNAL(finished(int, QProcess::ExitStatus)), QApplication::instance(), SLOT(quit()));
if (_process->state() == QProcess::Starting || QProcess::Running)
return true;
else
return false;
}
int VSession::type() const {
return Session::VSESSION;
}
QList<Session*> VSession::readXmlFile(const QString& filepath) {
QList<Session*> retval;
QDomDocument doc;
QFile file(filepath);
QFile backup_file(xml_filename);
if (!file.open(QIODevice::ReadOnly)) {
if (debugMode) {
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();
} 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;
}
}
}
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")) {
QDomDocument dummy;
dummy.appendChild(dummy.importNode(el, true));
VSession* e = new VSession;
if (e->init(dummy.toString(), dirName) && e->isActive()) {
e->readKeywords();
retval.append(e);
}
}
return retval;
}
bool VSession::is64Bit() const {
return imgtype() == VMWARE && getAttribute("os").endsWith("64");
// TODO: vmbox, qemu-kvm, ...
}
QVariant VSession::foregroundRole() const {
if (!g_noVtx || !is64Bit())
return Session::foregroundRole();
return QColor(180, 180, 180);
}
bool VSession::operator<(const Session& other) const {
int p0 = this->priority();
int p1 = other.priority();
if (p0 < p1) return true;
if (p0 == p1) {
QString d0 = this->shortDescription();
QString d1 = other.shortDescription();
return d0.localeAwareCompare(d1) < 0;
}
return false;
}