<commit_before>/******************************************************************************
 * This file is part of the Mula project
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "stardict.h"

#include "settingsdialog.h"
#include "file.h"

#include <core/dictionaryplugin.h>

#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QSettings>
#include <QtCore/QStack>
#include <QtCore/QDebug>

using namespace MulaPluginStarDict;

const int MaxFuzzy = 24;

class StarDict::Private
{
    public:
        Private()
            : sdLibs(new StarDictDictionaryManager)
            , reformatLists(false)
            , expandAbbreviations(false)
        {
        }

        ~Private()
        {
        }

        StarDictDictionaryManager *sdLibs;
        QStringList dictionaryDirectoryList;
        QHash<QString, int> loadedDictionaries;
        bool reformatLists;
        bool expandAbbreviations;

        QString ifoBookName;
        QString ifoFileName;
};

StarDict::StarDict(QObject *parent)
    : QObject(parent)
    , d(new Private)
{
    QSettings settings("mula","mula");

    d->dictionaryDirectoryList = settings.value("StarDict/dictionaryDirectoryList", d->dictionaryDirectoryList).toStringList();
    d->reformatLists = settings.value("StarDict/reformatLists", true).toBool();
    d->expandAbbreviations = settings.value("StarDict/expandAbbreviations", true).toBool();
    if (d->dictionaryDirectoryList.isEmpty())
    {
#ifdef Q_OS_UNIX
        d->dictionaryDirectoryList.append("/usr/share/stardict/dic");
#else
        d->dictionaryDirectoryList.append(QCoreApplication::applicationDirPath() + "/dic");
#endif
        d->dictionaryDirectoryList.append(QDir::homePath() + "/.stardict/dic");
    }
}

StarDict::~StarDict()
{
    QSettings settings("mula","mula");

    settings.setValue("StarDict/dictionaryDirectoryList", d->dictionaryDirectoryList);
    settings.setValue("StarDict/reformatLists", d->reformatLists);
    settings.setValue("StarDict/expandAbbreviations", d->expandAbbreviations);

    delete d->sdLibs;
}

QString
StarDict::name() const
{
    return "stardict";
}

QString
StarDict::version() const
{
    return "0.1";
}

QString
StarDict::description() const
{
    return "The StarDict plugin";
}

QStringList
StarDict::authors() const
{
    return QStringList() << "<NAME> <<EMAIL>>";
}

MulaCore::DictionaryPlugin::Features
StarDict::features() const
{
    return MulaCore::DictionaryPlugin::Features(SearchSimilar | SettingsDialog);
}

QString
StarDict::findAvailableDictionary(const QString& absolutePath)
{
    StarDictDictionaryInfo info;
    if (info.loadFromIfoFile(absolutePath, false))
        return info.bookName();

    return QString();
}

QString
StarDict::findIfoFile(const QString& absolutePath)
{
    StarDictDictionaryInfo info;
    if (info.loadFromIfoFile(absolutePath, false) && info.bookName() == d->ifoBookName)
        d->ifoFileName = absolutePath;

    return QString();
}

template <typename Method>
QStringList
StarDict::recursiveTemplateFind(const QString& directoryPath, Method method)
{
    QDir dir(directoryPath);
    QStringList result;

    // Going through the subfolders
    foreach (const QString& entryName, dir.entryList(QDir::Dirs & QDir::NoDotAndDotDot))
    {
        QString absolutePath = dir.absoluteFilePath(entryName);
        result.append(recursiveTemplateFind(absolutePath, method));
    }

    foreach (const QString& entryName, dir.entryList(QDir::Files & QDir::Drives & QDir::NoDotAndDotDot))
    {
        QString absolutePath = dir.absoluteFilePath(entryName);
        if (absolutePath.endsWith(QLatin1String(".ifo")))
            result.append((this->*method)(absolutePath));
    }

    return result;
}

QStringList
StarDict::availableDictionaryList()
{
    QStringList result;

    foreach (const QString& directoryPath, d->dictionaryDirectoryList)
       result.append(recursiveTemplateFind(directoryPath, &StarDict::findAvailableDictionary));

    return result;
}

QStringList
StarDict::loadedDictionaryList() const
{
    return d->loadedDictionaries.keys();
}

void
StarDict::setLoadedDictionaryList(const QStringList &loadedDictionaries)
{
    QStringList available = availableDictionaryList();
    QStringList disabled;
    foreach (const QString& dictionary, available)
    {
        if (!loadedDictionaries.contains(dictionary))
            disabled.append(dictionary);
    }

    d->sdLibs->reload(d->dictionaryDirectoryList, loadedDictionaries, disabled);

    d->loadedDictionaries.clear();
    for (int i = 0; i < d->sdLibs->dictionaryCount(); ++i)
        d->loadedDictionaries[d->sdLibs->dictionaryName(i)] = i;
}

MulaCore::DictionaryInfo
StarDict::dictionaryInfo(const QString &dictionary)
{
    StarDictDictionaryInfo nativeInfo;
    nativeInfo.setWordCount(0);
    if (!nativeInfo.loadFromIfoFile(findDictionary(dictionary, d->dictionaryDirectoryList), false))
        return MulaCore::DictionaryInfo();
<commit_msg>Some sane renaming inside the StarDict class source file ...<commit_after>/******************************************************************************
 * This file is part of the Mula project
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "stardict.h"

#include "settingsdialog.h"
#include "file.h"

#include <core/dictionaryplugin.h>

#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QSettings>
#include <QtCore/QStack>
#include <QtCore/QDebug>

using namespace MulaPluginStarDict;

const int MaxFuzzy = 24;

class StarDict::Private
{
    public:
        Private()
            : sdLibs(new StarDictDictionaryManager)
            , reformatLists(false)
            , expandAbbreviations(false)
        {
        }

        ~Private()
        {
        }

        StarDictDictionaryManager *sdLibs;
        QStringList dictionaryDirectoryList;
        QHash<QString, int> loadedDictionaries;
        bool reformatLists;
        bool expandAbbreviations;

        QString ifoBookName;
        QString ifoFileName;
};

StarDict::StarDict(QObject *parent)
    : QObject(parent)
    , d(new Private)
{
    QSettings settings("mula","mula");

    d->dictionaryDirectoryList = settings.value("StarDict/dictionaryDirectoryList", d->dictionaryDirectoryList).toStringList();
    d->reformatLists = settings.value("StarDict/reformatLists", true).toBool();
    d->expandAbbreviations = settings.value("StarDict/expandAbbreviations", true).toBool();
    if (d->dictionaryDirectoryList.isEmpty())
    {
#ifdef Q_OS_UNIX
        d->dictionaryDirectoryList.append("/usr/share/stardict/dic");
#else
        d->dictionaryDirectoryList.append(QCoreApplication::applicationDirPath() + "/dic");
#endif
        d->dictionaryDirectoryList.append(QDir::homePath() + "/.stardict/dic");
    }
}

StarDict::~StarDict()
{
    QSettings settings("mula","mula");

    settings.setValue("StarDict/dictionaryDirectoryList", d->dictionaryDirectoryList);
    settings.setValue("StarDict/reformatLists", d->reformatLists);
    settings.setValue("StarDict/expandAbbreviations", d->expandAbbreviations);

    delete d->sdLibs;
}

QString
StarDict::name() const
{
    return "stardict";
}

QString
StarDict::version() const
{
    return "0.1";
}

QString
StarDict::description() const
{
    return "The StarDict plugin";
}

QStringList
StarDict::authors() const
{
    return QStringList() << "<NAME> <<EMAIL>>";
}

MulaCore::DictionaryPlugin::Features
StarDict::features() const
{
    return MulaCore::DictionaryPlugin::Features(SearchSimilar | SettingsDialog);
}

QString
StarDict::findAvailableDictionary(const QString& absolutePath)
{
    StarDictDictionaryInfo info;
    if (info.loadFromIfoFile(absolutePath, false))
        return info.bookName();

    return QString();
}

QString
StarDict::findIfoFile(const QString& absolutePath)
{
    StarDictDictionaryInfo info;
    if (info.loadFromIfoFile(absolutePath, false) && info.bookName() == d->ifoBookName)
        d->ifoFileName = absolutePath;

    return QString();
}

template <typename Method>
QStringList
StarDict::recursiveTemplateFind(const QString& directoryPath, Method method)
{
    QDir dir(directoryPath);
    QStringList result;

    // Going through the subfolders
    foreach (const QString& entryName, dir.entryList(QDir::Dirs & QDir::NoDotAndDotDot))
    {
        QString absolutePath = dir.absoluteFilePath(entryName);
        result.append(recursiveTemplateFind(absolutePath, method));
    }

    foreach (const QString& entryName, dir.entryList(QDir::Files & QDir::Drives & QDir::NoDotAndDotDot))
    {
        QString absolutePath = dir.absoluteFilePath(entryName);
        if (absolutePath.endsWith(QLatin1String(".ifo")))
            result.append((this->*method)(absolutePath));
    }

    return result;
}

QStringList
StarDict::availableDictionaryList()
{
    QStringList result;

    foreach (const QString& directoryPath, d->dictionaryDirectoryList)
       result.append(recursiveTemplateFind(directoryPath, &StarDict::findAvailableDictionary));

    return result;
}

QStringList
StarDict::loadedDictionaryList() const
{
    return d->loadedDictionaries.keys();
}

void
StarDict::setLoadedDictionaryList(const QStringList &loadedDictionaryList)
{
    QStringList availableDictionaries = availableDictionaryList();
    QStringList disabledDictionaries;
    foreach (const QString& dictionary, availableDictionaries)
    {
        if (!loadedDictionaryList.contains(dictionary))
            disabledDictionaryies.append(dictionary);
    }

    d->sdLibs->reload(d->dictionaryDirectoryList, loadedDictionaryList, disabledDictionaries);

    d->loadedDictionaries.clear();
    for (int i = 0; i < d->sdLibs->dictionaryCount(); ++i)
        d->loadedDictionaries[d->sdLibs->dictionaryName(i)] = i;
}

MulaCore::DictionaryInfo
StarDict::dictionaryInfo(const QString &dictionary)
{
    StarDictDictionaryInfo nativeInfo;
    nativeInfo.setWordCount(0);
    if (!nativeInfo.loadFromIfoFile(findDictionary(dictionary, d->dictionaryDirectoryList), false))
        return MulaCore::DictionaryInfo();

{'fraction_non_alphanumeric': 0.12423155737704918, 'fraction_numerical': 0.004098360655737705, 'mean_word_length': 4.307204349796104, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 93, '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': '2336172', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3287, 'n_words': 783}