00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include "ktnefpropertyset.h"
00032 #include "ktnefproperty.h"
00033
00034 #include <kdebug.h>
00035
00036 #include <QtCore/QList>
00037
00038 using namespace KTnef;
00039
00040 class KTNEFPropertySet::Private
00041 {
00042 public:
00043 QMap<int,KTNEFProperty*> properties_;
00044 QMap<int,KTNEFProperty*> attributes_;
00045 };
00046
00047 KTNEFPropertySet::KTNEFPropertySet()
00048 : d( new Private )
00049 {
00050 }
00051
00052 KTNEFPropertySet::~KTNEFPropertySet()
00053 {
00054 clear( true );
00055
00056 delete d;
00057 }
00058
00059 void KTNEFPropertySet::addProperty( int key, int type, const QVariant &value,
00060 const QVariant &name, bool overwrite )
00061 {
00062 QMap<int,KTNEFProperty*>::ConstIterator it = d->properties_.constFind( key );
00063 if ( it != d->properties_.constEnd() ) {
00064 if ( overwrite ) {
00065 delete ( *it );
00066 } else {
00067 return;
00068 }
00069 }
00070 KTNEFProperty *p = new KTNEFProperty( key, type, value, name );
00071 d->properties_[ p->key() ] = p;
00072 }
00073
00074 QString KTNEFPropertySet::findProp( int key, const QString &fallback,
00075 bool upper ) const
00076 {
00077 QMap<int,KTNEFProperty*>::Iterator it = d->properties_.find( key );
00078 if ( d->properties_.end() != it ) {
00079 return upper ?
00080 KTNEFProperty::formatValue( (*it)->value(), false ).toUpper() :
00081 KTNEFProperty::formatValue( (*it)->value(), false );
00082 } else {
00083 return fallback;
00084 }
00085 }
00086
00087 QString KTNEFPropertySet::findNamedProp( const QString &name,
00088 const QString &fallback,
00089 bool upper ) const
00090 {
00091 for ( QMap<int,KTNEFProperty*>::Iterator it = d->properties_.begin();
00092 it != d->properties_.end();
00093 ++it ) {
00094 if ( (*it)->name().isValid() ) {
00095 QString s;
00096 if ( (*it)->name().type() == QVariant::String ) {
00097 s = (*it)->name().toString();
00098 } else {
00099 s = QString().sprintf( "0X%04X", (*it)->name().toUInt() );
00100 }
00101
00102 if ( s.toUpper() == name.toUpper() ) {
00103 QVariant value = ( *it )->value();
00104 if ( value.type() == QVariant::List ) {
00105 QList<QVariant> l = value.toList();
00106 s = "";
00107 for ( QList<QVariant>::ConstIterator lit = l.constBegin();
00108 lit != l.constEnd();
00109 ++lit ) {
00110 if ( !s.isEmpty() ) {
00111 s += ',';
00112 }
00113 s += KTNEFProperty::formatValue( *lit, false );
00114 }
00115 } else {
00116 s = KTNEFProperty::formatValue( value, false );
00117 }
00118 return upper ? s.toUpper() : s;
00119 }
00120 }
00121 }
00122 return fallback;
00123 }
00124
00125 QMap<int,KTNEFProperty*>& KTNEFPropertySet::properties()
00126 {
00127 return d->properties_;
00128 }
00129
00130 const QMap<int,KTNEFProperty*>& KTNEFPropertySet::properties() const
00131 {
00132 return d->properties_;
00133 }
00134
00135 QVariant KTNEFPropertySet::property( int key ) const
00136 {
00137 QMap<int,KTNEFProperty*>::ConstIterator it = d->properties_.constFind( key );
00138 if ( it == d->properties_.constEnd() ) {
00139 return QVariant();
00140 } else {
00141 return ( *it )->value();
00142 }
00143 }
00144
00145 void KTNEFPropertySet::clear( bool deleteAll )
00146 {
00147 if ( deleteAll ) {
00148 for ( QMap<int,KTNEFProperty*>::ConstIterator it=d->properties_.constBegin();
00149 it != d->properties_.constEnd();
00150 ++it )
00151 delete ( *it );
00152 for ( QMap<int,KTNEFProperty*>::ConstIterator it=d->attributes_.constBegin();
00153 it != d->attributes_.constEnd();
00154 ++it )
00155 delete ( *it );
00156 }
00157 d->properties_.clear();
00158 d->attributes_.clear();
00159 }
00160
00161 void KTNEFPropertySet::addAttribute( int key, int type, const QVariant &value,
00162 bool overwrite )
00163 {
00164 QMap<int,KTNEFProperty*>::ConstIterator it = d->attributes_.constFind( key );
00165 if ( it != d->attributes_.constEnd() ) {
00166 if ( overwrite ) {
00167 delete ( *it );
00168 } else {
00169 return;
00170 }
00171 }
00172 KTNEFProperty *p = new KTNEFProperty( key, type, value, QVariant() );
00173 d->attributes_[ p->key() ] = p;
00174 }
00175
00176 QMap<int,KTNEFProperty*>& KTNEFPropertySet::attributes()
00177 {
00178 return d->attributes_;
00179 }
00180
00181 const QMap<int,KTNEFProperty*>& KTNEFPropertySet::attributes() const
00182 {
00183 return d->attributes_;
00184 }
00185
00186 QVariant KTNEFPropertySet::attribute( int key ) const
00187 {
00188 QMap<int,KTNEFProperty*>::ConstIterator it = d->attributes_.constFind( key );
00189 if ( it == d->attributes_.constEnd() ) {
00190 return QVariant();
00191 } else {
00192 return ( *it )->value();
00193 }
00194 }