Diffstat (limited to 'kabc/vcardparser/vcardparser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/vcardparser/vcardparser.cpp | 233 |
1 files changed, 233 insertions, 0 deletions
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 | |||
29 | using namespace KABC; | ||
30 | |||
31 | VCardParser::VCardParser() | ||
32 | { | ||
33 | } | ||
34 | |||
35 | VCardParser::~VCardParser() | ||
36 | { | ||
37 | } | ||
38 | |||
39 | VCard::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 | |||
148 | QString 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 | } | ||