summaryrefslogblamecommitdiffstats
path: root/src/VSessionHandler.cpp
blob: bce0beddb8ffd0a245ed6635a511758fb3b27072 (plain) (tree)

























                                                                           
                   


































































































































































                                                                                                                                          
/*
 * Copyright (c) 2010,2011 - RZ Uni Freiburg
 * Copyright (c) 2010,2011 - OpenSLX Project
 *
 * This program/file is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your feedback to feedback@openslx.org
 *
 * General information about OpenSLX - libChooser can be found under
 * http://openslx.org
 *
 */

#include "VSessionHandler.h"

#include <QList>
#include <QString>
#include <QFile>
#include <QDir>
#include <QDirIterator>
#include <QProcess>
#include <QDebug>
#include <QDomDocument>

#include "Config.h"
#include "Session.h"
#include "VSession.h"


VSessionHandler::VSessionHandler() {
	debugMode = true;
    _filterScript = QString(LIBCHOOSER_BIN_PATH "/xmlfilter.sh");
    _runVmScript = QString(LIBCHOOSER_BIN_PATH "/run-virt.sh");
    _printerScript = QString(LIBCHOOSER_ETC_BASE_PATH "/printer.sh");
    _scannerScript = QString(LIBCHOOSER_ETC_BASE_PATH "/scanner.sh");
    _confPath = QString(LIBCHOOSER_ETC_BASE_PATH);
    _env = QString();
}

VSessionHandler::~VSessionHandler() {
	// TODO Auto-generated destructor stub
}

void VSessionHandler::setEnv(QString env) {
	_env = env;
}

void VSessionHandler::setPrinterScript(QString printerScript) {
	_printerScript = printerScript;
}

void VSessionHandler::setScannerScript(QString scannerScript) {
	_scannerScript = scannerScript;
}

void VSessionHandler::setRunVmScript(QString runVmScript) {
	_runVmScript = runVmScript;
}

void VSessionHandler::setConfPath(QString confPath) {
	_confPath = confPath;
}

void VSessionHandler::setFilterScript(QString filterScript) {
	_filterScript = filterScript;
}

QString VSessionHandler::getEnv()  {
	return _env;

}
QString VSessionHandler::getPrinterScript() {
	return _printerScript;

}
QString VSessionHandler::getScannerScript()  {
	return _scannerScript;

}
QString VSessionHandler::getRunVmScript()  {
	return _runVmScript;

}
QString VSessionHandler::getConfPath()  {
	return _confPath;

}
QString VSessionHandler::getFilterScript()  {
	return _filterScript;
}

bool VSessionHandler::hasEnv() {
	return (_env.size() != 0);
}

/**
 * - calls xmlfilter.sh to glob a folder for xmls
 *   -> if no xmlfilter.sh is available, it globs for available xmls
 * - reads all xml files and creates for each its own VSession-struct
 */
QList<Session*> VSessionHandler::readXmlDir(const QString& path) {
    QList<Session*> retval;

    if (QFile::exists(_filterScript)) {
        // run filterScript
        // treat every output line as a filename and read it
        QProcess myFilterScript;
        myFilterScript.start(_filterScript, QStringList(path),
                           QIODevice::ReadOnly);
        myFilterScript.waitForFinished();
        while (!myFilterScript.atEnd()) {
            QString filename(myFilterScript.readLine());
            filename = filename.trimmed();
            if (QDir::isRelativePath(filename)) {
                filename.prepend(path + "/");
            }
            retval.append(readXmlFile(filename));
        }

        myFilterScript.close();
    } else {
        // iterate over all .xml files in directory <path> (and sub-directories)
        // and read them
        QDirIterator di(path,
                        QDirIterator::Subdirectories |
                        QDirIterator::FollowSymlinks);
        while (di.hasNext()) {
            if (!di.next().endsWith(".xml")) continue;

            if (!di.fileInfo().isReadable()) {
            	if (debugMode) qDebug() << "skip" << di.fileInfo().absoluteFilePath() << ": xml not readable, incorrect file permissions";
            	continue;
            }

            QList<Session*> vsessionTmp = readXmlFile(di.fileInfo().absoluteFilePath());

            if (vsessionTmp.isEmpty()) {
            	if (debugMode) qDebug() << "skip" << di.fileInfo().absoluteFilePath() << ": reading xml failed for some reason";
            	continue;
            }

            if (!vsessionTmp.first()->isValid()) {
            	if (debugMode) qDebug() << "skip" << vsessionTmp.first()->shortDescription() << ": vdi/vmdk missing";
            	continue;
            }

            if (!vsessionTmp.first()->isActive()) {
            	if (debugMode) qDebug() << "skip" << vsessionTmp.first()->shortDescription() << ": not active";
            	continue;
            }

            retval.append(vsessionTmp);
        }
    }
    return retval;
}

QList<Session*> VSessionHandler::readXmlFile(const QString& filepath) {
    QList<Session*> retval;

    QDomDocument doc;
    QFile file(filepath);
    if (!file.open(QIODevice::ReadOnly)) {
        // TODO: error message
        return retval;
    }
    if (!doc.setContent(&file)) {
        // TODO: error message
        file.close();
        return retval;
    }
    file.close();

    QString dirName(QFileInfo(filepath).dir().absolutePath());
    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(this);
        if (e->init(dummy.toString(), dirName)) {
            retval.append(e);
        }
    }
    return retval;
}