File: language-plugin.cpp

package info (click to toggle)
lomiri-system-settings 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,484 kB
  • sloc: cpp: 15,892; python: 5,994; xml: 362; javascript: 80; makefile: 46; sh: 5
file content (470 lines) | stat: -rw-r--r-- 15,306 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * This file is part of system-settings
 *
 * Copyright (C) 2013-2016 Canonical Ltd.
 *
 * Contact: William Hua <william.hua@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <QFontDatabase>
#include <QProcess>
#include <QStandardPaths>
#include <QtDebug>
#include "language-plugin.h"
#include "keyboard-layout.h"

#include <algorithm>
#include <unicode/locid.h>
#include <unicode/unistr.h>

extern "C"
{
    #include <act/act.h>
}

struct LanguageLocale
{
public:

    // Should be true if locale is the default for its language.
    // e.g. 'en_US' is the likely locale for 'en', 'en_CA' is not.
    bool likely;

    QString localeName;
    QString displayName;
    icu::Locale locale;

public:

    explicit LanguageLocale(const QString &name);

    bool operator<(const LanguageLocale &l) const;
};

LanguageLocale::LanguageLocale(const QString &name) :
    likely(false),
    localeName(name),
    locale(qPrintable(name))
{
    std::string string;
    icu::UnicodeString unicodeString;
    locale.getDisplayName(locale, unicodeString);
    unicodeString.toUTF8String(string);
    displayName = string.c_str();
    /* workaround iso-codes casing being inconsistant */
    if (displayName.length() > 0)
        displayName[0] = displayName[0].toUpper();
}

bool LanguageLocale::operator<(const LanguageLocale &l) const
{
    // Likely locales should precede unlikely ones of the same language.
    if (strcasecmp(locale.getLanguage(), l.locale.getLanguage()) == 0) {
        if (likely || l.likely)
            return likely && !l.likely;
    }

    return QString::localeAwareCompare(displayName, l.displayName) < 0;
}

void managerLoaded(GObject    *object,
                   GParamSpec *pspec,
                   gpointer    user_data);

LanguagePlugin::LanguagePlugin(QObject *parent) :
    QObject(parent),
    m_currentLanguage(-1),
    m_nextCurrentLanguage(-1),
    m_manager(act_user_manager_get_default()),
    m_user(nullptr)
{
    if (m_manager != nullptr) {
        g_object_ref(m_manager);

        gboolean loaded;
        g_object_get(m_manager, "is-loaded", &loaded, nullptr);

        if (loaded)
            managerLoaded();
        else
            g_signal_connect(m_manager, "notify::is-loaded",
                             G_CALLBACK(::managerLoaded), this);
    }

    updateLanguageNamesAndCodes();
    updateCurrentLanguage();
    updateSpellCheckingModel();
}

LanguagePlugin::~LanguagePlugin()
{
    if (m_user != nullptr) {
        g_signal_handlers_disconnect_by_data(m_user, this);
        g_object_unref(m_user);
    }

    if (m_manager != nullptr) {
        g_signal_handlers_disconnect_by_data(m_manager, this);
        g_object_unref(m_manager);
    }
}

const QStringList &
LanguagePlugin::languageNames() const
{
    return m_languageNames;
}

const QStringList &
LanguagePlugin::languageCodes() const
{
    return m_languageCodes;
}

int
LanguagePlugin::currentLanguage() const
{
    return m_currentLanguage;
}

void
LanguagePlugin::setCurrentLanguage(int index)
{
    if (index >= 0 && index < m_languageCodes.length()) {
        m_nextCurrentLanguage = index;

        updateCurrentLanguage();
    }
}

SubsetModel *
LanguagePlugin::spellCheckingModel()
{
    return &m_spellCheckingModel;
}

void
LanguagePlugin::spellCheckingModelChanged()
{
    // TODO: update spell checking model
}

