Warning, file /plasma/plasma-workspace/kcms/region_language/localegeneratorubuntu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     localegeneratorubuntu.cpp
0003     SPDX-FileCopyrightText: 2022 Han Young <hanyoung@protonmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include <PackageKit/Daemon>
0009 
0010 #include <QStandardPaths>
0011 
0012 #include <KLocalizedString>
0013 
0014 #include "kcm_regionandlang_debug.h"
0015 #include "localegeneratorubuntu.h"
0016 
0017 void LocaleGeneratorUbuntu::localesGenerate(const QStringList &list)
0018 {
0019     ubuntuInstall(list);
0020 }
0021 
0022 void LocaleGeneratorUbuntu::ubuntuInstall(const QStringList &locales)
0023 {
0024     m_packageIDs.clear();
0025     if (!m_proc) {
0026         m_proc = new QProcess(this);
0027     }
0028     QStringList args;
0029     args.reserve(locales.size());
0030     for (const auto &locale : locales) {
0031         auto localeStripped = locale;
0032         localeStripped.remove(QStringLiteral(".UTF-8"));
0033         args.append(QStringLiteral("--language=%1").arg(localeStripped));
0034     }
0035     const QString binaryPath = QStandardPaths::findExecutable(QStringLiteral("check-language-support"));
0036     if (!binaryPath.isEmpty()) {
0037         m_proc->setProgram(binaryPath);
0038         m_proc->setArguments(args);
0039         connect(m_proc, &QProcess::finished, this, &LocaleGeneratorUbuntu::ubuntuLangCheck);
0040         m_proc->start();
0041     } else {
0042         Q_EMIT userHasToGenerateManually(i18nc("@info:warning", "Can't locate executable `check-language-support`"));
0043     }
0044 }
0045 
0046 void LocaleGeneratorUbuntu::ubuntuLangCheck(int statusCode, QProcess::ExitStatus exitStatus)
0047 {
0048     Q_UNUSED(exitStatus)
0049     if (statusCode != 0) {
0050         // Something wrong with this Ubuntu, don't try further
0051         Q_EMIT userHasToGenerateManually(i18nc("the arg is the output of failed check-language-support call",
0052                                                "check-language-support failed, output: %1",
0053                                                QString::fromUtf8(m_proc->readAllStandardOutput())));
0054         return;
0055     }
0056     const QString output = QString::fromUtf8(m_proc->readAllStandardOutput().simplified());
0057     QStringList packages = output.split(QLatin1Char(' '));
0058     packages.erase(std::remove_if(packages.begin(),
0059                                   packages.end(),
0060                                   [](QString &i) {
0061                                       return i.isEmpty();
0062                                   }),
0063                    packages.end());
0064 
0065     if (!packages.isEmpty()) {
0066         auto transaction = PackageKit::Daemon::resolve(packages, PackageKit::Transaction::FilterNotInstalled | PackageKit::Transaction::FilterArch);
0067         connect(transaction,
0068                 &PackageKit::Transaction::package,
0069                 this,
0070                 [this](PackageKit::Transaction::Info info, const QString &packageID, const QString &summary) {
0071                     Q_UNUSED(info);
0072                     Q_UNUSED(summary);
0073                     m_packageIDs << packageID;
0074                 });
0075         connect(transaction, &PackageKit::Transaction::errorCode, this, [](PackageKit::Transaction::Error error, const QString &details) {
0076             qCDebug(KCM_REGIONANDLANG) << "resolve error" << error << details;
0077         });
0078         connect(transaction, &PackageKit::Transaction::finished, this, [packages, this](PackageKit::Transaction::Exit status, uint code) {
0079             qCDebug(KCM_REGIONANDLANG) << "resolve finished" << status << code << m_packageIDs;
0080             if (m_packageIDs.size() != packages.size()) {
0081                 qCWarning(KCM_REGIONANDLANG) << "Not all missing packages managed to resolve!" << packages << m_packageIDs;
0082                 const QString packagesString = packages.join(QLatin1Char(';'));
0083                 Q_EMIT userHasToGenerateManually(i18nc("%1 is a list of package names", "Not all missing packages managed to resolve! %1", packagesString));
0084                 return;
0085             }
0086             auto transaction = PackageKit::Daemon::installPackages(m_packageIDs);
0087             connect(transaction, &PackageKit::Transaction::errorCode, this, [this](PackageKit::Transaction::Error error, const QString &details) {
0088                 qCDebug(KCM_REGIONANDLANG) << "install error:" << error << details;
0089                 Q_EMIT userHasToGenerateManually(i18nc("%1 is a list of package names", "Failed to install package %1", details));
0090             });
0091             connect(transaction, &PackageKit::Transaction::finished, this, [this](PackageKit::Transaction::Exit status, uint code) {
0092                 qCDebug(KCM_REGIONANDLANG) << "install finished:" << status << code;
0093                 if (status == PackageKit::Transaction::Exit::ExitSuccess) {
0094                     Q_EMIT success();
0095                 }
0096             });
0097         });
0098     } else {
0099         Q_EMIT success();
0100     }
0101 }