1 | /* |
2 | ktnefproperty.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 KTNEFProperty class. |
27 | * |
28 | * @author Michael Goffioul |
29 | */ |
30 | |
31 | #include "ktnefproperty.h" |
32 | #include "mapi.h" |
33 | |
34 | #include <QtCore/QDateTime> |
35 | |
36 | #include <ctype.h> |
37 | |
38 | using namespace KTnef; |
39 | |
40 | class KTNEFProperty::Private |
41 | { |
42 | public: |
43 | int _key; |
44 | int _type; |
45 | QVariant _value; |
46 | QVariant _name; |
47 | }; |
48 | |
49 | KTNEFProperty::KTNEFProperty() |
50 | : d( new Private ) |
51 | { |
52 | } |
53 | |
54 | KTNEFProperty::KTNEFProperty( int key_, int type_, const QVariant &value_, |
55 | const QVariant &name_ ) |
56 | : d( new Private ) |
57 | { |
58 | d->_key = key_; |
59 | d->_type = type_; |
60 | d->_value = value_; |
61 | d->_name = name_; |
62 | } |
63 | |
64 | KTNEFProperty::KTNEFProperty( const KTNEFProperty &p ) |
65 | : d( new Private ) |
66 | { |
67 | *d = *p.d; |
68 | } |
69 | |
70 | KTNEFProperty::~KTNEFProperty() |
71 | { |
72 | delete d; |
73 | } |
74 | |
75 | KTNEFProperty &KTNEFProperty::operator=( const KTNEFProperty &other ) |
76 | { |
77 | if ( this != &other ) { |
78 | *d = *other.d; |
79 | } |
80 | |
81 | return *this; |
82 | } |
83 | |
84 | QString KTNEFProperty::keyString() const |
85 | { |
86 | if ( d->_name.isValid() ) { |
87 | if ( d->_name.type() == QVariant::String ) { |
88 | return d->_name.toString(); |
89 | } else { |
90 | return mapiNamedTagString( d->_name.toUInt(), d->_key ); |
91 | } |
92 | } else { |
93 | return mapiTagString( d->_key ); |
94 | } |
95 | } |
96 | |
97 | QString KTNEFProperty::formatValue( const QVariant &value, bool beautify ) |
98 | { |
99 | if ( value.type() == QVariant::ByteArray ) { |
100 | // check the first bytes (up to 8) if they are |
101 | // printable characters |
102 | QByteArray arr = value.toByteArray(); |
103 | bool printable = true; |
104 | for ( int i=qMin( arr.size(), 8 )-1; i>=0 && printable; i-- ) { |
105 | printable = ( isprint( arr[ i ] ) != 0 ); |
106 | } |
107 | if ( !printable ) { |
108 | QString s; |
109 | int i; |
110 | int txtCount = beautify ? qMin( arr.size(), 32 ) : arr.size(); |
111 | for ( i=0; i < txtCount; ++i ) { |
112 | s.append( QString().sprintf( "%02X" , ( uchar )arr[ i ] ) ); |
113 | if ( beautify ) { |
114 | s.append( " " ); |
115 | } |
116 | } |
117 | if ( i < arr.size() ) { |
118 | s.append( "... (size=" + QString::number( arr.size() ) + ')' ); |
119 | } |
120 | return s; |
121 | } |
122 | } |
123 | //else if ( value.type() == QVariant::DateTime ) |
124 | // return value.toDateTime().toString(); |
125 | return value.toString(); |
126 | } |
127 | |
128 | QString KTNEFProperty::valueString() const |
129 | { |
130 | return formatValue( d->_value ); |
131 | } |
132 | |
133 | int KTNEFProperty::key() const |
134 | { |
135 | return d->_key; |
136 | } |
137 | |
138 | int KTNEFProperty::type() const |
139 | { |
140 | return d->_type; |
141 | } |
142 | |
143 | QVariant KTNEFProperty::value() const |
144 | { |
145 | return d->_value; |
146 | } |
147 | |
148 | QVariant KTNEFProperty::name() const |
149 | { |
150 | return d->_name; |
151 | } |
152 | |
153 | bool KTNEFProperty::isVector() const |
154 | { |
155 | return d->_value.type() == QVariant::List; |
156 | } |
157 | |