summaryrefslogblamecommitdiffstats
path: root/LogReceiver/ndgui.cpp
blob: 7514d8a9e244ab4aefaba3b90550f70a6f7b08dd (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                  

                                   
 





                                                                                                                                                            
 
 


                                                           
 

                                                                              
 


                                                               
                                   






                                                              

 
                 





                                               
                                                                                     





                                                     


                                                 
 
                              

 

                                                            
 

                                                         
 
                                                              

 

                                                          

                                                  
                                                    



                                                    

                                                 

                                                    
                                                            

                                                    

                             

                                                          

                                                          







                                                                              
 
 

                                                                         



                                                 
                              








                                                                     
 
                                                 















                                                                             

                                                         







                                                                             
                             
 








                                                                                  


 

                                                                     
 

                                                                                  
 



                                                                  


 


                                                            

 

                               
 


                         








































































                                                                                           
 
#include "ndgui.h"

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

	connect(&logReceiver, SIGNAL(addInterface(const QString &)), this, SLOT(addInterface( const QString &)));
	connect(&logReceiver, SIGNAL(changeProgressBarValue(const QString & , const int& )), this, SLOT(updateIfProgressBar(const QString & , const int&)));
	//connect(&logReceiver, SIGNAL(connectionEstablished(interfaceconfiguration*)), this, SLOT(handleConnectionEstablished(interfaceconfiguration*)));
	//connect(&logReceiver, SIGNAL(abortBoot(QString)), this, SLOT(handleAbortBoot(QString)));
	//connect(&logReceiver, SIGNAL(updateStatusLabel(QString,QString)), this, SLOT(handleUpdateStatusLabel(QString, QString)));
	//connect(&logReceiver, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished()));


	_webView = new QWebView(this);
	connect(_webView, SIGNAL(loadFinished(bool)), this,
			SLOT(loadFinished(bool)));

	connect(_webView->page()->mainFrame(), SIGNAL(
	         javaScriptWindowObjectCleared()), this, SLOT(attachToDOM()));

	setCentralWidget(_webView);
	_webView->load(QUrl("qrc:html/networkdiscovery.html"));
	_webView->show();
	setWindowTitle(tr("NetD"));
	setAttribute(Qt::WA_QuitOnClose, true);
	setWindowFlags(Qt::FramelessWindowHint);

	logReceiver.initAndRun("/var/tmp/qt_c_socket_custom");
	numberOfInterfaces = 0;


}

ndgui::~ndgui() {

}

void ndgui::buildGui() {

	ndStatusLabel = new QLabel(tr("test"));
	ndStatusLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	ndStatusLabel->setAlignment(Qt::AlignCenter);
	ndStatusLabel->setMinimumSize(100, 20);

	// create interface group box
	createInterfaceGroupBox();

	mainLayout = new QVBoxLayout;
	mainLayout->addWidget(ndStatusLabel);
	mainLayout->addWidget(interfaceGroupBox);

	setLayout(mainLayout);
}

void ndgui::createInterfaceGroupBox() {
	interfaceGroupBox = new QGroupBox(tr("Interfaces"));

	interfaceGroupBoxLayout = new QVBoxLayout;
	/* add interfaces via addInterfacesToGroupBox()*/

	interfaceGroupBox->setLayout(interfaceGroupBoxLayout);
}

void ndgui::addNewInterface(QString ifName) {
	qDebug() << "receive interface to add:" << ifName;
	QHBoxLayout *hBoxLayout = new QHBoxLayout;
	QLabel *label = new QLabel(ifName);
	QLabel *labelStatus = new QLabel("waiting");
	QProgressBar *pBar = new QProgressBar(this);
	pBar->setRange(1, 100);
	pBar->setMaximumSize(200, 20);

	statusLabels.insert(ifName, labelStatus);
	progressBars.insert(ifName, pBar);

	hBoxLayout->addWidget(label, Qt::AlignLeft);
	hBoxLayout->addWidget(labelStatus, Qt::AlignCenter);
	hBoxLayout->addWidget(pBar, Qt::AlignRight);

	numberOfInterfaces++;

	interfaceGroupBoxLayout->addLayout(hBoxLayout, 2);
}

void ndgui::handleProgress(QString ifName, int newValue) {
	qDebug() << "<[---]> SLOT handleProgress activated with: " << ifName
			<< newValue;
	QProgressBar * pBar = progressBars.value(ifName);
	if (newValue >= pBar->value()) {
		pBar->setValue(newValue);
	} else {
		qDebug() << "Error: new value is smaller than the old value!";
	}
}

void ndgui::handleConnectionEstablished(interfaceconfiguration *ifConf) {
	finalUsableIntefacesMap.insert(ifConf->getInterface(), ifConf);
}

void ndgui::handleAbortBoot(QString msg) {
	qDebug() << "abort boot. reason:" << msg;
	showAbortBootDialog();
}

void ndgui::handleUpdateStatusLabel(QString ifName, QString status) {
	QLabel* label = statusLabels.value(ifName);
	label->setText(status);
}

void ndgui::handleAllProcessesFinished() {
	qDebug() << "all Processes finished";

	if (finalUsableIntefacesMap.size() > 0) {
		showChooseInterfaceDialog();
	} else {
		showAbortBootDialog();
	}
}

void ndgui::showAbortBootDialog() {
	aBD = new AbortBootDialog(this);
	connect(aBD, SIGNAL(showLogSignal()), this, SLOT(showLog()));
	connect(aBD, SIGNAL(restartSignal()), this, SLOT(restartSystem()));
	connect(aBD, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem()));
	aBD->setModal(true);
	aBD->show();
}

void ndgui::showChooseInterfaceDialog() {
	QStringList list(finalUsableIntefacesMap.keys());
	cID = new ChooseInterfaceDialog(list, this);
	connect(cID, SIGNAL(continueSignal(QString)), this,
			SLOT(continueBoot(QString)));
	connect(cID, SIGNAL(restartSignal()), this, SLOT(restartSystem()));
	connect(cID, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem()));
	cID->setModal(true);
	cID->show();
}

void ndgui::restartSystem() {

	if (qobject_cast<AbortBootDialog*> (QObject::sender()) > 0) {
		qDebug() << "received Signal restart abd";
		aBD->closeDialog();
	} else if (qobject_cast<ChooseInterfaceDialog*> (QObject::sender()) > 0) {
		qDebug() << "received Signal restart cid";
		cID->close();
	} else {
		qDebug() << "unknown sender" << QObject::sender();
	}

}

void ndgui::shutDownSystem() {
	if (qobject_cast<AbortBootDialog*> (QObject::sender()) > 0) {

		aBD->closeDialog();
	} else if (qobject_cast<ChooseInterfaceDialog*> (QObject::sender()) > 0) {

		cID->close();
	} else {
		qDebug() << "unknown sender" << QObject::sender();
	}

}

void ndgui::continueBoot(QString ifName) {
	QString text = "continue with interface: " + ifName;
	cID->close();
}

void ndgui::showLog() {
	qDebug() << "show log";
}

/*test html gui version*/
/* slots */

void ndgui::loadFinished(bool ok) {
	qDebug() << "load finished:" << ok;
}

/************************************************/
//////////////////////////////////////////////////
/************************************************/

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::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() << "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() << "call addInterface";
   QString code = QString("addInterface('\%1')").arg(ifName);
   _webView->page()->mainFrame()->evaluateJavaScript(code);
}
void ndgui::notifyCall(){
	qDebug() << "------ called";
}