summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
diff options
context:
space:
mode:
authorNils Schwabe2014-03-03 15:14:20 +0100
committerNils Schwabe2014-03-03 15:14:20 +0100
commit030d91e628a2ecd5634376c9c4d903a1fa0fb423 (patch)
tree227029e3b48deb1d4d01ce497d9ec3b8d47f7a61 /src/main.cpp
parentInitial commit (diff)
downloadvmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.tar.gz
vmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.tar.xz
vmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.zip
added files from vmchooser1
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp238
1 files changed, 238 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..8ccbba2
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,238 @@
+#include <QMap>
+#include <QString>
+#include <QSettings>
+#include <QTranslator>
+#include <QtAlgorithms>
+#include <QtGui/QApplication>
+#include <QDesktopWidget>
+#include <QLocale>
+#include <QtDebug>
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+
+#include "command_line_options.h"
+#include "dialog.h"
+#include "globals.h"
+#include "save_restore_session.h"
+#include "vsession.h"
+#include "xsession.h"
+
+bool myLessThan(Session* a, Session* b) {
+ return *a < *b;
+}
+
+int main(int argc, char *argv[]) {
+ QApplication a(argc, argv);
+
+ QTranslator translator;
+ translator.load(":" + QLocale::system().name());
+ a.installTranslator(&translator);
+
+ CommandLineOptions cmdOptions(argc, argv);
+
+ std::string usage(a.translate(
+ "Console",
+ "Usage: vmchooser [ OPTIONS ]\n\n"
+ " -d, --default name of default session\n"
+ " -c, --config alternative config file\n"
+ " -e, --env name of the environment\n"
+ " -f, --file direct boot FILE\n"
+ " -p, --path path to vmware .xml files\n"
+ " -x, --xpath path of X Session .desktop files\n"
+ " -s, --size window size <width>x<height>\n"
+ " -t, --theme theme\n"
+ " -b, --pvs show pvs options\n"
+ " -D, --debug print debug information\n"
+ " -v, --version print version and exit\n"
+ " -h, --help print usage information and exit\n"
+ "\nFILE can be a vmware .xml or an X .desktop file\n")
+ .toUtf8().data());
+
+ if (cmdOptions.contains("error")) {
+ std::cerr << usage;
+ return EXIT_FAILURE;
+ }
+
+ if (cmdOptions.contains("usage")) {
+ std::cout << usage;
+ return EXIT_SUCCESS;
+ }
+
+ if (cmdOptions.contains("version")) {
+ std::cout << "vmchooser " << VMCHOOSER_VERSION << std::endl;
+ return EXIT_SUCCESS;
+ }
+
+ if (cmdOptions.contains("file")) {
+ QString file(cmdOptions.value("file"));
+
+ if (file.endsWith(".desktop")) {
+ XSession s;
+ if (s.init(file) && s.run()) {
+ return EXIT_SUCCESS;
+ }
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: failed to run session").toUtf8().data() <<
+ std::endl;
+ return EXIT_FAILURE;
+ } else if (file.endsWith(".xml")) {
+ // our XML-files can contain multiple sessions
+ // let's just take the first one
+ Session* s(VSession::readXmlFile(file).value(0));
+ if (s && s->run()) {
+ return EXIT_SUCCESS;
+ }
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: failed to run session").toUtf8().data() <<
+ std::endl;
+ } else {
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: invalid session file").toUtf8().data() <<
+ std::endl;
+ return EXIT_FAILURE;
+ }
+ }
+
+ // read configuration file:
+ // file supplied as command line option or
+ // user vmchooser.conf or
+ // globel vmchooser.conf
+ QString confFile;
+ if (cmdOptions.contains("config")) {
+ confFile = cmdOptions.value("config");
+ } else if (QFileInfo(userConfFile).exists()) {
+ confFile = userConfFile;
+ } else {
+ confFile = globalConfFile;
+ }
+ QSettings settings(confFile, QSettings::IniFormat);
+ settings.setIniCodec("UTF-8");
+
+ QString defaultSession;
+ if (cmdOptions.contains("default")) {
+ defaultSession = cmdOptions.value("default");
+ } else if (settings.contains("default")) {
+ defaultSession = settings.value("default").toString();
+ } else {
+ defaultSession = readSessionName();
+ }
+
+ if (cmdOptions.contains("path")) {
+ vSessionPath = cmdOptions.value("path");
+ } else if (settings.contains("path")) {
+ vSessionPath = settings.value("path").toString();
+ } // else keep default path
+
+ if (cmdOptions.contains("xpath")) {
+ xSessionPath = cmdOptions.value("xpath");
+ } else if (settings.contains("xpath")) {
+ xSessionPath = settings.value("xpath").toString();
+ } // else keep default path
+
+ QString size;
+ if (cmdOptions.contains("size")) {
+ size = cmdOptions.value("size");
+ } else if (settings.contains("size")) {
+ size = settings.value("size").toString();
+ }
+
+ int width, height;
+ QRegExp rx("^(\\d+)x(\\d+)$");
+ if (rx.indexIn(size) != -1) {
+ QStringList list = rx.capturedTexts();
+ width = list.value(1).toInt();
+ height = list.value(2).toInt();
+ } else if (!size.isEmpty()) {
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: invlid size argument").toUtf8().data()
+ << std::endl;
+ return EXIT_FAILURE;
+ } else {
+ width = VMCHOOSER_DEFAULT_WIDTH;
+ height = VMCHOOSER_DEFAULT_HEIGHT;
+ }
+
+ if (cmdOptions.contains("env")) {
+ pool = cmdOptions.value("env");
+ } else if (settings.contains("pool")) {
+ pool = settings.value("pool").toString();
+ }
+
+ if (cmdOptions.contains("theme")) {
+ theme = cmdOptions.value("theme");
+ } else if (settings.contains("theme")) {
+ theme = settings.value("theme").toString();
+ }
+
+ if (cmdOptions.contains("debugMode")) {
+ debugMode = true;
+ }
+
+ /* read session files */
+ QList<Session*> xsessions(XSession::readSessions(xSessionPath));
+ QList<Session*> vsessions(VSession::readXmlDir(vSessionPath));
+
+ Dialog w;
+
+ w.setTheme();
+
+ w.setWindowFlags(Qt::FramelessWindowHint);
+ if (cmdOptions.contains("pvs")) {
+ pvsEnabled = true;
+ } else if (settings.contains("pvs")) {
+ if (settings.value("pvs").toInt() == 1)
+ pvsEnabled = true;
+ }
+
+ if ( pvsEnabled )
+ w.showSettingsPVS();
+
+ w.resize(width, height);
+ if (xsessions.empty() && vsessions.empty()) {
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: no sessions found").toUtf8().data()
+ << std::endl;
+ return EXIT_FAILURE;
+ }
+ if (xsessions.size()) {
+ qSort(xsessions.begin(), xsessions.end(), myLessThan);
+ w.addItems(xsessions, a.translate("Dialog", "X Sessions"));
+ }
+ if (vsessions.size()) {
+ if (!(QFile::permissions(runVmScript) & QFile::ExeUser)) {
+ std::cerr << a.translate(
+ "Console",
+ "vmchooser: external script %1 is not executable")
+ .arg(runVmScript).toUtf8().data()
+ << std::endl;
+ return EXIT_FAILURE;
+ }
+ qSort(vsessions.begin(), vsessions.end(), myLessThan);
+ w.addItems(vsessions, a.translate("Dialog", "Virtual Sessions"));
+ }
+
+ QSettings SLXsettings(OPENSLXCONFIG, QSettings::NativeFormat);
+ if ( SLXsettings.contains("SLX_BENCHMARK_VM") ) {
+ QString vm = SLXsettings.value("SLX_BENCHMARK_VM").toString();
+ vm.remove('\'');
+ w.startSession(vm);
+ } else {
+ w.selectSession(defaultSession);
+ }
+ w.show();
+
+ // center dialog on primary screen
+ QRect desktopRect = QApplication::desktop()->availableGeometry(&w);
+ QPoint center = desktopRect.center();
+ w.move(center.x()-w.width()*0.5, center.y()-w.height()*0.5);
+ a.setActiveWindow(&w);
+
+ return a.exec();
+}