1 /*****************************************************************
2 * Copyright (C) 2009  Pierre Marchand
3 
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 *
17 ******************************************************************/
18 
19 #include "collection.h"
20 
21 
22 #include <QFile>
23 
24 
imageCollection()25 imageCollection::imageCollection()
26 {
27 }
28 
29 
collections(const QString & collectionsName)30 collections::collections ( const QString& collectionsName )
31 {
32 	name = collectionsName;
33 }
34 
35 
collectionReaderThread(QString & xmlFile2,bool importCollection)36 collectionReaderThread::collectionReaderThread ( QString &xmlFile2, bool importCollection )
37 {
38 	categoriesCount = 0;
39 	collection = nullptr;
40 	type = 0;
41 	restartThread = false;
42 
43 	xmlFile = xmlFile2;
44 	import = importCollection;
45 }
46 
47 
readFile()48 void collectionReaderThread::readFile()
49 {
50 	QFile inputFile ( xmlFile );
51 
52 	if ( !inputFile.open ( QFile::ReadOnly | QFile::Text ) )
53 	{
54 		return;
55 	}
56 
57 	setDevice ( &inputFile );
58 
59 	while ( !atEnd() )
60 	{
61 		readNext();
62 
63 		if ( isStartElement() )
64 		{
65 			if ( name() == "picturebrowser" )
66 			{
67 				//we have a collectionsdbfile
68 				if ( attributes().value ( "type" ) == "collectionsset" )
69 				{
70 					readCollectionsDb();
71 					type = 1;
72 				}
73 				//we have a collectionfile
74 				else if ( attributes().value ( "type" ) == "collection" )
75 				{
76 					collection = new imageCollection;
77 					collection->file = xmlFile;
78 
79 					QString name = attributes().value ( "name" ).toString();
80 
81 					if ( !name.isEmpty() )
82 					{
83 						collection->name = name;
84 					}
85 					else
86 					{
87 						collection->name = xmlFile;
88 					}
89 
90 					readCollectionFile();
91 					type = 2;
92 				}
93 			}
94 		}
95 	}
96 }
97 
98 
readCollectionsDb()99 void collectionReaderThread::readCollectionsDb()
100 {
101 	while ( !atEnd() )
102 	{
103 		readNext();
104 
105 		if ( isEndElement() )
106 		{
107 			break;
108 		}
109 
110 		if ( isStartElement() )
111 		{
112 			if ( name() == "category" )
113 			{
114 				QString name = attributes().value ( "name" ).toString();
115 				collections *tmpCollections = new collections ( name );
116 				collectionsSet.append ( tmpCollections );
117 
118 				readCategory();
119 
120 				categoriesCount++;
121 			}
122 			else
123 			{
124 				readUnknownElement();
125 			}
126 		}
127 	}
128 }
129 
130 
readCollectionFile()131 void collectionReaderThread::readCollectionFile()
132 {
133 	while ( !atEnd() && !restartThread )
134 	{
135 		readNext();
136 
137 		if ( isEndElement() )
138 		{
139 			break;
140 		}
141 
142 		if ( isStartElement() )
143 		{
144 			if ( name() == "image" )
145 			{
146 				QString tmpImageFile = attributes().value ( "file" ).toString();
147 				collection->imageFiles.append ( tmpImageFile );
148 
149 				readImage();
150 			}
151 			else
152 			{
153 				readUnknownElement();
154 			}
155 		}
156 	}
157 }
158 
159 
readCategory()160 void collectionReaderThread::readCategory()
161 {
162 	while ( !atEnd() )
163 	{
164 		readNext();
165 
166 		if ( isEndElement() )
167 		{
168 			break;
169 		}
170 
171 		if ( isStartElement() )
172 		{
173 			if ( name() == "collection" )
174 			{
175 				readCollection();
176 			}
177 			else
178 			{
179 				readUnknownElement();
180 			}
181 		}
182 	}
183 }
184 
185 
readCollection()186 void collectionReaderThread::readCollection()
187 {
188 	collections *tmpCollections = collectionsSet.at ( categoriesCount );
189 
190 	tmpCollections->collectionFiles.append ( attributes().value ( "file" ).toString() );
191 	tmpCollections->collectionNames.append ( readElementText() );
192 }
193 
194 
readImage()195 void collectionReaderThread::readImage()
196 {
197 	QStringList tmpTags;
198 
199 	while ( !atEnd() && !restartThread )
200 	{
201 		readNext();
202 
203 		if ( isEndElement() )
204 		{
205 			break;
206 		}
207 
208 		if ( isStartElement() )
209 		{
210 			if ( name() == "tag" )
211 			{
212 				//read tag here
213 				tmpTags.append ( readElementText() );
214 			}
215 			else
216 			{
217 				readUnknownElement();
218 			}
219 		}
220 	}
221 
222 	collection->tags.append ( tmpTags );
223 }
224 
225 
readUnknownElement()226 void collectionReaderThread::readUnknownElement()
227 {
228 	while ( !atEnd() )
229 	{
230 		readNext();
231 
232 		if ( isEndElement() )
233 		{
234 			break;
235 		}
236 
237 		if ( isStartElement() )
238 		{
239 			readUnknownElement();
240 		}
241 	}
242 }
243 
244 
run()245 void collectionReaderThread::run()
246 {
247 	readFile();
248 }
249 
250 
restart()251 void collectionReaderThread::restart()
252 {
253 	restartThread = true;
254 }
255 
256 
collectionListReaderThread(QStringList & xmlFiles2)257 collectionListReaderThread::collectionListReaderThread ( QStringList &xmlFiles2 )
258 {
259 	m_clrt = nullptr;
260 	restartThread = false;
261 
262 	xmlFiles = xmlFiles2;
263 }
264 
265 
run()266 void collectionListReaderThread::run()
267 {
268 	if ( xmlFiles.isEmpty() )
269 	{
270 		return;
271 	}
272 
273 	xmlFile = xmlFiles.takeAt ( 0 );
274 	m_clrt = new collectionReaderThread ( xmlFile, false );
275 	connect ( m_clrt, SIGNAL ( finished() ), this, SLOT ( collectionReaderThreadFinished() ) );
276 	m_clrt->start();
277 
278 	exec();
279 }
280 
281 
restart()282 void collectionListReaderThread::restart()
283 {
284 	restartThread = true;
285 }
286 
287 
collectionReaderThreadFinished()288 void collectionListReaderThread::collectionReaderThreadFinished()
289 {
290 	readCollections.append ( m_clrt->collection );
291 	delete m_clrt;
292 
293 	if ( xmlFiles.isEmpty() || restartThread )
294 	{
295 		quit();
296 	}
297 	else
298 	{
299 		xmlFile = xmlFiles.takeAt ( 0 );
300 		m_clrt = new collectionReaderThread ( xmlFile, false );
301 		connect ( m_clrt, SIGNAL ( finished() ), this, SLOT ( collectionReaderThreadFinished() ) );
302 		m_clrt->start();
303 	}
304 }
305 
306 
collectionsWriterThread(const QString & xmlFile2,QList<collections * > saveCollections2)307 collectionsWriterThread::collectionsWriterThread ( const QString& xmlFile2, QList<collections *> saveCollections2 )
308 {
309 	xmlFile = xmlFile2;
310 	saveCollections = saveCollections2;
311 	restartThread = false;
312 }
313 
314 
writeFile()315 void collectionsWriterThread::writeFile()
316 {
317 	QFile outputFile ( xmlFile );
318 
319 	if ( !outputFile.open ( QFile::WriteOnly | QFile::Text ) )
320 	{
321 		return;
322 	}
323 
324 	setDevice ( &outputFile );
325 
326 	writeStartDocument();
327 	writeCharacters ( "\n" );
328 //writeDTD("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
329 	writeStartElement ( "picturebrowser" );
330 
331 	writeAttribute ( "type", "collectionsset" );
332 
333 	writeCharacters ( "\n" );
334 
335 	for ( int i = 0 ; i < saveCollections.size() && !restartThread ; ++i )
336 	{
337 		writeCategory ( saveCollections.at ( i ) );
338 	}
339 
340 	writeEndDocument();
341 }
342 
343 
writeCategory(const collections * category)344 void collectionsWriterThread::writeCategory ( const collections *category )
345 {
346 	writeStartElement ( "category" );
347 	writeAttribute ( "name", category->name );
348 	writeCharacters ( "\n" );
349 
350 	for ( int i = 0 ; i < category->collectionNames.size() && !restartThread ; ++i )
351 	{
352 		writeCollection ( category->collectionNames.at ( i ), category->collectionFiles.at ( i ) );
353 	}
354 
355 	writeEndElement();
356 	writeCharacters ( "\n" );
357 }
358 
359 
writeCollection(const QString & collectionName,const QString & collectionFile)360 void collectionsWriterThread::writeCollection ( const QString &collectionName, const QString &collectionFile )
361 {
362 	writeStartElement ( "collection" );
363 	writeAttribute ( "file", collectionFile );
364 
365 	writeCharacters ( collectionName );
366 
367 	writeEndElement();
368 	writeCharacters ( "\n" );
369 }
370 
371 
run()372 void collectionsWriterThread::run()
373 {
374 	writeFile();
375 }
376 
377 
restart()378 void collectionsWriterThread::restart()
379 {
380 	restartThread = true;
381 }
382 
383 
384 
collectionWriterThread(QString & xmlFile2,imageCollection & saveCollection2)385 collectionWriterThread::collectionWriterThread ( QString &xmlFile2, imageCollection &saveCollection2 )
386 {
387 	xmlFile = xmlFile2;
388 	saveCollection = saveCollection2;
389 }
390 
391 
writeFile()392 void collectionWriterThread::writeFile()
393 {
394 	QFile outputFile ( xmlFile );
395 
396 	if ( !outputFile.open ( QFile::WriteOnly | QFile::Text ) )
397 	{
398 		return;
399 	}
400 
401 	setDevice ( &outputFile );
402 
403 	writeStartDocument();
404 	writeCharacters ( "\n" );
405 //writeDTD("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
406 	writeStartElement ( "picturebrowser" );
407 
408 	writeAttribute ( "type", "collection" );
409 
410 	if ( !saveCollection.name.isEmpty() )
411 	{
412 		writeAttribute ( "name", saveCollection.name );
413 	}
414 
415 	writeCharacters ( "\n" );
416 
417 	for ( int i = 0 ; i < saveCollection.imageFiles.size() ; ++i )
418 	{
419 		writeImage ( saveCollection.imageFiles.at ( i ), saveCollection.tags.at ( i ) );
420 	}
421 
422 	writeEndDocument();
423 }
424 
425 
writeImage(const QString & imageFile,const QStringList & tags)426 void collectionWriterThread::writeImage ( const QString &imageFile, const QStringList &tags )
427 {
428 	writeStartElement ( "image" );
429 	writeAttribute ( "file", imageFile );
430 	writeCharacters ( "\n" );
431 
432 	writeTags ( tags );
433 
434 	writeEndElement();
435 	writeCharacters ( "\n" );
436 }
437 
438 
writeTags(const QStringList & tags)439 void collectionWriterThread::writeTags ( const QStringList &tags )
440 {
441 	for ( int i = 0 ; i < tags.size() ; ++i )
442 	{
443 		writeStartElement ( "tag" );
444 
445 		writeCharacters ( tags.at ( i ) );
446 
447 		writeEndElement();
448 		writeCharacters ( "\n" );
449 	}
450 }
451 
452 
run()453 void collectionWriterThread::run()
454 {
455 	writeFile();
456 }
457 
458 
459 
460