1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Linguist of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "translatormessage.h"
30 
31 #include <qplatformdefs.h>
32 
33 #include <QDataStream>
34 #include <QDebug>
35 
36 #include <stdlib.h>
37 
38 
39 QT_BEGIN_NAMESPACE
40 
TranslatorMessage()41 TranslatorMessage::TranslatorMessage()
42   : m_lineNumber(-1), m_type(Unfinished), m_plural(false)
43 {
44 }
45 
TranslatorMessage(const QString & context,const QString & sourceText,const QString & comment,const QString & userData,const QString & fileName,int lineNumber,const QStringList & translations,Type type,bool plural)46 TranslatorMessage::TranslatorMessage(const QString &context,
47     const QString &sourceText, const QString &comment,
48     const QString &userData,
49     const QString &fileName, int lineNumber, const QStringList &translations,
50     Type type, bool plural)
51   : m_context(context), m_sourcetext(sourceText), m_comment(comment),
52     m_userData(userData),
53     m_translations(translations), m_fileName(fileName), m_lineNumber(lineNumber),
54     m_type(type), m_plural(plural)
55 {
56 }
57 
addReference(const QString & fileName,int lineNumber)58 void TranslatorMessage::addReference(const QString &fileName, int lineNumber)
59 {
60     if (m_fileName.isEmpty()) {
61         m_fileName = fileName;
62         m_lineNumber = lineNumber;
63     } else {
64         m_extraRefs.append(Reference(fileName, lineNumber));
65     }
66 }
67 
addReferenceUniq(const QString & fileName,int lineNumber)68 void TranslatorMessage::addReferenceUniq(const QString &fileName, int lineNumber)
69 {
70     if (m_fileName.isEmpty()) {
71         m_fileName = fileName;
72         m_lineNumber = lineNumber;
73     } else {
74         if (fileName == m_fileName && lineNumber == m_lineNumber)
75             return;
76         if (!m_extraRefs.isEmpty()) { // Rather common case, so special-case it
77             foreach (const Reference &ref, m_extraRefs) {
78                 if (fileName == ref.fileName() && lineNumber == ref.lineNumber())
79                     return;
80             }
81         }
82         m_extraRefs.append(Reference(fileName, lineNumber));
83     }
84 }
85 
clearReferences()86 void TranslatorMessage::clearReferences()
87 {
88     m_fileName.clear();
89     m_lineNumber = -1;
90     m_extraRefs.clear();
91 }
92 
setReferences(const TranslatorMessage::References & refs0)93 void TranslatorMessage::setReferences(const TranslatorMessage::References &refs0)
94 {
95     if (!refs0.isEmpty()) {
96         References refs = refs0;
97         const Reference &ref = refs.takeFirst();
98         m_fileName = ref.fileName();
99         m_lineNumber = ref.lineNumber();
100         m_extraRefs = refs;
101     } else {
102         clearReferences();
103     }
104 }
105 
allReferences() const106 TranslatorMessage::References TranslatorMessage::allReferences() const
107 {
108     References refs;
109     if (!m_fileName.isEmpty()) {
110         refs.append(Reference(m_fileName, m_lineNumber));
111         refs += m_extraRefs;
112     }
113     return refs;
114 }
115 
116 
hasExtra(const QString & key) const117 bool TranslatorMessage::hasExtra(const QString &key) const
118 {
119     return m_extra.contains(key);
120 }
121 
extra(const QString & key) const122 QString TranslatorMessage::extra(const QString &key) const
123 {
124     return m_extra[key];
125 }
126 
setExtra(const QString & key,const QString & value)127 void TranslatorMessage::setExtra(const QString &key, const QString &value)
128 {
129     m_extra[key] = value;
130 }
131 
unsetExtra(const QString & key)132 void TranslatorMessage::unsetExtra(const QString &key)
133 {
134     m_extra.remove(key);
135 }
136 
dump() const137 void TranslatorMessage::dump() const
138 {
139     qDebug()
140         << "\nId                : " << m_id
141         << "\nContext           : " << m_context
142         << "\nSource            : " << m_sourcetext
143         << "\nComment           : " << m_comment
144         << "\nUserData          : " << m_userData
145         << "\nExtraComment      : " << m_extraComment
146         << "\nTranslatorComment : " << m_translatorComment
147         << "\nTranslations      : " << m_translations
148         << "\nFileName          : " << m_fileName
149         << "\nLineNumber        : " << m_lineNumber
150         << "\nType              : " << m_type
151         << "\nPlural            : " << m_plural
152         << "\nExtra             : " << m_extra;
153 }
154 
155 
156 QT_END_NAMESPACE
157