/* * Copyright (C) 2010 Andriy Rysin (rysin@kde.org) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "iso_codes.h" #include #include #include #include #include class IsoCodesPrivate { public: IsoCodesPrivate(const QString& isoCode_): isoCode(isoCode_), isoCodesXmlDir(QLatin1String("/usr/share/xml/iso-codes")), loaded(false) { static const QStringList additionalCodesDirs = QStringList() << QLatin1String("/usr/local/share/xml/iso-codes") << QLatin1String("/usr/pkg/share/xml/iso-codes"); // NetBSD foreach (const QString &dir, additionalCodesDirs) { if (QDir(dir).exists()) { isoCodesXmlDir = dir; } } } void buildIsoEntryList(); const QString isoCode; QString isoCodesXmlDir; QList isoEntryList; bool loaded; }; class XmlHandler : public QXmlDefaultHandler { public: XmlHandler(const QString& isoCode_, QList& isoEntryList_): isoCode(isoCode_), qName("iso_"+isoCode+"_entry"), isoEntryList(isoEntryList_) {} bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes); // bool fatalError(const QXmlParseException &exception); // QString errorString() const; private: const QString isoCode; const QString qName; QList& isoEntryList; }; bool XmlHandler::startElement(const QString &/*namespaceURI*/, const QString &/*localName*/, const QString &qName, const QXmlAttributes &attributes) { if( qName == this->qName ) { IsoCodeEntry entry; for(int i=0; iinsertCatalog(QString("iso_")+d->isoCode); } IsoCodes::~IsoCodes() { KGlobal::locale()->removeCatalog(QString("iso_")+d->isoCode); delete d; } QList IsoCodes::getEntryList() { if( ! d->loaded ) { d->buildIsoEntryList(); } return d->isoEntryList; } //const char* IsoCodes::iso_639="639"; const char* IsoCodes::iso_639_3="639_3"; const char* IsoCodes::attr_name="name"; //const char* IsoCodes::attr_iso_639_2B_code="iso_639_2B_code"; //const char* IsoCodes::attr_iso_639_2T_code="iso_639_2T_code"; //const char* IsoCodes::attr_iso_639_1_code="iso_639_1_code"; const char* IsoCodes::attr_iso_639_3_id="id"; const IsoCodeEntry* IsoCodes::getEntry(const QString& attributeName, const QString& attributeValue) { if( ! d->loaded ) { d->buildIsoEntryList(); } for(QList::Iterator it = d->isoEntryList.begin(); it != d->isoEntryList.end(); ++it) { const IsoCodeEntry* isoCodeEntry = &(*it); if( isoCodeEntry->value(attributeName) == attributeValue ) return isoCodeEntry; } return NULL; } void IsoCodesPrivate::buildIsoEntryList() { loaded = true; QFile file(QString("%1/iso_%2.xml").arg(isoCodesXmlDir, isoCode)); if( !file.open(QFile::ReadOnly | QFile::Text) ) { kError() << "Can't open the xml file" << file.fileName(); return; } XmlHandler xmlHandler(isoCode, isoEntryList); QXmlSimpleReader reader; reader.setContentHandler(&xmlHandler); reader.setErrorHandler(&xmlHandler); QXmlInputSource xmlInputSource(&file); if( ! reader.parse(xmlInputSource) ) { kError() << "Failed to parse the xml file" << file.fileName(); return; } kDebug() << "Loaded" << isoEntryList.count() << ("iso entry definitions for iso"+isoCode) << "from" << file.fileName(); }