File indexing completed on 2022-09-27 12:25:41

0001 /*
0002     SPDX-FileCopyrightText: 2007 Pino Toscano <pino@kde.org>
0003     SPDX-FileCopyrightText: 2007-2008 Anne-Marie Mahfouf <annma@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 //project headers
0009 #include "langutils.h"
0010 #include "prefs.h"
0011 #include "klettres_debug.h"
0012 
0013 #include <QDir>
0014 
0015 #include <KLocalizedString>
0016 
0017 bool LangUtils::hasSpecialChars(const QString& lang)
0018 {
0019     if (lang== QLatin1String("cs")
0020         || lang== QLatin1String("da")
0021         || lang== QLatin1String("sk")
0022         || lang== QLatin1String("es")
0023         || lang== QLatin1String("de")
0024         || lang== QLatin1String("hu")
0025         || lang== QLatin1String("nds")
0026         || lang== QLatin1String("lt"))
0027         return true;
0028     return false;
0029 }
0030 
0031 bool LangUtils::isIndian(const QString& lang)
0032 {
0033     if (lang == QLatin1String("kn")
0034         || lang == QLatin1String("ml")
0035         || lang == QLatin1String("te")
0036         || lang == QLatin1String("pa"))
0037         return true;
0038     return false;
0039 }
0040 
0041 QStringList LangUtils::getLanguages()
0042 {
0043     QStringList m_languages;
0044     m_languages.clear();
0045     //the program scans in klettres/data/ to see what languages data is found
0046     const QStringList mdirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
0047                             QStringLiteral("klettres/"),
0048                             QStandardPaths::LocateDirectory);
0049     //qDebug() << mdirs;
0050 
0051     //if (mdirs.isEmpty()) return NULL;
0052     for (QStringList::const_iterator it =mdirs.constBegin(); it !=mdirs.constEnd(); ++it ) {
0053         QDir dir(*it);
0054         m_languages += dir.entryList(QDir::Dirs, QDir::Name);
0055         m_languages.removeAll(QStringLiteral("."));
0056         m_languages.removeAll(QStringLiteral(".."));
0057     }
0058     m_languages.removeAll(QStringLiteral("pics"));
0059     m_languages.removeAll(QStringLiteral("data"));
0060     m_languages.removeAll(QStringLiteral("icons"));
0061     m_languages.sort();
0062 
0063     //find duplicated entries in KDEDIR and KDEHOME
0064     QStringList temp_languages;
0065     for (int i=0;  i<m_languages.count(); i++)  {
0066         if (m_languages.count(m_languages[i])>1)  {
0067             temp_languages.append(m_languages[i]);
0068             m_languages.removeAll(m_languages[i]);
0069         }
0070         for (int i=0;  i<temp_languages.count(); i++)  {
0071             if (i%2==0)
0072             m_languages.append(temp_languages[i]);//append 1 of the 2 instances found
0073         }
0074         temp_languages.clear();
0075     }
0076     //TODO TEST in FRENCH
0077     m_languages.sort();
0078     qCDebug(KLETTRES_LOG) <<m_languages;
0079     return m_languages;
0080 }
0081 
0082 QStringList LangUtils::getLanguagesNames(const QStringList &languagesList)
0083 {
0084     QStringList languagesNames;
0085 
0086     for (const QString &language : languagesList) {
0087         if (language == QLatin1String("hi-ro")) {
0088             languagesNames.append(i18n("Romanized Hindi"));
0089         } else if (language == QLatin1String("lug_UG")) {
0090             languagesNames.append(i18n("Luganda"));
0091         } else if (language == QLatin1String("ep")) {
0092             languagesNames.append(i18n("English Phonics"));
0093         } else if (language == QLatin1String("tn")) {
0094             languagesNames.append(i18n("Tswana"));
0095         } else {
0096             QLocale locale(language);
0097             QString languageName = locale.nativeLanguageName();
0098             if (locale == QLocale::c())
0099                 languageName = i18nc("@item:inlistbox no language for that locale", "None");
0100             languagesNames.append(languageName);
0101         }
0102     }
0103     //never sort m_languageNames as it's m_languages translated
0104     return languagesNames;
0105 }
0106 
0107 void LangUtils::writeLangConfig()
0108 {
0109     //write the present languages in config so they cannot be downloaded
0110     KConfigGroup config(KSharedConfig::openConfig(), "KNewStuffStatus");
0111     QStringList m_languages = getLanguages();
0112     for (int i=0;  i<m_languages.count(); i++)  {
0113         QString tmp = m_languages[i];
0114         if (!config.readEntry(tmp, QString()).isEmpty()) {
0115             config.writeEntry(tmp, QDate::currentDate().toString());
0116         }
0117     }
0118     config.sync();
0119 }
0120 
0121