File indexing completed on 2024-04-14 03:52:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 #ifndef KIO_METADATA_H
0008 #define KIO_METADATA_H
0009 
0010 #include "kiocore_export.h"
0011 #include <QMap>
0012 #include <QString>
0013 #include <QVariant>
0014 
0015 namespace KIO
0016 {
0017 /**
0018  * @class KIO::MetaData metadata.h <KIO/MetaData>
0019  *
0020  * MetaData is a simple map of key/value strings.
0021  */
0022 class MetaData : public QMap<QString, QString>
0023 {
0024 public:
0025     /**
0026      * Creates an empty meta data map.
0027      */
0028     MetaData()
0029         : QMap<QString, QString>()
0030     {
0031     }
0032     /**
0033      * Copy constructor.
0034      */
0035     MetaData(const QMap<QString, QString> &metaData)
0036         : QMap<QString, QString>(metaData)
0037     {
0038     }
0039 
0040     /**
0041      * Creates a meta data map from a QVaraint map.
0042      * @since 4.3.1
0043      */
0044     MetaData(const QMap<QString, QVariant> &);
0045 
0046     /**
0047      * Adds the given meta data map to this map.
0048      * @param metaData the map to add
0049      * @return this map
0050      */
0051     MetaData &operator+=(const QMap<QString, QString> &metaData)
0052     {
0053         QMap<QString, QString>::ConstIterator it;
0054         for (it = metaData.constBegin(); it != metaData.constEnd(); ++it) {
0055             insert(it.key(), it.value());
0056         }
0057         return *this;
0058     }
0059 
0060     /**
0061      * Same as above except the value in the map is a QVariant.
0062      *
0063      * This convenience function allows you to easily assign the values
0064      * of a QVariant to this meta data class.
0065      *
0066      * @param metaData the map to add
0067      * @return this map
0068      * @since 4.3.1
0069      */
0070     MetaData &operator+=(const QMap<QString, QVariant> &metaData);
0071 
0072     /**
0073      * Sets the given meta data map to this map.
0074      * @param metaData the map to add
0075      * @return this map
0076      * @since 4.3.1
0077      */
0078     MetaData &operator=(const QMap<QString, QVariant> &metaData);
0079 
0080     /**
0081      * Returns the contents of the map as a QVariant.
0082      *
0083      * @return a QVariant representation of the meta data map.
0084      * @since 4.3.1
0085      */
0086     QVariant toVariant() const;
0087 };
0088 
0089 inline KIO::MetaData::MetaData(const QMap<QString, QVariant> &map)
0090 {
0091     *this = map;
0092 }
0093 
0094 inline KIO::MetaData &KIO::MetaData::operator+=(const QMap<QString, QVariant> &metaData)
0095 {
0096     QMapIterator<QString, QVariant> it(metaData);
0097 
0098     while (it.hasNext()) {
0099         it.next();
0100         insert(it.key(), it.value().toString());
0101     }
0102 
0103     return *this;
0104 }
0105 
0106 inline KIO::MetaData &KIO::MetaData::operator=(const QMap<QString, QVariant> &metaData)
0107 {
0108     clear();
0109     return (*this += metaData);
0110 }
0111 
0112 inline QVariant KIO::MetaData::toVariant() const
0113 {
0114     QMap<QString, QVariant> map;
0115     QMapIterator<QString, QString> it(*this);
0116 
0117     while (it.hasNext()) {
0118         it.next();
0119         map.insert(it.key(), it.value());
0120     }
0121 
0122     return QVariant(map);
0123 }
0124 
0125 } // namespace KIO
0126 
0127 #endif /* KIO_METADATA_H */