1/* GCompris - ActivityConfig.qml
2 *
3 * SPDX-FileCopyrightText: 2020 Johnny Jazeix <jazeix@gmail.com>
4 *
5 * Authors:
6 *   Johnny Jazeix <jazeix@gmail.com>
7 *
8 *   SPDX-License-Identifier: GPL-3.0-or-later
9 */
10import QtQuick 2.9
11import GCompris 1.0
12
13import "../../core"
14import "qrc:/gcompris/src/core/core.js" as Core
15
16Item {
17    id: activityConfiguration
18    property Item background
19    property alias localeBox: localeBox
20    property string locale: "system"
21    property string configurationLocale: "system"
22    width: if(background) background.width
23    property alias availableLangs: langs.languages
24    LanguageList {
25        id: langs
26    }
27
28    Column {
29        spacing: 10 * ApplicationInfo.ratio
30        width: activityConfiguration.width
31        GCComboBox {
32            id: localeBox
33            model: langs.languages
34            background: activityConfiguration.background
35            label: qsTr("Select your locale")
36        }
37    }
38
39    function setLocale(localeToSet) {
40        // Store the locale as-is to be displayed in menu
41        configurationLocale = localeToSet
42        activityConfiguration.locale = Core.resolveLocale(localeToSet)
43    }
44
45    property var dataToSave
46    function setDefaultValues() {
47        var localeUtf8 = dataToSave.locale;
48        if(localeUtf8 !== "system") {
49            localeUtf8 += ".UTF-8";
50        }
51
52        if(dataToSave.locale) {
53            setLocale(localeUtf8)
54        }
55        else {
56            activityConfiguration.localeBox.currentIndex = 0
57            setLocale(activityConfiguration.availableLangs[0].locale)
58        }
59
60        for(var i = 0 ; i < activityConfiguration.availableLangs.length ; i ++) {
61            if(activityConfiguration.availableLangs[i].locale === localeUtf8) {
62                activityConfiguration.localeBox.currentIndex = i;
63                break;
64            }
65        }
66    }
67
68    function saveValues() {
69        var newLocale = activityConfiguration.availableLangs[activityConfiguration.localeBox.currentIndex].locale;
70        // Remove .UTF-8
71        if(newLocale.indexOf('.') != -1) {
72            newLocale = newLocale.substring(0, newLocale.indexOf('.'))
73        }
74
75        setLocale(newLocale);
76
77        // Only change the locale to not erase the progress which is
78        // stored on dataToSave in the onSaveData on Lang.qml file
79        dataToSave["locale"] = newLocale;
80        dataToSave["activityLocale"] = activityConfiguration.locale
81    }
82}
83