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

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2011 Christian Mollekopf <chrigi_1@fastmail.fm>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadi-notes_export.h"
0010 
0011 #include <QMap>
0012 #include <QUrl>
0013 
0014 #include <memory>
0015 
0016 class QDateTime;
0017 class QString;
0018 
0019 template<typename T> class QSharedPointer;
0020 
0021 namespace KMime
0022 {
0023 class Message;
0024 using MessagePtr = QSharedPointer<Message>;
0025 }
0026 namespace Akonadi
0027 {
0028 namespace NoteUtils
0029 {
0030 /**
0031  * @return mimetype for notes
0032  * @since 4.8
0033  */
0034 AKONADI_NOTES_EXPORT QString noteMimeType();
0035 
0036 /**
0037  * @return icon for notes
0038  * @since 4.8
0039  */
0040 AKONADI_NOTES_EXPORT QString noteIconName();
0041 
0042 class AttachmentPrivate;
0043 
0044 /**
0045  * An attachment for a note
0046  * @since 4.9
0047  */
0048 class AKONADI_NOTES_EXPORT Attachment
0049 {
0050 public:
0051     /**
0052      * Create an attachment referencing a url only
0053      */
0054     Attachment();
0055     Attachment(const QUrl &url, const QString &mimetype);
0056     /**
0057      * Create an attachment with the content stored inline
0058      */
0059     Attachment(const QByteArray &data, const QString &mimetype);
0060     Attachment(const Attachment &other);
0061     ~Attachment();
0062 
0063     bool operator==(const Attachment &a) const;
0064     void operator=(const Attachment &a);
0065 
0066     /**
0067      * Returns the url for url-only attachments
0068      */
0069     [[nodiscard]] QUrl url() const;
0070 
0071     /**
0072      * Returns the date for inline attachments
0073      */
0074     [[nodiscard]] QByteArray data() const;
0075 
0076     /**
0077      * Sets the unique identifier of the attachment
0078      *
0079      * It can be used to refer to attachment from the note itself
0080      *
0081      * @since 5.15
0082      */
0083     void setContentID(const QString &contentID);
0084 
0085     /**
0086      * Returns the unique identifier for inline attachment
0087      *
0088      * @since 5.15
0089      */
0090     [[nodiscard]] QString contentID() const;
0091 
0092     /**
0093      * Set this to true if inline data provided via ctor
0094      * is already base64 encoded. Default value is false.
0095      *
0096      * @since 5.15
0097      */
0098     void setDataBase64Encoded(bool encoded);
0099 
0100     /**
0101      * Returns true if data is already base64-encoded
0102      *
0103      * @since 5.15
0104      */
0105     [[nodiscard]] bool dataBase64Encoded() const;
0106 
0107     /**
0108      * Returns the mimetype
0109      */
0110     [[nodiscard]] QString mimetype() const;
0111 
0112     /**
0113      * Sets the label to be presented to the user
0114      */
0115     void setLabel(const QString &label);
0116 
0117     /**
0118      * Returns the label of the attachment
0119      */
0120     [[nodiscard]] QString label() const;
0121 
0122 private:
0123     //@cond PRIVATE
0124     std::unique_ptr<AttachmentPrivate> const d_ptr;
0125     Q_DECLARE_PRIVATE(Attachment)
0126     //@endcond
0127 };
0128 
0129 class NoteMessageWrapperPrivate;
0130 
0131 /**
0132  * A convenience wrapper around KMime::MessagePtr for notes
0133  *
0134  * This is the format used by the Akonotes Resource
0135  *
0136  * A note has the following properties:
0137  * uid: globally unique identifier (generated if empty)
0138  * creationDate: timestamp when the note was created (generated if empty)
0139  * lastModified: lastModified (generated if empty)
0140  * classification: one of private, confidential, public. This is only meant as an indication to the user.
0141  * title: title of the note
0142  * text: textual content
0143  * from: author (generated if empty)
0144  * attachments: inline or url only
0145  * custom: key value pair for custom values
0146  *
0147  * Reading a note from an Akonotes akonadi item:
0148  * @code
0149  * if ( item.hasPayload<KMime::MessagePtr>() ) {
0150  *   NoteUtils::NoteMessageWrapper note(item.payload<KMime::MessagePtr>());
0151  *   qCDebug(AKONADINOTES_LOG) << note.text();
0152  *   textIsRich = messageWrapper.textFormat() == Qt::RichText;
0153  * }
0154  * @endcode
0155  *
0156  * Setting the note as payload of an akonadi Item
0157  * @code
0158  * item.setMimeType(NoteUtils::noteMimeType());
0159  * NoteUtils::NoteMessageWrapper note;
0160  * note.setTitle( "title" );
0161  * note.setText( "text" );
0162  * note.setFrom( QString::fromLatin1( "MyApplication@kde4" ) );
0163  * item.setPayload( note.message() );
0164  * @endcode
0165  *
0166  * @author Christian Mollekopf <chrigi_1@fastmail.fm>
0167  * @since 4.8
0168  */
0169 class AKONADI_NOTES_EXPORT NoteMessageWrapper
0170 {
0171 public:
0172     NoteMessageWrapper();
0173     explicit NoteMessageWrapper(const KMime::MessagePtr &msg);
0174     ~NoteMessageWrapper();
0175 
0176     /**
0177      * Set the uid of the note
0178      * @param uid should be globally unique
0179      */
0180     void setUid(const QString &uid);
0181 
0182     /**
0183      * Returns the uid of the note
0184      */
0185     [[nodiscard]] QString uid() const;
0186 
0187     enum Classification { Public, Private, Confidential };
0188 
0189     /**
0190      * Set the classification of the note
0191      */
0192     void setClassification(Classification);
0193 
0194     /**
0195      * Returns the classification of the note
0196      */
0197     [[nodiscard]] Classification classification() const;
0198 
0199     /**
0200      * Set the title of the note
0201      */
0202     void setTitle(const QString &title);
0203 
0204     /**
0205      * Returns the title of the note
0206      */
0207     [[nodiscard]] QString title() const;
0208 
0209     /**
0210      * Set the text of the note
0211      *
0212      * @param format only Qt::PlainText and Qt::RichText is supported
0213      */
0214     void setText(const QString &text, Qt::TextFormat format = Qt::PlainText);
0215 
0216     /**
0217      * Returns the text of the note
0218      */
0219     [[nodiscard]] QString text() const;
0220 
0221     /**
0222      * @return Qt::PlainText or Qt::RichText
0223      */
0224     [[nodiscard]] Qt::TextFormat textFormat() const;
0225 
0226     /**
0227      * @return plaintext version of the text (if richtext)
0228      */
0229     [[nodiscard]] QString toPlainText() const;
0230 
0231     /**
0232      * Set the creation date of the note (stored in the mime header)
0233      */
0234     void setCreationDate(const QDateTime &creationDate);
0235 
0236     /**
0237      * Returns the creation date of the note
0238      */
0239     [[nodiscard]] QDateTime creationDate() const;
0240 
0241     /**
0242      * Set the lastModified-date of the note
0243      */
0244     void setLastModifiedDate(const QDateTime &lastModifiedDate);
0245 
0246     /**
0247      * Returns the lastModified-date of the note
0248      */
0249     [[nodiscard]] QDateTime lastModifiedDate() const;
0250 
0251     /**
0252      * Set the origin (creator) of the note (stored in the mime header)
0253      * This is usually the application creating the note.
0254      * @param from must be an address in the style of foo@kde.org.
0255      */
0256     void setFrom(const QString &from);
0257 
0258     /**
0259      * Returns the origin (creator) of the note
0260      */
0261     [[nodiscard]] QString from() const;
0262 
0263     /**
0264      * Returns a reference to the list of attachments of the note
0265      */
0266     [[nodiscard]] QList<Attachment> &attachments();
0267 
0268     /**
0269      * Returns a reference to the custom-value map
0270      * @return key-value map containing all custom values
0271      */
0272     [[nodiscard]] QMap<QString, QString> &custom();
0273 
0274     /**
0275      * Assemble a KMime message with the given values
0276      *
0277      * The message can then i.e. be stored inside an akonadi item
0278      */
0279     KMime::MessagePtr message() const;
0280 
0281 private:
0282     //@cond PRIVATE
0283     Q_DISABLE_COPY(NoteMessageWrapper)
0284     std::unique_ptr<NoteMessageWrapperPrivate> const d_ptr;
0285     Q_DECLARE_PRIVATE(NoteMessageWrapper)
0286     //@endcond
0287 };
0288 
0289 }
0290 }
0291 
0292 Q_DECLARE_TYPEINFO(Akonadi::NoteUtils::Attachment, Q_RELOCATABLE_TYPE);