KTnef

ktnefpropertyset.cpp
Go to the documentation of this file.
1 /*
2  ktnefpropertyset.cpp
3 
4  SPDX-FileCopyrightText: 2002 Michael Goffioul <[email protected]>
5 
6  This file is part of KTNEF, the KDE TNEF support library/program.
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9  */
18 #include "ktnefpropertyset.h"
19 #include "ktnefproperty.h"
20 
21 #include "ktnef_debug.h"
22 
23 #include <QList>
24 
25 using namespace KTnef;
26 
27 class KTNEFPropertySetPrivate
28 {
29 public:
30  QMap<int, KTNEFProperty *> properties_; // used to store MAPI properties
31  QMap<int, KTNEFProperty *> attributes_; // used to store TNEF attributes
32 };
33 
35  : d(new KTNEFPropertySetPrivate)
36 {
37 }
38 
40 {
41  clear(true);
42 
43  delete d;
44 }
45 
46 void KTNEFPropertySet::addProperty(int key, int type, const QVariant &value,
47  const QVariant &name, bool overwrite)
48 {
50  if (it != d->properties_.constEnd()) {
51  if (overwrite) {
52  delete(*it);
53  } else {
54  return;
55  }
56  }
57  KTNEFProperty *p = new KTNEFProperty(key, type, value, name);
58  d->properties_[ p->key() ] = p;
59 }
60 
61 QString KTNEFPropertySet::findProp(int key, const QString &fallback,
62  bool upper) const
63 {
64  QMap<int, KTNEFProperty *>::Iterator it = d->properties_.find(key);
65  if (d->properties_.end() != it) {
66  return upper ?
67  KTNEFProperty::formatValue((*it)->value(), false).toUpper() :
68  KTNEFProperty::formatValue((*it)->value(), false);
69  } else {
70  return fallback;
71  }
72 }
73 
75  const QString &fallback,
76  bool upper) const
77 {
78  for (QMap<int, KTNEFProperty *>::Iterator it = d->properties_.begin(), protEnd = d->properties_.end(); it != protEnd; ++it) {
79  if ((*it)->name().isValid()) {
80  QString s;
81  if ((*it)->name().type() == QVariant::String) {
82  s = (*it)->name().toString();
83  } else {
84  s = QString::asprintf("0X%04X", (*it)->name().toUInt());
85  }
86 
87  if (s.toUpper() == name.toUpper()) {
88  QVariant value = (*it)->value();
89  if (value.type() == QVariant::List) {
90  QList<QVariant> l = value.toList();
91  s.clear();
92  for (QList<QVariant>::ConstIterator lit = l.constBegin(), litEnd = l.constEnd(); lit != litEnd; ++lit) {
93  if (!s.isEmpty()) {
94  s += QLatin1Char(',');
95  }
96  s += KTNEFProperty::formatValue(*lit, false);
97  }
98  } else {
99  s = KTNEFProperty::formatValue(value, false);
100  }
101  return upper ? s.toUpper() : s;
102  }
103  }
104  }
105  return fallback;
106 }
107 
109 {
110  return d->properties_;
111 }
112 
114 {
115  return d->properties_;
116 }
117 
119 {
120  QMap<int, KTNEFProperty *>::ConstIterator it = d->properties_.constFind(key);
121  if (it == d->properties_.constEnd()) {
122  return QVariant();
123  } else {
124  return (*it)->value();
125  }
126 }
127 
128 void KTNEFPropertySet::clear(bool deleteAll)
129 {
130  if (deleteAll) {
131  for (QMap<int, KTNEFProperty *>::ConstIterator it = d->properties_.constBegin();
132  it != d->properties_.constEnd();
133  ++it) {
134  delete(*it);
135  }
136  for (QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constBegin();
137  it != d->attributes_.constEnd();
138  ++it) {
139  delete(*it);
140  }
141  }
142  d->properties_.clear();
143  d->attributes_.clear();
144 }
145 
146 void KTNEFPropertySet::addAttribute(int key, int type, const QVariant &value,
147  bool overwrite)
148 {
149  QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constFind(key);
150  if (it != d->attributes_.constEnd()) {
151  if (overwrite) {
152  delete(*it);
153  } else {
154  return;
155  }
156  }
157  KTNEFProperty *p = new KTNEFProperty(key, type, value, QVariant());
158  d->attributes_[ p->key() ] = p;
159 }
160 
162 {
163  return d->attributes_;
164 }
165 
167 {
168  return d->attributes_;
169 }
170 
172 {
173  QMap<int, KTNEFProperty *>::ConstIterator it = d->attributes_.constFind(key);
174  if (it == d->attributes_.constEnd()) {
175  return QVariant();
176  } else {
177  return (*it)->value();
178  }
179 }
QVariant attribute(int key) const
Returns the attribute associated with the specified key.
QVariant property(int key) const
Returns the property associated with the specified key.
QString toUpper() const const
void addProperty(int key, int type, const QVariant &value, const QVariant &name=QVariant(), bool overwrite=false)
Adds a MAPI property.
QString asprintf(const char *cformat,...)
QString findProp(int key, const QString &fallback=QString(), bool convertToUpper=false) const
Finds a property by key, returning a formatted value.
QList< QVariant > toList() const const
int key() const
Returns the integer key of the property.
QMap::const_iterator constBegin() const const
T value() const const
QMap::const_iterator constFind(const Key &key) const const
QMap< int, KTNEFProperty * > & properties()
Returns a QMap of all (key,MAPI) properties.
void clear()
void addAttribute(int key, int type, const QVariant &value, bool overwrite=false)
Adds a TNEF attribute.
bool isEmpty() const const
QMap::const_iterator constEnd() const const
void clear(bool deleteAll=false)
Clears the MAPI and TNEF maps.
QMap::iterator begin()
QString findNamedProp(const QString &name, const QString &fallback=QString(), bool convertToUpper=false) const
Finds a property by name, returning a formatted value.
This file is part of the API for handling TNEF data and defines the KTNEFPropertySet class...
This file is part of the API for handling TNEF data and defines the KTNEFProperty class...
static QString formatValue(const QVariant &v, bool beautify=true)
Creates a formatted string from the value of the property.
KTNEFPropertySet()
Constructor.
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
QVariant::Type type() const const
QMap< int, KTNEFProperty * > & attributes()
Returns a QMap of all (key,TNEF) attributes.
Interface for setting MAPI properties.
Definition: ktnefproperty.h:33
QMap::iterator find(const Key &key)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Oct 28 2020 23:21:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.