1 /* 2 SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org> 3 4 SPDX-License-Identifier: LGPL-2.0-or-later 5 */ 6 7 #pragma once 8 9 #include "kpimtextedit_export.h" 10 #include <QFileInfo> 11 #include <QImage> 12 #include <QObject> 13 #include <QSharedPointer> 14 #include <QTextImageFormat> 15 namespace KPIMTextEdit 16 { 17 class RichTextComposer; 18 /** 19 * Holds information about an embedded HTML image that will be useful for mail clients. 20 * A list with all images can be retrieved with TextEdit::embeddedImages(). 21 */ 22 struct EmbeddedImage { 23 QByteArray image; ///< The image, encoded as PNG with base64 encoding 24 QString contentID; ///< The content id of the embedded image 25 QString imageName; ///< Name of the image as it is available as a resource in the editor 26 }; 27 28 /** 29 * Holds information about an embedded HTML image that will be generally useful. 30 * A list with all images can be retrieved with TextEdit::imagesWithName(). 31 * 32 * @since 4.4 33 */ 34 struct ImageWithName { 35 QImage image; ///< The image 36 QString name; ///< The name of the image as it is available as a resource in the editor 37 }; 38 39 typedef QSharedPointer<ImageWithName> ImageWithNamePtr; 40 typedef QVector<ImageWithNamePtr> ImageWithNameList; 41 typedef QVector<QSharedPointer<EmbeddedImage>> ImageList; 42 43 class KPIMTEXTEDIT_EXPORT RichTextComposerImages : public QObject 44 { 45 Q_OBJECT 46 public: 47 explicit RichTextComposerImages(RichTextComposer *composer, QObject *parent = nullptr); 48 ~RichTextComposerImages() override; 49 50 /** 51 * Adds an image. The image is loaded from file and then pasted to the current 52 * cursor position with the given @p width and @p height. 53 * 54 * @param url The URL of the file which contains the image 55 * @param width The width the inserted image will have. 56 * @param height The height the inserted image will have. 57 * 58 */ 59 void addImage(const QUrl &url, int width = -1, int height = -1); 60 61 /** 62 * Loads an image into the textedit. The difference to addImage() is that this 63 * function expects that the image tag is already present in the HTML source. 64 * 65 * @param image the image to load 66 * @param matchName the name of tags to match image 67 * @param resourceName the resource name of image 68 * So what this message does is that it scans the HTML source for the image 69 * tag that matches the @p matchName, and then inserts the @p image as a 70 * resource, giving that resource the name @p resourceName. 71 * 72 */ 73 void loadImage(const QImage &image, const QString &matchName, const QString &resourceName); 74 75 void addImageHelper(const QString &imageName, const QImage &image, int width = -1, int height = -1); 76 Q_REQUIRED_RESULT ImageWithNameList imagesWithName() const; 77 Q_REQUIRED_RESULT QVector<QSharedPointer<EmbeddedImage>> embeddedImages() const; 78 Q_REQUIRED_RESULT QVector<QTextImageFormat> embeddedImageFormats() const; 79 void addImageHelper(const QUrl &url, int width = -1, int height = -1); 80 void insertImage(const QImage &image, const QFileInfo &fileInfo); 81 82 /** 83 * For all given embedded images, this function replace the image name 84 * in the \<img\> tag of the HTML body with cid:content-id, so that the 85 * HTML references the image body parts, see RFC 2557. 86 * 87 * This is useful when building a MIME message with inline images. 88 * 89 * Note that this function works on encoded content already. 90 * 91 * @param htmlBody the HTML code in which the \<img\> tag will be modified. 92 * The HTML code here could come from toHtml(), for example. 93 * 94 * @param imageList the list of images of which the \<img\> tag will be modified. 95 * You can get such a list from the embeddedImages() function. 96 * 97 * @return a modified HTML code, where the \<img\> tags got replaced 98 */ 99 Q_REQUIRED_RESULT static QByteArray imageNamesToContentIds(const QByteArray &htmlBody, const ImageList &imageList); 100 101 Q_REQUIRED_RESULT QSharedPointer<EmbeddedImage> createEmbeddedImage(const QImage &img, const QString &imageName) const; 102 103 private: 104 class RichTextComposerImagesPrivate; 105 std::unique_ptr<RichTextComposerImagesPrivate> const d; 106 }; 107 } 108 109