emailuis/nmailui/src/nmeditortextdocument.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 14 May 2010 15:41:10 +0300
changeset 23 2dc6caa42ec3
parent 20 ecc8def7944a
child 30 759dc5235cdb
permissions -rw-r--r--
Revision: 201017 Kit: 201019

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/


#include "nmuiheaders.h"

static const char *NMUI_DOC_DEFAULT_IMAGE = ":/trolltech/styles/commonstyle/images/file-16.png";

/*!
    \class NmEditorTextDocument
    \brief Text document with URL image fetching support
*/

/*!
    Constructor. Caller needs to ensure manager is valid reference.
*/
NmEditorTextDocument::NmEditorTextDocument(QNetworkAccessManager &manager) :
    mManager(manager)
{
    connect(&mManager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(replyFinished(QNetworkReply*)));
}

/*!
    Destructor
*/
NmEditorTextDocument::~NmEditorTextDocument()
{
    foreach(QNetworkReply *reply, mReplyList) {
        if(reply) {
            reply->abort();
            reply->deleteLater();
        }
    }
}

/*!
    replyFinished. Signaled when the image has been loaded from Internet.
*/
void NmEditorTextDocument::replyFinished(QNetworkReply *reply)
{
    if(reply) {
        if(reply->error() == QNetworkReply::NoError) {
            QPixmap image;
            if(image.loadFromData(reply->readAll())) { 
                addResource(QTextDocument::ImageResource, reply->url(), QVariant(image));
                emit documentLayoutChanged();
            }
        }
        mReplyList.removeAll(reply);
        reply->deleteLater();
    }
}

/*!
    loadResource. Starts fetching image from given URL.
*/
QVariant NmEditorTextDocument::loadResource(int type, const QUrl &name)
{
    QVariant retVal;
    
    if(type == QTextDocument::ImageResource) {    
        // Load image data from the cache if it exists
        if(mManager.cache()) {
            QIODevice *ioDevice = mManager.cache()->data(name);
            if(ioDevice) {
                QPixmap image;
                if(image.loadFromData(ioDevice->readAll())) {
                    addResource(QTextDocument::ImageResource, name, QVariant(image));
                    retVal = QVariant(image);
                }
                delete ioDevice;
            }
        }
        
        // Loading from cache failed, try from internet.
        if(retVal.isNull()) {
            // Make a request to load the image from Internet. Store the reply
            // to be able to abort it if necessary.
            QNetworkRequest request(name);
            mReplyList.append(mManager.get(request));
            
            // Load Qt's default image to prevent multiple calls to loadResource.
            // This function keeps on getting called repeatedly until 
            // resource is added. 
            QPixmap image = QPixmap(QLatin1String(NMUI_DOC_DEFAULT_IMAGE));
            addResource(QTextDocument::ImageResource, name, QVariant(image));
            retVal = image;
        }
    }
    
    // Could not find the resource -> try Qt implementation
    if(retVal.isNull()) {
        retVal = QTextDocument::loadResource(type, name);
    }
    return retVal;
}