summaryrefslogblamecommitdiffstats
path: root/src/ndgui.cpp
blob: c0a390fb45a3b2013e6ce86e5f9ffb0696099c51 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                   
                          

















                                                                                                                                                                 
                         


                                                                          

                                                       





























































                                                                                             








                                                                                                                                                                        

              
                                                                              

























                                                                                      
                                                            





















































                                                                                
                                                                    

                                                                    
                                                                      


                                                                                  




                                                                   







                                                                    
                                              




                                              

                        







                                                                          
 
                                                                       



 






                                                                
                                                                  




                                           
                                                                  









































                                                                                           
                                              











































































































                                                                                         
                                                           
 
#include "ndgui.h"

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

	_tag = "[nd:ndgui]";

	_started = false;
	_userChoice = false;
	_tryAgain = 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);
	showFullScreen();

	_webView->load(QUrl("qrc:html/networkdiscovery_userchoice.html"));
	_webView->show();
	
	qxtLog->debug() << _tag << "start singel shot";
	QTimer::singleShot(2000, this, SLOT(prepareNetworkDiscover()));

}



ndgui::~ndgui() {

}



/**
 * creates an action which you can trigger with the F5 Button during the first
 * seconds.
 */
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);
}



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



/**
 * is connected to the singleShot event. Triggering this method means
 * that we go on with the main NetworkDiscovery screen.
 * connects the loadFinished signal of the _webView with the
 * startNetworkDiscovery slot and removes the
 * action.
 */
void ndgui::prepareNetworkDiscover() {
	connect(_webView, SIGNAL(loadFinished(bool)), this,
			SLOT(startNetworkDiscovery()));
	this->removeAction(_allowUserChoice);

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



/**
 * main starting point of the whole procedure.
 * disconnect the loadFinished signal with the startNetworkDiscovery
 * and starts the networkDiscovery.
 */
void ndgui::startNetworkDiscovery(){

	disconnect(_webView,SIGNAL(loadFinished(bool)), this, SLOT(startNetworkDiscovery()));
	if(!_started) {
		if (!_tryAgain) {
			_started = true;
			QStringList l;
			l << "-d";
			networkDiscovery.initAndRun("209.85.148.105", _userChoice, true, "/var/tmp/logfile","/var/tmp/qt_c_socket_custom", DEFAULT_PATHTODHCPCDEXE, &l);
		} else {
			_tryAgain = false;
			networkDiscovery.tryAgain();
		}
	}
	else {
		qxtLog->debug() << _tag << "NetworkDiscovery already started";
	}
}



/**
 * 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);
}



/**
 * 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() {
	qxtLog->debug() << _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!");
	}
}



/**
 * this method will restart the system.
 * triggered through a button click in the gui.
 */
void ndgui::restartSystem() {
	QFile file("/proc/sysrq-trigger");
	if (file.open(QIODevice::WriteOnly)) {
		file.write("b");
		file.close();
	} else {
		qxtLog->debug() << _tag << "Could not open /proc/sysrq-trigger";
	}
}



/**
 * this method will restart the system.
 * triggered through a button click in the gui.
 */
void ndgui::shutDownSystem() {
	QFile file("/proc/sysrq-trigger");
	if (file.open(QIODevice::WriteOnly)) {
		file.write("o");
		file.close();
	} else {
		qxtLog->debug() << _tag << "Could not open /proc/sysrq-trigger";
	}
}



/**
 * 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(QString ifName, int userChoice) {
	if (!userChoice) {
		QString text = "continue with interface: " + ifName;
		qxtLog->debug() << _tag << text << "no user choice";
	} else {
		QString text = "continue with interface: " + ifName;
		qxtLog->debug() << _tag << text << "with user choice";
		QString gateway = networkDiscovery.getGatewayForInterface(ifName);
		networkDiscovery.ip4_replaceDefaultRoute(ifName,gateway,0);
	}
	if (networkDiscovery.checkConnectivityViaTcp()) {
		emit initFbgui();
	} else {
		abortBoot("Interface was suddenly made unusable ");
	}
}



/**
 * read the log file. Log File will be presented inside of a dialog.
 */
QString ndgui::readLogFile() {
	qxtLog->debug() << _tag << "show log";
	return networkDiscovery.readLogFile();
}



/**/
void ndgui::tryAgain() {
	qxtLog->debug()<< _tag << " try again ";
	_tryAgain = true;
	_started = false;
	_ifNameList.clear();
	_manConfList.clear();
	createAction();
	_webView->load(QUrl("qrc:html/networkdiscovery_userchoice.html"));
	_webView->show();

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



/*test html gui version*/

/**
 * fills the drop down box of the manual interface configuration
 * dialog.
 */
QVariantList ndgui::getManualConfInterfaces() {
	qxtLog->debug() << _tag << "call getManualConfInterfaces";
	QVariantList jsonArr;
	foreach (QString s, _manConfList) {
		QVariant e(s);
		jsonArr << e;
	}
	qxtLog->debug() << _tag << "value of jsonArr:" << jsonArr;
	return jsonArr;
}



/**
 * takes the entered manual configuration dates and delivers it
 * to the networkDiscovery for further actions.
 */
int ndgui::ip4_setManualConfiguration(QVariantMap jsonArr) {
	return networkDiscovery.ip4_setManualConfiguration(jsonArr);

}



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

/**
 * stellt ein ndgui/fbgui Objekt zur verwendung durch die html bereit.
 */
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") {
            //qxtLog->debug()<< 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) {
	QString code = QString("abortBootDialog('\%1')").arg(msg);
	_webView->page()->mainFrame()->evaluateJavaScript(code);
}



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



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



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



/**
 * update the status for each interface
 *
 * @param	ifName
 * 			the name ot the interface to update
 *
 * @param 	status
 * 			the new status of the interface.
 */
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);
}



/**
 * adds an interface to the DOM tree. Creates its progress bar and it's status label.
 *
 * @param 	ifName
 * 			name of the new interface.
 */
void ndgui::addInterface(const QString &ifName) {
   if (ifName == "")
      return;
   _manConfList.append(ifName);
   QString code = QString("addInterface('\%1')").arg(ifName);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}



/**
 * just for debugging.
 */
void ndgui::notifyCall(QString msg){
	qxtLog->debug() << _tag << "------ called:" << msg;
}