summaryrefslogblamecommitdiffstats
path: root/src/fbgui/ndgui.cpp
blob: 447b396016ebf335b638d0b7f23f24f6bed963cc (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 _tryAgain;
	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;
	_userChoice = false;

	addActions();

	_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(connectionEstablished(QString)), this,
			SLOT(handleConnectionEstablished(QString)));
	connect(_networkDiscovery, SIGNAL(allProcessesFinished()), this,
			SLOT(handleAllProcessesFinished()));
	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"));
	}
	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, SLOT(setUserChoiceTrue()));
	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 set userChoice true
 *
 * is the connected to the triggered action pressing the F5 button.
 * set the _userChoice member true
 */
void ndgui::setUserChoiceTrue() {
	_userChoice = true;
}

/**
 * @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->initAndRun(_userChoice);
	} else {
		LOG4CXX_DEBUG(ndLogger, "NetworkDiscovery already started");
	}
}

/**
 * @brief handle if a interface is able to connect
 *
 * 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);
}

/**
 * @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() {
	LOG4CXX_DEBUG(ndLogger, "all Processes finished");
	_allowUserChoice->setEnabled(false);
	if (_ifNameList.size() > 0) {
		if (_userChoice) {
			_jsi->chooseInterfaceDialog(_ifNameList);
		} else {
			foreach(QString i, _ifNameList)
			{
				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 _tryAgain;
	delete _networkDiscovery;

	_ifNameList.clear();

	init();
}