diff --git a/autotests/testattendee.cpp b/autotests/testattendee.cpp index 0e4236484..30d3452eb 100644 --- a/autotests/testattendee.cpp +++ b/autotests/testattendee.cpp @@ -1,221 +1,222 @@ /* This file is part of the kcalcore library. Copyright (C) 2006,2008 Allen Winter Copyright (C) 2010 Casey Link Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "testattendee.h" #include "attendee.h" +#include "person.h" #include #include QTEST_MAIN(AttendeeTest) using namespace KCalCore; void AttendeeTest::testValidity() { Attendee attendee(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com")); attendee.setRole(Attendee::Chair); QVERIFY(attendee.role() == Attendee::Chair); } void AttendeeTest::testType() { Attendee attendee(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com")); QCOMPARE(attendee.cuType(), Attendee::Individual); QCOMPARE(attendee.cuTypeStr(), QLatin1String("INDIVIDUAL")); attendee.setCuType(attendee.cuTypeStr()); QCOMPARE(attendee.cuType(), Attendee::Individual); attendee.setCuType(QStringLiteral("INVALID")); QCOMPARE(attendee.cuType(), Attendee::Unknown); QCOMPARE(attendee.cuTypeStr(), QLatin1String("UNKNOWN")); attendee.setCuType(QStringLiteral("group")); QCOMPARE(attendee.cuType(), Attendee::Group); QCOMPARE(attendee.cuTypeStr(), QLatin1String("GROUP")); attendee.setCuType(QStringLiteral("resource")); QCOMPARE(attendee.cuType(), Attendee::Resource); QCOMPARE(attendee.cuTypeStr(), QLatin1String("RESOURCE")); attendee.setCuType(QStringLiteral("ROOM")); QCOMPARE(attendee.cuType(), Attendee::Room); QCOMPARE(attendee.cuTypeStr(), QLatin1String("ROOM")); attendee.setCuType(QStringLiteral("UNKNOWN")); QCOMPARE(attendee.cuType(), Attendee::Unknown); QCOMPARE(attendee.cuTypeStr(), QLatin1String("UNKNOWN")); attendee.setCuType(QStringLiteral("X-test")); QCOMPARE(attendee.cuType(), Attendee::Unknown); QCOMPARE(attendee.cuTypeStr(), QLatin1String("X-TEST")); attendee.setCuType(QStringLiteral("IANA-TEST")); QCOMPARE(attendee.cuType(), Attendee::Unknown); QCOMPARE(attendee.cuTypeStr(), QLatin1String("IANA-TEST")); attendee.setCuType(Attendee::Individual); QCOMPARE(attendee.cuType(), Attendee::Individual); attendee.setCuType(Attendee::Group); QCOMPARE(attendee.cuType(), Attendee::Group); attendee.setCuType(Attendee::Resource); QCOMPARE(attendee.cuType(), Attendee::Resource); attendee.setCuType(Attendee::Room); QCOMPARE(attendee.cuType(), Attendee::Room); attendee.setCuType(Attendee::Unknown); QCOMPARE(attendee.cuType(), Attendee::Unknown); } void AttendeeTest::testCompare() { Attendee attendee1(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com")); Attendee attendee2(QStringLiteral("wilma"), QStringLiteral("wilma@flintstone.com")); attendee1.setRole(Attendee::ReqParticipant); attendee2.setRole(Attendee::Chair); QVERIFY(!(attendee1 == attendee2)); attendee2.setRole(Attendee::ReqParticipant); QVERIFY(!(attendee1 == attendee2)); QVERIFY(attendee1.name() == QLatin1String("fred")); } void AttendeeTest::testCompareType() { Attendee attendee1(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com")); attendee1.setCuType(Attendee::Resource); Attendee attendee2 = attendee1; QCOMPARE(attendee2.cuType(), Attendee::Resource); QVERIFY(attendee1 == attendee2); attendee2.setCuType(Attendee::Individual); QVERIFY(!(attendee1 == attendee2)); } void AttendeeTest::testAssign() { Attendee attendee1(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com")); Attendee attendee2 = attendee1; QVERIFY(attendee1 == attendee2); attendee2.setRole(Attendee::NonParticipant); QVERIFY(!(attendee1 == attendee2)); Attendee attendee3(attendee1); QVERIFY(attendee3 == attendee1); } void AttendeeTest::testDataStreamOut() { Attendee::Ptr attendee1(new Attendee(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com"))); attendee1->setRSVP(true); attendee1->setRole(Attendee::Chair); attendee1->setUid(QStringLiteral("Shooby Doo Bop")); attendee1->setDelegate(QStringLiteral("I AM THE Delegate")); attendee1->setDelegator(QStringLiteral("AND I AM THE Delegator")); attendee1->setCuType(QStringLiteral("X-SPECIAL")); attendee1->setCustomProperty("name", QStringLiteral("value")); attendee1->setCustomProperty("foo", QStringLiteral("bar")); QByteArray byteArray; QDataStream out_stream(&byteArray, QIODevice::WriteOnly); out_stream << attendee1; QDataStream in_stream(&byteArray, QIODevice::ReadOnly); Person::Ptr person; bool rsvp; QString delegate, delegator, cuType, uid; CustomProperties customProperties; Attendee::Role role; Attendee::PartStat status; uint role_int, status_int; in_stream >> person; QVERIFY(person->name() == attendee1->name()); QVERIFY(person->email() == attendee1->email()); in_stream >> rsvp; QVERIFY(rsvp == attendee1->RSVP()); in_stream >> role_int; role = Attendee::Role(role_int); QVERIFY(role == attendee1->role()); in_stream >> status_int; status = Attendee::PartStat(status_int); QVERIFY(status == attendee1->status()); in_stream >> uid; QVERIFY(uid == attendee1->uid()); in_stream >> delegate; QVERIFY(delegate == attendee1->delegate()); in_stream >> delegator; QVERIFY(delegator == attendee1->delegator()); in_stream >> cuType; QVERIFY(cuType == attendee1->cuTypeStr()); in_stream >> customProperties; QVERIFY(customProperties == attendee1->customProperties()); } void AttendeeTest::testDataStreamIn() { Attendee::Ptr attendee1(new Attendee(QStringLiteral("fred"), QStringLiteral("fred@flintstone.com"))); attendee1->setRSVP(true); attendee1->setRole(Attendee::Chair); attendee1->setCuType(QStringLiteral("IANA-FOO")); attendee1->setUid(QStringLiteral("Shooby Doo Bop")); attendee1->setDelegate(QStringLiteral("I AM THE Delegate")); attendee1->setDelegator(QStringLiteral("AND I AM THE Delegator")); attendee1->setCustomProperty("name", QStringLiteral("value")); attendee1->setCustomProperty("foo", QStringLiteral("bar")); QByteArray byteArray; QDataStream out_stream(&byteArray, QIODevice::WriteOnly); out_stream << attendee1; Attendee::Ptr attendee2; QDataStream in_stream(&byteArray, QIODevice::ReadOnly); in_stream >> attendee2; QVERIFY(attendee2); QVERIFY(attendee2->uid() == attendee1->uid()); QVERIFY(attendee2->RSVP() == attendee1->RSVP()); QVERIFY(attendee2->role() == attendee1->role()); QVERIFY(attendee2->cuTypeStr() == attendee1->cuTypeStr()); QVERIFY(attendee2->status() == attendee1->status()); QVERIFY(attendee2->delegate() == attendee1->delegate()); QVERIFY(attendee2->delegator() == attendee1->delegator()); QVERIFY(attendee2->customProperties() == attendee1->customProperties()); QVERIFY(*attendee1 == *attendee2); } diff --git a/src/attendee.cpp b/src/attendee.cpp index 9b8614ee6..07f7c1cc6 100644 --- a/src/attendee.cpp +++ b/src/attendee.cpp @@ -1,316 +1,343 @@ /* This file is part of the kcalcore library. Copyright (c) 2001 Cornelius Schumacher Copyright (C) 2010 Casey Link Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Attendee class. @brief Represents information related to an attendee of an Calendar Incidence. @author Cornelius Schumacher \ */ #include "attendee.h" +#include "person.h" +#include "person_p.h" #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::Attendee::Private { public: void setCuType(CuType cuType); void setCuType(const QString &cuType); CuType cuType() const; QString cuTypeStr() const; bool mRSVP = false; Role mRole; PartStat mStatus; QString mUid; QString mDelegate; QString mDelegator; CustomProperties mCustomProperties; + QString mName; + QString mEmail; private: QString sCuType; CuType mCuType; }; //@endcond void KCalCore::Attendee::Private::setCuType(Attendee::CuType cuType) { mCuType = cuType; sCuType.clear(); } void KCalCore::Attendee::Private::setCuType(const QString &cuType) { const QString upper = cuType.toUpper(); if (upper == QLatin1String("INDIVIDUAL")) { setCuType(Attendee::Individual); } else if (upper == QLatin1String("GROUP")) { setCuType(Attendee::Group); } else if (upper == QLatin1String("RESOURCE")) { setCuType(Attendee::Resource); } else if (upper == QLatin1String("ROOM")) { setCuType(Attendee::Room); } else { setCuType(Attendee::Unknown); if (upper.startsWith(QLatin1String("X-")) || upper.startsWith(QLatin1String("IANA-"))) { sCuType = upper; } } } Attendee::CuType KCalCore::Attendee::Private::cuType() const { return mCuType; } QString KCalCore::Attendee::Private::cuTypeStr() const { switch (mCuType) { case Attendee::Individual: return QStringLiteral("INDIVIDUAL"); case Attendee::Group: return QStringLiteral("GROUP"); case Attendee::Resource: return QStringLiteral("RESOURCE"); case Attendee::Room: return QStringLiteral("ROOM"); case Attendee::Unknown: if (sCuType.isEmpty()) { return QStringLiteral("UNKNOWN"); } else { return sCuType; } } return QStringLiteral("UNKNOWN"); } Attendee::Attendee(const QString &name, const QString &email, bool rsvp, Attendee::PartStat status, Attendee::Role role, const QString &uid) : d(new Attendee::Private) { setName(name); setEmail(email); d->mRSVP = rsvp; d->mStatus = status; d->mRole = role; d->mUid = uid; d->setCuType(Attendee::Individual); } Attendee::Attendee(const Attendee &attendee) - : Person(attendee), - d(new Attendee::Private(*attendee.d)) + : d(new Attendee::Private(*attendee.d)) { } Attendee::~Attendee() { delete d; } bool KCalCore::Attendee::operator==(const Attendee &attendee) const { return d->mUid == attendee.d->mUid && d->mRSVP == attendee.d->mRSVP && d->mRole == attendee.d->mRole && d->mStatus == attendee.d->mStatus && d->mDelegate == attendee.d->mDelegate && d->mDelegator == attendee.d->mDelegator && d->cuTypeStr() == attendee.d->cuTypeStr() && - (const Person &) * this == (const Person &)attendee; + d->mName == attendee.d->mName && + d->mEmail == attendee.d->mEmail; } bool KCalCore::Attendee::operator!=(const Attendee &attendee) const { return !operator==(attendee); } Attendee &KCalCore::Attendee::operator=(const Attendee &attendee) { // check for self assignment if (&attendee == this) { return *this; } *d = *attendee.d; - setName(attendee.name()); - setEmail(attendee.email()); return *this; } +QString Attendee::name() const +{ + return d->mName; +} + +void Attendee::setName(const QString &name) +{ + d->mName = name; +} + +QString Attendee::fullName() const +{ + return fullNameHelper(d->mName, d->mEmail); +} + +QString Attendee::email() const +{ + return d->mEmail; +} + +void Attendee::setEmail(const QString &email) +{ + d->mEmail = email; +} + void Attendee::setRSVP(bool r) { d->mRSVP = r; } bool Attendee::RSVP() const { return d->mRSVP; } void Attendee::setStatus(Attendee::PartStat status) { d->mStatus = status; } Attendee::PartStat Attendee::status() const { return d->mStatus; } void Attendee::setCuType(Attendee::CuType cuType) { d->setCuType(cuType); } void Attendee::setCuType(const QString &cuType) { d->setCuType(cuType); } Attendee::CuType Attendee::cuType() const { return d->cuType(); } QString Attendee::cuTypeStr() const { return d->cuTypeStr(); } void Attendee::setRole(Attendee::Role role) { d->mRole = role; } Attendee::Role Attendee::role() const { return d->mRole; } void Attendee::setUid(const QString &uid) { d->mUid = uid; } QString Attendee::uid() const { return d->mUid; } void Attendee::setDelegate(const QString &delegate) { d->mDelegate = delegate; } QString Attendee::delegate() const { return d->mDelegate; } void Attendee::setDelegator(const QString &delegator) { d->mDelegator = delegator; } QString Attendee::delegator() const { return d->mDelegator; } void Attendee::setCustomProperty(const QByteArray &xname, const QString &xvalue) { d->mCustomProperties.setNonKDECustomProperty(xname, xvalue); } CustomProperties &Attendee::customProperties() { return d->mCustomProperties; } const CustomProperties &Attendee::customProperties() const { return d->mCustomProperties; } QDataStream &KCalCore::operator<<(QDataStream &stream, const KCalCore::Attendee::Ptr &attendee) { - KCalCore::Person::Ptr p(new KCalCore::Person(*((Person *)attendee.data()))); + KCalCore::Person::Ptr p(new KCalCore::Person(attendee->name(), attendee->email())); stream << p; return stream << attendee->d->mRSVP << int(attendee->d->mRole) << int(attendee->d->mStatus) << attendee->d->mUid << attendee->d->mDelegate << attendee->d->mDelegator << attendee->d->cuTypeStr() << attendee->d->mCustomProperties; } QDataStream &KCalCore::operator>>(QDataStream &stream, KCalCore::Attendee::Ptr &attendee) { bool RSVP; Attendee::Role role; Attendee::PartStat status; QString uid; QString delegate; QString delegator; QString cuType; CustomProperties customProperties; uint role_int; uint status_int; KCalCore::Person::Ptr person(new Person()); stream >> person; stream >> RSVP >> role_int >> status_int >> uid >> delegate >> delegator >> cuType >> customProperties; role = Attendee::Role(role_int); status = Attendee::PartStat(status_int); Attendee::Ptr att_temp(new KCalCore::Attendee(person->name(), person->email(), RSVP, status, role, uid)); att_temp->setDelegate(delegate); att_temp->setDelegator(delegator); att_temp->setCuType(cuType); att_temp->d->mCustomProperties = customProperties; attendee.swap(att_temp); return stream; } diff --git a/src/attendee.h b/src/attendee.h index dbfc121cb..0eb40c8f5 100644 --- a/src/attendee.h +++ b/src/attendee.h @@ -1,353 +1,372 @@ /* This file is part of the kcalcore library. Copyright (c) 2001-2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Attendee class. @author Cornelius Schumacher \ */ #ifndef KCALCORE_ATTENDEE_H #define KCALCORE_ATTENDEE_H #include +#include #include "kcalcore_export.h" #include "customproperties.h" -#include "person.h" namespace KCalCore { /** @brief Represents information related to an attendee of an Calendar Incidence, typically a meeting or task (to-do). Attendees are people with a name and (optional) email address who are invited to participate in some way in a meeting or task. This class also tracks that status of the invitation: accepted; tentatively accepted; declined; delegated to another person; in-progress; completed. Attendees may optionally be asked to @acronym RSVP ("Respond Please") to the invitation. Note that each attendee be can optionally associated with a @acronym UID (unique identifier) derived from a Calendar Incidence, Email Message, or any other thing you want. */ -class KCALCORE_EXPORT Attendee : private Person +class KCALCORE_EXPORT Attendee { public: - using Person::setEmail; - using Person::email; - using Person::setName; - using Person::name; - using Person::fullName; - /** The different types of participant status. The meaning is specific to the incidence type in context. */ enum PartStat { NeedsAction, /**< Event, to-do or journal needs action (default) */ Accepted, /**< Event, to-do or journal accepted */ Declined, /**< Event, to-do or journal declined */ Tentative, /**< Event or to-do tentatively accepted */ Delegated, /**< Event or to-do delegated */ Completed, /**< To-do completed */ InProcess, /**< To-do in process of being completed */ None }; /** The different types of participation roles. */ enum Role { ReqParticipant, /**< Participation is required (default) */ OptParticipant, /**< Participation is optional */ NonParticipant, /**< Non-Participant; copied for information purposes */ Chair /**< Chairperson */ }; /** * The different types of a participant. * * @since 4.14 */ enum CuType { Individual, /**< An individual (default) */ Group, /**< A group of individuals */ Resource, /**< A physical resource */ Room, /**< A room resource */ Unknown /**< Otherwise not known */ /** * Parameters that have to set via the QString variant of @setCuType() and @cuType() * x-name ; Experimental cuType * iana-token ; Other IANA-registered */ }; /** A shared pointer to an Attendee object. */ typedef QSharedPointer Ptr; /** List of attendees. */ typedef QVector List; /** - Constructs an attendee consisting of a Person name (@p name) and + Constructs an attendee consisting of a person name (@p name) and email address (@p email); invitation status and #Role; an optional @acronym RSVP flag and @acronym UID. @param name is person name of the attendee. @param email is person email address of the attendee. @param rsvp if true, the attendee is requested to reply to invitations. @param status is the #PartStat status of the attendee. @param role is the #Role of the attendee. @param uid is the @acronym UID of the attendee. */ Attendee(const QString &name, const QString &email, bool rsvp = false, PartStat status = None, Role role = ReqParticipant, const QString &uid = QString()); /** Constructs an attendee by copying another attendee. @param attendee is the attendee to be copied. */ Attendee(const Attendee &attendee); /** Destroys the attendee. */ ~Attendee(); + /** + Returns the name of the attendee. + */ + Q_REQUIRED_RESULT QString name() const; + /** + Sets the name of the attendee to @p name. + */ + void setName(const QString &name); + + /** + Returns the full name and email address of this attendee + @return A QString containing the person's full name in the form + "FirstName LastName \". + */ + Q_REQUIRED_RESULT QString fullName() const; + + /** + Returns the email address for this attendee. + */ + Q_REQUIRED_RESULT QString email() const; + /** + Sets the email address for this attendee to @p email. + */ + void setEmail(const QString &email); + /** Sets the Role of the attendee to @p role. @param role is the Role to use for the attendee. @see role() */ void setRole(Role role); /** Returns the Role of the attendee. @see setRole() */ Q_REQUIRED_RESULT Role role() const; /** Sets the @acronym UID of the attendee to @p uid. @param uid is the @acronym UID to use for the attendee. @see uid() */ void setUid(const QString &uid); /** Returns the @acronym UID of the attendee. @see setUid() */ Q_REQUIRED_RESULT QString uid() const; /** Sets the #PartStat of the attendee to @p status. @param status is the #PartStat to use for the attendee. @see status() */ void setStatus(PartStat status); /** Returns the #PartStat of the attendee. @see setStatus() */ Q_REQUIRED_RESULT PartStat status() const; /** Sets the #CuType of the attendee to @p cuType. @param cuType is the #CuType to use for the attendee. @see cuType() @since 4.14 */ void setCuType(CuType cuType); /** Sets the #CuType of the attendee to @p cuType. @param cuType is the #CuType to use for the attendee. @see cuType() @since 4.14 */ void setCuType(const QString &cuType); /** Returns the #CuType of the attendee. @see setCuType() @since 4.14 */ Q_REQUIRED_RESULT CuType cuType() const; /** Returns the #CuType of the attendee. @see setCuType() @since 4.14 */ Q_REQUIRED_RESULT QString cuTypeStr() const; /** Sets the @acronym RSVP flag of the attendee to @p rsvp. @param rsvp if set (true), the attendee is requested to reply to invitations. @see RSVP() */ void setRSVP(bool rsvp); /** Returns the attendee @acronym RSVP flag. @see setRSVP() */ Q_REQUIRED_RESULT bool RSVP() const; /** Compares this with @p attendee for equality. @param attendee the attendee to compare. */ bool operator==(const Attendee &attendee) const; /** Compares this with @p attendee for inequality. @param attendee the attendee to compare. */ bool operator!=(const Attendee &attendee) const; /** Sets the delegate. @param delegate is a string containing a MAILTO URI of those delegated to attend the meeting. @see delegate(), setDelegator(). */ void setDelegate(const QString &delegate); /** Returns the delegate. @see setDelegate(). */ Q_REQUIRED_RESULT QString delegate() const; /** Sets the delegator. @param delegator is a string containing a MAILTO URI of those who have delegated their meeting attendance. @see delegator(), setDelegate(). */ void setDelegator(const QString &delegator); /** Returns the delegator. @see setDelegator(). */ Q_REQUIRED_RESULT QString delegator() const; /** Adds a custom property. If the property already exists it will be overwritten. @param xname is the name of the property. @param xvalue is its value. */ void setCustomProperty(const QByteArray &xname, const QString &xvalue); /** Returns a reference to the CustomProperties object */ Q_REQUIRED_RESULT CustomProperties &customProperties(); /** Returns a const reference to the CustomProperties object */ const CustomProperties &customProperties() const; /** Sets this attendee equal to @p attendee. @param attendee is the attendee to copy. */ Attendee &operator=(const Attendee &attendee); private: //@cond PRIVATE class Private; Private *const d; //@endcond friend KCALCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KCalCore::Attendee::Ptr &attendee); friend KCALCORE_EXPORT QDataStream &operator>>(QDataStream &s, KCalCore::Attendee::Ptr &attendee); }; /** Serializes an Attendee object into a data stream. @param stream is a QDataStream. @param attendee is a pointer to a Attendee object to be serialized. */ KCALCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KCalCore::Attendee::Ptr &attendee); /** Initializes an Attendee object from a data stream. @param stream is a QDataStream. @param attendee is a pointer to a Attendee object to be initialized. */ KCALCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KCalCore::Attendee::Ptr &attendee); } //@cond PRIVATE Q_DECLARE_TYPEINFO(KCalCore::Attendee::Ptr, Q_MOVABLE_TYPE); Q_DECLARE_METATYPE(KCalCore::Attendee::Ptr) //@endcond #endif diff --git a/src/incidencebase.h b/src/incidencebase.h index ae14b0e1e..dc217c8e8 100644 --- a/src/incidencebase.h +++ b/src/incidencebase.h @@ -1,782 +1,783 @@ /* This file is part of the kcalcore library. Copyright (c) 2001-2003 Cornelius Schumacher Copyright (c) 2003-2004 Reinhold Kainhofer Copyright (c) 2005 Rafal Rzepecki Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. Contact: Alvaro Manera This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the IncidenceBase class. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ @author Rafal Rzepecki \ @glossary @anchor incidence @b incidence: General term for a calendar component. Examples are events, to-dos, and journals. @glossary @anchor event @b event: An @ref incidence that has a start and end time, typically representing some occurrence of social or personal importance. May be recurring. Examples are appointments, meetings, or holidays. @glossary @anchor to-do @b to-do: An @ref incidence that has an optional start time and an optional due time typically representing some undertaking to be performed. May be recurring. Examples are "fix the bug" or "pay the bills". @glossary @anchor todo @b todo: See @ref to-do. @glossary @anchor journal @b journal: An @ref incidence with a start date that represents a diary or daily record of one's activities. May @b not be recurring. */ #ifndef KCALCORE_INCIDENCEBASE_H #define KCALCORE_INCIDENCEBASE_H #include "attendee.h" #include "customproperties.h" #include "duration.h" +#include "person.h" #include #include #include #include class QUrl; class QDate; class QTimeZone; namespace KCalCore { /** List of dates */ typedef QList DateList; /** List of times */ typedef QList DateTimeList; class Event; class Todo; class Journal; class FreeBusy; class Visitor; /** @brief An abstract class that provides a common base for all calendar incidence classes. define: organizer (person) define: uid (same as the attendee uid?) Several properties are not allowed for VFREEBUSY objects (see rfc:2445), so they are not in IncidenceBase. The hierarchy is: IncidenceBase + FreeBusy + Incidence + Event + Todo + Journal So IncidenceBase contains all properties that are common to all classes, and Incidence contains all additional properties that are common to Events, Todos and Journals, but are not allowed for FreeBusy entries. */ class KCALCORE_EXPORT IncidenceBase : public CustomProperties { Q_GADGET Q_PROPERTY(QString uid READ uid WRITE setUid) Q_PROPERTY(QDateTime lastModified READ lastModified WRITE setLastModified) Q_PROPERTY(QDateTime dtStart READ dtStart WRITE setDtStart) Q_PROPERTY(bool allDay READ allDay WRITE setAllDay) public: /** A shared pointer to an IncidenceBase. */ typedef QSharedPointer Ptr; /** The different types of incidences, per RFC2445. @see type(), typeStr() */ enum IncidenceType { TypeEvent = 0, /**< Type is an event */ TypeTodo, /**< Type is a to-do */ TypeJournal, /**< Type is a journal */ TypeFreeBusy, /**< Type is a free/busy */ TypeUnknown /**< Type unknown */ }; /** The different types of incidence date/times roles. @see dateTime() */ enum DateTimeRole { RoleAlarmStartOffset = 0,/**< Role for an incidence alarm's starting offset date/time */ RoleAlarmEndOffset, /**< Role for an incidence alarm's ending offset date/time */ RoleSort, /**< Role for an incidence's date/time used when sorting */ RoleCalendarHashing, /**< Role for looking up an incidence in a Calendar */ RoleStartTimeZone, /**< Role for determining an incidence's starting timezone */ RoleEndTimeZone, /**< Role for determining an incidence's ending timezone */ RoleEndRecurrenceBase, RoleEnd, /**< Role for determining an incidence's dtEnd, will return an invalid QDateTime if the incidence does not support dtEnd */ RoleDisplayEnd, /**< Role used for display purposes, represents the end boundary if an incidence supports dtEnd */ RoleAlarm, /**< Role for determining the date/time of the first alarm. Returns invalid time if the incidence doesn't have any alarm */ RoleRecurrenceStart, /**< Role for determining the start of the recurrence. Currently that's DTSTART for an event and DTDUE for a to-do. (NOTE: If the incidence is a to-do, recurrence should be calculated having DTSTART for a reference, not DT-DUE. This is one place KCalCore isn't compliant with RFC2445) */ RoleDisplayStart, /**< Role for display purposes, represents the start boundary of an incidence. To-dos return dtDue here, for historical reasons */ RoleDnD /**< Role for determining new start and end dates after a DnD */ }; /** The different types of incidence fields. */ enum Field { FieldDtStart, ///> Field representing the DTSTART component. FieldDtEnd, ///> Field representing the DTEND component. FieldLastModified, ///> Field representing the LAST-MODIFIED component. FieldDescription, ///> Field representing the DESCRIPTION component. FieldSummary, ///> Field representing the SUMMARY component. FieldLocation, ///> Field representing the LOCATION component. FieldCompleted, ///> Field representing the COMPLETED component. FieldPercentComplete, ///> Field representing the PERCENT-COMPLETE component. FieldDtDue, ///> Field representing the DUE component. FieldCategories, ///> Field representing the CATEGORIES component. FieldRelatedTo, ///> Field representing the RELATED-TO component. FieldRecurrence, ///> Field representing the EXDATE, EXRULE, RDATE, and RRULE components. FieldAttachment, ///> Field representing the ATTACH component. FieldSecrecy, ///> Field representing the CLASS component. FieldStatus, ///> Field representing the STATUS component. FieldTransparency, ///> Field representing the TRANSPARENCY component. FieldResources, ///> Field representing the RESOURCES component. FieldPriority, ///> Field representing the PRIORITY component. FieldGeoLatitude, ///> Field representing the latitude part of the GEO component. FieldGeoLongitude, ///> Field representing the longitude part of the GEO component. FieldRecurrenceId, ///> Field representing the RECURRENCE-ID component. FieldAlarms, ///> Field representing the VALARM component. FieldSchedulingId, ///> Field representing the X-KDE-LIBKCAL-ID component. FieldAttendees, ///> Field representing the ATTENDEE component. FieldOrganizer, ///> Field representing the ORGANIZER component. FieldCreated, ///> Field representing the CREATED component. FieldRevision, ///> Field representing the SEQUENCE component. FieldDuration, ///> Field representing the DURATION component. FieldContact, ///> Field representing the CONTACT component. FieldComment, ///> Field representing the COMMENT component. FieldUid, ///> Field representing the UID component. FieldUnknown, ///> Something changed. Always set when you use the assignment operator. FieldUrl ///> Field representing the URL component. }; /** The IncidenceObserver class. */ class KCALCORE_EXPORT IncidenceObserver { public: /** Destroys the IncidenceObserver. */ virtual ~IncidenceObserver(); /** The IncidenceObserver interface. This function is called before any changes are made. @param uid is the string containing the incidence @ref uid. @param recurrenceId is possible recurrenceid of incidence. */ virtual void incidenceUpdate(const QString &uid, const QDateTime &recurrenceId) = 0; /** The IncidenceObserver interface. This function is called after changes are completed. @param uid is the string containing the incidence @ref uid. @param recurrenceId is possible recurrenceid of incidence. */ virtual void incidenceUpdated(const QString &uid, const QDateTime &recurrenceId) = 0; }; /** Constructs an empty IncidenceBase. */ IncidenceBase(); /** Destroys the IncidenceBase. */ ~IncidenceBase() override; /** Assignment operator. All data belonging to derived classes are also copied. @see assign(). The caller guarantees that both types match. @code if ( i1.type() == i2.type() ) { i1 = i2; } else { qCDebug(KCALCORE_LOG) << "Invalid assignment!"; } @endcode Dirty field FieldUnknown will be set. @param other is the IncidenceBase to assign. */ IncidenceBase &operator=(const IncidenceBase &other); /** Compares this with IncidenceBase @p ib for equality. All data belonging to derived classes are also compared. @see equals(). @param ib is the IncidenceBase to compare against. @return true if the incidences are equal; false otherwise. */ bool operator==(const IncidenceBase &ib) const; /** Compares this with IncidenceBase @p ib for inequality. @param ib is the IncidenceBase to compare against. @return true if the incidences are /not/ equal; false otherwise. */ bool operator!=(const IncidenceBase &ib) const; /** Accept IncidenceVisitor. A class taking part in the visitor mechanism has to provide this implementation:
        bool accept(Visitor &v) { return v.visit(this); }
      
