Framework for looking up contacts directly in nepomuk in addition to going through...
[kdepim.git] / messagecomposer / inserttextfilejob.cpp
blob4ef595d754e7c3f5e338d3037bb282fd504a06e7
1 /*
2 * Copyright 2010 Thomas McGuire <mcguire@kde.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
19 #include "inserttextfilejob.h"
21 #include "kmeditor.h"
23 #include <KCharsets>
24 #include <KDebug>
25 #include <KIO/Job>
27 #include <QTextCodec>
29 using namespace Message;
31 InsertTextFileJob::InsertTextFileJob( QTextEdit *editor, const KUrl &url )
32 : KJob( editor ), mEditor( editor ), mUrl( url )
36 InsertTextFileJob::~InsertTextFileJob()
40 void InsertTextFileJob::slotFileData ( KIO::Job* job, const QByteArray& data )
42 Q_UNUSED( job );
43 mFileData += data;
46 void InsertTextFileJob::slotGetJobFinished( KJob* job )
48 if ( job->error() ) {
49 kWarning() << job->errorString();
50 setError( job->error() );
51 setErrorText( job->errorText() );
52 emitResult();
53 return;
56 if ( mEditor ) {
57 const QTextCodec *fileCodec = 0;
58 if ( !mEncoding.isEmpty() ) {
59 fileCodec = KGlobal::charsets()->codecForName( mEncoding );
60 if ( fileCodec ) {
61 mEditor->textCursor().insertText( fileCodec->toUnicode( mFileData.data() ) );
62 } else {
63 mEditor->textCursor().insertText( QString::fromLocal8Bit( mFileData.data() ) );
68 emitResult();
71 void InsertTextFileJob::setEncoding( const QString &encoding )
73 mEncoding = encoding;
76 void InsertTextFileJob::start()
78 KIO::TransferJob *job = KIO::get( mUrl );
79 connect( job, SIGNAL(result(KJob*)),
80 this, SLOT(slotGetJobFinished(KJob*)) );
81 connect( job, SIGNAL(data(KIO::Job*,QByteArray)),
82 this, SLOT(slotFileData(KIO::Job*,QByteArray)) );
83 job->start();
86 #include "inserttextfilejob.moc"