signatureconfigurator.cpp 17 KB
Newer Older
1
/*  -*- c++ -*-
2
3
4
5
6
    SPDX-FileCopyrightText: 2008 Thomas McGuire <Thomas.McGuire@gmx.net>
    SPDX-FileCopyrightText: 2008 Edwin Schepers <yez@familieschepers.nl>
    SPDX-FileCopyrightText: 2004 Marc Mutz <mutz@kde.org>

    SPDX-License-Identifier: LGPL-2.1-or-later
7
8
9
*/

#include "signatureconfigurator.h"
10
#include "identity.h"
11

Laurent Montel's avatar
Laurent Montel committed
12
#include "kidentitymanagement_debug.h"
Laurent Montel's avatar
Laurent Montel committed
13
14
15
#include <KActionCollection>
#include <KIO/JobUiDelegate>
#include <KIO/OpenUrlJob>
Laurent Montel's avatar
Laurent Montel committed
16
#include <KLineEdit>
Laurent Montel's avatar
Laurent Montel committed
17
18
#include <KLocalizedString>
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
19
20
#include <KShellCompletion>
#include <KToolBar>
Laurent Montel's avatar
Laurent Montel committed
21
#include <KUrlRequester>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QUrl>
23

Laurent Montel's avatar
Laurent Montel committed
24
25
#include <KPIMTextEdit/RichTextComposer>
#include <KPIMTextEdit/RichTextComposerControler>
Laurent Montel's avatar
Laurent Montel committed
26
#include <KPIMTextEdit/RichTextComposerImages>
27

28
#include <QCheckBox>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QComboBox>
30
31
32
33
#include <QDir>
#include <QFileInfo>
#include <QLabel>
#include <QStackedWidget>
34

35
#include <QHBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
36
#include <QVBoxLayout>
37

38
#include <QStandardPaths>
Laurent Montel's avatar
Laurent Montel committed
39
#include <assert.h>
40

Laurent Montel's avatar
Laurent Montel committed
41
#include <kpimtextedit/richtexteditorwidget.h>
Laurent Montel's avatar
Laurent Montel committed
42

Laurent Montel's avatar
Laurent Montel committed
43
using namespace KIdentityManagement;
44

