summaryrefslogtreecommitdiffstats
path: root/src/fbbrowser.cpp
blob: 51cf7d6faaaca0a7aeea32ae0a61f7a704f2a963 (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
#include "fbbrowser.h"

#include <QFile>
#include <QFileInfo>
#include <QtWebKit>
#include "jsObject.h"

fbbrowser::fbbrowser(const QUrl & url)
{
	view = new QWebView(this);
 	baseUrl = url;
	// Create QNetworkAccessManager which is needed to send/receive requests.
	manager = new QNetworkAccessManager(this);
	// Create a QNetworkRequest object and set its URL.
	request.setUrl(url);

	// Let the manager send the request and receive the reply.
        reply = manager->get(request);

	// TODO: error differentiation
        // reply->error() returns 0 even for invalid URL.
        // A possibility to check for validity, is to listen to readyRead()
        // signal, haven't found a better way yet ...
	if(reply->error() == QNetworkReply::NoError)
	{
		//qDebug() << "No error, loading given URL...";
		view->load(url);
	}
	else 
	{
	    qDebug() << "QNetworkReply error code is: " << reply->error();
		qDebug() << "Error occured, loading error page...";
		view->load(QUrl("qrc:/html/errorPage.html"));
	}
	// **** TEST ****
	DownloadManager* dm = new DownloadManager(baseUrl);
	QObject::connect(this, SIGNAL(downloadFile(QString)), dm, SLOT(downloadFile(QString)));
	emit downloadFile(QString("test.php"));
	emit downloadFile(QString("blacklist.txt"));
	emit downloadFile(QString("whitelist.tar.gz"));

	qDebug() << "DM blocking app?";
	// **** TEST ****

	//remove the window decoration
	this->setWindowFlags(Qt::SplashScreen);

	//enable JavaScript access to qt objects
	QObject::connect(view->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJSObject()));

	//set form to fullscreen
	this->showFullScreen();

	setCentralWidget(view);
}

// Destructor
fbbrowser::~fbbrowser()
{
}

//
void fbbrowser::addJSObject()
{
	jso = new jsObject();
	view->page()->mainFrame()->addToJavaScriptWindowObject(QString("jsObject"), jso);
	
	// connect the signals of the jsObject to the slots of the fbbrowser
	connectJsSignalsToSlots();
}

void fbbrowser::connectJsSignalsToSlots() 
{
	QObject::connect(jso, SIGNAL(closeBrowser()), this, SLOT(quitAll()));
	QObject::connect(jso, SIGNAL(startDownload()), this, SLOT(startDownload_Slot()));
	QObject::connect(jso, SIGNAL(getMacAddress()), this, SLOT(getMacAddress_Slot()));
	QObject::connect(jso, SIGNAL(getIpAddress()), this, SLOT(getIpAddress_Slot()));
	QObject::connect(jso, SIGNAL(getIntegratedHardwareDevices()), this, SLOT(getIntegratedHardwareDevices_Slot()));
	QObject::connect(jso, SIGNAL(getUsbDevices()), this, SLOT(getUsbDevices_Slot()));
	QObject::connect(jso, SIGNAL(getHardDrives()), this, SLOT(getHardDrives_Slot()));

	// for testing reasons
	QObject::connect(jso, SIGNAL(showTime()), this, SLOT(showTime_Slot()));
	QObject::connect(jso, SIGNAL(showDate()), this, SLOT(showDate_Slot()));
	QObject::connect(jso, SIGNAL(showHelloWorld()), this, SLOT(showHelloWorld_Slot()));
}

void fbbrowser::writeText(QString text)
{
	QFile file("out.txt");
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
		return;

	QTextStream out(&file);
	out << text << "\n";
}

// This function needed now ?
void fbbrowser::quitAll()
{
	emit signalQuitAll();
}

void fbbrowser::startDownload_Slot()
{

}

void fbbrowser::getMacAddress_Slot()
{

	QNetworkInterface *qNetI = new QNetworkInterface();
	QList<QNetworkInterface> list;
   	list=qNetI->allInterfaces();
    	QString str;
	QString macAddress;
	for (int i = 0; i < list.size(); ++i) {
  		str = list.at(i).name();
		macAddress = list.at(i).hardwareAddress();
		qDebug() << str;
		qDebug() << macAddress;
	}	 	
	
	//TODO:: edit jsFunction name
	QString code;
	code = QString("printMacAddress(\"%1\")").arg(macAddress);
	view->page()->mainFrame()->evaluateJavaScript(code);
}
	
void fbbrowser::getIpAddress_Slot()
{

}

void fbbrowser::getIntegratedHardwareDevices_Slot()
{

}

void fbbrowser::getUsbDevices_Slot()
{

}

void fbbrowser::getHardDrives_Slot()
{

}

// for testing reasons
void fbbrowser::showTime_Slot()
{
	qDebug() << "---- call: showTime_Slot";
	QString time = QTime::currentTime().toString("hh:mm:ss");
	
	//TODO:: edit jsFunction name
	QString code;
	code = QString("printTime(\"%1\")").arg(time);
	view->page()->mainFrame()->evaluateJavaScript(code);
}

void fbbrowser::showDate_Slot()
{
	QString date = QDate::currentDate().toString("dd.MM.yyyy");
	//TODO:: edit jsFunction name
	//view->page()->mainFrame()->evaluateJavaScript("");
}

void fbbrowser::showHelloWorld_Slot()
{
	view->page()->mainFrame()->evaluateJavaScript("alert(\"Hello World\")");
}

void fbbrowser::getSysInfo()
{
	/*
	QString time = QTime::currentTime().toString("hh:mm:ss");
	QString date = QDate::currentDate().toString("dd.MM.yyyy");
	QList<QHostAddress> ipList = QNetworkInterface::allAddresses();
	QString macAddress = QNetworkInterface::hardwareAddress();
	*/
}