1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "translatormessage.h"
5
6#include <qplatformdefs.h>
7
8#include <QDataStream>
9#include <QDebug>
10
11#include <stdlib.h>
12
13
14QT_BEGIN_NAMESPACE
15
16TranslatorMessage::TranslatorMessage()
17 : m_lineNumber(-1), m_type(Unfinished), m_plural(false)
18{
19}
20
21TranslatorMessage::TranslatorMessage(const QString &context,
22 const QString &sourceText, const QString &comment,
23 const QString &userData,
24 const QString &fileName, int lineNumber, const QStringList &translations,
25 Type type, bool plural)
26 : m_context(context), m_sourcetext(sourceText), m_comment(comment),
27 m_userData(userData),
28 m_translations(translations), m_fileName(fileName), m_lineNumber(lineNumber),
29 m_type(type), m_plural(plural)
30{
31}
32
33void TranslatorMessage::addReference(const QString &fileName, int lineNumber)
34{
35 if (m_fileName.isEmpty()) {
36 m_fileName = fileName;
37 m_lineNumber = lineNumber;
38 } else {
39 m_extraRefs.append(t: Reference(fileName, lineNumber));
40 }
41}
42
43void TranslatorMessage::addReferenceUniq(const QString &fileName, int lineNumber)
44{
45 if (m_fileName.isEmpty()) {
46 m_fileName = fileName;
47 m_lineNumber = lineNumber;
48 } else {
49 if (fileName == m_fileName && lineNumber == m_lineNumber)
50 return;
51 if (!m_extraRefs.isEmpty()) { // Rather common case, so special-case it
52 for (const Reference &ref : std::as_const(t&: m_extraRefs)) {
53 if (fileName == ref.fileName() && lineNumber == ref.lineNumber())
54 return;
55 }
56 }
57 m_extraRefs.append(t: Reference(fileName, lineNumber));
58 }
59}
60
61void TranslatorMessage::clearReferences()
62{
63 m_fileName.clear();
64 m_lineNumber = -1;
65 m_extraRefs.clear();
66}
67
68void TranslatorMessage::setReferences(const TranslatorMessage::References &refs0)
69{
70 if (!refs0.isEmpty()) {
71 References refs = refs0;
72 const Reference &ref = refs.takeFirst();
73 m_fileName = ref.fileName();
74 m_lineNumber = ref.lineNumber();
75 m_extraRefs = refs;
76 } else {
77 clearReferences();
78 }
79}
80
81TranslatorMessage::References TranslatorMessage::allReferences() const
82{
83 References refs;
84 if (!m_fileName.isEmpty()) {
85 refs.append(t: Reference(m_fileName, m_lineNumber));
86 refs += m_extraRefs;
87 }
88 return refs;
89}
90
91
92bool TranslatorMessage::hasExtra(const QString &key) const
93{
94 return m_extra.contains(key);
95}
96
97QString TranslatorMessage::extra(const QString &key) const
98{
99 return m_extra[key];
100}
101
102void TranslatorMessage::setExtra(const QString &key, const QString &value)
103{
104 m_extra[key] = value;
105}
106
107void TranslatorMessage::unsetExtra(const QString &key)
108{
109 m_extra.remove(key);
110}
111
112void TranslatorMessage::dump() const
113{
114 qDebug()
115 << "\nId : " << m_id
116 << "\nContext : " << m_context
117 << "\nSource : " << m_sourcetext
118 << "\nComment : " << m_comment
119 << "\nUserData : " << m_userData
120 << "\nExtraComment : " << m_extraComment
121 << "\nTranslatorComment : " << m_translatorComment
122 << "\nTranslations : " << m_translations
123 << "\nFileName : " << m_fileName
124 << "\nLineNumber : " << m_lineNumber
125 << "\nType : " << m_type
126 << "\nPlural : " << m_plural
127 << "\nExtra : " << m_extra;
128}
129
130
131QT_END_NAMESPACE
132

source code of qttools/src/linguist/shared/translatormessage.cpp