|
|
#include "slxbrowser.h"
#include <QMessageBox>
#include <QApplication>
#include <QKeyEvent>
#include <QCommandLineParser>
#include <QTextStream>
class KeyHandler : public QObject
{
public:
KeyHandler(SlxBrowser *win) : QObject(), _win(win) {}
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);
_win->activity();
} else if (event->type() == QEvent::MouseMove) {
_win->activity();
}
return QObject::eventFilter(obj, event);
}
private:
SlxBrowser *_win;
};
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 maximized("maximized", "Maximize window on startup");
QCommandLineOption reloadInterval("reload-interval", "Reload displayed page every X seconds.", "seconds");
QCommandLineOption whitelist("whitelist", "Path to a file of allowed URLs.", "file");
QCommandLineOption blacklist("blacklist", "Path to a file of disallowed URLs.", "file");
QCommandLineOption zoom("zoom", "Default zoom level.", "percent");
parser.addOption(ignoreSsl);
parser.addOption(fullscreen);
parser.addOption(maximized);
parser.addOption(reloadInterval);
parser.addOption(whitelist);
parser.addOption(blacklist);
parser.addOption(zoom);
parser.process(app);
QStringList list(parser.positionalArguments());
if (list.empty()) {
QMessageBox::critical(nullptr, "Error", "Need one argument: file name");
return 1;
}
BrowserSettings settings;
settings.url = list[0];
settings.fullscreen = parser.isSet(fullscreen);
settings.maximized = parser.isSet(maximized);
settings.ignoreSslErrors = parser.isSet(ignoreSsl);
settings.reloadInterval = parser.value(reloadInterval).toInt();
if (parser.isSet(whitelist)) {
settings.whiteList = loadUrlList(parser.value(whitelist));
}
if (parser.isSet(blacklist)) {
settings.blackList = loadUrlList(parser.value(blacklist));
}
if (parser.isSet(zoom)) {
settings.zoom = parser.value(zoom).toInt();
} else {
settings.zoom = 100;
}
SlxBrowser main(settings);
main.show();
app.installEventFilter(new KeyHandler(&main));
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;
}
|