Make the boss happy.
[kdepim.git] / messagecore / attachmentfromurljob.cpp
blob987aa23aa0dd5b7b80cdc4e96c1a50fa401c9ff6
1 /*
2 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
4 Parts based on KMail code by various authors.
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
22 #include "attachmentfromurljob.h"
24 #include <KDebug>
25 #include <KGlobal>
26 #include <KIO/Scheduler>
27 #include <KLocale>
28 #include <KMimeType>
30 #include <QtCore/QFileInfo>
32 #include <boost/shared_ptr.hpp>
34 using namespace MessageCore;
36 class MessageCore::AttachmentFromUrlJob::Private
38 public:
39 Private( AttachmentFromUrlJob *qq );
41 void transferJobData( KIO::Job *job, const QByteArray &jobData );
42 void transferJobResult( KJob *job );
44 AttachmentFromUrlJob *const q;
45 KUrl mUrl;
46 QByteArray mData;
49 AttachmentFromUrlJob::Private::Private( AttachmentFromUrlJob *qq )
50 : q( qq )
54 void AttachmentFromUrlJob::Private::transferJobData( KIO::Job *job, const QByteArray &jobData )
56 Q_UNUSED( job );
57 mData += jobData;
60 void AttachmentFromUrlJob::Private::transferJobResult( KJob *job )
62 #ifndef KDEPIM_MOBILE_UI
63 if ( job->error() ) {
64 // TODO this loses useful stuff from KIO, like detailed error descriptions, causes+solutions,
65 // ... use UiDelegate somehow?
66 q->setError( job->error() );
67 q->setErrorText( job->errorString() );
68 q->emitResult();
69 return;
72 Q_ASSERT( dynamic_cast<KIO::TransferJob*>( job ) );
73 KIO::TransferJob *transferJob = static_cast<KIO::TransferJob*>( job );
75 // Determine the MIME type and filename of the attachment.
76 const QString mimeType = transferJob->mimetype();
77 kDebug() << "Mimetype is" << mimeType;
79 QString fileName = mUrl.fileName();
80 if ( fileName.isEmpty() ) {
81 const KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases );
82 if ( mimeTypePtr ) {
83 fileName = i18nc( "a file called 'unknown.ext'", "unknown%1",
84 mimeTypePtr->mainExtension() );
85 } else {
86 fileName = i18nc( "a file called 'unknown'", "unknown" );
89 #else
90 Q_UNUSED( job );
91 const QString filePath = mUrl.toLocalFile();
92 const QFileInfo fileInfo( filePath );
93 const QString fileName = fileInfo.fileName();
94 QFile file( filePath );
95 if ( file.open( QFile::ReadOnly ) ) {
96 mData = file.readAll();
97 } else {
98 q->setError( KJob::UserDefinedError );
99 q->setErrorText( i18n( "Could not read file %1.", fileName ) );
100 q->emitResult();
101 return;
103 const QString mimeType = KMimeType::findByContent( mData )->name();
104 #endif
106 // Create the AttachmentPart.
107 Q_ASSERT( q->attachmentPart() == 0 ); // Not created before.
109 AttachmentPart::Ptr part = AttachmentPart::Ptr( new AttachmentPart );
110 part->setCharset( mUrl.fileEncoding().toLatin1() );
111 part->setMimeType( mimeType.toLatin1() );
112 part->setName( fileName );
113 part->setFileName( fileName );
114 part->setData( mData );
115 q->setAttachmentPart( part );
116 q->emitResult(); // Success.
120 AttachmentFromUrlJob::AttachmentFromUrlJob( const KUrl &url, QObject *parent )
121 : AttachmentFromUrlBaseJob( url, parent ),
122 d( new Private( this ) )
124 d->mUrl = url;
127 AttachmentFromUrlJob::~AttachmentFromUrlJob()
129 delete d;
132 void AttachmentFromUrlJob::doStart()
134 if ( !d->mUrl.isValid() ) {
135 setError( KJob::UserDefinedError );
136 setErrorText( i18n( "\"%1\" not found. Please specify the full path.", d->mUrl.prettyUrl() ) );
137 emitResult();
138 return;
141 if ( maximumAllowedSize() != -1 && d->mUrl.isLocalFile() ) {
142 const qint64 size = QFileInfo( d->mUrl.toLocalFile() ).size();
143 if ( size > maximumAllowedSize() ) {
144 setError( KJob::UserDefinedError );
145 setErrorText( i18n( "You may not attach files bigger than %1.",
146 KGlobal::locale()->formatByteSize( maximumAllowedSize() ) ) );
147 emitResult();
148 return;
152 Q_ASSERT( d->mData.isEmpty() ); // Not started twice.
154 #ifndef KDEPIM_MOBILE_UI
155 KIO::TransferJob *job = KIO::get( d->mUrl, KIO::NoReload,
156 ( uiDelegate() ? KIO::DefaultFlags : KIO::HideProgressInfo ) );
157 QObject::connect( job, SIGNAL(result(KJob*)),
158 this, SLOT(transferJobResult(KJob*)) );
159 QObject::connect( job, SIGNAL(data(KIO::Job*,QByteArray)),
160 this, SLOT(transferJobData(KIO::Job*,QByteArray)) );
161 #else
162 d->transferJobResult( 0 );
163 #endif
166 #include "attachmentfromurljob.moc"