summaryrefslogtreecommitdiffstats
path: root/src/fbgui/javascriptinterfacendgui.cpp
blob: 4fd3763f43f97dfb7ffaa3f1aa8701baf706cb56 (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
/*
 * javascriptinterfacendgui.cpp
 *
 *  Created on: Feb 21, 2012
 *      Author: joe
 */

#include "javascriptinterfacendgui.h"

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

using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr ndjsiLogger(Logger::getLogger("ndgui.jsi"));

JavascriptInterfaceNDGUI::JavascriptInterfaceNDGUI(QWebFrame* parent, NetworkDiscovery* nd)
	: JavascriptInterface(parent){
	_nd = nd;
}

JavascriptInterfaceNDGUI::~JavascriptInterfaceNDGUI() {

}
//------------------------------------------------------------------------------------
//													SLOTS
//------------------------------------------------------------------------------------
/**
 * @brief show abortBoot dialog
 *
 * @param msg
 *        the message, displayed in the dialog.
 */
void JavascriptInterfaceNDGUI::abortBoot(const QString msg) {
	QString code = QString("abortBootDialog('\%1')").arg(msg);
	_targetFrame->evaluateJavaScript(code);
}

/**
 * @brief opens ths chooseInterfaceDialog
 *
 * @param msg
 *        the interfaces as json formated string. will be displayed in a select box.
 */
void JavascriptInterfaceNDGUI::chooseInterfaceDialog(const QList<QString> ifNameList) {

	QString jsonArr = "[";
	for (int i = 0; i < ifNameList.size() - 1; i++) {
		jsonArr += "\"" + ifNameList.value(i) + "\",";
	}
	jsonArr += "\"" + ifNameList.last() + "\"]";

	QString code = QString("chooseInterfaceDialog(\%1)").arg(jsonArr);
	_targetFrame->evaluateJavaScript(code);
}

/**
 * @brief updates the over all status
 *
 * @param status
 *        the new status message
 */
void JavascriptInterfaceNDGUI::updateStatus(const QString &status) {
   if (status == "")
      return;
   QString code = QString("updateStatus('\%1')").arg(status);
   _targetFrame->evaluateJavaScript(code);
}

/**
 * @brief updates the progress bar for each interface.
 *
 * @param	ifname
 * 			the name ot the interface to update
 *
 * @param	percent
 * 			the progress in percent
 */
void JavascriptInterfaceNDGUI::updateIfProgressBar(const QString &ifName, const int& percent) {
   if (percent == 0)
      return;
   QString code = QString("updateIfProgressBar('\%1',\%2)").arg(ifName).arg(percent);
   _targetFrame->evaluateJavaScript(code);
}

/**
 * @brief update the status for each interface
 *
 * @param	ifName
 * 			the name ot the interface to update
 *
 * @param 	status
 * 			the new status of the interface.
 */
void JavascriptInterfaceNDGUI::updateIfStatus(const QString &ifName, const QString &status) {
   if (ifName == "")
      return;
   QString code = QString("updateIfStatus('\%1','\%2')").arg(ifName).arg(status);
   _targetFrame->evaluateJavaScript(code);
}

void JavascriptInterfaceNDGUI::addInterface(const QString &ifName) {
   if (ifName == "")
      return;
   QString code = QString("addInterface('\%1')").arg(ifName);
   _targetFrame->evaluateJavaScript(code);
}

void JavascriptInterfaceNDGUI::continueBoot(const QString& ifName) {
	emit startFbgui(ifName);
}

/**
 * @brief just for debugging.
 */
void JavascriptInterfaceNDGUI::notifyCall(QString msg){
	LOG4CXX_DEBUG(ndjsiLogger, "Javascript notified: " << msg);
}

//------------------------------------------------------------------------------------
//												INVOKABLES
//------------------------------------------------------------------------------------

/**
 * @brief read the log file. Log File will be presented inside of a dialog.
 */
QString JavascriptInterfaceNDGUI::readLogFile() {
	QString retval("Contents of log file:\n");
	QFile logFile(logFilePath);
	if (logFile.exists()) {
		if (logFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
			while (!logFile.atEnd()) {
				retval.append(logFile.readLine());
			}
		} else {
			LOG4CXX_DEBUG(ndjsiLogger, "could not open log file");
			retval = "could not open log file";
		}
	} else {
		LOG4CXX_DEBUG(ndjsiLogger,
				"log file does not exist at: " << logFilePath);
		retval = " log file does not exist at: " + logFilePath;
	}
	return retval;
}

/**
 * @brief fills the drop down box of the manual interface configuration
 * dialog.
 */
QVariantList JavascriptInterfaceNDGUI::getManualConfInterfaces() {
	LOG4CXX_DEBUG(ndjsiLogger, "call getManualConfInterfaces");
	QVariantList jsonArr;
	QString debugOut;
	foreach (QString s, _nd->getIfUpList()) {
		QVariant e(s);
		jsonArr << e;
		debugOut += s + "; ";
	}
	LOG4CXX_DEBUG(ndjsiLogger, "value of jsonArr:" << debugOut);
	return jsonArr;
}

/**
 * @brief return a json formated interface configuration
 *
 * @param ifName
 *        the name of the interface
 */
QVariantMap JavascriptInterfaceNDGUI::getInterfaceConf(QString ifName) {
	InterfaceConfiguration* ifc = _nd->getInterfaceConfig(ifName);
	QVariantMap jsonObj;
	QList<QString> dns;
	if (ifc != NULL) {
		jsonObj.insert("ipaddr", ifc->getIpAddress());
		jsonObj.insert("netmask", ifc->getNetmask());
		jsonObj.insert("broadcast", ifc->getBroadcast());
		jsonObj.insert("gateway", ifc->getGateway());

		dns.clear();
		dns = ifc->getDnsservers().trimmed().split(" ");
		jsonObj.insert("dns", dns.first());
	}
	return jsonObj;
}

/**
 * @brief takes the entered manual configuration dates and delivers it
 * to the networkDiscovery for further actions.
 *
 * @param jsonArr
 *        a jsonArr which contains the manual entered interface configuration
 */
int JavascriptInterfaceNDGUI::ip4_setManualConfiguration(QVariantMap jsonArr) {
	return _nd->ip4_setManualConfiguration(jsonArr);

}