Commit 26cc3aef authored by Tomi Leppänen's avatar Tomi Leppänen

Merge branch 'jb47651_locale_changes' into 'master'

Store locale.conf per user and set system locale.

See merge request mer-core/nemo-qml-plugin-systemsettings!132
parents cc0a4308 660dd17c
...@@ -12,6 +12,7 @@ Requires: connman ...@@ -12,6 +12,7 @@ Requires: connman
Requires: mce >= 1.83.0 Requires: mce >= 1.83.0
Requires: libsailfishkeyprovider >= 0.0.14 Requires: libsailfishkeyprovider >= 0.0.14
Requires: connman-qt5 >= 1.2.21 Requires: connman-qt5 >= 1.2.21
Requires(post): coreutils
BuildRequires: pkgconfig(Qt5Qml) BuildRequires: pkgconfig(Qt5Qml)
BuildRequires: pkgconfig(Qt5SystemInfo) BuildRequires: pkgconfig(Qt5SystemInfo)
BuildRequires: pkgconfig(Qt5Test) BuildRequires: pkgconfig(Qt5Test)
...@@ -30,6 +31,7 @@ BuildRequires: pkgconfig(connman-qt5) >= 1.2.23 ...@@ -30,6 +31,7 @@ BuildRequires: pkgconfig(connman-qt5) >= 1.2.23
BuildRequires: pkgconfig(ssu-sysinfo) >= 1.1.0 BuildRequires: pkgconfig(ssu-sysinfo) >= 1.1.0
BuildRequires: pkgconfig(packagekitqt5) BuildRequires: pkgconfig(packagekitqt5)
BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(sailfishaccesscontrol)
%description %description
%{summary}. %{summary}.
...@@ -60,7 +62,23 @@ make %{?_smp_mflags} ...@@ -60,7 +62,23 @@ make %{?_smp_mflags}
rm -rf %{buildroot} rm -rf %{buildroot}
%qmake5_install %qmake5_install
%post -p /sbin/ldconfig %post
/sbin/ldconfig
# Migrate old installations to system/user locale, see JB#47651
if [ -e /var/lib/environment/nemo/locale.conf ]
then
# Copy system locale to user location
if [ ! -e /home/.system/var/lib/environment/100000/locale.conf ]
then
mkdir -p /home/.system/var/lib/environment/100000 || :
# Fix an issue with dir perms, from connman
chmod +rx /home/.system /home/.system/var /home/.system/var/lib || :
cp /var/lib/environment/nemo/locale.conf /home/.system/var/lib/environment/100000/ || :
fi
# Migrate to new system locale location
mv /var/lib/environment/nemo/locale.conf /etc/locale.conf || :
fi
%postun -p /sbin/ldconfig %postun -p /sbin/ldconfig
......
/* /*
* Copyright (C) 2019 Jolla Ltd. * Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2020 Open Mobile Platform LLC.
* Contact: Pekka Vuorela <pekka.vuorela@jolla.com> * Contact: Pekka Vuorela <pekka.vuorela@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
...@@ -30,7 +31,9 @@ ...@@ -30,7 +31,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/ */
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QDebug> #include <QDebug>
...@@ -38,9 +41,68 @@ ...@@ -38,9 +41,68 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <sailfishaccesscontrol.h>
#include "../src/localeconfig.h" #include "../src/localeconfig.h"
static bool ensureDirectory(QString filePath)
{
auto directory = QFileInfo(filePath).dir();
if (directory.exists())
return true;
// Ensure parent with correct rights (recursion)
if (!ensureDirectory(directory.path()))
return false;
// Create this directory
if (!directory.mkpath(QStringLiteral(".")))
return false;
// Set correct access perms, root:root 755
auto pathArray = directory.path().toUtf8();
const char *path = pathArray.data();
if (chmod(path, S_IWUSR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
qWarning() << "Failed to set directory permissions" << directory.path() << ":" << strerror(errno);
if (chown(path, 0, 0) == -1)
qWarning() << "Failed to set directory as root:root" << directory.path() << ":" << strerror(errno);
return true;
}
static bool writeLocale(QString &configPath, QString &locale)
{
QFile localeConfig(configPath);
if (!ensureDirectory(configPath)) {
qWarning() << "Unable to create directory for locale configuration file";
return false;
}
if (!localeConfig.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
qWarning() << "Unable to open locale configuration file for writing:" << configPath
<< "-" << localeConfig.errorString();
return false;
}
localeConfig.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner |
QFileDevice::ReadGroup | QFileDevice::ReadOther);
if (fchown(localeConfig.handle(), 0, 0) == -1) {
qWarning() << "Failed to set localeconfig as root:root:" << configPath << ":" << strerror(errno);
}
if (!configPath.startsWith("/etc/"))
localeConfig.write("# Autogenerated by settings\n");
localeConfig.write(QString("LANG=%1\n").arg(locale).toLatin1());
localeConfig.close();
return true;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc != 2) { if (argc != 2) {
...@@ -48,6 +110,11 @@ int main(int argc, char *argv[]) ...@@ -48,6 +110,11 @@ int main(int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (!sailfish_access_control_hasgroup(getuid(), "users")) {
qWarning() << "User with id" << getuid() << "is not member of users group";
return EXIT_FAILURE;
}
QString configPath = localeConfigPath(); QString configPath = localeConfigPath();
if (configPath.isEmpty()) { if (configPath.isEmpty()) {
...@@ -61,23 +128,19 @@ int main(int argc, char *argv[]) ...@@ -61,23 +128,19 @@ int main(int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
QFile localeConfig(configPath); if (!writeLocale(configPath, newLocale))
if (!localeConfig.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
qWarning() << "Unable to open locale configuration file for writing:" << configPath
<< "-" << localeConfig.errorString();
return EXIT_FAILURE; return EXIT_FAILURE;
}
localeConfig.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | // Set system locale as well if the user is device owner
QFileDevice::ReadGroup | QFileDevice::ReadOther); if (sailfish_access_control_hasgroup(getuid(), "sailfish-system")) {
QString configPath = systemLocaleConfigPath();
if (fchown(localeConfig.handle(), 0, 0)) { if (configPath.isEmpty()) {
qWarning() << "Failed to set localeconfig as root:root" << strerror(errno); qWarning() << "No path for system locale";
} else if (!writeLocale(configPath, newLocale)) {
qWarning() << "Could not set system locale";
} // else success
} }
localeConfig.write("# Autogenerated by settings\n");
localeConfig.write(QString("LANG=%1\n").arg(newLocale).toLatin1());
localeConfig.close();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -5,6 +5,9 @@ target.path = $$TARGETPATH ...@@ -5,6 +5,9 @@ target.path = $$TARGETPATH
QT = core QT = core
CONFIG += link_pkgconfig
PKGCONFIG += sailfishaccesscontrol
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
../src/localeconfig.cpp ../src/localeconfig.cpp
......
/* /*
* Copyright (C) 2019 Jolla Ltd. * Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2020 Open Mobile Platform LLC.
* Contact: Pekka Vuorela <pekka.vuorela@jolla.com> * Contact: Pekka Vuorela <pekka.vuorela@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
...@@ -36,17 +37,15 @@ ...@@ -36,17 +37,15 @@
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h>
QString localeConfigPath() QString localeConfigPath()
{ {
struct passwd *passwdInfo = getpwuid(getuid()); // User-wide locale config
return QString("/home/.system/var/lib/environment/%1/locale.conf").arg(getuid());
}
if (passwdInfo) { QString systemLocaleConfigPath()
QString userName = passwdInfo->pw_name; {
return QString("/var/lib/environment/%1/locale.conf").arg(userName); // System-wide locale config
} else { return QString("/etc/locale.conf");
qWarning() << "Unable to get user info";
return QString();
}
} }
/* /*
* Copyright (C) 2019 Jolla Ltd. * Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2020 Open Mobile Platform LLC.
* Contact: Pekka Vuorela <pekka.vuorela@jolla.com> * Contact: Pekka Vuorela <pekka.vuorela@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
...@@ -36,5 +37,6 @@ ...@@ -36,5 +37,6 @@
#include <QString> #include <QString>
QString localeConfigPath(); QString localeConfigPath();
QString systemLocaleConfigPath();
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment