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 <KCharsets>
11 #include "messagecomposer_debug.h"
12 #include <KIO/Job>
13 
14 #include <QTextCodec>
15 #include <QPointer>
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()
40 {
41  delete d;
42 }
43 
44 void InsertTextFileJob::slotFileData(KIO::Job *job, const QByteArray &data)
45 {
46  Q_UNUSED(job);
47  d->mFileData += data;
48 }
49 
50 void InsertTextFileJob::slotGetJobFinished(KJob *job)
51 {
52  if (job->error()) {
53  qCWarning(MESSAGECOMPOSER_LOG) << job->errorString();
54  setError(job->error());
55  setErrorText(job->errorText());
56  emitResult();
57  return;
58  }
59 
60  if (d->mEditor) {
61  if (!d->mEncoding.isEmpty()) {
62  const QTextCodec *fileCodec = KCharsets::charsets()->codecForName(d->mEncoding);
63  if (fileCodec) {
64  d->mEditor->textCursor().insertText(fileCodec->toUnicode(d->mFileData.data()));
65  } else {
66  d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
67  }
68  } else {
69  d->mEditor->textCursor().insertText(QString::fromLocal8Bit(d->mFileData.data()));
70  }
71  }
72 
73  emitResult();
74 }
75 
76 void InsertTextFileJob::setEncoding(const QString &encoding)
77 {
78  d->mEncoding = encoding;
79 }
80 
81 void InsertTextFileJob::start()
82 {
83  KIO::TransferJob *job = KIO::get(d->mUrl);
84  connect(job, &KIO::TransferJob::result, this, &InsertTextFileJob::slotGetJobFinished);
85  connect(job, &KIO::TransferJob::data, this, &InsertTextFileJob::slotFileData);
86  job->start();
87 }
QTextCodec * codecForName(const QString &name) const
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
QString errorText() const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 23:15:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.