summaryrefslogtreecommitdiffabout
path: root/kabc/vcardparser
authorzautrix <zautrix>2005-10-28 03:24:50 (UTC)
committer zautrix <zautrix>2005-10-28 03:24:50 (UTC)
commitf83a59cf4f9d0cff4111b6f5c88e88e6dc96e79e (patch) (unidiff)
tree969e6cf85499e3b1dcaa4dc24c972b65c906cfa9 /kabc/vcardparser
parentcbda16d4966c7483d20d1b6b5a64c8af367ea732 (diff)
downloadkdepimpi-f83a59cf4f9d0cff4111b6f5c88e88e6dc96e79e.zip
kdepimpi-f83a59cf4f9d0cff4111b6f5c88e88e6dc96e79e.tar.gz
kdepimpi-f83a59cf4f9d0cff4111b6f5c88e88e6dc96e79e.tar.bz2
many new bugs...
Diffstat (limited to 'kabc/vcardparser') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/vcardparser/vcardtool.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/kabc/vcardparser/vcardtool.cpp b/kabc/vcardparser/vcardtool.cpp
index c3d92b6..204326e 100644
--- a/kabc/vcardparser/vcardtool.cpp
+++ b/kabc/vcardparser/vcardtool.cpp
@@ -1,869 +1,869 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21#include <qdatastream.h> 21#include <qdatastream.h>
22#include <qstring.h> 22#include <qstring.h>
23#include <qregexp.h> 23#include <qregexp.h>
24 24
25#include "agent.h" 25#include "agent.h"
26#include "key.h" 26#include "key.h"
27#include "picture.h" 27#include "picture.h"
28#include "secrecy.h" 28#include "secrecy.h"
29#include "sound.h" 29#include "sound.h"
30 30
31#include "vcardtool.h" 31#include "vcardtool.h"
32 32
33using namespace KABC; 33using namespace KABC;
34 34
35VCardTool::VCardTool() 35VCardTool::VCardTool()
36{ 36{
37 mAddressTypeMap.insert( "dom", Address::Dom ); 37 mAddressTypeMap.insert( "dom", Address::Dom );
38 mAddressTypeMap.insert( "intl", Address::Intl ); 38 mAddressTypeMap.insert( "intl", Address::Intl );
39 mAddressTypeMap.insert( "postal", Address::Postal ); 39 mAddressTypeMap.insert( "postal", Address::Postal );
40 mAddressTypeMap.insert( "parcel", Address::Parcel ); 40 mAddressTypeMap.insert( "parcel", Address::Parcel );
41 mAddressTypeMap.insert( "home", Address::Home ); 41 mAddressTypeMap.insert( "home", Address::Home );
42 mAddressTypeMap.insert( "work", Address::Work ); 42 mAddressTypeMap.insert( "work", Address::Work );
43 mAddressTypeMap.insert( "pref", Address::Pref ); 43 mAddressTypeMap.insert( "pref", Address::Pref );
44 44
45 mPhoneTypeMap.insert( "HOME", PhoneNumber::Home ); 45 mPhoneTypeMap.insert( "HOME", PhoneNumber::Home );
46 mPhoneTypeMap.insert( "WORK", PhoneNumber::Work ); 46 mPhoneTypeMap.insert( "WORK", PhoneNumber::Work );
47 mPhoneTypeMap.insert( "MSG", PhoneNumber::Msg ); 47 mPhoneTypeMap.insert( "MSG", PhoneNumber::Msg );
48 mPhoneTypeMap.insert( "PREF", PhoneNumber::Pref ); 48 mPhoneTypeMap.insert( "PREF", PhoneNumber::Pref );
49 mPhoneTypeMap.insert( "VOICE", PhoneNumber::Voice ); 49 mPhoneTypeMap.insert( "VOICE", PhoneNumber::Voice );
50 mPhoneTypeMap.insert( "FAX", PhoneNumber::Fax ); 50 mPhoneTypeMap.insert( "FAX", PhoneNumber::Fax );
51 mPhoneTypeMap.insert( "CELL", PhoneNumber::Cell ); 51 mPhoneTypeMap.insert( "CELL", PhoneNumber::Cell );
52 mPhoneTypeMap.insert( "VIDEO", PhoneNumber::Video ); 52 mPhoneTypeMap.insert( "VIDEO", PhoneNumber::Video );
53 mPhoneTypeMap.insert( "BBS", PhoneNumber::Bbs ); 53 mPhoneTypeMap.insert( "BBS", PhoneNumber::Bbs );
54 mPhoneTypeMap.insert( "MODEM", PhoneNumber::Modem ); 54 mPhoneTypeMap.insert( "MODEM", PhoneNumber::Modem );
55 mPhoneTypeMap.insert( "CAR", PhoneNumber::Car ); 55 mPhoneTypeMap.insert( "CAR", PhoneNumber::Car );
56 mPhoneTypeMap.insert( "ISDN", PhoneNumber::Isdn ); 56 mPhoneTypeMap.insert( "ISDN", PhoneNumber::Isdn );
57 mPhoneTypeMap.insert( "PCS", PhoneNumber::Pcs ); 57 mPhoneTypeMap.insert( "PCS", PhoneNumber::Pcs );
58 mPhoneTypeMap.insert( "PAGER", PhoneNumber::Pager ); 58 mPhoneTypeMap.insert( "PAGER", PhoneNumber::Pager );
59} 59}
60 60
61VCardTool::~VCardTool() 61VCardTool::~VCardTool()
62{ 62{
63} 63}
64 64
65// TODO: make list a const& 65// TODO: make list a const&
66QString VCardTool::createVCards( Addressee::List list, VCard::Version version ) 66QString VCardTool::createVCards( Addressee::List list, VCard::Version version )
67{ 67{
68 VCard::List vCardList; 68 VCard::List vCardList;
69 static const QRegExp semiExp(";"); 69 static const QRegExp semiExp(";");
70 70
71 Addressee::List::ConstIterator addrIt; 71 Addressee::List::ConstIterator addrIt;
72 Addressee::List::ConstIterator listEnd( list.end() ); 72 Addressee::List::ConstIterator listEnd( list.end() );
73 for ( addrIt = list.begin(); addrIt != listEnd; ++addrIt ) { 73 for ( addrIt = list.begin(); addrIt != listEnd; ++addrIt ) {
74 VCard card; 74 VCard card;
75 QStringList::ConstIterator strIt; 75 QStringList::ConstIterator strIt;
76 76
77 // ADR + LABEL 77 // ADR + LABEL
78 const Address::List addresses = (*addrIt).addresses(); 78 const Address::List addresses = (*addrIt).addresses();
79 for ( Address::List::ConstIterator it = addresses.begin(); it != addresses.end(); ++it ) { 79 for ( Address::List::ConstIterator it = addresses.begin(); it != addresses.end(); ++it ) {
80 QStringList address; 80 QStringList address;
81 81
82 bool isEmpty = ( (*it).postOfficeBox().isEmpty() && 82 bool isEmpty = ( (*it).postOfficeBox().isEmpty() &&
83 (*it).extended().isEmpty() && 83 (*it).extended().isEmpty() &&
84 (*it).street().isEmpty() && 84 (*it).street().isEmpty() &&
85 (*it).locality().isEmpty() && 85 (*it).locality().isEmpty() &&
86 (*it).region().isEmpty() && 86 (*it).region().isEmpty() &&
87 (*it).postalCode().isEmpty() && 87 (*it).postalCode().isEmpty() &&
88 (*it).country().isEmpty() ); 88 (*it).country().isEmpty() );
89 89
90 address.append( (*it).postOfficeBox().replace( semiExp, "\\;" ) ); 90 address.append( (*it).postOfficeBox().replace( semiExp, "\\;" ) );
91 address.append( (*it).extended().replace( semiExp, "\\;" ) ); 91 address.append( (*it).extended().replace( semiExp, "\\;" ) );
92 address.append( (*it).street().replace( semiExp, "\\;" ) ); 92 address.append( (*it).street().replace( semiExp, "\\;" ) );
93 address.append( (*it).locality().replace( semiExp, "\\;" ) ); 93 address.append( (*it).locality().replace( semiExp, "\\;" ) );
94 address.append( (*it).region().replace( semiExp, "\\;" ) ); 94 address.append( (*it).region().replace( semiExp, "\\;" ) );
95 address.append( (*it).postalCode().replace( semiExp, "\\;" ) ); 95 address.append( (*it).postalCode().replace( semiExp, "\\;" ) );
96 address.append( (*it).country().replace( semiExp, "\\;" ) ); 96 address.append( (*it).country().replace( semiExp, "\\;" ) );
97 97
98 VCardLine adrLine( "ADR", address.join( ";" ) ); 98 VCardLine adrLine( "ADR", address.join( ";" ) );
99 if ( version == VCard::v2_1 ) { 99 if ( version == VCard::v2_1 ) {
100 adrLine.addParameter( "CHARSET", "UTF-8" ); 100 adrLine.addParameter( "CHARSET", "UTF-8" );
101 adrLine.addParameter( "ENCODING", "8BIT" ); 101 adrLine.addParameter( "ENCODING", "8BIT" );
102 } 102 }
103 103
104 VCardLine labelLine( "LABEL", (*it).label() ); 104 VCardLine labelLine( "LABEL", (*it).label() );
105 if ( version == VCard::v2_1 ) { 105 if ( version == VCard::v2_1 ) {
106 labelLine.addParameter( "CHARSET", "UTF-8" ); 106 labelLine.addParameter( "CHARSET", "UTF-8" );
107 labelLine.addParameter( "ENCODING", "8BIT" ); 107 labelLine.addParameter( "ENCODING", "8BIT" );
108 } 108 }
109 109
110 bool hasLabel = !(*it).label().isEmpty(); 110 bool hasLabel = !(*it).label().isEmpty();
111 QMap<QString, int>::ConstIterator typeIt; 111 QMap<QString, int>::ConstIterator typeIt;
112 for ( typeIt = mAddressTypeMap.begin(); typeIt != mAddressTypeMap.end(); ++typeIt ) { 112 for ( typeIt = mAddressTypeMap.begin(); typeIt != mAddressTypeMap.end(); ++typeIt ) {
113 if ( typeIt.data() & (*it).type() ) { 113 if ( typeIt.data() & (*it).type() ) {
114 adrLine.addParameter( "TYPE", typeIt.key() ); 114 adrLine.addParameter( "TYPE", typeIt.key() );
115 if ( hasLabel ) 115 if ( hasLabel )
116 labelLine.addParameter( "TYPE", typeIt.key() ); 116 labelLine.addParameter( "TYPE", typeIt.key() );
117 } 117 }
118 } 118 }
119 119
120 if ( !isEmpty ) 120 if ( !isEmpty )
121 card.addLine( adrLine ); 121 card.addLine( adrLine );
122 if ( hasLabel ) 122 if ( hasLabel )
123 card.addLine( labelLine ); 123 card.addLine( labelLine );
124 } 124 }
125 125
126 // AGENT 126 // AGENT
127 card.addLine( createAgent( version, (*addrIt).agent() ) ); 127 card.addLine( createAgent( version, (*addrIt).agent() ) );
128 128
129 // BDAY 129 // BDAY
130 card.addLine( VCardLine( "BDAY", createDateTime( (*addrIt).birthday() ) ) ); 130 card.addLine( VCardLine( "BDAY", createDateTime( (*addrIt).birthday() ) ) );
131 131
132 // CATEGORIES 132 // CATEGORIES
133 if ( version == VCard::v3_0 ) { 133 if ( version == VCard::v3_0 ) {
134 QStringList categories = (*addrIt).categories(); 134 QStringList categories = (*addrIt).categories();
135 QStringList::Iterator catIt; 135 QStringList::Iterator catIt;
136 for ( catIt = categories.begin(); catIt != categories.end(); ++catIt ) 136 for ( catIt = categories.begin(); catIt != categories.end(); ++catIt )
137 (*catIt).replace( QRegExp(","), "\\," ); 137 (*catIt).replace( QRegExp(","), "\\," );
138 138
139 VCardLine catLine( "CATEGORIES", categories.join( "," ) ); 139 VCardLine catLine( "CATEGORIES", categories.join( "," ) );
140 if ( version == VCard::v2_1 ) { 140 if ( version == VCard::v2_1 ) {
141 catLine.addParameter( "CHARSET", "UTF-8" ); 141 catLine.addParameter( "CHARSET", "UTF-8" );
142 catLine.addParameter( "ENCODING", "8BIT" ); 142 catLine.addParameter( "ENCODING", "8BIT" );
143 } 143 }
144 144
145 card.addLine( catLine ); 145 card.addLine( catLine );
146 } 146 }
147 147
148 // CLASS 148 // CLASS
149 if ( version == VCard::v3_0 ) { 149 if ( version == VCard::v3_0 ) {
150 card.addLine( createSecrecy( (*addrIt).secrecy() ) ); 150 card.addLine( createSecrecy( (*addrIt).secrecy() ) );
151 } 151 }
152 152
153 // EMAIL 153 // EMAIL
154 const QStringList emails = (*addrIt).emails(); 154 const QStringList emails = (*addrIt).emails();
155 bool pref = true; 155 bool pref = true;
156 for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) { 156 for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
157 VCardLine line( "EMAIL", *strIt ); 157 VCardLine line( "EMAIL", *strIt );
158 if ( version == VCard::v2_1 ) { 158 if ( version == VCard::v2_1 ) {
159 line.addParameter( "CHARSET", "UTF-8" ); 159 line.addParameter( "CHARSET", "UTF-8" );
160 line.addParameter( "ENCODING", "8BIT" ); 160 line.addParameter( "ENCODING", "8BIT" );
161 } 161 }
162 if ( pref == true && emails.count() > 1 ) { 162 if ( pref == true && emails.count() > 1 ) {
163 line.addParameter( "TYPE", "PREF" ); 163 line.addParameter( "TYPE", "PREF" );
164 pref = false; 164 pref = false;
165 } 165 }
166 card.addLine( line ); 166 card.addLine( line );
167 } 167 }
168 168
169 // FN 169 // FN
170 VCardLine fnLine( "FN", (*addrIt).formattedName() ); 170 VCardLine fnLine( "FN", (*addrIt).formattedName() );
171 if ( version == VCard::v2_1 ) { 171 if ( version == VCard::v2_1 ) {
172 fnLine.addParameter( "CHARSET", "UTF-8" ); 172 fnLine.addParameter( "CHARSET", "UTF-8" );
173 fnLine.addParameter( "ENCODING", "8BIT" ); 173 fnLine.addParameter( "ENCODING", "8BIT" );
174 } 174 }
175 card.addLine( fnLine ); 175 card.addLine( fnLine );
176 176
177 // GEO 177 // GEO
178 Geo geo = (*addrIt).geo(); 178 Geo geo = (*addrIt).geo();
179 if ( geo.isValid() ) { 179 if ( geo.isValid() ) {
180 QString str; 180 QString str;
181 str.sprintf( "%.6f;%.6f", geo.latitude(), geo.longitude() ); 181 str.sprintf( "%.6f;%.6f", geo.latitude(), geo.longitude() );
182 card.addLine( VCardLine( "GEO", str ) ); 182 card.addLine( VCardLine( "GEO", str ) );
183 } 183 }
184 184
185 // KEY 185 // KEY
186 const Key::List keys = (*addrIt).keys(); 186 const Key::List keys = (*addrIt).keys();
187 Key::List::ConstIterator keyIt; 187 Key::List::ConstIterator keyIt;
188 for ( keyIt = keys.begin(); keyIt != keys.end(); ++keyIt ) 188 for ( keyIt = keys.begin(); keyIt != keys.end(); ++keyIt )
189 card.addLine( createKey( *keyIt ) ); 189 card.addLine( createKey( *keyIt ) );
190 190
191 // LOGO 191 // LOGO
192 card.addLine( createPicture( "LOGO", (*addrIt).logo() ) ); 192 card.addLine( createPicture( "LOGO", (*addrIt).logo() ) );
193 193
194 // MAILER 194 // MAILER
195 VCardLine mailerLine( "MAILER", (*addrIt).mailer() ); 195 VCardLine mailerLine( "MAILER", (*addrIt).mailer() );
196 if ( version == VCard::v2_1 ) { 196 if ( version == VCard::v2_1 ) {
197 mailerLine.addParameter( "CHARSET", "UTF-8" ); 197 mailerLine.addParameter( "CHARSET", "UTF-8" );
198 mailerLine.addParameter( "ENCODING", "8BIT" ); 198 mailerLine.addParameter( "ENCODING", "8BIT" );
199 } 199 }
200 card.addLine( mailerLine ); 200 card.addLine( mailerLine );
201 201
202 // N 202 // N
203 QStringList name; 203 QStringList name;
204 name.append( (*addrIt).familyName().replace( semiExp, "\\;" ) ); 204 name.append( (*addrIt).familyName().replace( semiExp, "\\;" ) );
205 name.append( (*addrIt).givenName().replace( semiExp, "\\;" ) ); 205 name.append( (*addrIt).givenName().replace( semiExp, "\\;" ) );
206 name.append( (*addrIt).additionalName().replace( semiExp, "\\;" ) ); 206 name.append( (*addrIt).additionalName().replace( semiExp, "\\;" ) );
207 name.append( (*addrIt).prefix().replace( semiExp, "\\;" ) ); 207 name.append( (*addrIt).prefix().replace( semiExp, "\\;" ) );
208 name.append( (*addrIt).suffix().replace( semiExp, "\\;" ) ); 208 name.append( (*addrIt).suffix().replace( semiExp, "\\;" ) );
209 209
210 VCardLine nLine( "N", name.join( ";" ) ); 210 VCardLine nLine( "N", name.join( ";" ) );
211 if ( version == VCard::v2_1 ) { 211 if ( version == VCard::v2_1 ) {
212 nLine.addParameter( "CHARSET", "UTF-8" ); 212 nLine.addParameter( "CHARSET", "UTF-8" );
213 nLine.addParameter( "ENCODING", "8BIT" ); 213 nLine.addParameter( "ENCODING", "8BIT" );
214 } 214 }
215 card.addLine( nLine ); 215 card.addLine( nLine );
216 216
217 // NAME 217 // NAME
218 VCardLine nameLine( "NAME", (*addrIt).name() ); 218 VCardLine nameLine( "NAME", (*addrIt).name() );
219 if ( version == VCard::v2_1 ) { 219 if ( version == VCard::v2_1 ) {
220 nameLine.addParameter( "CHARSET", "UTF-8" ); 220 nameLine.addParameter( "CHARSET", "UTF-8" );
221 nameLine.addParameter( "ENCODING", "8BIT" ); 221 nameLine.addParameter( "ENCODING", "8BIT" );
222 } 222 }
223 card.addLine( nameLine ); 223 card.addLine( nameLine );
224 224
225 // NICKNAME 225 // NICKNAME
226 if ( version == VCard::v3_0 ) 226 if ( version == VCard::v3_0 )
227 card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) ); 227 card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) );
228 228
229 // NOTE 229 // NOTE
230 VCardLine noteLine( "NOTE", (*addrIt).note() ); 230 VCardLine noteLine( "NOTE", (*addrIt).note() );
231 if ( version == VCard::v2_1 ) { 231 if ( version == VCard::v2_1 ) {
232 noteLine.addParameter( "CHARSET", "UTF-8" ); 232 noteLine.addParameter( "CHARSET", "UTF-8" );
233 noteLine.addParameter( "ENCODING", "8BIT" ); 233 noteLine.addParameter( "ENCODING", "8BIT" );
234 } 234 }
235 card.addLine( noteLine ); 235 card.addLine( noteLine );
236 236
237 // ORG 237 // ORG
238 VCardLine orgLine( "ORG", (*addrIt).organization() ); 238 VCardLine orgLine( "ORG", (*addrIt).organization() );
239 if ( version == VCard::v2_1 ) { 239 if ( version == VCard::v2_1 ) {
240 orgLine.addParameter( "CHARSET", "UTF-8" ); 240 orgLine.addParameter( "CHARSET", "UTF-8" );
241 orgLine.addParameter( "ENCODING", "8BIT" ); 241 orgLine.addParameter( "ENCODING", "8BIT" );
242 } 242 }
243 card.addLine( orgLine ); 243 card.addLine( orgLine );
244 244
245 // PHOTO 245 // PHOTO
246 card.addLine( createPicture( "PHOTO", (*addrIt).photo() ) ); 246 card.addLine( createPicture( "PHOTO", (*addrIt).photo() ) );
247 247
248 // PROID 248 // PROID
249 if ( version == VCard::v3_0 ) 249 if ( version == VCard::v3_0 )
250 card.addLine( VCardLine( "PRODID", (*addrIt).productId() ) ); 250 card.addLine( VCardLine( "PRODID", (*addrIt).productId() ) );
251 251
252 // REV 252 // REV
253 card.addLine( VCardLine( "REV", createDateTime( (*addrIt).revision() ) ) ); 253 card.addLine( VCardLine( "REV", createDateTime( (*addrIt).revision() ) ) );
254 254
255 // ROLE 255 // ROLE
256 VCardLine roleLine( "ROLE", (*addrIt).role() ); 256 VCardLine roleLine( "ROLE", (*addrIt).role() );
257 if ( version == VCard::v2_1 ) { 257 if ( version == VCard::v2_1 ) {
258 roleLine.addParameter( "CHARSET", "UTF-8" ); 258 roleLine.addParameter( "CHARSET", "UTF-8" );
259 roleLine.addParameter( "ENCODING", "8BIT" ); 259 roleLine.addParameter( "ENCODING", "8BIT" );
260 } 260 }
261 card.addLine( roleLine ); 261 card.addLine( roleLine );
262 262
263 // SORT-STRING 263 // SORT-STRING
264 if ( version == VCard::v3_0 ) 264 if ( version == VCard::v3_0 )
265 card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) ); 265 card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) );
266 266
267 // SOUND 267 // SOUND
268 card.addLine( createSound( (*addrIt).sound() ) ); 268 card.addLine( createSound( (*addrIt).sound() ) );
269 269
270 // TEL 270 // TEL
271 const PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers(); 271 const PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers();
272 PhoneNumber::List::ConstIterator phoneIt; 272 PhoneNumber::List::ConstIterator phoneIt;
273 for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) { 273 for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) {
274 VCardLine line( "TEL", (*phoneIt).number() ); 274 VCardLine line( "TEL", (*phoneIt).number() );
275 275
276 QMap<QString, int>::ConstIterator typeIt; 276 QMap<QString, int>::ConstIterator typeIt;
277 for ( typeIt = mPhoneTypeMap.begin(); typeIt != mPhoneTypeMap.end(); ++typeIt ) { 277 for ( typeIt = mPhoneTypeMap.begin(); typeIt != mPhoneTypeMap.end(); ++typeIt ) {
278 if ( typeIt.data() & (*phoneIt).type() ) 278 if ( typeIt.data() & (*phoneIt).type() )
279 line.addParameter( "TYPE", typeIt.key() ); 279 line.addParameter( "TYPE", typeIt.key() );
280 } 280 }
281 281
282 card.addLine( line ); 282 card.addLine( line );
283 } 283 }
284 284
285 // TITLE 285 // TITLE
286 VCardLine titleLine( "TITLE", (*addrIt).title() ); 286 VCardLine titleLine( "TITLE", (*addrIt).title() );
287 if ( version == VCard::v2_1 ) { 287 if ( version == VCard::v2_1 ) {
288 titleLine.addParameter( "CHARSET", "UTF-8" ); 288 titleLine.addParameter( "CHARSET", "UTF-8" );
289 titleLine.addParameter( "ENCODING", "8BIT" ); 289 titleLine.addParameter( "ENCODING", "8BIT" );
290 } 290 }
291 card.addLine( titleLine ); 291 card.addLine( titleLine );
292 292
293 // TZ 293 // TZ
294 TimeZone timeZone = (*addrIt).timeZone(); 294 TimeZone timeZone = (*addrIt).timeZone();
295 if ( timeZone.isValid() ) { 295 if ( timeZone.isValid() ) {
296 QString str; 296 QString str;
297 297
298 int neg = 1; 298 int neg = 1;
299 if ( timeZone.offset() < 0 ) 299 if ( timeZone.offset() < 0 )
300 neg = -1; 300 neg = -1;
301 301
302 str.sprintf( "%c%02d:%02d", ( timeZone.offset() >= 0 ? '+' : '-' ), 302 str.sprintf( "%c%02d:%02d", ( timeZone.offset() >= 0 ? '+' : '-' ),
303 ( timeZone.offset() / 60 ) * neg, 303 ( timeZone.offset() / 60 ) * neg,
304 ( timeZone.offset() % 60 ) * neg ); 304 ( timeZone.offset() % 60 ) * neg );
305 305
306 card.addLine( VCardLine( "TZ", str ) ); 306 card.addLine( VCardLine( "TZ", str ) );
307 } 307 }
308 308
309 // UID 309 // UID
310 card.addLine( VCardLine( "UID", (*addrIt).uid() ) ); 310 card.addLine( VCardLine( "UID", (*addrIt).uid() ) );
311 311
312 // URL 312 // URL
313 card.addLine( VCardLine( "URL", (*addrIt).url().url() ) ); 313 card.addLine( VCardLine( "URL", (*addrIt).url().url() ) );
314 314
315 // VERSION 315 // VERSION
316 if ( version == VCard::v2_1 ) 316 if ( version == VCard::v2_1 )
317 card.addLine( VCardLine( "VERSION", "2.1" ) ); 317 card.addLine( VCardLine( "VERSION", "2.1" ) );
318 if ( version == VCard::v3_0 ) 318 if ( version == VCard::v3_0 )
319 card.addLine( VCardLine( "VERSION", "3.0" ) ); 319 card.addLine( VCardLine( "VERSION", "3.0" ) );
320 320
321 // X- 321 // X-
322 const QStringList customs = (*addrIt).customs(); 322 const QStringList customs = (*addrIt).customs();
323 for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) { 323 for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) {
324 QString identifier = "X-" + (*strIt).left( (*strIt).find( ":" ) ); 324 QString identifier = "X-" + (*strIt).left( (*strIt).find( ":" ) );
325 QString value = (*strIt).mid( (*strIt).find( ":" ) + 1 ); 325 QString value = (*strIt).mid( (*strIt).find( ":" ) + 1 );
326 if ( value.isEmpty() ) 326 if ( value.isEmpty() )
327 continue; 327 continue;
328 328
329 VCardLine line( identifier, value ); 329 VCardLine line( identifier, value );
330 if ( version == VCard::v2_1 ) { 330 if ( version == VCard::v2_1 ) {
331 line.addParameter( "CHARSET", "UTF-8" ); 331 line.addParameter( "CHARSET", "UTF-8" );
332 line.addParameter( "ENCODING", "8BIT" ); 332 line.addParameter( "ENCODING", "8BIT" );
333 } 333 }
334 card.addLine( line ); 334 card.addLine( line );
335 } 335 }
336 336
337 vCardList.append( card ); 337 vCardList.append( card );
338 } 338 }
339 339
340 return VCardParser::createVCards( vCardList ); 340 return VCardParser::createVCards( vCardList );
341} 341}
342 342
343Addressee::List VCardTool::parseVCards( const QString& vcard ) 343Addressee::List VCardTool::parseVCards( const QString& vcard )
344{ 344{
345 static const QChar semicolonSep( ';' ); 345 static const QChar semicolonSep( ';' );
346 static const QChar commaSep( ',' ); 346 static const QChar commaSep( ',' );
347 QString identifier; 347 QString identifier;
348 348
349 Addressee::List addrList; 349 Addressee::List addrList;
350 const VCard::List vCardList = VCardParser::parseVCards( vcard ); 350 const VCard::List vCardList = VCardParser::parseVCards( vcard );
351 351
352 VCard::List::ConstIterator cardIt; 352 VCard::List::ConstIterator cardIt;
353 VCard::List::ConstIterator listEnd( vCardList.end() ); 353 VCard::List::ConstIterator listEnd( vCardList.end() );
354 for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) { 354 for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) {
355 Addressee addr; 355 Addressee addr;
356 356
357 const QStringList idents = (*cardIt).identifiers(); 357 const QStringList idents = (*cardIt).identifiers();
358 QStringList::ConstIterator identIt; 358 QStringList::ConstIterator identIt;
359 QStringList::ConstIterator identEnd( idents.end() ); 359 QStringList::ConstIterator identEnd( idents.end() );
360 for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) { 360 for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) {
361 const VCardLine::List lines = (*cardIt).lines( (*identIt) ); 361 const VCardLine::List lines = (*cardIt).lines( (*identIt) );
362 VCardLine::List::ConstIterator lineIt; 362 VCardLine::List::ConstIterator lineIt;
363 363
364 // iterate over the lines 364 // iterate over the lines
365 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) { 365 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
366 identifier = (*lineIt).identifier().lower(); 366 identifier = (*lineIt).identifier().lower();
367 // ADR 367 // ADR
368 if ( identifier == "adr" ) { 368 if ( identifier == "adr" ) {
369 Address address; 369 Address address;
370 const QStringList addrParts = splitString( semicolonSep, (*lineIt).valueString() ); 370 const QStringList addrParts = splitString( semicolonSep, (*lineIt).valueString() );
371 if ( addrParts.count() > 0 ) 371 if ( addrParts.count() > 0 )
372 address.setPostOfficeBox( addrParts[ 0 ] ); 372 address.setPostOfficeBox( addrParts[ 0 ] );
373 if ( addrParts.count() > 1 ) 373 if ( addrParts.count() > 1 )
374 address.setExtended( addrParts[ 1 ] ); 374 address.setExtended( addrParts[ 1 ] );
375 if ( addrParts.count() > 2 ) 375 if ( addrParts.count() > 2 )
376 address.setStreet( addrParts[ 2 ] ); 376 address.setStreet( addrParts[ 2 ] );
377 if ( addrParts.count() > 3 ) 377 if ( addrParts.count() > 3 )
378 address.setLocality( addrParts[ 3 ] ); 378 address.setLocality( addrParts[ 3 ] );
379 if ( addrParts.count() > 4 ) 379 if ( addrParts.count() > 4 )
380 address.setRegion( addrParts[ 4 ] ); 380 address.setRegion( addrParts[ 4 ] );
381 if ( addrParts.count() > 5 ) 381 if ( addrParts.count() > 5 )
382 address.setPostalCode( addrParts[ 5 ] ); 382 address.setPostalCode( addrParts[ 5 ] );
383 if ( addrParts.count() > 6 ) 383 if ( addrParts.count() > 6 )
384 address.setCountry( addrParts[ 6 ] ); 384 address.setCountry( addrParts[ 6 ] );
385 385
386 int type = 0; 386 int type = 0;
387 387
388 const QStringList types = (*lineIt).parameters( "type" ); 388 const QStringList types = (*lineIt).parameters( "type" );
389 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) 389 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
390 type += mAddressTypeMap[ (*it).lower() ]; 390 type += mAddressTypeMap[ (*it).lower() ];
391 391
392 address.setType( type ); 392 address.setType( type );
393 addr.insertAddress( address ); 393 addr.insertAddress( address );
394 } 394 }
395 395
396 // AGENT 396 // AGENT
397 else if ( identifier == "agent" ) 397 else if ( identifier == "agent" )
398 addr.setAgent( parseAgent( *lineIt ) ); 398 addr.setAgent( parseAgent( *lineIt ) );
399 399
400 // BDAY 400 // BDAY
401 else if ( identifier == "bday" ) 401 else if ( identifier == "bday" )
402 addr.setBirthday( parseDateTime( (*lineIt).valueString() ) ); 402 addr.setBirthday( parseDateTime( (*lineIt).valueString() ) );
403 403
404 // CATEGORIES 404 // CATEGORIES
405 else if ( identifier == "categories" ) { 405 else if ( identifier == "categories" ) {
406 const QStringList categories = splitString( commaSep, (*lineIt).valueString() ); 406 const QStringList categories = splitString( commaSep, (*lineIt).valueString() );
407 addr.setCategories( categories ); 407 addr.setCategories( categories );
408 } 408 }
409 409
410 // CLASS 410 // CLASS
411 else if ( identifier == "class" ) 411 else if ( identifier == "class" )
412 addr.setSecrecy( parseSecrecy( *lineIt ) ); 412 addr.setSecrecy( parseSecrecy( *lineIt ) );
413 413
414 // EMAIL 414 // EMAIL
415 else if ( identifier == "email" ) { 415 else if ( identifier == "email" ) {
416 const QStringList types = (*lineIt).parameters( "type" ); 416 const QStringList types = (*lineIt).parameters( "type" );
417 addr.insertEmail( (*lineIt).valueString(), types.findIndex( "PREF" ) != -1 ); 417 addr.insertEmail( (*lineIt).valueString(), types.findIndex( "PREF" ) != -1 );
418 } 418 }
419 419
420 // FN 420 // FN
421 else if ( identifier == "fn" ) 421 else if ( identifier == "fn" )
422 addr.setFormattedName( (*lineIt).valueString() ); 422 addr.setFormattedName( (*lineIt).valueString() );
423 423
424 // GEO 424 // GEO
425 else if ( identifier == "geo" ) { 425 else if ( identifier == "geo" ) {
426 Geo geo; 426 Geo geo;
427 427
428 const QStringList geoParts = QStringList::split( ';', (*lineIt).valueString(), true ); 428 const QStringList geoParts = QStringList::split( ';', (*lineIt).valueString(), true );
429 geo.setLatitude( geoParts[ 0 ].toFloat() ); 429 geo.setLatitude( geoParts[ 0 ].toFloat() );
430 geo.setLongitude( geoParts[ 1 ].toFloat() ); 430 geo.setLongitude( geoParts[ 1 ].toFloat() );
431 431
432 addr.setGeo( geo ); 432 addr.setGeo( geo );
433 } 433 }
434 434
435 // KEY 435 // KEY
436 else if ( identifier == "key" ) 436 else if ( identifier == "key" )
437 addr.insertKey( parseKey( *lineIt ) ); 437 addr.insertKey( parseKey( *lineIt ) );
438 438
439 // LABEL 439 // LABEL
440 else if ( identifier == "label" ) { 440 else if ( identifier == "label" ) {
441 int type = 0; 441 int type = 0;
442 442
443 const QStringList types = (*lineIt).parameters( "type" ); 443 const QStringList types = (*lineIt).parameters( "type" );
444 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) 444 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
445 type += mAddressTypeMap[ (*it).lower() ]; 445 type += mAddressTypeMap[ (*it).lower() ];
446 446
447 bool available = false; 447 bool available = false;
448 KABC::Address::List addressList = addr.addresses(); 448 KABC::Address::List addressList = addr.addresses();
449 KABC::Address::List::Iterator it; 449 KABC::Address::List::Iterator it;
450 for ( it = addressList.begin(); it != addressList.end(); ++it ) { 450 for ( it = addressList.begin(); it != addressList.end(); ++it ) {
451 if ( (*it).type() == type ) { 451 if ( (*it).type() == type ) {
452 (*it).setLabel( (*lineIt).valueString() ); 452 (*it).setLabel( (*lineIt).valueString() );
453 addr.insertAddress( *it ); 453 addr.insertAddress( *it );
454 available = true; 454 available = true;
455 break; 455 break;
456 } 456 }
457 } 457 }
458 458
459 if ( !available ) { // a standalone LABEL tag 459 if ( !available ) { // a standalone LABEL tag
460 KABC::Address address( type ); 460 KABC::Address address( type );
461 address.setLabel( (*lineIt).valueString() ); 461 address.setLabel( (*lineIt).valueString() );
462 addr.insertAddress( address ); 462 addr.insertAddress( address );
463 } 463 }
464 } 464 }
465 465
466 // LOGO 466 // LOGO
467 else if ( identifier == "logo" ) 467 else if ( identifier == "logo" )
468 addr.setLogo( parsePicture( *lineIt ) ); 468 addr.setLogo( parsePicture( *lineIt ) );
469 469
470 // MAILER 470 // MAILER
471 else if ( identifier == "mailer" ) 471 else if ( identifier == "mailer" )
472 addr.setMailer( (*lineIt).valueString() ); 472 addr.setMailer( (*lineIt).valueString() );
473 473
474 // N 474 // N
475 else if ( identifier == "n" ) { 475 else if ( identifier == "n" ) {
476 const QStringList nameParts = splitString( semicolonSep, (*lineIt).valueString() ); 476 const QStringList nameParts = splitString( semicolonSep, (*lineIt).valueString() );
477 if ( nameParts.count() > 0 ) 477 if ( nameParts.count() > 0 )
478 addr.setFamilyName( nameParts[ 0 ] ); 478 addr.setFamilyName( nameParts[ 0 ] );
479 if ( nameParts.count() > 1 ) 479 if ( nameParts.count() > 1 )
480 addr.setGivenName( nameParts[ 1 ] ); 480 addr.setGivenName( nameParts[ 1 ] );
481 if ( nameParts.count() > 2 ) 481 if ( nameParts.count() > 2 )
482 addr.setAdditionalName( nameParts[ 2 ] ); 482 addr.setAdditionalName( nameParts[ 2 ] );
483 if ( nameParts.count() > 3 ) 483 if ( nameParts.count() > 3 )
484 addr.setPrefix( nameParts[ 3 ] ); 484 addr.setPrefix( nameParts[ 3 ] );
485 if ( nameParts.count() > 4 ) 485 if ( nameParts.count() > 4 )
486 addr.setSuffix( nameParts[ 4 ] ); 486 addr.setSuffix( nameParts[ 4 ] );
487 } 487 }
488 488
489 // NAME 489 // NAME
490 else if ( identifier == "name" ) 490 else if ( identifier == "name" )
491 addr.setName( (*lineIt).valueString() ); 491 addr.setName( (*lineIt).valueString() );
492 492
493 // NICKNAME 493 // NICKNAME
494 else if ( identifier == "nickname" ) 494 else if ( identifier == "nickname" )
495 addr.setNickName( (*lineIt).valueString() ); 495 addr.setNickName( (*lineIt).valueString() );
496 496
497 // NOTE 497 // NOTE
498 else if ( identifier == "note" ) 498 else if ( identifier == "note" )
499 addr.setNote( (*lineIt).valueString() ); 499 addr.setNote( (*lineIt).valueString() );
500 500
501 // ORGANIZATION 501 // ORGANIZATION
502 else if ( identifier == "org" ) 502 else if ( identifier == "org" )
503 addr.setOrganization( (*lineIt).valueString() ); 503 addr.setOrganization( (*lineIt).valueString() );
504 504
505 // PHOTO 505 // PHOTO
506 else if ( identifier == "photo" ) 506 else if ( identifier == "photo" )
507 addr.setPhoto( parsePicture( *lineIt ) ); 507 addr.setPhoto( parsePicture( *lineIt ) );
508 508
509 // PROID 509 // PROID
510 else if ( identifier == "prodid" ) 510 else if ( identifier == "prodid" )
511 addr.setProductId( (*lineIt).valueString() ); 511 addr.setProductId( (*lineIt).valueString() );
512 512
513 // REV 513 // REV
514 else if ( identifier == "rev" ) 514 else if ( identifier == "rev" )
515 addr.setRevision( parseDateTime( (*lineIt).valueString() ) ); 515 addr.setRevision( parseDateTime( (*lineIt).valueString() ) );
516 516
517 // ROLE 517 // ROLE
518 else if ( identifier == "role" ) 518 else if ( identifier == "role" )
519 addr.setRole( (*lineIt).valueString() ); 519 addr.setRole( (*lineIt).valueString() );
520 520
521 // SORT-STRING 521 // SORT-STRING
522 else if ( identifier == "sort-string" ) 522 else if ( identifier == "sort-string" )
523 addr.setSortString( (*lineIt).valueString() ); 523 addr.setSortString( (*lineIt).valueString() );
524 524
525 // SOUND 525 // SOUND
526 else if ( identifier == "sound" ) 526 else if ( identifier == "sound" )
527 addr.setSound( parseSound( *lineIt ) ); 527 addr.setSound( parseSound( *lineIt ) );
528 528
529 // TEL 529 // TEL
530 else if ( identifier == "tel" ) { 530 else if ( identifier == "tel" ) {
531 PhoneNumber phone; 531 PhoneNumber phone;
532 phone.setNumber( (*lineIt).valueString() ); 532 phone.setNumber( (*lineIt).valueString() );
533 533
534 int type = 0; 534 int type = 0;
535 535
536 const QStringList types = (*lineIt).parameters( "type" ); 536 const QStringList types = (*lineIt).parameters( "type" );
537 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) 537 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
538 type += mPhoneTypeMap[(*it).upper()]; 538 type += mPhoneTypeMap[(*it).upper()];
539 539
540 phone.setType( type ); 540 phone.setType( type );
541 541
542 addr.insertPhoneNumber( phone ); 542 addr.insertPhoneNumber( phone );
543 } 543 }
544 544
545 // TITLE 545 // TITLE
546 else if ( identifier == "title" ) 546 else if ( identifier == "title" )
547 addr.setTitle( (*lineIt).valueString() ); 547 addr.setTitle( (*lineIt).valueString() );
548 548
549 // TZ 549 // TZ
550 else if ( identifier == "tz" ) { 550 else if ( identifier == "tz" ) {
551 TimeZone tz; 551 TimeZone tz;
552 const QString date = (*lineIt).valueString(); 552 const QString date = (*lineIt).valueString();
553 553
554 int hours = date.mid( 1, 2).toInt(); 554 int hours = date.mid( 1, 2).toInt();
555 int minutes = date.mid( 4, 2 ).toInt(); 555 int minutes = date.mid( 4, 2 ).toInt();
556 int offset = ( hours * 60 ) + minutes; 556 int offset = ( hours * 60 ) + minutes;
557 offset = offset * ( date[ 0 ] == '+' ? 1 : -1 ); 557 offset = offset * ( date[ 0 ] == '+' ? 1 : -1 );
558 558
559 tz.setOffset( offset ); 559 tz.setOffset( offset );
560 addr.setTimeZone( tz ); 560 addr.setTimeZone( tz );
561 } 561 }
562 562
563 // UID 563 // UID
564 else if ( identifier == "uid" ) 564 else if ( identifier == "uid" )
565 addr.setUid( (*lineIt).valueString() ); 565 addr.setUid( (*lineIt).valueString() );
566 566
567 // URL 567 // URL
568 else if ( identifier == "url" ) 568 else if ( identifier == "url" )
569 addr.setUrl( KURL( (*lineIt).valueString() ) ); 569 addr.setUrl( KURL( (*lineIt).valueString() ) );
570 570
571 // X- 571 // X-
572 else if ( identifier.startsWith( "x-" ) ) { 572 else if ( identifier.startsWith( "x-" ) ) {
573 const QString key = (*lineIt).identifier().mid( 2 ); 573 const QString key = (*lineIt).identifier().mid( 2 );
574 int dash = key.find( "-" ); 574 int dash = key.find( "-" );
575 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).valueString() ); 575 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).valueString() );
576 } 576 }
577 } 577 }
578 } 578 }
579 579 addr.makePhoneNumbersOLcompatible();
580 addrList.append( addr ); 580 addrList.append( addr );
581 } 581 }
582 582
583 return addrList; 583 return addrList;
584} 584}
585 585
586QDateTime VCardTool::parseDateTime( const QString &str ) 586QDateTime VCardTool::parseDateTime( const QString &str )
587{ 587{
588 QDateTime dateTime; 588 QDateTime dateTime;
589 589
590 if ( str.find( '-' ) == -1 ) { // is base format (yyyymmdd) 590 if ( str.find( '-' ) == -1 ) { // is base format (yyyymmdd)
591 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(), 591 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
592 str.mid( 6, 2 ).toInt() ) ); 592 str.mid( 6, 2 ).toInt() ) );
593 593
594 if ( str.find( 'T' ) ) // has time information yyyymmddThh:mm:ss 594 if ( str.find( 'T' ) ) // has time information yyyymmddThh:mm:ss
595 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(), 595 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
596 str.mid( 17, 2 ).toInt() ) ); 596 str.mid( 17, 2 ).toInt() ) );
597 597
598 } else { // is extended format yyyy-mm-dd 598 } else { // is extended format yyyy-mm-dd
599 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(), 599 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
600 str.mid( 8, 2 ).toInt() ) ); 600 str.mid( 8, 2 ).toInt() ) );
601 601
602 if ( str.find( 'T' ) ) // has time information yyyy-mm-ddThh:mm:ss 602 if ( str.find( 'T' ) ) // has time information yyyy-mm-ddThh:mm:ss
603 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(), 603 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
604 str.mid( 17, 2 ).toInt() ) ); 604 str.mid( 17, 2 ).toInt() ) );
605 } 605 }
606 606
607 return dateTime; 607 return dateTime;
608} 608}
609 609
610QString VCardTool::createDateTime( const QDateTime &dateTime ) 610QString VCardTool::createDateTime( const QDateTime &dateTime )
611{ 611{
612 QString str; 612 QString str;
613 613
614 if ( dateTime.date().isValid() ) { 614 if ( dateTime.date().isValid() ) {
615 str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(), 615 str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
616 dateTime.date().day() ); 616 dateTime.date().day() );
617 if ( dateTime.time().isValid() ) { 617 if ( dateTime.time().isValid() ) {
618 QString tmp; 618 QString tmp;
619 tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(), 619 tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
620 dateTime.time().second() ); 620 dateTime.time().second() );
621 str += tmp; 621 str += tmp;
622 } 622 }
623 } 623 }
624 624
625 return str; 625 return str;
626} 626}
627 627
628Picture VCardTool::parsePicture( const VCardLine &line ) 628Picture VCardTool::parsePicture( const VCardLine &line )
629{ 629{
630 Picture pic; 630 Picture pic;
631 631
632 const QStringList params = line.parameterList(); 632 const QStringList params = line.parameterList();
633 if ( params.findIndex( "encoding" ) != -1 ) { 633 if ( params.findIndex( "encoding" ) != -1 ) {
634 QImage img; 634 QImage img;
635 img.loadFromData( line.valueBytes() ); 635 img.loadFromData( line.valueBytes() );
636 pic.setData( img ); 636 pic.setData( img );
637 } else if ( params.findIndex( "value" ) != -1 ) { 637 } else if ( params.findIndex( "value" ) != -1 ) {
638 if ( line.parameter( "value" ).lower() == "uri" ) 638 if ( line.parameter( "value" ).lower() == "uri" )
639 pic.setUrl( line.valueString() ); 639 pic.setUrl( line.valueString() );
640 } 640 }
641 641
642 if ( params.findIndex( "type" ) != -1 ) 642 if ( params.findIndex( "type" ) != -1 )
643 pic.setType( line.parameter( "type" ) ); 643 pic.setType( line.parameter( "type" ) );
644 644
645 return pic; 645 return pic;
646} 646}
647 647
648VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic ) 648VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic )
649{ 649{
650 VCardLine line( identifier ); 650 VCardLine line( identifier );
651 651
652 if ( pic.isIntern() ) { 652 if ( pic.isIntern() ) {
653 if ( !pic.data().isNull() ) { 653 if ( !pic.data().isNull() ) {
654 QByteArray input; 654 QByteArray input;
655 QDataStream s( input, IO_WriteOnly ); 655 QDataStream s( input, IO_WriteOnly );
656 s.setVersion( 4 ); 656 s.setVersion( 4 );
657 s << pic.data(); 657 s << pic.data();
658 line.setValueBytes( input ); 658 line.setValueBytes( input );
659 line.addParameter( "encoding", "b" ); 659 line.addParameter( "encoding", "b" );
660 line.addParameter( "type", "image/png" ); 660 line.addParameter( "type", "image/png" );
661 } 661 }
662 } else if ( !pic.url().isEmpty() ) { 662 } else if ( !pic.url().isEmpty() ) {
663 QByteArray ba; 663 QByteArray ba;
664 line.setValueString( pic.url() ); 664 line.setValueString( pic.url() );
665 line.addParameter( "value", "URI" ); 665 line.addParameter( "value", "URI" );
666 } 666 }
667 667
668 return line; 668 return line;
669} 669}
670 670
671Sound VCardTool::parseSound( const VCardLine &line ) 671Sound VCardTool::parseSound( const VCardLine &line )
672{ 672{
673 Sound snd; 673 Sound snd;
674 674
675 const QStringList params = line.parameterList(); 675 const QStringList params = line.parameterList();
676 if ( params.findIndex( "encoding" ) != -1 ) 676 if ( params.findIndex( "encoding" ) != -1 )
677 snd.setData( line.valueBytes() ); 677 snd.setData( line.valueBytes() );
678 else if ( params.findIndex( "value" ) != -1 ) { 678 else if ( params.findIndex( "value" ) != -1 ) {
679 if ( line.parameter( "value" ).lower() == "uri" ) 679 if ( line.parameter( "value" ).lower() == "uri" )
680 snd.setUrl( line.valueString() ); 680 snd.setUrl( line.valueString() );
681 } 681 }
682 682
683/* TODO: support sound types 683/* TODO: support sound types
684 if ( params.contains( "type" ) ) 684 if ( params.contains( "type" ) )
685 snd.setType( line.parameter( "type" ) ); 685 snd.setType( line.parameter( "type" ) );
686*/ 686*/
687 687
688 return snd; 688 return snd;
689} 689}
690 690
691VCardLine VCardTool::createSound( const Sound &snd ) 691VCardLine VCardTool::createSound( const Sound &snd )
692{ 692{
693 VCardLine line( "SOUND" ); 693 VCardLine line( "SOUND" );
694 694
695 if ( snd.isIntern() ) { 695 if ( snd.isIntern() ) {
696 if ( !snd.data().isEmpty() ) { 696 if ( !snd.data().isEmpty() ) {
697 line.setValueBytes( snd.data() ); 697 line.setValueBytes( snd.data() );
698 line.addParameter( "encoding", "b" ); 698 line.addParameter( "encoding", "b" );
699 // TODO: need to store sound type!!! 699 // TODO: need to store sound type!!!
700 } 700 }
701 } else if ( !snd.url().isEmpty() ) { 701 } else if ( !snd.url().isEmpty() ) {
702 line.setValueString( snd.url() ); 702 line.setValueString( snd.url() );
703 line.addParameter( "value", "URI" ); 703 line.addParameter( "value", "URI" );
704 } 704 }
705 705
706 return line; 706 return line;
707} 707}
708 708
709Key VCardTool::parseKey( const VCardLine &line ) 709Key VCardTool::parseKey( const VCardLine &line )
710{ 710{
711 Key key; 711 Key key;
712 712
713 const QStringList params = line.parameterList(); 713 const QStringList params = line.parameterList();
714 if ( params.findIndex( "encoding" ) != -1 ) 714 if ( params.findIndex( "encoding" ) != -1 )
715 key.setBinaryData( line.valueBytes() ); 715 key.setBinaryData( line.valueBytes() );
716 else 716 else
717 key.setTextData( line.valueString() ); 717 key.setTextData( line.valueString() );
718 718
719 if ( params.findIndex( "type" ) != -1 ) { 719 if ( params.findIndex( "type" ) != -1 ) {
720 if ( line.parameter( "type" ).lower() == "x509" ) 720 if ( line.parameter( "type" ).lower() == "x509" )
721 key.setType( Key::X509 ); 721 key.setType( Key::X509 );
722 else if ( line.parameter( "type" ).lower() == "pgp" ) 722 else if ( line.parameter( "type" ).lower() == "pgp" )
723 key.setType( Key::PGP ); 723 key.setType( Key::PGP );
724 else { 724 else {
725 key.setType( Key::Custom ); 725 key.setType( Key::Custom );
726 key.setCustomTypeString( line.parameter( "type" ) ); 726 key.setCustomTypeString( line.parameter( "type" ) );
727 } 727 }
728 } 728 }
729 729
730 return key; 730 return key;
731} 731}
732 732
733VCardLine VCardTool::createKey( const Key &key ) 733VCardLine VCardTool::createKey( const Key &key )
734{ 734{
735 VCardLine line( "KEY" ); 735 VCardLine line( "KEY" );
736 736
737 if ( key.isBinary() ) { 737 if ( key.isBinary() ) {
738 if ( !key.binaryData().isEmpty() ) { 738 if ( !key.binaryData().isEmpty() ) {
739 line.setValueBytes( key.binaryData() ); 739 line.setValueBytes( key.binaryData() );
740 line.addParameter( "encoding", "b" ); 740 line.addParameter( "encoding", "b" );
741 } 741 }
742 } else if ( !key.textData().isEmpty() ) 742 } else if ( !key.textData().isEmpty() )
743 line.setValueString( key.textData() ); 743 line.setValueString( key.textData() );
744 744
745 if ( key.type() == Key::X509 ) 745 if ( key.type() == Key::X509 )
746 line.addParameter( "type", "X509" ); 746 line.addParameter( "type", "X509" );
747 else if ( key.type() == Key::PGP ) 747 else if ( key.type() == Key::PGP )
748 line.addParameter( "type", "PGP" ); 748 line.addParameter( "type", "PGP" );
749 else if ( key.type() == Key::Custom ) 749 else if ( key.type() == Key::Custom )
750 line.addParameter( "type", key.customTypeString() ); 750 line.addParameter( "type", key.customTypeString() );
751 751
752 return line; 752 return line;
753} 753}
754 754
755Secrecy VCardTool::parseSecrecy( const VCardLine &line ) 755Secrecy VCardTool::parseSecrecy( const VCardLine &line )
756{ 756{
757 Secrecy secrecy; 757 Secrecy secrecy;
758 758
759 if ( line.valueString().lower() == "public" ) 759 if ( line.valueString().lower() == "public" )
760 secrecy.setType( Secrecy::Public ); 760 secrecy.setType( Secrecy::Public );
761 if ( line.valueString().lower() == "private" ) 761 if ( line.valueString().lower() == "private" )
762 secrecy.setType( Secrecy::Private ); 762 secrecy.setType( Secrecy::Private );
763 if ( line.valueString().lower() == "confidential" ) 763 if ( line.valueString().lower() == "confidential" )
764 secrecy.setType( Secrecy::Confidential ); 764 secrecy.setType( Secrecy::Confidential );
765 765
766 return secrecy; 766 return secrecy;
767} 767}
768 768
769VCardLine VCardTool::createSecrecy( const Secrecy &secrecy ) 769VCardLine VCardTool::createSecrecy( const Secrecy &secrecy )
770{ 770{
771 VCardLine line( "CLASS" ); 771 VCardLine line( "CLASS" );
772 772
773 int type = secrecy.type(); 773 int type = secrecy.type();
774 774
775 if ( type == Secrecy::Public ) 775 if ( type == Secrecy::Public )
776 line.setValueString( "PUBLIC" ); 776 line.setValueString( "PUBLIC" );
777 else if ( type == Secrecy::Private ) 777 else if ( type == Secrecy::Private )
778 line.setValueString( "PRIVATE" ); 778 line.setValueString( "PRIVATE" );
779 else if ( type == Secrecy::Confidential ) 779 else if ( type == Secrecy::Confidential )
780 line.setValueString( "CONFIDENTIAL" ); 780 line.setValueString( "CONFIDENTIAL" );
781 781
782 return line; 782 return line;
783} 783}
784 784
785Agent VCardTool::parseAgent( const VCardLine &line ) 785Agent VCardTool::parseAgent( const VCardLine &line )
786{ 786{
787 Agent agent; 787 Agent agent;
788 788
789 const QStringList params = line.parameterList(); 789 const QStringList params = line.parameterList();
790 if ( params.findIndex( "value" ) != -1 ) { 790 if ( params.findIndex( "value" ) != -1 ) {
791 if ( line.parameter( "value" ).lower() == "uri" ) 791 if ( line.parameter( "value" ).lower() == "uri" )
792 agent.setUrl( line.valueString() ); 792 agent.setUrl( line.valueString() );
793 } else { 793 } else {
794 QString str = line.valueString(); 794 QString str = line.valueString();
795 str.replace( QRegExp("\\\\n"), "\r\n" ); 795 str.replace( QRegExp("\\\\n"), "\r\n" );
796 str.replace( QRegExp("\\\\N"), "\r\n" ); 796 str.replace( QRegExp("\\\\N"), "\r\n" );
797 str.replace( QRegExp("\\\\;"), ";" ); 797 str.replace( QRegExp("\\\\;"), ";" );
798 str.replace( QRegExp("\\\\:"), ":" ); 798 str.replace( QRegExp("\\\\:"), ":" );
799 str.replace( QRegExp("\\\\,"), "," ); 799 str.replace( QRegExp("\\\\,"), "," );
800 800
801 const Addressee::List list = parseVCards( str ); 801 const Addressee::List list = parseVCards( str );
802 if ( list.count() > 0 ) { 802 if ( list.count() > 0 ) {
803 Addressee *addr = new Addressee; 803 Addressee *addr = new Addressee;
804 *addr = list[ 0 ]; 804 *addr = list[ 0 ];
805 agent.setAddressee( addr ); 805 agent.setAddressee( addr );
806 } 806 }
807 } 807 }
808 808
809 return agent; 809 return agent;
810} 810}
811 811
812VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent ) 812VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent )
813{ 813{
814 VCardLine line( "AGENT" ); 814 VCardLine line( "AGENT" );
815 815
816 if ( agent.isIntern() ) { 816 if ( agent.isIntern() ) {
817 if ( agent.addressee() != 0 ) { 817 if ( agent.addressee() != 0 ) {
818 Addressee::List list; 818 Addressee::List list;
819 list.append( *agent.addressee() ); 819 list.append( *agent.addressee() );
820 820
821 QString str = createVCards( list, version ); 821 QString str = createVCards( list, version );
822 str.replace( QRegExp("\\r\\n"), "\\n" ); 822 str.replace( QRegExp("\\r\\n"), "\\n" );
823 str.replace( QRegExp(";"), "\\;" ); 823 str.replace( QRegExp(";"), "\\;" );
824 str.replace( QRegExp(":"), "\\:" ); 824 str.replace( QRegExp(":"), "\\:" );
825 str.replace( QRegExp(","), "\\," ); 825 str.replace( QRegExp(","), "\\," );
826 line.setValueString( str ); 826 line.setValueString( str );
827 } 827 }
828 } else if ( !agent.url().isEmpty() ) { 828 } else if ( !agent.url().isEmpty() ) {
829 line.setValueString( agent.url() ); 829 line.setValueString( agent.url() );
830 line.addParameter( "value", "URI" ); 830 line.addParameter( "value", "URI" );
831 } 831 }
832 832
833 return line; 833 return line;
834} 834}
835 835
836QStringList VCardTool::splitString( const QChar &sep, const QString &str ) 836QStringList VCardTool::splitString( const QChar &sep, const QString &str )
837{ 837{
838 QStringList list; 838 QStringList list;
839 QString value( str ); 839 QString value( str );
840 840
841 int start = 0; 841 int start = 0;
842 int pos = value.find( sep, start ); 842 int pos = value.find( sep, start );
843 843
844 while ( pos != -1 ) { 844 while ( pos != -1 ) {
845 if ( value[ pos - 1 ] != '\\' ) { 845 if ( value[ pos - 1 ] != '\\' ) {
846 if ( pos > start && pos <= (int)value.length() ) 846 if ( pos > start && pos <= (int)value.length() )
847 list << value.mid( start, pos - start ); 847 list << value.mid( start, pos - start );
848 else 848 else
849 list << QString::null; 849 list << QString::null;
850 850
851 start = pos + 1; 851 start = pos + 1;
852 pos = value.find( sep, start ); 852 pos = value.find( sep, start );
853 } else { 853 } else {
854 if ( pos != 0 ) { 854 if ( pos != 0 ) {
855 value.replace( pos - 1, 2, sep ); 855 value.replace( pos - 1, 2, sep );
856 pos = value.find( sep, pos ); 856 pos = value.find( sep, pos );
857 } else 857 } else
858 pos = value.find( sep, pos + 1 ); 858 pos = value.find( sep, pos + 1 );
859 } 859 }
860 } 860 }
861 861
862 int l = value.length() - 1; 862 int l = value.length() - 1;
863 if ( value.mid( start, l - start + 1 ).length() > 0 ) 863 if ( value.mid( start, l - start + 1 ).length() > 0 )
864 list << value.mid( start, l - start + 1 ); 864 list << value.mid( start, l - start + 1 );
865 else 865 else
866 list << QString::null; 866 list << QString::null;
867 867
868 return list; 868 return list;
869} 869}