File indexing completed on 2024-05-19 05:21:45

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kpimtextedit_export.h"
0010 #include <QFileInfo>
0011 #include <QImage>
0012 #include <QObject>
0013 #include <QSharedPointer>
0014 #include <QTextImageFormat>
0015 namespace KPIMTextEdit
0016 {
0017 class RichTextComposer;
0018 /**
0019  * Holds information about an embedded HTML image that will be useful for mail clients.
0020  * A list with all images can be retrieved with TextEdit::embeddedImages().
0021  */
0022 struct EmbeddedImage {
0023     QByteArray image; ///< The image, encoded as PNG with base64 encoding
0024     QString contentID; ///< The content id of the embedded image
0025     QString imageName; ///< Name of the image as it is available as a resource in the editor
0026 };
0027 
0028 /**
0029  * Holds information about an embedded HTML image that will be generally useful.
0030  * A list with all images can be retrieved with TextEdit::imagesWithName().
0031  *
0032  * @since 4.4
0033  */
0034 struct ImageWithName {
0035     QImage image; ///< The image
0036     QString name; ///< The name of the image as it is available as a resource in the editor
0037 };
0038 
0039 using ImageWithNamePtr = QSharedPointer<ImageWithName>;
0040 using ImageWithNameList = QList<ImageWithNamePtr>;
0041 using ImageList = QList<QSharedPointer<EmbeddedImage>>;
0042 
0043 class KPIMTEXTEDIT_EXPORT RichTextComposerImages : public QObject
0044 {
0045     Q_OBJECT
0046 public:
0047     explicit RichTextComposerImages(RichTextComposer *composer, QObject *parent = nullptr);
0048     ~RichTextComposerImages() override;
0049 
0050     /**
0051      * Adds an image. The image is loaded from file and then pasted to the current
0052      * cursor position with the given @p width and @p height.
0053      *
0054      * @param url The URL of the file which contains the image
0055      * @param width The width the inserted image will have.
0056      * @param height The height the inserted image will have.
0057      *
0058      */
0059     void addImage(const QUrl &url, int width = -1, int height = -1);
0060 
0061     /**
0062      * Loads an image into the textedit. The difference to addImage() is that this
0063      * function expects that the image tag is already present in the HTML source.
0064      *
0065      * @param image the image to load
0066      * @param matchName the name of tags to match image
0067      * @param resourceName the resource name of image
0068      * So what this message does is that it scans the HTML source for the image
0069      * tag that matches the @p matchName, and then inserts the @p image as a
0070      * resource, giving that resource the name @p resourceName.
0071      *
0072      */
0073     void loadImage(const QImage &image, const QString &matchName, const QString &resourceName);
0074 
0075     void addImageHelper(const QString &imageName, const QImage &image, int width = -1, int height = -1);
0076     [[nodiscard]] ImageWithNameList imagesWithName() const;
0077     [[nodiscard]] QList<QSharedPointer<EmbeddedImage>> embeddedImages() const;
0078     [[nodiscard]] QList<QTextImageFormat> embeddedImageFormats() const;
0079     void addImageHelper(const QUrl &url, int width = -1, int height = -1);
0080     void insertImage(const QImage &image, const QFileInfo &fileInfo);
0081 
0082     /**
0083      * For all given embedded images, this function replace the image name
0084      * in the \<img\> tag of the HTML body with cid:content-id, so that the
0085      * HTML references the image body parts, see RFC 2557.
0086      *
0087      * This is useful when building a MIME message with inline images.
0088      *
0089      * Note that this function works on encoded content already.
0090      *
0091      * @param htmlBody the HTML code in which the \<img\> tag will be modified.
0092      *                 The HTML code here could come from toHtml(), for example.
0093      *
0094      * @param imageList the list of images of which the \<img\> tag will be modified.
0095      *                  You can get such a list from the embeddedImages() function.
0096      *
0097      * @return a modified HTML code, where the \<img\> tags got replaced
0098      */
0099     [[nodiscard]] static QByteArray imageNamesToContentIds(const QByteArray &htmlBody, const ImageList &imageList);
0100 
0101     [[nodiscard]] QSharedPointer<EmbeddedImage> createEmbeddedImage(const QImage &img, const QString &imageName) const;
0102 
0103 private:
0104     class RichTextComposerImagesPrivate;
0105     std::unique_ptr<RichTextComposerImagesPrivate> const d;
0106 };
0107 }