KIdentityManagement

signature.h
1/*
2 SPDX-FileCopyrightText: 2002-2004 Marc Mutz <mutz@kde.org>
3 SPDX-FileCopyrightText: 2007 Tom Albers <tomalbers@kde.nl>
4 SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
5 Author: Stefan Taferner <taferner@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#pragma once
11
12#include "kidentitymanagementcore_export.h"
13
14#include <QImage>
15#include <QSharedPointer>
16#include <QString>
17#include <memory>
18
19class KConfigGroup;
20namespace KIdentityManagementCore
21{
22class Signature;
23class Identity;
24class SignaturePrivate;
25KIDENTITYMANAGEMENTCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const KIdentityManagementCore::Signature &sig);
26KIDENTITYMANAGEMENTCORE_EXPORT QDataStream &operator>>(QDataStream &stream, KIdentityManagementCore::Signature &sig);
27
28/**
29 * @short Abstraction of a signature (aka "footer").
30 *
31 * The signature can either be plain text, HTML text, text returned from a command or text stored
32 * in a file.
33 *
34 * In case of HTML text, the signature can contain images.
35 * Since you set the HTML source with setText(), there also needs to be a way to add the images
36 * to the signature, as the HTML source contains only the img tags that reference those images.
37 * To add the image to the signature, call addImage(). The name given there must match the name
38 * of the img tag in the HTML source.
39 *
40 * The images need to be stored somewhere. The Signature class handles that by storing all images
41 * in a directory. You must set that directory with setImageLocation(), before calling addImage().
42 * The images added with addImage() are then saved to that directory when calling writeConfig().
43 * When loading a signature, readConfig() automatically loads the images as well.
44 * To actually add the images to a text edit, call insertIntoTextEdit().
45 *
46 * Example of creating a HTML signature and then inserting it into a text edit:
47 * @code
48 * Signature htmlSig;
49 * htmlSig.setText( "<img src=\"hello.png\"> World" );
50 * htmlSig.setInlinedHtml( true );
51 * htmlSig.setImageLocation( KStandardDirs::locateLocal( "data", "emailidentities/example/" );
52 * QImage image = ...;
53 * htmlSig.addImage( image, "hello.png" );
54 * ...
55 * KTextEdit edit;
56 * htmlSig.insertIntoTextEdit( KIdentityManagementCore::Signature::End,
57 * KIdentityManagementCore::Signature::AddSeparator, &edit );
58 * @endcode
59 */
60class KIDENTITYMANAGEMENTCORE_EXPORT Signature
61{
62 friend class Identity;
63
64 friend KIDENTITYMANAGEMENTCORE_EXPORT QDataStream &operator<<(QDataStream &stream, const Signature &sig);
65 friend KIDENTITYMANAGEMENTCORE_EXPORT QDataStream &operator>>(QDataStream &stream, Signature &sig);
66
67public:
68 /** Type of signature (ie. way to obtain the signature text) */
69 enum Type { Disabled = 0, Inlined = 1, FromFile = 2, FromCommand = 3 };
70
71 /**
72 * Describes the placement of the signature text when it is to be inserted into a
73 * text edit
74 */
75 enum Placement {
76 Start, ///< The signature is placed at the start of the textedit
77 End, ///< The signature is placed at the end of the textedit
78 AtCursor ///< The signature is placed at the current cursor position
79 };
80
81 struct EmbeddedImage {
82 QImage image;
83 QString name;
84 };
85 using EmbeddedImagePtr = QSharedPointer<EmbeddedImage>;
86
87 /** Used for comparison */
88 bool operator==(const Signature &other) const;
89
90 /** Constructor for disabled signature */
91 Signature();
92 /** Constructor for inline text */
93 Signature(const QString &text);
94 /** Constructor for text from a file or from output of a command */
95 Signature(const QString &path, bool isExecutable);
96 /** Copy constructor */
97 Signature(const Signature &that);
98 /** Assignment operator */
99 Signature &operator=(const Signature &that);
100 /** Destructor */
102
103 /** @return the raw signature text as entered resp. read from file.
104 @param ok set to @c true if reading succeeded
105 @param errorMessage If available, contains a human readable explanation for @p ok being @c false.
106 */
107 [[nodiscard]] QString rawText(bool *ok = nullptr, QString *errorMessage = nullptr) const;
108
109 /** @return the signature text with a "-- \n" separator added, if
110 necessary. A newline will not be appended or prepended.
111 @param ok set to @c true if reading succeeded
112 @param errorMessage If available, contains a human readable explanation for @p ok being @c false.
113 */
114 [[nodiscard]] QString withSeparator(bool *ok = nullptr, QString *errorMessage = nullptr) const;
115
116 /** Set the signature text and mark this signature as being of
117 "inline text" type. */
118 void setText(const QString &text);
119 [[nodiscard]] QString text() const;
120
121 /**
122 * Returns the text of the signature. If the signature is HTML, the HTML
123 * tags will be stripped.
124 * @since 4.4
125 */
126 [[nodiscard]] QString toPlainText() const;
127
128 /** Set the signature URL and mark this signature as being of
129 "from file" resp. "from output of command" type. */
130 void setPath(const QString &path, bool isExecutable = false);
131 [[nodiscard]] QString path() const;
132
133 /// @return the type of signature (ie. way to obtain the signature text)
134 [[nodiscard]] Type type() const;
135 void setType(Type type);
136
137 /**
138 * Sets the inlined signature to text or html
139 * @param isHtml sets the inlined signature to html
140 * @since 4.1
141 */
142 void setInlinedHtml(bool isHtml);
143
144 /**
145 * @return boolean whether the inlined signature is html
146 * @since 4.1
147 */
148 [[nodiscard]] bool isInlinedHtml() const;
149
150 /**
151 * Sets the location where the copies of the signature images will be stored.
152 * The images will be stored there when calling writeConfig(). The image location
153 * is stored in the config, so the next readConfig() call knows where to look for
154 * images.
155 * It is recommended to use KStandardDirs::locateLocal( "data", "emailidentities/%1" )
156 * for the location, where %1 is the unique identifier of the identity.
157 *
158 * @warning readConfig will delete all other PNG files in this directory, as they could
159 * be stale inline image files
160 *
161 * Like with addImage(), the SignatureConfigurator will handle this for you.
162 * @param path the path to set as image location
163 * @since 4.4
164 */
165 void setImageLocation(const QString &path);
166 [[nodiscard]] QString imageLocation() const;
167
168 /**
169 * Adds the given image to the signature.
170 * This is needed if you use setText() to set some HTML source that references images. Those
171 * referenced images needed to be added by calling this function. The @imageName has to match
172 * the src attribute of the img tag.
173 *
174 * If you use SignatureConfigurator, you don't need to call this function, as the configurator
175 * will handle this for you.
176 * setImageLocation() needs to be called once before.
177 * @since 4.4
178 */
179 void addImage(const QImage &image, const QString &imageName);
180
181 /**
182 * @brief setEnabledSignature
183 * @param enabled enables signature if set as @c true
184 * @since 4.9
185 */
186 void setEnabledSignature(bool enabled);
187 [[nodiscard]] bool isEnabledSignature() const;
188
190 AddNothing = 0, ///< Don't add any text to the signature
191 AddSeparator = 1 << 0, ///< The separator '-- \n' will be added in front
192 /// of the signature
193 AddNewLines = 1 << 1 ///< Add a newline character in front or after the signature,
194 /// depending on the placement
195 };
196
197 /// Describes which additional parts should be added to the signature
199
200 [[nodiscard]] QList<Signature::EmbeddedImagePtr> embeddedImages() const;
201 void setEmbeddedImages(const QList<EmbeddedImagePtr> &embedded);
202
203protected:
204 // TODO: KDE5: BIC: Move all to private class
205 void writeConfig(KConfigGroup &config) const;
206 void readConfig(const KConfigGroup &config);
207
208private:
209 //@cond PRIVATE
210 std::unique_ptr<SignaturePrivate> const d;
211 //@endcond
212};
213
214Q_DECLARE_OPERATORS_FOR_FLAGS(Signature::AddedText)
215}
User identity information.
Definition identity.h:72
Abstraction of a signature (aka "footer").
Definition signature.h:61
Placement
Describes the placement of the signature text when it is to be inserted into a text edit.
Definition signature.h:75
@ Start
The signature is placed at the start of the textedit.
Definition signature.h:76
@ End
The signature is placed at the end of the textedit.
Definition signature.h:77
Type
Type of signature (ie.
Definition signature.h:69
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
QDebug operator<<(QDebug dbg, const PerceptualColor::LchaDouble &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:09 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.