File indexing completed on 2024-05-12 05:11:11

0001 /*  -*- mode: C++ -*-
0002     This file is part of Akonadi.
0003     SPDX-FileCopyrightText: 2005 Andreas Gungl <a.gungl@gmx.de>
0004     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0005     SPDX-FileCopyrightText: 2010 Leo Franchi <lfranchi@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include "akonadi-mime_export.h"
0012 #include <QDebug>
0013 #include <QSet>
0014 
0015 class QString;
0016 
0017 namespace Akonadi
0018 {
0019 //---------------------------------------------------------------------------
0020 /**
0021   @short Akonadi KMime Message Status.
0022   @author Andreas Gungl <a.gungl@gmx.de>
0023 
0024   The class encapsulates the handling of the different flags
0025   which describe the status of a message.
0026   The flags themselves are not intended to be used outside this class.
0027 
0028   In the status pairs Watched/Ignored and Spam/Ham, there both
0029   values can't be set at the same time, however they can
0030   be unset at the same time.
0031 
0032   Note that this class does not sync with the Akonadi storage. It is
0033   used as an in-memory helper when manipulating Akonadi items.
0034 
0035   @since 4.6.
0036 */
0037 class AKONADI_MIME_EXPORT MessageStatus
0038 {
0039     Q_GADGET
0040     Q_PROPERTY(bool isOfUnknownStatus READ isOfUnknownStatus CONSTANT)
0041     Q_PROPERTY(bool isRead READ isRead WRITE setRead)
0042     Q_PROPERTY(bool isDeleted READ isDeleted WRITE setDeleted)
0043     Q_PROPERTY(bool isReplied READ isReplied WRITE setReplied)
0044     Q_PROPERTY(bool isForwarded READ isForwarded WRITE setForwarded)
0045     Q_PROPERTY(bool isQueued READ isQueued WRITE setQueued)
0046     Q_PROPERTY(bool isSent READ isSent WRITE setSent)
0047     Q_PROPERTY(bool isImportant READ isImportant WRITE setImportant)
0048     Q_PROPERTY(bool isWatched READ isWatched WRITE setWatched)
0049     Q_PROPERTY(bool isIgnored READ isIgnored WRITE setIgnored)
0050     Q_PROPERTY(bool isSpam READ isSpam WRITE setSpam)
0051     Q_PROPERTY(bool isHam READ isHam WRITE setHam)
0052     Q_PROPERTY(bool isToAct READ isToAct WRITE setToAct)
0053     Q_PROPERTY(bool hasAttachment READ hasAttachment WRITE setHasAttachment)
0054     Q_PROPERTY(bool hasInvitation READ hasInvitation WRITE setHasInvitation)
0055     Q_PROPERTY(bool isEncrypted READ isEncrypted WRITE setEncrypted)
0056     Q_PROPERTY(bool isSigned READ isSigned WRITE setSigned)
0057     Q_PROPERTY(bool hasError READ hasError WRITE setHasError)
0058 
0059 public:
0060     /** Constructor - sets status initially to unknown. */
0061     MessageStatus();
0062 
0063     /** Compare the status with that from another instance.
0064         @return true if the stati are equal, false if different.
0065         @param other message status to compare with current object
0066     */
0067     bool operator==(MessageStatus other) const;
0068 
0069     /** Compare the status with that from another instance.
0070         @return true if the stati are equal, false if different.
0071         @param other message status to compare with current object
0072     */
0073     bool operator!=(MessageStatus other) const;
0074 
0075     /** Check, if some of the flags in the status match
0076         with those flags from another instance.
0077         @return true if at least one flag is set in both stati.
0078         @param other message status to compare objects' flags
0079     */
0080     bool operator&(MessageStatus other) const;
0081 
0082     /** Clear all status flags, this resets to unknown. */
0083     void clear();
0084 
0085     /** Set / add stati described by another MessageStatus object.
0086         This can be used to merge in multiple stati at once without
0087         using the single setter methods.
0088         However, internally the setters are used anyway to ensure the
0089         integrity of the resulting status.
0090         @param other message status to set
0091     */
0092     void set(MessageStatus other);
0093 
0094     /** Toggle one or more stati described by another MessageStatus object.
0095         Internally the setters are used to ensure the integrity of the
0096         resulting status.
0097         @param other message status to toggle
0098     */
0099     void toggle(MessageStatus other);
0100 
0101     /* ----- getters ----------------------------------------------------- */
0102 
0103     /** Check for Unknown status.
0104         @return true if status is unknown.
0105     */
0106     [[nodiscard]] bool isOfUnknownStatus() const;
0107 
0108     /** Check for Read status. Note that ignored messages are read.
0109         @return true if status is read.
0110     */
0111     [[nodiscard]] bool isRead() const;
0112 
0113     /** Check for Deleted status.
0114         @return true if status is deleted.
0115     */
0116     [[nodiscard]] bool isDeleted() const;
0117 
0118     /** Check for Replied status.
0119         @return true if status is replied.
0120     */
0121     [[nodiscard]] bool isReplied() const;
0122 
0123     /** Check for Forwarded status.
0124         @return true if status is forwarded.
0125     */
0126     [[nodiscard]] bool isForwarded() const;
0127 
0128     /** Check for Queued status.
0129         @return true if status is queued.
0130     */
0131     [[nodiscard]] bool isQueued() const;
0132 
0133     /** Check for Sent status.
0134         @return true if status is sent.
0135     */
0136     [[nodiscard]] bool isSent() const;
0137 
0138     /** Check for Important status.
0139         @return true if status is important.
0140     */
0141     [[nodiscard]] bool isImportant() const;
0142 
0143     /** Check for Watched status.
0144         @return true if status is watched.
0145     */
0146     [[nodiscard]] bool isWatched() const;
0147 
0148     /** Check for Ignored status.
0149         @return true if status is ignored.
0150     */
0151     [[nodiscard]] bool isIgnored() const;
0152 
0153     /** Check for ToAct status.
0154         @return true if status is action item.
0155     */
0156     [[nodiscard]] bool isToAct() const;
0157 
0158     /** Check for Spam status.
0159         @return true if status is spam.
0160     */
0161     [[nodiscard]] bool isSpam() const;
0162 
0163     /** Check for Ham status.
0164         @return true if status is not spam.
0165     */
0166     [[nodiscard]] bool isHam() const;
0167 
0168     /** Check for Attachment status.
0169         @return true if status indicates an attachment.
0170     */
0171     [[nodiscard]] bool hasAttachment() const;
0172 
0173     /** Check for Invitation status.
0174         @return true if status indicates an invitation.
0175     */
0176     [[nodiscard]] bool hasInvitation() const;
0177 
0178     /** Check for Signed status.
0179         @return true if status is signed.
0180     */
0181     [[nodiscard]] bool isSigned() const;
0182 
0183     /** Check for Encrypted status.
0184         @return true if status is encrypted.
0185     */
0186     [[nodiscard]] bool isEncrypted() const;
0187 
0188     /** Check for error status.
0189         @return true if status indicates an error.
0190     */
0191     [[nodiscard]] bool hasError() const;
0192 
0193     /* ----- setters ----------------------------------------------------- */
0194 
0195     /**
0196      * Set the status to read
0197      * @param read new read status
0198      */
0199     void setRead(bool read = true);
0200 
0201     /** Set the status for deleted.
0202         @param deleted Set (true) or unset (false) this status flag.
0203     */
0204     void setDeleted(bool deleted = true);
0205 
0206     /** Set the status for replied.
0207         @param replied Set (true) or unset (false) this status flag.
0208     */
0209     void setReplied(bool replied = true);
0210 
0211     /** Set the status for forwarded.
0212         @param forwarded Set (true) or unset (false) this status flag.
0213     */
0214     void setForwarded(bool forwarded = true);
0215 
0216     /** Set the status for queued.
0217         @param queued Set (true) or unset (false) this status flag.
0218     */
0219     void setQueued(bool queued = true);
0220 
0221     /** Set the status for sent.
0222         @param sent Set (true) or unset (false) this status flag.
0223     */
0224     void setSent(bool sent = true);
0225 
0226     /** Set the status for important.
0227         @param important Set (true) or unset (false) this status flag.
0228     */
0229     void setImportant(bool important = true);
0230 
0231     /** Set the status to watched.
0232         @param watched Set (true) or unset (false) this status flag.
0233     */
0234     void setWatched(bool watched = true);
0235 
0236     /** Set the status to ignored.
0237         @param ignored Set (true) or unset (false) this status flag.
0238     */
0239     void setIgnored(bool ignored = true);
0240 
0241     /** Set the status to action item.
0242         @param toAct Set (true) or unset (false) this status flag.
0243     */
0244     void setToAct(bool toAct = true);
0245 
0246     /** Set the status to spam.
0247         @param spam Set (true) or unset (false) this status flag.
0248     */
0249     void setSpam(bool spam = true);
0250 
0251     /** Set the status to not spam.
0252         @param ham Set (true) or unset (false) this status flag.
0253     */
0254     void setHam(bool ham = true);
0255 
0256     /** Set the status for an attachment.
0257         @param hasAttachment Set (true) or unset (false) this status flag.
0258     */
0259     void setHasAttachment(bool hasAttachment = true);
0260 
0261     /** Set the status for an invitation.
0262         @param hasInvitation Set (true) or unset (false) this status flag.
0263     */
0264     void setHasInvitation(bool hasInvitation = true);
0265 
0266     /** Set the status to signed.
0267         @param value Set (true) or unset (false) this status flag.
0268     */
0269     void setSigned(bool value = true);
0270 
0271     /** Set the status to encrypted.
0272         @param value Set (true) or unset (false) this status flag.
0273     */
0274     void setEncrypted(bool value = true);
0275 
0276     /** Set the status to error.
0277         @param value Set (true) or unset (false) this status flag.
0278     */
0279     void setHasError(bool value = true);
0280 
0281     /* ----- state representation  --------------------------------------- */
0282 
0283     /** Get the status as a whole e.g. for storage in an index.
0284      D on't manipulte the *index via this value, this bypasses
0285      all integrity checks in the setter methods.
0286      @return The status encoded in bits.
0287      */
0288     [[nodiscard]] qint32 toQInt32() const;
0289 
0290     /** Set the status as a whole e.g. for reading from an index.
0291         Don't manipulte the index via this value, this bypasses
0292         all integrity checks in the setter methods.
0293         @param status The status encoded in bits to be set in this instance.
0294     */
0295     void fromQInt32(qint32 status);
0296 
0297     /** Convert the status to a string representation.
0298         @return A string containing coded uppercase letters
0299                 which describe the status.
0300 
0301         @note This code is legacy for the KMail1 indexes
0302     */
0303     [[nodiscard]] QString statusStr() const;
0304 
0305     /** Set the status based on a string representation.
0306         @param aStr The status string to be analyzed.
0307                     Normally it is a string obtained using
0308                     getStatusStr().
0309 
0310         @note This code is legacy for the KMail1 indexes
0311     */
0312     void setStatusFromStr(const QString &aStr);
0313 
0314     /** Get the status as a whole e.g. for storage as IMAP flags.
0315         @return The status encoded in flags.
0316     */
0317     [[nodiscard]] QSet<QByteArray> statusFlags() const;
0318 
0319     /** Set the status as a whole e.g. for reading from IMAP flags.
0320         @param flags set of flags for status as a whole
0321     */
0322     void setStatusFromFlags(const QSet<QByteArray> &flags);
0323 
0324     /* ----- static accessors to simple states --------------------------- */
0325 
0326     /** Return a special status that expresses Unread.
0327         This status can only be used for comparison with other states.
0328     */
0329     static const MessageStatus statusUnread();
0330 
0331     /** Return a predefined status initialized as Read as is useful
0332         e.g. when providing a state for comparison.
0333         @return A reference to a status instance initialized as Read.
0334     */
0335     static const MessageStatus statusRead();
0336 
0337     /** Return a predefined status initialized as Deleted as is useful
0338         e.g. when providing a state for comparison.
0339         @return A reference to a status instance initialized as Deleted.
0340     */
0341     static const MessageStatus statusDeleted();
0342 
0343     /** Return a predefined status initialized as Replied as is useful
0344         e.g. when providing a state for comparison.
0345         @return A reference to a status instance initialized as Replied.
0346     */
0347     static const MessageStatus statusReplied();
0348 
0349     /** Return a predefined status initialized as Forwarded as is useful
0350         e.g. when providing a state for comparison.
0351         @return A reference to a status instance initialized as Forwarded.
0352     */
0353     static const MessageStatus statusForwarded();
0354 
0355     /** Return a predefined status initialized as Queued as is useful
0356         e.g. when providing a state for comparison.
0357         @return A reference to a status instance initialized as Queued.
0358     */
0359     static const MessageStatus statusQueued();
0360 
0361     /** Return a predefined status initialized as Sent as is useful
0362         e.g. when providing a state for comparison.
0363         @return A reference to a status instance initialized as Sent.
0364     */
0365     static const MessageStatus statusSent();
0366 
0367     /** Return a predefined status initialized as Important as is useful
0368         e.g. when providing a state for comparison.
0369         @return A reference to a status instance initialized as Important.
0370     */
0371     static const MessageStatus statusImportant();
0372 
0373     /** Return a predefined status initialized as Watched as is useful
0374         e.g. when providing a state for comparison.
0375         @return A reference to a status instance initialized as Watched.
0376     */
0377     static const MessageStatus statusWatched();
0378 
0379     /** Return a predefined status initialized as Ignored as is useful
0380         e.g. when providing a state for comparison.
0381         @return A reference to a status instance initialized as Ignored.
0382     */
0383     static const MessageStatus statusIgnored();
0384 
0385     /** Return a predefined status initialized as Action Item as is useful
0386         e.g. when providing a state for comparison.
0387         @return A reference to a status instance initialized as ToAct.
0388     */
0389     static const MessageStatus statusToAct();
0390 
0391     /** Return a predefined status initialized as Spam as is useful
0392         e.g. when providing a state for comparison.
0393         @return A reference to a status instance initialized as Spam.
0394     */
0395     static const MessageStatus statusSpam();
0396 
0397     /** Return a predefined status initialized as Ham as is useful
0398         e.g. when providing a state for comparison.
0399         @return A reference to a status instance initialized as Ham.
0400     */
0401     static const MessageStatus statusHam();
0402 
0403     /** Return a predefined status initialized as Attachment as is useful
0404         e.g. when providing a state for comparison.
0405         @return A reference to a status instance initialized as Attachment.
0406     */
0407     static const MessageStatus statusHasAttachment();
0408 
0409     /** Return a predefined status initialized as Invitation as is useful
0410         e.g. when providing a state for comparison.
0411         @return A reference to a status instance initialized as Invitation.
0412     */
0413     static const MessageStatus statusHasInvitation();
0414 
0415     /** Return a predefined status initialized as Signed as is useful
0416         e.g. when providing a state for comparison.
0417         @return A reference to a status instance initialized as Signed.
0418     */
0419     static const MessageStatus statusSigned();
0420 
0421     /** Return a predefined status initialized as Encrypted as is useful
0422         e.g. when providing a state for comparison.
0423         @return A reference to a status instance initialized as Encrypted.
0424     */
0425     static const MessageStatus statusEncrypted();
0426 
0427     /** Return a predefined status initialized as Error as is useful
0428         e.g. when providing a state for comparison.
0429         @return A reference to a status instance initialized as Error.
0430     */
0431     static const MessageStatus statusHasError();
0432 
0433 private:
0434     quint32 mStatus;
0435 };
0436 } // namespace Akonadi
0437 
0438 AKONADI_MIME_EXPORT QDebug operator<<(QDebug d, const Akonadi::MessageStatus &t);