| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the Qt Linguist of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | TranslatorMessage::TranslatorMessage()
|
|---|
| 57 | : m_lineNumber(-1), m_type(Unfinished), m_utf8(false), m_nonUtf8(false), m_plural(false)
|
|---|
| 58 | {
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | TranslatorMessage::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 |
|
|---|
| 73 | void 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 |
|
|---|
| 83 | void 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 |
|
|---|
| 99 | void TranslatorMessage::clearReferences()
|
|---|
| 100 | {
|
|---|
| 101 | m_fileName.clear();
|
|---|
| 102 | m_lineNumber = -1;
|
|---|
| 103 | m_extraRefs.clear();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void 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 |
|
|---|
| 119 | TranslatorMessage::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 |
|
|---|
| 129 | static 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 |
|
|---|
| 137 | bool TranslatorMessage::needs8Bit() const
|
|---|
| 138 | {
|
|---|
| 139 | //dump();
|
|---|
| 140 | return needs8BitHelper(m_sourcetext)
|
|---|
| 141 | || needs8BitHelper(m_comment)
|
|---|
| 142 | || needs8BitHelper(m_context);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | bool TranslatorMessage::hasExtra(const QString &key) const
|
|---|
| 147 | {
|
|---|
| 148 | return m_extra.contains(key);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | QString TranslatorMessage::extra(const QString &key) const
|
|---|
| 152 | {
|
|---|
| 153 | return m_extra[key];
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | void TranslatorMessage::setExtra(const QString &key, const QString &value)
|
|---|
| 157 | {
|
|---|
| 158 | m_extra[key] = value;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void TranslatorMessage::unsetExtra(const QString &key)
|
|---|
| 162 | {
|
|---|
| 163 | m_extra.remove(key);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | void TranslatorMessage::dump() const
|
|---|
| 167 | {
|
|---|
| 168 | qDebug()
|
|---|
| 169 | << "\nId : " << m_id
|
|---|
| 170 | << "\nContext : " << m_context
|
|---|
| 171 | << "\nSource : " << m_sourcetext
|
|---|
| 172 | << "\nComment : " << m_comment
|
|---|
| 173 | << "\nUserData : " << m_userData
|
|---|
| 174 | << "\nExtraComment : " << m_extraComment
|
|---|
| 175 | << "\nTranslatorComment : " << m_translatorComment
|
|---|
| 176 | << "\nTranslations : " << m_translations
|
|---|
| 177 | << "\nFileName : " << m_fileName
|
|---|
| 178 | << "\nLineNumber : " << m_lineNumber
|
|---|
| 179 | << "\nType : " << m_type
|
|---|
| 180 | << "\nPlural : " << m_plural
|
|---|
| 181 | << "\nExtra : " << m_extra;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | QT_END_NAMESPACE
|
|---|
| 186 |
|
|---|
| 187 | #endif // QT_NO_TRANSLATION
|
|---|