File indexing completed on 2024-05-12 05:35:41
0001 /* 0002 SPDX-FileCopyrightText: 2010 Andriy Rysin <rysin@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "iso_codes.h" 0008 #include "debug.h" 0009 0010 #include <KLocalizedString> 0011 0012 #include <QFile> 0013 #include <QStandardPaths> 0014 #include <QXmlStreamReader> 0015 0016 class IsoCodesPrivate 0017 { 0018 public: 0019 IsoCodesPrivate(const QString &isoCode_) 0020 : isoCode(isoCode_) 0021 , loaded(false) 0022 { 0023 } 0024 void buildIsoEntryList(); 0025 0026 const QString isoCode; 0027 QList<IsoCodeEntry> isoEntryList; 0028 bool loaded; 0029 }; 0030 0031 IsoCodes::IsoCodes(const QString &isoCode) 0032 : d(new IsoCodesPrivate(isoCode)) 0033 { 0034 } 0035 0036 IsoCodes::~IsoCodes() 0037 { 0038 delete d; 0039 } 0040 0041 QList<IsoCodeEntry> IsoCodes::getEntryList() 0042 { 0043 if (!d->loaded) { 0044 d->buildIsoEntryList(); 0045 } 0046 return d->isoEntryList; 0047 } 0048 0049 // const char* IsoCodes::iso_639="639"; 0050 const char IsoCodes::iso_639_3[] = "639_3"; 0051 const char IsoCodes::attr_name[] = "name"; 0052 // const char* IsoCodes::attr_iso_639_2B_code="iso_639_2B_code"; 0053 // const char* IsoCodes::attr_iso_639_2T_code="iso_639_2T_code"; 0054 // const char* IsoCodes::attr_iso_639_1_code="iso_639_1_code"; 0055 const char IsoCodes::attr_iso_639_3_id[] = "id"; 0056 0057 const IsoCodeEntry *IsoCodes::getEntry(const QString &attributeName, const QString &attributeValue) 0058 { 0059 if (!d->loaded) { 0060 d->buildIsoEntryList(); 0061 } 0062 for (QList<IsoCodeEntry>::Iterator it = d->isoEntryList.begin(); it != d->isoEntryList.end(); ++it) { 0063 const IsoCodeEntry *isoCodeEntry = &(*it); 0064 if (isoCodeEntry->value(attributeName) == attributeValue) 0065 return isoCodeEntry; 0066 } 0067 return nullptr; 0068 } 0069 0070 void IsoCodesPrivate::buildIsoEntryList() 0071 { 0072 loaded = true; 0073 0074 QString filePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("xml/iso-codes/iso_%1.xml").arg(isoCode)); 0075 QFile file(filePath); 0076 if (!file.open(QFile::ReadOnly | QFile::Text)) { 0077 qCCritical(KCM_KEYBOARD) << "Can't open the xml file" << file.fileName(); 0078 return; 0079 } 0080 0081 QString qName("iso_" + isoCode + "_entry"); 0082 QXmlStreamReader reader(&file); 0083 while (!reader.atEnd()) { 0084 if (reader.readNext() == QXmlStreamReader::StartElement && reader.name() == qName) { 0085 IsoCodeEntry entry; 0086 const auto attributes = reader.attributes(); 0087 for (const auto &attr : attributes) { 0088 entry.insert(attr.qualifiedName().toString(), attr.value().toString()); 0089 } 0090 isoEntryList.append(entry); 0091 } 0092 } 0093 0094 if (reader.hasError()) { 0095 qCCritical(KCM_KEYBOARD) << "Failed to parse the xml file" << file.fileName() << reader.errorString(); 0096 return; 0097 } 0098 0099 qCDebug(KCM_KEYBOARD) << "Loaded" << isoEntryList.count() << ("iso entry definitions for iso" + isoCode) << "from" << file.fileName(); 0100 }