File indexing completed on 2025-01-05 03:52:04

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-04-04
0007  * Description : a tool to generate HTML image galleries
0008  *
0009  * SPDX-FileCopyrightText: 2006-2010 by Aurelien Gateau <aurelien dot gateau at free dot fr>
0010  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_GALLERY_XML_UTILS_H
0017 #define DIGIKAM_GALLERY_XML_UTILS_H
0018 
0019 // Qt includes
0020 
0021 #include <QString>
0022 #include <QMap>
0023 
0024 // libxml includes
0025 
0026 #include <libxml/xmlwriter.h>
0027 
0028 namespace DigikamGenericHtmlGalleryPlugin
0029 {
0030 
0031 /**
0032  * A simple wrapper for a C structure pointed to by @p Ptr, which must be freed
0033  * with @p freeFcn
0034  */
0035 template <class Ptr, void(*freeFcn)(Ptr)>
0036 
0037 class CWrapper
0038 {
0039 public:
0040 
0041     CWrapper()
0042         : m_ptr(nullptr)
0043     {
0044     }
0045 
0046     // cppcheck-suppress noExplicitConstructor
0047     CWrapper(Ptr ptr)        // krazy:exclude=explicit
0048         : m_ptr(ptr)
0049     {
0050     }
0051 
0052     ~CWrapper()
0053     {
0054         freeFcn(m_ptr);
0055     }
0056 
0057     operator Ptr() const
0058     {
0059         return m_ptr;
0060     }
0061 
0062     bool operator!() const
0063     {
0064         return !m_ptr;
0065     }
0066 
0067     void assign(Ptr ptr)
0068     {
0069         if (m_ptr)
0070         {
0071             freeFcn(m_ptr);
0072         }
0073 
0074         m_ptr = ptr;
0075     }
0076 
0077 private:
0078 
0079     Ptr m_ptr;
0080 };
0081 
0082 // --------------------------------------------------------------------------
0083 
0084 /**
0085  * Simple wrapper around xmlTextWriter
0086  */
0087 class XMLWriter
0088 {
0089 public:
0090 
0091     bool open(const QString& name);
0092     operator xmlTextWriterPtr() const;
0093 
0094     void writeElement(const char* element, const QString& value);
0095     void writeElement(const char* element, int value);
0096 
0097 private:
0098 
0099     CWrapper<xmlTextWriterPtr, xmlFreeTextWriter> m_writer;
0100 };
0101 
0102 
0103 // --------------------------------------------------------------------------
0104 /**
0105  * A list of attributes for an XML element. To be used with @ref XMLElement
0106  */
0107 class XMLAttributeList
0108 {
0109 public:
0110 
0111     void write(XMLWriter& writer) const;
0112     void append(const QString& key, const QString& value);
0113     void append(const QString& key, int value);
0114 
0115 private:
0116 
0117     typedef QMap<QString, QString> Map;
0118 
0119     Map m_map;
0120 };
0121 
0122 // --------------------------------------------------------------------------
0123 
0124 /**
0125  * A class to generate an XML element
0126  */
0127 class XMLElement
0128 {
0129 public:
0130 
0131     explicit XMLElement(XMLWriter& writer,
0132                         const QString& element,
0133                         const XMLAttributeList* attributeList = nullptr);
0134     ~XMLElement();
0135 
0136 private:
0137 
0138     XMLWriter& m_writer;
0139 };
0140 
0141 } // namespace DigikamGenericHtmlGalleryPlugin
0142 
0143 #endif // DIGIKAM_GALLERY_XML_UTILS_H