Laurent Montel's avatar
Laurent Montel committed
45
46
namespace KIdentityManagement
{
47
48
49
50
51
/**
   Private class that helps to provide binary compatibility between releases.
   @internal
  */
//@cond PRIVATE
52
class Q_DECL_HIDDEN SignatureConfigurator::Private
53
{
Laurent Montel's avatar
Laurent Montel committed
54
55
public:
    Private(SignatureConfigurator *parent);
56
    void init();
Laurent Montel's avatar
Port++    
Laurent Montel committed
57
58
    // Returns the current text of the textedit as HTML code, but strips
    // unnecessary tags Qt inserts
Laurent Montel's avatar
Laurent Montel committed
59
    Q_REQUIRED_RESULT QString asCleanedHTML() const;
60
61

    QString imageLocation;
Laurent Montel's avatar
Laurent Montel committed
62
    SignatureConfigurator *q;
Laurent Montel's avatar
Laurent Montel committed
63
64
65
66
67
68
69
70
71
    QCheckBox *mEnableCheck = nullptr;
    QCheckBox *mHtmlCheck = nullptr;
    QComboBox *mSourceCombo = nullptr;
    KUrlRequester *mFileRequester = nullptr;
    QPushButton *mEditButton = nullptr;
    KLineEdit *mCommandEdit = nullptr;
    KToolBar *mEditToolBar = nullptr;
    KToolBar *mFormatToolBar = nullptr;
    KPIMTextEdit::RichTextComposer *mTextEdit = nullptr;
Laurent Montel's avatar
Laurent Montel committed
72
    bool inlinedHtml = false;
73
74
75
};
//@endcond

Laurent Montel's avatar
Laurent Montel committed
76
SignatureConfigurator::Private::Private(SignatureConfigurator *parent)
Laurent Montel's avatar
Laurent Montel committed
77
78
    : q(parent)
    , inlinedHtml(true)
79
80
81
{
}

Laurent Montel's avatar
Port++    
Laurent Montel committed
82
83
QString SignatureConfigurator::Private::asCleanedHTML() const
{
Laurent Montel's avatar
Laurent Montel committed
84
    QString text = mTextEdit->toHtml();
Laurent Montel's avatar
Port++    
Laurent Montel committed
85

Laurent Montel's avatar
Laurent Montel committed
86
87
88
    // Beautiful little hack to find the html headers produced by Qt.
    QTextDocument textDocument;
    QString html = textDocument.toHtml();
Laurent Montel's avatar
Port++    
Laurent Montel committed
89

Laurent Montel's avatar
Laurent Montel committed
90
    // Now remove each line from the text, the result is clean html.
Laurent Montel's avatar
Laurent Montel committed
91
92
    const QStringList lst = html.split(QLatin1Char('\n'));
    for (const QString &line : lst) {
Laurent Montel's avatar
Laurent Montel committed
93
94
95
        text.remove(line + QLatin1Char('\n'));
    }
    return text;
Laurent Montel's avatar
Port++    
Laurent Montel committed
96
97
}

98
99
void SignatureConfigurator::Private::init()
{
Laurent Montel's avatar
Laurent Montel committed
100
    auto *vlay = new QVBoxLayout(q);
Laurent Montel's avatar
Laurent Montel committed
101
102
    vlay->setObjectName(QStringLiteral("main layout"));

Yuri Chornoivan's avatar
Yuri Chornoivan committed
103
    // "enable signature" checkbox:
Laurent Montel's avatar
Laurent Montel committed
104
105
106
107
108
109
110
    mEnableCheck = new QCheckBox(i18n("&Enable signature"), q);
    mEnableCheck->setWhatsThis(
        i18n("Check this box if you want KMail to append a signature to mails "
             "written with this identity."));
    vlay->addWidget(mEnableCheck);

    // "obtain signature text from" combo and label:
Laurent Montel's avatar
Laurent Montel committed
111
    auto *hlay = new QHBoxLayout(); // inherits spacing
Laurent Montel's avatar
Laurent Montel committed
112
    vlay->addLayout(hlay);
113
    mSourceCombo = new QComboBox(q);
Laurent Montel's avatar
Laurent Montel committed
114
    mSourceCombo->setEditable(false);
Laurent Montel's avatar
Laurent Montel committed
115
116
117
118
119
    mSourceCombo->setWhatsThis(i18n("Click on the widgets below to obtain help on the input methods."));
    mSourceCombo->setEnabled(false); // since !mEnableCheck->isChecked()
    mSourceCombo->addItems(QStringList() << i18nc("continuation of \"obtain signature text from\"", "Input Field Below")
                                         << i18nc("continuation of \"obtain signature text from\"", "File")
                                         << i18nc("continuation of \"obtain signature text from\"", "Output of Command"));
Laurent Montel's avatar
Laurent Montel committed
120
    QLabel *label = new QLabel(i18n("Obtain signature &text from:"), q);
Laurent Montel's avatar
Laurent Montel committed
121
    label->setBuddy(mSourceCombo);
Laurent Montel's avatar
Laurent Montel committed
122
    label->setEnabled(false); // since !mEnableCheck->isChecked()
Laurent Montel's avatar
Laurent Montel committed
123
124
125
126
    hlay->addWidget(label);
    hlay->addWidget(mSourceCombo, 1);

    // widget stack that is controlled by the source combo:
Laurent Montel's avatar
Laurent Montel committed
127
    auto *widgetStack = new QStackedWidget(q);
Laurent Montel's avatar
Laurent Montel committed
128
    widgetStack->setEnabled(false); // since !mEnableCheck->isChecked()
Laurent Montel's avatar
Laurent Montel committed
129
    vlay->addWidget(widgetStack, 1);
130
131
    q->connect(mSourceCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), widgetStack, &QStackedWidget::setCurrentIndex);
    q->connect(mSourceCombo, QOverload<int>::of(&QComboBox::highlighted), widgetStack, &QStackedWidget::setCurrentIndex);
Laurent Montel's avatar
Laurent Montel committed
132
133
    // connects for the enabling of the widgets depending on
    // signatureEnabled:
134
    q->connect(mEnableCheck, &QCheckBox::toggled, mSourceCombo, &QComboBox::setEnabled);
Laurent Montel's avatar
Laurent Montel committed
135
136
    q->connect(mEnableCheck, &QCheckBox::toggled, widgetStack, &QStackedWidget::setEnabled);
    q->connect(mEnableCheck, &QCheckBox::toggled, label, &QLabel::setEnabled);
Laurent Montel's avatar
Laurent Montel committed
137
    // The focus might be still in the widget that is disabled
Laurent Montel's avatar
Laurent Montel committed
138
    q->connect(mEnableCheck, &QCheckBox::clicked, mEnableCheck, QOverload<>::of(&QCheckBox::setFocus));
Laurent Montel's avatar
Laurent Montel committed
139
140
141

