File indexing completed on 2024-04-14 03:50:39

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0005   SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0006   SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0007 
0008   SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 /**
0011   @file
0012   This file is part of the API for handling calendar data and
0013   defines the Attendee class.
0014 
0015   @brief
0016   Represents information related to an attendee of an Calendar Incidence.
0017 
0018   @author Cornelius Schumacher \<schumacher@kde.org\>
0019 */
0020 
0021 #include "attendee.h"
0022 #include "person.h"
0023 #include "person_p.h"
0024 
0025 #include <QDataStream>
0026 
0027 using namespace KCalendarCore;
0028 
0029 /**
0030   Private class that helps to provide binary compatibility between releases.
0031   @internal
0032 */
0033 //@cond PRIVATE
0034 class Q_DECL_HIDDEN KCalendarCore::Attendee::Private : public QSharedData
0035 {
0036 public:
0037     void setCuType(CuType cuType);
0038     void setCuType(const QString &cuType);
0039     CuType cuType() const;
0040     QString cuTypeStr() const;
0041 
0042     bool mRSVP = false;
0043     Role mRole = Attendee::ReqParticipant;
0044     PartStat mStatus = Attendee::NeedsAction;
0045     mutable QString mUid;
0046     QString mDelegate;
0047     QString mDelegator;
0048     CustomProperties mCustomProperties;
0049     QString mName;
0050     QString mEmail;
0051 
0052 private:
0053     QString sCuType;
0054     CuType mCuType = Attendee::Individual;
0055 };
0056 //@endcond
0057 
0058 void KCalendarCore::Attendee::Private::setCuType(Attendee::CuType cuType)
0059 {
0060     mCuType = cuType;
0061     sCuType.clear();
0062 }
0063 
0064 void KCalendarCore::Attendee::Private::setCuType(const QString &cuType)
0065 {
0066     const QString upper = cuType.toUpper();
0067     if (upper == QLatin1String("INDIVIDUAL")) {
0068         setCuType(Attendee::Individual);
0069     } else if (upper == QLatin1String("GROUP")) {
0070         setCuType(Attendee::Group);
0071     } else if (upper == QLatin1String("RESOURCE")) {
0072         setCuType(Attendee::Resource);
0073     } else if (upper == QLatin1String("ROOM")) {
0074         setCuType(Attendee::Room);
0075     } else {
0076         setCuType(Attendee::Unknown);
0077         if (upper.startsWith(QLatin1String("X-")) || upper.startsWith(QLatin1String("IANA-"))) {
0078             sCuType = upper;
0079         }
0080     }
0081 }
0082 
0083 Attendee::CuType KCalendarCore::Attendee::Private::cuType() const
0084 {
0085     return mCuType;
0086 }
0087 
0088 QString KCalendarCore::Attendee::Private::cuTypeStr() const
0089 {
0090     switch (mCuType) {
0091     case Attendee::Individual:
0092         return QStringLiteral("INDIVIDUAL");
0093     case Attendee::Group:
0094         return QStringLiteral("GROUP");
0095     case Attendee::Resource:
0096         return QStringLiteral("RESOURCE");
0097     case Attendee::Room:
0098         return QStringLiteral("ROOM");
0099     case Attendee::Unknown:
0100         if (sCuType.isEmpty()) {
0101             return QStringLiteral("UNKNOWN");
0102         } else {
0103             return sCuType;
0104         }
0105     }
0106     return QStringLiteral("UNKNOWN");
0107 }
0108 
0109 Attendee::Attendee()
0110     : d(new Attendee::Private)
0111 {
0112 }
0113 
0114 Attendee::Attendee(const QString &name, const QString &email, bool rsvp, Attendee::PartStat status, Attendee::Role role, const QString &uid)
0115     : d(new Attendee::Private)
0116 {
0117     setName(name);
0118     setEmail(email);
0119     d->mRSVP = rsvp;
0120     d->mStatus = status;
0121     d->mRole = role;
0122     d->mUid = uid;
0123     d->setCuType(Attendee::Individual);
0124 }
0125 
0126 Attendee::Attendee(const Attendee &attendee)
0127     : d(attendee.d)
0128 {
0129 }
0130 
0131 Attendee::~Attendee() = default;
0132 
0133 bool Attendee::isNull() const
0134 {
0135     // isNull rather than isEmpty, as user code is actually creating empty but non-null attendees...
0136     return d->mName.isNull() && d->mEmail.isNull();
0137 }
0138 
0139 bool KCalendarCore::Attendee::operator==(const Attendee &attendee) const
0140 {
0141     return d->mUid == attendee.d->mUid && d->mRSVP == attendee.d->mRSVP && d->mRole == attendee.d->mRole && d->mStatus == attendee.d->mStatus
0142         && d->mDelegate == attendee.d->mDelegate && d->mDelegator == attendee.d->mDelegator && d->cuTypeStr() == attendee.d->cuTypeStr()
0143         && d->mName == attendee.d->mName && d->mEmail == attendee.d->mEmail;
0144 }
0145 
0146 bool KCalendarCore::Attendee::operator!=(const Attendee &attendee) const
0147 {
0148     return !operator==(attendee);
0149 }
0150 
0151 Attendee &KCalendarCore::Attendee::operator=(const Attendee &attendee)
0152 {
0153     // check for self assignment
0154     if (&attendee == this) {
0155         return *this;
0156     }
0157 
0158     d = attendee.d;
0159     return *this;
0160 }
0161 
0162 QString Attendee::name() const
0163 {
0164     return d->mName;
0165 }
0166 
0167 void Attendee::setName(const QString &name)
0168 {
0169     if (name.startsWith(QLatin1String("mailto:"), Qt::CaseInsensitive)) {
0170         d->mName = name.mid(7);
0171     } else {
0172         d->mName = name;
0173     }
0174 }
0175 
0176 QString Attendee::fullName() const
0177 {
0178     return fullNameHelper(d->mName, d->mEmail);
0179 }
0180 
0181 QString Attendee::email() const
0182 {
0183     return d->mEmail;
0184 }
0185 
0186 void Attendee::setEmail(const QString &email)
0187 {
0188     if (email.startsWith(QLatin1String("mailto:"), Qt::CaseInsensitive)) {
0189         d->mEmail = email.mid(7);
0190     } else {
0191         d->mEmail = email;
0192     }
0193 }
0194 
0195 void Attendee::setRSVP(bool r)
0196 {
0197     d->mRSVP = r;
0198 }
0199 
0200 bool Attendee::RSVP() const
0201 {
0202     return d->mRSVP;
0203 }
0204 
0205 void Attendee::setStatus(Attendee::PartStat status)
0206 {
0207     d->mStatus = status;
0208 }
0209 
0210 Attendee::PartStat Attendee::status() const
0211 {
0212     return d->mStatus;
0213 }
0214 
0215 void Attendee::setCuType(Attendee::CuType cuType)
0216 {
0217     d->setCuType(cuType);
0218 }
0219 
0220 void Attendee::setCuType(const QString &cuType)
0221 {
0222     d->setCuType(cuType);
0223 }
0224 
0225 Attendee::CuType Attendee::cuType() const
0226 {
0227     return d->cuType();
0228 }
0229 
0230 QString Attendee::cuTypeStr() const
0231 {
0232     return d->cuTypeStr();
0233 }
0234 
0235 void Attendee::setRole(Attendee::Role role)
0236 {
0237     d->mRole = role;
0238 }
0239 
0240 Attendee::Role Attendee::role() const
0241 {
0242     return d->mRole;
0243 }
0244 
0245 void Attendee::setUid(const QString &uid)
0246 {
0247     d->mUid = uid;
0248 }
0249 
0250 QString Attendee::uid() const
0251 {
0252     /* If Uid is empty, just use the pointer to Attendee (encoded to
0253      * string) as Uid. Only thing that matters is that the Uid is unique
0254      * insofar IncidenceBase is concerned, and this does that (albeit
0255      * not very nicely). If these are ever saved to disk, should use
0256      * (considerably more expensive) CalFormat::createUniqueId(). As Uid
0257      * is not part of Attendee in iCal std, it's fairly safe bet that
0258      * these will never hit disc though so faster generation speed is
0259      * more important than actually being forever unique.*/
0260     if (d->mUid.isEmpty()) {
0261         d->mUid = QString::number((qlonglong)d.constData());
0262     }
0263 
0264     return d->mUid;
0265 }
0266 
0267 void Attendee::setDelegate(const QString &delegate)
0268 {
0269     d->mDelegate = delegate;
0270 }
0271 
0272 QString Attendee::delegate() const
0273 {
0274     return d->mDelegate;
0275 }
0276 
0277 void Attendee::setDelegator(const QString &delegator)
0278 {
0279     d->mDelegator = delegator;
0280 }
0281 
0282 QString Attendee::delegator() const
0283 {
0284     return d->mDelegator;
0285 }
0286 
0287 void Attendee::setCustomProperty(const QByteArray &xname, const QString &xvalue)
0288 {
0289     d->mCustomProperties.setNonKDECustomProperty(xname, xvalue);
0290 }
0291 
0292 CustomProperties &Attendee::customProperties()
0293 {
0294     return d->mCustomProperties;
0295 }
0296 
0297 const CustomProperties &Attendee::customProperties() const
0298 {
0299     return d->mCustomProperties;
0300 }
0301 
0302 QDataStream &KCalendarCore::operator<<(QDataStream &stream, const KCalendarCore::Attendee &attendee)
0303 {
0304     KCalendarCore::Person p(attendee.name(), attendee.email());
0305     stream << p;
0306     return stream << attendee.d->mRSVP << int(attendee.d->mRole) << int(attendee.d->mStatus) << attendee.d->mUid << attendee.d->mDelegate
0307                   << attendee.d->mDelegator << attendee.d->cuTypeStr() << attendee.d->mCustomProperties;
0308 }
0309 
0310 QDataStream &KCalendarCore::operator>>(QDataStream &stream, KCalendarCore::Attendee &attendee)
0311 {
0312     bool RSVP;
0313     Attendee::Role role;
0314     Attendee::PartStat status;
0315     QString uid;
0316     QString delegate;
0317     QString delegator;
0318     QString cuType;
0319     CustomProperties customProperties;
0320     uint role_int;
0321     uint status_int;
0322 
0323     KCalendarCore::Person person;
0324     stream >> person;
0325     stream >> RSVP >> role_int >> status_int >> uid >> delegate >> delegator >> cuType >> customProperties;
0326 
0327     role = Attendee::Role(role_int);
0328     status = Attendee::PartStat(status_int);
0329 
0330     attendee = Attendee(person.name(), person.email(), RSVP, status, role, uid);
0331     attendee.setDelegate(delegate);
0332     attendee.setDelegator(delegator);
0333     attendee.setCuType(cuType);
0334     attendee.d->mCustomProperties = customProperties;
0335     return stream;
0336 }
0337 
0338 #include "moc_attendee.cpp"