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  language_name = language_code;
41  country_name = "";
42  if(parts.isEmpty()) {
43  return;
44  }
45  if(parts.size() >= 1) {
46  language_name = m_languageTable.contains(parts[0]) ? m_languageTable.value(parts[0]) : parts[0];
47  }
48  if(parts.size() >= 2) {
49  country_name = m_countryTable.contains(parts[1]) ? m_countryTable.value(parts[1]) : parts[1];
50  }
51 }
52 
53 Codetable::Codetable()
54 {
55 #ifdef Q_OS_WIN32
56  QDir dataDir = QDir(QString("%1/../share").arg(QCoreApplication::applicationDirPath()));
57 #else
58  QDir dataDir = QDir(ISO_CODES_PREFIX "/share").absolutePath();
59 #endif
60 
61  bindtextdomain(ISO_639_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
62  bind_textdomain_codeset(ISO_639_DOMAIN, "UTF-8");
63 
64  bindtextdomain(ISO_3166_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
65  bind_textdomain_codeset(ISO_3166_DOMAIN, "UTF-8");
66 
67  parse(dataDir, "iso_639.xml", parseIso639Elements, m_languageTable);
68  parse(dataDir, "iso_3166.xml", parseIso3166Elements, m_countryTable);
69 }
70 
71 void Codetable::parseIso639Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
72 {
73  if(xml.name() == "iso_639_entry" ){
74  QString name = xml.attributes().value("name").toString();
75  QString code = xml.attributes().value("iso_639_1_code").toString();
76  if(!name.isEmpty() && !code.isEmpty()){
77  name = QString::fromUtf8(dgettext(ISO_639_DOMAIN, name.toLatin1().data()));
78  table.insert(code, name);
79  }
80  }
81 }
82 
83 void Codetable::parseIso3166Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
84 {
85  if(xml.name() == "iso_3166_entry" ){
86  QString name = xml.attributes().value("name").toString();
87  QString code = xml.attributes().value("alpha_2_code").toString();
88  if(!name.isEmpty() && !code.isEmpty()){
89  name = QString::fromUtf8(dgettext(ISO_3166_DOMAIN, name.toLatin1().data()));;
90  table.insert(code, name);
91  }
92  }
93 }
94 
95 void Codetable::parse(const QDir& dataDir, const QString& basename, const parser_t& parser, QMap<QString, QString>& table)
96 {
97  QString filename = QDir(QDir(dataDir.filePath("xml")).filePath("iso-codes")).absoluteFilePath(basename);
98  QFile file(filename);
99  if(!file.open(QIODevice::ReadOnly)){
100  qWarning("Failed to open %s for reading", file.fileName().toLatin1().data());
101  return;
102  }
103 
104  QXmlStreamReader xml(&file);
105  while(!xml.atEnd() && !xml.hasError()){
106  if(xml.readNext() == QXmlStreamReader::StartElement){
107  parser(xml, table);
108  }
109  }
110 }
111 
112 } // 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