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
22 #include "attachmentfromurljob.h"
26 #include <KIO/Scheduler>
30 #include <QtCore/QFileInfo>
32 #include <boost/shared_ptr.hpp>
34 using namespace MessageCore
;
36 class MessageCore::AttachmentFromUrlJob::Private
39 Private( AttachmentFromUrlJob
*qq
);
41 void transferJobData( KIO::Job
*job
, const QByteArray
&jobData
);
42 void transferJobResult( KJob
*job
);
44 AttachmentFromUrlJob
*const q
;
49 AttachmentFromUrlJob::Private::Private( AttachmentFromUrlJob
*qq
)
54 void AttachmentFromUrlJob::Private::transferJobData( KIO::Job
*job
, const QByteArray
&jobData
)
60 void AttachmentFromUrlJob::Private::transferJobResult( KJob
*job
)
62 #ifndef KDEPIM_MOBILE_UI
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() );
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
);
83 fileName
= i18nc( "a file called 'unknown.ext'", "unknown%1",
84 mimeTypePtr
->mainExtension() );
86 fileName
= i18nc( "a file called 'unknown'", "unknown" );
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();
98 q
->setError( KJob::UserDefinedError
);
99 q
->setErrorText( i18n( "Could not read file %1.", fileName
) );
103 const QString mimeType
= KMimeType::findByContent( mData
)->name();
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 ) )
127 AttachmentFromUrlJob::~AttachmentFromUrlJob()
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() ) );
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() ) ) );
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
)) );
162 d
->transferJobResult( 0 );
166 #include "attachmentfromurljob.moc"