diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 45 | ||||
-rw-r--r-- | src/Defaults.h | 25 | ||||
-rw-r--r-- | src/Session.h | 50 | ||||
-rw-r--r-- | src/SessionTreeItem.cpp | 65 | ||||
-rw-r--r-- | src/SessionTreeItem.h | 49 | ||||
-rw-r--r-- | src/SessionTreeModel.cpp | 179 | ||||
-rw-r--r-- | src/SessionTreeModel.h | 50 | ||||
-rw-r--r-- | src/VSession.cpp | 356 | ||||
-rw-r--r-- | src/VSession.h | 136 | ||||
-rw-r--r-- | src/VSessionHandler.cpp | 190 | ||||
-rw-r--r-- | src/VSessionHandler.h | 62 | ||||
-rw-r--r-- | src/XSession.cpp | 150 | ||||
-rw-r--r-- | src/XSession.h | 93 | ||||
-rw-r--r-- | src/XSessionHandler.cpp | 47 | ||||
-rw-r--r-- | src/XSessionHandler.h | 29 |
15 files changed, 1526 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..d81ebf8 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,45 @@ +include(${QT_USE_FILE}) + +set(QT_USE_QTXML TRUE) +set(QT_USE_QTSVG TRUE) + +include(${QT_USE_FILE}) + +set(LCHOOSER_MOC_HEADERS "SessionTreeModel.h") +set(LCHOOSER_MOC_SOURCES "SessionTreeModel.cpp") +set(LCHOOSER_HEADERS "Session.h" "SessionTreeItem.h" "VSession.h" "VSessionHandler.h" "XSession.h" "XSessionHandler.h") +set(LCHOOSER_SOURCES "SessionTreeItem.cpp" "VSession.cpp" "VSessionHandler.cpp" "XSession.cpp" "XSessionHandler.cpp") + +QT4_WRAP_CPP(LCHOOSER_MOC_SOURCES ${LCHOOSER_MOC_HEADERS}) + +add_library (Chooser_static STATIC ${LCHOOSER_SOURCES} ${LCHOOSER_MOC_SOURCES} ${LCHOOSER_HEADERS} ${LCHOOSER_MOC_HEADERS}) +set_target_properties (Chooser_static PROPERTIES OUTPUT_NAME Chooser) +add_library (Chooser_shared SHARED ${LCHOOSER_SOURCES} ${LCHOOSER_MOC_SOURCES} ${LCHOOSER_HEADERS} ${LCHOOSER_MOC_HEADERS}) +set_target_properties (Chooser_shared PROPERTIES OUTPUT_NAME Chooser) +set(MY_OWN_INSTALL_PREFIX "/usr" CACHE PATH "Prefix prepended to install directories") +set(CMAKE_INSTALL_PREFIX "${MY_OWN_INSTALL_PREFIX}" CACHE INTERNAL "Prefix prepended to install directories" FORCE) +install(TARGETS Chooser_static Chooser_shared + LIBRARY DESTINATION lib COMPONENT libs + ARCHIVE DESTINATION lib COMPONENT libs +) + +install(FILES ${LCHOOSER_MOC_HEADERS} ${LCHOOSER_HEADERS} DESTINATION include COMPONENT dev) + +set(CPACK_COMPONENTS_ALL libs dev) +set(CPACK_COMPONENT_LIBS_DISPLAY_NAME "libChooser") +set(CPACK_COMPONENT_DEV_DISPLAY_NAME "libChooser-dev") +set(CPACK_COMPONENT_LIBS_DESCRIPTION "libChooser") +set(CPACK_COMPONENT_DEV_DESCRIPTION "libChooser development headers") +set(CPACK_COMPONENT_LIBS_GROUP "core") +set(CPACK_COMPONENT_DEV_GROUP "dev") +set(CPACK_COMPONENT_DEV_DEPENDS libs) + +set(CPACK_GENERATOR "DEB") +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Sebastian Schmelzer <ssc@xss.nu>") #required +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "emuchooser is ...") +set(CPACK_PACKAGE_DESCRIPTION "...") +set(CPACK_DEBIAN_PACKAGE_DEPENDS "libqtcore4") +set(CPACK_DEBIAN_PACKAGE_SECTION "") +set(CPACK_DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}1.1.1+lenny1") + +INCLUDE(CPack) diff --git a/src/Defaults.h b/src/Defaults.h new file mode 100644 index 0000000..d168248 --- /dev/null +++ b/src/Defaults.h @@ -0,0 +1,25 @@ +/* + * 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 + * + */ + +#ifndef CHOOSERDEFAULTS_H_ +#define CHOOSERDEFAULTS_H_ + +#define LIBCHOOSER_BIN_PATH "/var/opt/openslx/bin" +#define LIBCHOOSER_ETC_BASE_PATH "/etc/vmchooser" +#define LIBCHOOSER_VMPATH "/var/lib/virt" + +#define LIBCHOOSER_X_SESSIONS_PATH "/usr/share/xsessions" + +#endif /* CHOOSERDEFAULTS_H_ */ diff --git a/src/Session.h b/src/Session.h new file mode 100644 index 0000000..4b0ad69 --- /dev/null +++ b/src/Session.h @@ -0,0 +1,50 @@ +/* + * 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 + * + */ + +#ifndef VMCHOOSER_SESSION_H_ +#define VMCHOOSER_SESSION_H_ + +class QString; + +class Session { + public: + virtual ~Session() {} + + virtual bool isActive() const = 0; + virtual bool isLocked() const = 0; + virtual bool isValid() const = 0; + virtual int priority() const = 0; + virtual QString shortDescription() const = 0; + virtual QString description() const = 0; + virtual QString icon() const = 0; + + virtual QString title() const = 0; + virtual QString screenshot() const = 0; + virtual QString os() const = 0; + virtual QString emulator() const = 0; + virtual QString author() const = 0; + virtual QString creationDate () const = 0; + virtual QString changeDate() const = 0; + + virtual bool run() const = 0; + virtual int type() const = 0; + + virtual bool operator<(const Session& s) const = 0; + + const static int XSESSION = 0; + const static int VSESSION = 1; + +}; +#endif /*VMCHOOSER_SESSION_H_*/ diff --git a/src/SessionTreeItem.cpp b/src/SessionTreeItem.cpp new file mode 100644 index 0000000..ed7ead7 --- /dev/null +++ b/src/SessionTreeItem.cpp @@ -0,0 +1,65 @@ +/* + * 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 "SessionTreeItem.h" + +SessionTreeItem::SessionTreeItem(const Session* session, + SessionTreeItem *parent) + : parent_(parent), session_(session) { +} + +SessionTreeItem::SessionTreeItem(const QString& text, SessionTreeItem *parent) + : parent_(parent), session_(NULL), text_(text) { +} + +SessionTreeItem::~SessionTreeItem() { + qDeleteAll(children_); +} + +void SessionTreeItem::appendChild(SessionTreeItem *item) { + children_.append(item); +} + +SessionTreeItem *SessionTreeItem::child(int row) { + return children_.value(row); +} + +int SessionTreeItem::childCount() const { + return children_.count(); +} + +int SessionTreeItem::columnCount() const { + return 1; +} + +SessionTreeItem *SessionTreeItem::parent() { + return parent_; +} + +int SessionTreeItem::row() const { + if (parent_) { + return parent_->children_.indexOf(const_cast<SessionTreeItem*>(this)); + } + + return 0; +} + +const Session* SessionTreeItem::session() const { + return session_; +} + +const QString SessionTreeItem::text() const { + return text_; +} diff --git a/src/SessionTreeItem.h b/src/SessionTreeItem.h new file mode 100644 index 0000000..1c41195 --- /dev/null +++ b/src/SessionTreeItem.h @@ -0,0 +1,49 @@ +/* + * 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 + * + */ + +#ifndef SESSIONTREEITEM_H +#define SESSIONTREEITEM_H + +#include <QList> +#include <QVariant> + +// class ... instead of include? +#include "Session.h" +#include <QString> + +class SessionTreeItem { + public: + SessionTreeItem(const Session* session, SessionTreeItem *parent = 0); + SessionTreeItem(const QString& text, SessionTreeItem *parent = 0); + ~SessionTreeItem(); + + void appendChild(SessionTreeItem *child); + + SessionTreeItem *child(int row); + int childCount() const; + int columnCount() const; + int row() const; + SessionTreeItem *parent(); + const Session* session() const; + const QString text() const; + + private: + QList<SessionTreeItem*> children_; + SessionTreeItem *parent_; + const Session *session_; + const QString text_; +}; + +#endif // SESSIONTREEITEM_H diff --git a/src/SessionTreeModel.cpp b/src/SessionTreeModel.cpp new file mode 100644 index 0000000..273d050 --- /dev/null +++ b/src/SessionTreeModel.cpp @@ -0,0 +1,179 @@ +/* + * 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 <QFileInfo> +#include <QIcon> +#include <QResource> +#include <QString> + +#include "SessionTreeItem.h" +#include "SessionTreeModel.h" + + +SessionTreeModel::SessionTreeModel(QObject *parent) + : QAbstractItemModel(parent) { + root_ = new SessionTreeItem("dummy"); +} + +SessionTreeModel::~SessionTreeModel() { + delete root_; +} + +int SessionTreeModel::columnCount(const QModelIndex& /*parent*/) const { + return 1; +} + +int SessionTreeModel::rowCount(const QModelIndex &parent) const { + SessionTreeItem* parentItem; + if (parent.column() > 0) { + return 0; + } + + if (!parent.isValid()) { + parentItem = root_; + } else { + parentItem = static_cast<SessionTreeItem*>(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +QVariant SessionTreeModel::data(const QModelIndex &index, int role) const { + if (!index.isValid()) { + return QVariant(); + } + + SessionTreeItem* item = + static_cast<SessionTreeItem*>(index.internalPointer()); + + const Session* s = item->session(); + if (s) { + if (role == Qt::DisplayRole) + return s->shortDescription(); + if (role == Qt::ToolTipRole) + return s->description(); + if (role == Qt::DecorationRole) { + // TODO: use cache for icons + if (index.column() == 0) { // TODO: is this line needed? + QString icon(s->icon()); + + if (QFileInfo(icon).isAbsolute()) { + // try to load icon from file + return QIcon(icon); + } else { + // try to load icon from QResource + if (QResource(":" + icon.toLower() + ".svg").isValid()) { + return QIcon(":" + icon.toLower() + ".svg"); + } else if (QResource(":icons/" + icon.toLower()).isValid()) { + return QIcon(":icons/" + icon.toLower()); + } else if (QResource(":" + icon.toLower()).isValid()) { + return QIcon(":" + icon.toLower()); + } else { + return QIcon(":none"); + } + } + } + } + } else if (role == Qt::DisplayRole) { + // this is a section item (X Sessions / Virtual Sessions) + return item->text(); + } + + return QVariant(); +} + +Qt::ItemFlags SessionTreeModel::flags(const QModelIndex &index) const { + if (!index.isValid()) { + return 0; + } + + SessionTreeItem* item = + static_cast<SessionTreeItem*>(index.internalPointer()); + + if (item->session()) { + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; + } else { + return Qt::ItemIsEnabled; + } +} + +QVariant SessionTreeModel::headerData(int /*section*/, + Qt::Orientation /*orientation*/, + int /*role*/) const { + return QVariant(); +} + +QModelIndex SessionTreeModel::index(int row, int column, + const QModelIndex &parent) const { + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + SessionTreeItem *parentItem; + + if (!parent.isValid()) { + parentItem = root_; + } else { + parentItem = static_cast<SessionTreeItem*>(parent.internalPointer()); + } + + SessionTreeItem *childItem = parentItem->child(row); + if (childItem) { + return createIndex(row, column, childItem); + } else { + return QModelIndex(); + } +} + +QModelIndex SessionTreeModel::parent(const QModelIndex &index) const { + if (!index.isValid()) { + return QModelIndex(); + } + + SessionTreeItem *childItem = + static_cast<SessionTreeItem*>(index.internalPointer()); + SessionTreeItem *parentItem = childItem->parent(); + + if (parentItem == root_) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +void SessionTreeModel::addItems(const QList<Session*>& sessions, + const QString& section) { + SessionTreeItem* parentItem; + + bool sectionExists = false; + + for (int i = 0; i < root_->childCount(); ++i) { + SessionTreeItem* item = root_->child(i); + if (item->text() == section) { + parentItem = item; + sectionExists = true; + break; + } + } + + if (!sectionExists) { + parentItem = new SessionTreeItem(section, root_); + root_->appendChild(parentItem); + } + + foreach (Session* s, sessions) { + parentItem->appendChild(new SessionTreeItem(s, parentItem)); + } +} diff --git a/src/SessionTreeModel.h b/src/SessionTreeModel.h new file mode 100644 index 0000000..34b830e --- /dev/null +++ b/src/SessionTreeModel.h @@ -0,0 +1,50 @@ +/* + * 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 + * + */ + +#ifndef VMCHOOSER_SESSIONTREEMODEL_H +#define VMCHOOSER_SESSIONTREEMODEL_H + +#include <QAbstractItemModel> +#include <QModelIndex> +#include <QVariant> + +class SessionTreeItem; +class Session; + +class SessionTreeModel : public QAbstractItemModel { + Q_OBJECT + + public: + explicit SessionTreeModel(QObject *parent = 0); + ~SessionTreeModel(); + + QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &index) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + + void addItems(const QList<Session*>& sessions, const QString& section); + + + private: + SessionTreeItem* root_; +}; + +#endif // VMCHOOSER_SESSIONTREEMODEL_H diff --git a/src/VSession.cpp b/src/VSession.cpp new file mode 100644 index 0000000..76e13b0 --- /dev/null +++ b/src/VSession.cpp @@ -0,0 +1,356 @@ +/* + * 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 <QtXml> +#include <QDir> +#include <QApplication> +#include <QProcess> +#include <QDate> +#include <QStringList> +#include <QTextStream> +#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 "VSessionHandler.h" + +bool VSession::debugMode = false; + +VSession::VSession(VSessionHandler *parent) { + sessionHandler = parent; +} + +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); +} + +QString VSession::icon() const { + QString icon(getAttribute("icon")); + if (icon.isEmpty()) { + if (imgtype() == VMWARE) icon = "vmware"; + else if (imgtype() == VBOX) icon = "virtualbox"; + else icon = "unknown"; + } else if (icon.contains(".") && QDir::isRelativePath(icon)) { + // non-built-in icon with relative path + icon.prepend(baseDirPath_ + "/"); + } + return icon; + } + +QString VSession::screenshot() const { + QString ret; + ret = getAttribute("screenshot"); +// qDebug() << ret; + if (ret.size() > 0) { + ret.prepend(baseDirPath_ + "/"); + ret.prepend("file://"); + return ret; + } + return QString("qrc:/img/noscreenshot"); +}; + + +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); +} + +QString VSession::getNodeText(const QString& nodeName) const { + QString str; + QTextStream stream(&str); + QDomNode node = this->doc_.namedItem("eintrag").namedItem(nodeName).toElement(); + node.save(stream, 4); + +// return this->doc_.namedItem("eintrag").namedItem(nodeName).toElement().text(); + return str; +} + +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) { + 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() << "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"; + return false; + } + } + + if (sessionHandler->hasEnv()) { + QStringList pools = getAttribute("pools").split("\\s*,\\s*"); + if (!pools.isEmpty() && !pools.contains(sessionHandler->getEnv())) { + // pools does not contain pool + if (debugMode) qDebug() << "Not active. Reason: vsession is not part of active env"; + 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 { + return getAttribute("priority").toInt(); +} + + +void VSession::addPrinters(const QString& script, const QString& type) { + QDomElement printersNode = + this->doc_.namedItem("eintrag").namedItem(type + "s").toElement(); + + if (printersNode.isNull()) { + // create new printers node + printersNode = this->doc_.createElement(type + "s"); + this->doc_.namedItem("eintrag").appendChild(printersNode); + } + + QProcess addPrintersScript; + addPrintersScript.start(script, QIODevice::ReadOnly); + while (!addPrintersScript.atEnd()) { + QString line(addPrintersScript.readLine()); + QStringList tokens(line.split("\t")); + + if (tokens.size() < 2 or tokens.size() > 3) { + // TODO error message + // invalid output of printerScript + // printserver\tprinter\tprinter description + continue; + } + + QDomElement printerNode(doc_.createElement(type)); + printerNode.setAttribute("name", tokens.at(1)); + printerNode.setAttribute("path", + "\\\\" + tokens.at(0) + "\\" + tokens.at(1)); + if (tokens.size() == 3) { + QDomText printerDescriptionNode(doc_.createTextNode(tokens.at(3))); + printerNode.appendChild(printerDescriptionNode); + } + printersNode.appendChild(printerNode); + } + + addPrintersScript.close(); + + return; +} + +void VSession::addScanners(const QString& script) { + addPrinters(script, "scanner"); +} + +void VSession::addUserAndHostname() { + QString username(getpwuid(geteuid())->pw_name); + this->addNodeWithAttribute("username", username); + + // Qt >= 4.7 has <QHostInfo> + // QString hostname(QHostInfo::localHostName()); + char hname[255]; + gethostname(hname, sizeof(hname)); + QString hostname(hname); + this->addNodeWithAttribute("hostname", hostname); + + QString image(this->getAttribute("image_name")); + if (QFileInfo(image).isRelative()) { + // make path to image absolute + this->addNodeWithAttribute("image_name", + this->baseDirPath_ + "/" + 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); + this->doc_.namedItem("eintrag").insertBefore(computername, QDomNode()); +} + +void VSession::mergePoolXml() { + QDomDocument doc; + + QString poolXmlFile = sessionHandler->getConfPath() + "/vmchooser-" + sessionHandler->getEnv() + ".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") != sessionHandler->getEnv()) 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 { + QString command = getAttribute("command"); + if (!command.isEmpty()) { + return QProcess::startDetached(command); + } + + VSession session = *this; + + session.addPrinters(sessionHandler->getPrinterScript()); + session.addScanners(sessionHandler->getScannerScript()); + + 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(sessionHandler->getRunVmScript(), QStringList(tmpfile.fileName())); + qDebug() << sessionHandler->getRunVmScript(); + 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; +} + +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; +} diff --git a/src/VSession.h b/src/VSession.h new file mode 100644 index 0000000..8eb99cd --- /dev/null +++ b/src/VSession.h @@ -0,0 +1,136 @@ +/* + * 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 + * + */ + +#ifndef VMCHOOSER_VSESSION_H_ +#define VMCHOOSER_VSESSION_H_ + +#include <QString> +#include <QList> +#include <QDomDocument> +#include <QDir> +#include <QProcess> +#include <QFSFileEngine> +#include <QDateTime> + +#include "Session.h" +#include "VSessionHandler.h" + +enum ImgType { + VMWARE, + VBOX, + OTHER +}; + +class VSession : public Session { + public: + VSession(VSessionHandler *parent); + bool init(const QString& xml, const QString& baseDirPath); + + ImgType imgtype() const; + bool isActive() const; + bool isLocked() const; + bool isValid() const; + int priority() const; + + QString shortDescription() const { + return getAttribute("short_description"); + } + + QString description() const { + QString ret; + ret = getNodeText("description_html"); + if (ret.size() > 0) return ret; + return getAttribute("long_description"); + } + + QString icon() const; + + QString os() const { + QString ret; + ret = getAttribute("operating_system"); + if (ret.size() > 0) return ret; + return getAttribute("os"); + } + + QString title() const { + QString tmp = getAttribute("title"); + if (tmp.size() > 0) return tmp; + return getAttribute("short_description"); + }; + + QString screenshot() const; + + QString emulator() const { + return getAttribute("emulator"); + }; + + QString author() const { + QString tmp; + tmp = getAttribute("creator") + "<" + getAttribute("email") + ">"; + return tmp; + }; + + QString creationDate () const { + if (QFile::exists(QString(this->baseDirPath_).append("/").append(getAttribute("image_name")))) { + QFSFileEngine fe(QString(this->baseDirPath_).append("/").append(getAttribute("image_name"))); //QAbstractFileEngine::ModificationTime + return fe.fileTime(QAbstractFileEngine::CreationTime).toString(Qt::SystemLocaleLongDate); + } + return QString(); + }; + + QString changeDate() const { + if (QFile::exists(QString(this->baseDirPath_).append("/").append(getAttribute("image_name")))) { + QFSFileEngine fe(QString(this->baseDirPath_).append("/").append(getAttribute("image_name"))); //QAbstractFileEngine::ModificationTime + return fe.fileTime(QAbstractFileEngine::AccessTime).toString(Qt::SystemLocaleLongDate); + } + return QString(); + }; + + + QString getAttribute(const QString& nodeName, + const QString& attribute = "param") const; + QString getNodeText(const QString& nodeName) const; + + void addNodeWithAttribute(const QString& nodeName, + const QString& value, + const QString& attribute = "param", + bool replace = true); + void addPrinters(const QString& script, const QString& type = "printer"); + void addScanners(const QString& script); + void addUserAndHostname(); + void mergePoolXml(); + + QString toXml() const; + + bool run() const; + + int type() const; + + bool operator<(const Session& other) const; + + static bool debugMode; + + + + private: + QDomDocument doc_; + QString baseDirPath_; + VSessionHandler *sessionHandler; + + + QProcess *_process; +}; + +#endif /*VMCHOOSER_VSESSION_H_*/ diff --git a/src/VSessionHandler.cpp b/src/VSessionHandler.cpp new file mode 100644 index 0000000..bda85d0 --- /dev/null +++ b/src/VSessionHandler.cpp @@ -0,0 +1,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; +} + diff --git a/src/VSessionHandler.h b/src/VSessionHandler.h new file mode 100644 index 0000000..c0806c8 --- /dev/null +++ b/src/VSessionHandler.h @@ -0,0 +1,62 @@ +/* + * 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 + * + */ + + +#ifndef VSESSIONHANDLER_H_ +#define VSESSIONHANDLER_H_ + +#include <QList> +#include <QString> + +class Session; + +class VSessionHandler { +public: + VSessionHandler(); + virtual ~VSessionHandler(); + + QList<Session*> readXmlDir(const QString& path); + QList<Session*> readXmlFile(const QString& filepath); + + + void setEnv(QString env); + void setPrinterScript(QString printerScript); + void setScannerScript(QString scannerScript); + void setRunVmScript(QString runVmScript); + void setConfPath(QString confPath); + void setFilterScript(QString filterScript); + + QString getEnv() ; + QString getPrinterScript() ; + QString getScannerScript() ; + QString getRunVmScript() ; + QString getConfPath() ; + QString getFilterScript() ; + + bool hasEnv(); + + +private: + QString _filterScript; + QString _env; + QString _confPath; + QString _printerScript; + QString _scannerScript; + QString _runVmScript; + bool debugMode; + +}; + +#endif /* VSESSIONHANDLER_H_ */ diff --git a/src/XSession.cpp b/src/XSession.cpp new file mode 100644 index 0000000..47db987 --- /dev/null +++ b/src/XSession.cpp @@ -0,0 +1,150 @@ +/* + * 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 <QDir> +#include <QSettings> +#include <QLocale> +#include <QApplication> +#include <QProcess> + +#include "XSession.h" + +void XSession::init(const QString& name, const QString& exec, + const QString& comment, const QString& icon) { + this->name_ = name; + this->exec_ = exec; + this->comment_ = comment; + this->icon_ = icon; +} + +bool XSession::init(const QString& filename) { + QSettings settings(filename, QSettings::IniFormat); + settings.setIniCodec("UTF-8"); + + settings.beginGroup("Desktop Entry"); + + if (settings.value("NoDisplay").toString().compare("true") == 0 || + settings.value("Hidden").toString().compare("true") == 0) { + return false; + } + + if (!settings.contains("Exec")) { + return false; + } + QString exec(settings.value("Exec").toString()); + + QString locale(QLocale::system().name()); + QString language(locale.split("_").at(0)); + QString name; + if (settings.contains("Name[" + locale + "]")) { + name = settings.value("Name[" + locale + "]").toString(); + } else if (settings.contains("Name[" + language + "]")) { + name = settings.value("Name[" + language + "]").toString(); + } else if (settings.contains("Name")) { + name = settings.value("Name").toString(); + } else { + return false; + } + + QString comment; + if (settings.contains("Comment[" + locale + "]")) { + comment = settings.value("Comment[" + locale + "]").toString(); + } else if (settings.contains("Comment[" + language + "]")) { + comment = settings.value("Comment[" + language + "]").toString(); + } else { + comment = settings.value("Comment").toString(); + } + + QString icon(settings.value("Icon").toString()); + if (QDir::isRelativePath(icon)) { + // icons with relative paths are too complicated to find + // see http://freedesktop.org/wiki/Specifications/icon-theme-spec + // let's just ignore them + icon = QString(); + } + + this->name_ = name; + this->exec_ = exec; + this->comment_ = comment; + this->icon_ = icon; + + _process = new QProcess(); + + return true; +} + +bool XSession::isActive() const { + return true; +} + +bool XSession::isValid() const { + return true; +} + +bool XSession::isLocked() const { + return false; +} + +int XSession::priority() const { + return 0; +} + +QString XSession::icon() const { + QString icon(this->icon_); + + if (icon.isEmpty()) { + if (this->exec_.contains("kde", Qt::CaseInsensitive)) { + icon = "kde"; + } else if (this->exec_.contains("gnome", Qt::CaseInsensitive)) { + icon = "gnome"; + } else if (this->exec_.contains("term", Qt::CaseInsensitive)) { + icon = "term"; + } else if (this->exec_.contains("xfce", Qt::CaseInsensitive)) { + icon = "xfce"; + } else if (this->exec_.contains("lxde", Qt::CaseInsensitive)) { + icon = "lxde"; + } else { + icon = "linux"; + } + } + + return icon; +} + +bool XSession::run() const { + _process->start(this->exec_); + 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 XSession::type() const { + return Session::XSESSION; +} + +bool XSession::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; +} diff --git a/src/XSession.h b/src/XSession.h new file mode 100644 index 0000000..c1b83c9 --- /dev/null +++ b/src/XSession.h @@ -0,0 +1,93 @@ +/* + * 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 + * + */ + +#ifndef VMCHOOSER_XSESSION_H_ +#define VMCHOOSER_XSESSION_H_ + +#include <QString> +#include <QList> +#include <QDomDocument> +#include <QDir> +#include <QProcess> + +#include "Session.h" + +class XSession : public Session { + public: + void init(const QString& name, const QString& exec, + const QString& comment, const QString& icon); + bool init(const QString& filename); + + bool isActive() const; + bool isLocked() const; + bool isValid() const; + int priority() const; + + + QString title() const { + return this->name_; + }; + + QString screenshot() const { + return QString(); + }; + + QString os() const { + return QString(); + }; + + QString emulator() const { + return QString(); + }; + + QString author() const { + return QString(); + }; + + QString creationDate () const { + return QString(); + }; + + QString changeDate() const { + return QString(); + }; + + + QString shortDescription() const { + return QString(); + } + + QString description() const { + return QString(); + } + + QString icon() const; + + bool run() const; + + int type() const; + + bool operator<(const Session& other) const; + + private: + QString name_; + QString exec_; + QString comment_; + QString icon_; + + QProcess *_process; +}; + +#endif /*VMCHOOSER_XSESSION_H_*/ diff --git a/src/XSessionHandler.cpp b/src/XSessionHandler.cpp new file mode 100644 index 0000000..4291a9a --- /dev/null +++ b/src/XSessionHandler.cpp @@ -0,0 +1,47 @@ +/* + * 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 <QFile> +#include <QList> +#include <QFileInfo> + + +#include "XSessionHandler.h" +#include "XSession.h" +#include "Session.h" + +XSessionHandler::XSessionHandler() { + // TODO Auto-generated constructor stub + +} + +XSessionHandler::~XSessionHandler() { + // TODO Auto-generated destructor stub +} + + +QList<Session*> XSessionHandler::readSessions(const QString& path) { + QList<Session*> retval; + foreach (QFileInfo fi, QDir(path).entryInfoList(QStringList("*.desktop"))) { + if (fi.baseName().compare("default") == 0) { + continue; + } + XSession* session = new XSession; + if (session->init(fi.absoluteFilePath())) { + retval.append(session); + } + } + return retval; +} diff --git a/src/XSessionHandler.h b/src/XSessionHandler.h new file mode 100644 index 0000000..461bc4e --- /dev/null +++ b/src/XSessionHandler.h @@ -0,0 +1,29 @@ +/* + * 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 + * + */ + + +#ifndef XSESSIONHANDLER_H_ +#define XSESSIONHANDLER_H_ + +class Session; + +class XSessionHandler { +public: + XSessionHandler(); + virtual ~XSessionHandler(); + QList<Session*> readSessions(const QString& path); +}; + +#endif /* XSESSIONHANDLER_H_ */ |