summaryrefslogtreecommitdiffstats
path: root/src/fbgui/ndgui.cpp
blob: 447b396016ebf335b638d0b7f23f24f6bed963cc (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
/**
 * @class ndgui
 *
 * @brief the GUI.
 *
 * This class is responsible for creating and displaying the user interface.
 * It also connects the webView via QWebBridge to javascript functions inside the html files.
 */



#include "ndgui.h"
#include "javascriptinterfacendgui.h"

#include <log4cxx/logger.h>
#include "qlog4cxx.h"

using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr ndLogger(Logger::getLogger("fbgui.nd"));

/**
 * constructor
 */
ndgui::ndgui() :
		agui() {
}

/**
 * destructor
 */
ndgui::~ndgui() {
	delete _allowUserChoice;
	delete _tryAgain;
	delete _networkDiscovery;
}

/**
 * @brief initialize all variables and prepare everything for a successful run
 */
void ndgui::init() {
	LOG4CXX_DEBUG(ndLogger, "Initializing ndgui...");
	setWindowTitle(tr("NetD"));

	_started = false;
	_userChoice = false;

	addActions();

	_networkDiscovery = new NetworkDiscovery();
	_jsi = new JavascriptInterfaceNDGUI(_webView->page()->mainFrame(), _networkDiscovery);


	connect(_networkDiscovery, SIGNAL(abortBoot(QString)), _jsi,
			SLOT(abortBoot(const QString)));
	connect(_networkDiscovery, SIGNAL(updateStatus(QString)), _jsi,
			SLOT(updateStatus(const QString&)));
	connect(_networkDiscovery, SIGNAL(addInterface(const QString &)), _jsi,
			SLOT(addInterface( const QString &)));
	connect(_networkDiscovery, SIGNAL(updateIfStatus(QString,QString)), _jsi,
			SLOT(updateIfStatus(const QString &, const QString &)));
	connect(_networkDiscovery,
			SIGNAL(changeProgressBarValue(const QString & , const int& )),
			_jsi, SLOT(updateIfProgressBar(const QString & , const int&)));
	connect(_networkDiscovery, SIGNAL(connectionEstablished(QString)), this,
			SLOT(handleConnectionEstablished(QString)));
	connect(_networkDiscovery, SIGNAL(allProcessesFinished()), this,
			SLOT(handleAllProcessesFinished()));
	connect(_jsi, SIGNAL(startFbgui(const QString&)), this,
			SLOT(continueBoot(const QString&)));
	connect(_networkDiscovery, SIGNAL(continueBootWithoutCheck(QString )),
			this, SLOT(continueBootWithoutCheck(QString)));

	connect(_webView->page()->mainFrame(), SIGNAL(
			javaScriptWindowObjectCleared()), _jsi, SLOT(attachToDOM()));
	connect(_webView, SIGNAL(loadFinished(bool)), this, SLOT(startNetworkDiscovery()));


	if (debugMode > -1) {
		_webView->load(QUrl("qrc:html/networkdiscovery_debug.html"));
	} else {
		_webView->load(QUrl("qrc:html/networkdiscovery.html"));
	}
	showFullScreen();

}

/**
 * @brief Add actions
 *
 * Add actions which you can trigger with the F5 and F9 Button.
 */
void ndgui::addActions() {
	_allowUserChoice = new QAction(tr("&userChoice"), this);
	_allowUserChoice->setShortcut(QKeySequence(Qt::Key_F5));
	connect(_allowUserChoice, SIGNAL(triggered()), this, SLOT(setUserChoiceTrue()));
	this->addAction(_allowUserChoice);
	_tryAgain = new QAction(tr("&tryAgain"), this);
	_tryAgain->setShortcut(QKeySequence(Qt::Key_F9));
	connect(_tryAgain, SIGNAL(triggered()), this, SLOT(tryAgain()));
	this->addAction(_tryAgain);
}

/**
 * @brief set userChoice true
 *
 * is the connected to the triggered action pressing the F5 button.
 * set the _userChoice member true
 */
void ndgui::setUserChoiceTrue() {
	_userChoice = true;
}

/**
 * @brief start the network discovery
 *
 * main starting point of the whole procedure.
 * disconnect the loadFinished signal with the startNetworkDiscovery
 * and starts the networkDiscovery.
 */
void ndgui::startNetworkDiscovery() {
	if (!_started) {
		_started = true;
		_networkDiscovery->initAndRun(_userChoice);
	} else {
		LOG4CXX_DEBUG(ndLogger, "NetworkDiscovery already started");
	}
}

/**
 * @brief handle if a interface is able to connect
 *
 * if we have a user choice (_userChoice = true) than networkDiscovery will
 * emit connectionEstablished signals.
 * Add the interface name to a _ifNameList. This list holds all interfaces
 * the user can choose out of.
 */
void ndgui::handleConnectionEstablished(QString ifName) {
	_ifNameList.append(ifName);
}

/**
 * @brief determines if we continue the boot sequence or if we show the chooseInterface or abortBoot dialog
 *
 * if we have a user choice (_userChoice = true) than networkDiscovery will
 * emit a allProcessesFinished signal if all processes are done.
 * This method determines if user will see an abort boot dialog (no interface names in
 * the ifNameList list) or an
 * choose interface dialog (one or more interface names in the list (add with
 * handleConnectionEstablished)).
 */
void ndgui::handleAllProcessesFinished() {
	LOG4CXX_DEBUG(ndLogger, "all Processes finished");
	_allowUserChoice->setEnabled(false);
	if (_ifNameList.size() > 0) {
		if (_userChoice) {
			_jsi->chooseInterfaceDialog(_ifNameList);
		} else {
			foreach(QString i, _ifNameList)
			{
				if (_networkDiscovery->checkConnectivity(i)) {
					continueBootWithoutCheck(i);
					break;
				}
			}
		}
	} else {
		LOG4CXX_DEBUG(ndLogger, " No usable interfaces found!: " << _networkDiscovery->GetErrorStr());
		LOG4CXX_DEBUG(ndLogger, " list is empty");
		_jsi->abortBoot("No usable interfaces found!"
				+ _networkDiscovery->GetErrorStr());
	}
}

/**
 * @brief continue the boot sequence
 *
 * represents the end of the NetworkDiscovery life time.
 * will start the fbgui screen. All networkDiscovery signals
 * will be ignored after this point.
 */
void ndgui::continueBoot(const QString& ifName) {
	if (_networkDiscovery->checkConnectivity(ifName)) {
		LOG4CXX_DEBUG(ndLogger, " continue with interface: " << ifName);
		gInterfaceName = ifName;
		emit initFbgui();
		this->close();
	} else {
		_jsi->abortBoot("Interface was suddenly made unusable. Please check the log and try again.");
	}
}

/**
 * @brief continue the boot sequence without further checking if the connection is still possible.
 */
void ndgui::continueBootWithoutCheck(QString ifName) {
	LOG4CXX_DEBUG(ndLogger, " continue with interface: " << ifName);
	gInterfaceName = ifName;
	emit initFbgui();
	this->close();
}

/**
 * @brief crashes everything :)
 */
void ndgui::tryAgain() {
	LOG4CXX_DEBUG(ndLogger, " try again ");
	_networkDiscovery->prepareTryAgain();
	delete _allowUserChoice;
	delete _tryAgain;
	delete _networkDiscovery;

	_ifNameList.clear();

	init();
}