~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/iso_codes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 
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
 
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.
 
17
 */
 
18
 
 
19
#include "iso_codes.h"
 
20
 
 
21
#include <kglobal.h>
 
22
#include <klocale.h>
 
23
#include <kdebug.h>
 
24
 
 
25
#include <QtXml/QXmlAttributes>
 
26
 
 
27
 
 
28
class IsoCodesPrivate {
 
29
public:
 
30
        IsoCodesPrivate(const QString& isoCode_, const QString& isoCodesXmlDir_):
 
31
                isoCode(isoCode_),
 
32
                isoCodesXmlDir(isoCodesXmlDir_),
 
33
                loaded(false)
 
34
        {}
 
35
        void buildIsoEntryList();
 
36
 
 
37
        const QString isoCode;
 
38
        const QString isoCodesXmlDir;
 
39
        QList<IsoCodeEntry> isoEntryList;
 
40
        bool loaded;
 
41
};
 
42
 
 
43
class XmlHandler : public QXmlDefaultHandler
 
44
{
 
45
public:
 
46
        XmlHandler(const QString& isoCode_, QList<IsoCodeEntry>& isoEntryList_):
 
47
                isoCode(isoCode_),
 
48
                qName("iso_"+isoCode+"_entry"),
 
49
                isoEntryList(isoEntryList_) {}
 
50
 
 
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;
 
55
 
 
56
private:
 
57
    const QString isoCode;
 
58
    const QString qName;
 
59
    QList<IsoCodeEntry>& isoEntryList;
 
60
};
 
61
 
 
62
bool XmlHandler::startElement(const QString &/*namespaceURI*/, const QString &/*localName*/,
 
63
                      const QString &qName, const QXmlAttributes &attributes)
 
64
{
 
65
        if( qName == this->qName ) {
 
66
                IsoCodeEntry entry;
 
67
                for(int i=0; i<attributes.count(); i++) {
 
68
                        entry.insert(attributes.qName(i), attributes.value(i));
 
69
                }
 
70
                isoEntryList.append(entry);
 
71
        }
 
72
        return true;
 
73
}
 
74
 
 
75
 
 
76
IsoCodes::IsoCodes(const QString& isoCode, const QString& isoCodesXmlDir):
 
77
        d(new IsoCodesPrivate(isoCode, isoCodesXmlDir))
 
78
{
 
79
        KGlobal::locale()->insertCatalog(QString("iso_")+d->isoCode);
 
80
}
 
81
 
 
82
IsoCodes::~IsoCodes()
 
83
{
 
84
        KGlobal::locale()->removeCatalog(QString("iso_")+d->isoCode);
 
85
        delete d;
 
86
}
 
87
 
 
88
QList<IsoCodeEntry> IsoCodes::getEntryList()
 
89
{
 
90
        if( ! d->loaded ) {
 
91
                d->buildIsoEntryList();
 
92
        }
 
93
        return d->isoEntryList;
 
94
}
 
95
 
 
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";
 
101
 
 
102
const IsoCodeEntry* IsoCodes::getEntry(const QString& attributeName, const QString& attributeValue)
 
103
{
 
104
        if( ! d->loaded ) {
 
105
                d->buildIsoEntryList();
 
106
        }
 
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 )
 
110
                        return isoCodeEntry;
 
111
        }
 
112
        return NULL;
 
113
}
 
114
 
 
115
void IsoCodesPrivate::buildIsoEntryList()
 
116
{
 
117
        loaded = true;
 
118
 
 
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();
 
122
                return;
 
123
        }
 
124
 
 
125
        XmlHandler xmlHandler(isoCode, isoEntryList);
 
126
 
 
127
        QXmlSimpleReader reader;
 
128
        reader.setContentHandler(&xmlHandler);
 
129
        reader.setErrorHandler(&xmlHandler);
 
130
 
 
131
        QXmlInputSource xmlInputSource(&file);
 
132
 
 
133
        if( ! reader.parse(xmlInputSource) ) {
 
134
                kError() << "Failed to parse the xml file" << file.fileName();
 
135
                return;
 
136
        }
 
137
 
 
138
        kDebug() << "Loaded" << isoEntryList.count() << ("iso entry definitions for iso"+isoCode) << "from" << file.fileName();
 
139
}