1 /* Copyright (C) 2003-2006 Jesper K. Pedersen <blackie@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
18 #include "XMLHandler.h"
20 #include "Utilities/Util.h"
21 #include "DB/ResultId.h"
23 using Utilities::StringSet
;
26 * \class ImportExport::XMLHandler
27 * \brief Helper class for
28 * reading and writting the index.xml file located in exported .kim file.
29 * This class is a simple helper class which encapsulate the code needed for generating an index.xml for the export file.
30 * There should never be a need to keep any instances around of this class, simply create one on the stack, and call
31 * thee method \ref createIndexXML().
33 * Notice, you will find a lot of duplicated code inhere from the XML database, there are two reasons for this
34 * (1) In the long run the XML database ought to be an optional part (users might instead use, say an SQL database)
35 * (2) To ensure that the .kim files are compatible both forth and back between versions, I'd rather keep that code
36 * separate from the normal index.xml file, which might change with KPhotoAlbum versions to e.g. support compression.
38 QByteArray
ImportExport::XMLHandler::createIndexXML(
39 const DB::Result
& images
,
40 const QString
& baseUrl
,
41 ImageFileLocation location
,
42 Utilities::UniqFilenameMapper
* nameMap
)
45 doc
.appendChild( doc
.createProcessingInstruction( QString::fromLatin1("xml"),
46 QString::fromLatin1("version=\"1.0\" encoding=\"UTF-8\"") ) );
48 QDomElement top
= doc
.createElement( QString::fromLatin1( "KimDaBa-export" ) ); // Don't change, as this will make the files unreadable for KimDaBa 2.1 and back.
49 top
.setAttribute( QString::fromLatin1( "location" ),
50 location
== Inline
? QString::fromLatin1( "inline" ) : QString::fromLatin1( "external" ) );
51 if ( !baseUrl
.isEmpty() )
52 top
.setAttribute( QString::fromLatin1( "baseurl" ), baseUrl
);
53 doc
.appendChild( top
);
56 Q_FOREACH(const DB::ImageInfoPtr info
, images
.fetchInfos()) {
57 QString mappedFile
= nameMap
->uniqNameFor(info
->fileName(DB::AbsolutePath
));
58 QDomElement elm
= save(doc
, info
);
59 elm
.setAttribute( QString::fromLatin1( "file" ), mappedFile
);
60 top
.appendChild( elm
);
62 return doc
.toByteArray();
65 QDomElement
ImportExport::XMLHandler::save( QDomDocument doc
, const DB::ImageInfoPtr
& info
)
67 QDomElement elm
= doc
.createElement( QString::fromLatin1("image") );
68 elm
.setAttribute( QString::fromLatin1("label"), info
->label() );
69 elm
.setAttribute( QString::fromLatin1("description"), info
->description() );
71 DB::ImageDate date
= info
->date();
72 QDateTime start
= date
.start();
73 QDateTime end
= date
.end();
75 elm
.setAttribute( QString::fromLatin1("yearFrom"), start
.date().year() );
76 elm
.setAttribute( QString::fromLatin1("monthFrom"), start
.date().month() );
77 elm
.setAttribute( QString::fromLatin1("dayFrom"), start
.date().day() );
78 elm
.setAttribute( QString::fromLatin1("hourFrom"), start
.time().hour() );
79 elm
.setAttribute( QString::fromLatin1("minuteFrom"), start
.time().minute() );
80 elm
.setAttribute( QString::fromLatin1("secondFrom"), start
.time().second() );
82 elm
.setAttribute( QString::fromLatin1("yearTo"), end
.date().year() );
83 elm
.setAttribute( QString::fromLatin1("monthTo"), end
.date().month() );
84 elm
.setAttribute( QString::fromLatin1("dayTo"), end
.date().day() );
86 elm
.setAttribute( QString::fromLatin1( "width" ), info
->size().width() );
87 elm
.setAttribute( QString::fromLatin1( "height" ), info
->size().height() );
88 elm
.setAttribute( QString::fromLatin1( "md5sum" ), info
->MD5Sum().toHexString() );
89 elm
.setAttribute( QString::fromLatin1( "angle" ), info
->angle() );
91 writeCategories( doc
, elm
, info
);
97 void ImportExport::XMLHandler::writeCategories( QDomDocument doc
, QDomElement root
, const DB::ImageInfoPtr
& info
)
99 QDomElement elm
= doc
.createElement( QString::fromLatin1("options") );
101 bool anyAtAll
= false;
102 QStringList grps
= info
->availableCategories();
103 for( QStringList::Iterator categoryIt
= grps
.begin(); categoryIt
!= grps
.end(); ++categoryIt
) {
104 QDomElement opt
= doc
.createElement( QString::fromLatin1("option") );
105 QString name
= *categoryIt
;
106 opt
.setAttribute( QString::fromLatin1("name"), name
);
108 StringSet items
= info
->itemsOfCategory(*categoryIt
);
110 for( StringSet::const_iterator itemIt
= items
.begin(); itemIt
!= items
.end(); ++itemIt
) {
111 QDomElement val
= doc
.createElement( QString::fromLatin1("value") );
112 val
.setAttribute( QString::fromLatin1("value"), *itemIt
);
113 opt
.appendChild( val
);
118 elm
.appendChild( opt
);
122 root
.appendChild( elm
);