emailuis/nmailui/src/nmviewerviewnetreply.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_NET_REPLY_CONTENT_ID = "cid";
       
    22 
       
    23 /*!
       
    24     \class NmViewerViewNetReply
       
    25     \brief Mail viewer network reply class
       
    26 */
       
    27 
       
    28 /*!
       
    29     Constructor
       
    30 */
       
    31 NmViewerViewNetReply::NmViewerViewNetReply(QVariant data, NmUiEngine &uiEngine)
       
    32     : QNetworkReply(),
       
    33       mDataArray(data.toByteArray()),
       
    34       mUiEngine(uiEngine),
       
    35       mOperation(NULL),
       
    36       mReadIndex(0)
       
    37 {
       
    38     NM_FUNCTION;
       
    39     
       
    40     open(ReadWrite);
       
    41     setReadBufferSize(mDataArray.length());
       
    42     // QNetworkAccessManager starts listening the signals only
       
    43     // after the construction, so we cannot signal reply to be
       
    44     // ready instantly. We need to emit the signals after the
       
    45     // construction.
       
    46     QMetaObject::invokeMethod(this, "signalReady", Qt::QueuedConnection);
       
    47 }
       
    48 
       
    49 /*!
       
    50 
       
    51 */
       
    52 NmViewerViewNetReply::NmViewerViewNetReply(QVariant data, NmUiEngine &uiEngine, 
       
    53         const NmId &mailboxId, const NmId &folderId, const NmId &messageId, 
       
    54         const NmId &messagePartId)
       
    55     : QNetworkReply(),
       
    56       mDataArray(data.toByteArray()),
       
    57       mUiEngine(uiEngine),
       
    58       mMailboxId(mailboxId),
       
    59       mFolderId(folderId),
       
    60       mMessageId(messageId),
       
    61       mMessagePartId(messagePartId),
       
    62       mOperation(NULL),
       
    63       mReadIndex(0)
       
    64 {
       
    65     mOperation = uiEngine.fetchMessagePart(mMailboxId, mFolderId, mMessageId, mMessagePartId);
       
    66     if (mOperation) {
       
    67         connect(mOperation, SIGNAL(operationCompleted(int)),
       
    68             this, SLOT(fetchCompleted(int)));
       
    69         connect(mOperation, SIGNAL(operationCancelled()),
       
    70             this, SLOT(fetchCancelled()));
       
    71     }
       
    72     else {
       
    73         open(ReadWrite);
       
    74         setReadBufferSize(mDataArray.length());
       
    75         QMetaObject::invokeMethod(this, "signalReady", Qt::QueuedConnection);
       
    76     }
       
    77 }
       
    78 
       
    79 /*!
       
    80     Destructor
       
    81 */
       
    82 NmViewerViewNetReply::~NmViewerViewNetReply()
       
    83 {
       
    84     NM_FUNCTION;
       
    85 }
       
    86 
       
    87 /*!
       
    88     signalReady
       
    89 */
       
    90 void NmViewerViewNetReply::signalReady()
       
    91 {
       
    92     NM_FUNCTION;
       
    93     
       
    94     // Insert embedded images into cache manually
       
    95     if(manager()) {
       
    96         if(manager()->cache() && request().url().scheme() == NMUI_NET_REPLY_CONTENT_ID) {
       
    97             // Store url to use for reply in access manager finished emitting. 
       
    98             setUrl(request().url());
       
    99             
       
   100             // Metadata required for inserted data
       
   101             QNetworkCacheMetaData metaData;
       
   102             metaData.setUrl(request().url());
       
   103             metaData.setSaveToDisk(true);
       
   104             
       
   105             // Insert data into cache
       
   106             QIODevice *device = manager()->cache()->prepare(metaData);
       
   107             if(device) {
       
   108                 device->write(mDataArray);
       
   109                 manager()->cache()->insert(device);
       
   110             }
       
   111         }
       
   112     }
       
   113     // Emit these signals to properly emulate downloading from URL
       
   114     emit readyRead();
       
   115     emit finished();
       
   116 }
       
   117 
       
   118 /*!
       
   119     Slot. Called when fetch operation completes
       
   120 */
       
   121 void NmViewerViewNetReply::fetchCompleted(int result)
       
   122 {
       
   123     NM_FUNCTION;
       
   124     
       
   125     Q_UNUSED(result);
       
   126     NmMessage *message = mUiEngine.message(
       
   127                         mMailboxId, mFolderId, mMessageId);
       
   128     if (message) {
       
   129         QList<NmMessagePart*> partList;
       
   130         message->attachmentList(partList);
       
   131         NmMessagePart *part(NULL);
       
   132         for (int i = 0; !part && i < partList.count(); i++) {
       
   133             if (partList[i]->partId() == mMessagePartId) {
       
   134                 part = partList[i];
       
   135                 break;
       
   136             }
       
   137         }
       
   138         if (part) {
       
   139             int error = mUiEngine.contentToMessagePart(mMailboxId, mFolderId, mMessageId, *part);
       
   140             if (error == NmNoError) {
       
   141                 mDataArray = part->binaryContent();
       
   142             }
       
   143         }
       
   144     }
       
   145     delete message;
       
   146     message = NULL;
       
   147     open(ReadWrite);
       
   148     setReadBufferSize(mDataArray.length());
       
   149     QMetaObject::invokeMethod(this, "signalReady", Qt::QueuedConnection);
       
   150 }
       
   151 
       
   152 /*!
       
   153     Slot. Called if fetch operation is cancelled
       
   154 */
       
   155 void NmViewerViewNetReply::fetchCancelled()
       
   156 {
       
   157     NM_FUNCTION;
       
   158     
       
   159     // just call fetch completed
       
   160     fetchCompleted(NmCancelError);
       
   161 }
       
   162 
       
   163 /*!
       
   164     setOriginalRequest. This function is created to provide access to call
       
   165     base class' protected function setRequest
       
   166 */
       
   167 void NmViewerViewNetReply::setOriginalRequest(const QNetworkRequest &request)
       
   168 {
       
   169     NM_FUNCTION;
       
   170     
       
   171     setRequest(request);
       
   172 }
       
   173 
       
   174 /*!
       
   175     readData
       
   176 */
       
   177 qint64 NmViewerViewNetReply::readData(char *data, qint64 maxlen)
       
   178 {
       
   179     NM_FUNCTION;
       
   180     
       
   181     qint64 i = 0;
       
   182     const qint64 dataLength(mDataArray.length());
       
   183 
       
   184     if (0 >= dataLength || !data) {
       
   185         return -1;
       
   186     }
       
   187 
       
   188     for (i = 0; i < maxlen && mReadIndex < dataLength; i++, mReadIndex++) {
       
   189         data[i] = mDataArray.at(mReadIndex);
       
   190     }
       
   191     return i;
       
   192 }
       
   193 
       
   194 /*!
       
   195     readBufferSize. Returns the size of the read buffer, in bytes.
       
   196 */
       
   197 qint64 NmViewerViewNetReply::readBufferSize() const
       
   198 {
       
   199     NM_FUNCTION;
       
   200     
       
   201     return mDataArray.length();
       
   202 }
       
   203 
       
   204 /*!
       
   205     bytesAvailable. Returns the number of bytes that are available for reading.
       
   206     This function is commonly used with sequential devices to determine the number of bytes to
       
   207     allocate in a buffer before reading. Base implementation is called in order to
       
   208     include the size of QIODevices' buffer.
       
   209 */
       
   210 qint64 NmViewerViewNetReply::bytesAvailable() const
       
   211 {
       
   212     NM_FUNCTION;
       
   213     
       
   214     return mDataArray.length() + QIODevice::bytesAvailable();
       
   215 }
       
   216 
       
   217 /*!
       
   218     isSequential. Returns true if this device is sequential.
       
   219 */
       
   220 bool NmViewerViewNetReply::isSequential () const
       
   221 {
       
   222     NM_FUNCTION;
       
   223     
       
   224     return false;
       
   225 }
       
   226 
       
   227 /*!
       
   228     abort. Aborts the operation immediately and close down any
       
   229     network connections still open
       
   230 */
       
   231 void NmViewerViewNetReply::abort()
       
   232 {
       
   233     NM_FUNCTION;
       
   234 }
       
   235