static QFontDatabase::WritingSystem localeScript(const QLocale &locale) {
    /* Unfortunately Qt has two different enums for language scripts:
     * QLocale::Script and QFontDatabase::WritingSystem, and their values do
     * not match.
     * This function converts the QLocale one into the QFontDatabase one.
     */
    switch (locale.script()) {
    case QLocale::LatinScript: return QFontDatabase::Latin;
    case QLocale::GreekScript: return QFontDatabase::Greek;
    case QLocale::CyrillicScript: return QFontDatabase::Cyrillic;
    case QLocale::ArmenianScript: return QFontDatabase::Armenian;
    case QLocale::HebrewScript: return QFontDatabase::Hebrew;
    case QLocale::ArabicScript: return QFontDatabase::Arabic;
    case QLocale::SyriacScript: return QFontDatabase::Syriac;
    case QLocale::ThaanaScript: return QFontDatabase::Thaana;
    case QLocale::DevanagariScript: return QFontDatabase::Devanagari;
    case QLocale::BengaliScript: return QFontDatabase::Bengali;
    case QLocale::GurmukhiScript: return QFontDatabase::Gurmukhi;
    case QLocale::GujaratiScript: return QFontDatabase::Gujarati;
    case QLocale::OriyaScript: return QFontDatabase::Oriya;
    case QLocale::TamilScript: return QFontDatabase::Tamil;
    case QLocale::TeluguScript: return QFontDatabase::Telugu;
    case QLocale::KannadaScript: return QFontDatabase::Kannada;
    case QLocale::MalayalamScript: return QFontDatabase::Malayalam;
    case QLocale::SinhalaScript: return QFontDatabase::Sinhala;
    case QLocale::ThaiScript: return QFontDatabase::Thai;
    case QLocale::LaoScript: return QFontDatabase::Lao;
    case QLocale::TibetanScript: return QFontDatabase::Tibetan;
    case QLocale::MyanmarScript: return QFontDatabase::Myanmar;
    case QLocale::GeorgianScript: return QFontDatabase::Georgian;
    case QLocale::KhmerScript: return QFontDatabase::Khmer;
    case QLocale::SimplifiedChineseScript: return QFontDatabase::SimplifiedChinese;
    case QLocale::TraditionalChineseScript: return QFontDatabase::TraditionalChinese;
    case QLocale::JapaneseScript: return QFontDatabase::Japanese;
    case QLocale::KoreanScript: return QFontDatabase::Korean;
    case QLocale::OghamScript: return QFontDatabase::Ogham;
    case QLocale::RunicScript: return QFontDatabase::Runic;
    case QLocale::NkoScript: return QFontDatabase::Nko;
    default:
        qWarning() << "Unsupported QLocale script" <<
            QLocale::scriptToString(locale.script());
        return QFontDatabase::Any;
    }
}

static QSet<QString> parseLocaleOutput(const QByteArray &localeOutput)
{
    const QStringList lines = QString::fromUtf8(localeOutput).split('\n');
    QSet<QString> localeNames;
    for (QString line: lines) {
        if (line.isEmpty()) continue;
        /* "locale -a" returns lines like "bg_BG.utf8", but AccountsService
         * wants "bg_BG.UTF-8" */
        localeNames.insert(line.replace("utf8", "UTF-8"));
    }
    return localeNames;
}

