<commit_before>/****************************************************************************
**
** Copyright (c) 2021 Jolla Ltd.
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "searchenginemodel.h"
#include "opensearchconfigs.h"
#include "../bookmarks/datafetcher.h"

#include <QString>
#include <MGConfItem>

const auto SEARCH_ENGINE_CONFIG = QStringLiteral("/apps/sailfish-browser/settings/search_engine");

SearchEngineModel::SearchEngineModel(QObject *parent)
    : QAbstractListModel(parent)
{
    QStringList searchEngineList = OpenSearchConfigs::getSearchEngineList();
    for (const QString &name : searchEngineList) {
        SearchEngine engine(QUrl(), name, true);
        m_searchEngines.append(engine);
    }
}

SearchEngineModel::~SearchEngineModel()
{

}

int SearchEngineModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_searchEngines.count();
}

QVariant SearchEngineModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_searchEngines.count())
        return QVariant();

    const SearchEngine &searchEngine = m_searchEngines.at(index.row());
    switch (role) {
    case UrlRole:
        return searchEngine.url;
    case TitleRole:
        return searchEngine.title;
    case InstalledRole:
        return searchEngine.installed;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> SearchEngineModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[UrlRole] = "url";
    roles[TitleRole] = "title";
    roles[InstalledRole] = "installed";
    return roles;
}

void SearchEngineModel::classBegin()
{

}

void SearchEngineModel::componentComplete()
{

}

void SearchEngineModel::add(const QString& title, const QString& url)
{
    for (const SearchEngine& engine : m_searchEngines) {
        if (engine.title == title) {
            return;
        }
    }
    SearchEngine engine(url, title, false);
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_searchEngines.append(engine);
    endInsertRows();
    emit countChanged();
}

void SearchEngineModel::install(const QString& title)
{
    for (int i = 0; i < m_searchEngines.count(); i++) {
        if (m_searchEngines[i].title == title && !m_searchEngines[i].installed) {
            DataFetcher *fetcher = new DataFetcher();
            fetcher->setType(DataFetcher::Type::OpenSearch);
            connect(fetcher, &DataFetcher::statusChanged, [this, fetcher, i]() {
                if (fetcher->status() == DataFetcher::Status::Ready) {
                    m_searchEngines[i].installed = true;
                    emit dataChanged(index(i), index(i), QVector<int>(InstalledRole));

                    MGConfItem gconf(SEARCH_ENGINE_CONFIG);
                    gconf.set(m_searchEngines[i].title);

                    fetcher->deleteLater();
                } else if (fetcher->status() == DataFetcher::Status::Error) {
                    // TODO: error notification
                    fetcher->deleteLater();
                }
            });
            fetcher->fetch(m_searchEngines[i].url.toString());
            break;
        }
    }
}
<commit_msg>[browser] Store available opensearch engines. Fixes JB#53153<commit_after>/****************************************************************************
**
** Copyright (c) 2021 Jolla Ltd.
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "searchenginemodel.h"
#include "opensearchconfigs.h"
#include "../bookmarks/datafetcher.h"

#include <QString>
#include <MGConfItem>

const auto SEARCH_ENGINE_CONFIG = QStringLiteral("/apps/sailfish-browser/settings/search_engine");
const auto SEARCH_ENGINES_AVAILABLE_CONFIG = QStringLiteral("/apps/sailfish-browser/settings/search_engines_available");

SearchEngineModel::SearchEngineModel(QObject *parent)
    : QAbstractListModel(parent)
{
    QStringList searchEngineList = OpenSearchConfigs::getSearchEngineList();
    for (const QString &name : searchEngineList) {
        SearchEngine engine(QUrl(), name, true);
        m_searchEngines.append(engine);
    }

    MGConfItem gconf(SEARCH_ENGINES_AVAILABLE_CONFIG);
    QMap<QString, QVariant> engines = gconf.value().toMap();
    for (const QString &name : engines.keys()) {
        SearchEngine engine(engines.value(name).toString(), name, false);
        m_searchEngines.append(engine);
    }
}

SearchEngineModel::~SearchEngineModel()
{

}

int SearchEngineModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_searchEngines.count();
}

QVariant SearchEngineModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_searchEngines.count())
        return QVariant();

    const SearchEngine &searchEngine = m_searchEngines.at(index.row());
    switch (role) {
    case UrlRole:
        return searchEngine.url;
    case TitleRole:
        return searchEngine.title;
    case InstalledRole:
        return searchEngine.installed;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> SearchEngineModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[UrlRole] = "url";
    roles[TitleRole] = "title";
    roles[InstalledRole] = "installed";
    return roles;
}

void SearchEngineModel::classBegin()
{

}

void SearchEngineModel::componentComplete()
{

}

void SearchEngineModel::add(const QString& title, const QString& url)
{
    for (const SearchEngine& engine : m_searchEngines) {
        if (engine.title == title) {
            return;
        }
    }
    SearchEngine engine(url, title, false);
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_searchEngines.append(engine);
    endInsertRows();
    emit countChanged();

    MGConfItem gconf(SEARCH_ENGINES_AVAILABLE_CONFIG);
    QMap<QString, QVariant> engines = gconf.value().toMap();
    engines.insert(title, url);
    gconf.set(engines);
}

void SearchEngineModel::install(const QString& title)
{
    for (int i = 0; i < m_searchEngines.count(); i++) {
        if (m_searchEngines[i].title == title && !m_searchEngines[i].installed) {
            DataFetcher *fetcher = new DataFetcher();
            fetcher->setType(DataFetcher::Type::OpenSearch);
            connect(fetcher, &DataFetcher::statusChanged, [this, fetcher, i]() {
                if (fetcher->status() == DataFetcher::Status::Ready) {
                    m_searchEngines[i].installed = true;
                    emit dataChanged(index(i), index(i), QVector<int>() << InstalledRole);

                    MGConfItem gconf(SEARCH_ENGINE_CONFIG);
                    gconf.set(m_searchEngines[i].title);
                    MGConfItem available(SEARCH_ENGINES_AVAILABLE_CONFIG);
                    QMap<QString, QVariant> engines = available.value().toMap();
                    engines.remove(m_searchEngines[i].title);
                    available.set(engines);

                    fetcher->deleteLater();
                } else if (fetcher->status() == DataFetcher::Status::Error) {
                    // TODO: error notification
                    fetcher->deleteLater();
                }
            });
            fetcher->fetch(m_searchEngines[i].url.toString());
            break;
        }
    }
}

{'fraction_non_alphanumeric': 0.1596116504854369, 'fraction_numerical': 0.003236245954692557, 'mean_word_length': 3.176216216216216, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '358933', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2229, 'n_words': 460}