File indexing completed on 2024-04-28 05:19:26

0001 /*
0002     ktnefpropertyset.cpp
0003 
0004     SPDX-FileCopyrightText: 2002 Michael Goffioul <kdeprint@swing.be>
0005 
0006     This file is part of KTNEF, the KDE TNEF support library/program.
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 /**
0011  * @file
0012  * This file is part of the API for handling TNEF data and
0013  * defines the KTNEFPropertySet class.
0014  *
0015  * @author Michael Goffioul
0016  */
0017 
0018 #include "ktnefpropertyset.h"
0019 #include "ktnefproperty.h"
0020 
0021 #include <QList>
0022 
0023 using namespace KTnef;
0024 
0025 class KTNEFPropertySetPrivate
0026 {
0027 public:
0028     QMap<int, KTNEFProperty *> properties_; // used to store MAPI properties
0029     QMap<int, KTNEFProperty *> attributes_; // used to store TNEF attributes
0030 };
0031 
0032 KTNEFPropertySet::KTNEFPropertySet()
0033     : d(new KTNEFPropertySetPrivate)
0034 {
0035 }
0036 
0037 KTNEFPropertySet::~KTNEFPropertySet()
0038 {
0039     clear(true);
0040 }
0041 
0042 void KTNEFPropertySet::addProperty(int key, int type, const QVariant &value, const QVariant &name, bool overwrite)
0043 {
0044     QMap<int, KTNEFProperty *>::ConstIterator it = d->properties_.constFind(key);
0045     if (it != d->properties_.constEnd()) {
0046         if (overwrite) {
0047             delete (*it);
0048         } else {
0049             return;
0050         }
0051     }
0052     auto p = new KTNEFProperty(key, type, value, name);
0053     d->properties_[p->key()] = p;
0054 }
0055 
0056 QString KTNEFPropertySet::findProp(int key, const QString &fallback, bool upper) const
0057 {
0058     QMap<int, KTNEFProperty *>::Iterator it = d->properties_.find(key);
0059     if (d->properties_.end() != it) {
0060         return upper ? KTNEFProperty::formatValue((*it)->value(), false).toUpper() : KTNEFProperty::formatValue((*it)->value(), false);
0061     } else {
0062         return fallback;
0063     }
0064 }
0065 
0066 QString KTNEFPropertySet::findNamedProp(const QString &name, const QString &fallback, bool upper) const
0067 {
0068     for (QMap<int, KTNEFProperty *>::Iterator it = d->properties_.begin(), protEnd = d->properties_.end(); it != protEnd; ++it) {
0069         if ((*it)->name().isValid()) {
0070             QString s;
0071             if ((*it)->name().metaType().id() == QMetaType::QString) {
0072                 s = (*it)->name().toString();
0073             } else {
0074                 s = QString::asprintf("0X%04X", (*it)->name().toUInt());
0075             }
0076 
0077             if (s.toUpper() == name.toUpper()) {
0078                 QVariant value = (*it)->value();
0079                 if (value.metaType().id() == QMetaType::QVariantList) {
0080                     QList<QVariant> l = value.toList();
0081                     s.clear();
0082                     for (QList<QVariant>::ConstIterator lit = l.constBegin(), litEnd = l.constEnd(); lit != litEnd; ++lit) {
0083                         if (!s.isEmpty()) {
0084                             s += QLatin1Char(',');
0085                         }
0086                         s += KTNEFProperty::formatValue(*lit, false);
0087                     }
0088                 } else {
0089                     s = KTNEFProperty::formatValue(value, false);
0090                 }
0091                 return upper ? s.toUpper() : s;
0092             }
0093         }
0094     }
0095     return fallback;
0096 }
0097 
0098 QMap<int, KTNEFProperty *> &KTNEFPropertySet::properties()
0099 {
0100     return d->properties_;
0101 }
0102 
0103 const QMap<int, KTNEFProperty *> &KTNEFPropertySet::properties() const
0104 {
0105     return d->properties_;
0106 }
0107 
0108 QVariant KTNEFPropertySet::property(int key) const
0109 {
0110     QMap<int, KTNEFProperty *>::ConstIterator it = d->properties_.constFind(key);
0111     if (it == d->properties_.constEnd()) {
0112         return {};
0113     } else {
0114         return (*it)->value();
0115     }
0116 }
0117 
0118 void KTNEFPropertySet::clear(bool deleteAll)
0119 {
0120     if (deleteAll) {
0121         for (QMap<int, KTNEFProperty *>::ConstIterator it = d->properties_.constBegin(); it != d->properties_.constEnd(); ++it) {
0122             delete (*it);
0123         }
0124         for (QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constBegin(); it != d->attributes_.constEnd(); ++it) {
0125             delete (*it);
0126         }
0127     }
0128     d->properties_.clear();
0129     d->attributes_.clear();
0130 }
0131 
0132 void KTNEFPropertySet::addAttribute(int key, int type, const QVariant &value, bool overwrite)
0133 {
0134     QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constFind(key);
0135     if (it != d->attributes_.constEnd()) {
0136         if (overwrite) {
0137             delete (*it);
0138         } else {
0139             return;
0140         }
0141     }
0142     auto p = new KTNEFProperty(key, type, value, QVariant());
0143     d->attributes_[p->key()] = p;
0144 }
0145 
0146 QMap<int, KTNEFProperty *> &KTNEFPropertySet::attributes()
0147 {
0148     return d->attributes_;
0149 }
0150 
0151 const QMap<int, KTNEFProperty *> &KTNEFPropertySet::attributes() const
0152 {
0153     return d->attributes_;
0154 }
0155 
0156 QVariant KTNEFPropertySet::attribute(int key) const
0157 {
0158     QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constFind(key);
0159     if (it == d->attributes_.constEnd()) {
0160         return {};
0161     } else {
0162         return (*it)->value();
0163     }
0164 }