#include "fbgui.h"
#include "downloadManager.h"
#include "javascriptInterface.h"
#include <iostream>
#include <QUrl>
#include <QDir>
#include <QHostInfo>
#include <QtWebKit>
QUrl baseURL(DEFAULT_URL);
QString binPath(QApplication::applicationDirPath());
QString downloadPath("/tmp/fbgui/downloads");
int updateInterval = DEFAULT_UPDATE_INTERVAL;
bool debug = false;
//-------------------------------------------------------------------------------------------
fbgui::fbgui()
{
/* TEST CODE */
logViewer *logView = new logViewer(this);
/* Dock widget to place logView on bottom */
QDockWidget *dw = new QDockWidget(QString("debug console"), this);
dw->setAllowedAreas(Qt::BottomDockWidgetArea);
dw->setWidget(logView);
/* Create toggle action (F4) and add to fbgui's action list */
QAction *toggleLog = dw->toggleViewAction();
toggleLog->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D));
this->addAction(toggleLog);
/* Dock logView to bottom */
addDockWidget(Qt::BottomDockWidgetArea, dw);
/* initialize "browser" */
checkHost();
QWebView *webView = new QWebView(this);
webView->load(baseURL);
/* 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(QString, double)),
jsi, SLOT(downloadInfo(QString, double)));
QObject::connect(jsi, SIGNAL(requestFile(QString&)), dm, SLOT(downloadFile(QString&)));
QObject::connect(dm, SIGNAL(updateProgress(int, double, QString)),
jsi, SLOT(updateProgressBar(int, double, QString)));
QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(callbackOnDlQueueFinished()));
setWindowTitle("fbgui");
setAttribute(Qt::WA_QuitOnClose, true);
setWindowFlags(Qt::FramelessWindowHint);
setCentralWidget(webView);
showFullScreen();
}
//-------------------------------------------------------------------------------------------
void fbgui::createActions(){
_act = new QAction(tr("&test"), this);
_act->setShortcut(QKeySequence(Qt::Key_F5));
this->addAction(_act);
connect(_act, SIGNAL(triggered()), this, SLOT(testAct()));
}
//-------------------------------------------------------------------------------------------
void fbgui::testAct(){
qDebug() << "Action triggered!";
}
//-------------------------------------------------------------------------------------------
void fbgui::checkHost() const
{
QHostInfo hostInfo = QHostInfo::fromName(baseURL.host());
if (hostInfo.error() != QHostInfo::NoError){
qDebug() << "Lookup of " << baseURL.host() << "failed. Exiting...";
exit(EXIT_FAILURE);
}
}