iso-codes.cpp (kaffeine-2.0.16) | : | iso-codes.cpp (kaffeine-2.0.17) | ||
---|---|---|---|---|
skipping to change at line 23 | skipping to change at line 23 | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
*/ | */ | |||
#include "log.h" | #include "log.h" | |||
#include <KLocalizedString> | #include <KLocalizedString> | |||
#include <QFile> | #include <QFile> | |||
#include <QLocale> | #include <QLocale> | |||
#include <QRegularExpression> | ||||
#include <QStandardPaths> | #include <QStandardPaths> | |||
#include <QXmlStreamReader> | #include <QXmlStreamReader> | |||
#include "iso-codes.h" | ||||
namespace IsoCodes | namespace IsoCodes | |||
{ | { | |||
static void load(QHash<QString, QString> &hash, | static void load(QHash<QString, QString> *code_3letters, | |||
QString file, QString main_key, | QHash<QString, QString> *code_2letters, | |||
QString entry_key, QString code_key, | QString file, | |||
QString main_key, | ||||
QString entry_key, | ||||
QString code_3_key, | ||||
QString code_2_key, | ||||
QString name_key) | QString name_key) | |||
{ | { | |||
if (!hash.isEmpty()) | if (!code_3letters->isEmpty()) | |||
return; | return; | |||
const QString fileName = QStandardPaths::locate(QStandardPaths::G enericDataLocation, file); | const QString fileName = QStandardPaths::locate(QStandardPaths::G enericDataLocation, file); | |||
if (fileName.isEmpty()) { | if (fileName.isEmpty()) { | |||
qCInfo(logConfig, | qCInfo(logConfig, | |||
"Could not locate %s (is iso-codes installed?)", | "Could not locate %s (is iso-codes installed?)", | |||
qPrintable(file)); | qPrintable(file)); | |||
return; | return; | |||
} | } | |||
QFile f(fileName); | QFile f(fileName); | |||
if (!f.open(QIODevice::ReadOnly)) { | if (!f.open(QIODevice::ReadOnly)) { | |||
qCWarning(logConfig, | qCWarning(logConfig, | |||
"Could not open %s (%s)", | "Could not open %s (%s)", | |||
qPrintable(fileName), | qPrintable(fileName), | |||
qPrintable(f.errorString())); | qPrintable(f.errorString())); | |||
return; | return; | |||
} | } | |||
QXmlStreamReader r(&f); | QXmlStreamReader r(&f); | |||
bool inDoc = false; | bool inDoc = false; | |||
while (!r.atEnd()) { | while (!r.atEnd()) { | |||
const QXmlStreamReader::TokenType t = r.readNext(); | const QXmlStreamReader::TokenType t = r.readNext(); | |||
QStringRef name; | QStringRef name; | |||
switch (t) { | switch (t) { | |||
case QXmlStreamReader::StartElement: | case QXmlStreamReader::StartElement: | |||
name = r.name(); | name = r.name(); | |||
if (inDoc && name == entry_key) { | if (inDoc && name == entry_key) { | |||
const QXmlStreamAttributes attrs = r.attr ibutes(); | const QXmlStreamAttributes attrs = r.attr ibutes(); | |||
const QString code = attrs.value(code_key ).toString().toUpper(); | const QString code3 = attrs.value(code_3_ key).toString().toUpper(); | |||
const QString lang = attrs.value(name_key ).toString(); | const QString lang = attrs.value(name_key ).toString(); | |||
hash.insert(code, lang); | code_3letters->insert(code3, lang); | |||
if (code_2letters) { | ||||
const QString code2 = attrs.value | ||||
(code_2_key).toString().toUpper(); | ||||
if (code2 != "") | ||||
code_2letters->insert(cod | ||||
e2, code3); | ||||
} | ||||
} else if (name == main_key) { | } else if (name == main_key) { | |||
inDoc = true; | inDoc = true; | |||
} | } | |||
break; | break; | |||
case QXmlStreamReader::EndElement: | case QXmlStreamReader::EndElement: | |||
name = r.name(); | name = r.name(); | |||
if (inDoc && name == main_key) { | if (inDoc && name == main_key) { | |||
inDoc = false; | inDoc = false; | |||
} | } | |||
break; | break; | |||
skipping to change at line 88 | skipping to change at line 100 | |||
case QXmlStreamReader::StartDocument: | case QXmlStreamReader::StartDocument: | |||
case QXmlStreamReader::EndDocument: | case QXmlStreamReader::EndDocument: | |||
case QXmlStreamReader::Characters: | case QXmlStreamReader::Characters: | |||
case QXmlStreamReader::Comment: | case QXmlStreamReader::Comment: | |||
case QXmlStreamReader::DTD: | case QXmlStreamReader::DTD: | |||
case QXmlStreamReader::EntityReference: | case QXmlStreamReader::EntityReference: | |||
case QXmlStreamReader::ProcessingInstruction: | case QXmlStreamReader::ProcessingInstruction: | |||
break; | break; | |||
} | } | |||
} | } | |||
if (hash.isEmpty()) | if (code_3letters->isEmpty()) | |||
qCWarning(logConfig, | qCWarning(logConfig, | |||
"Error parsing %s: no entries found.", | "Error parsing %s: no entries found.", | |||
qPrintable(fileName)); | qPrintable(fileName)); | |||
} | } | |||
/* | /* | |||
* ISO 639-2 language codes | * ISO 639-2 language codes | |||
* Loaded and translated at runtime from iso-codes. | * Loaded and translated at runtime from iso-codes. | |||
*/ | */ | |||
static QHash<QString, QString> iso639_2_codes; | static QHash<QString, QString> iso639_2_codes; | |||
/* | ||||
* ISO 639-1 to ISO 639-2 language code conversion | ||||
* Loaded and translated at runtime from iso-codes. | ||||
*/ | ||||
static QHash<QString, QString> iso639_1_codes; | ||||
bool getLanguage(const QString &iso_code, QString *language) | bool getLanguage(const QString &iso_code, QString *language) | |||
{ | { | |||
static bool first = true; | static bool first = true; | |||
QString code = iso_code.toUpper(); | QString code = iso_code.toUpper(); | |||
if (code == "QAA") { | if (code == "QAA") { | |||
*language = i18n("Original Language"); | *language = i18n("Original Language"); | |||
return true; | return true; | |||
} | } | |||
if (first) { | if (first) { | |||
load(iso639_2_codes, | load(&iso639_2_codes, | |||
&iso639_1_codes, | ||||
QString("xml/iso-codes/iso_639-2.xml"), | QString("xml/iso-codes/iso_639-2.xml"), | |||
QLatin1String("iso_639_entries"), | QLatin1String("iso_639_entries"), | |||
QLatin1String("iso_639_entry"), | QLatin1String("iso_639_entry"), | |||
QLatin1String("iso_639_2B_code"), | QLatin1String("iso_639_2B_code"), | |||
QLatin1String("iso_639_1_code"), | ||||
QLatin1String("name")); | QLatin1String("name")); | |||
first = false; | first = false; | |||
} | } | |||
QHash<QString, QString>::ConstIterator it = iso639_2_codes.constF ind(code); | QHash<QString, QString>::ConstIterator it = iso639_2_codes.constF ind(code); | |||
if (it == iso639_2_codes.constEnd()) { | if (it == iso639_2_codes.constEnd()) { | |||
/* | /* | |||
* The ETSI EN 300 468 Annex F spec defines the | * The ETSI EN 300 468 Annex F spec defines the | |||
* original audio soundtrack code to be "QAA". Yet, | * original audio soundtrack code to be "QAA". Yet, | |||
* TV bundle providers could use something else. | * TV bundle providers could use something else. | |||
skipping to change at line 147 | skipping to change at line 167 | |||
} | } | |||
return false; | return false; | |||
} | } | |||
if (language) { | if (language) { | |||
*language = i18nd("iso_639-2", it.value().toUtf8().constD ata()); | *language = i18nd("iso_639-2", it.value().toUtf8().constD ata()); | |||
} | } | |||
return true; | return true; | |||
} | } | |||
const QString code2Convert(const QString &code2) | ||||
{ | ||||
static bool first = true; | ||||
QString code = code2.toUpper(); | ||||
/* Ignore any embedded Country data */ | ||||
code.remove(QRegularExpression("_.*")); | ||||
if (first) { | ||||
load(&iso639_2_codes, | ||||
&iso639_1_codes, | ||||
QString("xml/iso-codes/iso_639-2.xml"), | ||||
QLatin1String("iso_639_entries"), | ||||
QLatin1String("iso_639_entry"), | ||||
QLatin1String("iso_639_2B_code"), | ||||
QLatin1String("iso_639_1_code"), | ||||
QLatin1String("name")); | ||||
first = false; | ||||
} | ||||
QHash<QString, QString>::ConstIterator it = iso639_1_codes.constF | ||||
ind(code); | ||||
if (it == iso639_1_codes.constEnd()) { | ||||
return "QAA"; | ||||
} | ||||
return it.value().toUtf8().constData(); | ||||
} | ||||
/* | /* | |||
* ISO 3166-1 Alpha 3 Country codes | * ISO 3166-1 Alpha 3 Country codes | |||
* Loaded and translated at runtime from iso-codes. | * Loaded and translated at runtime from iso-codes. | |||
*/ | */ | |||
static QHash<QString, QString> iso3166_1_codes; | static QHash<QString, QString> iso3166_1_codes, iso3166_2_codes; | |||
bool getCountry(const QString &code, QString *country) | bool getCountry(const QString &_code, QString *country) | |||
{ | { | |||
static bool first = true; | static bool first = true; | |||
QString code; | ||||
if (first) { | if (first) { | |||
load(iso3166_1_codes, | load(&iso3166_1_codes, | |||
&iso3166_2_codes, | ||||
QString("xml/iso-codes/iso_3166-1.xml"), | QString("xml/iso-codes/iso_3166-1.xml"), | |||
QLatin1String("iso_3166_entries"), | QLatin1String("iso_3166_entries"), | |||
QLatin1String("iso_3166_entry"), | QLatin1String("iso_3166_entry"), | |||
QLatin1String("alpha_3_code"), | QLatin1String("alpha_3_code"), | |||
QString("alpha_2_code"), | ||||
QLatin1String("name")); | QLatin1String("name")); | |||
first = false; | first = false; | |||
} | } | |||
if (_code.size() == 2) { | ||||
QHash<QString, QString>::ConstIterator it = iso3166_1_cod | ||||
es.constFind(code); | ||||
if (it == iso3166_2_codes.constEnd()) | ||||
return false; | ||||
code = it.value(); | ||||
} else { | ||||
code = _code; | ||||
} | ||||
QHash<QString, QString>::ConstIterator it = iso3166_1_codes.const Find(code); | QHash<QString, QString>::ConstIterator it = iso3166_1_codes.const Find(code); | |||
if (it == iso3166_1_codes.constEnd()) { | if (it == iso3166_1_codes.constEnd()) { | |||
return false; | return false; | |||
} | } | |||
if (country) { | if (country) { | |||
*country = i18nd("iso_3166-1", it.value().toUtf8().constD ata()); | *country = i18nd("iso_3166-1", it.value().toUtf8().constD ata()); | |||
} | } | |||
return true; | return true; | |||
} | } | |||
End of changes. 20 change blocks. | ||||
18 lines changed or deleted | 82 lines changed or added |