From 5dfb7c41d0619bbe0830e04a2f7a8685c1e00a29 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 25 Dec 2017 15:26:01 +0100 Subject: --profile no longer causes a crash --- src/browser.cpp | 82 ++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 45 deletions(-) (limited to 'src/browser.cpp') diff --git a/src/browser.cpp b/src/browser.cpp index 6899c00..6305c0e 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -39,42 +39,9 @@ void Browser::setConfiguration(std::shared_ptr &config) // 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())]; + m_defaultProfile = profile(QString::fromStdString(m_config->value("browser.profile").value())); -#ifdef QT_DEBUG - qDebug("<< Profiles end..."); -#endif } MainWindow *Browser::createWindow() @@ -133,18 +100,43 @@ MainWindow *Browser::createSession(const QString &profileName, bool newWindow, c return window; } -std::shared_ptr Browser::profile(const QString name) +std::shared_ptr Browser::profile(const QString storageName) { - return m_profiles[name]; + if(m_profiles.contains(storageName)) { + return m_profiles[storageName]; + } + + // profile with name storageName has not been loaded + Q_ASSERT(m_config); + + if(storageName.isEmpty()) { + // construct 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); + + return otr; + } else { + // regular profile + const QString &path = QString::fromStdString(m_config->value("profile.path").value()); + + // Build a profile list from the folders in the profile.path + QDir profileDir(path); + + // set profile parents to nullptr, otherwise both Browser and std::shared_ptr try to free them + std::shared_ptr profile = std::make_shared(storageName, profileDir.absoluteFilePath(storageName), nullptr); + profile->setRequestInterceptor(m_urlRequestInterceptor.get()); + connect(profile.get(), &WebEngineProfile::downloadRequested, m_downloadManager.get(), &DownloadsWidget::addDownload); + m_profiles.insert(storageName, profile); + + return profile; + } + } -/* -QStringList Browser::profiles() + +QStringList Browser::profiles() const { - QStringList l; - const QStringList keys = m_profiles.keys(); - for(const QString &key : keys) { - l.append(key); - } - return l; + return m_profiles.keys(); } -*/ + -- cgit v1.2.1