• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kabc
 

kabc

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

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kabc

Skip menu "kabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kabc by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |