KContacts

translatedcountrylist.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Sune Vuorela <[email protected]>
3 
4  SPDX-License-Identifier: MIT
5 */
6 
7 #include "translatedcountrylist.h"
8 
9 #include <QDebug>
10 #include <QDirIterator>
11 #include <QFile>
12 #include <QXmlStreamReader>
13 
14 static void parseFile(QIODevice *device, const QString &lang, TranslatedCountries::TranslationCountryMap &map)
15 {
16  QXmlStreamReader reader(device);
17  const QStringList blacklist = { QLatin1String("eu"), QLatin1String("un"), QLatin1String("zz") };
18  while(!reader.atEnd()) {
19  QXmlStreamReader::TokenType type = reader.readNext();
20  switch(type) {
22  if (reader.name() == QLatin1String("territory")) {
23  const auto territory = reader.attributes().value(QLatin1String("type")).toString().toLower();
24  if (territory.size() != 2 || !territory.at(0).isLetter() || !territory.at(1).isLetter() || blacklist.contains(territory)) {
25  break;
26  }
27  const auto name = reader.readElementText();
28  if (!name.isEmpty()) {
29  map.push_back({name, territory, lang});
30  }
31  }
32  break;
33  default:
34  break;
35  }
36  }
37 }
38 
39 static void parseFilePath(const QString &filePath, TranslatedCountries::TranslationCountryMap &map)
40 {
41  QFile f(filePath);
42  bool success = f.open(QIODevice::ReadOnly);
43  if (!success) {
44  qWarning() << "Failed to open" << f.fileName() << f.errorString();
45  return;
46  }
47  return parseFile(&f, QFileInfo(filePath).baseName(), map);
48 }
49 
50 TranslatedCountries::TranslationCountryMap TranslatedCountries::parseFilesRecursive(const QString &directoryPath)
51 {
52  QDirIterator it(directoryPath, {QStringLiteral("*.xml")}, QDir::NoDotAndDotDot | QDir::Files, QDirIterator::Subdirectories);
53  TranslationCountryMap map;
54  while (it.hasNext()) {
55  it.next();
56  QString path = it.filePath();
57  parseFilePath(path, map);
58  }
59  return map;
60 }
QString name(const QVariant &location)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
Type type(const QSqlDatabase &db)
bool isEmpty() const const
NoDotAndDotDot
void push_back(QChar ch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 22:54:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.