#include "fbgui.h" #include "sysinfo.h" #include "loggerengine.h" #include "downloadmanager.h" #include "javascriptinterface.h" #include "sysinfolibsysfs.h" #include #include #include QString fileToTriggerURL(""); QString sessionID(""); QUrl baseURL(""); QString binPath(""); QString downloadPath(""); int updateInterval = -1; int debugMode = -1; //------------------------------------------------------------------------------------------- fbgui::fbgui() { // test for libsys function SysInfoLibsysfs* sil = new SysInfoLibsysfs(); sil->getInfoAboutNetworkInterface(); sil->getInfoAboutClassNet(); // setup basic debug qxtLog->disableLoggerEngine("DEFAULT"); qxtLog->enableLogLevels(QxtLogger::DebugLevel); if (debugMode == 0){ qxtLog->addLoggerEngine("std_logger", new LoggerEngine_std); qxtLog->initLoggerEngine("std_logger"); qxtLog->setMinimumLevel("std_logger", QxtLogger::DebugLevel); qxtLog->debug() << "Initializing fbgui..."; } // base of the gui _webView = new QWebView(this); // debug console split or normal browser if (debugMode == 1) setupDebugSplit(); else { setCentralWidget(_webView); } // initialize javascript interface JavascriptInterface* jsi = new JavascriptInterface(_webView->page()->mainFrame()); QObject::connect(jsi, SIGNAL(quitFbgui()), this, SLOT(close())); QObject::connect(_webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), jsi, SLOT(attachToDOM())); // initialize download manager DownloadManager* dm = new DownloadManager(); QObject::connect(dm, SIGNAL(downloadInfo(const QString&, const double&)), jsi, SLOT(downloadInfo(const QString&, const double&))); QObject::connect(dm, SIGNAL(notify(const QString&)), jsi, SLOT(notify(const QString&))); QObject::connect(jsi, SIGNAL(requestFile(const QString&)), dm, SLOT(downloadFile(const QString&))); QObject::connect(dm, SIGNAL(updateProgress(const int&, const double&, const QString&)), jsi, SLOT(updateProgressBar(const int&, const double&, const QString&))); QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(callbackOnDlQueueFinished())); createActions(); _webView->load(QUrl("qrc:/html/loadAbout.html")); // watch creation of fileToTriggerURL watchFileTrigger(); // set properties setWindowTitle("fbgui"); setAttribute(Qt::WA_QuitOnClose, true); setWindowFlags(Qt::FramelessWindowHint); showFullScreen(); } //------------------------------------------------------------------------------------------- void fbgui::createActions() { // CTRL + X to kill the gui _quit = new QAction(tr("&quit"), this); _quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_X)); this->addAction(_quit); connect(_quit, SIGNAL(triggered()), this, SLOT(close())); } //------------------------------------------------------------------------------------------- void fbgui::watchFileTrigger() { // check if the directory to fileToTriggerURL exists QFileInfo fi(fileToTriggerURL); if (!fi.absoluteDir().exists()){ qxtLog->debug() << fi.absolutePath() << " does not exists!"; if (QDir::home().mkdir(fi.absolutePath())) qxtLog->debug() << "Successfully created " << fi.absolutePath(); else qxtLog->debug() << "Failed to create " << fi.absolutePath(); } else qxtLog->debug() << fi.absolutePath() << " exists!"; // check if fileToTriggerURL already exists if (fi.exists()){ qxtLog->debug() << fileToTriggerURL << " exists already!"; // try to remove it QFile file(fi.absoluteFilePath()); if (file.remove()) qxtLog->debug() << "Purged: " << file.fileName(); else{ qxtLog->debug() << "Could not remove: " << file.fileName(); exit(EXIT_FAILURE); } } else { // watch the path where trigger file is expected qxtLog->debug() << "Watching " << fi.absolutePath() << " for file: " << fi.fileName(); QStringList pathToWatch(fi.absolutePath()); _watcher = new QFileSystemWatcher(pathToWatch, this); QObject::connect(_watcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(checkForTrigger(const QString&))); } } //------------------------------------------------------------------------------------------- void fbgui::checkForTrigger(const QString& dirname) { // check if fileToTriggerURL exists in the directory where the change occured QFileInfo tfi(fileToTriggerURL); QFileInfo fi(dirname + "/" + tfi.fileName()); if (fi.exists()){ qxtLog->debug() << fileToTriggerURL << " detected."; loadURL(); } else // do nothing / keep watching qxtLog->debug() << "weird file!"; } //------------------------------------------------------------------------------------------- bool fbgui::checkHost() const { QHostInfo hostInfo = QHostInfo::fromName(baseURL.host()); if (hostInfo.error() != QHostInfo::NoError){ qxtLog->debug() << "Lookup of " << baseURL.host() << "failed. Exiting..."; return false; } else{ qxtLog->debug() << "Lookup of " << baseURL.host() << " succeeded."; return true; } } //------------------------------------------------------------------------------------------- void fbgui::loadURL(){ // TODO actual data... // PBS POST data tests QNetworkRequest req(baseURL); QByteArray postData("mac=d8:d3:85:80:81:8b&hardwarehash=12341234123412341234123412341234&bootisoID=1&serial="); // serial number QByteArray serial; // fetch serial number from rootfs // try to open, TODO configurable path QFile file("/serial"); if (!file.exists()){ qxtLog->debug() << "No such file: " << file.fileName(); serial = "10-23-43-55-67"; // tests } if (!file.open(QIODevice::ReadOnly)){ qxtLog->debug() << "Could not open: " << file.fileName(); serial = "10-23-43-55-67"; // tests } // everything ok, read data serial = file.readAll(); file.close(); if (serial.isEmpty()) qxtLog->debug() << "No data in file: " << file.fileName(); //qxtLog->debug() << "Serial number is: " << serial; // load if host is valid if (checkHost()){ qxtLog->debug() << "Loading URL..."; _webView->load(req, QNetworkAccessManager::PostOperation, postData); } } //------------------------------------------------------------------------------------------- void fbgui::setupDebugSplit() { _debugConsole = new QTextEdit(this); _debugConsole->setWindowFlags(Qt::FramelessWindowHint); QPalette pal; pal.setColor(QPalette::Base, Qt::black); _debugConsole->setPalette(pal); _debugConsole->setTextColor(Qt::white); _debugConsole->insertPlainText("Debug console initialized.\n"); // enable custom logger engine qxtLog->addLoggerEngine("fb_logger", new LoggerEngine_fb(_debugConsole)); //qxtLog->initLoggerEngine("fb_logger"); qxtLog->setMinimumLevel("fb_logger", QxtLogger::DebugLevel); // display browser and debug in a splitter _splitter = new QSplitter(Qt::Vertical, this); _splitter->addWidget(_webView); _splitter->addWidget(_debugConsole); // CTRL + D toggles debug window _toggleDebug = new QAction(tr("&toggleDebug"), this); _toggleDebug->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D)); addAction(_toggleDebug); connect(_toggleDebug, SIGNAL(triggered()), this, SLOT(toggleDebug())); //_webView->load(QUrl("qrc:/html/loadAbout.html")); setCentralWidget(_splitter); } //------------------------------------------------------------------------------------------- void fbgui::toggleDebug() { if (_debugConsole->isVisible()) _debugConsole->hide(); else _debugConsole->show(); }