source: trunk/tools/linguist/shared/ui.cpp@ 275

Last change on this file since 275 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "translator.h"
43
44#include <QtCore/QDebug>
45#include <QtCore/QFile>
46#include <QtCore/QString>
47
48#include <QtXml/QXmlAttributes>
49#include <QtXml/QXmlDefaultHandler>
50#include <QtXml/QXmlLocator>
51#include <QtXml/QXmlParseException>
52
53
54QT_BEGIN_NAMESPACE
55
56// in cpp.cpp
57void fetchtrInlinedCpp(const QString &in, Translator &tor, const QString &context);
58
59class UiReader : public QXmlDefaultHandler
60{
61public:
62 UiReader(Translator &translator, ConversionData &cd)
63 : m_translator(translator), m_cd(cd), m_lineNumber(-1),
64 m_needUtf8(translator.codecName() != "UTF-8")
65 {}
66
67 bool startElement(const QString &namespaceURI, const QString &localName,
68 const QString &qName, const QXmlAttributes &atts);
69 bool endElement(const QString &namespaceURI, const QString &localName,
70 const QString &qName);
71 bool characters(const QString &ch);
72 bool fatalError(const QXmlParseException &exception);
73
74 void setDocumentLocator(QXmlLocator *locator) { m_locator = locator; }
75
76private:
77 void flush();
78
79 Translator &m_translator;
80 ConversionData &m_cd;
81 QString m_context;
82 QString m_source;
83 QString m_comment;
84 QXmlLocator *m_locator;
85
86 QString m_accum;
87 int m_lineNumber;
88 bool m_isTrString;
89 bool m_needUtf8;
90};
91
92bool UiReader::startElement(const QString &namespaceURI,
93 const QString &localName, const QString &qName, const QXmlAttributes &atts)
94{
95 Q_UNUSED(namespaceURI);
96 Q_UNUSED(localName);
97
98 if (qName == QLatin1String("item")) { // UI3 menu entries
99 flush();
100 if (!atts.value(QLatin1String("text")).isEmpty())
101 m_source = atts.value(QLatin1String("text"));
102 } else if (qName == QLatin1String("string")) {
103 flush();
104 if (atts.value(QLatin1String("notr")).isEmpty() ||
105 atts.value(QLatin1String("notr")) != QLatin1String("true")) {
106 m_isTrString = true;
107 m_comment = atts.value(QLatin1String("comment"));
108 } else {
109 m_isTrString = false;
110 }
111 }
112 if (m_isTrString && !m_cd.m_noUiLines)
113 m_lineNumber = m_locator->lineNumber();
114 m_accum.clear();
115 return true;
116}
117
118bool UiReader::endElement(const QString &namespaceURI,
119 const QString &localName, const QString &qName)
120{
121 Q_UNUSED(namespaceURI);
122 Q_UNUSED(localName);
123
124 m_accum.replace(QLatin1String("\r\n"), QLatin1String("\n"));
125
126 if (qName == QLatin1String("class")) { // UI "header"
127 if (m_context.isEmpty())
128 m_context = m_accum;
129 } else if (qName == QLatin1String("string") && m_isTrString) {
130 m_source = m_accum;
131 } else if (qName == QLatin1String("comment")) { // FIXME: what's that?
132 m_comment = m_accum;
133 flush();
134 } else if (qName == QLatin1String("function")) { // UI3 embedded code
135 fetchtrInlinedCpp(m_accum, m_translator, m_context);
136 } else {
137 flush();
138 }
139 return true;
140}
141
142bool UiReader::characters(const QString &ch)
143{
144 m_accum += ch;
145 return true;
146}
147
148bool UiReader::fatalError(const QXmlParseException &exception)
149{
150 QString msg;
151 msg.sprintf("XML error: Parse error at line %d, column %d (%s).",
152 exception.lineNumber(), exception.columnNumber(),
153 exception.message().toLatin1().data());
154 m_cd.appendError(msg);
155 return false;
156}
157
158void UiReader::flush()
159{
160 if (!m_context.isEmpty() && !m_source.isEmpty()) {
161 TranslatorMessage msg(m_context, m_source,
162 m_comment, QString(), m_cd.m_sourceFileName,
163 m_lineNumber, QStringList());
164 if (m_needUtf8 && msg.needs8Bit())
165 msg.setUtf8(true);
166 m_translator.extend(msg);
167 }
168 m_source.clear();
169 m_comment.clear();
170}
171
172bool loadUI(Translator &translator, QIODevice &dev, ConversionData &cd)
173{
174 QXmlInputSource in(&dev);
175 QXmlSimpleReader reader;
176 reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), false);
177 reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), true);
178 reader.setFeature(QLatin1String(
179 "http://trolltech.com/xml/features/report-whitespace-only-CharData"), false);
180 UiReader handler(translator, cd);
181 reader.setContentHandler(&handler);
182 reader.setErrorHandler(&handler);
183 bool result = reader.parse(in);
184 if (!result)
185 cd.appendError(QLatin1String("Parse error in UI file"));
186 reader.setContentHandler(0);
187 reader.setErrorHandler(0);
188 return result;
189}
190
191bool saveUI(const Translator &translator, QIODevice &dev, ConversionData &cd)
192{
193 Q_UNUSED(dev);
194 Q_UNUSED(translator);
195 cd.appendError(QLatin1String("Cannot save .ui files"));
196 return false;
197}
198
199int initUI()
200{
201 Translator::FileFormat format;
202
203 // "real" Qt Designer
204 format.extension = QLatin1String("ui");
205 format.description = QObject::tr("Qt Designer form files");
206 format.fileType = Translator::FileFormat::SourceCode;
207 format.priority = 0;
208 format.loader = &loadUI;
209 format.saver = &saveUI;
210 Translator::registerFileFormat(format);
211
212 // same for jambi
213 format.extension = QLatin1String("jui");
214 format.description = QObject::tr("Qt Jambi form files");
215 format.fileType = Translator::FileFormat::SourceCode;
216 format.priority = 0;
217 format.loader = &loadUI;
218 format.saver = &saveUI;
219 Translator::registerFileFormat(format);
220
221 return 1;
222}
223
224Q_CONSTRUCTOR_FUNCTION(initUI)
225
226QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.