void
LanguagePlugin::updateLanguageNamesAndCodes()
{
    m_languageNames.clear();
    m_languageCodes.clear();
    m_indicesByLocale.clear();

    /* Run "locale -a" to get the list of supported locales; we run it in the
     * background, while we compute a list of supported languages (having
     * translations, and for which we have an available font). Then we'll
     * intersect the results.
     */
    QProcess localeProcess;
    localeProcess.start("locale", { "-a" });

    const QByteArray localeRoot = qgetenv("SNAP") + "/usr/share/locale";
    QDir localeDir(localeRoot);

    const QStringList locales = localeDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable);

    /* Get the list of writing systems supported by our fonts. Unfortunately we
     * have to exclude all unsupported locales, because text written in their
     * script would be unprintable. */
    const QFontDatabase fontDb;
    const QList<QFontDatabase::WritingSystem> supportedWritingSystems =
        fontDb.writingSystems();

    QSet<QString> localeNames {
        QStringLiteral("en_US.UTF-8"), // That's the native language of our UI texts
        QStringLiteral("en_CA.UTF-8"), // This doesn't have any translation, but we still want it
    };
    Q_FOREACH(const QString &localeName, locales) {
        QFileInfo systemSettingsTranslations(
            localeDir,
            localeName + "/LC_MESSAGES/" GETTEXT_PACKAGE ".mo");
        if (!systemSettingsTranslations.exists()) {
            /* If the locale doesn't even have a translation for the System
             * Settings, it's not worth considering.
             */
            continue;
        }
        if (systemSettingsTranslations.size() < 10000) {
            /* The check on the size is to exclude those languages whose
             * translation is mostly incomplete: while it's true that the size
             * depends on the encoding and other factors, most of the
             * translation files are at least 20KB in size (with the most
             * complete exceeding 40 or 50 KB); so excluding those languages
             * for which the translation file weighs less than 10K seems
             * appropriate.
             */
            qDebug() << "Skipping locale" << localeName << "as not fully translated";
            continue;
        }

        QLocale tmpLoc(localeName == "pt" ? "pt_PT" : localeName); // "pt" work around for https://bugreports.qt.io/browse/QTBUG-47891
        if (!supportedWritingSystems.contains(localeScript(tmpLoc))) {
            qDebug() << "Skipping locale" << localeName << "as we lack a font for it";
            continue;
        }
        localeNames.insert(tmpLoc.name() + QStringLiteral(".UTF-8"));
    }

    QList<LanguageLocale> languageLocales;

    /* Collect the result from "locale -a" and intersect */
    if (!localeProcess.waitForFinished(10000)) {
        qWarning() << "Running `locale -a` took more than 10 seconds!";
        return;
    }
    const QSet<QString> availableLocales =
        parseLocaleOutput(localeProcess.readAllStandardOutput());
    localeNames = localeNames.intersect(availableLocales);

    Q_FOREACH(const QString &loc, localeNames) {
        LanguageLocale languageLocale(loc);

        // Filter out locales for which we have no display name.
        if (languageLocale.displayName.isEmpty())
            continue;

        // Ignore "C"
        // https://github.com/ubports/ubports-touch/issues/182
        if (languageLocale.displayName == "C")
            continue;

        QLocale tmpLoc(languageLocale.locale.getLanguage());
        languageLocale.likely = tmpLoc.name() == loc.left(loc.indexOf('.')) || // likely if: en_US -> en -> en_US, NOT likely if: en_GB -> en -> en_US
                (loc.startsWith("pt_PT") && !loc.startsWith("pt_BR")); // "pt" work around for https://bugreports.qt.io/browse/QTBUG-47891

        languageLocales += languageLocale;
    }
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
    qSort(languageLocales);
#else
    std::sort(languageLocales.begin(), languageLocales.end());
#endif

    for (int i(0); i < languageLocales.length(); i++) {
        const LanguageLocale &languageLocale(languageLocales[i]);

        m_languageNames += languageLocale.displayName;
        m_languageCodes += languageLocale.localeName;

        QString localeName(languageLocale.localeName);
        localeName = localeName.left(localeName.indexOf('.'));
        m_indicesByLocale.insert(localeName, i);

        if (languageLocale.likely) {
            localeName = localeName.left(localeName.indexOf('_'));
            m_indicesByLocale.insert(localeName, i);
        }
    }
}