@param v is a reference to a Visitor object. @param incidence is a valid IncidenceBase object for visting. */ virtual bool accept(Visitor &v, const IncidenceBase::Ptr &incidence); /** Returns the incidence type. */ virtual IncidenceType type() const = 0; /** Prints the type of incidence as a string. */ virtual QByteArray typeStr() const = 0; /** Sets the unique id for the incidence to @p uid. @param uid is the string containing the incidence @ref uid. @see uid() */ void setUid(const QString &uid); /** Returns the unique id (@ref uid) for the incidence. @see setUid() */ Q_REQUIRED_RESULT QString uid() const; /** Returns the uri for the incidence, of form urn:x-ical:\ */ Q_REQUIRED_RESULT QUrl uri() const; /** Sets the time the incidence was last modified to @p lm. It is stored as a UTC date/time. @param lm is the QDateTime when the incidence was last modified. @see lastModified() */ virtual void setLastModified(const QDateTime &lm); /** Returns the time the incidence was last modified. @see setLastModified() */ Q_REQUIRED_RESULT QDateTime lastModified() const; /** Sets the organizer for the incidence. @param organizer is a non-null Person to use as the incidence @ref organizer. @see organizer(), setOrganizer(const QString &) */ void setOrganizer(const Person::Ptr &organizer); /** Sets the incidence organizer to any string @p organizer. @param organizer is a string to use as the incidence @ref organizer. @see organizer(), setOrganizer(const Person &) */ void setOrganizer(const QString &organizer); /** Returns the Person associated with this incidence. If no Person was set through setOrganizer(), a default Person() is returned. @see setOrganizer(const QString &), setOrganizer(const Person &) */ Person::Ptr organizer() const; /** Sets readonly status. @param readOnly if set, the incidence is read-only; else the incidence can be modified. @see isReadOnly(). */ virtual void setReadOnly(bool readOnly); /** Returns true the object is read-only; false otherwise. @see setReadOnly() */ Q_REQUIRED_RESULT bool isReadOnly() const; /** Sets the incidence's starting date/time with a QDateTime. The incidence's all-day status is set according to whether @p dtStart is a date/time (not all-day) or date-only (all-day). @param dtStart is the incidence start date/time. @see dtStart(). */ virtual void setDtStart(const QDateTime &dtStart); /** Returns an incidence's starting date/time as a QDateTime. @see setDtStart(). */ virtual QDateTime dtStart() const; /** Sets the incidence duration. @param duration the incidence duration @see duration() */ virtual void setDuration(const Duration &duration); /** Returns the length of the incidence duration. @see setDuration() */ Q_REQUIRED_RESULT Duration duration() const; /** Sets if the incidence has a duration. @param hasDuration true if the incidence has a duration; false otherwise. @see hasDuration() */ void setHasDuration(bool hasDuration); /** Returns true if the incidence has a duration; false otherwise. @see setHasDuration() */ Q_REQUIRED_RESULT bool hasDuration() const; /** Returns true or false depending on whether the incidence is all-day. i.e. has a date but no time attached to it. @see setAllDay() */ Q_REQUIRED_RESULT bool allDay() const; /** Sets whether the incidence is all-day, i.e. has a date but no time attached to it. @param allDay sets whether the incidence is all-day. @see allDay() */ virtual void setAllDay(bool allDay); /** Shift the times of the incidence so that they appear at the same clock time as before but in a new time zone. The shift is done from a viewing time zone rather than from the actual incidence time zone. For example, shifting an incidence whose start time is 09:00 America/New York, using an old viewing time zone (@p oldSpec) of Europe/London, to a new time zone (@p newSpec) of Europe/Paris, will result in the time being shifted from 14:00 (which is the London time of the incidence start) to 14:00 Paris time. @param oldZone the time zone which provides the clock times @param newZone the new time zone */ virtual void shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone); /** Adds a comment to the incidence. Does not add a linefeed character; simply appends the text as specified. @param comment is the QString containing the comment to add. @see removeComment(). */ void addComment(const QString &comment); /** Removes a comment from the incidence. Removes the first comment whose string is an exact match for the specified string in @p comment. @param comment is the QString containing the comment to remove. @return true if match found, false otherwise. @see addComment(). */ Q_REQUIRED_RESULT bool removeComment(const QString &comment); /** Deletes all incidence comments. */ void clearComments(); /** Returns all incidence comments as a list of strings. */ Q_REQUIRED_RESULT QStringList comments() const; /** Adds a contact to thieincidence. Does not add a linefeed character; simply appends the text as specified. @param contact is the QString containing the contact to add. @see removeContact(). */ void addContact(const QString &contact); /** Removes a contact from the incidence. Removes the first contact whose string is an exact match for the specified string in @p contact. @param contact is the QString containing the contact to remove. @return true if match found, false otherwise. @see addContact(). */ Q_REQUIRED_RESULT bool removeContact(const QString &contact); /** Deletes all incidence contacts. */ void clearContacts(); /** Returns all incidence contacts as a list of strings. */ Q_REQUIRED_RESULT QStringList contacts() const; /** Add Attendee to this incidence. IncidenceBase takes ownership of the Attendee object. @param attendee a pointer to the attendee to add @param doUpdate If true the Observers are notified, if false they are not. */ void addAttendee(const Attendee::Ptr &attendee, bool doUpdate = true); /** Removes all attendees from the incidence. */ void clearAttendees(); /** Delete single attendee from the incidence. The given attendee will be delete()d at the end of this call. @param attendee The attendee to be removeComment @param doUpdate If true the Observers are notified, if false they are not. */ void deleteAttendee(const Attendee::Ptr &attendee, bool doUpdate = true); /** Returns a list of incidence attendees. All pointers in the list are valid. */ Q_REQUIRED_RESULT Attendee::List attendees() const; /** Returns the number of incidence attendees. */ Q_REQUIRED_RESULT int attendeeCount() const; /** Returns the attendee with the specified email address. @param email is a QString containing an email address of the form "FirstName LastName ". @see attendeeByMails(), attendeesByUid(). */ Attendee::Ptr attendeeByMail(const QString &email) const; /** Returns the first incidence attendee with one of the specified email addresses. @param emails is a list of QStrings containing email addresses of the form "FirstName LastName ". @param email is a QString containing a single email address to search in addition to the list specified in @p emails. @see attendeeByMail(), attendeesByUid(). */ Attendee::Ptr attendeeByMails(const QStringList &emails, const QString &email = QString()) const; /** Returns the incidence attendee with the specified attendee @acronym UID. @param uid is a QString containing an attendee @acronym UID. @see attendeeByMail(), attendeeByMails(). */ Attendee::Ptr attendeeByUid(const QString &uid) const; /** Sets the incidences url. This property can be used to point to a more dynamic rendition of the incidence. I.e. a website related to the incidence. @param url of the incience. @see url() @since 4.12 */ void setUrl(const QUrl &url); /** Returns the url. @return incidences url value @see setUrl() @since 4.12 */ Q_REQUIRED_RESULT QUrl url() const; /** Register observer. The observer is notified when the observed object changes. @param observer is a pointer to an IncidenceObserver object that will be watching this incidence. @see unRegisterObserver() */ void registerObserver(IncidenceObserver *observer); /** Unregister observer. It isn't notified anymore about changes. @param observer is a pointer to an IncidenceObserver object that will be watching this incidence. @see registerObserver(). */ void unRegisterObserver(IncidenceObserver *observer); /** Call this to notify the observers after the IncidenceBase object will be changed. */ void update(); /** Call this to notify the observers after the IncidenceBase object has changed. */ void updated(); /** Call this when a group of updates is going to be made. This suppresses change notifications until endUpdates() is called, at which point updated() will automatically be called. */ void startUpdates(); /** Call this when a group of updates is complete, to notify observers that the instance has changed. This should be called in conjunction with startUpdates(). */ void endUpdates(); /** Returns a date/time corresponding to the specified DateTimeRole. @param role is a DateTimeRole. */ virtual QDateTime dateTime(DateTimeRole role) const = 0; /** Sets the date/time corresponding to the specified DateTimeRole. @param dateTime is QDateTime value to set. @param role is a DateTimeRole. */ virtual void setDateTime(const QDateTime &dateTime, DateTimeRole role) = 0; /** Returns the Akonadi specific sub MIME type of a KCalCore::IncidenceBase item, e.g. getting "application/x-vnd.akonadi.calendar.event" for a KCalCore::Event. */ virtual QLatin1String mimeType() const = 0; /** Returns the incidence recurrenceId. @return incidences recurrenceId value @see setRecurrenceId(). */ virtual QDateTime recurrenceId() const; /** Returns a QSet with all Fields that were changed since the incidence was created or resetDirtyFields() was called. @see resetDirtyFields() */ QSet dirtyFields() const; /** Sets which fields are dirty. @see dirtyFields() @since 4.8 */ void setDirtyFields(const QSet &); /** Resets dirty fields. @see dirtyFields() */ void resetDirtyFields(); /** * Constant that identifies KCalCore data in a binary stream. * * @since 4.12 */ Q_REQUIRED_RESULT static quint32 magicSerializationIdentifier(); protected: /** Marks Field @p field as dirty. @param field is the Field type to mark as dirty. @see dirtyFields() */ void setFieldDirty(IncidenceBase::Field field); /** @copydoc CustomProperties::customPropertyUpdate() */ void customPropertyUpdate() override; /** @copydoc CustomProperties::customPropertyUpdated() */ void customPropertyUpdated() override; /** Constructs an IncidenceBase as a copy of another IncidenceBase object. @param ib is the IncidenceBase to copy. */ IncidenceBase(const IncidenceBase &ib); /** Provides polymorfic comparison for equality. Only called by IncidenceBase::operator==() which guarantees that @p incidenceBase is of the right type. @param incidenceBase is the IncidenceBase to compare against. @return true if the incidences are equal; false otherwise. */ virtual bool equals(const IncidenceBase &incidenceBase) const; /** Provides polymorfic assignment. @param other is the IncidenceBase to assign. */ virtual IncidenceBase &assign(const IncidenceBase &other); enum VirtualHook { SerializerHook, DeserializerHook }; /** Standard trick to add virtuals later. @param id is any integer unique to this class which we will use to identify the method to be called. @param data is a pointer to some glob of data, typically a struct. */ virtual void virtual_hook(VirtualHook id, void *data) = 0; /** Identifies a read-only incidence. */ bool mReadOnly; private: //@cond PRIVATE class Private; Private *const d; //@endcond friend KCALCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KCalCore::IncidenceBase::Ptr &); friend KCALCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KCalCore::IncidenceBase::Ptr &); }; /** * Incidence serializer. * Uses the virtual_hook internally to avoid slicing. * * // TODO_KDE5: Provide a virtual serialize() method, as done with assign() and equals(). * * @since 4.12 */ KCALCORE_EXPORT QDataStream &operator<<(QDataStream &out, const KCalCore::IncidenceBase::Ptr &); /** * Incidence deserializer. * Uses the virtual_hook internally to avoid slicing. * * // TODO_KDE5: Provide a virtual serialize() method, as done with assign() and equals(). * * @since 4.12 */ KCALCORE_EXPORT QDataStream &operator>>(QDataStream &in, KCalCore::IncidenceBase::Ptr &); } Q_DECLARE_METATYPE(KCalCore::IncidenceBase *) Q_DECLARE_METATYPE(KCalCore::IncidenceBase::Ptr) #endif diff --git a/src/person.cpp b/src/person.cpp index f2131c152..feae4432a 100644 --- a/src/person.cpp +++ b/src/person.cpp @@ -1,385 +1,389 @@ /* This file is part of the kcalcore library. Copyright (c) 2001 Cornelius Schumacher Copyright (C) 2003-2004 Reinhold Kainhofer Copyright (C) 2010 Casey Link Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Person class. @brief Represents a person, by name and email address. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ */ #include "person.h" +#include "person_p.h" #include #include using namespace KCalCore; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class Q_DECL_HIDDEN KCalCore::Person::Private { public: Private() {} QString mName; // person name QString mEmail; // person email address }; //@endcond Person::Person() : d(new KCalCore::Person::Private) { } Person::Person(const QString &name, const QString &email) : d(new KCalCore::Person::Private) { d->mName = name; d->mEmail = email; } Person::Person(const Person &person) : d(new KCalCore::Person::Private(*person.d)) { } Person::~Person() { delete d; } bool KCalCore::Person::operator==(const Person &person) const { return d->mName == person.d->mName && d->mEmail == person.d->mEmail; } bool KCalCore::Person::operator!=(const Person &person) const { return !(*this == person); } Person &KCalCore::Person::operator=(const Person &person) { // check for self assignment if (&person == this) { return *this; } *d = *person.d; return *this; } -QString Person::fullName() const +QString KCalCore::fullNameHelper(const QString &name, const QString &email) { - if (d->mName.isEmpty()) { - return d->mEmail; - } else { - if (d->mEmail.isEmpty()) { - return d->mName; - } else { - // Taken from KContacts::Addressee::fullEmail - QString name = d->mName; - QRegExp needQuotes(QStringLiteral("[^ 0-9A-Za-z\\x0080-\\xFFFF]")); - bool weNeedToQuote = name.indexOf(needQuotes) != -1; - if (weNeedToQuote) { - if (name[0] != QLatin1Char('"')) { - name.prepend(QLatin1Char('"')); - } - if (name[ name.length() - 1 ] != QLatin1Char('"')) { - name.append(QLatin1Char('"')); - } - } - return name + QStringLiteral(" <") + d->mEmail + QLatin1Char('>'); + if (name.isEmpty()) { + return email; + } + if (email.isEmpty()) { + return name; + } + // Taken from KContacts::Addressee::fullEmail + QString fullName = name; + QRegExp needQuotes(QStringLiteral("[^ 0-9A-Za-z\\x0080-\\xFFFF]")); + bool weNeedToQuote = name.indexOf(needQuotes) != -1; + if (weNeedToQuote) { + if (fullName[0] != QLatin1Char('"')) { + fullName.prepend(QLatin1Char('"')); + } + if (fullName[ fullName.length() - 1 ] != QLatin1Char('"')) { + fullName.append(QLatin1Char('"')); } } + return fullName + QStringLiteral(" <") + email + QLatin1Char('>'); +} + +QString Person::fullName() const +{ + return fullNameHelper(d->mName, d->mEmail); } QString Person::name() const { return d->mName; } QString Person::email() const { return d->mEmail; } bool Person::isEmpty() const { return d->mEmail.isEmpty() && d->mName.isEmpty(); } void Person::setName(const QString &name) { d->mName = name; } void Person::setEmail(const QString &email) { if (email.startsWith(QLatin1String("mailto:"), Qt::CaseInsensitive)) { d->mEmail = email.mid(7); } else { d->mEmail = email; } } bool Person::isValidEmail(const QString &email) { const int pos = email.lastIndexOf(QLatin1Char('@')); return (pos > 0) && (email.lastIndexOf(QLatin1Char('.')) > pos) && ((email.length() - pos) > 4); } uint KCalCore::qHash(const KCalCore::Person &key) { return qHash(key.fullName()); } QDataStream &KCalCore::operator<<(QDataStream &stream, const KCalCore::Person::Ptr &person) { return stream << person->d->mName << person->d->mEmail << (int)(0); } QDataStream &KCalCore::operator>>(QDataStream &stream, Person::Ptr &person) { QString name, email; int count; stream >> name >> email >> count; Person::Ptr person_tmp(new Person(name, email)); person.swap(person_tmp); return stream; } // The following function was lifted directly from KPIMUtils // in order to eliminate the dependency on that library. // Any changes made here should be ported there, and vice versa. static bool extractEmailAddressAndName(const QString &aStr, QString &mail, QString &name) { name.clear(); mail.clear(); const int len = aStr.length(); const char cQuotes = '"'; bool bInComment = false; bool bInQuotesOutsideOfEmail = false; int i = 0, iAd = 0, iMailStart = 0, iMailEnd = 0; QChar c; unsigned int commentstack = 0; // Find the '@' of the email address // skipping all '@' inside "(...)" comments: while (i < len) { c = aStr[i]; if (QLatin1Char('(') == c) { commentstack++; } if (QLatin1Char(')') == c) { commentstack--; } bInComment = commentstack != 0; if (QLatin1Char('"') == c && !bInComment) { bInQuotesOutsideOfEmail = !bInQuotesOutsideOfEmail; } if (!bInComment && !bInQuotesOutsideOfEmail) { if (QLatin1Char('@') == c) { iAd = i; break; // found it } } ++i; } if (!iAd) { // We suppose the user is typing the string manually and just // has not finished typing the mail address part. // So we take everything that's left of the '<' as name and the rest as mail for (i = 0; len > i; ++i) { c = aStr[i]; if (QLatin1Char('<') != c) { name.append(c); } else { break; } } mail = aStr.mid(i + 1); if (mail.endsWith(QLatin1Char('>'))) { mail.truncate(mail.length() - 1); } } else { // Loop backwards until we find the start of the string // or a ',' that is outside of a comment // and outside of quoted text before the leading '<'. bInComment = false; bInQuotesOutsideOfEmail = false; for (i = iAd - 1; 0 <= i; --i) { c = aStr[i]; if (bInComment) { if (QLatin1Char('(') == c) { if (!name.isEmpty()) { name.prepend(QLatin1Char(' ')); } bInComment = false; } else { name.prepend(c); // all comment stuff is part of the name } } else if (bInQuotesOutsideOfEmail) { if (QLatin1Char(cQuotes) == c) { bInQuotesOutsideOfEmail = false; } else if (c != QLatin1Char('\\')) { name.prepend(c); } } else { // found the start of this addressee ? if (QLatin1Char(',') == c) { break; } // stuff is before the leading '<' ? if (iMailStart) { if (QLatin1Char(cQuotes) == c) { bInQuotesOutsideOfEmail = true; // end of quoted text found } else { name.prepend(c); } } else { switch (c.toLatin1()) { case '<': iMailStart = i; break; case ')': if (!name.isEmpty()) { name.prepend(QLatin1Char(' ')); } bInComment = true; break; default: if (QLatin1Char(' ') != c) { mail.prepend(c); } } } } } name = name.simplified(); mail = mail.simplified(); if (mail.isEmpty()) { return false; } mail.append(QLatin1Char('@')); // Loop forward until we find the end of the string // or a ',' that is outside of a comment // and outside of quoted text behind the trailing '>'. bInComment = false; bInQuotesOutsideOfEmail = false; int parenthesesNesting = 0; for (i = iAd + 1; len > i; ++i) { c = aStr[i]; if (bInComment) { if (QLatin1Char(')') == c) { if (--parenthesesNesting == 0) { bInComment = false; if (!name.isEmpty()) { name.append(QLatin1Char(' ')); } } else { // nested ")", add it name.append(QLatin1Char(')')); // name can't be empty here } } else { if (QLatin1Char('(') == c) { // nested "(" ++parenthesesNesting; } name.append(c); // all comment stuff is part of the name } } else if (bInQuotesOutsideOfEmail) { if (QLatin1Char(cQuotes) == c) { bInQuotesOutsideOfEmail = false; } else if (c != QLatin1Char('\\')) { name.append(c); } } else { // found the end of this addressee ? if (QLatin1Char(',') == c) { break; } // stuff is behind the trailing '>' ? if (iMailEnd) { if (QLatin1Char(cQuotes) == c) { bInQuotesOutsideOfEmail = true; // start of quoted text found } else { name.append(c); } } else { switch (c.toLatin1()) { case '>': iMailEnd = i; break; case '(': if (!name.isEmpty()) { name.append(QLatin1Char(' ')); } if (++parenthesesNesting > 0) { bInComment = true; } break; default: if (QLatin1Char(' ') != c) { mail.append(c); } } } } } } name = name.simplified(); mail = mail.simplified(); return !(name.isEmpty() || mail.isEmpty()); } Person::Ptr Person::fromFullName(const QString &fullName) { QString email, name; extractEmailAddressAndName(fullName, email, name); return Person::Ptr(new Person(name, email)); } diff --git a/src/person.h b/src/person.h index 553f837ab..af8990e91 100644 --- a/src/person.h +++ b/src/person.h @@ -1,211 +1,210 @@ /* This file is part of the kcalcore library. Copyright (c) 2001-2003 Cornelius Schumacher Copyright (C) 2003-2004 Reinhold Kainhofer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Person class. @author Cornelius Schumacher \ @author Reinhold Kainhofer \ */ #ifndef KCALCORE_PERSON_H #define KCALCORE_PERSON_H #include "kcalcore_export.h" #include #include #include #include namespace KCalCore { /** @brief Represents a person, by name and email address. This class represents a person, with a name and an email address. It supports the "FirstName LastName\ " format. */ class KCALCORE_EXPORT Person { public: /** A shared pointer to a Person object. */ typedef QSharedPointer Ptr; /** List of persons. */ typedef QVector List; /** Constructs a blank person. */ Person(); /** Constructs a person with name and email address taken from @p fullName. @param fullName is the name and email of the person in the form "FirstName LastName \". @return A Person object pointer. */ static Person::Ptr fromFullName(const QString &fullName); /** Constructs a person with the name @p name and email address @p email. @param name is the name of this person. @param email is the email address of this person. */ Person(const QString &name, const QString &email); /** Constructs a person as a copy of another person object. @param person is the person to copy. */ Person(const Person &person); /** Destroys a person. */ virtual ~Person(); /** Returns true if the person name and email address are empty. */ Q_REQUIRED_RESULT bool isEmpty() const; /** Returns the full name of this person. @return A QString containing the person's full name in the form "FirstName LastName \". */ Q_REQUIRED_RESULT QString fullName() const; /** Sets the name of the person to @p name. @param name is the name of this person. @see name() */ void setName(const QString &name); /** Returns the person name string. @see setName() */ Q_REQUIRED_RESULT QString name() const; /** Sets the email address for this person to @p email. @param email is the email address for this person. @see email() */ void setEmail(const QString &email); /** Returns the email address for this person. @return A QString containing the person's email address. @see setEmail() */ Q_REQUIRED_RESULT QString email() const; /** Returns true if person's email address is valid. Simple email validity check, test that there: * is at least one @ * is at least one character in the local part * is at least one dot in the domain part * is at least four characters in the domain (assuming that no-one has an address at the tld, that the tld is at least 2 chars) @param email is the email address to validate */ Q_REQUIRED_RESULT static bool isValidEmail(const QString &email); /** Compares this with @p person for equality. @param person is the person to compare. */ bool operator==(const Person &person) const; /** Compares this with @p person for non-equality. @param person is the person to compare. */ bool operator!=(const Person &person) const; /** Sets this person equal to @p person. @param person is the person to copy. */ Person &operator=(const Person &person); private: //@cond PRIVATE class Private; Private *const d; //@endcond - // TODO_KDE5: FIXME: This operator does slicing,if the object is in fact one of the derived classes (Attendee) friend KCALCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KCalCore::Person::Ptr &person); friend KCALCORE_EXPORT QDataStream &operator>>(QDataStream &s, KCalCore::Person::Ptr &person); }; /** Serializes the @p person object into the @p stream. */ KCALCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KCalCore::Person::Ptr &person); /** Initializes the @p person object from the @p stream. */ KCALCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KCalCore::Person::Ptr &person); /** Return a hash value for a Person argument. @param key is a Person. */ KCALCORE_EXPORT uint qHash(const KCalCore::Person &key); } //@cond PRIVATE Q_DECLARE_TYPEINFO(KCalCore::Person::Ptr, Q_MOVABLE_TYPE); Q_DECLARE_METATYPE(KCalCore::Person::Ptr) //@endcond #endif diff --git a/src/person_p.h b/src/person_p.h new file mode 100644 index 000000000..2ed2f013c --- /dev/null +++ b/src/person_p.h @@ -0,0 +1,32 @@ +/* + This file is part of the kcalcore library. + + Copyright (c) 2019 Volker Krause + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KCALCORE_PERSON_P_H +#define KCALCORE_PERSON_P_H + +class QString; + +namespace KCalCore +{ + QString fullNameHelper(const QString &name, const QString &email); +} + +#endif