Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit d02d59d

Browse files
committed
Merge branch 'localehelper' into 'master'
Implement locale helper tool See merge request mer-core/nemo-qml-plugin-systemsettings!105
2 parents aade641 + ef05064 commit d02d59d

File tree

8 files changed

+200
-24
lines changed

8 files changed

+200
-24
lines changed

rpm/nemo-qml-plugin-systemsettings.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ rm -rf %{buildroot}
6969
%{_libdir}/qt5/qml/org/nemomobile/systemsettings/plugins.qmltypes
7070
%{_libdir}/qt5/qml/org/nemomobile/systemsettings/qmldir
7171
%{_libdir}/libsystemsettings.so.*
72+
%attr(4710,-,privileged) %{_libexecdir}/setlocale
7273
%dir %attr(0775, root, privileged) /etc/location
7374
%config %attr(0664, root, privileged) /etc/location/location.conf
7475

setlocale/main.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) 2019 Jolla Ltd.
3+
* Contact: Pekka Vuorela <[email protected]>
4+
*
5+
* You may use this file under the terms of the BSD license as follows:
6+
*
7+
* "Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are
9+
* met:
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Nemo Mobile nor the names of its contributors
17+
* may be used to endorse or promote products derived from this
18+
* software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
31+
*/
32+
33+
#include <QFile>
34+
#include <QRegularExpression>
35+
#include <QDebug>
36+
37+
#include <stdlib.h>
38+
#include <unistd.h>
39+
#include <errno.h>
40+
#include <string.h>
41+
42+
#include "../src/localeconfig.h"
43+
44+
int main(int argc, char *argv[])
45+
{
46+
if (argc != 2) {
47+
qWarning() << "No locale given";
48+
return EXIT_FAILURE;
49+
}
50+
51+
QString configPath = localeConfigPath();
52+
53+
if (configPath.isEmpty()) {
54+
return EXIT_FAILURE;
55+
}
56+
57+
QString newLocale = QString(argv[1]);
58+
QRegularExpression allowedInput("^[a-zA-Z0-9\\.@_]*$");
59+
if (!allowedInput.match(newLocale).hasMatch()) {
60+
qWarning() << "Invalid locale input:" << newLocale;
61+
return EXIT_FAILURE;
62+
}
63+
64+
QFile localeConfig(configPath);
65+
if (!localeConfig.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
66+
qWarning() << "Unable to open locale configuration file for writing:" << configPath
67+
<< "-" << localeConfig.errorString();
68+
return EXIT_FAILURE;
69+
}
70+
71+
localeConfig.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner |
72+
QFileDevice::ReadGroup | QFileDevice::ReadOther);
73+
74+
if (fchown(localeConfig.handle(), 0, 0)) {
75+
qWarning() << "Failed to set localeconfig as root:root" << strerror(errno);
76+
}
77+
78+
localeConfig.write("# Autogenerated by settings\n");
79+
localeConfig.write(QString("LANG=%1\n").arg(newLocale).toLatin1());
80+
localeConfig.close();
81+
82+
return EXIT_SUCCESS;
83+
}

setlocale/setlocale.pro

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
TEMPLATE = app
2+
TARGET = setlocale
3+
TARGETPATH = /usr/libexec
4+
target.path = $$TARGETPATH
5+
6+
QT = core
7+
8+
SOURCES += \
9+
main.cpp \
10+
../src/localeconfig.cpp
11+
12+
HEADERS += \
13+
../src/localeconfig.h
14+
15+
INSTALLS += target

src/languagemodel.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@
3030
*/
3131

3232
#include "languagemodel.h"
33+
#include "localeconfig.h"
3334

3435
#include <QDir>
3536
#include <QDebug>
3637
#include <QSettings>
3738
#include <QHash>
3839
#include <QDBusInterface>
39-
40-
#include <unistd.h>
41-
#include <sys/types.h>
42-
#include <pwd.h>
43-
40+
#include <QProcess>
4441

4542
namespace {
4643
const char * const LanguageSupportDirectory = "/usr/share/jolla-supported-languages";
@@ -50,15 +47,6 @@ bool nameLessThan(const Language &lang1, const Language &lang2)
5047
return (lang1.name().localeAwareCompare(lang2.name()) <= 0);
5148
}
5249

