summaryrefslogblamecommitdiffstats
path: root/src/fbgui/ndgui.cpp
blob: 03e52542c6ac91ebd60f083a3709b889f6612094 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                                             
                  
                                     
 






                                                  


              

                        
 
 


             
                 
                                

                                 
 


                                                                              
                    
                                                         
                                   
 
                         

                                                   


                                                                                              



                                                                       
                                                                               
                                                              

                                                                                 

                                                                                      
                                                                                       

                                                                            

                                                               



                                                                              

                                                                                           
 




                                                                             
         


                     
                         
 
 
 
   
                     
  
                                                               
   
                          
                                                                
                                                                

                                                                                
                                          



                                                                        

 
   

                                     



                                                                    
                                     

                                
                                          
                
                                                                            


         
   

                                                                                                           






                                                                                      
                                                         
                                                          
                                            

                                                                                 
                                 
                                                                     
                        
                                                           



                                                                              
                                 
                         
                 
                
                                                                                                              
                                                          
                                                                                                  


         
   

                                    



                                                            
                                                 
                                                           
                                                                                
                                        
                                 
                              
                
                                                                                                             
         
 
 


                                                                                                  
                                                      
                                                                        
                                


                         
 
   
                               
   
                        
                                               
                                             
                                

                                 
                            
 
               
 
/**
 * @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 _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;

	_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(allProcessesFinished(bool)), this,
			SLOT(handleAllProcessesFinished(bool)));
	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"));
	}

	addActions();

	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->_networkDiscovery, SLOT(activateUserChoice()));
	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 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->init();
	} else {
		LOG4CXX_DEBUG(ndLogger, "NetworkDiscovery already started");
	}
}

/**
 * @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(bool userChoice) {
	LOG4CXX_DEBUG(ndLogger, "all Processes finished");
	_allowUserChoice->setEnabled(false);
	QList<QString> ifConnectedList = _networkDiscovery->getIfConnectedList();
	if (ifConnectedList.size() > 0) {
		if (userChoice) {
			_jsi->chooseInterfaceDialog(ifConnectedList);
		} else {
			foreach(QString i, ifConnectedList)
			{
				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 _networkDiscovery;

	_ifNameList.clear();

	init();
}