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