summaryrefslogtreecommitdiffstats
path: root/src/fbgui.cpp
blob: c9565882b0240794d078eb4f2720a48ddc17570c (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "fbgui.h"
#include "sysinfo.h"
#include "loggerengine.h"
#include "downloadmanager.h"
#include "javascriptinterface.h"
#include "sysinfolibsysfs.h"

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

QString serialLocation("");
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();

	// start basic console debug log
	qxtLog->disableLoggerEngine("DEFAULT");
	qxtLog->enableLogLevels(QxtLogger::DebugLevel);
	if (debugMode == 0 || debugMode == 1){
		qxtLog->addLoggerEngine("std_logger", new LoggerEngine_std);
		qxtLog->initLoggerEngine("std_logger");
		qxtLog->setMinimumLevel("std_logger", QxtLogger::DebugLevel);
		qxtLog->debug() << "Initializing fbgui...";
	}

	// setup base layout of the gui
	_webView = new QWebView(this);
	// debug console split or normal browser
	if (debugMode == 1)
		setupDebugSplit();
	else {
		setCentralWidget(_webView);
	}
	createActions();

	// 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(callbackOnFinished()));


	// load content
	_webView->load(QUrl("qrc:/html/loadAbout.html"));
	watchForTrigger();

	// 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::watchForTrigger()
{
    // check if the directory to fileToTriggerURL exists
    QFileInfo fi(fileToTriggerURL);
    if (!fi.absoluteDir().exists()){
    	qxtLog->debug() << "[watcher] " << fi.absolutePath() << " does not exists!";
    	if (QDir::home().mkdir(fi.absolutePath()))
    		qxtLog->debug() << "[watcher] Successfully created " << fi.absolutePath();
    	else
    		qxtLog->debug() << "[watcher] Failed to create " << fi.absolutePath();
    }
    else
    	qxtLog->debug() << "[watcher] " << fi.absolutePath() << " exists!";

    // check if fileToTriggerURL already exists
    if (fi.exists()){
    	qxtLog->debug() << "[watcher] " << fileToTriggerURL << " exists already!";
    	// try to remove it
    	QFile file(fi.absoluteFilePath());
    	if (file.remove())
    		qxtLog->debug() << "[watcher] Purged: " << file.fileName();
    	else{
    		qxtLog->debug() << "[watcher] Could not remove: " << file.fileName();
    		exit(EXIT_FAILURE);
    	}
    }
    else {
    	// watch the path where trigger file is expected
    	qxtLog->debug() << "[watcher] 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() << "[watcher] " << fileToTriggerURL << " detected.";
		loadURL();
	}
	else
		// do nothing / keep watching
		qxtLog->debug() << "[watcher] weird file!";
}
//-------------------------------------------------------------------------------------------
void fbgui::loadURL(){
	// load if host is valid
	if (checkHost()){
		qxtLog->debug() << "[gui] Loading URL...";
		QByteArray postData = generatePOSTData();
		qxtLog->debug() << "[gui] POST data: " << postData;
		QNetworkRequest req(baseURL);
		_webView->load(req, QNetworkAccessManager::PostOperation, postData);
	}
}
//-------------------------------------------------------------------------------------------
bool fbgui::checkHost() const
{
	QHostInfo hostInfo = QHostInfo::fromName(baseURL.host());
	if (hostInfo.error() != QHostInfo::NoError){
		qxtLog->debug() << "[gui] Lookup of " << baseURL.host() << "failed. Exiting...";
		return false;
	}
	else{
		qxtLog->debug() << "[gui] Lookup of " << baseURL.host() << " succeeded.";
		return true;
	}
}
//-------------------------------------------------------------------------------------------
QByteArray fbgui::generatePOSTData(){
	// hash testing
	SysInfo si;
	QByteArray data(si.getInfo("mac").toUtf8() + "|" + si.getInfo("ip").toUtf8());
	qxtLog->debug() << "[post] Hashing: " << data;
	QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
	qxtLog->debug() << "[post] MD5 Hash: " << hash.toHex();

	// fetch serial number from usb rootfs
	QByteArray serial;
	QFile file(serialLocation);
	if (!file.exists()){
		qxtLog->debug() << "[post] No such file: " << file.fileName();
		serial = "10-23-43-55-67"; // tests
	}
	if (!file.open(QIODevice::ReadOnly)){
		qxtLog->debug() << "[post] 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() << "[post] No data in file: " << file.fileName();

	serial.chop(1);
	qxtLog->debug() << "[post] Serial number is: " <<  serial;

	// construct final byte array
	QByteArray postData("bootisoID=1");
	postData.append("&mac=" + si.getInfo("mac"));
	postData.append("&hardwarehash=" + hash.toHex());
	postData.append("&serial=" + serial);
	return 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);
	// 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();
}