Messagelib

signaturecontroller.cpp
1 /*
2  * SPDX-FileCopyrightText: 2010 Volker Krause <[email protected]>
3  *
4  * Based on kmail/kmcomposewin.cpp
5  * SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
6  *
7  * Based on KMail code by:
8  * SPDX-FileCopyrightText: 1997 Markus Wuebben <[email protected]>
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 <KIdentityManagement/Identity>
19 #include <KIdentityManagement/IdentityCombo>
20 #include <KIdentityManagement/IdentityManager>
21 
22 using namespace MessageComposer;
23 
24 class MessageComposer::SignatureControllerPrivate
25 {
26 public:
27  SignatureControllerPrivate() = default;
28 
29  uint m_currentIdentityId = 0;
30  KIdentityManagement::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(KIdentityManagement::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 KIdentityManagement::Identity &newIdentity = d->m_identityCombo->identityManager()->identityForUoid(id);
58  if (newIdentity.isNull() || !d->m_editor) {
59  return;
60  }
61 
62  const KIdentityManagement::Identity &oldIdentity = d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_currentIdentityId);
63 
64  const KIdentityManagement::Signature oldSig = const_cast<KIdentityManagement::Identity &>(oldIdentity).signature();
65  const KIdentityManagement::Signature newSig = const_cast<KIdentityManagement::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, &KIdentityManagement::IdentityCombo::identityChanged, this, &SignatureController::identityChanged);
80  }
81 }
82 
84 {
85  if (d->m_identityCombo) {
86  connect(d->m_identityCombo, &KIdentityManagement::IdentityCombo::identityChanged, this, &SignatureController::identityChanged);
87  }
88 }
89 
91 {
92  insertSignatureHelper(KIdentityManagement::Signature::End);
93 }
94 
96 {
97  insertSignatureHelper(KIdentityManagement::Signature::Start);
98 }
99 
101 {
102  insertSignatureHelper(KIdentityManagement::Signature::AtCursor);
103 }
104 
105 void SignatureController::cleanSpace()
106 {
107  if (!d->m_editor || !d->m_identityCombo) {
108  return;
109  }
110  const KIdentityManagement::Identity &ident = d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_identityCombo->currentIdentity());
111  const KIdentityManagement::Signature signature = const_cast<KIdentityManagement::Identity &>(ident).signature();
112  d->m_editor->composerSignature()->cleanWhitespace(signature);
113 }
114 
115 void SignatureController::insertSignatureHelper(KIdentityManagement::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<KIdentityManagement::Identity &>(d->m_identityCombo->identityManager()->identityForUoidOrDefault(d->m_identityCombo->currentIdentity()));
125  const KIdentityManagement::Signature signature = ident.signature();
126 
127  if (signature.isInlinedHtml() && signature.type() == KIdentityManagement::Signature::Inlined) {
128  Q_EMIT enableHtml();
129  }
130 
132  if (MessageComposer::MessageComposerSettings::self()->dashDashSignature()) {
134  }
135  d->m_editor->insertSignature(signature, placement, addedText);
136  if ((placement == KIdentityManagement::Signature::Start) || (placement == KIdentityManagement::Signature::End)) {
137  Q_EMIT signatureAdded();
138  }
139 }
140 
142 {
143  if (!d->m_editor) {
144  return;
145  }
146 
147  if (MessageComposer::MessageComposerSettings::self()->autoTextSignature() == QLatin1String("auto")) {
149  if (MessageComposer::MessageComposerSettings::self()->dashDashSignature()) {
151  }
152  if (MessageComposer::MessageComposerSettings::self()->prependSignature()) {
153  d->m_editor->insertSignature(signature, KIdentityManagement::Signature::Start, addedText);
154  } else {
155  d->m_editor->insertSignature(signature, KIdentityManagement::Signature::End, addedText);
156  }
157  }
158 }
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
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.
void applySignature(const KIdentityManagement::Signature &signature)
Adds the given signature to the editor, taking user preferences into account.
The RichTextComposerNg class.
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 enableHtml()
A HTML signature is about to be inserted, so enable HTML support in the editor.
QString rawText(bool *ok=nullptr, QString *errorMessage=nullptr) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 19 2022 03:53:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.