Messagelib

inserttextfilejob.cpp
1/*
2 * SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6#include "inserttextfilejob.h"
7
8#include <QTextEdit>
9
10#include "messagecomposer_debug.h"
11#include <KIO/TransferJob>
12
13#include <QPointer>
14#include <QStringDecoder>
15
16using namespace MessageComposer;
17class MessageComposer::InsertTextFileJobPrivate
18{
19public:
20 InsertTextFileJobPrivate(QTextEdit *editor, const QUrl &url)
21 : mEditor(editor)
22 , mUrl(url)
23 {
24 }
25
26 QPointer<QTextEdit> mEditor;
27 QUrl mUrl;
28 QString mEncoding;
29 QByteArray mFileData;
30};
31
32InsertTextFileJob::InsertTextFileJob(QTextEdit *editor, const QUrl &url)
33 : KJob(editor)
34 , d(new MessageComposer::InsertTextFileJobPrivate(editor, url))
35{
36}
37
38InsertTextFileJob::~InsertTextFileJob() = default;
39
40void InsertTextFileJob::slotFileData(KIO::Job *job, const QByteArray &data)
41{
42 Q_UNUSED(job)
43 d->mFileData += data;
44}
45
46void InsertTextFileJob::slotGetJobFinished(KJob *job)
47{
48 if (job->error()) {
49 qCWarning(MESSAGECOMPOSER_LOG) << job->errorString();
50 setError(job->error());
51 setErrorText(job->errorText());
52 emitResult();
53 return;
54 }
55
56 if (d->mEditor) {
57 if (!d->mEncoding.isEmpty()) {
58 QStringDecoder fileCodec(d->mEncoding.toLatin1().constData());
59 if (fileCodec.isValid()) {
60 d->mEditor->textCursor().insertText(fileCodec.decode(d->mFileData.data()));
61 } else {
62 d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
63 }
64 } else {
65 d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
66 }
67 }
68
69 emitResult();
70}
71
72void InsertTextFileJob::setEncoding(const QString &encoding)
73{
74 d->mEncoding = encoding;
75}
76
77void InsertTextFileJob::start()
78{
79 KIO::TransferJob *job = KIO::get(d->mUrl);
80 connect(job, &KIO::TransferJob::result, this, &InsertTextFileJob::slotGetJobFinished);
81 connect(job, &KIO::TransferJob::data, this, &InsertTextFileJob::slotFileData);
82 job->start();
83}
84
85#include "moc_inserttextfilejob.cpp"
void data(KIO::Job *job, const QByteArray &data)
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
virtual Q_SCRIPTABLE void start()=0
QString errorText() const
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLocal8Bit(QByteArrayView str)
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.