|
QtSpell
0.8.2
Spell checking for Qt text widgets
|
00001 /* QtSpell - Spell checking for Qt text widgets. 00002 * Copyright (c) 2014 Sandro Mani 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 00019 #include "Codetable.hpp" 00020 #include <QCoreApplication> 00021 #include <QDir> 00022 #include <QFile> 00023 #include <QXmlStreamReader> 00024 #include <libintl.h> 00025 00026 #define ISO_639_DOMAIN "iso_639" 00027 #define ISO_3166_DOMAIN "iso_3166" 00028 00029 namespace QtSpell { 00030 00031 Codetable* Codetable::instance() 00032 { 00033 static Codetable codetable; 00034 return &codetable; 00035 } 00036 00037 void Codetable::lookup(const QString &language_code, QString &language_name, QString &country_name) const 00038 { 00039 QStringList parts = language_code.split("_"); 00040 if(parts.size() != 2) { 00041 language_name = language_code; 00042 country_name = ""; 00043 return; 00044 } 00045 00046 language_name = m_languageTable.contains(parts[0]) ? m_languageTable.value(parts[0]) : parts[0]; 00047 country_name = m_countryTable.contains(parts[1]) ? m_countryTable.value(parts[1]) : parts[1]; 00048 } 00049 00050 Codetable::Codetable() 00051 { 00052 #ifdef Q_OS_WIN32 00053 QDir dataDir = QDir(QString("%1/../share").arg(QCoreApplication::applicationDirPath())); 00054 #else 00055 QDir dataDir = QDir(ISO_CODES_PREFIX "/share").absolutePath(); 00056 #endif 00057 00058 bindtextdomain(ISO_639_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data()); 00059 bind_textdomain_codeset(ISO_639_DOMAIN, "UTF-8"); 00060 00061 bindtextdomain(ISO_3166_DOMAIN, dataDir.absoluteFilePath("locale").toLocal8Bit().data()); 00062 bind_textdomain_codeset(ISO_3166_DOMAIN, "UTF-8"); 00063 00064 parse(dataDir, "iso_639.xml", parseIso639Elements, m_languageTable); 00065 parse(dataDir, "iso_3166.xml", parseIso3166Elements, m_countryTable); 00066 } 00067 00068 void Codetable::parseIso639Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table) 00069 { 00070 if(xml.name() == "iso_639_entry" ){ 00071 QString name = xml.attributes().value("name").toString(); 00072 QString code = xml.attributes().value("iso_639_1_code").toString(); 00073 if(!name.isEmpty() && !code.isEmpty()){ 00074 name = QString::fromUtf8(dgettext(ISO_639_DOMAIN, name.toLatin1().data())); 00075 table.insert(code, name); 00076 } 00077 } 00078 } 00079 00080 void Codetable::parseIso3166Elements(const QXmlStreamReader &xml, QMap<QString, QString> &table) 00081 { 00082 if(xml.name() == "iso_3166_entry" ){ 00083 QString name = xml.attributes().value("name").toString(); 00084 QString code = xml.attributes().value("alpha_2_code").toString(); 00085 if(!name.isEmpty() && !code.isEmpty()){ 00086 name = QString::fromUtf8(dgettext(ISO_3166_DOMAIN, name.toLatin1().data()));; 00087 table.insert(code, name); 00088 } 00089 } 00090 } 00091 00092 void Codetable::parse(const QDir& dataDir, const QString& basename, const parser_t& parser, QMap<QString, QString>& table) 00093 { 00094 QString filename = QDir(QDir(dataDir.filePath("xml")).filePath("iso-codes")).absoluteFilePath(basename); 00095 QFile file(filename); 00096 if(!file.open(QIODevice::ReadOnly)){ 00097 qWarning("Failed to open %s for reading", file.fileName().toLatin1().data()); 00098 return; 00099 } 00100 00101 QXmlStreamReader xml(&file); 00102 while(!xml.atEnd() && !xml.hasError()){ 00103 if(xml.readNext() == QXmlStreamReader::StartElement){ 00104 parser(xml, table); 00105 } 00106 } 00107 } 00108 00109 } // QtSpell
1.7.6.1