    int pageno = 0;
    // page 0: input field for direct entering:
Laurent Montel's avatar
Laurent Montel committed
142
    QWidget *page = new QWidget(widgetStack);
Laurent Montel's avatar
Laurent Montel committed
143
    widgetStack->insertWidget(pageno, page);
Laurent Montel's avatar
Laurent Montel committed
144
    auto *page_vlay = new QVBoxLayout(page);
145
    page_vlay->setContentsMargins(0, 0, 0, 0);
146

147
#ifndef QT_NO_TOOLBAR
Laurent Montel's avatar
Laurent Montel committed
148
149
150
    mEditToolBar = new KToolBar(q);
    mEditToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
    page_vlay->addWidget(mEditToolBar, 0);
151

Laurent Montel's avatar
Laurent Montel committed
152
153
154
    mFormatToolBar = new KToolBar(q);
    mFormatToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
    page_vlay->addWidget(mFormatToolBar, 1);
155
#endif
156

Laurent Montel's avatar
Laurent Montel committed
157
    mTextEdit = new KPIMTextEdit::RichTextComposer(q);
Laurent Montel's avatar
Laurent Montel committed
158

Laurent Montel's avatar
Laurent Montel committed
159
    auto *richTextEditorwidget = new KPIMTextEdit::RichTextEditorWidget(mTextEdit, q);
Laurent Montel's avatar
Laurent Montel committed
160
    page_vlay->addWidget(richTextEditorwidget, 2);
Laurent Montel's avatar
Laurent Montel committed
161
162
163
164
    mTextEdit->setWhatsThis(i18n("Use this field to enter an arbitrary static signature."));

    // Fill the toolbars.
    KActionCollection *actionCollection = new KActionCollection(q);
Laurent Montel's avatar
Laurent Montel committed
165
    mTextEdit->createActions(actionCollection);
166
#ifndef QT_NO_TOOLBAR
Laurent Montel's avatar
Laurent Montel committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_bold")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_italic")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_underline")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_strikeout")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_foreground_color")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_text_background_color")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_font_family")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_font_size")));
    mEditToolBar->addAction(actionCollection->action(QStringLiteral("format_reset")));

    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_list_style")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_list_indent_more")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_list_indent_less")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_list_indent_less")));
    mFormatToolBar->addSeparator();

    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_align_left")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_align_center")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_align_right")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_align_justify")));
    mFormatToolBar->addSeparator();

    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("insert_horizontal_rule")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("manage_link")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("format_painter")));

    mFormatToolBar->addSeparator();
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("add_image")));
    mFormatToolBar->addSeparator();
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("insert_html")));
    mFormatToolBar->addAction(actionCollection->action(QStringLiteral("insert_table")));
198
#endif
199

Laurent Montel's avatar
Laurent Montel committed
200
201
202
    hlay = new QHBoxLayout(); // inherits spacing
    page_vlay->addLayout(hlay);
    mHtmlCheck = new QCheckBox(i18n("&Use HTML"), page);
