QtSpell  0.8.5
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, QString& extra) 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  if(parts.size() > 2) {
52  extra = QStringList(parts.mid(2)).join("_");
53  }
54 }
55 
56 Codetable::Codetable()
57 {
58 #ifdef Q_OS_WIN32
59  QDir dataDir = QDir(QString("%1/../share").arg(QCoreApplication::applicationDirPath()));
60 #else
61  QDir dataDir = QDir(ISO_CODES_PREFIX "/share").absolutePath();
62 #endif
63 
64  bindtextdomain(ISO_639_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
65  bind_textdomain_codeset(ISO_639_DOMAIN, "UTF-8");
66 
67  bindtextdomain(ISO_3166_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data());
68  bind_textdomain_codeset(ISO_3166_DOMAIN, "UTF-8");
69 
70  parse(dataDir, "iso_639.xml", parseIso639Elements, m_languageTable);
71  parse(dataDir, "iso_3166.xml", parseIso3166Elements, m_countryTable);
72 }
73 
74 void Codetable::parseIso639Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
75 {
76  if(xml.name() == "iso_639_entry" ){
77  QString name = xml.attributes().value("name").toString();
78  QString code = xml.attributes().value("iso_639_1_code").toString();
79  if(!name.isEmpty() && !code.isEmpty()){
80  name = QString::fromUtf8(dgettext(ISO_639_DOMAIN, name.toLatin1().data()));
81  table.insert(code, name);
82  }
83  }
84 }
85 
86 void Codetable::parseIso3166Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table)
87 {
88  if(xml.name() == "iso_3166_entry" ){
89  QString name = xml.attributes().value("name").toString();
90  QString code = xml.attributes().value("alpha_2_code").toString();
91  if(!name.isEmpty() && !code.isEmpty()){
92  name = QString::fromUtf8(dgettext(ISO_3166_DOMAIN, name.toLatin1().data()));;
93  table.insert(code, name);
94  }
95  }
96 }
97 
98 void Codetable::parse(const QDir& dataDir, const QString& basename, const parser_t& parser, QMap<QString, QString>& table)
99 {
100  QString filename = QDir(QDir(dataDir.filePath("xml")).filePath("iso-codes")).absoluteFilePath(basename);
101  QFile file(filename);
102  if(!file.open(QIODevice::ReadOnly)){
103  qWarning("Failed to open %s for reading", file.fileName().toLatin1().data());
104  return;
105  }
106 
107  QXmlStreamReader xml(&file);
108  while(!xml.atEnd() && !xml.hasError()){
109  if(xml.readNext() == QXmlStreamReader::StartElement){
110  parser(xml, table);
111  }
112  }
113 }
114 
115 } // QtSpell
QtSpell
QtSpell namespace.
Definition: Checker.cpp:65
QtSpell::Codetable
Class for translating locale identifiers into human readable strings.
Definition: Codetable.hpp:34
QtSpell::Codetable::lookup
void lookup(const QString &language_code, QString &language_name, QString &country_name, QString &extra) const
Looks up the language and country name for the specified language code. If no matching entries are fo...
Definition: Codetable.cpp:37
QtSpell::Codetable::instance
static Codetable * instance()
Get codetable instance.
Definition: Codetable.cpp:31