/*
 * 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 "webengineprofile.h"
#include <QSettings>
#include <QWebEngineSettings>

WebEngineProfile::WebEngineProfile(QObject *parent) :
    QWebEngineProfile(parent)
{
    m_name = tr("Off-the-record");

#ifdef QT_DEBUG
    qDebug("Creating off-the-record profile");
#endif

    // Off-the-record profiles have no persistent path
}

WebEngineProfile::WebEngineProfile(const QString &name, const QString &path, QObject *parent) :
    QWebEngineProfile(name, parent)
{
    m_name = name;

#ifdef QT_DEBUG
    qDebug("Creating profile %s", qUtf8Printable(m_name));
#endif

    setPersistentStoragePath(path + "/storage");
    setCachePath(path + "/cache");

    // Read profile settings
    const QString profileIniPath = path + "/profile.ini";
    qDebug("Reading profile from [%s]", qUtf8Printable(profileIniPath));
    QSettings config(profileIniPath, QSettings::IniFormat);

    m_homepage = config.value("homepage", m_homepage).toUrl();
    m_newtab = config.value("newtab", m_newtab).toUrl();

    config.beginGroup("http");
    setHttpUserAgent(config.value("userAgent").toString());
    setHttpAcceptLanguage(config.value("accept-lang").toString());
    {
        QString cacheType = config.value("cacheType").toString();
        if(cacheType == "memory") {
            setHttpCacheType(MemoryHttpCache);
        } else if(cacheType == "disk") {
            setHttpCacheType(DiskHttpCache);
        } else if(cacheType == "disabled") {
            setHttpCacheType(NoCache);
        }
    }
    setHttpCacheMaximumSize(config.value("cacheSize").toInt());
    config.endGroup();  // http

    config.beginGroup("policy");
    {
        QString cookies = config.value("cookies").toString();
        if(cookies == "disabled") {
            setPersistentCookiesPolicy(NoPersistentCookies);
        } else if(cookies == "allow") {
            setPersistentCookiesPolicy(AllowPersistentCookies);
        } else if(cookies == "force") {
            setPersistentCookiesPolicy(ForcePersistentCookies);
        }
    }
    config.endGroup();  // policy

    config.beginGroup("attributes");
    settings()->setAttribute(QWebEngineSettings::AutoLoadImages, config.value("autoLoadImages", true).toBool());
    settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, config.value("javascriptEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, config.value("javascriptCanOpenWindows", true).toBool());
    settings()->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, config.value("javascriptCanAccessClipboard", false).toBool());
    settings()->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, config.value("linksIncludedInFocusChain", true).toBool());
    settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, config.value("localStorageEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, config.value("localContentCanAccessRemoteUrls", false).toBool());
    settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, config.value("xssAuditingEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, config.value("spatialNavigationEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, config.value("localContentCanAccessFileUrls", true).toBool());
    settings()->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, config.value("hyperlinkAuditingEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, config.value("scrollAnimatorEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, config.value("errorPageEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::PluginsEnabled, config.value("pluginsEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, config.value("fullscreenSupportEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, config.value("screenCaptureEnabled", false).toBool());
    settings()->setAttribute(QWebEngineSettings::WebGLEnabled, config.value("webglEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, config.value("accelerated2dCanvasEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, config.value("autoLoadIconsForPage", true).toBool());
    settings()->setAttribute(QWebEngineSettings::TouchIconsEnabled, config.value("touchIconsEnabled", false).toBool());
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
    settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, config.value("focusOnNavigationEnabled", true).toBool());
    settings()->setAttribute(QWebEngineSettings::PrintElementBackgrounds, config.value("printElementBackgrounds", true).toBool());
    settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, config.value("allowRunningInsecureContent", false).toBool());
#endif
    config.endGroup();  // attributes


}

WebEngineProfile::~WebEngineProfile()
{
    if(shouldSaveProfile) {
        saveProfile();
    }
}

QString WebEngineProfile::name() const
{
    return m_name;
}

QUrl WebEngineProfile::homepage() const
{
    return m_homepage;
}

void WebEngineProfile::setHomepage(const QUrl &url)
{
    m_homepage = url;
}

QUrl WebEngineProfile::newtab() const
{
    return m_newtab;
}

void WebEngineProfile::setNewtab(const QUrl &url)
{
    m_newtab = url;
}

void WebEngineProfile::saveProfile()
{
    QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat);

    config.setValue("homepage", homepage().toString());
    config.setValue("newtab", newtab().toString());

    config.beginGroup("http");
    config.setValue("userAgent", httpUserAgent());
    config.setValue("accept-lang", httpAcceptLanguage());
    switch (httpCacheType()) {
    case MemoryHttpCache:
        config.setValue("cacheType", "memory");
        break;
    case DiskHttpCache:
        config.setValue("cacheType", "disk");
        break;
    case NoCache:
        config.setValue("cacheType", "disabled");
        break;
    }
    config.setValue("cacheSize", httpCacheMaximumSize());
    config.endGroup();  // http

    config.beginGroup("policy");
    switch (persistentCookiesPolicy()) {
    case NoPersistentCookies:
        config.setValue("cookies", "disabled");
        break;
    case AllowPersistentCookies:
        config.setValue("cookies", "allow");
        break;
    case ForcePersistentCookies:
        config.setValue("cookies", "force");
        break;
    }
    config.endGroup();  // policy

    QWebEngineSettings *s = settings();
    config.beginGroup("attributes");
    config.setValue("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages));
    config.setValue("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled));
    config.setValue("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows));
    config.setValue("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard));
    config.setValue("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain));
    config.setValue("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled));
    config.setValue("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls));
    config.setValue("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled));
    config.setValue("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled));
    config.setValue("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls));
    config.setValue("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled));
    config.setValue("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled));
    config.setValue("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled));
    config.setValue("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled));
    config.setValue("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled));
    config.setValue("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled));
    config.setValue("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled));
    config.setValue("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled));
    config.setValue("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage));
    config.setValue("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled));
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
    config.setValue("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled));
    config.setValue("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds));
    config.setValue("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent));
#endif
    config.endGroup();  // attributes

    config.sync();
}