#include "slxbrowser.h" #include #include #include #include #include class KeyHandler : public QObject { public: bool eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(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; }