<commit_before>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of libdui.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "duilogicalvalues.h"
#include "duilogicalvalues_p.h"
#include "duistylesheetattribute.h"
#include "duidebug.h"

#include <QDir>
#include <QFile>
#include <QStringList>
#include <QTextCodec>

DuiLogicalValues::DuiLogicalValues() :
    d_ptr(new DuiLogicalValuesPrivate)
{
}

DuiLogicalValues::~DuiLogicalValues()
{
    delete d_ptr;
}

bool DuiLogicalValuesPrivate::parse(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QString group = "General";
    data.insert("General", Values());

    while (!file.atEnd()) {
        QString line = file.readLine().trimmed();
        // skip comments
        if (line.startsWith("[")) {
            // parse group header
            int index = line.indexOf("]", 1);
            if (index == -1) {
                duiWarning("DuiLogicalValues") << "Error occurred when parsing .ini file:" << line;
                file.close();
                return false;
            }
            // this will be the currently active group
            group = line.mid(1, index - 1);
        } else {

            // key/value pair
            QString key, value;
            QString *target = &key;

            // stores the last 'good' character
            int truncation = 0;
            // go through whole line
            for (int i = 0; i < line.length(); i++) {
                QChar character = line[i];
                if (character == ';') {
                    break;
                } else if (character == '=') {
                    // remove trailing whitespaces
                    target->truncate(truncation);
                    // start to parse value
                    target = &value;
                    truncation = 0;
                } else {
                    if (target->isEmpty() && character.isSpace()) {
                        // do not add whitespaces at the beginning
                    } else {
                        (*target) += character;
                        if (!character.isSpace())
                            truncation = target->length();
                    }
                }
            }
            // remove trailing whitespaces
            target->truncate(truncation);

            // consistency check
            if (!line.startsWith(';') && line.length() > 0) {
                if (key.isEmpty() || value.isEmpty()) {
                    duiWarning("DuiLogicalValues") << "Error occurred when parsing .ini file:" << line;
                    file.close();
                    return false;
                }
                // store
                if (!data.contains(group))
                    data.insert(group, Values());

                Values &values = data[group];

                if (!values.contains(key)) {
                    values.insert(key, value);
                }
            }
        }
    }

    file.close();
    return true;
}

bool DuiLogicalValues::append(const QString &filename)
{
    Q_D(DuiLogicalValues);

    // make sure that the file exists
    if (!QFile(filename).exists())
        return false;

    // load it
    if (!d->parse(filename))
        return false;

    return true;
}

void DuiLogicalValues::load(const QStringList &themeInheritanceChain, const QString &locale)
{
    Q_D(DuiLogicalValues);

    // clear existing config
    d->data.clear();

    // go through whole inheritance hierarchy
    foreach(QString path, themeInheritanceChain) {

        // load locale-specific constant definitions
        if (!locale.isEmpty()) {
            QString localeFile = QString("dui") + QDir::separator() + QString("locale") + QDir::separator() + locale + QDir::separator() + QString("constants.ini");
            append(path + localeFile);
        }

        // determine filename & append
        append(path + QString("dui") + QDir::separator() + QString("constants.ini"));
    }
}

bool DuiLogicalValues::findKey(const QString &key, QString &group, QString &value) const
{
    Q_D(const DuiLogicalValues);

    // search from every group
    for (DuiLogicalValuesPrivate::Groups::const_iterator iterator = d->data.begin(); iterator != d->data.end(); iterator++) {
        // get values from this group
        const DuiLogicalValuesPrivate::Values &values = iterator.value();
<commit_msg>Changes: Modified DuiLogicalValues to go trough the locale-specific constants first.<commit_after>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of libdui.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "duilogicalvalues.h"
#include "duilogicalvalues_p.h"
#include "duistylesheetattribute.h"
#include "duidebug.h"

#include <QDir>
#include <QFile>
#include <QStringList>
#include <QTextCodec>

DuiLogicalValues::DuiLogicalValues() :
    d_ptr(new DuiLogicalValuesPrivate)
{
}

DuiLogicalValues::~DuiLogicalValues()
{
    delete d_ptr;
}

bool DuiLogicalValuesPrivate::parse(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QString group = "General";
    data.insert("General", Values());

    while (!file.atEnd()) {
        QString line = file.readLine().trimmed();
        // skip comments
        if (line.startsWith("[")) {
            // parse group header
            int index = line.indexOf("]", 1);
            if (index == -1) {
                duiWarning("DuiLogicalValues") << "Error occurred when parsing .ini file:" << line;
                file.close();
                return false;
            }
            // this will be the currently active group
            group = line.mid(1, index - 1);
        } else {

            // key/value pair
            QString key, value;
            QString *target = &key;

            // stores the last 'good' character
            int truncation = 0;
            // go through whole line
            for (int i = 0; i < line.length(); i++) {
                QChar character = line[i];
                if (character == ';') {
                    break;
                } else if (character == '=') {
                    // remove trailing whitespaces
                    target->truncate(truncation);
                    // start to parse value
                    target = &value;
                    truncation = 0;
                } else {
                    if (target->isEmpty() && character.isSpace()) {
                        // do not add whitespaces at the beginning
                    } else {
                        (*target) += character;
                        if (!character.isSpace())
                            truncation = target->length();
                    }
                }
            }
            // remove trailing whitespaces
            target->truncate(truncation);

            // consistency check
            if (!line.startsWith(';') && line.length() > 0) {
                if (key.isEmpty() || value.isEmpty()) {
                    duiWarning("DuiLogicalValues") << "Error occurred when parsing .ini file:" << line;
                    file.close();
                    return false;
                }
                // store
                if (!data.contains(group))
                    data.insert(group, Values());

                Values &values = data[group];

                if (!values.contains(key)) {
                    values.insert(key, value);
                }
            }
        }
    }

    file.close();
    return true;
}

bool DuiLogicalValues::append(const QString &filename)
{
    Q_D(DuiLogicalValues);

    // make sure that the file exists
    if (!QFile(filename).exists())
        return false;

    // load it
    if (!d->parse(filename))
        return false;

    return true;
}

void DuiLogicalValues::load(const QStringList &themeInheritanceChain, const QString &locale)
{
    Q_D(DuiLogicalValues);

    // clear existing config
    d->data.clear();

    // load locale-specific constant definitions
    if (!locale.isEmpty()) {

        // go through whole inheritance hierarchy
        foreach(QString path, themeInheritanceChain) {

            append(path + QString("dui") + QDir::separator() + QString("locale") + QDir::separator() + locale + QDir::separator() + QString("constants.ini"));
        }
    }

    // go through whole inheritance hierarchy
    foreach(QString path, themeInheritanceChain) {

        append(path + QString("dui") + QDir::separator() + QString("constants.ini"));
    }
}

bool DuiLogicalValues::findKey(const QString &key, QString &group, QString &value) const
{
    Q_D(const DuiLogicalValues);

    // search from every group
    for (DuiLogicalValuesPrivate::Groups::const_iterator iterator = d->data.begin(); iterator != d->data.end(); iterator++) {
        // get values from this group
        const DuiLogicalValuesPrivate::Values &values = iterator.value();

{'fraction_non_alphanumeric': 0.1461523360816647, 'fraction_numerical': 0.0027483313702394976, 'mean_word_length': 1.9827283372365339, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '6790967', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2573, 'n_words': 739}