summaryrefslogblamecommitdiffstats
path: root/src/fbgui/javascriptinterface.cpp
blob: 8260fabf420689f27782174ddcae80165a0db2d0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13



                                








                                                    
                                                                                                         

                                                                                                         



                 
                          
   
                                                             
                                                                    
                    

                                                                                                         


                       

                                                                    
                                                                                                         









                                                                      
                                         

                                                                

                                                                                                         










                                                                     
                                        
                                       


                                             
 


                                                   



                                 
                                                





                                                             
 
                                         
                                                                  
       

    

                                                                                                         

                                                                                                         






                                                                    
                                                                  





                                                             

                                                                                                         







                                                                    
                                                                          
                                                          
                                                    

                                                                                                         



















                                                                                  
                                                                    

                           

                                                                                                         






                                                                    
                                  
                    
 











                                                                                                         
                         











                                                                                                         
                       
 


                                                                                                         






                                                                     



                                                                        
                                     

                                                                                                         


                                        

                                                                                       


                                   

                                                               

                    

                                                                              
                                     

                                                                                                         
   

                                                                                             
   
                                                      
                                                  

                                                    

                                                                                                         
   
                                                                                                     
   
                                                

                                                                   
 
#include "fbgui.h"
#include "javascriptinterface.h"
#include "sysinfo.h"


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

using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr jsiLogger(Logger::getLogger("fbgui.jsi"));


//-------------------------------------------------------------------------------------------------------
//                                         Initialisation
//-------------------------------------------------------------------------------------------------------
/**
 * A constructor.
 *
 * @param parent
 *   Is of type QWebFrame.
 */
JavascriptInterface::JavascriptInterface(QWebFrame *parent) {
   LOG4CXX_DEBUG(jsiLogger, "Initializing javascript interface...");
   _parent = parent;
}
//-------------------------------------------------------------------------------------------------------
/**
 * An empty destructor.
 */
JavascriptInterface::~JavascriptInterface() { /* destructor dummy */
}
//-------------------------------------------------------------------------------------------------------
/**
 * Attaches an instance of this class to the DOM of the HTML page.
 *
 * Attaches an instance of this class to the DOM of the HTML page.
 * This enables the possibility to call slots/methods of this class in
 * JavaScript functions of HTML page. It also calls the
 * JavascriptInterface::loadJQuery() method.
 *
 * @see JavascriptInterface::loadJQuery()
 */
