emailuis/nmailui/src/nmeditortextdocument.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "nmuiheaders.h"
       
    20 
       
    21 static const char *NMUI_DOC_DEFAULT_IMAGE = ":/trolltech/styles/commonstyle/images/file-16.png";
       
    22 
       
    23 /*!
       
    24     \class NmEditorTextDocument
       
    25     \brief Text document with URL image fetching support
       
    26 */
       
    27 
       
    28 /*!
       
    29     Constructor. Caller needs to ensure manager is valid reference.
       
    30 */
       
    31 NmEditorTextDocument::NmEditorTextDocument(QNetworkAccessManager &manager) :
       
    32     mManager(manager)
       
    33 {
       
    34     NM_FUNCTION;
       
    35     
       
    36     connect(&mManager, SIGNAL(finished(QNetworkReply*)),
       
    37         this, SLOT(replyFinished(QNetworkReply*)));
       
    38 }
       
    39 
       
    40 /*!
       
    41     Destructor
       
    42 */
       
    43 NmEditorTextDocument::~NmEditorTextDocument()
       
    44 {
       
    45     NM_FUNCTION;
       
    46     
       
    47     foreach(QNetworkReply *reply, mReplyList) {
       
    48         if(reply) {
       
    49             reply->abort();
       
    50             reply->deleteLater();
       
    51         }
       
    52     }
       
    53 }
       
    54 
       
    55 /*!
       
    56     replyFinished. Signaled when the image has been loaded from Internet.
       
    57 */
       
    58 void NmEditorTextDocument::replyFinished(QNetworkReply *reply)
       
    59 {
       
    60     NM_FUNCTION;
       
    61     
       
    62     if(reply) {
       
    63         if(reply->error() == QNetworkReply::NoError) {
       
    64             QPixmap image;
       
    65             if(image.loadFromData(reply->readAll())) { 
       
    66                 addResource(QTextDocument::ImageResource, reply->url(), QVariant(image));
       
    67                 emit documentLayoutChanged();
       
    68             }
       
    69         }
       
    70         // If this has created the request, then this needs to handle deletion also.
       
    71         if (mReplyList.removeAll(reply)) {
       
    72             reply->deleteLater();
       
    73         }
       
    74     }
       
    75 }
       
    76 
       
    77 /*!
       
    78     loadResource. Starts fetching image from given URL.
       
    79 */
       
    80 QVariant NmEditorTextDocument::loadResource(int type, const QUrl &name)
       
    81 {
       
    82     NM_FUNCTION;
       
    83     
       
    84     QVariant retVal;
       
    85     
       
    86     if(type == QTextDocument::ImageResource) {    
       
    87         // Load image data from the cache if it exists
       
    88         if(mManager.cache()) {
       
    89             QIODevice *ioDevice = mManager.cache()->data(name);
       
    90             if(ioDevice) {
       
    91                 QPixmap image;
       
    92                 if(image.loadFromData(ioDevice->readAll())) {
       
    93                     addResource(QTextDocument::ImageResource, name, QVariant(image));
       
    94                     retVal = QVariant(image);
       
    95                 }
       
    96                 delete ioDevice;
       
    97             }
       
    98         }
       
    99         
       
   100         // Loading from cache failed, try from internet.
       
   101         if(retVal.isNull()) {
       
   102             // Make a request to load the image from Internet. Store the reply
       
   103             // to be able to abort it if necessary.
       
   104             QNetworkRequest request(name);
       
   105             mReplyList.append(mManager.get(request));
       
   106             
       
   107             // Load Qt's default image to prevent multiple calls to loadResource.
       
   108             // This function keeps on getting called repeatedly until 
       
   109             // resource is added. 
       
   110             QPixmap image = QPixmap(QLatin1String(NMUI_DOC_DEFAULT_IMAGE));
       
   111             addResource(QTextDocument::ImageResource, name, QVariant(image));
       
   112             retVal = image;
       
   113         }
       
   114     }
       
   115     
       
   116     // Could not find the resource -> try Qt implementation
       
   117     if(retVal.isNull()) {
       
   118         retVal = QTextDocument::loadResource(type, name);
       
   119     }
       
   120     return retVal;
       
   121 }