QtSpell  0.8.2
Spell checking for Qt text widgets
Codetable.cpp
1 /* QtSpell - Spell checking for Qt text widgets.
2  * Copyright (c) 2014 Sandro Mani
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "Codetable.hpp"
20 #include <QCoreApplication>
21 #include <QDir>
22 #include <QFile>
23 #include <QXmlStreamReader>
24 #include <libintl.h>
25 
26 #define ISO_639_DOMAIN "iso_639"
27 #define ISO_3166_DOMAIN "iso_3166"
28 
29 namespace QtSpell {
30 
32 {
33  static Codetable codetable;
34  return &codetable;
35 }
36 
37 void Codetable::lookup(const QString &language_code, QString &language_name, QString &country_name) const
38 {
39  QStringList parts = language_code.split("_");
40  if(parts.size() != 2) {
41  language_name = language_code;
42  country_name = "";
43  return;
44  }
45 
46  language_name = m_languageTable.contains(parts[0]) ? m_languageTable.value(parts[0]) : parts[0];
47  country_name = m_countryTable.contains(parts[1]) ? m_countryTable.value(parts[1]) : parts[1];
48 }
49 
50 Codetable::Codetable()
51 {
52 #ifdef Q_OS_WIN32
53  QDir dataDir = QDir(QString("%1/../share").arg(QCoreApplication::applicationDirPath()));
54 #else
55  QDir dataDir = QDir(ISO_CODES_PREFIX "/share").absolutePath();
56 #endif
57 
58  bindtextdomain(ISO_639_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
59  bind_textdomain_codeset(ISO_639_DOMAIN, "UTF-8");
60 
61  bindtextdomain(ISO_3166_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
62  bind_textdomain_codeset(ISO_3166_DOMAIN, "UTF-8");
63 
64  parse(dataDir, "iso_639.xml", parseIso639Elements, m_languageTable);
65  parse(dataDir, "iso_3166.xml", parseIso3166Elements, m_countryTable);
66 }
67 
68 void Codetable::parseIso639Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
69 {
70  if(xml.name() == "iso_639_entry" ){
71  QString name = xml.attributes().value("name").toString();
72  QString code = xml.attributes().value("iso_639_1_code").toString();
73  if(!name.isEmpty() && !code.isEmpty()){
74  name = QString::fromUtf8(dgettext(ISO_639_DOMAIN, name.toLatin1().data()));
75  table.insert(code, name);
76  }
77  }
78 }
79 
80 void Codetable::parseIso3166Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
81 {
82  if(xml.name() == "iso_3166_entry" ){
83  QString name = xml.attributes().value("name").toString();
84  QString code = xml.attributes().value("alpha_2_code").toString();
85  if(!name.isEmpty() && !code.isEmpty()){
86  name = QString::fromUtf8(dgettext(ISO_3166_DOMAIN, name.toLatin1().data()));;
87  table.insert(code, name);
88  }
89  }
90 }
91 
92 void Codetable::parse(const QDir& dataDir, const QString& basename, const parser_t& parser, QMap<QString, QString>& table)
93 {
94  QString filename = QDir(QDir(dataDir.filePath("xml")).filePath("iso-codes")).absoluteFilePath(basename);
95  QFile file(filename);
96  if(!file.open(QIODevice::ReadOnly)){
97  qWarning("Failed to open %s for reading", file.fileName().toLatin1().data());
98  return;
99  }
100 
101  QXmlStreamReader xml(&file);
102  while(!xml.atEnd() && !xml.hasError()){
103  if(xml.readNext() == QXmlStreamReader::StartElement){
104  parser(xml, table);
105  }
106  }
107 }
108 
109 } // QtSpell
static Codetable * instance()
Get codetable instance.
Definition: Codetable.cpp:31
void lookup(const QString &language_code, QString &language_name, QString &country_name) const
Looks up the language and country name for the specified language code. If no matching entries are fo...
Definition: Codetable.cpp:37
QtSpell namespace.
Definition: Checker.cpp:56
Class for translating locale identifiers into human readable strings.
Definition: Codetable.hpp:33