• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

kioslave/imap4

  • kioslave
  • imap4
mailaddress.cpp
1/**********************************************************************
2 *
3 * mailaddress.cc - mail address parser
4 * Copyright (C) 2000 Sven Carstens <s.carstens@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * Send comments and bug fixes to
21 *
22 *********************************************************************/
23
24#include "mailaddress.h"
25#include "mimehdrline.h"
26#include <kimap/rfccodecs.h>
27#include <kmime/kmime_util.h>
28#include <QByteArray>
29
30using namespace KIMAP;
31
32mailAddress::mailAddress ()
33{
34}
35
36mailAddress::mailAddress (const mailAddress & lr):
37user (lr.user),
38host (lr.host),
39rawFullName (lr.rawFullName),
40rawComment (lr.rawComment)
41{
42// kDebug(7116) <<"mailAddress::mailAddress -" << getStr();
43}
44
45mailAddress & mailAddress::operator = (const mailAddress & lr)
46{
47 // Avoid a = a.
48 if ( this == &lr ) {
49 return *this;
50 }
51
52 user = lr.user;
53 host = lr.host;
54 rawFullName = lr.rawFullName;
55 rawComment = lr.rawComment;
56
57// kDebug(7116) << "mailAddress::operator= -" << getStr();
58
59 return *this;
60}
61
62mailAddress::~mailAddress ()
63{
64}
65
66mailAddress::mailAddress (char *aCStr)
67{
68 parseAddress( aCStr );
69}
70
71int mailAddress::parseAddress (const char *aCStr)
72{
73 int retVal = 0;
74 int skip;
75 uint len;
76 int pt;
77
78 if ( aCStr ) {
79 //skip leading white space
80 skip = mimeHdrLine::skipWS( aCStr );
81 if ( skip > 0 ) {
82 aCStr += skip;
83 retVal += skip;
84 }
85 while ( *aCStr ) {
86 int advance;
87
88 switch ( *aCStr ) {
89 case '"':
90 advance = mimeHdrLine::parseQuoted( '"', '"', aCStr );
91 rawFullName += QByteArray( aCStr, advance );
92 break;
93 case '(':
94 advance = mimeHdrLine::parseQuoted( '(', ')', aCStr );
95 rawComment += QByteArray( aCStr, advance );
96 break;
97 case '<':
98 advance = mimeHdrLine::parseQuoted( '<', '>', aCStr );
99 user = QByteArray( aCStr, advance ); // copy it
100 len = advance;
101 user = user.mid ( 1, len - 2 ); // strip <>
102 len -= 2;
103 pt = user.indexOf( '@' );
104 host = user.right( len - pt - 1 ); // split it into host
105 user.truncate( pt ); // and user
106 break;
107 default:
108 advance = mimeHdrLine::parseWord( aCStr );
109 //if we've seen a FQ mailname the rest must be quoted or is just junk
110 if ( user.isEmpty() ) {
111 if ( *aCStr != ',' ) {
112 rawFullName += aCStr;
113 if ( mimeHdrLine::skipWS( aCStr+advance ) > 0 ) {
114 rawFullName += ' ';
115 }
116 }
117 }
118 break;
119 }
120 if ( advance ) {
121 retVal += advance;
122 aCStr += advance;
123 } else {
124 break;
125 }
126 advance = mimeHdrLine::skipWS( aCStr );
127 if ( advance > 0 ) {
128 retVal += advance;
129 aCStr += advance;
130 }
131 //reached end of current address
132 if ( *aCStr == ',' ) {
133 advance++;
134 break;
135 }
136 }
137 //let's see what we've got
138 if ( rawFullName.isEmpty() ) {
139 if ( user.isEmpty() ) {
140 retVal = 0;
141 } else {
142 if ( host.isEmpty() ) {
143 rawFullName = user;
144 user.truncate( 0 );
145 }
146 }
147 } else if ( user.isEmpty() ) {
148 pt = rawFullName.indexOf( '@' );
149 if ( pt >= 0 ) {
150 user = rawFullName;
151 host = user.right( user.length() - pt - 1 );
152 user.truncate( pt );
153 rawFullName.truncate( 0 );
154 }
155 }
156
157#if 0
158// dead
159 if ( !rawFullName.isEmpty() ) {
160// if( fullName[0] == '"' ) {
161// fullName = fullName.mid( 1, fullName.length() - 2 );
162// }
163// fullName = fullName.simplified().trimmed();
164// fullName = KIMAP::decodeRFC2047String( fullName.ascii() );
165 }
166#endif
167 if ( !rawComment.isEmpty() ) {
168 if ( rawComment[0] == '(' ) {
169 rawComment = rawComment.mid( 1, rawComment.length() - 2 );
170 }
171 rawComment = rawComment.trimmed();
172// comment = KIMAP::decodeRFC2047String( comment.ascii() );
173 }
174 } else {
175 //debug();
176 }
177 return retVal;
178}
179
180const QByteArray mailAddress::getStr () const
181{
182 QByteArray retVal;
183 retVal.reserve( 128 ); // Should be generally big enough
184
185 if ( !rawFullName.isEmpty() ) {
186 QByteArray tmpName( rawFullName );
187 KMime::addQuotes( tmpName, false );
188 retVal = tmpName + ' ';
189 }
190 if ( !user.isEmpty() ) {
191 retVal += '<';
192 retVal += user;
193 if ( !host.isEmpty() ) {
194 retVal += '@';
195 retVal += host;
196 }
197 retVal += '>';
198 }
199 if ( !rawComment.isEmpty() ) {
200 retVal += " (" + rawComment + ')';
201 }
202 //kDebug() << retVal;
203 return retVal;
204}
205
206bool mailAddress::isEmpty () const
207{
208 return user.isEmpty();
209}
210
211void mailAddress::setFullName (const QString & _str)
212{
213 rawFullName = KIMAP::encodeRFC2047String( _str ).toLatin1();
214}
215
216const QString mailAddress::getFullName () const
217{
218 return KIMAP::decodeRFC2047String( rawFullName );
219}
220
221void mailAddress::setCommentRaw (const QByteArray & _str)
222{
223 rawComment = _str;
224}
225
226void mailAddress::setComment (const QString & _str)
227{
228 rawComment = KIMAP::encodeRFC2047String( _str ).toLatin1();
229}
230
231const QString mailAddress::getComment () const
232{
233 return KIMAP::decodeRFC2047String( rawComment );
234}
235
236const QByteArray & mailAddress::getCommentRaw () const
237{
238 return rawComment;
239}
240
241QString mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr)
242{
243 QString retVal;
244 if ( !adr.getFullName().isEmpty() ) {
245 // should do some umlaut escaping
246 retVal += adr.getFullName() + ' ';
247 }
248 if ( !adr.getUser().isEmpty() && !shortAdr ) {
249 retVal += "&lt;" + adr.getUser();
250 if ( !adr.getHost().isEmpty() ) {
251 retVal += '@' + adr.getHost();
252 }
253 retVal += "&gt; ";
254 }
255 if ( !adr.getComment().isEmpty() ) {
256 // should do some umlaut escaping
257 retVal = '(' + adr.getComment() + ')';
258 }
259
260 if ( !adr.getUser().isEmpty() ) {
261 QString mail;
262 mail = adr.getUser();
263 if ( !mail.isEmpty() && !adr.getHost().isEmpty() ) {
264 mail += '@' + adr.getHost();
265 }
266 if ( !mail.isEmpty() ) {
267 retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>";
268 }
269 }
270 return retVal;
271}
272
273QString mailAddress::emailAddrAsAnchor (const QList < mailAddress *> &list, bool value)
274{
275 QString retVal;
276 QListIterator < mailAddress *> it( list );
277
278 while ( it.hasNext() ) {
279 retVal += emailAddrAsAnchor ( ( *it.next() ), value ) + "<BR></BR>\n";
280 }
281 return retVal;
282}
283
284void mailAddress::clear()
285{
286 user.truncate( 0 );
287 host.truncate( 0 );
288 rawFullName.truncate( 0 );
289 rawComment.truncate( 0 );
290}
mimeHdrLine::parseWord
static int parseWord(const char *)
slurp one word
Definition mimehdrline.cpp:132
mimeHdrLine::skipWS
static int skipWS(const char *)
skip all white space characters
Definition mimehdrline.cpp:252
mimeHdrLine::parseQuoted
static int parseQuoted(char, char, const char *)
slurp one word
Definition mimehdrline.cpp:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kioslave/imap4

Skip menu "kioslave/imap4"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal