2
* Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
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.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "iso_codes.h"
25
#include <QtXml/QXmlAttributes>
28
class IsoCodesPrivate {
30
IsoCodesPrivate(const QString& isoCode_, const QString& isoCodesXmlDir_):
32
isoCodesXmlDir(isoCodesXmlDir_),
35
void buildIsoEntryList();
37
const QString isoCode;
38
const QString isoCodesXmlDir;
39
QList<IsoCodeEntry> isoEntryList;
43
class XmlHandler : public QXmlDefaultHandler
46
XmlHandler(const QString& isoCode_, QList<IsoCodeEntry>& isoEntryList_):
48
qName("iso_"+isoCode+"_entry"),
49
isoEntryList(isoEntryList_) {}
51
bool startElement(const QString &namespaceURI, const QString &localName,
52
const QString &qName, const QXmlAttributes &attributes);
53
// bool fatalError(const QXmlParseException &exception);
54
// QString errorString() const;
57
const QString isoCode;
59
QList<IsoCodeEntry>& isoEntryList;
62
bool XmlHandler::startElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
63
const QString &qName, const QXmlAttributes &attributes)
65
if( qName == this->qName ) {
67
for(int i=0; i<attributes.count(); i++) {
68
entry.insert(attributes.qName(i), attributes.value(i));
70
isoEntryList.append(entry);
76
IsoCodes::IsoCodes(const QString& isoCode, const QString& isoCodesXmlDir):
77
d(new IsoCodesPrivate(isoCode, isoCodesXmlDir))
79
KGlobal::locale()->insertCatalog(QString("iso_")+d->isoCode);
84
KGlobal::locale()->removeCatalog(QString("iso_")+d->isoCode);
88
QList<IsoCodeEntry> IsoCodes::getEntryList()
91
d->buildIsoEntryList();
93
return d->isoEntryList;
96
const char* IsoCodes::iso_639="639";
97
const char* IsoCodes::attr_name="name";
98
const char* IsoCodes::attr_iso_639_2B_code="iso_639_2B_code";
99
const char* IsoCodes::attr_iso_639_2T_code="iso_639_2T_code";
100
const char* IsoCodes::attr_iso_639_1_code="iso_639_1_code";
102
const IsoCodeEntry* IsoCodes::getEntry(const QString& attributeName, const QString& attributeValue)
105
d->buildIsoEntryList();
107
for(QList<IsoCodeEntry>::Iterator it = d->isoEntryList.begin(); it != d->isoEntryList.end(); ++it) {
108
const IsoCodeEntry* isoCodeEntry = &(*it);
109
if( isoCodeEntry->value(attributeName) == attributeValue )
115
void IsoCodesPrivate::buildIsoEntryList()
119
QFile file(QString("%1/iso_%2.xml").arg(isoCodesXmlDir, isoCode));
120
if( !file.open(QFile::ReadOnly | QFile::Text) ) {
121
kError() << "Can't open the xml file" << file.fileName();
125
XmlHandler xmlHandler(isoCode, isoEntryList);
127
QXmlSimpleReader reader;
128
reader.setContentHandler(&xmlHandler);
129
reader.setErrorHandler(&xmlHandler);
131
QXmlInputSource xmlInputSource(&file);
133
if( ! reader.parse(xmlInputSource) ) {
134
kError() << "Failed to parse the xml file" << file.fileName();
138
kDebug() << "Loaded" << isoEntryList.count() << ("iso entry definitions for iso"+isoCode) << "from" << file.fileName();