summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
diff options
context:
space:
mode:
authorJan Darmochwal2010-10-07 14:53:20 +0200
committerJan Darmochwal2010-10-07 14:53:20 +0200
commit3932297962d7394574346c016c38ef5103dc5a7c (patch)
tree2a1f8d128d1e87965fea6be41943717b3cddce65 /src/main.cpp
parentrecursive globbing in VSession::readXmlDir() (diff)
downloadvmchooser-3932297962d7394574346c016c38ef5103dc5a7c.tar.gz
vmchooser-3932297962d7394574346c016c38ef5103dc5a7c.tar.xz
vmchooser-3932297962d7394574346c016c38ef5103dc5a7c.zip
Support for command line arguments and conf files
* anyoption has been replaced by getopt * replaces huge unmaintainable .cpp and .h files from an external source by standard POSIX library calls * I would suggest using Boost.Program_options if cross-platform support is needed * support user/global configuration file
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp300
1 files changed, 130 insertions, 170 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 87bb84f..90a9fbc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,18 +4,19 @@
#include <iostream>
#include <stdlib.h>
#include <QTranslator>
+#include <QMap>
+#include <QString>
+#include <QSettings>
#include "save_restore_session.h"
#include "xsession.h"
#include "vsession.h"
-#include "anyoption.h"
#include "globals.h"
+#include "command_line_options.h"
#include <boost/filesystem.hpp>
namespace bfs=boost::filesystem;
-using namespace std;
-
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
@@ -23,176 +24,135 @@ int main(int argc, char *argv[]) {
translator.load(":" + QLocale::system().name());
a.installTranslator(&translator);
- string version = "0.0.13";
- AnyOption* opt = new AnyOption();
- char* xmlpath = NULL;
- char* lsesspath = NULL;
- char* dsession = NULL;
- int width=0, height=0;
-
- //opt->setVerbose();
- opt->autoUsagePrint(false);
-
- opt->addUsage("");
- opt->addUsage("SessionChooser Usage: vmchooser [OPTS|image.xml]");
- opt->addUsage("\t{-d |--default=} name of session to select (part of)");
- opt->addUsage("\t{-p |--path=} path to vmware (.xml) files");
- opt->addUsage("\t{-l |--lpath=} path to linux session (.desktop) files");
- opt->addUsage("\t{-s |--size=} [widthxheight]");
- opt->addUsage("\t{-v |--version} print out version");
- opt->addUsage("\t{-h |--help} prints help");
- opt->addUsage("");
- opt->addUsage("Run with xml-file as additional argument to start image at once.");
-
- opt->setFlag("help",'h');
- opt->setFlag("version",'v');
- opt->setOption("default", 'd');
- opt->setOption("path", 'p');
- opt->setOption("lpath", 'l');
- opt->setOption("size",'s');
-
- opt->processCommandArgs(argc, argv);
-
- /** HELP */
- if(opt->getFlag("help") || opt->getFlag('h')) {
- opt->printUsage();
- return 0;
- }
-
- /**
- * XML - PATH
- *
- * 1. read from stage3.conf
- * 2. option -p
- * 3. option --path
- * 4. default value "/var/lib/virt/vmware/"
- *
- **/
-
- ifstream ifs (
- string(VMCHOOSER_ETC_BASE_PATH).append("vmchooser.conf").c_str(),
- ifstream::in
- );
- if(ifs) {
- int n = 255;
- char buf[n];
- string s = "";
- while(!ifs.eof()) {
- ifs.getline(buf, n);
- s = buf;
- if(s.substr(0,17) == "vmchooser_xmlpath") {
- xmlpath = (char*)strdup(s.substr(19,s.length()-20).append("/").c_str());
- }
- }
-
- }
-
- if(opt->getValue('d')!=NULL) {
- dsession = opt->getValue('d');
- }
-
- if(opt->getValue("default")!= NULL) {
- dsession = opt->getValue("default");
- }
-
- if(opt->getValue('p')!=NULL) {
- xmlpath = opt->getValue('p');
- }
-
- if(opt->getValue("path")!= NULL) {
- xmlpath = opt->getValue("path");
- }
-
- if (xmlpath == NULL) {
- // Default Path comes here
- xmlpath = (char *) VMCHOOSER_VMPATH;
- }
-
- /* VERSION */
- if(opt->getFlag('v') || opt->getFlag("version")) {
- // just print out version information - helps testing
- cout << "virtual machine chooser " << version << endl;
- delete opt;
- return 0;
-
- }
-
- /** LINUX SESSION PATH */
- if(opt->getValue('l')!=NULL) {
- lsesspath = opt->getValue('l');
- }
- if(opt->getValue("lpath")!= NULL) {
- lsesspath = opt->getValue("lpath");
- }
- if (lsesspath == NULL) {
- // TODO: absolute paths as constants
- lsesspath = (char *) "/usr/share/xsessions/";
- }
-
- /** Size of Window */
- string size;
- unsigned int i;
-
- if(opt->getValue('s')!=NULL) {
- size = opt->getValue('s');
- }
- if(opt->getValue("size")!= NULL) {
- size = opt->getValue("size");
- }
-
- if (size.empty()) {
- width = 500;
- height = 550;
- }
- else {
- i = size.find_first_of("x");
- if (i == string::npos) {
- cerr << "Please write <width>x<height> as argument for -s|--size." << endl;
- return 1;
- }
- height = atoi(size.substr(i+1).c_str());
- width = atoi(size.substr(0, size.size()-i).c_str());
- }
-
-
- // additional xml argument -> start image directly
-#if 0
- if(opt->getArgc() > 0) {
- string single_arg = opt->getArgv(0);
- if(bfs::is_directory(single_arg)) {
- fprintf(stderr, "Only argument is a folder, should be a valid xml file!\n");
- return 1;
- }
- // read xml image
- xmlDoc* doc = xmlReadFile(single_arg.c_str(), NULL, XML_PARSE_RECOVER);
- if (doc == NULL) {
- fprintf(stderr, "Error: could not parse file %s\n", single_arg.c_str());
- return 1;
- }
-
- DataEntry* result = get_entry(doc);
- if(result) {
- runImage(*result, single_arg );
- }
- else {
- fprintf(stderr, "Error: can not start image from xml\n\tcheck your <active> setting!\n");
- return 1;
- }
- }
-#endif
-
- delete opt;
-
- /* read xml files */
- QList<Session*> xsessions(XSession::readSessions(lsesspath));
- QList<Session*> vsessions(VSession::readXmlDir(xmlpath));
+ std::string version = "0.0.13";
+
+ CommandLineOptions cmdOptions(argc, argv);
+
+ std::string usage(a.translate(
+ "Console",
+ "Usage: vmchooser [ OPTIONS | FILE ]\n\n"
+ " -d, --default name of default session\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"
+ " -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 1;
+ }
+
+ if (cmdOptions.contains("usage")) {
+ std::cout << usage;
+ return 0;
+ }
+
+ if (cmdOptions.contains("version")) {
+ std::cout << version;
+ return 0;
+ }
+
+ if (cmdOptions.contains("file")) {
+ QString file(cmdOptions.value("file"));
+
+ if (file.endsWith(".desktop")) {
+ XSession s;
+ return s.init(file) && s.run();
+ } 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));
+ return s && s->run();
+ } else {
+ std::cerr << "not a valid session file" << std::endl;
+ return 1;
+ }
+ }
+
+ // read configuration file:
+ // file supplied as command line option or
+ // user vmchooser.conf or
+ // globel vmchooser.conf
+ QString confFile;
+ QString userConfFile(QDir::homePath() + "/" +
+ VMCHOOSER_USER_PATH + "/" +
+ VMCHOOSER_CONF_FILE);
+ QString globalConfFile(QString(VMCHOOSER_ETC_BASE_PATH) + "/" +
+ VMCHOOSER_CONF_FILE);
+ 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();
+ }
+
+ QString vSessionXmlPath;
+ if (cmdOptions.contains("path")) {
+ vSessionXmlPath = cmdOptions.value("path");
+ } else if (settings.contains("path")) {
+ vSessionXmlPath = settings.value("path").toString();
+ } else {
+ vSessionXmlPath = VMCHOOSER_VMPATH;
+ }
+
+ QString xSessionPath;
+ if (cmdOptions.contains("xpath")) {
+ xSessionPath = cmdOptions.value("xpath");
+ } else if (settings.contains("xpath")) {
+ xSessionPath = settings.value("xpath").toString();
+ } else {
+ xSessionPath = VMCHOOSER_X_SESSIONS_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", "invlid size argument").toUtf8().data() << std::endl;
+ exit(1);
+ } else {
+ width = VMCHOOSER_DEFAULT_WIDTH;
+ height = VMCHOOSER_DEFAULT_HEIGHT;
+ }
+
+ /* read session files */
+ QList<Session*> xsessions(XSession::readSessions(xSessionPath));
+ QList<Session*> vsessions(VSession::readXmlDir(vSessionXmlPath));
Dialog w;
w.resize(width, height);
-
- w.addItems(xsessions, a.translate("Dialog", "X Sessions"));
- w.addItems(vsessions, a.translate("Dialog", "Virtual Sessions"));
- w.selectPreviousSession();
+ if (xsessions.size()) {
+ w.addItems(xsessions, a.translate("Dialog", "X Sessions"));
+ }
+ if (vsessions.size()) {
+ w.addItems(vsessions, a.translate("Dialog", "Virtual Sessions"));
+ }
+ w.selectSession(defaultSession);
w.show();
return a.exec();
}