summaryrefslogtreecommitdiffstats
path: root/NetworkDiscovery/ndgui.cpp
blob: bf3b05e035859079af49fc88afb1c4977e28db9c (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
#include "ndgui.h"

ndgui::ndgui(QMainWindow *parent) :
	QMainWindow(parent) {

	_tag = "[nd:ndgui]";

	_started = false;
	_userChoice = false;

	createAction();

	connect(&networkDiscovery, SIGNAL(addInterface(const QString &)), this, SLOT(addInterface( const QString &)));
	connect(&networkDiscovery, SIGNAL(changeProgressBarValue(const QString & , const int& )), this, SLOT(updateIfProgressBar(const QString & , const int&)));
	connect(&networkDiscovery, SIGNAL(connectionEstablished(QString)), this, SLOT(handleConnectionEstablished(QString)));
	connect(&networkDiscovery, SIGNAL(abortBoot(QString)), this, SLOT(abortBoot(const QString)));
	connect(&networkDiscovery, SIGNAL(updateStatusLabel(QString,QString)), this, SLOT(updateIfStatus(const QString &, const QString &)));
	connect(&networkDiscovery, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished()));
	connect(&networkDiscovery, SIGNAL(continueBoot(QString, int)), this, SLOT(continueBoot(QString, int)));



	_webView = new QWebView(this);
	connect(_webView->page()->mainFrame(), SIGNAL(
		         javaScriptWindowObjectCleared()), this, SLOT(attachToDOM()));
	setCentralWidget(_webView);
	setWindowTitle(tr("NetD"));
	setAttribute(Qt::WA_QuitOnClose, true);
	setWindowFlags(Qt::FramelessWindowHint);

	_webView->load(QUrl("qrc:html/networkdiscovery_userchoice.html"));
	_webView->show();

	QTimer::singleShot(2000, this, SLOT(prepareNetworkDiscover()));

}



ndgui::~ndgui() {

}



void ndgui::createAction() {
	_allowUserChoice = new QAction(tr("&quit"), this);
	_allowUserChoice->setShortcut(QKeySequence(Qt::Key_F5));
	connect(_allowUserChoice, SIGNAL(triggered()), this, SLOT(setUserChoiceTrue()));
	this->addAction(_allowUserChoice);
}



void ndgui::setUserChoiceTrue() {
	_userChoice = true;
}



void ndgui::prepareNetworkDiscover() {
	connect(_webView, SIGNAL(loadFinished(bool)), this,
			SLOT(startNetworkDiscovery()));
	this->removeAction(_allowUserChoice);

	_webView->load(QUrl("qrc:html/networkdiscovery.html"));
	_webView->show();
}



void ndgui::startNetworkDiscovery(){

	disconnect(_webView,SIGNAL(loadFinished(bool)), this, SLOT(startNetworkDiscovery()));
	if(!_started) {
		_started = true;
		networkDiscovery.initAndRun("209.85.148.105", _userChoice, true, "/var/tmp/logfile","/var/tmp/qt_c_socket_custom");
	}
	else {
		qDebug() << _tag << "NetworkDiscovery already started";
	}
}



void ndgui::handleConnectionEstablished(QString ifName) {
	_ifNameList.append(ifName);
}



void ndgui::handleAllProcessesFinished() {
	qDebug() << _tag << "all Processes finished";
	if(_ifNameList.size() > 0) {
		QString jsonArr = "[";
		for(int i = 0; i < _ifNameList.size()-1; i++) {
		  jsonArr += "\"" + _ifNameList.value(i) + "\",";
		}
		jsonArr += "\"" + _ifNameList.last() + "\"]";
		chooseInterfaceDialog(jsonArr);
	} else {
		abortBoot("No usable interfaces found!");
	}
}



void ndgui::restartSystem() {

}



void ndgui::shutDownSystem() {

}



void ndgui::continueBoot(QString ifName, int userChoice) {
	if (!userChoice) {
		QString text = "continue with interface: " + ifName;
		qDebug() << _tag << text << "no user choice";
	} else {
		QString text = "continue with interface: " + ifName;
		qDebug() << _tag << text << "with user choice";
		QString gateway = networkDiscovery.getGatewayForInterface(ifName);
		networkDiscovery.ip4_replaceDefaultRoute(ifName,gateway,0);
	}
	_webView->load(QUrl("qrc:html/continueBoot.html"));
}



QString ndgui::readLogFile() {
	qDebug() << _tag << "show log";
	return networkDiscovery.readLogFile();
}



/*test html gui version*/

QVariantList ndgui::getManualConfInterfaces() {
	qDebug() << _tag << "call getManualConfInterfaces";
	QVariantList jsonArr;
	foreach (QString s, _manConfList) {
		QVariant e(s);
		jsonArr << e;
	}
	qDebug() << _tag << "value of jsonArr:" << jsonArr;
	return jsonArr;
}



int ndgui::ip4_setManualConfiguration(QVariantMap jsonArr) {
	return networkDiscovery.ip4_setManualConfiguration(jsonArr);

}



/* slots */
/************************************************/
//////////////////////////////////////////////////
/************************************************/

void ndgui::attachToDOM(){
	_webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("fbgui"), this);
	loadJQuery();
}



void ndgui::loadJQuery() {
   QString js;
   QString pathToJsDir(":/html");
   pathToJsDir.append("/js");

   QDir qrcJSDir(pathToJsDir);
   QFileInfoList fiList = qrcJSDir.entryInfoList();
   QFileInfo fi;
   foreach(fi, fiList)
      {
         if (fi.suffix() == "js") {
            //qDebug()<< fi.fileName();
            //qxtLog->debug() << fi.fileName();
            if (fi.fileName() != "test.js" && fi.fileName() != "nd-functions.js") {
               QFile file;
               file.setFileName(pathToJsDir + "/" + fi.fileName());
               file.open(QIODevice::ReadOnly);
               js = file.readAll();
               file.close();

               _webView->page()->mainFrame()->evaluateJavaScript(js);
               //qxtLog->debug() << "evaluated " + fi.fileName();
            }
         }
      }
}



//diese methoden müssen später in die javascriptInterface Klasse eingefügt werden.

// dieser code muss später in die javascriptInterface klasse der fbgui eingefügt werden
// tausche dazu ndgui zu javascriptinterface
// und _webView->page()->mainFrame() zu _parent

void ndgui::abortBoot(const QString msg) {
	qDebug() << _tag << "call abortBoot:" << msg;
	QString code = QString("abortBootDialog('\%1')").arg(msg);
	_webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::chooseInterfaceDialog(const QString msg) {
	QString code = QString("chooseInterfaceDialog(\%1)").arg(msg);
	_webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::updateStatus(const QString &status) {
   if (status == "")
      return;
   QString code = QString("updateStatus('\%1')").arg(status);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::updateIfProgressBar(const QString &ifName, const int& percent) {
   if (percent == 0)
      return;
   qDebug() << _tag << "call updateIfProgressBar";
   QString code = QString("updateIfProgressBar('\%1',\%2)").arg(ifName).arg(percent);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::updateIfStatus(const QString &ifName, const QString &status) {
   if (ifName == "")
      return;
   QString code = QString("updateIfStatus('\%1','\%2')").arg(ifName).arg(status);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::addInterface(const QString &ifName) {
   if (ifName == "")
      return;
   qDebug() << _tag << "call addInterface";
   _manConfList.append(ifName);
   QString code = QString("addInterface('\%1')").arg(ifName);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}



void ndgui::notifyCall(QString msg){
	qDebug() << _tag << "------ called:" << msg;
}