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
|
#include "slxbrowser.h"
#include <QMessageBox>
#include <QApplication>
#include <QKeyEvent>
#include <QCommandLineParser>
#include <QTextStream>
class KeyHandler : public QObject
{
public:
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if ( keyEvent->key() == Qt::Key_Q && (keyEvent->modifiers() & Qt::ControlModifier))
exit(0);
}
return QObject::eventFilter(obj, event);
}
};
QStringList loadUrlList(const QString &file);
/**
* MAIN
*/
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QCommandLineParser parser;
parser.addHelpOption();
parser.addPositionalArgument("url", "URL to load.");
QCommandLineOption ignoreSsl("insecure", "Ignore SSL errors.");
QCommandLineOption fullscreen("fullscreen", "Run browser in full screen.");
QCommandLineOption reloadInterval("reload-interval", "Reload displayed page every X seconds.", "seconds");
QCommandLineOption whitelist("whitelist", "Path to a file of allowed URLs. Mutually exclusive with blacklist.", "file");
QCommandLineOption blacklist("blacklist", "Path to a file of disallowed URLs. Mutually exclusive with whitelist.", "file");
parser.addOption(ignoreSsl);
parser.addOption(fullscreen);
parser.addOption(reloadInterval);
parser.addOption(whitelist);
parser.addOption(blacklist);
parser.process(app);
QStringList list(parser.positionalArguments());
if (list.empty()) {
QMessageBox::critical(nullptr, "Error", "Need one argument: file name");
return 1;
}
if (parser.isSet(whitelist) && parser.isSet(blacklist)) {
QMessageBox::critical(nullptr, "Error", "Need either blacklist or whitelist, not both");
return 2;
}
BrowserSettings settings;
settings.url = list[0];
settings.fullscreen = parser.isSet(fullscreen);
settings.ignoreSslErrors = parser.isSet(ignoreSsl);
settings.reloadInterval = parser.value(reloadInterval).toInt();
if (parser.isSet(whitelist)) {
settings.urlList = loadUrlList(parser.value(whitelist));
} else if (parser.isSet(blacklist)) {
settings.urlList = loadUrlList(parser.value(blacklist));
}
settings.isWhitelist = parser.isSet(whitelist);
SLXbrowser main(settings);
main.show();
app.installEventFilter(new KeyHandler());
app.exec();
return 0;
}
QStringList loadUrlList(const QString &file)
{
QStringList stringList;
QFile textFile(file);
if (!textFile.open(QFile::ReadOnly)) {
QTextStream(stdout) << "Cannot open URL list\n";
return QStringList();
}
QTextStream textStream(&textFile);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line);
}
return stringList;
}
|