taskswitcher/server/src/tsserializeddataprovider.cpp
changeset 116 305818acdca4
equal deleted inserted replaced
112:dbfb5e38438b 116:305818acdca4
       
     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 #include <s32mem.h>
       
    18 #include "tsserializeddataprovider.h"
       
    19 #include "tsmodel.h"
       
    20 // -----------------------------------------------------------------------------
       
    21 /**
       
    22  * Leaving constructor. 
       
    23  * @param observer - data observer
       
    24  */
       
    25 CTsSerializedDataProvider* CTsSerializedDataProvider::NewL(MTsDataObserver &observer)
       
    26 {
       
    27     return new (ELeave) CTsSerializedDataProvider(observer);
       
    28     
       
    29 }
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 /**
       
    33  * Constructor
       
    34  * @param observer - data observer
       
    35  */
       
    36 CTsSerializedDataProvider::CTsSerializedDataProvider(MTsDataObserver& observer)
       
    37 :
       
    38     iObserver(observer)
       
    39 {}
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 /**
       
    43  * Destructor
       
    44  */
       
    45 CTsSerializedDataProvider::~CTsSerializedDataProvider()
       
    46 {
       
    47     iData.Close();
       
    48 }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 /**
       
    52  * Interface implementation.
       
    53  * @see MTsDataProvider::Data()
       
    54  */
       
    55 const TDesC8& CTsSerializedDataProvider::Data() const
       
    56 {
       
    57     return iData;
       
    58 }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 /**
       
    62  * Interface implementation.
       
    63  * @see MTsModelObserver::dataChanged(MTsModel &)
       
    64  */
       
    65 void CTsSerializedDataProvider::DataChanged(MTsModel &model)
       
    66 {
       
    67     serializeModel(model);
       
    68 }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 /**
       
    72  * Serialize source model into internal data storage
       
    73  * @param src - model
       
    74  */
       
    75 void CTsSerializedDataProvider::serializeModel(const MTsModel &src)
       
    76 {
       
    77     TRAPD(errNo, serializeModelL(src));
       
    78     if(KErrNone == errNo ) {
       
    79         iObserver.DataChanged();
       
    80     }
       
    81 }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 /**
       
    85  * Serialize source model into internal data storage. Function can leave.
       
    86  * @param src - source model
       
    87  */
       
    88 void CTsSerializedDataProvider::serializeModelL(const MTsModel &src)
       
    89 {
       
    90     const TInt KExpandSize = 256;
       
    91     CBufFlat* buf = CBufFlat::NewL(KExpandSize);
       
    92     CleanupStack::PushL(buf);
       
    93     RBufWriteStream stream(*buf); 
       
    94     CleanupClosePushL(stream);
       
    95     
       
    96     serializeModelL(stream, src);
       
    97     
       
    98     CleanupStack::PopAndDestroy(&stream);
       
    99     iData.Close();
       
   100     iData.CreateL(buf->Size());
       
   101     buf->Read(0, iData, buf->Size());
       
   102     CleanupStack::PopAndDestroy(buf);
       
   103 }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 /**
       
   107  * Serialize source model into destination stream. Function can leave.
       
   108  * @param dst - destination stream
       
   109  * @param src - source model
       
   110  */
       
   111 void CTsSerializedDataProvider::serializeModelL(RWriteStream& dst, const MTsModel &src)
       
   112 {
       
   113     dst.WriteInt32L( src.Count() );
       
   114     for (TInt iter(0); iter < src.Count(); ++iter) {
       
   115         dst << src.ItemL( iter );
       
   116     }
       
   117 }