source: branches/vendor/trolltech/qt/current/tools/linguist/shared/translatormessage.cpp @ 2

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

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

File size: 6.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
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 qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "translatormessage.h"
43
44#include <qplatformdefs.h>
45
46#ifndef QT_NO_TRANSLATION
47
48#include <QDataStream>
49#include <QDebug>
50
51#include <stdlib.h>
52
53
54QT_BEGIN_NAMESPACE
55
56TranslatorMessage::TranslatorMessage()
57  : m_lineNumber(-1), m_type(Unfinished), m_utf8(false), m_nonUtf8(false), m_plural(false)
58{
59}
60
61TranslatorMessage::TranslatorMessage(const QString &context,
62    const QString &sourceText, const QString &comment,
63    const QString &userData,
64    const QString &fileName, int lineNumber, const QStringList &translations,
65    Type type, bool plural)
66  : m_context(context), m_sourcetext(sourceText), m_comment(comment),
67    m_userData(userData),
68    m_translations(translations), m_fileName(fileName), m_lineNumber(lineNumber),
69    m_type(type), m_utf8(false), m_nonUtf8(false), m_plural(plural)
70{
71}
72
73void TranslatorMessage::addReference(const QString &fileName, int lineNumber)
74{
75    if (m_fileName.isEmpty()) {
76        m_fileName = fileName;
77        m_lineNumber = lineNumber;
78    } else {
79        m_extraRefs.append(Reference(fileName, lineNumber));
80    }
81}
82
83void TranslatorMessage::addReferenceUniq(const QString &fileName, int lineNumber)
84{
85    if (m_fileName.isEmpty()) {
86        m_fileName = fileName;
87        m_lineNumber = lineNumber;
88    } else {
89        if (fileName == m_fileName && lineNumber == m_lineNumber)
90            return;
91        if (!m_extraRefs.isEmpty()) // Rather common case, so special-case it
92            foreach (const Reference &ref, m_extraRefs)
93                if (fileName == ref.fileName() && lineNumber == ref.lineNumber())
94                    return;
95        m_extraRefs.append(Reference(fileName, lineNumber));
96    }
97}
98
99void TranslatorMessage::clearReferences()
100{
101    m_fileName.clear();
102    m_lineNumber = -1;
103    m_extraRefs.clear();
104}
105
106void TranslatorMessage::setReferences(const TranslatorMessage::References &refs0)
107{
108    if (!refs0.isEmpty()) {
109        References refs = refs0;
110        const Reference &ref = refs.takeFirst();
111        m_fileName = ref.fileName();
112        m_lineNumber = ref.lineNumber();
113        m_extraRefs = refs;
114    } else {
115        clearReferences();
116    }
117}
118
119TranslatorMessage::References TranslatorMessage::allReferences() const
120{
121    References refs;
122    if (!m_fileName.isEmpty()) {
123        refs.append(Reference(m_fileName, m_lineNumber));
124        refs += m_extraRefs;
125    }
126    return refs;
127}
128
129static bool needs8BitHelper(const QString &ba)
130{
131    for (int i = ba.size(); --i >= 0; )
132        if (ba.at(i).unicode() >= 0x80)
133            return true;
134    return false;
135}
136
137bool TranslatorMessage::needs8Bit() const
138{
139    //dump();
140    return needs8BitHelper(m_sourcetext)
141        || needs8BitHelper(m_comment)
142        || needs8BitHelper(m_context);
143}
144
145
146bool TranslatorMessage::operator==(const TranslatorMessage& m) const
147{
148    static QString msgIdPlural = QLatin1String("po-msgid_plural");
149
150    // Special treatment for context comments (empty source).
151    return (m_context == m.m_context)
152        && m_sourcetext == m.m_sourcetext
153        && m_extra[msgIdPlural] == m.m_extra[msgIdPlural]
154        && (m_sourcetext.isEmpty() || m_comment == m.m_comment);
155}
156
157
158bool TranslatorMessage::operator<(const TranslatorMessage& m) const
159{
160    if (m_context != m.m_context)
161        return m_context < m.m_context;
162    if (m_sourcetext != m.m_sourcetext)
163        return m_sourcetext < m.m_sourcetext;
164    return m_comment < m.m_comment;
165}
166
167int qHash(const TranslatorMessage &msg)
168{
169    return
170        qHash(msg.context()) ^
171        qHash(msg.sourceText()) ^
172        qHash(msg.extra(QLatin1String("po-msgid_plural"))) ^
173        qHash(msg.comment());
174}
175
176bool TranslatorMessage::hasExtra(const QString &key) const
177{
178    return m_extra.contains(key);
179}
180
181QString TranslatorMessage::extra(const QString &key) const
182{
183    return m_extra[key];
184}
185
186void TranslatorMessage::setExtra(const QString &key, const QString &value)
187{
188    m_extra[key] = value;
189}
190
191void TranslatorMessage::unsetExtra(const QString &key)
192{
193    m_extra.remove(key);
194}
195
196void TranslatorMessage::dump() const
197{
198    qDebug()
199        << "\nId                : " << m_id
200        << "\nContext           : " << m_context
201        << "\nSource            : " << m_sourcetext
202        << "\nComment           : " << m_comment
203        << "\nUserData          : " << m_userData
204        << "\nExtraComment      : " << m_extraComment
205        << "\nTranslatorComment : " << m_translatorComment
206        << "\nTranslations      : " << m_translations
207        << "\nFileName          : " << m_fileName
208        << "\nLineNumber        : " << m_lineNumber
209        << "\nType              : " << m_type
210        << "\nPlural            : " << m_plural
211        << "\nExtra             : " << m_extra;
212}
213
214
215QT_END_NAMESPACE
216
217#endif // QT_NO_TRANSLATION
Note: See TracBrowser for help on using the repository browser.