1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 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 "vcardconverter.h"
22#include "vcardtool.h"
23
24using namespace KABC;
25
26VCardConverter::VCardConverter()
27 : d( 0 )
28{
29}
30
31VCardConverter::~VCardConverter()
32{
33}
34
35QByteArray VCardConverter::exportVCard( const Addressee &addr, Version version ) const
36{
37 Addressee::List list;
38 list.append( addr );
39
40 return exportVCards( list, version );
41}
42
43QByteArray VCardConverter::exportVCards( const Addressee::List &list, Version version ) const
44{
45 VCardTool tool;
46 QByteArray returnValue;
47 switch (version) {
48 case v2_1:
49 returnValue = tool.exportVCards( list, VCard::v2_1 );
50 break;
51 case v3_0:
52 returnValue = tool.exportVCards( list, VCard::v3_0 );
53 break;
54 case v4_0:
55 returnValue = tool.exportVCards( list, VCard::v4_0 );
56 break;
57 }
58
59 return returnValue;
60}
61
62QByteArray VCardConverter::createVCard( const Addressee &addr, Version version ) const
63{
64 Addressee::List list;
65 list.append( addr );
66
67 return createVCards( list, version );
68}
69
70QByteArray VCardConverter::createVCards( Addressee::List list, Version version ) const
71{
72 VCardTool tool;
73 QByteArray returnValue;
74 switch (version) {
75 case v2_1:
76 returnValue = tool.createVCards( list, VCard::v2_1 );
77 break;
78 case v3_0:
79 returnValue = tool.createVCards( list, VCard::v3_0 );
80 break;
81 case v4_0:
82 returnValue = tool.createVCards( list, VCard::v4_0 );
83 break;
84 }
85
86 return returnValue;
87}
88
89Addressee VCardConverter::parseVCard( const QByteArray &vcard ) const
90{
91 Addressee::List list = parseVCards( vcard );
92
93 return list.isEmpty() ? Addressee() : list[ 0 ];
94}
95
96Addressee::List VCardConverter::parseVCards( const QByteArray &vcard ) const
97{
98 VCardTool tool;
99
100 return tool.parseVCards( vcard );
101}
102
103/* Helper functions */
104
105QString KABC::dateToVCardString( const QDateTime &dateTime )
106{
107 return dateTime.toString( QLatin1String( "yyyyMMddThhmmssZ" ) );
108}
109
110QString KABC::dateToVCardString( const QDate &date )
111{
112 return date.toString( QLatin1String( "yyyyMMdd" ) );
113}
114
115QDateTime KABC::VCardStringToDate( const QString &dateString )
116{
117 QDate date;
118 QTime time;
119 QString d( dateString );
120
121 d = d.remove( QLatin1Char( '-' ) ).remove( QLatin1Char( ':' ) );
122
123 if ( d.length() >= 8 ) {
124 date = QDate( d.mid( 0, 4 ).toUInt(), d.mid( 4, 2 ).toUInt(), d.mid( 6, 2 ).toUInt() );
125 }
126
127 if ( d.length() > 9 && d[ 8 ].toUpper() == QLatin1Char( 'T' ) ) {
128 time = QTime( d.mid( 9, 2 ).toUInt(), d.mid( 11, 2 ).toUInt(), d.mid( 13, 2 ).toUInt() );
129 }
130
131 return QDateTime( date, time );
132}
133