1/*
2 ktnefpropertyset.cpp
3
4 Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be>
5
6 This file is part of KTNEF, the KDE TNEF support library/program.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22 */
23/**
24 * @file
25 * This file is part of the API for handling TNEF data and
26 * defines the KTNEFPropertySet class.
27 *
28 * @author Michael Goffioul
29 */
30
31#include "ktnefpropertyset.h"
32#include "ktnefproperty.h"
33
34#include <kdebug.h>
35
36#include <QtCore/QList>
37
38using namespace KTnef;
39
40class KTNEFPropertySet::Private
41{
42 public:
43 QMap<int,KTNEFProperty*> properties_; // used to store MAPI properties
44 QMap<int,KTNEFProperty*> attributes_; // used to store TNEF attributes
45};
46
47KTNEFPropertySet::KTNEFPropertySet()
48 : d( new Private )
49{
50}
51
52KTNEFPropertySet::~KTNEFPropertySet()
53{
54 clear( true );
55
56 delete d;
57}
58
59void KTNEFPropertySet::addProperty( int key, int type, const QVariant &value,
60 const QVariant &name, bool overwrite )
61{
62 QMap<int,KTNEFProperty*>::ConstIterator it = d->properties_.constFind( key );
63 if ( it != d->properties_.constEnd() ) {
64 if ( overwrite ) {
65 delete ( *it );
66 } else {
67 return;
68 }
69 }
70 KTNEFProperty *p = new KTNEFProperty( key, type, value, name );
71 d->properties_[ p->key() ] = p;
72}
73
74QString KTNEFPropertySet::findProp( int key, const QString &fallback,
75 bool upper ) const
76{
77 QMap<int,KTNEFProperty*>::Iterator it = d->properties_.find( key );
78 if ( d->properties_.end() != it ) {
79 return upper ?
80 KTNEFProperty::formatValue( (*it)->value(), false ).toUpper() :
81 KTNEFProperty::formatValue( (*it)->value(), false );
82 } else {
83 return fallback;
84 }
85}
86
87QString KTNEFPropertySet::findNamedProp( const QString &name,
88 const QString &fallback,
89 bool upper ) const
90{
91 for ( QMap<int,KTNEFProperty*>::Iterator it = d->properties_.begin();
92 it != d->properties_.end();
93 ++it ) {
94 if ( (*it)->name().isValid() ) {
95 QString s;
96 if ( (*it)->name().type() == QVariant::String ) {
97 s = (*it)->name().toString();
98 } else {
99 s = QString().sprintf( "0X%04X", (*it)->name().toUInt() );
100 }
101
102 if ( s.toUpper() == name.toUpper() ) {
103 QVariant value = ( *it )->value();
104 if ( value.type() == QVariant::List ) {
105 QList<QVariant> l = value.toList();
106 s = "";
107 for ( QList<QVariant>::ConstIterator lit = l.constBegin();
108 lit != l.constEnd();
109 ++lit ) {
110 if ( !s.isEmpty() ) {
111 s += ',';
112 }
113 s += KTNEFProperty::formatValue( *lit, false );
114 }
115 } else {
116 s = KTNEFProperty::formatValue( value, false );
117 }
118 return upper ? s.toUpper() : s;
119 }
120 }
121 }
122 return fallback;
123}
124
125QMap<int,KTNEFProperty*>& KTNEFPropertySet::properties()
126{
127 return d->properties_;
128}
129
130const QMap<int,KTNEFProperty*>& KTNEFPropertySet::properties() const
131{
132 return d->properties_;
133}
134
135QVariant KTNEFPropertySet::property( int key ) const
136{
137 QMap<int,KTNEFProperty*>::ConstIterator it = d->properties_.constFind( key );
138 if ( it == d->properties_.constEnd() ) {
139 return QVariant();
140 } else {
141 return ( *it )->value();
142 }
143}
144
145void KTNEFPropertySet::clear( bool deleteAll )
146{
147 if ( deleteAll ) {
148 for ( QMap<int,KTNEFProperty*>::ConstIterator it=d->properties_.constBegin();
149 it != d->properties_.constEnd();
150 ++it )
151 delete ( *it );
152 for ( QMap<int,KTNEFProperty*>::ConstIterator it=d->attributes_.constBegin();
153 it != d->attributes_.constEnd();
154 ++it )
155 delete ( *it );
156 }
157 d->properties_.clear();
158 d->attributes_.clear();
159}
160
161void KTNEFPropertySet::addAttribute( int key, int type, const QVariant &value,
162 bool overwrite )
163{
164 QMap<int,KTNEFProperty*>::ConstIterator it = d->attributes_.constFind( key );
165 if ( it != d->attributes_.constEnd() ) {
166 if ( overwrite ) {
167 delete ( *it );
168 } else {
169 return;
170 }
171 }
172 KTNEFProperty *p = new KTNEFProperty( key, type, value, QVariant() );
173 d->attributes_[ p->key() ] = p;
174}
175
176QMap<int,KTNEFProperty*>& KTNEFPropertySet::attributes()
177{
178 return d->attributes_;
179}
180
181const QMap<int,KTNEFProperty*>& KTNEFPropertySet::attributes() const
182{
183 return d->attributes_;
184}
185
186QVariant KTNEFPropertySet::attribute( int key ) const
187{
188 QMap<int,KTNEFProperty*>::ConstIterator it = d->attributes_.constFind( key );
189 if ( it == d->attributes_.constEnd() ) {
190 return QVariant();
191 } else {
192 return ( *it )->value();
193 }
194}
195