summaryrefslogtreecommitdiffstats
path: root/src/VSessionHandler.cpp
blob: bda85d06ce7de7735e07f6c66269273859536e53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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 "Defaults.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;
}