/* * 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 "browser.h" #include "addressbar/addressbar.h" #include "mainwindow/mainwindow.h" #include "mainwindow/subwindow.h" #include "webengine/urlinterceptor.h" #include "webengine/webprofile.h" #include #include #include #include #include #include #include #include #include inline QVector loadPlugins(const QString &location) { QDir pluginsDir(location); QVector 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(pluginsDir.absoluteFilePath(name))); #endif Plugin p; p.instance = std::shared_ptr(loader.instance()); list.append(p); } else { #ifdef QT_DEBUG qDebug("Loading plugin: %s [failed]", qUtf8Printable(name)); #endif } } } return list; } inline QHash loadProfiles(const QString &location) { QDir profilesDir(location); QHash list; if(profilesDir.exists()) { const QFileInfoList entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable); for(const auto &entry : entries) { auto *profile = new WebProfile(entry.baseName()); loadProfile(profile, entry.absoluteFilePath()); list.insert(entry.baseName(), profile); } } return list; } Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { setApplicationName("smolbote"); setWindowIcon(QIcon(":/icon.svg")); setApplicationVersion(SMOLBOTE_VERSION); } Browser::~Browser() { if(m_bookmarks) m_bookmarks->save(); qDeleteAll(m_windows); m_windows.clear(); //qDeleteAll(m_plugins); m_plugins.clear(); qDeleteAll(m_profiles); } void Browser::setConfiguration(std::shared_ptr &config) { Q_ASSERT(config); m_config = config; } void Browser::setup(const QString &defaultProfile) { Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); // load profiles { auto *otr = new WebProfile(m_config->section("profile"), this); loadProfile(otr, QString::fromStdString(m_config->value("profile.path").value()) + "/otr.ini"); m_profiles.insert(tr("Off-the-record"), otr); m_profiles.unite(loadProfiles(QString::fromStdString(m_config->value("profile.path").value()))); if(defaultProfile == "") { WebProfile::setDefaultProfile(otr); } else { WebProfile::setDefaultProfile(m_profiles.value(defaultProfile)); } } // plugins m_plugins.append(loadPlugins(QString::fromStdString(m_config->value("plugins.path").value()))); // register commands for(Plugin p : m_plugins) { auto *plugin = qobject_cast(p.instance.get()); if(plugin) { m_commands.unite(plugin->commands()); } if(p.instance->inherits("ProfileInterface")) { auto *profileEditor = qobject_cast(p.instance.get()); Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); m_commands.insert("profileEditor:edit", [this, profileEditor]() -> int { QHash p; for(const QString &key : m_profiles.keys()) { p.insert(key, m_profiles.value(key)); } return profileEditor->createWidget(p, nullptr)->exec(); }); } } // url request filter m_urlFilter = std::make_shared(QString::fromStdString(m_config->value("filter.path").value())); WebProfile::defaultProfile()->setRequestInterceptor(m_urlFilter.get()); // cookie request filter // bookmarks m_bookmarks = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); connect(m_bookmarks.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) { m_windows.last()->createTab(url); }); connect(WebProfile::defaultProfile(), &WebProfile::addBookmarkRequested, m_bookmarks.get(), &BookmarksWidget::addBookmark); // downloads m_downloads = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value())); connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); } WebProfile *Browser::profile(const QString &name) const { if(m_profiles.contains(name)) return m_profiles.value(name); else return nullptr; } int Browser::command(const QString &command) { if(m_commands.contains(command)) { return m_commands.value(command)(); } else { qWarning("No such command: %s", qUtf8Printable(command)); return -1; } } void Browser::createSession(const QString &profileName, bool newWindow, const QStringList &urls) { if(m_windows.isEmpty()) { createWindow(); } auto *mainwindow = m_windows.last(); if(newWindow) { QString firstUrl;// = WebProfile::defaultProfile()->homepage(); if(!urls.isEmpty()) firstUrl = urls.at(0); auto *w = mainwindow->createSubWindow(firstUrl); for(int i = 1; i < urls.count() - 1; i++) { w->addTab(QUrl::fromUserInput(urls.at(i))); } } else { if(urls.isEmpty()) mainwindow->createTab(WebProfile::defaultProfile()->homepage()); else { for(const QString &url : urls) { mainwindow->createTab(QUrl::fromUserInput(url)); } } } } MainWindow *Browser::createWindow() { // the window will delete itself when it closes, so we don't need to delete it MainWindow *window = new MainWindow(m_config); connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search); auto *bookmarksAction = new QAction(tr("Bookmarks"), window); bookmarksAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value("bookmarks.shortcut").value()))); connect(bookmarksAction, &QAction::triggered, window, [this, window]() { bool wasVisible = m_bookmarks->isVisible(); for(MainWindow *w : m_windows) { w->removeDockWidget(m_bookmarks.get()); } if(!wasVisible) { window->addDockWidget(Qt::RightDockWidgetArea, m_bookmarks.get()); } }); window->addAction(MainWindow::ToolsMenu, bookmarksAction); auto *downloadsAction = new QAction(tr("Downloads"), window); downloadsAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value("downloads.shortcut").value()))); connect(downloadsAction, &QAction::triggered, window, [this, window]() { bool wasVisible = m_downloads->isVisible(); for(MainWindow *w : m_windows) { w->removeDockWidget(m_downloads.get()); } if(!wasVisible) { window->addDockWidget(Qt::RightDockWidgetArea, m_downloads.get()); } }); window->addAction(MainWindow::ToolsMenu, downloadsAction); for(Plugin p : m_plugins) { auto *profileEditor = qobject_cast(p.instance.get()); if(profileEditor) { auto *profileAction = new QAction(tr("Profile"), window); connect(profileAction, &QAction::triggered, window, [this, profileEditor]() { QHash p; for(const QString &key : m_profiles.keys()) { p.insert(key, m_profiles.value(key)); } profileEditor->createWidget(p, nullptr)->show(); //profileEditor->createWidget(WebProfile::defaultProfile(), nullptr)->show(); }); window->addAction(MainWindow::ToolsMenu, profileAction); } } m_windows.append(window); connect(window, &MainWindow::destroyed, this, [this, window]() { m_windows.removeOne(window); }); return window; }