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 
22 using namespace MessageComposer;
23 
24 class MessageComposer::SignatureControllerPrivate
25 {
26 public:
27  SignatureControllerPrivate() = default;
28 
29  uint m_currentIdentityId = 0;
30  KIdentityManagementWidgets::IdentityCombo *m_identityCombo = nullptr;
31  MessageComposer::RichTextComposerNg *m_editor = nullptr;
32 };
33 
34 SignatureController::SignatureController(QObject *parent)
35  : QObject(parent)
36  , d(new MessageComposer::SignatureControllerPrivate)
37 {
38 }
39 
40 SignatureController::~SignatureController() = default;
41 
42 void SignatureController::setEditor(MessageComposer::RichTextComposerNg *editor)
43 {
44  d->m_editor = editor;
45 }
46 
47 void SignatureController::setIdentityCombo(KIdentityManagementWidgets::IdentityCombo *combo)
48 {
49  d->m_identityCombo = combo;
50  d->m_currentIdentityId = combo->currentIdentity();
51  resume();
52 }
53 
54 void 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 
91 {
92  insertSignatureHelper(KIdentityManagementCore::Signature::End);
93 }
94 
96 {
97  insertSignatureHelper(KIdentityManagementCore::Signature::Start);
98 }
99 
101 {
102  insertSignatureHelper(KIdentityManagementCore::Signature::AtCursor);
103 }
104 
105 void 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 
115 void 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) {
128  Q_EMIT enableHtml();
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"
void resume()
Resume identity change tracking after a previous call to suspend().
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Q_EMITQ_EMIT
void applySignature(const KIdentityManagementCore::Signature &signature)
Adds the given signature to the editor, taking user preferences into account.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void appendSignature()
Append signature to the end of the text in the editor.
bool isEmpty() const const
void insertSignatureAtCursor()
Insert signature at the cursor position of the text in the editor.
The RichTextComposerNg class.
void suspend()
Temporarily disable identity tracking, useful for initial loading for example.
KIdentityManagementCore::Identity::Id currentIdentity() const
QString rawText(bool *ok=nullptr, QString *errorMessage=nullptr) const
void prependSignature()
Prepend signature at the beginning of the text in the editor.
void identityChanged(KIdentityManagementCore::Identity::Id uoid)
void enableHtml()
A HTML signature is about to be inserted, so enable HTML support in the editor.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.