• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 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 
30 using namespace KIMAP;
31 
32 mailAddress::mailAddress ()
33 {
34 }
35 
36 mailAddress::mailAddress (const mailAddress & lr):
37 user (lr.user),
38 host (lr.host),
39 rawFullName (lr.rawFullName),
40 rawComment (lr.rawComment)
41 {
42 // kDebug(7116) <<"mailAddress::mailAddress -" << getStr();
43 }
44 
45 mailAddress & mailAddress::operator = (const mailAddress & lr)
46 {
47  // Avoid a = a.
48  if (this == &lr)
49  return *this;
50 
51  user = lr.user;
52  host = lr.host;
53  rawFullName = lr.rawFullName;
54  rawComment = lr.rawComment;
55 
56 // kDebug(7116) <<"mailAddress::operator= -" << getStr();
57 
58  return *this;
59 }
60 
61 
62 
63 
64 mailAddress::~mailAddress ()
65 {
66 }
67 
68 mailAddress::mailAddress (char *aCStr)
69 {
70  parseAddress (aCStr);
71 }
72 
73 int mailAddress::parseAddress (const char *aCStr)
74 {
75  int retVal = 0;
76  int skip;
77  uint len;
78  int pt;
79 
80  if (aCStr)
81  {
82  //skip leading white space
83  skip = mimeHdrLine::skipWS (aCStr);
84  if (skip > 0)
85  {
86  aCStr += skip;
87  retVal += skip;
88  }
89  while (*aCStr)
90  {
91  int advance;
92 
93  switch (*aCStr)
94  {
95  case '"':
96  advance = mimeHdrLine::parseQuoted ('"', '"', aCStr);
97  rawFullName += QByteArray (aCStr, advance);
98  break;
99  case '(':
100  advance = mimeHdrLine::parseQuoted ('(', ')', aCStr);
101  rawComment += QByteArray (aCStr, advance);
102  break;
103  case '<':
104  advance = mimeHdrLine::parseQuoted ('<', '>', aCStr);
105  user = QByteArray (aCStr, advance); // copy it
106  len = advance;
107  user = user.mid (1, len - 2); // strip <>
108  len -= 2;
109  pt = user.indexOf('@');
110  host = user.right (len - pt - 1); // split it into host
111  user.truncate(pt); // and user
112  break;
113  default:
114  advance = mimeHdrLine::parseWord (aCStr);
115  //if we've seen a FQ mailname the rest must be quoted or is just junk
116  if (user.isEmpty ())
117  {
118  if (*aCStr != ',')
119  {
120  rawFullName += aCStr;
121  if (mimeHdrLine::skipWS (aCStr+advance) > 0)
122  {
123  rawFullName += ' ';
124  }
125  }
126  }
127  break;
128  }
129  if (advance)
130  {
131  retVal += advance;
132  aCStr += advance;
133  }
134  else
135  break;
136  advance = mimeHdrLine::skipWS (aCStr);
137  if (advance > 0)
138  {
139  retVal += advance;
140  aCStr += advance;
141  }
142  //reached end of current address
143  if (*aCStr == ',')
144  {
145  advance++;
146  break;
147  }
148  }
149  //let's see what we've got
150  if (rawFullName.isEmpty ())
151  {
152  if (user.isEmpty ())
153  retVal = 0;
154  else
155  {
156  if (host.isEmpty ())
157  {
158  rawFullName = user;
159  user.truncate(0);
160  }
161  }
162  }
163  else if (user.isEmpty ())
164  {
165  pt = rawFullName.indexOf ('@');
166  if (pt >= 0)
167  {
168  user = rawFullName;
169  host = user.right (user.length () - pt - 1);
170  user.truncate(pt);
171  rawFullName.truncate(0);
172  }
173  }
174 
175 #if 0
176 // dead
177  if (!rawFullName.isEmpty ())
178  {
179 // if(fullName[0] == '"')
180 // fullName = fullName.mid(1,fullName.length()-2);
181 // fullName = fullName.simplified().trimmed();
182 // fullName = KIMAP::decodeRFC2047String(fullName.ascii());
183  }
184 #endif
185  if (!rawComment.isEmpty ())
186  {
187  if (rawComment[0] == '(')
188  rawComment = rawComment.mid (1, rawComment.length () - 2);
189  rawComment = rawComment.trimmed ();
190 // comment = KIMAP::decodeRFC2047String(comment.ascii());
191  }
192  }
193  else
194  {
195  //debug();
196  }
197  return retVal;
198 }
199 
200 const QByteArray mailAddress::getStr () const
201 {
202  QByteArray retVal;
203  retVal.reserve(128); // Should be generally big enough
204 
205  if (!rawFullName.isEmpty ())
206  {
207  QByteArray tmpName( rawFullName );
208  KMime::addQuotes( tmpName, false );
209  retVal = tmpName + ' ';
210  }
211  if (!user.isEmpty ())
212  {
213  retVal += '<';
214  retVal += user;
215  if (!host.isEmpty ()) {
216  retVal += '@';
217  retVal += host;
218  }
219  retVal += '>';
220  }
221  if (!rawComment.isEmpty ())
222  {
223  retVal += " (" + rawComment + ')';
224  }
225  //kDebug() << retVal;
226  return retVal;
227 }
228 
229 bool mailAddress::isEmpty () const
230 {
231  return user.isEmpty ();
232 }
233 
234 void mailAddress::setFullName (const QString & _str)
235 {
236  rawFullName = KIMAP::encodeRFC2047String (_str).toLatin1 ();
237 }
238 
239 const QString mailAddress::getFullName () const
240 {
241  return KIMAP::decodeRFC2047String (rawFullName);
242 }
243 
244 void mailAddress::setCommentRaw (const QByteArray & _str)
245 {
246  rawComment = _str;
247 }
248 
249 void mailAddress::setComment (const QString & _str)
250 {
251  rawComment = KIMAP::encodeRFC2047String (_str).toLatin1 ();
252 }
253 
254 const QString mailAddress::getComment () const
255 {
256  return KIMAP::decodeRFC2047String (rawComment);
257 }
258 
259 const QByteArray & mailAddress::getCommentRaw () const
260 {
261  return rawComment;
262 }
263 
264 QString mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr)
265 {
266  QString retVal;
267  if (!adr.getFullName ().isEmpty ())
268  {
269  // should do some umlaut escaping
270  retVal += adr.getFullName () + ' ';
271  }
272  if (!adr.getUser ().isEmpty () && !shortAdr)
273  {
274  retVal += "&lt;" + adr.getUser ();
275  if (!adr.getHost ().isEmpty ())
276  retVal += '@' + adr.getHost ();
277  retVal += "&gt; ";
278  }
279  if (!adr.getComment ().isEmpty ())
280  {
281  // should do some umlaut escaping
282  retVal = '(' + adr.getComment () + ')';
283  }
284 
285  if (!adr.getUser ().isEmpty ())
286  {
287  QString mail;
288  mail = adr.getUser ();
289  if (!mail.isEmpty () && !adr.getHost ().isEmpty ())
290  mail += '@' + adr.getHost ();
291  if (!mail.isEmpty ())
292  retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>";
293  }
294  return retVal;
295 }
296 
297 QString mailAddress::emailAddrAsAnchor (const QList < mailAddress *> &list, bool value)
298 {
299  QString retVal;
300  QListIterator < mailAddress *> it (list);
301 
302  while (it.hasNext())
303  {
304  retVal += emailAddrAsAnchor ((*it.next()), value) + "<BR></BR>\n";
305  }
306 
307  return retVal;
308 }
309 
310 
311 void mailAddress::clear() {
312  user.truncate(0);
313  host.truncate(0);
314  rawFullName.truncate(0);
315  rawComment.truncate(0);
316 }
317 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Nov 26 2012 16:46:54 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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