Messagelib

signaturecontroller.cpp
1/*
2 * SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
3 *
4 * Based on kmail/kmcomposewin.cpp
5 * SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
6 *
7 * Based on KMail code by:
8 * SPDX-FileCopyrightText: 1997 Markus Wuebben <markus.wuebben@kde.org>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13#include "signaturecontroller.h"
14#include "composer-ng/richtextcomposerng.h"
15#include "composer-ng/richtextcomposersignatures.h"
16#include "settings/messagecomposersettings.h"
17
18#include <KIdentityManagementCore/Identity>
19#include <KIdentityManagementCore/IdentityManager>
20#include <KIdentityManagementWidgets/IdentityCombo>
21
22using namespace MessageComposer;
23
24class MessageComposer::SignatureControllerPrivate
25{
26public:
27 SignatureControllerPrivate() = default;
28
29 uint m_currentIdentityId = 0;
30 KIdentityManagementWidgets::IdentityCombo *m_identityCombo = nullptr;
31 MessageComposer::RichTextComposerNg *m_editor = nullptr;
32};
33
34SignatureController::SignatureController(QObject *parent)
35 : QObject(parent)
36 , d(new MessageComposer::SignatureControllerPrivate)
37{
38}
39
40SignatureController::~SignatureController() = default;
41
42void SignatureController::setEditor(MessageComposer::RichTextComposerNg *editor)
43{
44 d->m_editor = editor;
45}
46
47void SignatureController::setIdentityCombo(KIdentityManagementWidgets::IdentityCombo *combo)
48{
49 d->m_identityCombo = combo;
50 d->m_currentIdentityId = combo->currentIdentity();
51 resume();
52}
53
54void SignatureController::identityChanged(uint id)
55{
56 Q_ASSERT(d->m_identityCombo);
57 const KIdentityManagementCore::Identity &newIdentity = d->m_identityCombo->identityManager()->identityForUoid(id);
58 if (newIdentity.isNull() || !d->m_editor) {
59 return;
60 }
61
62 const KIdentityManagementCore::Identity &oldIdentity = d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_currentIdentityId);
63
64 const KIdentityManagementCore::Signature oldSig = const_cast<KIdentityManagementCore::Identity &>(oldIdentity).signature();
65 const KIdentityManagementCore::Signature newSig = const_cast<KIdentityManagementCore::Identity &>(newIdentity).signature();
66 const bool replaced = d->m_editor->composerSignature()->replaceSignature(oldSig, newSig);
67
68 // Just append the signature if there was no old signature
69 if (!replaced && oldSig.rawText().isEmpty()) {
70 applySignature(newSig);
71 }
72
73 d->m_currentIdentityId = id;
74}
75
77{
78 if (d->m_identityCombo) {
79 disconnect(d->m_identityCombo, &KIdentityManagementWidgets::IdentityCombo::identityChanged, this, &SignatureController::identityChanged);
80 }
81}
82
84{
85 if (d->m_identityCombo) {
86 connect(d->m_identityCombo, &KIdentityManagementWidgets::IdentityCombo::identityChanged, this, &SignatureController::identityChanged);
87 }
88}
89
94
99
104
105void SignatureController::cleanSpace()
106{
107 if (!d->m_editor || !d->m_identityCombo) {
108 return;
109 }
110 const KIdentityManagementCore::Identity &ident = d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_identityCombo->currentIdentity());
111 const KIdentityManagementCore::Signature signature = const_cast<KIdentityManagementCore::Identity &>(ident).signature();
112 d->m_editor->composerSignature()->cleanWhitespace(signature);
113}
114
115void SignatureController::insertSignatureHelper(KIdentityManagementCore::Signature::Placement placement)
116{
117 if (!d->m_identityCombo || !d->m_editor) {
118 return;
119 }
120
121 // Identity::signature() is not const, although it should be, therefore the
122 // const_cast.
123 auto &ident =
124 const_cast<KIdentityManagementCore::Identity &>(d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_identityCombo->currentIdentity()));
125 const KIdentityManagementCore::Signature signature = ident.signature();
126
127 if (signature.isInlinedHtml() && signature.type() == KIdentityManagementCore::Signature::Inlined) {
129 }
130
132 if (MessageComposer::MessageComposerSettings::self()->dashDashSignature()) {
134 }
135 d->m_editor->insertSignature(signature, placement, addedText);
137 Q_EMIT signatureAdded();
138 }
139}
140
142{
143 if (!d->m_editor) {
144 return;
145 }
146
147 if (MessageComposer::MessageComposerSettings::self()->autoTextSignature() == QLatin1StringView("auto")) {
149 if (MessageComposer::MessageComposerSettings::self()->dashDashSignature()) {
151 }
152 if (MessageComposer::MessageComposerSettings::self()->prependSignature()) {
153 d->m_editor->insertSignature(signature, KIdentityManagementCore::Signature::Start, addedText);
154 } else {
155 d->m_editor->insertSignature(signature, KIdentityManagementCore::Signature::End, addedText);
156 }
157 }
158}
159
160#include "moc_signaturecontroller.cpp"
QString rawText(bool *ok=nullptr, QString *errorMessage=nullptr) const
void identityChanged(KIdentityManagementCore::Identity::Id uoid)
KIdentityManagementCore::Identity::Id currentIdentity() const
The RichTextComposerNg class.
void insertSignatureAtCursor()
Insert signature at the cursor position of the text in the editor.
void appendSignature()
Append signature to the end of the text in the editor.
void resume()
Resume identity change tracking after a previous call to suspend().
void suspend()
Temporarily disable identity tracking, useful for initial loading for example.
void prependSignature()
Prepend signature at the beginning of the text in the editor.
void applySignature(const KIdentityManagementCore::Signature &signature)
Adds the given signature to the editor, taking user preferences into account.
void enableHtml()
A HTML signature is about to be inserted, so enable HTML support in the editor.
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.