attachmentfromurljob.cpp 5.21 KB
Newer Older
1 2
/*
  Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  Parts based on KMail code by various authors.

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "attachmentfromurljob.h"

#include <KDebug>
#include <KGlobal>
#include <KIO/Scheduler>
Laurent Montel's avatar
Laurent Montel committed
27
#include <KLocalizedString>
28
#include <KMimeType>
Laurent Montel's avatar
Laurent Montel committed
29
#include <KLocale>
30

31 32 33
#include <QtCore/QFileInfo>

#include <boost/shared_ptr.hpp>
34

35 36 37
using namespace MessageCore;

class MessageCore::AttachmentFromUrlJob::Private
38
{
Laurent Montel's avatar
Laurent Montel committed
39
public:
40 41 42 43 44 45
    Private( AttachmentFromUrlJob *qq );

    void transferJobData( KIO::Job *job, const QByteArray &jobData );
    void transferJobResult( KJob *job );

    AttachmentFromUrlJob *const q;
46
    QByteArray mData;
47 48 49
};

AttachmentFromUrlJob::Private::Private( AttachmentFromUrlJob *qq )
Laurent Montel's avatar
Laurent Montel committed
50
    : q( qq )
51 52 53 54 55
{
}

void AttachmentFromUrlJob::Private::transferJobData( KIO::Job *job, const QByteArray &jobData )
{
Laurent Montel's avatar
Laurent Montel committed
56 57
    Q_UNUSED( job );
    mData += jobData;
58 59 60 61
}

void AttachmentFromUrlJob::Private::transferJobResult( KJob *job )
{
62
#ifndef KDEPIM_MOBILE_UI
Laurent Montel's avatar
Laurent Montel committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if ( job->error() ) {
        // TODO this loses useful stuff from KIO, like detailed error descriptions, causes+solutions,
        // ... use UiDelegate somehow?
        q->setError( job->error() );
        q->setErrorText( job->errorString() );
        q->emitResult();
        return;
    }

    Q_ASSERT( dynamic_cast<KIO::TransferJob*>( job ) );
    KIO::TransferJob *transferJob = static_cast<KIO::TransferJob*>( job );

    // Determine the MIME type and filename of the attachment.
    const QString mimeType = transferJob->mimetype();
    kDebug() << "Mimetype is" << mimeType;

79
    QString fileName = q->url().fileName();
Laurent Montel's avatar
Laurent Montel committed
80 81 82 83 84 85 86 87
    if ( fileName.isEmpty() ) {
        const KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases );
        if ( mimeTypePtr ) {
            fileName = i18nc( "a file called 'unknown.ext'", "unknown%1",
                              mimeTypePtr->mainExtension() );
        } else {
            fileName = i18nc( "a file called 'unknown'", "unknown" );
        }
88
    }
89
#else
Laurent Montel's avatar
Laurent Montel committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
    Q_UNUSED( job );
    const QString filePath = mUrl.toLocalFile();
    const QFileInfo fileInfo( filePath );
    const QString fileName = fileInfo.fileName();
    QFile file( filePath );
    if ( file.open( QFile::ReadOnly ) ) {
        mData = file.readAll();
    } else {
        q->setError( KJob::UserDefinedError );
        q->setErrorText( i18n( "Could not read file %1.", fileName ) );
        q->emitResult();
        return;
    }
    const QString mimeType = KMimeType::findByContent( mData )->name();
104
#endif
105

Laurent Montel's avatar
Laurent Montel committed
106 107 108 109
    // Create the AttachmentPart.
    Q_ASSERT( q->attachmentPart() == 0 ); // Not created before.

    AttachmentPart::Ptr part = AttachmentPart::Ptr( new AttachmentPart );
110
    part->setCharset( q->url().fileEncoding().toLatin1() );
Laurent Montel's avatar
Laurent Montel committed
111 112 113
    part->setMimeType( mimeType.toLatin1() );
    part->setName( fileName );
    part->setFileName( fileName );
114
    part->setUrl(q->url());
Laurent Montel's avatar
Laurent Montel committed
115 116 117
    part->setData( mData );
    q->setAttachmentPart( part );
    q->emitResult(); // Success.
118 119 120 121
}


AttachmentFromUrlJob::AttachmentFromUrlJob( const KUrl &url, QObject *parent )
Laurent Montel's avatar
Laurent Montel committed
122 123
    : AttachmentFromUrlBaseJob( url, parent ),
      d( new Private( this ) )
124 125 126 127 128
{
}

AttachmentFromUrlJob::~AttachmentFromUrlJob()
{
Laurent Montel's avatar
Laurent Montel committed
129
    delete d;
130 131 132 133
}

void AttachmentFromUrlJob::doStart()
{
134
    if ( !url().isValid() ) {
Laurent Montel's avatar
Laurent Montel committed
135
        setError( KJob::UserDefinedError );
136
        setErrorText( i18n( "\"%1\" not found. Please specify the full path.", url().prettyUrl() ) );
Laurent Montel's avatar
Laurent Montel committed
137 138 139 140
        emitResult();
        return;
    }

141 142
    if ( maximumAllowedSize() != -1 && url().isLocalFile() ) {
        const qint64 size = QFileInfo( url().toLocalFile() ).size();
Laurent Montel's avatar
Laurent Montel committed
143 144 145 146 147 148 149
        if ( size > maximumAllowedSize() ) {
            setError( KJob::UserDefinedError );
            setErrorText( i18n( "You may not attach files bigger than %1. Share it with storage service.",
                                KGlobal::locale()->formatByteSize( maximumAllowedSize() ) ) );
            emitResult();
            return;
        }
150 151
    }

Laurent Montel's avatar
Laurent Montel committed
152
    Q_ASSERT( d->mData.isEmpty() ); // Not started twice.
153

154
#ifndef KDEPIM_MOBILE_UI
155
    KIO::TransferJob *job = KIO::get( url(), KIO::NoReload,
Laurent Montel's avatar
Laurent Montel committed
156 157 158 159 160
                                      ( uiDelegate() ? KIO::DefaultFlags : KIO::HideProgressInfo ) );
    QObject::connect( job, SIGNAL(result(KJob*)),
                      this, SLOT(transferJobResult(KJob*)) );
    QObject::connect( job, SIGNAL(data(KIO::Job*,QByteArray)),
                      this, SLOT(transferJobData(KIO::Job*,QByteArray)) );
161
#else
Laurent Montel's avatar
Laurent Montel committed
162
    d->transferJobResult( 0 );
163
#endif
164 165
}

Stephen Kelly's avatar
Stephen Kelly committed
166
#include "moc_attachmentfromurljob.cpp"