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
|
#include <QApplication>
#include <QSettings>
#include <QtCore>
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include "fbgui.h"
void printHelp()
{
QTextStream qout(stdout);
qout << QObject::tr("Usage: ./fbgui [OPTIONS]") << endl;
qout << QObject::tr("Options:") << endl;
qout << "-u <URL>, --url=<URL> " << QObject::tr("Set which URL to load.") << endl;
qout << "-d <dir>, --download=<dir> " << QObject::tr("Specify the download directory.") << endl;
qout << "-c <path>, --config=<path> " << QObject::tr("Path to config file.") << endl;
qout << "-D, --debug " << QObject::tr("Activate debug mode.") << endl;
qout << "-h, --help " << QObject::tr("Prints usage information.") << endl;
qout.flush();
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv, QApplication::GuiServer);
app.setOrganizationName("team_projekt_2011");
app.setApplicationName("prebootGUI");
binPath = QApplication::applicationDirPath();
QTranslator translator;
translator.load(":" + QLocale::system().name());
app.installTranslator(&translator);
// parse command line arguments
QMap<QString, QString> clOpts;
int longIndex = 0;
static const char *optString = "u:d:c:D:h";
static const struct option longOpts[] =
{
{"url", required_argument, NULL, 'u'},
{"download", required_argument, NULL, 'd'},
{"config", required_argument, NULL, 'c'},
{"debug", required_argument, NULL, 'D'},
{"help", no_argument, NULL, 'h'}
};
int opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
while (opt != -1)
{
switch(opt)
{
case 'u':
clOpts.insert("url", optarg);
break;
case 'd':
clOpts.insert("downloadDir", optarg);
break;
case 'c':
clOpts.insert("configFile", optarg);
case 'D':
clOpts.insert("debug", optarg);
break;
case 'h':
clOpts.insert("help", "help");
break;
}
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
}
if (clOpts.contains("help"))
printHelp();
if (clOpts.contains("debug"))
debugMode = clOpts.value("debug").toInt();
else
debugMode = -1;
// look for config file
QString configFilePath;
QFileInfo confInfo;
if (clOpts.contains("configFile"))
configFilePath = clOpts.value("configFile");
else {
confInfo = QFileInfo(QDir::home(), ".fbgui.conf");
if (confInfo.exists())
configFilePath = confInfo.absoluteFilePath();
else {
confInfo = QFileInfo(QString("/etc/fbgui.conf"));
if (confInfo.exists())
configFilePath = QString("/etc/fbgui.conf");
else
configFilePath = DEFAULT_CONFIG_PATH;
}
}
// read the config file
QSettings confFileSettings(configFilePath, QSettings::IniFormat);
confFileSettings.setIniCodec("UTF-8");
if (clOpts.contains("url")) {
baseURL = QUrl(clOpts.value("url"));
}
else if (confFileSettings.contains("default/url")) {
baseURL = confFileSettings.value("default/url").toUrl();
}
else {
baseURL = DEFAULT_URL;
}
// setting directory for downloads
if (clOpts.contains("downloadDir")){
downloadPath = clOpts.value("downloadDir");
}
else if (confFileSettings.contains("default/downloadDirectory")){
downloadPath = confFileSettings.value("default/downloadDirectory").toString();
}
else {
downloadPath = DEFAULT_DOWNLOAD_DIR;
}
if (confFileSettings.contains("default/updateInterval")){
updateInterval = confFileSettings.value("default/updateInterval").toInt();
}
else updateInterval = DEFAULT_UPDATE_INTERVAL;
fbgui gui;
gui.show();
return app.exec();
}
|