Laurent Montel's avatar
Laurent Montel committed
203
    q->connect(mHtmlCheck, &QCheckBox::clicked, q, &SignatureConfigurator::slotSetHtml);
Laurent Montel's avatar
Laurent Montel committed
204
205
206
    hlay->addWidget(mHtmlCheck);
    inlinedHtml = true;

Laurent Montel's avatar
Laurent Montel committed
207
    widgetStack->setCurrentIndex(0); // since mSourceCombo->currentItem() == 0
Laurent Montel's avatar
Laurent Montel committed
208
209
210
211

    // page 1: "signature file" requester, label, "edit file" button:
    ++pageno;
    page = new QWidget(widgetStack);
Laurent Montel's avatar
Laurent Montel committed
212
    widgetStack->insertWidget(pageno, page); // force sequential numbers (play safe)
Laurent Montel's avatar
Laurent Montel committed
213
    page_vlay = new QVBoxLayout(page);
214
    page_vlay->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
215
216
217
    hlay = new QHBoxLayout(); // inherits spacing
    page_vlay->addLayout(hlay);
    mFileRequester = new KUrlRequester(page);
218
    mFileRequester->setFilter(QStringLiteral("*.txt"));
Laurent Montel's avatar
Laurent Montel committed
219
220
221
222
223
224
225
226
227
    mFileRequester->setWhatsThis(
        i18n("Use this requester to specify a text file that contains your "
             "signature. It will be read every time you create a new mail or "
             "append a new signature."));
    label = new QLabel(i18n("S&pecify file:"), page);
    label->setBuddy(mFileRequester);
    hlay->addWidget(label);
    hlay->addWidget(mFileRequester, 1);
    mFileRequester->button()->setAutoDefault(false);
228
229
    q->connect(mFileRequester, &KUrlRequester::textEdited, q, &SignatureConfigurator::slotUrlChanged);
    q->connect(mFileRequester, &KUrlRequester::urlSelected, q, &SignatureConfigurator::slotUrlChanged);
Laurent Montel's avatar
Laurent Montel committed
230
231
    mEditButton = new QPushButton(i18n("Edit &File"), page);
    mEditButton->setWhatsThis(i18n("Opens the specified file in a text editor."));
Laurent Montel's avatar
Laurent Montel committed
232
    q->connect(mEditButton, &QPushButton::clicked, q, &SignatureConfigurator::slotEdit);
Laurent Montel's avatar
Laurent Montel committed
233
    mEditButton->setAutoDefault(false);
Laurent Montel's avatar
Laurent Montel committed
234
    mEditButton->setEnabled(false); // initially nothing to edit
Laurent Montel's avatar
Laurent Montel committed
235
    hlay->addWidget(mEditButton);
Laurent Montel's avatar
Laurent Montel committed
236
    page_vlay->addStretch(1); // spacer
Laurent Montel's avatar
Laurent Montel committed
237
238
239
240
241
242

    // page 2: "signature command" requester and label:
    ++pageno;
    page = new QWidget(widgetStack);
    widgetStack->insertWidget(pageno, page);
    page_vlay = new QVBoxLayout(page);
243
    page_vlay->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
244
245
246
    hlay = new QHBoxLayout(); // inherits spacing
    page_vlay->addLayout(hlay);
    mCommandEdit = new KLineEdit(page);
Laurent Montel's avatar
Laurent Montel committed
247
    mCommandEdit->setClearButtonEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
248
249
250
251
252
253
254
    mCommandEdit->setCompletionObject(new KShellCompletion());
    mCommandEdit->setAutoDeleteCompletionObject(true);
    mCommandEdit->setWhatsThis(
        i18n("You can add an arbitrary command here, either with or without path "
             "depending on whether or not the command is in your Path. For every "
             "new mail, KMail will execute the command and use what it outputs (to "
             "standard output) as a signature. Usual commands for use with this "
Yuri Chornoivan's avatar
Yuri Chornoivan committed
255
256
             "mechanism are \"fortune\" or \"ksig -random\". "
             "(Be careful, script needs a shebang line)."));
