activityfw/storage/s60/src/afentry.cpp
changeset 104 9b022b1f357c
equal deleted inserted replaced
103:b99b84bcd2d1 104:9b022b1f357c
       
     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 "afentry.h"
       
    18 #include <s32mem.h>
       
    19 
       
    20 // -----------------------------------------------------------------------------
       
    21 /**
       
    22  * Two-phase constructor. Create and initialize instance
       
    23  * @return entry instance
       
    24  */
       
    25 CAfEntry* CAfEntry::NewL()
       
    26 {
       
    27     CAfEntry *self = CAfEntry::NewLC();
       
    28     CleanupStack::Pop(self);
       
    29     return self;
       
    30 }
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 /**
       
    34  * Two-phase constructor. Create, initialize and push instance into cleanup stack
       
    35  * @return entry instance
       
    36  */
       
    37 CAfEntry* CAfEntry::NewLC()
       
    38 {
       
    39     CAfEntry *self = new (ELeave)CAfEntry();
       
    40     CleanupStack::PushL(self);
       
    41     return self;
       
    42 }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 /**
       
    46  * Two-phase constructor. Create and initialize instance
       
    47  * @param flags - entry flags
       
    48  * @param applicationId - application unique identifier
       
    49  * @param activityId - activity unique identifier
       
    50  * @param imgSrc - thumbanail source
       
    51  * @param privateData - privated application data
       
    52  * @param publicData - public activity data
       
    53  * @return entry instance
       
    54  */
       
    55 CAfEntry* CAfEntry::NewL(TInt flags,
       
    56                          TInt applicationId,
       
    57                          const TDesC &activityId,
       
    58                          const TDesC &imgSrc,
       
    59                          const TDesC8 &privateData,
       
    60                          const TDesC8 &publicData)
       
    61 {
       
    62     CAfEntry* self = CAfEntry::NewLC(flags, 
       
    63                                      applicationId, 
       
    64                                      activityId, 
       
    65                                      imgSrc, 
       
    66                                      privateData, 
       
    67                                      publicData);
       
    68     CleanupStack::Pop(self);
       
    69     return self;
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 /**
       
    74  * Two-phase constructor. Create, initialize and push instance into cleanup stack
       
    75  * @param flags - entry flags
       
    76  * @param applicationId - application unique identifier
       
    77  * @param activityId - activity unique identifier
       
    78  * @param imgSrc - thumbanail source
       
    79  * @param privateData - privated application data
       
    80  * @param publicData - public activity data
       
    81  * @return entry instance
       
    82  */
       
    83 CAfEntry* CAfEntry::NewLC(TInt flags,
       
    84                           TInt applicationId,
       
    85                           const TDesC &activityId,
       
    86                           const TDesC &imgSrc,
       
    87                           const TDesC8 &privateData,
       
    88                           const TDesC8 &publicData)
       
    89 {
       
    90     CAfEntry *self = CAfEntry::NewLC();
       
    91     self->ConstructL(flags, 
       
    92                      applicationId, 
       
    93                      activityId, 
       
    94                      imgSrc, 
       
    95                      privateData, 
       
    96                      publicData);
       
    97     return self;
       
    98 }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 CAfEntry* CAfEntry::NewLC(const RMessage2& msg, int offset)
       
   102 {
       
   103     CAfEntry* self = CAfEntry::NewLC();
       
   104     RBuf8 serializedEntry;
       
   105     CleanupClosePushL(serializedEntry);
       
   106     ReallocL(serializedEntry, msg.GetDesMaxLength(offset));
       
   107     msg.ReadL(offset, serializedEntry);
       
   108     RDesReadStream entryReader(serializedEntry);
       
   109     CleanupClosePushL(entryReader);
       
   110     entryReader >> (*self);
       
   111     CleanupStack::PopAndDestroy(&entryReader);
       
   112     CleanupStack::PopAndDestroy(&serializedEntry);
       
   113     return self;
       
   114 }
       
   115 // -----------------------------------------------------------------------------
       
   116 /**
       
   117  * First phase constructor
       
   118  */
       
   119 CAfEntry::CAfEntry()
       
   120 {
       
   121 }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 /**
       
   125  * Second phase constructor. Initialize instance
       
   126  * @param flags - entry flags
       
   127  * @param applicationId - application unique identifier
       
   128  * @param activityId - activity unique identifier
       
   129  * @param imgSrc - thumbanail source
       
   130  * @param privateData - privated application data
       
   131  * @param publicData - public activity data 
       
   132  */
       
   133 void CAfEntry::ConstructL(TInt flags,
       
   134                           TInt applicationId,
       
   135                           const TDesC &activityId,
       
   136                           const TDesC &imgSrc,
       
   137                           const TDesC8 &privateData,
       
   138                           const TDesC8 &publicData)
       
   139 {
       
   140     mFlags = flags;
       
   141     mAppId = applicationId;
       
   142     CopyL(mActivityId, activityId);
       
   143     CopyL(mImgSrc, imgSrc);
       
   144     CopyL(mPrivateData, privateData);
       
   145     CopyL(mPublicData, publicData);
       
   146 }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 /**
       
   150  * Destructor. Release allocated resources 
       
   151  */
       
   152 CAfEntry::~CAfEntry()
       
   153 {
       
   154     mActivityId.Close();
       
   155     mPrivateData.Close();
       
   156     mPublicData.Close();
       
   157     mImgSrc.Close();
       
   158 }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 /**
       
   162  * Provide size of serialized entry
       
   163  * @return size of serialized entry instance
       
   164  */
       
   165 TInt CAfEntry::Size() const
       
   166 {
       
   167     return (sizeof(TInt) * 3) + //flags + appId + actId size info 
       
   168            mActivityId.Size() + //actId content size
       
   169            DataSize(); //data size
       
   170            
       
   171 }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 /**
       
   175  * Provide size of serialized entry
       
   176  * @return size of serialized entry instance
       
   177  */
       
   178 TInt CAfEntry::DataSize() const
       
   179 {
       
   180     return (sizeof(TInt) * 3) + //privData size info + pubData size info + imgSrc size info
       
   181             mImgSrc.Size() + //imgSize content size
       
   182             mPrivateData.Size() + //privData content size
       
   183             mPublicData.Size(); //pubData content size 
       
   184 }
       
   185 // -----------------------------------------------------------------------------
       
   186 /**
       
   187  * Serialize entry content into output stream.
       
   188  * @param stream - output stream
       
   189  */
       
   190 void CAfEntry::ExternalizeL(RWriteStream &stream) const
       
   191 {
       
   192     stream.WriteInt32L(mFlags);
       
   193     stream.WriteInt32L(mAppId);
       
   194     ExternalizeL(stream, mActivityId);
       
   195     ExternalizeDataOnlyL(stream);
       
   196 }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 /**
       
   200  * Deserialize entry content from input stream
       
   201  * @param stream - input stream
       
   202  */
       
   203 void CAfEntry::InternalizeL(RReadStream &stream)
       
   204 {
       
   205     mFlags = stream.ReadInt32L();
       
   206     mAppId = stream.ReadInt32L();
       
   207     InternalizeL(mActivityId, stream);
       
   208     InternalizeDataOnlyL(stream);
       
   209 }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 /**
       
   213  * Serialize entry content into output stream.
       
   214  * @param stream - output stream
       
   215  */
       
   216 void CAfEntry::ExternalizeDataOnlyL(RWriteStream &stream) const
       
   217 {
       
   218     ExternalizeL(stream, mImgSrc);
       
   219     ExternalizeL(stream, mPrivateData);
       
   220     ExternalizeL(stream, mPublicData);
       
   221 }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 /**
       
   225  * Deserialize entry content from input stream
       
   226  * @param stream - input stream
       
   227  */
       
   228 void CAfEntry::InternalizeDataOnlyL(RReadStream &stream)
       
   229 {
       
   230     
       
   231     InternalizeL(mImgSrc, stream);
       
   232     InternalizeL(mPrivateData, stream);
       
   233     InternalizeL(mPublicData, stream);
       
   234     
       
   235 }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 /**
       
   239  * Provide access to activity flags
       
   240  * @return activity flags 
       
   241  */
       
   242 TInt CAfEntry::Flags() const
       
   243 {
       
   244     return mFlags;
       
   245 }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 /**
       
   249  * Provide access to activity identifier.
       
   250  * @return activity identifier
       
   251  */
       
   252 TInt CAfEntry::ApplicationId() const
       
   253 {
       
   254     return mAppId;
       
   255 }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 /**
       
   259  * Provide access to activity identifier.
       
   260  * @return activity identifier
       
   261  */
       
   262 const TDesC& CAfEntry::ActivityId() const
       
   263 {
       
   264     return mActivityId;
       
   265 }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 /**
       
   269  * Provide access to activity data.
       
   270  * @param rights - type of requested data
       
   271  * @return activity data
       
   272  */
       
   273 const TDesC8& CAfEntry::Data(CAfEntry::AccessRights rights) const
       
   274 {
       
   275     return Private == rights ? mPrivateData : mPublicData;
       
   276 }
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 /**
       
   280  * Provide access to activity data.
       
   281  * @param rights - type of requested data
       
   282  * @return activity data
       
   283  */
       
   284 void CAfEntry::SetDataL(const TDesC8& src, CAfEntry::AccessRights rights)
       
   285 {
       
   286     CopyL(Private == rights ? mPrivateData : mPublicData, src);
       
   287 }
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 /**
       
   291  * Provide access to activity thumbail path
       
   292  * @return path to activity thumbnail 
       
   293  */
       
   294 const TDesC& CAfEntry::ImageSrc() const
       
   295 {
       
   296     return mImgSrc;
       
   297 }
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 /**
       
   301  * Set new value of image source
       
   302  */
       
   303 void CAfEntry::SetImageSrcL(const TDesC& src)
       
   304 {
       
   305     CopyL(mImgSrc, src);
       
   306 }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 /**
       
   310  * Reallocate blob buffer to requested size
       
   311  * @param dst - destination buffer
       
   312  * @param length - requested length
       
   313  */
       
   314 void CAfEntry::ReallocL(RBuf8 &dst,TInt length)
       
   315 {
       
   316     if (0 < length) {
       
   317         if (dst.MaxLength() < length) {
       
   318             dst.ReAllocL(length);
       
   319         }
       
   320     } else {
       
   321         dst.Close();
       
   322     }
       
   323 }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 /**
       
   327  * Reallocate text buffer to requested size
       
   328  * @param dst - destination buffer
       
   329  * @param length - requested length
       
   330  */
       
   331 void CAfEntry::ReallocL(RBuf &dst,TInt length)
       
   332 {
       
   333     if (0 < length) {
       
   334         if (dst.MaxLength() < length) {
       
   335             dst.ReAllocL(length);
       
   336         }
       
   337     } else {
       
   338         dst.Close();
       
   339     }
       
   340 }
       
   341 // -----------------------------------------------------------------------------
       
   342 /**
       
   343  * Copy blob content from input stream
       
   344  * @param dst - destination buffer
       
   345  * @param src - source buffer
       
   346  */
       
   347 void CAfEntry::CopyL(RBuf8 &dst,const TDesC8 &src)
       
   348 {
       
   349     ReallocL(dst, src.Length());
       
   350     if(0 < src.Length()) {
       
   351         dst.Copy(src);
       
   352     }
       
   353 }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 /**
       
   357  * Copy text content from input buffer
       
   358  * @param dst - destination buffer
       
   359  * @param src - source buffer
       
   360  */
       
   361 void CAfEntry::CopyL(RBuf &dst,const TDesC &src)
       
   362 {
       
   363     ReallocL(dst, src.Length());
       
   364     if(0 < src.Length()) {
       
   365         dst.Copy(src);
       
   366     }
       
   367 }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 /**
       
   371  * Deserialize blob content from input stream
       
   372  * @param dst - destination buffer
       
   373  * @param src - input stream
       
   374  */
       
   375 void CAfEntry::InternalizeL(RBuf8 &dst, RReadStream &src)
       
   376 {
       
   377     const TInt length(src.ReadInt32L());
       
   378     ReallocL(dst, length);
       
   379     if (0 < length) {
       
   380         src.ReadL(dst, length);
       
   381     }
       
   382 }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 /**
       
   386  * Deserialize text content from input stream
       
   387  * @param dst - destination buffer
       
   388  * @param src - input stream
       
   389  */
       
   390 void CAfEntry::InternalizeL(RBuf &dst, RReadStream &src)
       
   391 {
       
   392     const TInt length(src.ReadInt32L());
       
   393     ReallocL(dst, length);
       
   394     if (0 < length) {
       
   395         src.ReadL(dst, length);
       
   396     }
       
   397 }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 /**
       
   401  * Serialize blob content into output stream
       
   402  * @param dst - destination stream
       
   403  * @param src - input buffer
       
   404  */
       
   405 void CAfEntry::ExternalizeL(RWriteStream &dst,const TDesC8 & src)
       
   406 {
       
   407     dst.WriteInt32L(src.Length());
       
   408     if (src.Length()) {
       
   409         dst.WriteL(src, src.Length());
       
   410     }
       
   411 }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 /**
       
   415  * Serialize text content into output stream
       
   416  * @param dst - destination stream
       
   417  * @param src - input buffer
       
   418  */
       
   419 void CAfEntry::ExternalizeL(RWriteStream &dst,const TDesC& src)
       
   420 {
       
   421     dst.WriteInt32L(src.Length());
       
   422     if (src.Length()) {
       
   423         dst.WriteL(src, src.Length());
       
   424     }
       
   425 }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 RPointerArray<CAfEntry>& operator <<(RPointerArray<CAfEntry>& dst, const TDesC8 &src)
       
   429 {
       
   430     dst.ResetAndDestroy();
       
   431     RDesReadStream srcStream(src);
       
   432     CleanupClosePushL(srcStream);
       
   433     int numOfItems(srcStream.ReadInt32L());
       
   434     for (int i(0); i < numOfItems; ++i) {
       
   435         CAfEntry *entry = CAfEntry::NewLC();
       
   436         srcStream >> (*entry);
       
   437         dst.AppendL(entry);
       
   438         CleanupStack::Pop(entry);
       
   439     }
       
   440     CleanupStack::PopAndDestroy(&srcStream);
       
   441     return dst;
       
   442 }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 RBuf8& operator <<(RBuf8 &dst, const RPointerArray<CAfEntry>& src)
       
   446 {
       
   447     int iter(0), 
       
   448         requiredSize(sizeof(int));
       
   449     for(iter =0; iter< src.Count(); ++iter) {
       
   450         requiredSize += src[iter]->Size();
       
   451     }
       
   452     CAfEntry::ReallocL(dst, requiredSize);
       
   453     RDesWriteStream dstStream(dst);
       
   454     CleanupClosePushL(dstStream);
       
   455     dstStream.WriteInt32L(src.Count());
       
   456     for (iter =0; iter < src.Count(); ++iter) {
       
   457         dstStream << *(src[iter]);
       
   458     }
       
   459     CleanupStack::PopAndDestroy(&dstStream);
       
   460     return dst;
       
   461 }