languagemodel.cpp 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2013 Jolla Ltd. <pekka.vuorela@jollamobile.com>
 *
 * You may use this file under the terms of the BSD license as follows:
 *
 * "Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Nemo Mobile nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 */
31

32
#include "languagemodel.h"
33
#include "localeconfig.h"
34 35 36 37 38 39

#include <QDir>
#include <QDebug>
#include <QSettings>
#include <QHash>
#include <QDBusInterface>
40
#include <QProcess>
41 42 43 44 45 46 47 48 49 50 51 52

namespace {
const char * const LanguageSupportDirectory = "/usr/share/jolla-supported-languages";

bool nameLessThan(const Language &lang1, const Language &lang2)
{
    return (lang1.name().localeAwareCompare(lang2.name()) <= 0);
}

}


53 54
Language::Language(QString name, QString localeCode, QString region, QString regionLabel)
    : m_name(name), m_localeCode(localeCode), m_region(region), m_regionLabel(regionLabel)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
}

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

QString Language::localeCode() const
{
    return m_localeCode;
}

QString Language::region() const
{
    return m_region;
}

73 74 75 76
QString Language::regionLabel() const
{
    return m_regionLabel;
}
77 78 79 80 81

LanguageModel::LanguageModel(QObject *parent)
    : QAbstractListModel(parent),
      m_currentIndex(-1)
{
82
    m_languages = supportedLanguages();
83 84 85 86 87 88 89
    readCurrentLocale();
}

LanguageModel::~LanguageModel()
{
}

90 91 92 93 94 95 96 97 98 99
QHash<int, QByteArray> LanguageModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    roles[LocaleRole] = "locale";
    roles[RegionRole] = "region";
    roles[RegionLabelRole] = "regionLabel";

    return roles;
}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

void LanguageModel::readCurrentLocale()
{
    QFile localeConfig(localeConfigPath());

    if (!localeConfig.exists() || !localeConfig.open(QIODevice::ReadOnly)) {
        return;
    }

    QString locale;
    while (!localeConfig.atEnd()) {
        QString line = localeConfig.readLine().trimmed();
        if (line.startsWith("LANG=")) {
             locale = line.mid(5);
             break;
        }
    }

    m_currentIndex = getLocaleIndex(locale);
}



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

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

    const Language &language = m_languages.at(row);
    switch (role) {
    case NameRole:
        return language.name();
    case LocaleRole:
        return language.localeCode();
    case RegionRole:
        return language.region();
144 145
    case RegionLabelRole:
        return language.regionLabel();
146 147 148 149 150 151 152 153 154 155
    default:
        return QVariant();
    }
}

int LanguageModel::currentIndex() const
{
    return m_currentIndex;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
QString LanguageModel::languageName(int index) const
{
    if (index < 0 || index >= m_languages.count()) {
        return QString();
    }
    return m_languages.at(index).name();
}

QString LanguageModel::locale(int index) const
{
    if (index < 0 || index >= m_languages.count()) {
        return QString();
    }
    return m_languages.at(index).localeCode();
}

172 173
void LanguageModel::setSystemLocale(const QString &localeCode, LocaleUpdateMode updateMode)
{
174 175 176
    int ret = QProcess::execute(QLatin1String("/usr/libexec/setlocale"), QStringList(localeCode));
    if (ret != 0) {
        qWarning() << "Setting user locale failed!";
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        return;
    }

    int oldLocale = m_currentIndex;
    m_currentIndex = getLocaleIndex(localeCode);
    if (m_currentIndex != oldLocale) {
        emit currentIndexChanged();
    }

    if (updateMode == UpdateAndReboot) {
        QDBusInterface dsmeInterface("com.nokia.dsme", "/com/nokia/dsme/request", "com.nokia.dsme.request",
                                     QDBusConnection::systemBus());
        dsmeInterface.call("req_reboot");
    }
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
QList<Language> LanguageModel::supportedLanguages()
{
    // get supported languages
    QDir languageDirectory(LanguageSupportDirectory);
    QFileInfoList fileInfoList = languageDirectory.entryInfoList(QStringList("*.conf"), QDir::Files);
    QList<Language> languages;

    foreach (const QFileInfo &fileInfo, fileInfoList) {
        QSettings settings(fileInfo.filePath(), QSettings::IniFormat);
        settings.setIniCodec("UTF-8");
        QString name = settings.value("Name").toString();
        QString localeCode = settings.value("LocaleCode").toString();
        QString region = settings.value("Region").toString();
        //% "Region: %1"
Tomi Leppänen's avatar
Tomi Leppänen committed
207
        QString regionLabel = settings.value("RegionLabel", qtTrId("systemsettings-la-region")).toString();
208
        if (name.isEmpty() || localeCode.isEmpty()) {
209 210 211 212 213 214
            continue;
        }
        Language newLanguage(name, localeCode, region, regionLabel);
        languages.append(newLanguage);
    }

215
    std::sort(languages.begin(), languages.end(), nameLessThan);
216 217 218
    return languages;
}

219 220 221 222 223 224 225 226 227 228 229 230
int LanguageModel::getLocaleIndex(const QString &locale) const
{
    int i = 0;
    foreach (Language language, m_languages) {
        if (language.localeCode() == locale) {
            return i;
        }
        i++;
    }

    return -1;
}