void
LanguagePlugin::updateCurrentLanguage()
{
    int previousLanguage(m_currentLanguage);

    if (m_user != nullptr && act_user_is_loaded(m_user)) {
        if (m_nextCurrentLanguage >= 0) {
            m_currentLanguage = m_nextCurrentLanguage;
            m_nextCurrentLanguage = -1;

            QString formatsLocale(m_languageCodes[m_currentLanguage]);
            QString language(formatsLocale.left(formatsLocale.indexOf('.')));
            act_user_set_language(m_user, qPrintable(language));

            #ifdef ENABLE_UBUNTU_ACCOUNTSSERVICE
                act_user_set_formats_locale(m_user, qPrintable(formatsLocale));
            #else
                m_accountsService.setUserProperty ("com.lomiri.shell.AccountsService", "FormatsLocale", formatsLocale);
            #endif
        }
        else {
            QString formatsLocale = "";

            #ifdef ENABLE_UBUNTU_ACCOUNTSSERVICE
                formatsLocale = act_user_get_formats_locale (m_user);
            #else
                QVariant answer = m_accountsService.getUserProperty ("com.lomiri.shell.AccountsService", "FormatsLocale");
                formatsLocale = answer.toString ();
            #endif

            m_currentLanguage = indexForLocale(formatsLocale);

            if (m_currentLanguage < 0) {
                QString language(act_user_get_language(m_user));
                m_currentLanguage = indexForLocale(language);
            }
        }
    }

    if (m_currentLanguage < 0)
        m_currentLanguage = indexForLocale(QLocale::system().name());

    if (m_currentLanguage != previousLanguage)
        Q_EMIT currentLanguageChanged();
}


void
LanguagePlugin::updateSpellCheckingModel()
{
    // TODO: populate spell checking model
    QVariantList superset;

    for (QStringList::const_iterator
         i(m_languageNames.begin()); i != m_languageNames.end(); ++i) {
        QVariantList element;
        element += *i;
        superset += QVariant(element);
    }

    m_spellCheckingModel.setCustomRoles(QStringList("language"));
    m_spellCheckingModel.setSuperset(superset);
    m_spellCheckingModel.setSubset(QList<int>());
    m_spellCheckingModel.setAllowEmpty(false);

    connect(&m_spellCheckingModel,
            SIGNAL(subsetChanged()), SLOT(spellCheckingModelChanged()));
}

int
LanguagePlugin::indexForLocale(const QString &name) const
{
    return m_indicesByLocale.value(name.left(name.indexOf('.')), -1);
}

void
LanguagePlugin::userLoaded()
{
    if (act_user_is_loaded(m_user)) {
        g_signal_handlers_disconnect_by_data(m_user, this);

        updateCurrentLanguage();
    }
}

void
userLoaded(GObject    *object,
           GParamSpec *pspec,
           gpointer    user_data)
{
    Q_UNUSED(object);
    Q_UNUSED(pspec);

    LanguagePlugin *plugin(static_cast<LanguagePlugin *>(user_data));
    plugin->userLoaded();
}

void
LanguagePlugin::managerLoaded()
{
    gboolean loaded;
    g_object_get(m_manager, "is-loaded", &loaded, nullptr);

    if (loaded) {
        g_signal_handlers_disconnect_by_data(m_manager, this);

        m_user = act_user_manager_get_user_by_id(m_manager, geteuid());

        if (m_user != nullptr) {
            g_object_ref(m_user);

            if (act_user_is_loaded(m_user))
                userLoaded();
            else
                g_signal_connect(m_user, "notify::is-loaded",
                                 G_CALLBACK(::userLoaded), this);
        }
    }
}

void
managerLoaded(GObject    *object,
              GParamSpec *pspec,
              gpointer    user_data)
{
    Q_UNUSED(object);
    Q_UNUSED(pspec);

    LanguagePlugin *plugin(static_cast<LanguagePlugin *>(user_data));
    plugin->managerLoaded();
}

void LanguagePlugin::reboot()
{
    m_sessionService.reboot();
}