1 | // Copyright (C) 2018 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 "collectionconfigreader.h" |
5 | |
6 | #include <QtGui/QGuiApplication> |
7 | |
8 | class QCG { |
9 | Q_DECLARE_TR_FUNCTIONS(QCollectionGenerator) |
10 | }; |
11 | |
12 | void CollectionConfigReader::raiseErrorWithLine() |
13 | { |
14 | raiseError(message: QCG::tr(sourceText: "Unknown token at line %1." ).arg(a: lineNumber())); |
15 | } |
16 | |
17 | void CollectionConfigReader::readData(const QByteArray &contents) |
18 | { |
19 | m_enableFilterFunctionality = true; |
20 | m_hideFilterFunctionality = true; |
21 | m_enableAddressBar = true; |
22 | m_hideAddressBar = true; |
23 | m_enableDocumentationManager = true; |
24 | m_enableFullTextSearchFallback = false; |
25 | |
26 | addData(data: contents); |
27 | while (!atEnd()) { |
28 | readNext(); |
29 | if (isStartElement()) { |
30 | if (name() == QLatin1String("QHelpCollectionProject" ) |
31 | && attributes().value(qualifiedName: QLatin1String("version" )) == QLatin1String("1.0" )) |
32 | readConfig(); |
33 | else |
34 | raiseError(message: QCG::tr(sourceText: "Unknown token at line %1. " |
35 | "Expected \"QtHelpCollectionProject\"." ) |
36 | .arg(a: lineNumber())); |
37 | } |
38 | } |
39 | } |
40 | |
41 | void CollectionConfigReader::readConfig() |
42 | { |
43 | bool ok = false; |
44 | while (!atEnd()) { |
45 | readNext(); |
46 | if (isStartElement()) { |
47 | if (name() == QLatin1String("assistant" )) |
48 | readAssistantSettings(); |
49 | else if (name() == QLatin1String("docFiles" )) |
50 | readDocFiles(); |
51 | else |
52 | raiseErrorWithLine(); |
53 | } else if (isEndElement() && name() == QLatin1String("QHelpCollectionProject" )) { |
54 | ok = true; |
55 | } |
56 | } |
57 | if (!ok && !hasError()) |
58 | raiseError(message: QCG::tr(sourceText: "Missing end tags." )); |
59 | } |
60 | |
61 | void CollectionConfigReader::readAssistantSettings() |
62 | { |
63 | while (!atEnd()) { |
64 | readNext(); |
65 | if (isStartElement()) { |
66 | if (name() == QLatin1String("title" )) { |
67 | m_title = readElementText(); |
68 | } else if (name() == QLatin1String("homePage" )) { |
69 | m_homePage = readElementText(); |
70 | } else if (name() == QLatin1String("startPage" )) { |
71 | m_startPage = readElementText(); |
72 | } else if (name() == QLatin1String("currentFilter" )) { |
73 | m_currentFilter = readElementText(); |
74 | } else if (name() == QLatin1String("applicationIcon" )) { |
75 | m_applicationIcon = readElementText(); |
76 | } else if (name() == QLatin1String("enableFilterFunctionality" )) { |
77 | if (attributes().value(qualifiedName: QLatin1String("visible" )) == QLatin1String("true" )) |
78 | m_hideFilterFunctionality = false; |
79 | if (readElementText() == QLatin1String("false" )) |
80 | m_enableFilterFunctionality = false; |
81 | } else if (name() == QLatin1String("enableDocumentationManager" )) { |
82 | if (readElementText() == QLatin1String("false" )) |
83 | m_enableDocumentationManager = false; |
84 | } else if (name() == QLatin1String("enableAddressBar" )) { |
85 | if (attributes().value(qualifiedName: QLatin1String("visible" )) == QLatin1String("true" )) |
86 | m_hideAddressBar = false; |
87 | if (readElementText() == QLatin1String("false" )) |
88 | m_enableAddressBar = false; |
89 | } else if (name() == QLatin1String("aboutMenuText" )) { |
90 | readMenuTexts(); |
91 | } else if (name() == QLatin1String("aboutDialog" )) { |
92 | readAboutDialog(); |
93 | } else if (name() == u"cacheDirectory" ) { |
94 | m_cacheDirRelativeToCollection = |
95 | attributes().value(qualifiedName: QLatin1String("base" )) |
96 | == QLatin1String("collection" ); |
97 | m_cacheDirectory = readElementText(); |
98 | } else if (name() == QLatin1String("enableFullTextSearchFallback" )) { |
99 | if (readElementText() == QLatin1String("true" )) |
100 | m_enableFullTextSearchFallback = true; |
101 | } else { |
102 | raiseErrorWithLine(); |
103 | } |
104 | } else if (isEndElement() && name() == QLatin1String("assistant" )) { |
105 | break; |
106 | } |
107 | } |
108 | } |
109 | |
110 | void CollectionConfigReader::() |
111 | { |
112 | while (!atEnd()) { |
113 | readNext(); |
114 | if (isStartElement()) { |
115 | if (name() == QLatin1String("text" )) { |
116 | QString lang = attributes().value(qualifiedName: QLatin1String("language" )).toString(); |
117 | if (lang.isEmpty()) |
118 | lang = QLatin1String("default" ); |
119 | m_aboutMenuTexts.insert(key: lang, value: readElementText()); |
120 | } else { |
121 | raiseErrorWithLine(); |
122 | } |
123 | } else if (isEndElement() && name() == QLatin1String("aboutMenuText" )) { |
124 | break; |
125 | } |
126 | } |
127 | } |
128 | |
129 | void CollectionConfigReader::readAboutDialog() |
130 | { |
131 | while (!atEnd()) { |
132 | readNext(); |
133 | if (isStartElement()) { |
134 | if (name() == QLatin1String("file" )) { |
135 | QString lang = attributes().value(qualifiedName: QLatin1String("language" )).toString(); |
136 | if (lang.isEmpty()) |
137 | lang = QLatin1String("default" ); |
138 | m_aboutTextFiles.insert(key: lang, value: readElementText()); |
139 | } else if (name() == QLatin1String("icon" )) { |
140 | m_aboutIcon = readElementText(); |
141 | } else { |
142 | raiseErrorWithLine(); |
143 | } |
144 | } else if (isEndElement() && name() == QLatin1String("aboutDialog" )) { |
145 | break; |
146 | } |
147 | } |
148 | } |
149 | |
150 | void CollectionConfigReader::readDocFiles() |
151 | { |
152 | while (!atEnd()) { |
153 | readNext(); |
154 | if (isStartElement()) { |
155 | if (name() == QLatin1String("generate" )) { |
156 | readGenerate(); |
157 | } else if (name() == QLatin1String("register" )) { |
158 | readRegister(); |
159 | } else { |
160 | raiseErrorWithLine(); |
161 | } |
162 | } else if (isEndElement() && name() == QLatin1String("docFiles" )) { |
163 | break; |
164 | } |
165 | } |
166 | } |
167 | |
168 | void CollectionConfigReader::readGenerate() |
169 | { |
170 | while (!atEnd()) { |
171 | readNext(); |
172 | if (isStartElement()) { |
173 | if (name() == QLatin1String("file" )) |
174 | readFiles(); |
175 | else |
176 | raiseErrorWithLine(); |
177 | } else if (isEndElement() && name() == QLatin1String("generate" )) { |
178 | break; |
179 | } |
180 | } |
181 | } |
182 | |
183 | void CollectionConfigReader::readFiles() |
184 | { |
185 | QString input; |
186 | QString output; |
187 | while (!atEnd()) { |
188 | readNext(); |
189 | if (isStartElement()) { |
190 | if (name() == QLatin1String("input" )) |
191 | input = readElementText(); |
192 | else if (name() == QLatin1String("output" )) |
193 | output = readElementText(); |
194 | else |
195 | raiseErrorWithLine(); |
196 | } else if (isEndElement() && name() == QLatin1String("file" )) { |
197 | break; |
198 | } |
199 | } |
200 | if (input.isEmpty() || output.isEmpty()) { |
201 | raiseError(message: QCG::tr(sourceText: "Missing input or output file for help file generation." )); |
202 | return; |
203 | } |
204 | m_filesToGenerate.insert(key: input, value: output); |
205 | } |
206 | |
207 | void CollectionConfigReader::readRegister() |
208 | { |
209 | while (!atEnd()) { |
210 | readNext(); |
211 | if (isStartElement()) { |
212 | if (name() == QLatin1String("file" )) |
213 | m_filesToRegister.append(t: readElementText()); |
214 | else |
215 | raiseErrorWithLine(); |
216 | } else if (isEndElement() && name() == QLatin1String("register" )) { |
217 | break; |
218 | } |
219 | } |
220 | } |
221 | |
222 | |
223 | |