diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-02 13:24:45 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-02 13:24:45 +0200 |
commit | 76e346c8e5ac7067cc49063e0c11d88c23871115 (patch) | |
tree | d0bf22a28330a2feb675b7d68011f22a4473bdfd /src | |
parent | Split off UrlFilter into library (diff) | |
download | smolbote-76e346c8e5ac7067cc49063e0c11d88c23871115.tar.xz |
Add Util namespace
- Util::files lists files in specified .path
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/browser.cpp | 19 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/plugin.h | 51 | ||||
-rw-r--r-- | src/util.cpp | 45 | ||||
-rw-r--r-- | src/util.h | 20 |
6 files changed, 85 insertions, 58 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6162cd4..8d43df5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,8 @@ set(srclist browser.h session.cpp session.h + util.cpp + util.h # resources (icons, etc) ${PROJECT_SOURCE_DIR}/data/resources.qrc diff --git a/src/browser.cpp b/src/browser.cpp index e93123c..1542370 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -10,6 +10,7 @@ #include "addressbar/addressbar.h" #include "mainwindow/mainwindow.h" #include "subwindow/subwindow.h" +#include "util.h" #include "webengine/urlinterceptor.h" #include <QAction> #include <QDir> @@ -111,14 +112,20 @@ void Browser::setup(const QString &defaultProfile) // load profiles m_profileManager = new ProfileManager(m_config->section("profile"), defaultProfile, this); - // connect profiles - for(const QString &id : m_profileManager->idList()) { - auto *profile = m_profileManager->profile(id); - connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); - profile->setRequestInterceptor(m_urlFilter.get()); + for(const QString &profilePath : Util::files(m_config->value<QString>("profile.path").value(), { "*.profile" })) { + this->loadProfile(profilePath); } + // set default profile - WebProfile::setDefaultProfile(m_profileManager->profile(defaultProfile)); + { + const QString id = m_config->value<QString>("profile.default").value(); + auto *profile = m_profileManager->profile(id); + if(profile == nullptr) { + profile = this->loadProfile(id).second; + } + + WebProfile::setDefaultProfile(profile); + } // bookmarks m_bookmarks = std::make_shared<BookmarksWidget>(QString::fromStdString(m_config->value<std::string>("bookmarks.path").value())); diff --git a/src/main.cpp b/src/main.cpp index 98f2965..69389d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include "browser.h" #include "plugin.h" #include "session.h" +#include "util.h" #include "version.h" #include <QFile> #include <QLibraryInfo> @@ -86,7 +87,10 @@ int main(int argc, char **argv) return 0; } - QVector<Plugin> plugins = loadPlugins(config->value<QString>("plugins.path").value()); + QVector<Plugin> plugins; + for(const QString &path : Util::files(config->value<QString>("plugins.path").value())) { + plugins.append(loadPluginFromPath(path)); + } QMap<QString, std::function<int()>> pluginCommands; for(const auto &plugin : plugins) { auto *pluginInterface = qobject_cast<PluginInterface *>(plugin.instance); diff --git a/src/plugin.h b/src/plugin.h index 4de3407..e43215b 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -39,55 +39,4 @@ inline Plugin loadPluginFromPath(const QString &path) return p; } -inline QVector<Plugin> loadPlugins(const QString &path) -{ - QVector<Plugin> list; - - // quit if there's nothing to load - if(path.isEmpty()) - return list; - - // plugins can be a semicolon-separated list - if(path.contains(';')) { - auto pluginList = path.split(';'); - for(const auto &pluginPath : pluginList) { - auto plugin = loadPluginFromPath(pluginPath); - if(plugin.instance) - list.append(plugin); - } - - return list; - } - - // check if path is path to a file or a folder - QFileInfo location(path); - - if(!location.exists()) { - qDebug("Plugin path doesn't exist."); - return list; - } - - if(location.isFile()) { - // only load this one plugin - auto p = loadPluginFromPath(location.absoluteFilePath()); - if(p.instance) - list.append(p); - - } else if(location.isDir()) { - // load all profiles from this directory - const auto entries = QDir(location.absoluteFilePath()).entryInfoList(QDir::Files | QDir::Readable); - for(const auto &f : entries) { - auto p = loadPluginFromPath(f.absoluteFilePath()); - if(p.instance) - list.append(p); - } -#ifdef QT_DEBUG - } else { - qDebug("Path is neither file nor folder: %s", qUtf8Printable(path)); -#endif - } - - return list; -} - #endif // SMOLBOTE_PLUGIN_H diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..5b1478b --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,45 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "util.h" +#include <QDir> +#include <QFileInfo> + +#define ListSeparator QLatin1Literal(";") + +QStringList Util::files(const QString &location, const QStringList &nameFilters) +{ + if(location.isEmpty()) + return QStringList(); + + QStringList filelist; + + // check if location is a list of locations (contains a ';') + if(location.contains(ListSeparator)) { + const QStringList locations = location.split(ListSeparator); + + for(const QString &l : locations) { + filelist.append(Util::files(l, nameFilters)); + } + + return filelist; + } + + const QFileInfo info(location); + + // check if location is a folder + if(info.isDir()) { + for(const QFileInfo &entryInfo : QDir(info.absoluteFilePath()).entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Time)) { + filelist.append(entryInfo.absoluteFilePath()); + } + } else if(info.isFile()) { + filelist.append(info.absoluteFilePath()); + } + + return filelist; +}
\ No newline at end of file diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..4ea6639 --- /dev/null +++ b/src/util.h @@ -0,0 +1,20 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_UTIL_H +#define SMOLBOTE_UTIL_H + +#include <QStringList> + +namespace Util { + +QStringList files(const QString &location, const QStringList &nameFilters = QStringList()); + +} + +#endif // SMOLBOTE_UTIL_H |