Laurent Montel's avatar
Laurent Montel committed
257
258
259
260
    label = new QLabel(i18n("S&pecify command:"), page);
    label->setBuddy(mCommandEdit);
    hlay->addWidget(label);
    hlay->addWidget(mCommandEdit, 1);
Laurent Montel's avatar
Laurent Montel committed
261
    page_vlay->addStretch(1); // spacer
262
263
}

Laurent Montel's avatar
Laurent Montel committed
264
SignatureConfigurator::SignatureConfigurator(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
265
266
    : QWidget(parent)
    , d(new Private(this))
Laurent Montel's avatar
Laurent Montel committed
267
{
268
    d->init();
Laurent Montel's avatar
Laurent Montel committed
269
}
270

Laurent Montel's avatar
Laurent Montel committed
271
272
SignatureConfigurator::~SignatureConfigurator()
{
273
    delete d;
Laurent Montel's avatar
Laurent Montel committed
274
}
275

Laurent Montel's avatar
Laurent Montel committed
276
277
bool SignatureConfigurator::isSignatureEnabled() const
{
Laurent Montel's avatar
Laurent Montel committed
278
    return d->mEnableCheck->isChecked();
Laurent Montel's avatar
Laurent Montel committed
279
}
280

Laurent Montel's avatar
Laurent Montel committed
281
282
283
284
void SignatureConfigurator::setSignatureEnabled(bool enable)
{
    d->mEnableCheck->setChecked(enable);
}
285

Laurent Montel's avatar
Laurent Montel committed
286
287
288
Signature::Type SignatureConfigurator::signatureType() const
{
    switch (d->mSourceCombo->currentIndex()) {
Laurent Montel's avatar
Laurent Montel committed
289
290
291
292
293
294
295
296
    case 0:
        return Signature::Inlined;
    case 1:
        return Signature::FromFile;
    case 2:
        return Signature::FromCommand;
    default:
        return Signature::Disabled;
297
    }
Laurent Montel's avatar
Laurent Montel committed
298
}
299

Laurent Montel's avatar
Laurent Montel committed
300
301
void SignatureConfigurator::setSignatureType(Signature::Type type)
{
302
    int idx = 0;
Laurent Montel's avatar
Laurent Montel committed
303
    switch (type) {
Laurent Montel's avatar
Laurent Montel committed
304
305
306
307
308
309
310
311
312
313
314
315
316
    case Signature::Inlined:
        idx = 0;
        break;
    case Signature::FromFile:
        idx = 1;
        break;
    case Signature::FromCommand:
        idx = 2;
        break;
    default:
        idx = 0;
        break;
    }
317

Laurent Montel's avatar
Laurent Montel committed
318
319
    d->mSourceCombo->setCurrentIndex(idx);
}
320

Laurent Montel's avatar
Laurent Montel committed
321
322
323
324
void SignatureConfigurator::setInlineText(const QString &text)
{
    d->mTextEdit->setTextOrHtml(text);
}
325

Laurent Montel's avatar
Fix api    
Laurent Montel committed
326
QString SignatureConfigurator::filePath() const
Laurent Montel's avatar
Laurent Montel committed
327
{
Laurent Montel's avatar
Laurent Montel committed
328
    QString file = d->mFileRequester->url().path();
329
330
331

    // Force the filename to be relative to ~ instead of $PWD depending
    // on the rest of the code (KRun::run in Edit and KFileItem on save)
Laurent Montel's avatar
Laurent Montel committed
332
    if (!file.isEmpty() && QFileInfo(file).isRelative()) {
Laurent Montel's avatar
Laurent Montel committed
333
        file = QDir::home().absolutePath() + QLatin1Char('/') + file;
334
    }
335
    return file;
Laurent Montel's avatar
Laurent Montel committed
336
}
337

Laurent Montel's avatar
Laurent Montel committed
338
339
void SignatureConfigurator::setFileURL(const QString &url)
{
Laurent Montel's avatar
Minor    
Laurent Montel committed
340
    d->mFileRequester->setUrl(QUrl::fromLocalFile(url));
341
    d->mEditButton->setDisabled(url.trimmed().isEmpty());
Laurent Montel's avatar
Laurent Montel committed
342
}
343

Laurent Montel's avatar
Fix api    
Laurent Montel committed
344
QString SignatureConfigurator::commandPath() const
Laurent Montel's avatar
Laurent Montel committed
345
{
Laurent Montel's avatar
Laurent Montel committed
346
    return d->mCommandEdit->text();
Laurent Montel's avatar
Laurent Montel committed
347
}
348

Laurent Montel's avatar
Laurent Montel committed
349
350
351
352
void SignatureConfigurator::setCommandURL(const QString &url)
{
    d->mCommandEdit->setText(url);
}
353

Laurent Montel's avatar
Laurent Montel committed
354
355
Signature SignatureConfigurator::signature() const
{
356
    Signature sig;
357
    const Signature::Type sigType = signatureType();
Laurent Montel's avatar
Laurent Montel committed
358
    switch (sigType) {
359
    case Signature::Inlined:
Laurent Montel's avatar
Laurent Montel committed
360
361
362
363
364
365
        sig.setInlinedHtml(d->inlinedHtml);
        sig.setText(d->inlinedHtml ? d->asCleanedHTML() : d->mTextEdit->textOrHtml());
        if (d->inlinedHtml) {
            if (!d->imageLocation.isEmpty()) {
                sig.setImageLocation(d->imageLocation);
            }
Laurent Montel's avatar
Laurent Montel committed
366
367
            const KPIMTextEdit::ImageWithNameList images = d->mTextEdit->composerControler()->composerImages()->imagesWithName();
            for (const KPIMTextEdit::ImageWithNamePtr &image : images) {
Laurent Montel's avatar
Laurent Montel committed
368
369
                sig.addImage(image->image, image->name);
            }
370
        }
Laurent Montel's avatar
Laurent Montel committed
371
        break;
372
    case Signature::FromCommand:
Laurent Montel's avatar
Fix api    
Laurent Montel committed
373
        sig.setPath(commandPath(), true);
Laurent Montel's avatar
Laurent Montel committed
374
        break;
375
    case Signature::FromFile:
Laurent Montel's avatar
Fix api    
Laurent Montel committed
376
        sig.setPath(filePath(), false);
Laurent Montel's avatar
Laurent Montel committed
377
        break;
378
    case Signature::Disabled:
Laurent Montel's avatar
Laurent Montel committed
379
380
        /* do nothing */
        break;
381
    }
Laurent Montel's avatar
Laurent Montel committed
382
383
    sig.setEnabledSignature(isSignatureEnabled());
    sig.setType(sigType);
384
    return sig;
Laurent Montel's avatar
Laurent Montel committed
385
}
386

Laurent Montel's avatar
Laurent Montel committed
387
388
389
390
void SignatureConfigurator::setSignature(const Signature &sig)
{
    setSignatureType(sig.type());
    setSignatureEnabled(sig.isEnabledSignature());
391

Laurent Montel's avatar
Laurent Montel committed
392
393
    if (sig.isInlinedHtml()) {
        d->mHtmlCheck->setCheckState(Qt::Checked);
394
    } else {
Laurent Montel's avatar
Laurent Montel committed
395
        d->mHtmlCheck->setCheckState(Qt::Unchecked);
396
    }
397
    slotSetHtml();
398
399

    // Let insertIntoTextEdit() handle setting the text, as that function also adds the images.
Laurent Montel's avatar
Laurent Montel committed
400
    d->mTextEdit->clear();
Laurent Montel's avatar
Laurent Montel committed
401
    sig.insertIntoTextEdit(KIdentityManagement::Signature::Start, KIdentityManagement::Signature::AddNothing, d->mTextEdit, true);
Laurent Montel's avatar
Laurent Montel committed
402
    if (sig.type() == Signature::FromFile) {
Laurent Montel's avatar
Fix api    
Laurent Montel committed
403
        setFileURL(sig.path());
404
    } else {
Laurent Montel's avatar
Laurent Montel committed
405
        setFileURL(QString());
406
    }
407

Laurent Montel's avatar
Laurent Montel committed
408
    if (sig.type() == Signature::FromCommand) {
Laurent Montel's avatar
Fix api    
Laurent Montel committed
409
        setCommandURL(sig.path());
410
    } else {
Laurent Montel's avatar
Laurent Montel committed
411
        setCommandURL(QString());
412
    }
Laurent Montel's avatar
Laurent Montel committed
413
}
414

415
void SignatureConfigurator::slotUrlChanged()
Laurent Montel's avatar
Laurent Montel committed
416
{
417
    const QString file = filePath();
Laurent Montel's avatar
Laurent Montel committed
418
    const QFileInfo infoFile(file);
419
    if (infoFile.isFile() && (infoFile.size() > 1000)) {
420
421
        KMessageBox::information(this, i18n("This text file size exceeds 1kb."), i18n("Text File Size"));
    }
422
    d->mEditButton->setDisabled(file.isEmpty());
Laurent Montel's avatar
Laurent Montel committed
423
}
424

Laurent Montel's avatar
Laurent Montel committed
425
426
void SignatureConfigurator::slotEdit()
{
427
    const QString url = filePath();
428
    // slotEnableEditButton should prevent this assert from being hit:
Laurent Montel's avatar
Laurent Montel committed
429
    assert(!url.isEmpty());
430

431
432
433
434
    KIO::OpenUrlJob *job = new KIO::OpenUrlJob(QUrl::fromLocalFile(url), QStringLiteral("text/plain"));
    job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
    job->setDeleteTemporaryFile(true);
    job->start();
Laurent Montel's avatar
Laurent Montel committed
435
}
436

Laurent Montel's avatar
Laurent Montel committed
437
438
439
440
441
// "use HTML"-checkbox (un)checked
void SignatureConfigurator::slotSetHtml()
{
    if (d->mHtmlCheck->checkState() == Qt::Unchecked) {
        d->mHtmlCheck->setText(i18n("&Use HTML"));
442
#ifndef QT_NO_TOOLBAR
Laurent Montel's avatar
Laurent Montel committed
443
444
445
446
        d->mEditToolBar->setVisible(false);
        d->mEditToolBar->setEnabled(false);
        d->mFormatToolBar->setVisible(false);
        d->mFormatToolBar->setEnabled(false);
447
#endif
Laurent Montel's avatar
Laurent Montel committed
448
449
450
451
452
        d->mTextEdit->switchToPlainText();
        d->inlinedHtml = false;
    } else {
        d->mHtmlCheck->setText(i18n("&Use HTML (disabling removes formatting)"));
        d->inlinedHtml = true;
453
#ifndef QT_NO_TOOLBAR
Laurent Montel's avatar
Laurent Montel committed
454
455
456
457
        d->mEditToolBar->setVisible(true);
        d->mEditToolBar->setEnabled(true);
        d->mFormatToolBar->setVisible(true);
        d->mFormatToolBar->setEnabled(true);
458
#endif
Laurent Montel's avatar
Laurent Montel committed
459
        d->mTextEdit->activateRichText();
460
    }
Laurent Montel's avatar
Laurent Montel committed
461
}
462

Laurent Montel's avatar
Laurent Montel committed
463
464
void SignatureConfigurator::setImageLocation(const QString &path)
{
465
    d->imageLocation = path;
Laurent Montel's avatar
Laurent Montel committed
466
467
468
469
}

void SignatureConfigurator::setImageLocation(const Identity &identity)
{
Laurent Montel's avatar
Laurent Montel committed
470
471
    const QString dir =
        QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/emailidentities/%1/").arg(QString::number(identity.uoid()));
Laurent Montel's avatar
Laurent Montel committed
472
    QDir().mkpath(dir);
Laurent Montel's avatar
Laurent Montel committed
473
    setImageLocation(dir);
Laurent Montel's avatar
Laurent Montel committed
474
}
475
}