summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: e511c095aba7484080469874dbee5ef0f1f85ee1 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <QMap>
#include <QString>
#include <QSettings>
#include <QTranslator>
#include <QtAlgorithms>
#include <QtGui/QApplication>
#include <QDesktopWidget>
#include <QLocale>
#include <QtDebug>
#include <QMessageBox>

#include <cstdlib>
#include <iostream>
#include <string>

#include "command_line_options.h"
#include "dialog.h"
#include "globals.h"
#include "vsession.h"
#include "xsession.h"
#include "httpxmldownloader.h"
#include "choosersettings.h"

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

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

	CommandLineOptions cmdOptions(argc, argv);

	QString autostart_uuid("");

	std::string usage(
			a.translate("Console", "Usage: vmchooser [ OPTIONS ]\n\n"
					"  -b  --base       base directory where VM images are accessible\n"
					"  -d, --default    name of default session\n"
					"  -c, --config     alternative config file\n"
					"  -l, --locations  location id(s), space separated\n"
					"  --location-mode  how to treat entries for this location (IGNORE, BUMP or EXCLUSIVE)\n"
					"  --exam-mode      enable exam mode\n"
					"  -P, --pool       one or more pool names to display (comma separated)\n"
					"  -f, --file       direct boot .desktop file\n"
					"  -x, --xpath      path of X Session .desktop files\n"
					"  -u, --url        url of vmware .xml file\n"
					"  -s, --size       window size <width>x<height>\n"
					"  -t, --theme      theme\n"
					"  --template-mode  how to treat template entries (IGNORE or BUMP)\n"
					"  -p, --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"
					"  -S, --runscript  change path to run-virt.sh\n"
					"  -T  --tab        default tab (0=xsession, 1=my vms, 2=all vms)\n"
					"  --no-vtx         Host doesn't support VT-x/AMD-V (mark 64bit guests)\n"
					"  --start-uuid	    start lecture with the given uuid\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("uuid")) {
		autostart_uuid = cmdOptions.value("uuid");
	}

	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().constData()
					<< std::endl;
			return EXIT_FAILURE;
		} else {
			std::cerr
					<< a.translate("Console", "vmchooser: invalid session file").toUtf8().constData()
					<< std::endl;
			return EXIT_FAILURE;
		}
	}

	// read configuration file:
	//   file supplied as command line option or
	//   user vmchooser.conf or
	//   global vmchooser.conf
	QString confFile;
	if (cmdOptions.contains("config")) {
		if (QFileInfo(cmdOptions.value("config")).exists()) {
			confFile = cmdOptions.value("config");
		} else {
			std::cerr << a.translate("Console", "Requested --config not found: ").toUtf8().constData() << cmdOptions.value("config").toUtf8().constData() << std::endl;
		}
	}
	if (confFile.isEmpty() && QFileInfo(userConfFile).exists()) {
		confFile = userConfFile;
		std::cerr << a.translate("Console", "Using user specific config: ").toUtf8().constData() << userConfFile.toUtf8().constData() << std::endl;
	}
	if (confFile.isEmpty() && QFileInfo(globalConfFile).exists()) {
		confFile = globalConfFile;
		std::cerr << a.translate("Console", "Using global config: ").toUtf8().constData() << globalConfFile.toUtf8().constData() << std::endl;
	}
	if (confFile.isEmpty()) {
		std::cerr << a.translate("Console", "No config file found or given").toUtf8().constData() << std::endl;
	}

	// TODO: This is the system wide settings, give proper name
	QSettings settings(confFile, QSettings::IniFormat);
	settings.setIniCodec("UTF-8");

	if (ChooserSettings::getSetting("last-session").isEmpty()) {
		QString defaultSession;
		if (cmdOptions.contains("default")) {
			defaultSession = cmdOptions.value("default");
		} else if (settings.contains("default")) {
			defaultSession = settings.value("default").toString();
		}
		ChooserSettings::setSetting("last-session", defaultSession);
	}

	if (cmdOptions.contains("xpath")) {
		xSessionPath = cmdOptions.value("xpath");
	} else if (settings.contains("xpath")) {
		xSessionPath = settings.value("xpath").toString();
	}  // else keep default path

	if (cmdOptions.contains("url")) {
		urlBase = cmdOptions.value("url");
	} else if (settings.contains("url")) {
		urlBase = settings.value("url").toString();
	} else {
		std::cerr
				<< a.translate("Console", "vmchooser: no URL given").toUtf8().data()
				<< std::endl;
		return EXIT_FAILURE;
	}

	// Change the runVmScript path
	if (cmdOptions.contains("runscript")) {
		runVmScript = cmdOptions.value("runscript");
	} else if (settings.contains("runscript")) {
		runVmScript = settings.value("runscript").toString();
	}

	/* PARSE URL */
	if (!urlBase.startsWith("http://")) {
		std::cerr
				<< a.translate("Console", "vmchooser: invalid URL").toUtf8().data()
				<< std::endl;
		return EXIT_FAILURE;
	} else {
		if (!urlBase.endsWith("/")) {
			urlBase += "/";
		}
	}

	QString size;
	int width, height;
	if (cmdOptions.contains("fullscreen")) {
		size = "fullscreen";
	} else if (settings.contains("fullscreen")) {
		size = "fullscreen";
	} else if (cmdOptions.contains("size")) {
		size = cmdOptions.value("size");
	} else if (settings.contains("size")) {
		size = settings.value("size").toString();
	}

	if (size != "fullscreen") {
		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: invalid size argument").toUtf8().data()
					<< std::endl;
			return EXIT_FAILURE;
		} else {
			width = VMCHOOSER_DEFAULT_WIDTH;
			height = VMCHOOSER_DEFAULT_HEIGHT;
		}
	}

	if (cmdOptions.contains("pool")) {
		pool = cmdOptions.value("pool");
	} 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;
	}

	if (cmdOptions.contains("base")) {
		basePath = cmdOptions.value("base");
	} else if (settings.contains("base")) {
		basePath = settings.value("base").toString();
	} else if (settings.contains("path")) { // Compatibility to v1.0
		basePath = settings.value("path").toString();
	}

	if (cmdOptions.contains("location-mode")) {
		QString mode = cmdOptions.value("location-mode");
		if (mode == "IGNORE") {
			g_forLocationHandling = LOCATION_IGNORE;
		} else if (mode == "BUMP") {
			g_forLocationHandling = LOCATION_BUMP;
		} else if (mode == "EXCLUSIVE") {
			g_forLocationHandling = LOCATION_EXCLUSIVE;
		} else {
			qDebug() << "Invalid location mode: " << mode;
			QMessageBox::critical(NULL, "Error", "Invalid location mode: " + mode);
			return 1;
		}
	}

	if (cmdOptions.contains("template-mode")) {
		QString mode = cmdOptions.value("template-mode");
		if (mode == "IGNORE") {
			g_templateHandling = LOCATION_IGNORE;
		} else if (mode == "BUMP") {
			g_templateHandling = LOCATION_BUMP;
		} else {
			qDebug() << "Invalid template mode: " << mode;
			QMessageBox::critical(NULL, "Error", "Invalid template mode: " + mode);
			return 1;
		}
	}

	int defaultTab = -1;
	if (cmdOptions.contains("tab")) {
		defaultTab = cmdOptions.value("tab").toInt();
	}

	QString locationIds;
	if (cmdOptions.contains("locations")) {
		locationIds = cmdOptions.value("locations");
	}

	if (cmdOptions.contains("no-vtx")) {
		g_noVtx = true;
	}

	/* read session files */
	QList<Session*> xsessions(XSession::readSessions(xSessionPath));

	Dialog w(defaultTab, cmdOptions.contains("exam-mode"));

	/* DOWNLOAD VSESSIONS */
	HttpXmlDownloader httpxmldownloader;
	httpxmldownloader.connectSlot(&w,
			SLOT(addSessionsAfterDownload(QNetworkReply*)));

	// read xml and add items later
	if (cmdOptions.contains("exam-mode")) {
		httpxmldownloader.makeRequest(urlBase + "list.php?exams=exam-mode", locationIds);
	} else {
		httpxmldownloader.makeRequest(urlBase + "list.php", locationIds);
	}

	/* DOWNLOAD NEWS */
	HttpXmlDownloader news_downloader;
	news_downloader.connectSlot(&w, SLOT(addNewsAfterDownload(QNetworkReply*)));
	news_downloader.makeRequest(urlBase + "news.php");

	/* DOWNLOAD HELP-SECTION */
	HttpXmlDownloader help_downloader;
	help_downloader.connectSlot(&w, SLOT(addHelpAfterDownload(QNetworkReply*)));
	help_downloader.makeRequest(urlBase + "help.php");

	w.setTheme();

	w.setWindowFlags(Qt::FramelessWindowHint);
	if (cmdOptions.contains("pvs")) {
		pvsEnabled = true;
	}

	QRect desktopRect = QApplication::desktop()->availableGeometry(&w);
	if (size == "fullscreen") {
		width = desktopRect.width();
		height = desktopRect.height();
		_fullscreen = true;
	}

	w.resize(width, height);

	if (xsessions.size()) {
		qSort(xsessions.begin(), xsessions.end(), myLessThan);
		w.addItems(xsessions, 0);
	}

	w.show();

	if (autostart_uuid != "") {
		qDebug() << "using startSession() from main.cpp";
		w.startSession(autostart_uuid);
	}



	// center dialog on primary screen
	QPoint center = desktopRect.center();
	w.move(center.x() - w.width() * 0.5, center.y() - w.height() * 0.5);
	a.setActiveWindow(&w);
	w.lower();

	return a.exec();
}