webengine/osswebengine/WebCore/platform/network/symbian/PostDataItem.cpp
changeset 0 dd21522fd290
child 10 a359256acfc6
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:  Implementation of PostDataItem & FileDataItem
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "PostDataItem.h"
       
    21 #include "FormData.h"
       
    22 #include "StaticObjectsContainer.h"
       
    23 
       
    24 using namespace WebCore;
       
    25 
       
    26 // EXTERNAL DATA STRUCTURES
       
    27 
       
    28 // EXTERNAL FUNCTION PROTOTYPES
       
    29 
       
    30 // CONSTANTS
       
    31 
       
    32 // MACROS
       
    33 
       
    34 // LOCAL CONSTANTS AND MACROS
       
    35 
       
    36 // MODULE DATA STRUCTURES
       
    37 
       
    38 // LOCAL FUNCTION PROTOTYPES
       
    39 
       
    40 // FORWARD DECLARATIONS
       
    41 
       
    42 // ============================= LOCAL FUNCTIONS ===============================
       
    43 
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // class members initialization
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void PostDataItem::initL(const FormDataElement* formDataElement)
       
    52 {
       
    53     m_dataOffset = 0;
       
    54     TPtrC8 ptr((TText8*)formDataElement->m_data.data(), formDataElement->m_data.size());
       
    55     m_data = ptr.AllocL();
       
    56 }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // destructor
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 PostDataItem::~PostDataItem()
       
    63 {
       
    64     delete m_data;
       
    65 }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // Retuns data contained by the post data itemaSizeOfDataToSupply , size of the data
       
    69 // to be supplied if the aSizeOfDataToSupply is not supplied , then the content till
       
    70 // the end of the buffer is returned
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 HBufC8* PostDataItem::dataL(int sizeToSupply )
       
    74 {
       
    75     HBufC8* dataToSupply = NULL;
       
    76     int sizeOfDataToSupply = sizeToSupply;
       
    77     // if sizeOfDataToSupply exceeds the size of data supplied then set the sizeOfDataToSupply
       
    78     // to the content size
       
    79     if(sizeOfDataToSupply == -1 || sizeOfDataToSupply >= (m_data->Length() - m_dataOffset)) {
       
    80         sizeOfDataToSupply = m_data->Length()- m_dataOffset;
       
    81     }
       
    82     TPtrC8 dataSupplyDes(m_data->Ptr() + m_dataOffset, sizeOfDataToSupply);
       
    83     dataToSupply = dataSupplyDes.AllocL();
       
    84     // add supplied size count to the data offset
       
    85     m_dataOffset += sizeOfDataToSupply;
       
    86     return  dataToSupply;
       
    87 }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // resets the state of the object
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 void PostDataItem::reset()
       
    94 {
       
    95     m_dataOffset = 0;
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // returns the pending size of the content to be posted
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 int PostDataItem::pendingContentSize() const
       
   103 {
       
   104     return m_data->Length() - m_dataOffset;
       
   105 }
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // destructor
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 FileDataItem::~FileDataItem()
       
   113 {
       
   114     if(m_fileLocked) {
       
   115         m_file.UnLock(0, m_fileSize);
       
   116     }
       
   117     m_file.Close();
       
   118 }
       
   119 
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // class members initialization
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void FileDataItem::initL(const FormDataElement* formDataElement)
       
   126 {
       
   127     m_dataOffset = 0;
       
   128     m_fileLocked = false;
       
   129     m_data = NULL;
       
   130     RFs rfs = StaticObjectsContainer::instance()->fsSession();
       
   131     HBufC* fileName = formDataElement->m_filename.des().AllocLC();
       
   132     // size of the file
       
   133     User::LeaveIfError(m_file.Open(rfs, fileName->Des(), EFileRead | EFileShareReadersOnly));
       
   134     User::LeaveIfError(m_file.Size(m_fileSize));
       
   135     User::LeaveIfError(m_file.Lock(0, m_fileSize));
       
   136     m_fileLocked = ETrue;
       
   137     CleanupStack::PopAndDestroy();// fileName
       
   138 }
       
   139 
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // Retuns data contained by the post data itemaSizeOfDataToSupply , size of the data
       
   143 // to be supplied if the sizeToSupply is not supplied , then the content till
       
   144 // the end of the buffer is returned
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 HBufC8* FileDataItem::dataL(int sizeToSupply )
       
   148 {
       
   149     HBufC8* dataToSupply = NULL;
       
   150     int sizeOfDataToSupply = sizeToSupply;
       
   151     
       
   152     // if the sizeOfDataToSupply is not set then read from the offset to the end of the file
       
   153     // if the sizeOfDataToSupply  is greater than the size of the pending file content
       
   154     // then return content of size sizeOfDataToSupply
       
   155     if(sizeOfDataToSupply == -1 || sizeOfDataToSupply >= (m_fileSize - m_dataOffset)) {
       
   156         sizeOfDataToSupply = m_fileSize - m_dataOffset;
       
   157     }
       
   158     dataToSupply = HBufC8::NewLC(sizeOfDataToSupply);
       
   159     TPtr8 dataToSupplyDes = dataToSupply->Des();
       
   160     // read from the offset to the size of the data to be supplied
       
   161     User::LeaveIfError(m_file.Read(m_dataOffset, dataToSupplyDes, sizeOfDataToSupply));
       
   162     // add the read byte size count to the data offset
       
   163     m_dataOffset += sizeOfDataToSupply;
       
   164     CleanupStack::Pop();//dataToSupply
       
   165     return  dataToSupply;
       
   166 }
       
   167 
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // returns the pending size of the content to be posted
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 int FileDataItem::pendingContentSize() const
       
   174 {
       
   175     return m_fileSize - m_dataOffset;
       
   176 }
       
   177