1/*
2 This file is part of the kcal library.
3
4 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22/**
23 @file
24 This file is part of the API for handling calendar data and
25 defines the Person class.
26
27 @brief
28 Represents a person, by name and email address.
29
30 @author Cornelius Schumacher \<schumacher@kde.org\>
31 @author Reinhold Kainhofer \<reinhold@kainhofer.com\>
32*/
33
34#include "person.h"
35
36#include "kpimutils/email.h"
37
38#include <QtCore/QRegExp>
39
40#include <kdebug.h>
41#include <klocalizedstring.h>
42
43using namespace KCal;
44
45/**
46 Private class that helps to provide binary compatibility between releases.
47 @internal
48*/
49//@cond PRIVATE
50class KCal::Person::Private
51{
52 public:
53 QString mName; // person name
54 QString mEmail; // person email address
55};
56//@endcond
57
58Person::Person() : d( new KCal::Person::Private )
59{
60}
61
62Person::Person( const QString &fullName )
63 : d( new Private )
64{
65 KPIMUtils::extractEmailAddressAndName( fullName, d->mEmail, d->mName );
66}
67
68Person Person::fromFullName( const QString &fullName )
69{
70 QString email, name;
71 KPIMUtils::extractEmailAddressAndName( fullName, email, name );
72 return Person( name, email );
73}
74
75Person::Person( const QString &name, const QString &email )
76 : d( new KCal::Person::Private )
77{
78 d->mName = name;
79 d->mEmail = email;
80}
81
82Person::Person( const Person &person )
83 : d( new KCal::Person::Private( *person.d ) )
84{
85}
86
87Person::~Person()
88{
89 delete d;
90}
91
92#if defined(Q_CC_MSVC)
93bool KCal::Person::operator==( const Person &person ) const
94#else
95bool KCal::Person::operator==( const Person &person )
96#endif
97{
98 return
99 d->mName == person.d->mName &&
100 d->mEmail == person.d->mEmail;
101}
102
103Person &KCal::Person::operator=( const Person &person )
104{
105 // check for self assignment
106 if ( &person == this ) {
107 return *this;
108 }
109
110 *d = *person.d;
111 return *this;
112}
113
114QString Person::fullName() const
115{
116 if ( d->mName.isEmpty() ) {
117 return d->mEmail;
118 } else {
119 if ( d->mEmail.isEmpty() ) {
120 return d->mName;
121 } else {
122 // Taken from KABC::Addressee::fullEmail
123 QString name = d->mName;
124 QRegExp needQuotes( "[^ 0-9A-Za-z\\x0080-\\xFFFF]" );
125 bool weNeedToQuote = name.indexOf( needQuotes ) != -1;
126 if ( weNeedToQuote ) {
127 if ( name[0] != '"' ) {
128 name.prepend( '"' );
129 }
130 if ( name[ name.length()-1 ] != '"' ) {
131 name.append( '"' );
132 }
133 }
134 return name + " <" + d->mEmail + '>';
135 }
136 }
137}
138
139QString Person::name() const
140{
141 return d->mName;
142}
143
144QString Person::email() const
145{
146 return d->mEmail;
147}
148
149bool Person::isEmpty() const
150{
151 return d->mEmail.isEmpty() && d->mName.isEmpty();
152}
153
154void Person::setName( const QString &name )
155{
156 d->mName = name;
157}
158
159void Person::setEmail( const QString &email )
160{
161 if ( email.startsWith( QLatin1String( "mailto:" ), Qt::CaseInsensitive ) ) {
162 d->mEmail = email.mid( 7 );
163 } else {
164 d->mEmail = email;
165 }
166}
167