/* * 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: git://neueland.iserlohn-fortress.net/smolbote.git * * SPDX-License-Identifier: GPL-3.0 */ #include "browser.h" #include #include #include "webengine/urlinterceptor.h" #include "mainwindow.h" #include #include Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { setApplicationName("smolbote"); setWindowIcon(QIcon(":/icon.svg")); connect(this, &Browser::messageAvailable, this, &Browser::createSession); } Browser::~Browser() { // TODO: make sure all windows have saved their settings // TODO: save session } void Browser::setConfiguration(std::shared_ptr &config) { m_config = config; m_bookmarksManager = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); m_downloadManager = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value())); // TODO: change path m_urlRequestInterceptor = std::make_shared(QString::fromStdString(m_config->value("browser.filterPath").value_or(""))); } void Browser::loadProfiles() { Q_ASSERT(m_config); const QString &path = QString::fromStdString(m_config->value("profile.path").value()); #ifdef QT_DEBUG qDebug(">> Looking for profiles... [%s]", qUtf8Printable(path)); #endif // Build a profile list from the folders in the profile.path QDir profileDir(path); const QStringList profileList = profileDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); // set profile parents to nullptr, otherwise both Browser and std::shared_ptr try to free them for(const QString &name : profileList) { std::shared_ptr profile = std::make_shared(name, profileDir.absoluteFilePath(name), nullptr); profile->setRequestInterceptor(m_urlRequestInterceptor.get()); connect(profile.get(), &WebEngineProfile::downloadRequested, m_downloadManager.get(), &DownloadsWidget::addDownload); m_profiles.insert(name, profile); } // Also add the Off-the-record profile std::shared_ptr otr = std::make_shared(nullptr); otr->setRequestInterceptor(m_urlRequestInterceptor.get()); connect(otr.get(), &WebEngineProfile::downloadRequested, m_downloadManager.get(), &DownloadsWidget::addDownload); m_profiles.insert("", otr); // set default profile m_defaultProfile = m_profiles[QString::fromStdString(m_config->value("browser.profile").value())]; #ifdef QT_DEBUG qDebug("<< Profiles end..."); #endif } 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); window->setBookmarksWidget(m_bookmarksManager); window->setDownloadsWidget(m_downloadManager); window->setProfile(m_defaultProfile); m_windows.append(window); // has to be window.get(), but can't be *window connect(window, &MainWindow::destroyed, this, [this, window]() { m_windows.removeOne(window); }); window->show(); return window; } MainWindow *Browser::createSession(const SessionParam ¶ms) { MainWindow *window = nullptr; if(params.newWindow || m_windows.isEmpty()) { window = createWindow(); window->setProfile(profile(params.profile)); } else { // reverse-iterate through windows to check for window with the same profile for(auto it = m_windows.rbegin(); it != m_windows.rend(); ++it) { if((*it)->profile()->storageName() == params.profile) { window = *it; break; } } // if none is found, create one if(window == nullptr) { window = createWindow(); window->setProfile(profile(params.profile)); } } Q_CHECK_PTR(window); if(params.urls.isEmpty()) { // no URLs were given window->newTab(QUrl::fromUserInput(m_config->value("profile.homepage").value().c_str())); } else { for(const QUrl &url : params.urls) { window->newTab(url); } } return window; } std::shared_ptr Browser::profile(const QString name) { return m_profiles[name]; } /* QStringList Browser::profiles() { QStringList l; const QStringList keys = m_profiles.keys(); for(const QString &key : keys) { l.append(key); } return l; } */