summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 21983cad721f422c18ab957ba19894e0cf470a3a (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
#include <QApplication>
#include <QSettings>
#include <QtCore>
#include <getopt.h>
#include <cstdlib>
#include <iostream>
#include "loggerengine.h"
#include "fbgui.h"
#include "ndgui.h"

#include "../common/fbgui.h"

void printHelp() {
   QTextStream qout(stdout);
   qout << QObject::tr("Usage: ./fbgui [OPTIONS]") << endl;
   qout << QObject::tr("Options:") << endl;
   qout << "-c <path>,  --config=<path>     " << QObject::tr("Path to configuration file.") << endl;
   qout << "-u <URL>,   --url=<URL>         " << QObject::tr("Sets the URL to be loaded.") << endl;
   qout << "-d <path>,  --download=<path>   " << QObject::tr("Specify the download directory.")
         << endl;
   qout << "-t <path,   --trigger=<path>    " << QObject::tr(
         "Specify location of the file triggering the URL load.") << endl;
   qout << "-s <path,   --serial=<path>     " << QObject::tr(
         "Specify location of the file containing the serial number.") << endl;
   qout << "-D <level>, --debug=<level>     " << QObject::tr("Activate debug mode. [0,1]") << endl;
   qout << "-h,         --help              " << QObject::tr("Prints this help.") << endl;
   qout.flush();
   exit( EXIT_SUCCESS);
}

int main(int argc, char *argv[]) {
   // Initialisation of the QApplication:
   //   In QT, every application is composed of two separate
   //   components: the GUI-Client and the GUI-Server.
   //
   //   The third parameter sets the application as the
   //   GUI-Server (aswell as the GUI-Client).

   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 using getopt
   QMap<QString, QString> clOpts;
   int longIndex = 0;
   static const char *optString = "c:u:d:s:t:D:hl:";
   static const struct option longOpts[] = { { "config", required_argument, NULL, 'c' }, { "url",
         required_argument, NULL, 'u' }, { "download", required_argument, NULL, 'd' }, { "serial",
         required_argument, NULL, 's' }, { "trigger", required_argument, NULL, 't' }, { "debug",
         required_argument, NULL, 'D' }, { "help", no_argument, NULL, 'h' }, { "log",
         required_argument, NULL, 'l' } };
   int opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
   while (opt != -1) {
      switch (opt) {
      case 'c':
         clOpts.insert("configFile", optarg);
         break;
      case 'l':
         clOpts.insert("logFile", optarg);
         break;
      case 'u':
         clOpts.insert("url", optarg);
         break;
      case 'd':
         clOpts.insert("downloadDir", optarg);
         break;
      case 's':
         clOpts.insert("serialLocation", optarg);
         break;
      case 't':
         clOpts.insert("trigger", optarg);
         break;
      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();
      // start basic debug output on terminal
      qxtLog->disableLoggerEngine("DEFAULT");
      qxtLog->enableLogLevels(QxtLogger::DebugLevel);
      qxtLog->addLoggerEngine("std_logger", new LoggerEngine_std);
      qxtLog->initLoggerEngine("std_logger");
      qxtLog->setMinimumLevel("std_logger", QxtLogger::DebugLevel);
   } else
      debugMode = -1;

   // look for config file either in:
   //   - the path found in the configuration file
   //   - the user's home directory (as .fbgui.conf)
   //   - /etc/fbgui.conf

   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");

   // set base URL to be loaded
   if (clOpts.contains("url"))
      baseURL = QUrl(clOpts.value("url"));
   else if (confFileSettings.contains("default/pbs_url"))
      baseURL = confFileSettings.value("default/pbs_url").toUrl();
   else
      baseURL = DEFAULT_URL;

   // set directory for downloads
   if (clOpts.contains("downloadDir"))
      downloadPath = clOpts.value("downloadDir");
   else if (confFileSettings.contains("default/download_directory"))
      downloadPath = confFileSettings.value("default/download_directory").toString();
   else
      downloadPath = DEFAULT_DOWNLOAD_DIR;

   // set update interval for download progress functions of download manager.
   if (confFileSettings.contains("default/update_interval"))
      updateInterval = confFileSettings.value("default/update_interval").toInt();
   else
      updateInterval = DEFAULT_UPDATE_INTERVAL;

   // set which file to watch to trigger loading of URL
   if (clOpts.contains("trigger"))
      fileToTriggerURL = clOpts.value("trigger");
   else if (confFileSettings.contains("default/file_trigger"))
      fileToTriggerURL = confFileSettings.value("default/file_trigger").toString();
   else
      fileToTriggerURL = DEFAULT_FILE_TRIGGER;

   // set serial location
   if (clOpts.contains("serialLocation"))
      serialLocation = clOpts.value("serialLocation");
   else if (confFileSettings.contains("default/serial_location"))
      serialLocation = confFileSettings.value("default/serial_location").toString();
   else
      serialLocation = QString("/serial"); // tests

   // save ip config location (file generated by uchpc)
   if (confFileSettings.contains("default/ip_config"))
      ipConfigFilePath = confFileSettings.value("default/ip_config").toString();

   // save path to log file
   if (clOpts.contains("logFile"))
      logFilePath = clOpts.value("logFile");
   else if (confFileSettings.contains("default/log_file"))
      logFilePath = confFileSettings.value("default/log_file").toString();
   else
      logFilePath = DEFAULT_LOG_FILE_PATH;

   // activate file logger if debug mode activated.
   if (debugMode > -1) {
      // start debug logging to file.
      qxtLog->addLoggerEngine("file_logger", new LoggerEngine_file(logFilePath));
      qxtLog->setMinimumLevel("file_logger", QxtLogger::DebugLevel);
   }

   // print config
   qxtLog->debug() << "*************   CONFIG INFO   *************";
   qxtLog->debug() << "configFilePath:   " << configFilePath.toUtf8();
   qxtLog->debug() << "logFilePath:      " << logFilePath.toUtf8();
   qxtLog->debug() << "ipConfigFilePath: " << ipConfigFilePath.toUtf8();
   qxtLog->debug() << "baseURL:          " << baseURL.toString().toUtf8();
   qxtLog->debug() << "downloadDir :     " << downloadPath.toUtf8();
   qxtLog->debug() << "trigger:          " << fileToTriggerURL.toUtf8();
   qxtLog->debug() << "serialLocation:   " << serialLocation.toUtf8();
   qxtLog->debug() << "*******************************************";

   // set invisible cursor
   //QWSServer::instance()->setCursorVisible(false);

   // set default keyboard / mouse drivers. TODO: fix this, doesn't work...
   //QWSServer::instance()->setDefaultKeyboard("TTY:/dev/tty0");
   //QWSServer::instance()->setDefaultMouse("IntelliMouse:/dev/mice");


   // start ndgui
   qxtLog->debug() << "Initializing ndgui...";
   ndgui ngui;
   fbgui gui;

   QObject::connect(&ngui, SIGNAL(initFbgui()), &gui, SLOT(init()));
   ngui.show();
   return app.exec();
}