Messagelib

stringutil.h
1/* SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
2
3 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4*/
5#pragma once
6
7#include "messagecore_export.h"
8#include <KMime/HeaderParsing>
9#include <KMime/Message>
10
11#include <QList>
12#include <QStringList>
13class QUrl;
14
15namespace KMime
16{
17namespace Types
18{
19struct Address;
20using AddressList = QList<Address>;
21class Mailbox;
22}
23namespace Headers
24{
25namespace Generics
26{
27class MailboxList;
28class AddressList;
29}
30}
31}
32
33namespace MessageCore
34{
35/**
36 * This namespace contain helper functions for string manipulation
37 */
38namespace StringUtil
39{
40/**
41 * Parses a mailto: url and extracts the information in the QMap (field name as key).
42 */
43[[nodiscard]] MESSAGECORE_EXPORT QList<QPair<QString, QString>> parseMailtoUrl(const QUrl &url);
44
45/**
46 * Strips the signature blocks from a message text. "-- " is considered as a signature block separator.
47 *
48 * @param message The message to remove the signature block from.
49 */
50[[nodiscard]] MESSAGECORE_EXPORT QString stripSignature(const QString &message);
51
52/**
53 * Splits the given address list @p text into separate addresses.
54 */
55[[nodiscard]] MESSAGECORE_EXPORT KMime::Types::AddressList splitAddressField(const QByteArray &text);
56
57/**
58 * Generates the Message-Id. It uses either the Message-Id @p suffix
59 * defined by the user or the given email address as suffix. The @p address
60 * must be given as addr-spec as defined in RFC 2822.
61 */
62[[nodiscard]] MESSAGECORE_EXPORT QString generateMessageId(const QString &address, const QString &suffix);
63
64/**
65 * Quotes the following characters which have a special meaning in HTML:
66 * '<' '>' '&' '"'. Additionally '\\n' is converted to "<br />" if
67 * @p removeLineBreaks is false. If @p removeLineBreaks is true, then
68 * '\\n' is removed. Last but not least '\\r' is removed.
69 */
70[[nodiscard]] MESSAGECORE_EXPORT QString quoteHtmlChars(const QString &text, bool removeLineBreaks = false);
71
72/**
73 * Removes all private header fields (e.g. *Status: and X-KMail-*) from the given @p message.
74 * if cleanUpHeader is false don't remove X-KMail-Identity and X-KMail-Dictionary which is useful when we want restore mail.
75 */
76MESSAGECORE_EXPORT void removePrivateHeaderFields(const KMime::Message::Ptr &message, bool cleanUpHeader = true);
77
78/**
79 * Returns the @p message contents with the headers that should not be sent stripped off.
80 */
81[[nodiscard]] MESSAGECORE_EXPORT QByteArray asSendableString(const KMime::Message::Ptr &message);
82
83/**
84 * Return the message header with the headers that should not be sent stripped off.
85 */
86[[nodiscard]] MESSAGECORE_EXPORT QByteArray headerAsSendableString(const KMime::Message::Ptr &message);
87
88/**
89 * Used to determine if the visible part of the anchor contains
90 * only the name part and not the given emailAddr or the full address.
91 */
92enum Display {
93 DisplayNameOnly,
94 DisplayFullAddress,
95};
96
97/**
98 * Used to determine if the address should be a link or not.
99 */
100enum Link {
101 ShowLink,
102 HideLink,
103};
104
105/**
106 * Used to determine if the address field should be expandable/collapsible.
107 */
109 ExpandableAddresses,
110 FullAddresses,
111};
112
113/**
114 * Converts the email address(es) to (a) nice HTML mailto: anchor(s).
115 * @p display determines if only the name part or the entire address should be returned.
116 * @p cssStyle a custom css template.
117 * @p link determines if the result should be a html link or not.
118 * @p expandable determines if a long list of addresses should be expandable or shown
119 * in full.
120 * @p fieldName the name that the divs should be based on if expandable is set to ExpanableAddesses.
121 * @p The number of addresses to show before collapsing the rest, if expandable is set to
122 * ExpandableAddresses.
123 */
124[[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const KMime::Headers::Generics::MailboxList *mailboxList,
125 Display display = DisplayNameOnly,
126 const QString &cssStyle = QString(),
127 Link link = ShowLink,
128 AddressMode expandable = FullAddresses,
129 const QString &fieldName = QString(),
130 int collapseNumber = 4);
131
132/**
133 * Same as above method, only for AddressList headers.
134 */
135[[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const KMime::Headers::Generics::AddressList *addressList,
136 Display display = DisplayNameOnly,
137 const QString &cssStyle = QString(),
138 Link link = ShowLink,
139 AddressMode expandable = FullAddresses,
140 const QString &fieldName = QString(),
141 int collapseNumber = 4);
142
143/**
144 * Same as the above, only for Mailbox::List types.
145 */
146[[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const QList<KMime::Types::Mailbox> &mailboxList,
147 Display display = DisplayNameOnly,
148 const QString &cssStyle = QString(),
149 Link link = ShowLink,
150 AddressMode expandable = FullAddresses,
151 const QString &fieldName = QString(),
152 int collapseNumber = 4);
153
154/**
155 * Returns true if the given address is contained in the given address list.
156 */
157[[nodiscard]] MESSAGECORE_EXPORT bool addressIsInAddressList(const QString &address, const QStringList &addresses);
158
159/**
160 * Uses the hostname as domain part and tries to determine the real name
161 * from the entries in the password file.
162 */
163[[nodiscard]] MESSAGECORE_EXPORT QString guessEmailAddressFromLoginName(const QString &userName);
164
165/**
166 * Relayouts the given string so that the individual lines don't exceed the given
167 * maximal length.
168 *
169 * As the name of the function implies, it is smart, which means it deals with quoting
170 * correctly. This means if a line already starts with quote characters and needs to be
171 * broken, the same quote characters are prepended to the next line as well.
172 *
173 * This does _not_ add new quote characters in front of every line, that is the responsibility
174 * of the caller.
175 *
176 * @param message The string which it to be relayouted
177 * @param maxLineLength reformat text to be this amount of columns at maximum. Note that this
178 * also includes the trailing \n!
179 */
180[[nodiscard]] MESSAGECORE_EXPORT QString smartQuote(const QString &message, int maxLineLength);
181
182/**
183 * Convert quote wildcards into the final quote prefix.
184 * @param wildString the string to be converted
185 * @param fromDisplayString displayable string of the from email address
186 */
187[[nodiscard]] MESSAGECORE_EXPORT QString formatQuotePrefix(const QString &wildString, const QString &fromDisplayString);
188
189/**
190 * Cleans a filename by replacing characters not allowed or wanted on the filesystem
191 * e.g. ':', '/', '\' with '_'
192 */
193[[nodiscard]] MESSAGECORE_EXPORT QString cleanFileName(const QString &fileName);
194
195/**
196 * Removes the forward and reply marks (e.g. Re: or Fwd:) from a @p subject string.
197 * Additional markers to act on can be specified in the MessageCore::GlobalSettings
198 * object.
199 */
200[[nodiscard]] MESSAGECORE_EXPORT QString stripOffPrefixes(const QString &subject);
201
202MESSAGECORE_EXPORT void setEncodingFile(QUrl &url, const QString &encoding);
203
204/** Check for prefixes @p prefixRegExps in #subject(). If none
205 is found, @p newPrefix + ' ' is prepended to the subject and the
206 resulting string is returned. If @p replace is true, any
207 sequence of whitespace-delimited prefixes at the beginning of
208 #subject() is replaced by @p newPrefix
209 **/
210[[nodiscard]] MESSAGECORE_EXPORT QString cleanSubject(KMime::Message *msg, const QStringList &prefixRegExps, bool replace, const QString &newPrefix);
211
212/** Return this mails subject, with all "forward" and "reply"
213 prefixes removed */
214[[nodiscard]] MESSAGECORE_EXPORT QString cleanSubject(KMime::Message *msg);
215
216/** Return this mails subject, formatted for "forward" mails */
217[[nodiscard]] MESSAGECORE_EXPORT QString forwardSubject(KMime::Message *msg);
218
219/** Return this mails subject, formatted for "reply" mails */
220[[nodiscard]] MESSAGECORE_EXPORT QString replySubject(KMime::Message *msg);
221/** Check for prefixes @p prefixRegExps in @p str. If none
222 is found, @p newPrefix + ' ' is prepended to @p str and the
223 resulting string is returned. If @p replace is true, any
224 sequence of whitespace-delimited prefixes at the beginning of
225 @p str is replaced by @p newPrefix.
226 **/
227[[nodiscard]] MESSAGECORE_EXPORT QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bool replace, const QString &newPrefix);
228}
229}
AddressMode
Used to determine if the address field should be expandable/collapsible.
Definition stringutil.h:108
QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bool replace, const QString &newPrefix)
Check for prefixes prefixRegExps in str.
bool addressIsInAddressList(const QString &address, const QStringList &addresses)
Returns true if the given address is contained in the given address list.
QList< QPair< QString, QString > > parseMailtoUrl(const QUrl &url)
Parses a mailto: url and extracts the information in the QMap (field name as key).
QString stripOffPrefixes(const QString &subject)
Removes the forward and reply marks (e.g.
QString guessEmailAddressFromLoginName(const QString &loginName)
Uses the hostname as domain part and tries to determine the real name from the entries in the passwor...
Display
Used to determine if the visible part of the anchor contains only the name part and not the given ema...
Definition stringutil.h:92
QString formatQuotePrefix(const QString &wildString, const QString &fromDisplayString)
Convert quote wildcards into the final quote prefix.
QByteArray headerAsSendableString(const KMime::Message::Ptr &originalMessage)
Return the message header with the headers that should not be sent stripped off.
QString cleanSubject(KMime::Message *msg)
Return this mails subject, with all "forward" and "reply" prefixes removed.
QString stripSignature(const QString &msg)
Strips the signature blocks from a message text.
QString smartQuote(const QString &msg, int maxLineLength)
Relayouts the given string so that the individual lines don't exceed the given maximal length.
void removePrivateHeaderFields(const KMime::Message::Ptr &message, bool cleanUpHeader)
Removes all private header fields (e.g.
QString cleanFileName(const QString &name)
Cleans a filename by replacing characters not allowed or wanted on the filesystem e....
AddressList splitAddressField(const QByteArray &text)
Splits the given address list text into separate addresses.
QByteArray asSendableString(const KMime::Message::Ptr &originalMessage)
Returns the message contents with the headers that should not be sent stripped off.
QString replySubject(KMime::Message *msg)
Return this mails subject, formatted for "reply" mails.
QString generateMessageId(const QString &address, const QString &suffix)
Generates the Message-Id.
Link
Used to determine if the address should be a link or not.
Definition stringutil.h:100
QString quoteHtmlChars(const QString &str, bool removeLineBreaks)
Quotes the following characters which have a special meaning in HTML: '<' '>' '&' '"'....
QString forwardSubject(KMime::Message *msg)
Return this mails subject, formatted for "forward" mails.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.