Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | #include "qtextdocumentwriter.h" | - |
40 | | - |
41 | #include <QtCore/qfile.h> | - |
42 | #include <QtCore/qbytearray.h> | - |
43 | #include <QtCore/qfileinfo.h> | - |
44 | #include <QtCore/qtextcodec.h> | - |
45 | #include <QtCore/qtextstream.h> | - |
46 | #include <QtCore/qdebug.h> | - |
47 | #include "qtextdocument.h" | - |
48 | #include "qtextdocumentfragment.h" | - |
49 | | - |
50 | #include "qtextdocumentfragment_p.h" | - |
51 | #include "qtextodfwriter_p.h" | - |
52 | | - |
53 | #include <algorithm> | - |
54 | | - |
55 | QT_BEGIN_NAMESPACE | - |
56 | | - |
57 | class QTextDocumentWriterPrivate | - |
58 | { | - |
59 | public: | - |
60 | QTextDocumentWriterPrivate(QTextDocumentWriter* qq); | - |
61 | | - |
62 | | - |
63 | QByteArray format; | - |
64 | QIODevice *device; | - |
65 | bool deleteDevice; | - |
66 | #ifndef QT_NO_TEXTCODEC | - |
67 | QTextCodec *codec; | - |
68 | #endif | - |
69 | | - |
70 | QTextDocumentWriter *q; | - |
71 | }; | - |
72 | | - |
73 | | - |
74 | | - |
75 | | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | | - |
89 | | - |
90 | | - |
91 | | - |
92 | | - |
93 | | - |
94 | | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | | - |
103 | | - |
104 | QTextDocumentWriterPrivate::QTextDocumentWriterPrivate(QTextDocumentWriter *qq) | - |
105 | : device(0), | - |
106 | deleteDevice(false), | - |
107 | #ifndef QT_NO_TEXTCODEC | - |
108 | codec(QTextCodec::codecForName("utf-8")), | - |
109 | #endif | - |
110 | q(qq) | - |
111 | { | - |
112 | } | - |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
117 | | - |
118 | | - |
119 | QTextDocumentWriter::QTextDocumentWriter() | - |
120 | : d(new QTextDocumentWriterPrivate(this)) | - |
121 | { | - |
122 | } | - |
123 | | - |
124 | | - |
125 | | - |
126 | | - |
127 | | - |
128 | QTextDocumentWriter::QTextDocumentWriter(QIODevice *device, const QByteArray &format) | - |
129 | : d(new QTextDocumentWriterPrivate(this)) | - |
130 | { | - |
131 | d->device = device; | - |
132 | d->format = format; | - |
133 | } | - |
134 | | - |
135 | | - |
136 | | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | QTextDocumentWriter::QTextDocumentWriter(const QString &fileName, const QByteArray &format) | - |
142 | : d(new QTextDocumentWriterPrivate(this)) | - |
143 | { | - |
144 | QFile *file = new QFile(fileName); | - |
145 | d->device = file; | - |
146 | d->deleteDevice = true; | - |
147 | d->format = format; | - |
148 | } | - |
149 | | - |
150 | | - |
151 | | - |
152 | | - |
153 | QTextDocumentWriter::~QTextDocumentWriter() | - |
154 | { | - |
155 | if (d->deleteDevice) | - |
156 | delete d->device; | - |
157 | delete d; | - |
158 | } | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | | - |
165 | | - |
166 | | - |
167 | | - |
168 | | - |
169 | | - |
170 | | - |
171 | void QTextDocumentWriter::setFormat (const QByteArray &format) | - |
172 | { | - |
173 | d->format = format; | - |
174 | } | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | | - |
181 | QByteArray QTextDocumentWriter::format () const | - |
182 | { | - |
183 | return d->format; | - |
184 | } | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | | - |
190 | | - |
191 | | - |
192 | | - |
193 | | - |
194 | | - |
195 | | - |
196 | | - |
197 | | - |
198 | | - |
199 | | - |
200 | void QTextDocumentWriter::setDevice (QIODevice *device) | - |
201 | { | - |
202 | if (d->device && d->deleteDevice) | - |
203 | delete d->device; | - |
204 | | - |
205 | d->device = device; | - |
206 | d->deleteDevice = false; | - |
207 | } | - |
208 | | - |
209 | | - |
210 | | - |
211 | | - |
212 | | - |
213 | QIODevice *QTextDocumentWriter::device () const | - |
214 | { | - |
215 | return d->device; | - |
216 | } | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | | - |
225 | void QTextDocumentWriter::setFileName (const QString &fileName) | - |
226 | { | - |
227 | setDevice(new QFile(fileName)); | - |
228 | d->deleteDevice = true; | - |
229 | } | - |
230 | | - |
231 | | - |
232 | | - |
233 | | - |
234 | | - |
235 | | - |
236 | | - |
237 | | - |
238 | QString QTextDocumentWriter::fileName () const | - |
239 | { | - |
240 | QFile *file = qobject_cast<QFile *>(d->device); | - |
241 | return file ? file->fileName() : QString(); | - |
242 | } | - |
243 | | - |
244 | | - |
245 | | - |
246 | | - |
247 | | - |
248 | bool QTextDocumentWriter::write(const QTextDocument *document) | - |
249 | { | - |
250 | QByteArray suffix; | - |
251 | | - |
252 | if (d->device && d->format.isEmpty()) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
253 | | - |
254 | | - |
255 | if (QFile *file = qobject_cast<QFile *>(d->device))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
256 | suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1(); never executed: suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1(); | 0 |
257 | } never executed: end of block | 0 |
258 | | - |
259 | QByteArray format = !d->format.isEmpty() ? d->format.toLower() : suffix;TRUE | never evaluated | FALSE | never evaluated |
| 0 |
260 | | - |
261 | #ifndef QT_NO_TEXTODFWRITER | - |
262 | if (format == "odf" || format == "opendocumentformat" || format == "odt") {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
263 | QTextOdfWriter writer(*document, d->device); | - |
264 | #ifndef QT_NO_TEXTCODEC | - |
265 | writer.setCodec(d->codec); | - |
266 | #endif | - |
267 | return writer.writeAll(); never executed: return writer.writeAll(); | 0 |
268 | } | - |
269 | #endif // QT_NO_TEXTODFWRITER | - |
270 | | - |
271 | #ifndef QT_NO_TEXTHTMLPARSER | - |
272 | if (format == "html" || format == "htm") {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
273 | if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
274 | qWarning() << ("QTextDocumentWriter::write: the device can not be opened for writing";); | - |
275 | return false; never executed: return false; | 0 |
276 | } | - |
277 | QTextStream ts(d->device); | - |
278 | #ifndef QT_NO_TEXTCODEC | - |
279 | ts.setCodec(d->codec); | - |
280 | ts << document->toHtml(d->codec->name()); | - |
281 | #endif | - |
282 | d->device->close(); | - |
283 | return true; never executed: return true; | 0 |
284 | } | - |
285 | #endif | - |
286 | if (format == "txt" || format == "plaintext") {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
287 | if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) {TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
288 | qWarning() << ("QTextDocumentWriter::write: the device can not be opened for writing";); | - |
289 | return false; never executed: return false; | 0 |
290 | } | - |
291 | QTextStream ts(d->device); | - |
292 | #ifndef QT_NO_TEXTCODEC | - |
293 | ts.setCodec(d->codec); | - |
294 | #endif | - |
295 | ts << document->toPlainText(); | - |
296 | d->device->close(); | - |
297 | return true; never executed: return true; | 0 |
298 | } | - |
299 | | - |
300 | return false; never executed: return false; | 0 |
301 | } | - |
302 | | - |
303 | | - |
304 | | - |
305 | | - |
306 | | - |
307 | bool QTextDocumentWriter::write(const QTextDocumentFragment &fragment) | - |
308 | { | - |
309 | if (fragment.d == 0) | - |
310 | return false; | - |
311 | QTextDocument *doc = fragment.d->doc; | - |
312 | if (doc) | - |
313 | return write(doc); | - |
314 | return false; | - |
315 | } | - |
316 | | - |
317 | | - |
318 | | - |
319 | | - |
320 | | - |
321 | | - |
322 | | - |
323 | #ifndef QT_NO_TEXTCODEC | - |
324 | void QTextDocumentWriter::setCodec(QTextCodec *codec) | - |
325 | { | - |
326 | if (codec == 0) | - |
327 | codec = QTextCodec::codecForName("UTF-8"); | - |
328 | Q_ASSERT(codec); | - |
329 | d->codec = codec; | - |
330 | } | - |
331 | #endif | - |
332 | | - |
333 | | - |
334 | | - |
335 | | - |
336 | #ifndef QT_NO_TEXTCODEC | - |
337 | QTextCodec *QTextDocumentWriter::codec() const | - |
338 | { | - |
339 | return d->codec; | - |
340 | } | - |
341 | #endif | - |
342 | | - |
343 | | - |
344 | | - |
345 | | - |
346 | | - |
347 | | - |
348 | | - |
349 | | - |
350 | | - |
351 | | - |
352 | | - |
353 | | - |
354 | | - |
355 | | - |
356 | | - |
357 | QList<QByteArray> QTextDocumentWriter::supportedDocumentFormats() | - |
358 | { | - |
359 | QList<QByteArray> answer; | - |
360 | answer << "plaintext"; | - |
361 | | - |
362 | #ifndef QT_NO_TEXTHTMLPARSER | - |
363 | answer << "HTML"; | - |
364 | #endif | - |
365 | #ifndef QT_NO_TEXTODFWRITER | - |
366 | answer << "ODF"; | - |
367 | #endif // QT_NO_TEXTODFWRITER | - |
368 | | - |
369 | std::sort(answer.begin(), answer.end()); | - |
370 | return answer; | - |
371 | } | - |
372 | | - |
373 | QT_END_NAMESPACE | - |
| | |