From e1f240f490324f8c6f4d8132630ad85756f6d191 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 22 Apr 2018 16:10:18 +0200 Subject: Merge CommandLine functionality into Configuration --- src/CMakeLists.txt | 2 - src/browser.cpp | 3 +- src/commandline.cpp | 135 ---------------------------------------------------- src/commandline.h | 38 --------------- src/main.cpp | 26 ++++------ 5 files changed, 12 insertions(+), 192 deletions(-) delete mode 100644 src/commandline.cpp delete mode 100644 src/commandline.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1321604..dd325ec 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,8 +13,6 @@ add_executable(poi singleapplication.h browser.cpp browser.h - commandline.cpp - commandline.h ../data/resources.qrc # main window diff --git a/src/browser.cpp b/src/browser.cpp index 9b691e4..3f4805a 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -28,7 +28,8 @@ Browser::Browser(int &argc, char *argv[]) Browser::~Browser() { - m_bookmarks->save(); + if(m_bookmarks) + m_bookmarks->save(); qDeleteAll(m_windows); m_windows.clear(); } diff --git a/src/commandline.cpp b/src/commandline.cpp deleted file mode 100644 index 6082686..0000000 --- a/src/commandline.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "commandline.h" -#include "version.h" -#include -#include -#include - -inline std::string optionText(const QStringList &opts) -{ - std::string r; - for(auto i = opts.constBegin(); i != opts.constEnd(); ) { - if(i->length() == 1) { - r.append("-" + i->toStdString()); - } else { - r.append("--" + i->toStdString()); - } - - ++i; - if(i != opts.constEnd()) { - r.append(", "); - } - } - return r; -} - -inline void printOption(const QCommandLineOption &option) -{ - std::cout << " " - << std::setw(18) << std::left << optionText(option.names()) - << std::setw(32) << std::left << option.description().toStdString() - << std::setw(18) << std::left << option.defaultValues().join(", ").toStdString() - << std::endl; -} - -constexpr const char *socketPath() -{ -#if defined(Q_OS_UNIX) - // could be a path such as "/tmp/foo" - return "/tmp/smolbote.socket"; -#elif defined(Q_OS_WIN32) - // could be a pipe path such as "\\.\pipe\foo" - return "\\\\.\\pipe\\smolbote_socket"; -#endif -} - -CommandLine::CommandLine() - : QCommandLineParser() - , helpOption(addHelpOption()) - , versionOption(addVersionOption()) - , buildOption("build", "Displays build information.") - , configOption({ "c", "config" }, "Sets configuration file.", "path", Configuration::defaultUserConfigLocation()) - , profileOption({ "p", "profile" }, "Sets default profile.", "profile", "") - , socketOption("socket", "Sets local socket.", "name", socketPath()) - , newWindowOption("in-new-window", "Open URL in new window") -{ - setApplicationDescription("yet another no-frills browser"); - - addOption(buildOption); - addOption(configOption); - addOption(profileOption); - addOption(socketOption); - addOption(newWindowOption); - - addPositionalArgument("URL", "URL(s) to open"); -} - -void CommandLine::parseCommandLine(const QCoreApplication &app) -{ - QCommandLineParser::parse(app.arguments()); - - application = const_cast(&app); - - if(isSet(helpOption)) { - printHelp(); - } - - if(isSet(versionOption)) { - printVersion(); - } - - if(isSet(buildOption)) { - printBuild(); - } -} - -void CommandLine::printHelp(int exitCode) -{ - std::cout << "Usage: " << application->arguments().at(0).toStdString() << " [options] URL" << std::endl; - std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << ": " - << applicationDescription().toStdString() << std::endl - << std::endl; - - std::cout << "Options: " << std::endl; - printOption(helpOption); - printOption(versionOption); - printOption(buildOption); - printOption(configOption); - printOption(profileOption); - printOption(socketOption); - printOption(newWindowOption); - std::cout << std::endl; - - std::cout << "You can also overwrite configuration options using the syntax: " << std::endl - << "--browser.setting.path=value" << std::endl - << "More information on available keys can be found on the manual page." << std::endl - << std::endl; - - std::cout << "Arguments: " << std::endl; - std::cout << "- " - << std::setw(20) << std::left << "URL" - << std::setw(40) << std::left << "URL(s) to open" - << std::endl - << std::endl; - - exit(exitCode); -} - -void CommandLine::printVersion(int exitCode) -{ - std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << std::endl; - exit(exitCode); -} - -void CommandLine::printBuild(int exitCode) -{ - std::cout << SMOLBOTE_BRANCH << ":" << SMOLBOTE_COMMIT << std::endl; - exit(exitCode); -} diff --git a/src/commandline.h b/src/commandline.h deleted file mode 100644 index cb2f334..0000000 --- a/src/commandline.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_COMMANDLINE_H -#define SMOLBOTE_COMMANDLINE_H - -#include - -class CommandLine : public QCommandLineParser -{ -public: - CommandLine(); - - void parseCommandLine(const QCoreApplication &app); - - void printHelp(int exitCode = 0); - void printVersion(int exitCode = 0); - void printBuild(int exitCode = 0); - - const QCommandLineOption helpOption; - const QCommandLineOption versionOption; - const QCommandLineOption buildOption; - - const QCommandLineOption configOption; - const QCommandLineOption profileOption; - const QCommandLineOption socketOption; - const QCommandLineOption newWindowOption; - -private: - QCoreApplication *application; -}; - -#endif // SMOLBOTE_COMMANDLINE_H diff --git a/src/main.cpp b/src/main.cpp index 52df7ac..bfe4ecd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ */ #include "browser.h" -#include "commandline.h" #include "mainwindow/mainwindow.h" #include "version.h" #include "webengine/webprofile.h" @@ -18,36 +17,31 @@ int main(int argc, char **argv) { Browser app(argc, argv); - CommandLine parser; - parser.parseCommandLine(app); - // create and load configuration std::shared_ptr config = std::make_shared(); - config->read(parser.value(parser.configOption)); - - if(!parser.unknownOptionNames().isEmpty()) { - qDebug("config->parse: %s", config->parse(argc, argv) ? "true" : "false"); + if(!config->parseCommandLine(argc, argv)) { + qWarning("Check --help for usage."); + return -1; + } + if(!config->parseConfigFile(config->value("config").value())) { + qWarning("Error parsing config file."); } app.setConfiguration(config); // set up socket - bool isSingleInstance = app.bindLocalSocket(parser.value(parser.socketOption)); + bool isSingleInstance = app.bindLocalSocket(QString::fromStdString(config->value("socket").value())); #ifdef QT_DEBUG - qDebug("bindLocalSocket(%s) = %s", qUtf8Printable(parser.value(parser.socketOption)), isSingleInstance ? "true" : "false"); + qDebug("bindLocalSocket(%s) = %s", qUtf8Printable(QString::fromStdString(config->value("socket").value())), isSingleInstance ? "true" : "false"); #endif // if we are the only instance, set up the browser if(isSingleInstance) { - if(parser.isSet(parser.profileOption)) - app.setup(parser.value(parser.profileOption)); - else - app.setup(QString::fromStdString(config->value("profile.default").value())); - + app.setup(QString::fromStdString(config->value("profile.default").value())); QObject::connect(&app, &Browser::messageAvailable, &app, &Browser::createSession); } - app.sendMessage(parser.value(parser.profileOption), parser.isSet(parser.newWindowOption), parser.positionalArguments()); + app.sendMessage("", false, config->positionalArguments()); if(isSingleInstance) return app.exec(); -- cgit v1.2.1