Messagelib

inserttextfilejob.cpp
1 /*
2  * SPDX-FileCopyrightText: 2010 Thomas McGuire <[email protected]>
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 <KCharsets>
12 #include <KIO/Job>
13 
14 #include <QPointer>
15 #include <QTextCodec>
16 
17 using namespace MessageComposer;
18 class MessageComposer::InsertTextFileJobPrivate
19 {
20 public:
21  InsertTextFileJobPrivate(QTextEdit *editor, const QUrl &url)
22  : mEditor(editor)
23  , mUrl(url)
24  {
25  }
26 
27  QPointer<QTextEdit> mEditor;
28  QUrl mUrl;
29  QString mEncoding;
30  QByteArray mFileData;
31 };
32 
33 InsertTextFileJob::InsertTextFileJob(QTextEdit *editor, const QUrl &url)
34  : KJob(editor)
35  , d(new MessageComposer::InsertTextFileJobPrivate(editor, url))
36 {
37 }
38 
39 InsertTextFileJob::~InsertTextFileJob() = default;
40 
41 void InsertTextFileJob::slotFileData(KIO::Job *job, const QByteArray &data)
42 {
43  Q_UNUSED(job)
44  d->mFileData += data;
45 }
46 
47 void InsertTextFileJob::slotGetJobFinished(KJob *job)
48 {
49  if (job->error()) {
50  qCWarning(MESSAGECOMPOSER_LOG) << job->errorString();
51  setError(job->error());
52  setErrorText(job->errorText());
53  emitResult();
54  return;
55  }
56 
57  if (d->mEditor) {
58  if (!d->mEncoding.isEmpty()) {
59  const QTextCodec *fileCodec = KCharsets::charsets()->codecForName(d->mEncoding);
60  if (fileCodec) {
61  d->mEditor->textCursor().insertText(fileCodec->toUnicode(d->mFileData.data()));
62  } else {
63  d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
64  }
65  } else {
66  d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
67  }
68  }
69 
70  emitResult();
71 }
72 
73 void InsertTextFileJob::setEncoding(const QString &encoding)
74 {
75  d->mEncoding = encoding;
76 }
77 
78 void InsertTextFileJob::start()
79 {
80  KIO::TransferJob *job = KIO::get(d->mUrl);
81  connect(job, &KIO::TransferJob::result, this, &InsertTextFileJob::slotGetJobFinished);
82  connect(job, &KIO::TransferJob::data, this, &InsertTextFileJob::slotFileData);
83  job->start();
84 }
QTextCodec * codecForName(const QString &name) const
A job that downloads a given URL, interprets the result as a text file with the given encoding and th...
virtual QString errorString() const
QString fromLocal8Bit(const char *str, int size)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
static KCharsets * charsets()
void data(KIO::Job *job, const QByteArray &data)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
virtual Q_SCRIPTABLE void start()=0
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toUnicode(const QByteArray &a) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:16:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.