pimappservices/calendar/shared/src/agmcalendarinfo.cpp
changeset 0 f979ecb2b13e
child 30 d68a4b5d5885
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <s32strm.h>
       
    17 #include <s32buf.h>
       
    18 #include <s32file.h>
       
    19 #include <badesca.h>
       
    20 
       
    21 #include "agmcalendarinfo.h"
       
    22 #include "agmcalendarinfoproperty.h"
       
    23 #include "calsessionimpl.h"
       
    24 
       
    25 EXPORT_C CAgnCalendarInfo* CAgnCalendarInfo::NewL() 
       
    26     {
       
    27     CAgnCalendarInfo* self = new(ELeave) CAgnCalendarInfo();
       
    28     CleanupStack::PushL(self);
       
    29     self->ConstructL();
       
    30     CleanupStack::Pop(self);
       
    31     return self;
       
    32     }
       
    33 
       
    34 EXPORT_C CAgnCalendarInfo::CAgnCalendarInfo()
       
    35     :iLinearOrder(CAgnCalendarInfoProperty::Compare)
       
    36     {
       
    37     }
       
    38 
       
    39 EXPORT_C void CAgnCalendarInfo::ConstructL()
       
    40     {
       
    41     iFileName = KNullDesC().AllocL();
       
    42     iName = KNullDesC().AllocL();
       
    43     iDescription = KNullDesC().AllocL();
       
    44     }
       
    45 
       
    46 EXPORT_C CAgnCalendarInfo::~CAgnCalendarInfo()   
       
    47     {
       
    48     delete iFileName;
       
    49     delete iName;
       
    50     delete iDescription;
       
    51     
       
    52     iProperties.ResetAndDestroy();
       
    53     }
       
    54 
       
    55 EXPORT_C void CAgnCalendarInfo::SetIsValid(TBool aIsValid)
       
    56     {
       
    57     iIsValid = aIsValid;
       
    58     }
       
    59 
       
    60 EXPORT_C TBool CAgnCalendarInfo::IsValid() const
       
    61     {
       
    62     return iIsValid;
       
    63     }
       
    64 
       
    65 EXPORT_C void CAgnCalendarInfo::SetFileNameL(const TDesC& aFileName)
       
    66     {
       
    67     HBufC* tempFileName = aFileName.AllocL();
       
    68     delete iFileName;
       
    69     iFileName = tempFileName;
       
    70     }
       
    71 
       
    72 EXPORT_C const TDesC& CAgnCalendarInfo::FileNameL() const
       
    73     {
       
    74     return *iFileName;
       
    75     }
       
    76 
       
    77 EXPORT_C void CAgnCalendarInfo::SetNameL(const TDesC& aName)
       
    78     {
       
    79     HBufC* tempName = aName.AllocL();
       
    80     delete iName;
       
    81     iName = tempName;
       
    82     }
       
    83 
       
    84 EXPORT_C const TDesC& CAgnCalendarInfo::NameL() const 
       
    85     {
       
    86     return *iName;
       
    87     }
       
    88 
       
    89 EXPORT_C void CAgnCalendarInfo::SetDescriptionL(const TDesC& aDescription)
       
    90     {
       
    91     HBufC* tempDescription = aDescription.AllocL();
       
    92     delete iDescription;
       
    93     iDescription = tempDescription;
       
    94     }
       
    95 
       
    96 EXPORT_C const TDesC& CAgnCalendarInfo::DescriptionL() const 
       
    97     {
       
    98     return *iDescription;
       
    99     }
       
   100 
       
   101 EXPORT_C void CAgnCalendarInfo::SetColor(TRgb aColor)  
       
   102     {
       
   103     iColor = aColor;
       
   104     }
       
   105 
       
   106 EXPORT_C TRgb CAgnCalendarInfo::Color() const   
       
   107     {
       
   108     return iColor;
       
   109     }
       
   110 
       
   111 EXPORT_C void CAgnCalendarInfo::SetEnabled(TBool aEnabled) 
       
   112     {
       
   113     iEnabled = aEnabled;
       
   114     }
       
   115 
       
   116 EXPORT_C TBool CAgnCalendarInfo::Enabled() const
       
   117     {
       
   118     return iEnabled;
       
   119     }
       
   120 
       
   121 EXPORT_C CDesC8Array* CAgnCalendarInfo::PropertyKeysL() const
       
   122     {
       
   123     const TInt KPropertyCount(iProperties.Count());
       
   124     
       
   125     CDesC8Array* propertyKeys = new(ELeave) CDesC8ArrayFlat(KPropertyCount);
       
   126     CleanupStack::PushL(propertyKeys);
       
   127     
       
   128     for (TInt i(0) ; i < KPropertyCount ; ++i)
       
   129         {
       
   130         if (iProperties[i]->State() != CAgnCalendarInfoProperty::EDeleted)
       
   131             {
       
   132             propertyKeys->AppendL(iProperties[i]->Key());
       
   133             }
       
   134         }
       
   135     
       
   136     CleanupStack::Pop(propertyKeys);
       
   137     return propertyKeys;
       
   138     }
       
   139 
       
   140 EXPORT_C const TDesC8& CAgnCalendarInfo::PropertyValueL(const TDesC8& /*aKey*/) const
       
   141     {
       
   142     // This is a virtual function overridden in CCalCalendarInfoImpl
       
   143     // This should never be called
       
   144     __ASSERT_DEBUG(EFalse, User::Invariant());
       
   145     
       
   146     return KNullDesC8;
       
   147     }
       
   148 
       
   149 // Adds or updates a property by key and value
       
   150 EXPORT_C void CAgnCalendarInfo::SetPropertyL(const TDesC8& aKey, const TDesC8& aValue)
       
   151     {
       
   152     if (aKey.Compare(KNullDesC8) == 0
       
   153             || aValue.Compare(KNullDesC8) == 0)
       
   154         {
       
   155         // Either the key ot the value is NULL so leave
       
   156         User::Leave(KErrArgument);
       
   157         }
       
   158     
       
   159     TInt position = iProperties.FindInOrder(aKey, CAgnCalendarInfoProperty::Compare);
       
   160     
       
   161     if (position != KErrNotFound)
       
   162         {
       
   163         // This property already exists so set the value
       
   164         
       
   165         iProperties[position]->SetValue(aValue.AllocL());
       
   166         }
       
   167     else
       
   168         {
       
   169         // This property does not exist yet so insert it in key order
       
   170         
       
   171         CAgnCalendarInfoProperty* property = CAgnCalendarInfoProperty::NewLC(aKey);
       
   172         
       
   173         property->SetValue(aValue.AllocL());
       
   174         
       
   175         iProperties.InsertInOrderL(property, iLinearOrder);
       
   176         
       
   177         CleanupStack::Pop(property);
       
   178         }
       
   179     }
       
   180 
       
   181 EXPORT_C void CAgnCalendarInfo::RemovePropertyL(const TDesC8& aKey)
       
   182     {
       
   183     TInt position = iProperties.FindInOrder(aKey, CAgnCalendarInfoProperty::Compare);
       
   184     
       
   185     if (position != KErrNotFound)
       
   186         {
       
   187         iProperties[position]->SetState(CAgnCalendarInfoProperty::EDeleted);
       
   188         }
       
   189     }
       
   190 
       
   191 EXPORT_C void CAgnCalendarInfo::FileInternalizeL(RReadStream& aReadStream)
       
   192     {
       
   193     delete iName;
       
   194     delete iDescription;
       
   195     iName = NULL;
       
   196     iDescription = NULL;
       
   197     
       
   198     iName = HBufC::NewL(aReadStream, KMaxTInt);
       
   199     iDescription = HBufC::NewL(aReadStream, KMaxTInt);
       
   200     
       
   201     iColor = TRgb(aReadStream.ReadUint32L());
       
   202     iEnabled = aReadStream.ReadInt8L();
       
   203     
       
   204     // Remove any existing properties
       
   205     iProperties.ResetAndDestroy();
       
   206     
       
   207     const TInt KNewPropertyCount(aReadStream.ReadUint32L());
       
   208     
       
   209     for (TInt i(0) ; i < KNewPropertyCount ; ++i)
       
   210         {
       
   211         HBufC8* key = HBufC8::NewLC(aReadStream, KMaxTInt);
       
   212         CAgnCalendarInfoProperty* property = CAgnCalendarInfoProperty::NewLC(*key);
       
   213         
       
   214         TStreamId streamId;
       
   215         aReadStream >> streamId;
       
   216         property->SetStreamId(streamId);
       
   217         
       
   218         iProperties.InsertInOrderL(property, iLinearOrder);
       
   219         CleanupStack::Pop(property);
       
   220         CleanupStack::PopAndDestroy(key);
       
   221         }
       
   222     }
       
   223 
       
   224 EXPORT_C void CAgnCalendarInfo::FileExternalizeL(RWriteStream& aWriteStream) const
       
   225     {
       
   226     aWriteStream << *iName;
       
   227     aWriteStream << *iDescription;
       
   228     aWriteStream.WriteUint32L(iColor.Value());
       
   229     aWriteStream.WriteInt8L(iEnabled);
       
   230     
       
   231     const TInt KPropertyCount(iProperties.Count());
       
   232     
       
   233     TStreamPos streamCountPos = aWriteStream.Sink()->TellL(MStreamBuf::EWrite);
       
   234     
       
   235     aWriteStream.WriteUint32L(KPropertyCount);
       
   236     
       
   237     TInt actualPropertyCount(0);
       
   238     
       
   239     for (TInt i(0) ; i < KPropertyCount ; ++i)
       
   240         {
       
   241         if (iProperties[i]->State() != CAgnCalendarInfoProperty::EDeleted)
       
   242             {
       
   243             aWriteStream << iProperties[i]->Key();
       
   244             aWriteStream << iProperties[i]->StreamId();
       
   245             
       
   246             ++actualPropertyCount;
       
   247             }
       
   248         }
       
   249     
       
   250     if (actualPropertyCount != KPropertyCount)
       
   251         {
       
   252         // We have deleted some properties so write the actual count
       
   253         
       
   254         aWriteStream.Sink()->SeekL(MStreamBuf::EWrite, streamCountPos);
       
   255         aWriteStream.WriteUint32L(actualPropertyCount);
       
   256         aWriteStream.Sink()->SeekL(MStreamBuf::EWrite, EStreamEnd);
       
   257         }
       
   258     }
       
   259 
       
   260 EXPORT_C void CAgnCalendarInfo::IpcInternalizeL(RReadStream& aReadStream)
       
   261     {
       
   262     delete iFileName;
       
   263     delete iName;
       
   264     delete iDescription;
       
   265     iFileName = NULL;
       
   266     iName = NULL;
       
   267     iDescription = NULL;
       
   268     
       
   269     iIsValid = aReadStream.ReadInt8L();
       
   270     iFileName = HBufC::NewL(aReadStream, KMaxTInt);
       
   271     iName = HBufC::NewL(aReadStream, KMaxTInt);
       
   272     iDescription = HBufC::NewL(aReadStream, KMaxTInt);
       
   273     iColor = TRgb(aReadStream.ReadUint32L());
       
   274     iEnabled = aReadStream.ReadInt8L();
       
   275     
       
   276     // Remove any existing properties
       
   277     iProperties.ResetAndDestroy();
       
   278     
       
   279     const TInt KNewPropertyCount(aReadStream.ReadUint32L());
       
   280     
       
   281     for (TInt i(0) ; i < KNewPropertyCount ; ++i)
       
   282         {
       
   283         HBufC8* key = HBufC8::NewLC(aReadStream, KMaxTInt);
       
   284         CAgnCalendarInfoProperty* property = CAgnCalendarInfoProperty::NewLC(*key);
       
   285         
       
   286         TStreamId streamId;
       
   287         aReadStream >> streamId;
       
   288         property->SetStreamId(streamId);
       
   289         
       
   290         property->SetState(static_cast<CAgnCalendarInfoProperty::TState>(aReadStream.ReadUint8L()));
       
   291         
       
   292         iProperties.InsertInOrderL(property, iLinearOrder);
       
   293         CleanupStack::Pop(property);
       
   294         CleanupStack::PopAndDestroy(key);
       
   295         
       
   296         if (aReadStream.ReadUint8L())
       
   297             {
       
   298             // There was a value, so read it in
       
   299             HBufC8* value = HBufC8::NewL(aReadStream, KMaxTInt);
       
   300             property->SetValue(value);
       
   301             }
       
   302         }
       
   303     }
       
   304 
       
   305 EXPORT_C void CAgnCalendarInfo::IpcExternalizeL(RWriteStream& aWriteStream) const
       
   306     {
       
   307     aWriteStream.WriteInt8L(iIsValid);
       
   308     aWriteStream << *iFileName;
       
   309     aWriteStream << *iName;
       
   310     aWriteStream << *iDescription;
       
   311     aWriteStream.WriteUint32L(iColor.Value());
       
   312     aWriteStream.WriteInt8L(iEnabled);
       
   313     
       
   314     const TInt KPropertyCount(iProperties.Count());
       
   315     
       
   316     aWriteStream.WriteUint32L(KPropertyCount);
       
   317     
       
   318     for (TInt i(0) ; i < KPropertyCount ; ++i)
       
   319         {
       
   320         aWriteStream << iProperties[i]->Key();
       
   321         aWriteStream << iProperties[i]->StreamId();
       
   322         aWriteStream.WriteUint8L(iProperties[i]->State());
       
   323         
       
   324         if (iProperties[i]->Value().Compare(KNullDesC8) == 0)
       
   325             {
       
   326             // This is empty so don't write the value
       
   327             aWriteStream.WriteUint8L(EFalse);
       
   328             }
       
   329         else
       
   330             {
       
   331             aWriteStream.WriteUint8L(ETrue);
       
   332             aWriteStream << iProperties[i]->Value();
       
   333             }
       
   334         }
       
   335     }
       
   336 
       
   337 EXPORT_C void CAgnCalendarInfo::ExternalizePropertiesL(CFileStore& iFileStore, const CAgnCalendarInfo* aOldCalendarInfo) const
       
   338     {
       
   339     if (aOldCalendarInfo)
       
   340         {
       
   341         // We are updating the metadata so we should first delete any of the
       
   342         // old properties that cannot be found in the new metadata
       
   343     
       
   344         const TInt KOldPropertyCount(aOldCalendarInfo->iProperties.Count());
       
   345         for (TInt i(0) ; i < KOldPropertyCount ; ++i)
       
   346             {
       
   347             TInt position(iProperties.FindInOrder(aOldCalendarInfo->iProperties[i], CAgnCalendarInfoProperty::Compare));
       
   348             
       
   349             if (position == KErrNotFound)
       
   350                 {
       
   351                 // This property was not found in the new list so delete it
       
   352                 iFileStore.DeleteL(aOldCalendarInfo->iProperties[i]->StreamId());
       
   353                 }
       
   354             }
       
   355         }
       
   356     
       
   357     // Now update or delete properties that are in the new list 
       
   358     const TInt KPropertyCount(iProperties.Count());
       
   359     for (TInt i(0) ; i < KPropertyCount ; ++i)
       
   360         {
       
   361         // Find this property in the old metadata
       
   362         TInt position(KErrNotFound);
       
   363         if (aOldCalendarInfo)
       
   364             {
       
   365             position = aOldCalendarInfo->iProperties.FindInOrder(iProperties[i], CAgnCalendarInfoProperty::Compare);
       
   366             }
       
   367 
       
   368         switch (iProperties[i]->State())
       
   369             {
       
   370             case CAgnCalendarInfoProperty::EDeleted:
       
   371                 {
       
   372                 // The property has been marked for deleting so delete the stream
       
   373                 if (aOldCalendarInfo && position != KErrNotFound)
       
   374                     {
       
   375                     // Use the old property list's stream id
       
   376                     // Note: if it wasn't found then I can't have been stored before
       
   377                     // so we don't need to delete it
       
   378                     iFileStore.DeleteL(aOldCalendarInfo->iProperties[position]->StreamId());
       
   379                     }
       
   380                 }
       
   381                 break;
       
   382             case CAgnCalendarInfoProperty::EModified:
       
   383                 {
       
   384                 RStoreWriteStream writeStream;
       
   385                 
       
   386                 if (aOldCalendarInfo && position != KErrNotFound)
       
   387                     {
       
   388                     // The property has been edited so replace the stream
       
   389                     // Use the old property list's stream id
       
   390                     writeStream.ReplaceLC(iFileStore, aOldCalendarInfo->iProperties[position]->StreamId());
       
   391                     iProperties[i]->SetStreamId(aOldCalendarInfo->iProperties[position]->StreamId());
       
   392                     }
       
   393                 else if (iProperties[i]->StreamId() == KNullStreamId)
       
   394                     {
       
   395                     // This is a new property for this metadata
       
   396                     TStreamId newStreamId = writeStream.CreateLC(iFileStore);
       
   397                     iProperties[i]->SetStreamId(newStreamId);
       
   398                     }
       
   399                 else
       
   400                     {
       
   401                     // The property has been edited so replace the stream
       
   402                     writeStream.ReplaceLC(iFileStore, iProperties[i]->StreamId());
       
   403                     }
       
   404                 
       
   405                 writeStream << iProperties[i]->Value();
       
   406                 
       
   407                 writeStream.CommitL();
       
   408                 CleanupStack::PopAndDestroy(&writeStream);
       
   409                 }
       
   410                 break;
       
   411             default:
       
   412                 break;
       
   413             }
       
   414         }
       
   415     }
       
   416 
       
   417