author | William Roberts <williamr@symbian.org> |
Mon, 21 Jun 2010 22:38:13 +0100 | |
branch | GCC_SURGE |
changeset 27 | 93b982ccede2 |
parent 18 | 2f34d5167611 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "phrase.h" |
|
43 |
#include "translator.h" |
|
44 |
||
45 |
#include <QApplication> |
|
46 |
#include <QFile> |
|
47 |
#include <QFileInfo> |
|
48 |
#include <QMessageBox> |
|
49 |
#include <QRegExp> |
|
50 |
#include <QTextCodec> |
|
51 |
#include <QTextStream> |
|
52 |
#include <QXmlAttributes> |
|
53 |
#include <QXmlDefaultHandler> |
|
54 |
#include <QXmlParseException> |
|
55 |
||
56 |
QT_BEGIN_NAMESPACE |
|
57 |
||
58 |
static QString protect(const QString & str) |
|
59 |
{ |
|
60 |
QString p = str; |
|
61 |
p.replace(QLatin1Char('&'), QLatin1String("&")); |
|
62 |
p.replace(QLatin1Char('\"'), QLatin1String(""")); |
|
63 |
p.replace(QLatin1Char('>'), QLatin1String(">")); |
|
64 |
p.replace(QLatin1Char('<'), QLatin1String("<")); |
|
65 |
p.replace(QLatin1Char('\''), QLatin1String("'")); |
|
66 |
return p; |
|
67 |
} |
|
68 |
||
69 |
Phrase::Phrase() |
|
70 |
: shrtc(-1), m_phraseBook(0) |
|
71 |
{ |
|
72 |
} |
|
73 |
||
74 |
Phrase::Phrase(const QString &source, const QString &target, |
|
75 |
const QString &definition, int sc) |
|
76 |
: shrtc(sc), s(source), t(target), d(definition), |
|
77 |
m_phraseBook(0) |
|
78 |
{ |
|
79 |
} |
|
80 |
||
81 |
Phrase::Phrase(const QString &source, const QString &target, |
|
82 |
const QString &definition, PhraseBook *phraseBook) |
|
83 |
: shrtc(-1), s(source), t(target), d(definition), |
|
84 |
m_phraseBook(phraseBook) |
|
85 |
{ |
|
86 |
} |
|
87 |
||
88 |
void Phrase::setSource(const QString &ns) |
|
89 |
{ |
|
90 |
if (s == ns) |
|
91 |
return; |
|
92 |
s = ns; |
|
93 |
if (m_phraseBook) |
|
94 |
m_phraseBook->phraseChanged(this); |
|
95 |
} |
|
96 |
||
97 |
void Phrase::setTarget(const QString &nt) |
|
98 |
{ |
|
99 |
if (t == nt) |
|
100 |
return; |
|
101 |
t = nt; |
|
102 |
if (m_phraseBook) |
|
103 |
m_phraseBook->phraseChanged(this); |
|
104 |
} |
|
105 |
||
106 |
void Phrase::setDefinition(const QString &nd) |
|
107 |
{ |
|
108 |
if (d == nd) |
|
109 |
return; |
|
110 |
d = nd; |
|
111 |
if (m_phraseBook) |
|
112 |
m_phraseBook->phraseChanged(this); |
|
113 |
} |
|
114 |
||
115 |
bool operator==(const Phrase &p, const Phrase &q) |
|
116 |
{ |
|
117 |
return p.source() == q.source() && p.target() == q.target() && |
|
118 |
p.definition() == q.definition() && p.phraseBook() == q.phraseBook(); |
|
119 |
} |
|
120 |
||
121 |
class QphHandler : public QXmlDefaultHandler |
|
122 |
{ |
|
123 |
public: |
|
124 |
QphHandler(PhraseBook *phraseBook) |
|
125 |
: pb(phraseBook), ferrorCount(0) { } |
|
126 |
||
127 |
virtual bool startElement(const QString &namespaceURI, |
|
128 |
const QString &localName, const QString &qName, |
|
129 |
const QXmlAttributes &atts); |
|
130 |
virtual bool endElement(const QString &namespaceURI, |
|
131 |
const QString &localName, const QString &qName); |
|
132 |
virtual bool characters(const QString &ch); |
|
133 |
virtual bool fatalError(const QXmlParseException &exception); |
|
134 |
||
135 |
QString language() const { return m_language; } |
|
136 |
QString sourceLanguage() const { return m_sourceLanguage; } |
|
137 |
||
138 |
private: |
|
139 |
PhraseBook *pb; |
|
140 |
QString source; |
|
141 |
QString target; |
|
142 |
QString definition; |
|
143 |
QString m_language; |
|
144 |
QString m_sourceLanguage; |
|
145 |
||
146 |
QString accum; |
|
147 |
int ferrorCount; |
|
148 |
}; |
|
149 |
||
150 |
bool QphHandler::startElement(const QString & /* namespaceURI */, |
|
151 |
const QString & /* localName */, |
|
152 |
const QString &qName, |
|
153 |
const QXmlAttributes &atts) |
|
154 |
{ |
|
155 |
if (qName == QLatin1String("QPH")) { |
|
156 |
m_language = atts.value(QLatin1String("language")); |
|
157 |
m_sourceLanguage = atts.value(QLatin1String("sourcelanguage")); |
|
158 |
} else if (qName == QLatin1String("phrase")) { |
|
159 |
source.truncate(0); |
|
160 |
target.truncate(0); |
|
161 |
definition.truncate(0); |
|
162 |
} |
|
163 |
accum.truncate(0); |
|
164 |
return true; |
|
165 |
} |
|
166 |
||
167 |
bool QphHandler::endElement(const QString & /* namespaceURI */, |
|
168 |
const QString & /* localName */, |
|
169 |
const QString &qName) |
|
170 |
{ |
|
171 |
if (qName == QLatin1String("source")) |
|
172 |
source = accum; |
|
173 |
else if (qName == QLatin1String("target")) |
|
174 |
target = accum; |
|
175 |
else if (qName == QLatin1String("definition")) |
|
176 |
definition = accum; |
|
177 |
else if (qName == QLatin1String("phrase")) |
|
178 |
pb->m_phrases.append(new Phrase(source, target, definition, pb)); |
|
179 |
return true; |
|
180 |
} |
|
181 |
||
182 |
bool QphHandler::characters(const QString &ch) |
|
183 |
{ |
|
184 |
accum += ch; |
|
185 |
return true; |
|
186 |
} |
|
187 |
||
188 |
bool QphHandler::fatalError(const QXmlParseException &exception) |
|
189 |
{ |
|
190 |
if (ferrorCount++ == 0) { |
|
191 |
QString msg; |
|
192 |
msg.sprintf("Parse error at line %d, column %d (%s).", |
|
193 |
exception.lineNumber(), exception.columnNumber(), |
|
194 |
exception.message().toLatin1().constData()); |
|
195 |
QMessageBox::information(0, |
|
196 |
QObject::tr("Qt Linguist"), msg); |
|
197 |
} |
|
198 |
return false; |
|
199 |
} |
|
200 |
||
201 |
PhraseBook::PhraseBook() : |
|
202 |
m_changed(false), |
|
203 |
m_language(QLocale::C), |
|
204 |
m_sourceLanguage(QLocale::C), |
|
205 |
m_country(QLocale::AnyCountry), |
|
206 |
m_sourceCountry(QLocale::AnyCountry) |
|
207 |
{ |
|
208 |
} |
|
209 |
||
210 |
PhraseBook::~PhraseBook() |
|
211 |
{ |
|
212 |
qDeleteAll(m_phrases); |
|
213 |
} |
|
214 |
||
215 |
void PhraseBook::setLanguageAndCountry(QLocale::Language lang, QLocale::Country country) |
|
216 |
{ |
|
217 |
if (m_language == lang && m_country == country) |
|
218 |
return; |
|
219 |
m_language = lang; |
|
220 |
m_country = country; |
|
221 |
setModified(true); |
|
222 |
} |
|
223 |
||
224 |
void PhraseBook::setSourceLanguageAndCountry(QLocale::Language lang, QLocale::Country country) |
|
225 |
{ |
|
226 |
if (m_sourceLanguage == lang && m_sourceCountry == country) |
|
227 |
return; |
|
228 |
m_sourceLanguage = lang; |
|
229 |
m_sourceCountry = country; |
|
230 |
setModified(true); |
|
231 |
} |
|
232 |
||
233 |
bool PhraseBook::load(const QString &fileName, bool *langGuessed) |
|
234 |
{ |
|
235 |
QFile f(fileName); |
|
236 |
if (!f.open(QIODevice::ReadOnly)) |
|
237 |
return false; |
|
238 |
||
239 |
m_fileName = fileName; |
|
240 |
||
241 |
QXmlInputSource in(&f); |
|
242 |
QXmlSimpleReader reader; |
|
243 |
// don't click on these! |
|
244 |
reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), false); |
|
245 |
reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), true); |
|
246 |
reader.setFeature(QLatin1String("http://trolltech.com/xml/features/report-whitespace" |
|
247 |
"-only-CharData"), false); |
|
248 |
QphHandler *hand = new QphHandler(this); |
|
249 |
reader.setContentHandler(hand); |
|
250 |
reader.setErrorHandler(hand); |
|
251 |
||
252 |
bool ok = reader.parse(in); |
|
253 |
reader.setContentHandler(0); |
|
254 |
reader.setErrorHandler(0); |
|
255 |
||
256 |
Translator::languageAndCountry(hand->language(), &m_language, &m_country); |
|
257 |
*langGuessed = false; |
|
258 |
if (m_language == QLocale::C) { |
|
259 |
QLocale sys; |
|
260 |
m_language = sys.language(); |
|
261 |
m_country = sys.country(); |
|
262 |
*langGuessed = true; |
|
263 |
} |
|
264 |
||
265 |
QString lang = hand->sourceLanguage(); |
|
266 |
if (lang.isEmpty()) { |
|
267 |
m_sourceLanguage = QLocale::C; |
|
268 |
m_sourceCountry = QLocale::AnyCountry; |
|
269 |
} else { |
|
270 |
Translator::languageAndCountry(lang, &m_sourceLanguage, &m_sourceCountry); |
|
271 |
} |
|
272 |
||
273 |
delete hand; |
|
274 |
f.close(); |
|
275 |
if (!ok) { |
|
276 |
qDeleteAll(m_phrases); |
|
277 |
m_phrases.clear(); |
|
278 |
} else { |
|
279 |
emit listChanged(); |
|
280 |
} |
|
281 |
||
282 |
return ok; |
|
283 |
} |
|
284 |
||
285 |
bool PhraseBook::save(const QString &fileName) |
|
286 |
{ |
|
287 |
QFile f(fileName); |
|
288 |
if (!f.open(QIODevice::WriteOnly)) |
|
289 |
return false; |
|
290 |
||
291 |
m_fileName = fileName; |
|
292 |
||
293 |
QTextStream t(&f); |
|
294 |
t.setCodec( QTextCodec::codecForName("UTF-8") ); |
|
295 |
||
296 |
t << "<!DOCTYPE QPH>\n<QPH"; |
|
297 |
if (sourceLanguage() != QLocale::C) |
|
298 |
t << " sourcelanguage=\"" |
|
299 |
<< Translator::makeLanguageCode(sourceLanguage(), sourceCountry()) << '"'; |
|
300 |
if (language() != QLocale::C) |
|
301 |
t << " language=\"" << Translator::makeLanguageCode(language(), country()) << '"'; |
|
302 |
t << ">\n"; |
|
303 |
foreach (Phrase *p, m_phrases) { |
|
304 |
t << "<phrase>\n"; |
|
305 |
t << " <source>" << protect( p->source() ) << "</source>\n"; |
|
306 |
t << " <target>" << protect( p->target() ) << "</target>\n"; |
|
307 |
if (!p->definition().isEmpty()) |
|
308 |
t << " <definition>" << protect( p->definition() ) |
|
309 |
<< "</definition>\n"; |
|
310 |
t << "</phrase>\n"; |
|
311 |
} |
|
312 |
t << "</QPH>\n"; |
|
313 |
f.close(); |
|
314 |
setModified(false); |
|
315 |
return true; |
|
316 |
} |
|
317 |
||
318 |
void PhraseBook::append(Phrase *phrase) |
|
319 |
{ |
|
320 |
m_phrases.append(phrase); |
|
321 |
phrase->setPhraseBook(this); |
|
322 |
setModified(true); |
|
323 |
emit listChanged(); |
|
324 |
} |
|
325 |
||
326 |
void PhraseBook::remove(Phrase *phrase) |
|
327 |
{ |
|
328 |
m_phrases.removeOne(phrase); |
|
329 |
phrase->setPhraseBook(0); |
|
330 |
setModified(true); |
|
331 |
emit listChanged(); |
|
332 |
} |
|
333 |
||
334 |
void PhraseBook::setModified(bool modified) |
|
335 |
{ |
|
336 |
if (m_changed != modified) { |
|
337 |
emit modifiedChanged(modified); |
|
338 |
m_changed = modified; |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
void PhraseBook::phraseChanged(Phrase *p) |
|
343 |
{ |
|
344 |
Q_UNUSED(p); |
|
345 |
||
346 |
setModified(true); |
|
347 |
} |
|
348 |
||
349 |
QString PhraseBook::friendlyPhraseBookName() const |
|
350 |
{ |
|
351 |
if (!m_fileName.isEmpty()) |
|
352 |
return QFileInfo(m_fileName).fileName(); |
|
353 |
return QString(); |
|
354 |
} |
|
355 |
||
356 |
QT_END_NAMESPACE |