summaryrefslogtreecommitdiffstats
path: root/src/fbgui.cpp
blob: f642da24e30a98fb7898f1e87ba9a870200513d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "fbgui.h"
#include "loggerEngine.h"
#include "downloadManager.h"
#include "javascriptInterface.h"

#include <iostream>
#include <QtWebKit>
#include <QxtCore>

QUrl baseURL(DEFAULT_URL);
QString binPath("");
QString downloadPath("/tmp/fbgui/downloads");
int updateInterval = DEFAULT_UPDATE_INTERVAL;
bool debug = false;


//-------------------------------------------------------------------------------------------
fbgui::fbgui(){
	/* setup qxt logger and enable custom std engine*/
	qxtLog->disableLoggerEngine("DEFAULT");
	qxtLog->addLoggerEngine("std_logger", new loggerEngine_std);
	qxtLog->initLoggerEngine("std_logger");
	qxtLog->setMinimumLevel("std_logger", QxtLogger::DebugLevel);
	qxtLog->debug() << "Initializing fbgui...";

	/* initliaze browser */
	checkHost();
	_webView = new QWebView(this);
	_webView->load(baseURL);

	/* debug split if debug mode, normal browser else */
	if (debug) 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(QString, double)),
					jsi, SLOT(downloadInfo(QString, double)));
	QObject::connect(dm, SIGNAL(notify(QString)),
					jsi, SLOT(notify(QString)));
	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);
	showFullScreen();
}
//-------------------------------------------------------------------------------------------
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::cyan);
	_debugConsole->insertPlainText("Debug console initialized.\n");
	/* switch engine to custom engine class "logger" */
	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()));
	setCentralWidget(_splitter);
}
//-------------------------------------------------------------------------------------------
void fbgui::toggleDebug(){
	if (_debugConsole->isVisible())
		_debugConsole->hide();
	else
		_debugConsole->show();
}
//-------------------------------------------------------------------------------------------
void fbgui::checkHost() const{
	// TODO instead of closing app, show error page from qrc
	QHostInfo hostInfo = QHostInfo::fromName(baseURL.host());
	if (hostInfo.error() != QHostInfo::NoError){
		qxtLog->debug() << "Lookup of " << baseURL.host() << "failed. Exiting...";
		exit(EXIT_FAILURE);
	}
}