diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-15 19:17:51 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-15 19:17:51 +0200 |
commit | e43cf73d33d731b5d817d98c3dcb3d66eba1f718 (patch) | |
tree | 20a2d826c95761d8317104c92e66be085a977a5e | |
parent | Profile editor plugin (diff) | |
download | smolbote-e43cf73d33d731b5d817d98c3dcb3d66eba1f718.tar.xz |
Move help and version functions to main (out of Configuration)
Make loading profiles a free function in Browser
-rw-r--r-- | config/main.cpp | 4 | ||||
-rw-r--r-- | lib/configuration/configuration.cpp | 19 | ||||
-rw-r--r-- | lib/configuration/configuration.h | 29 | ||||
-rw-r--r-- | src/browser.cpp | 45 | ||||
-rw-r--r-- | src/browser.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 36 |
6 files changed, 84 insertions, 51 deletions
diff --git a/config/main.cpp b/config/main.cpp index 36f79f5..569249c 100644 --- a/config/main.cpp +++ b/config/main.cpp @@ -17,8 +17,8 @@ int main(int argc, char **argv) app.setQuitOnLastWindowClosed(true); Configuration config; - config.parseCommandLine(argc, argv); - config.parseConfigFile(config.value<std::string>("config").value()); + config.parse(argc, argv); + config.parse(config.value<std::string>("config").value()); SettingsDialog dlg(&config); dlg.configPath = QString::fromStdString(config.value<std::string>("config").value()); diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 1829a5e..def997d 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -45,6 +45,7 @@ Configuration::Configuration() commandLine_desc.add_options() ("help,h", "Display this help.") ("version,v", "Display version information.") + ("build", "Display build branch and commit.") ("config,c", po::value<std::string>()->default_value(defaultUserConfigLocation()), "Set the configuration file.") ("socket,s", po::value<std::string>()->default_value(defaultSocketPath()), "Local server socket") @@ -122,7 +123,7 @@ Configuration::Configuration() Configuration::~Configuration() = default; -bool Configuration::parseConfigFile(const std::string &path) +bool Configuration::parse(const std::string &path) { std::ifstream f(path, std::ifstream::in); @@ -131,7 +132,7 @@ bool Configuration::parseConfigFile(const std::string &path) return true; } -bool Configuration::parseCommandLine(int argc, char **argv) +bool Configuration::parse(int argc, char **argv) { try { auto cmd = po::command_line_parser(argc, argv); @@ -145,20 +146,6 @@ bool Configuration::parseCommandLine(int argc, char **argv) return false; } - if(vm.count("help")) { - std::cout << qUtf8Printable(qApp->applicationName()) << " " << qUtf8Printable(qApp->applicationVersion()) << ": yet another no-frills browser" << std::endl; - std::cout << "Usage: " << qUtf8Printable(qApp->arguments().at(0)) << " [options] URL(s)" << std::endl; - - std::cout << std::endl << "Command-line Options: " << std::endl << commandLine_desc << std::endl; - std::cout << std::endl << "Configuration Options: " << std::endl << configuration_desc << std::endl; - exit(0); - } - - if(vm.count("version")) { - std::cout << qUtf8Printable(qApp->applicationName()) << " " << qUtf8Printable(qApp->applicationVersion()) << std::endl; - exit(0); - } - return true; } diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index af75122..1d0fb54 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -23,8 +23,17 @@ public: explicit Configuration(); ~Configuration(); - bool parseConfigFile(const std::string &path); - bool parseCommandLine(int argc, char **argv); + bool parse(const std::string &path); + bool parse(int argc, char **argv); + + const std::vector<boost::shared_ptr<boost::program_options::option_description>> &options() + { + return configuration_desc.options(); + } + + bool exists(const char *path) { + return vm.count(path) ? true : false; + } template <typename T> std::optional<T> value(const char *path) const @@ -64,12 +73,6 @@ public: return std::optional<T>(vm[path].as<T>()); } - const std::vector<boost::shared_ptr<boost::program_options::option_description>> &options() - { - return configuration_desc.options(); - } - - QHash<QString, QString> section(const std::string &prefix) const; QStringList positionalArguments() const { QStringList l; @@ -82,6 +85,16 @@ public: return l; } + QHash<QString, QString> section(const std::string &prefix) const; + const boost::program_options::options_description commandlineOptions() const + { + return commandLine_desc; + } + const boost::program_options::options_description configurationOptions() const + { + return configuration_desc; + } + private: boost::program_options::options_description commandLine_desc; boost::program_options::options_description configuration_desc; diff --git a/src/browser.cpp b/src/browser.cpp index 790abcb..09db2d1 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -20,6 +20,35 @@ #include <QDir> #include <QPluginLoader> +QVector<Plugin> loadPlugins(const QString &location) +{ + QDir pluginsDir(location); + QVector<Plugin> list; + + if(pluginsDir.exists()) { + const QStringList entries = pluginsDir.entryList(QDir::Files | QDir::Readable); + + for(const QString &name : entries) { + QPluginLoader loader(pluginsDir.absoluteFilePath(name)); + + if(loader.load()) { +#ifdef QT_DEBUG + qDebug("Loading plugin: %s [ok]", qUtf8Printable(name)); +#endif + Plugin p; + p.instance = std::shared_ptr<QObject>(loader.instance()); + list.append(p); + } else { +#ifdef QT_DEBUG + qDebug("Loading plugin: %s [failed]", qUtf8Printable(name)); +#endif + } + } + } + + return list; +} + Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { @@ -51,21 +80,7 @@ void Browser::setup(const QString &defaultProfile) Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); // plugins - QDir pluginsDir(QString::fromStdString(m_config->value<std::string>("plugins.path").value())); - if(pluginsDir.exists()) { - const QStringList entries = pluginsDir.entryList(QDir::Files | QDir::Readable); - for(const QString &name : entries) { - QPluginLoader loader(pluginsDir.absoluteFilePath(name)); - qDebug("Loading plugin %s: %s", qUtf8Printable(name), loader.load() ? "ok" : "failed"); - if(!loader.isLoaded()) { - qDebug("Error: %s", qUtf8Printable(loader.errorString())); - } else { - Plugin p; - p.instance = std::shared_ptr<QObject>(loader.instance()); - m_plugins.append(p); - } - } - } + m_plugins.append(loadPlugins(QString::fromStdString(m_config->value<std::string>("plugins.path").value()))); // url request filter m_urlFilter = std::make_shared<UrlRequestInterceptor>(QString::fromStdString(m_config->value<std::string>("filter.path").value())); diff --git a/src/browser.h b/src/browser.h index 3052279..01de9bb 100644 --- a/src/browser.h +++ b/src/browser.h @@ -15,6 +15,8 @@ #include <memory> #include <interfaces.h> +QVector<Plugin> loadPlugins(const QString &location); + class Configuration; class BookmarksWidget; class DownloadsWidget; diff --git a/src/main.cpp b/src/main.cpp index 548aec8..f1ac1ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,30 +7,46 @@ */ #include "browser.h" -#include "mainwindow/mainwindow.h" #include "version.h" -#include "webengine/webprofile.h" +#include <QFile> #include <configuration/configuration.h> #include <memory> -#include <QFile> +#include <iostream> int main(int argc, char **argv) { - Browser app(argc, argv); - - // set this, otherwise the webview becomes black when using a stylesheet - app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - // create and load configuration std::shared_ptr<Configuration> config = std::make_shared<Configuration>(); - if(!config->parseCommandLine(argc, argv)) { + if(!config->parse(argc, argv)) { qWarning("Check --help for usage."); return -1; } - if(!config->parseConfigFile(config->value<std::string>("config").value())) { + if(!config->parse(config->value<std::string>("config").value())) { qWarning("Error parsing config file."); } + if(config->exists("help")) { + std::cout << "smolbote " << SMOLBOTE_VERSION << ": yet another no-frills browser" << std::endl; + std::cout << "Usage: " << argv[0] << " [options] URL(s)" << std::endl; + + std::cout << std::endl << "Command-line Options: " << std::endl << config->commandlineOptions() << std::endl; + std::cout << std::endl << "Configuration Options: " << std::endl << config->configurationOptions() << std::endl; + return 0; + } + + if(config->exists("version")) { + std::cout << "smolbote " << SMOLBOTE_VERSION << std::endl; + return 0; + } + + if(config->exists("build")) { + std::cout << SMOLBOTE_BRANCH << ":" << SMOLBOTE_COMMIT; + return 0; + } + + Browser app(argc, argv); + // set this, otherwise the webview becomes black when using a stylesheet + app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); app.setConfiguration(config); // set up socket |