53-
QString localeConfigPath()
54-
{
55-
struct passwd *passwdInfo = getpwuid(getuid());
56-
QString userName;
57-
if (passwdInfo) {
58-
userName = passwdInfo->pw_name;
59-
}
60-
return QString("/var/lib/environment/%1/locale.conf").arg(userName);
61-
}
6250
}
6351

6452

@@ -183,17 +171,12 @@ QString LanguageModel::locale(int index) const
183171

184172
void LanguageModel::setSystemLocale(const QString &localeCode, LocaleUpdateMode updateMode)
185173
{
186-
QFile localeConfig(localeConfigPath());
187-
if (!localeConfig.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
188-
qWarning() << "Language model unable to open locale configuration file for writing:" << localeConfigPath()
189-
<< " - " << localeConfig.errorString();
174+
int ret = QProcess::execute(QLatin1String("/usr/libexec/setlocale"), QStringList(localeCode));
175+
if (ret != 0) {
176+
qWarning() << "Setting user locale failed!";
190177
return;
191178
}
192179

193-
localeConfig.write("# Autogenerated by settings\n");
194-
localeConfig.write(QString("LANG=%1\n").arg(localeCode).toLatin1());
195-
localeConfig.close();
196-
197180
int oldLocale = m_currentIndex;
198181
m_currentIndex = getLocaleIndex(localeCode);
199182
if (m_currentIndex != oldLocale) {
@@ -229,7 +212,7 @@ QList<Language> LanguageModel::supportedLanguages()
229212
languages.append(newLanguage);
230213
}
231214

232-
qSort(languages.begin(), languages.end(), nameLessThan);
215+
std::sort(languages.begin(), languages.end(), nameLessThan);
233216
return languages;
234217
}
235218

src/localeconfig.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2019 Jolla Ltd.
3+
* Contact: Pekka Vuorela <[email protected]>
4+
*
5+
* You may use this file under the terms of the BSD license as follows:
6+
*
7+
* "Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are
9+
* met:
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Nemo Mobile nor the names of its contributors
17+
* may be used to endorse or promote products derived from this
18+
* software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
31+
*/
32+
33+
#include <QDebug>
34+
35+
#include "localeconfig.h"
36+
37+
#include <unistd.h>
38+
#include <sys/types.h>
39+
#include <pwd.h>
40+
41+
QString localeConfigPath()
42+
{
43+
struct passwd *passwdInfo = getpwuid(getuid());
44+
45+
if (passwdInfo) {
46+
QString userName = passwdInfo->pw_name;
47+
return QString("/var/lib/environment/%1/locale.conf").arg(userName);
48+
} else {
49+
qWarning() << "Unable to get user info";
50+
return QString();
51+
}
52+
}

src/localeconfig.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2019 Jolla Ltd.
3+
* Contact: Pekka Vuorela <[email protected]>
4+
*
5+
* You may use this file under the terms of the BSD license as follows:
6+
*
7+
* "Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are
9+
* met:
10+
* * Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* * Neither the name of Nemo Mobile nor the names of its contributors
17+
* may be used to endorse or promote products derived from this
18+
* software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
31+
*/
32+
33+
#ifndef LOCALECONFIG_H
34+
#define LOCALECONFIG_H
35+
36+
#include <QString>
37+
38+
QString localeConfigPath();
39+
40+
#endif

src/src.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ system(qdbusxml2cpp -c ConnmanServiceProxy -p connmanserviceproxy ../dbus/net.co
1616

1717
SOURCES += \
1818
languagemodel.cpp \
19+
localeconfig.cpp \
1920
logging.cpp \
2021
datetimesettings.cpp \
2122
profilecontrol.cpp \
@@ -71,6 +72,7 @@ PUBLIC_HEADERS = \
7172
HEADERS += \
7273
$$PUBLIC_HEADERS \
7374
qdbusxml2cpp_dbus_types.h \
75+
localeconfig.h \
7476
batterystatus_p.h \
7577
logging_p.h \
7678
diskusage_p.h \

systemsettings.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ src_plugins.depends = src
66

77
OTHER_FILES += rpm/nemo-qml-plugin-systemsettings.spec
88

9-
SUBDIRS = src src_plugins tests
9+
SUBDIRS = src src_plugins setlocale tests

0 commit comments

Comments
 (0)