summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 90a9fbcf61e502568adb228a2e4558db55f1dc6d (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
#include <QtGui/QApplication>
#include "dialog.h"

#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 "globals.h"
#include "command_line_options.h"

#include <boost/filesystem.hpp>

namespace bfs=boost::filesystem;

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QTranslator translator;
    translator.load(":" + QLocale::system().name());
    a.installTranslator(&translator);

    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);
  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();
}