void JavascriptInterface::attachToDOM() {
   _parent->addToJavaScriptWindowObject(QString("fbgui"), this);
   loadJQuery();
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method load the required jQuery libraries into the HTML page.
 *
 * This method load the required jQuery libraries into the HTML page.
 * The libraries are contained in the fbgui.qrc file.
 * The Path to the files is: ":/html/js".
 * Each library will be read and loaded into the HTML page via
 * the evaluateJavaScript() method.
 *
 * @see JavascriptInterface::attachToDOM()
 */
void JavascriptInterface::loadJQuery() {
   // to test if this actually works...
   QString js;
   QString pathToJsDir(DEFAULT_QRC_HTML_DIR);
   pathToJsDir.append("/js");

   QDir qrcJSDir(pathToJsDir);
   QFileInfoList fiList = qrcJSDir.entryInfoList();
   QFileInfo fi;
foreach(fi, fiList)
{
   if (fi.suffix() == "js") {
      //qDebug()<< fi.fileName();
      //LOG4CXX_DEBUG(jsiLogger, fi.fileName());
      if (fi.fileName() != "test.js") {
         QFile file;
         file.setFileName(pathToJsDir + "/" + fi.fileName());
         file.open(QIODevice::ReadOnly);
         js = file.readAll();
         file.close();

         _parent->evaluateJavaScript(js);
         //LOG4CXX_DEBUG(jsiLogger, "evaluated " + fi.fileName());
      }
   }
}
}
//-------------------------------------------------------------------------------------------------------
//                                Javascript functions for webpage
//-------------------------------------------------------------------------------------------------------
/**
 * This method start a download.
 *
 * This method start a download.
 * Can be called from inside a JavaScript function of the HTML page.
 * Emits the JavascriptInterface::requestFile(const QString) signal.
 */
void JavascriptInterface::startDownload(const QString& filename) {
   // ignore if empty filename
   if (filename.isEmpty()) {
      _parent->evaluateJavaScript("alert(\"No filename!\")");
      return;
   }
   emit requestFile(filename);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method start a download.
 *
 * This method start a download.
 * Can be called from inside a JavaScript function of the HTML page.
 *
 * @todo add some more informations
 */
void JavascriptInterface::setCallbackOnFinished(const QString& function) {
   LOG4CXX_DEBUG(jsiLogger, "Callback set: " << function);
   _callbackOnDownloadsFinished = QString(function);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method delivers system informations.
 *
 * This method delivers system informations. Type of informations, are defined by
 * the parameter. The output of this method depends on the parameter.
 * Can be called from inside a JavaScript function of the HTML page.
 *
 * @param infoName
 *   Is of type QString. Defines which method will be called. Possible values are:
 *     - mac
 *     - ip
 *     - mbserial
 *     - usb
 *
 * @return QString
 *   the output of the called method or "info_error" if an error occurred
 *   (e. g. invalid parameter).
 *
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString JavascriptInterface::getSysInfo(const QString& info) {
   SysInfo si;
   return si.getInfo(info);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method quits the whole program.
 *
 * This method quits the whole program.
 * Can be called from inside a JavaScript function of the HTML page.
 * Emits JavascriptInterface::quitFbgui() signal
 */
void JavascriptInterface::quit() {
   emit quitFbgui();
}

//-------------------------------------------------------------------------------------------------------
/**
 * This method performs a shutdown of the client.
 *
 * This method performs a shutdown of the client.
 * Emits the JavascriptInterface::shutDownClient() signal.It is
 * connected with the fbgui::performShutDown() method.
 *
 * @see fbgui::performShutDown()
 */
void JavascriptInterface::shutDown() {
   emit shutDownClient();
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method performs a reboot of the client.
 *
 * This method performs a reboot of the client.
 * Emits the JavascriptInterface::rebootClient() signal. It is
 * connected with the fbgui::performReboot() method.
 *
 * @see fbgui::performReboot()
 */
void JavascriptInterface::reboot() {
   emit rebootClient();
}
//-------------------------------------------------------------------------------------------------------
//                            Download Manager information exchange
//-------------------------------------------------------------------------------------------------------
/**
 * This method delivers some informations about the downloading file.
 *
 * This method delivers some informations about the downloading file.
 *
 * @todo add some more informations
 */
void JavascriptInterface::downloadInfo(const QString& filename,
      const double& filesize) {
   QString code = QString("downloadInfo('\%1', \%2)").arg(filename).arg(
         filesize);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method updates the progress bar.
 *
 * This method calls a Javascript function to update the progress bar of the download.
 * Javascript must have a function called "updateProgress" to receive this information.
 *
 * @todo add some more informations
 */
void JavascriptInterface::updateProgressBar(const int& percent,
      const double& speed, const QString& unit) {
   if (percent == 0)
      return;
   QString code = QString("updateProgress(\%1, \%2, '\%3')").arg(percent).arg(
         speed).arg(unit);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method sends out messages to Javascript. A corresponding function must be implemented
 * on the webpage to receive these.
 */
void JavascriptInterface::notify(const QString& msg) {
   LOG4CXX_DEBUG(jsiLogger, "Notifying: " << msg);
   QString code = QString("notify('\%1')").arg(msg);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * Sets a callback function for when downloads are finished (will be called when the queue is empty).
 */
void JavascriptInterface::callbackOnFinished() {
   QString code = QString("\%1").arg(_callbackOnDownloadsFinished);
   _parent->evaluateJavaScript(code);
}