emailuis/nmailui/src/nmviewerviewnetreply.cpp
changeset 43 99bcbff212ad
parent 20 ecc8def7944a
child 47 f83bd4ae1fe3
child 54 997a02608b3a
equal deleted inserted replaced
42:139d4b7b2938 43:99bcbff212ad
    26 */
    26 */
    27 
    27 
    28 /*!
    28 /*!
    29     Constructor
    29     Constructor
    30 */
    30 */
    31 NmViewerViewNetReply::NmViewerViewNetReply(QVariant data)
    31 NmViewerViewNetReply::NmViewerViewNetReply(QVariant data, NmUiEngine &uiEngine)
    32     : QNetworkReply(),
    32     : QNetworkReply(),
    33       mDataArray(data.toByteArray()),
    33       mDataArray(data.toByteArray()),
       
    34       mUiEngine(uiEngine),
       
    35       mOperation(NULL),
    34       mReadIndex(0)
    36       mReadIndex(0)
    35 {
    37 {
       
    38     NM_FUNCTION;
       
    39     
    36     open(ReadWrite);
    40     open(ReadWrite);
    37     setReadBufferSize(mDataArray.length());
    41     setReadBufferSize(mDataArray.length());
    38     // QNetworkAccessManager starts listening the signals only
    42     // QNetworkAccessManager starts listening the signals only
    39     // after the construction, so we cannot signal reply to be
    43     // after the construction, so we cannot signal reply to be
    40     // ready instantly. We need to emit the signals after the
    44     // ready instantly. We need to emit the signals after the
    41     // construction.
    45     // construction.
    42     QTimer::singleShot(100, this, SLOT(signalReady()));
    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     }
    43 }
    77 }
    44 
    78 
    45 /*!
    79 /*!
    46     Destructor
    80     Destructor
    47 */
    81 */
    48 NmViewerViewNetReply::~NmViewerViewNetReply()
    82 NmViewerViewNetReply::~NmViewerViewNetReply()
    49 {
    83 {
       
    84     NM_FUNCTION;
    50 }
    85 }
    51 
    86 
    52 /*!
    87 /*!
    53     signalReady
    88     signalReady
    54 */
    89 */
    55 void NmViewerViewNetReply::signalReady()
    90 void NmViewerViewNetReply::signalReady()
    56 {
    91 {
       
    92     NM_FUNCTION;
       
    93     
    57     // Insert embedded images into cache manually
    94     // Insert embedded images into cache manually
    58     if(manager()) {
    95     if(manager()) {
    59         if(manager()->cache() && request().url().scheme() == NMUI_NET_REPLY_CONTENT_ID) {
    96         if(manager()->cache() && request().url().scheme() == NMUI_NET_REPLY_CONTENT_ID) {
    60             // Metadata required for inserted data
    97             // Metadata required for inserted data
    61             QNetworkCacheMetaData metaData;
    98             QNetworkCacheMetaData metaData;
    74     emit readyRead();
   111     emit readyRead();
    75     emit finished();
   112     emit finished();
    76 }
   113 }
    77 
   114 
    78 /*!
   115 /*!
       
   116     Slot. Called when fetch operation completes
       
   117 */
       
   118 void NmViewerViewNetReply::fetchCompleted(int result)
       
   119 {
       
   120     NM_FUNCTION;
       
   121     
       
   122     Q_UNUSED(result);
       
   123     NmMessage *message = NULL;
       
   124     message = mUiEngine.message(
       
   125             mMailboxId,
       
   126             mFolderId,
       
   127             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             }
       
   136         }
       
   137         if (part) {
       
   138             int error = mUiEngine.contentToMessagePart(mMailboxId, mFolderId, mMessageId, *part);
       
   139             if (error == NmNoError) {
       
   140                 mDataArray = part->binaryContent();
       
   141             }
       
   142         }
       
   143     }
       
   144     delete message;
       
   145     message = NULL;
       
   146     open(ReadWrite);
       
   147     setReadBufferSize(mDataArray.length());
       
   148     QMetaObject::invokeMethod(this, "signalReady", Qt::QueuedConnection);
       
   149 }
       
   150 
       
   151 /*!
       
   152     Slot. Called if fetch operation is cancelled
       
   153 */
       
   154 void NmViewerViewNetReply::fetchCancelled()
       
   155 {
       
   156     NM_FUNCTION;
       
   157     
       
   158     // just call fetch completed
       
   159     fetchCompleted(NmCancelError);
       
   160 }
       
   161 
       
   162 /*!
    79     setOriginalRequest. This function is created to provide access to call
   163     setOriginalRequest. This function is created to provide access to call
    80     base class' protected function setRequest
   164     base class' protected function setRequest
    81 */
   165 */
    82 void NmViewerViewNetReply::setOriginalRequest(const QNetworkRequest &request)
   166 void NmViewerViewNetReply::setOriginalRequest(const QNetworkRequest &request)
    83 {
   167 {
       
   168     NM_FUNCTION;
       
   169     
    84     setRequest(request);
   170     setRequest(request);
    85 }
   171 }
    86 
   172 
    87 /*!
   173 /*!
    88     readData
   174     readData
    89 */
   175 */
    90 qint64 NmViewerViewNetReply::readData(char *data, qint64 maxlen)
   176 qint64 NmViewerViewNetReply::readData(char *data, qint64 maxlen)
    91 {
   177 {
       
   178     NM_FUNCTION;
       
   179     
    92     qint64 i = 0;
   180     qint64 i = 0;
    93     const qint64 dataLength(mDataArray.length());
   181     const qint64 dataLength(mDataArray.length());
    94 
   182 
    95     if (0 >= dataLength || !data) {
   183     if (0 >= dataLength || !data) {
    96         return -1;
   184         return -1;
   105 /*!
   193 /*!
   106     readBufferSize. Returns the size of the read buffer, in bytes.
   194     readBufferSize. Returns the size of the read buffer, in bytes.
   107 */
   195 */
   108 qint64 NmViewerViewNetReply::readBufferSize() const
   196 qint64 NmViewerViewNetReply::readBufferSize() const
   109 {
   197 {
       
   198     NM_FUNCTION;
       
   199     
   110     return mDataArray.length();
   200     return mDataArray.length();
   111 }
   201 }
   112 
   202 
   113 /*!
   203 /*!
   114     bytesAvailable. Returns the number of bytes that are available for reading.
   204     bytesAvailable. Returns the number of bytes that are available for reading.
   116     allocate in a buffer before reading. Base implementation is called in order to
   206     allocate in a buffer before reading. Base implementation is called in order to
   117     include the size of QIODevices' buffer.
   207     include the size of QIODevices' buffer.
   118 */
   208 */
   119 qint64 NmViewerViewNetReply::bytesAvailable() const
   209 qint64 NmViewerViewNetReply::bytesAvailable() const
   120 {
   210 {
       
   211     NM_FUNCTION;
       
   212     
   121     return mDataArray.length() + QIODevice::bytesAvailable();
   213     return mDataArray.length() + QIODevice::bytesAvailable();
   122 }
   214 }
   123 
   215 
   124 /*!
   216 /*!
   125     isSequential. Returns true if this device is sequential.
   217     isSequential. Returns true if this device is sequential.
   126 */
   218 */
   127 bool NmViewerViewNetReply::isSequential () const
   219 bool NmViewerViewNetReply::isSequential () const
   128 {
   220 {
       
   221     NM_FUNCTION;
       
   222     
   129     return false;
   223     return false;
   130 }
   224 }
   131 
   225 
   132 /*!
   226 /*!
   133     abort. Aborts the operation immediately and close down any
   227     abort. Aborts the operation immediately and close down any
   134     network connections still open
   228     network connections still open
   135 */
   229 */
   136 void NmViewerViewNetReply::abort()
   230 void NmViewerViewNetReply::abort()
   137 {
   231 {
   138 }
   232     NM_FUNCTION;
   139 
   233 }
       
   234