activityfw/activitydatabase/hsactivitydbserver/src/afstoragesynctask.cpp
changeset 102 8b8b34fa9751
parent 100 0920c6a9b6c8
child 106 e78d6e055a5b
equal deleted inserted replaced
100:0920c6a9b6c8 102:8b8b34fa9751
     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 <hash.h>
       
    19 #include <fbs.h>
       
    20 #include <bautils.h>
       
    21 
       
    22 #include "afstoragesynctask.h"
       
    23 #include "activitycmd.h"
       
    24 #include "afentry.h"
       
    25 const TInt KMaxPathLength = 256;
       
    26 
       
    27 _LIT(KUnsupportedStorageSyncTask, "Unsupported sync storage task");
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 /**
       
    31  * Handle synchronous data storage requests
       
    32  * @param dataStorage - data storage
       
    33  * @param msg - request message
       
    34  */
       
    35 void AfStorageSyncTask::ExecuteL(MAfTaskStorage& observers,
       
    36                                  CAfStorage& dataStorage, 
       
    37                                  const RMessage2& msg)
       
    38 {
       
    39     switch (msg.Function()) {
       
    40     case AddActivity: 
       
    41         AddActivityL(dataStorage, msg);
       
    42         break;
       
    43     case UpdateActivity:
       
    44         UpdateActivityL(dataStorage, msg);
       
    45         break;
       
    46     case RemoveActivity:
       
    47         DeleteActivityL(dataStorage, msg);
       
    48         break;
       
    49     case RemoveApplicationActivities:
       
    50         DeleteApplicationActivitiesL(dataStorage, msg);
       
    51         break;
       
    52     default:
       
    53         //this code shouldn't be called. fatal error: means wrong session implementation 
       
    54         User::Panic(KUnsupportedStorageSyncTask, KErrGeneral);
       
    55     };
       
    56     msg.Complete(KErrNone);
       
    57     NotifyChangeL(observers, msg);
       
    58 }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 /**
       
    62  * Handle adding new activity.
       
    63  * @param dataStorage - data storage
       
    64  * @param msg - request message
       
    65  */
       
    66 void AfStorageSyncTask::AddActivityL(CAfStorage& dataStorage, 
       
    67                                      const RMessage2& msg)
       
    68 {
       
    69     //Read message and bitmap handle
       
    70     TPckgBuf<TInt> bitmapHdl(0);
       
    71     CAfEntry *entry = CAfEntry::NewLC();
       
    72     ReadEntryL(*entry, msg);
       
    73     msg.ReadL(1, bitmapHdl);
       
    74     
       
    75     RBuf thumbnailPath;
       
    76     CleanupClosePushL(thumbnailPath);
       
    77     ThumbnailPathL(thumbnailPath, 
       
    78                    dataStorage.Fs(), 
       
    79                    entry->ApplicationId(), 
       
    80                    entry->ActivityId());
       
    81     CreateThumbnailL(thumbnailPath, bitmapHdl());
       
    82     entry->SetImageSrcL(thumbnailPath);
       
    83     dataStorage.AddActivityL(*entry);
       
    84     CleanupStack::PopAndDestroy(&thumbnailPath);
       
    85     
       
    86     CleanupStack::PopAndDestroy(entry);
       
    87 }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 /**
       
    91  * Handle updating existing activiy
       
    92  * @param dataStorage - data storage
       
    93  * @param msg - request message
       
    94  */
       
    95 void AfStorageSyncTask::UpdateActivityL(CAfStorage& dataStorage, 
       
    96                                         const RMessage2& msg)
       
    97 {
       
    98     TPckgBuf<TInt> bitmapHdl(0);
       
    99     CAfEntry *entry = CAfEntry::NewLC();
       
   100     ReadEntryL(*entry, msg);
       
   101     msg.ReadL(1, bitmapHdl);
       
   102         
       
   103     RBuf thumbnailPath;
       
   104     CleanupClosePushL(thumbnailPath);
       
   105     ThumbnailPathL(thumbnailPath, 
       
   106                    dataStorage.Fs(), 
       
   107                    entry->ApplicationId(), 
       
   108                    entry->ActivityId());
       
   109     CreateThumbnailL(thumbnailPath, bitmapHdl());
       
   110     entry->SetImageSrcL(thumbnailPath);
       
   111     dataStorage.UpdateActivityL(*entry);
       
   112     CleanupStack::PopAndDestroy(&thumbnailPath);
       
   113     CleanupStack::PopAndDestroy(entry);
       
   114 }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 /**
       
   118  * Handle removing activity.
       
   119  * @param dataStorage - data storage
       
   120  * @param msg - request message
       
   121  */
       
   122 void AfStorageSyncTask::DeleteActivityL(CAfStorage& dataStorage, 
       
   123                                         const RMessage2& msg)
       
   124 {
       
   125     CAfEntry *entry = CAfEntry::NewLC();
       
   126     ReadEntryL(*entry, msg);
       
   127     dataStorage.DeleteActivityL(*entry);
       
   128     CleanupStack::PopAndDestroy(entry);
       
   129 }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 /**
       
   133  * Handle removing all application activities.
       
   134  * @param dataStorage - data storage
       
   135  * @param msg - request message
       
   136  */
       
   137 void AfStorageSyncTask::DeleteApplicationActivitiesL(CAfStorage& dataStorage, 
       
   138                                                      const RMessage2& msg)
       
   139 {
       
   140     CAfEntry *entry = CAfEntry::NewLC();
       
   141     ReadEntryL(*entry, msg);
       
   142     dataStorage.DeleteActivitiesL(*entry);
       
   143     CleanupStack::PopAndDestroy(entry);
       
   144 }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 void AfStorageSyncTask::NotifyChangeL(MAfTaskStorage& observers,
       
   151                                       const RMessage2& msg)
       
   152 {
       
   153     const RPointerArray<CAfTask> &table(observers.StorageData());
       
   154     for (TInt iter(table.Count() - 1); 0 <= iter; --iter) {
       
   155         table[iter]->BroadcastReceivedL(msg);
       
   156     }
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void AfStorageSyncTask::ReadEntryL(CAfEntry& entry, const RMessage2& msg)
       
   164 {
       
   165     RBuf8 serializedEntry;
       
   166     CleanupClosePushL(serializedEntry);
       
   167     serializedEntry.CreateL(msg.GetDesLengthL(0));
       
   168     msg.ReadL(0, serializedEntry);
       
   169     RDesReadStream reader(serializedEntry);
       
   170     CleanupClosePushL(reader);
       
   171     
       
   172     reader >> entry;
       
   173     
       
   174     CleanupStack::PopAndDestroy(&reader);
       
   175     CleanupStack::PopAndDestroy(&serializedEntry);
       
   176 }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void AfStorageSyncTask::CreateThumbnailL(const TDesC &path, TInt hdl)
       
   183 {
       
   184     if (0 >= hdl) {
       
   185         User::Leave(KErrCorrupt);
       
   186     }
       
   187     CFbsBitmap *bitmap = new (ELeave) CFbsBitmap;
       
   188     CleanupStack::PushL(bitmap);
       
   189     User::LeaveIfError(bitmap->Duplicate(hdl));
       
   190     User::LeaveIfError(bitmap->Save(path));
       
   191     CleanupStack::PopAndDestroy(bitmap);
       
   192 }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void AfStorageSyncTask::ThumbnailPathL(RBuf &dst, 
       
   199                                        RFs& fileSystem, 
       
   200                                        TInt uid, 
       
   201                                        const TDesC &activityName)
       
   202 {
       
   203     _LIT(KUidFormat, "%+08x\\");
       
   204     _LIT(KExtFormat, ".mbm");
       
   205     //Generate activity hash
       
   206     RBuf8 buff8;
       
   207     CleanupClosePushL(buff8);
       
   208     buff8.CreateL(activityName.Length());
       
   209     buff8.Copy(activityName);
       
   210     HBufC8 *activityHash = Md5HexDigestL(buff8);
       
   211     CleanupStack::PopAndDestroy(&buff8);
       
   212     CleanupStack::PushL(activityHash);
       
   213     
       
   214     //Get private path
       
   215     RBuf privatePath;
       
   216     CleanupClosePushL(privatePath);
       
   217     privatePath.CreateL(KMaxPathLength);
       
   218     User::LeaveIfError(fileSystem.PrivatePath(privatePath));
       
   219     
       
   220     //Format activity path
       
   221     privatePath.AppendFormat( KUidFormat, uid);
       
   222     
       
   223     const TInt requiredSize(privatePath.Length() + 
       
   224                             activityHash->Length() + 
       
   225                             KExtFormat().Length());
       
   226     CAfEntry::ReallocL(dst, requiredSize);
       
   227     
       
   228     //Copy path
       
   229     dst.Copy(privatePath);
       
   230     privatePath.Copy(*activityHash);//reuse already allocated buffer to convert 8 -> 16
       
   231     dst.Append(privatePath);
       
   232     dst.Append(KExtFormat());
       
   233     
       
   234     CleanupStack::PopAndDestroy(&privatePath);
       
   235     CleanupStack::PopAndDestroy(activityHash);
       
   236     
       
   237     BaflUtils::EnsurePathExistsL(fileSystem, dst);
       
   238 }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 HBufC8* AfStorageSyncTask::Md5HexDigestL(const TDes8 &string)
       
   245 {
       
   246     _LIT8(KMd5HexFormat, "%+02x");
       
   247     CMD5* md5 = CMD5::NewL();
       
   248     CleanupStack::PushL(md5);
       
   249     
       
   250     TPtrC8 hashedSig(md5->Hash(string));
       
   251     
       
   252     HBufC8* buf = HBufC8::NewL(hashedSig.Length() * 2);
       
   253     TPtr8 bufPtr = buf->Des();
       
   254     
       
   255     for(TInt i(0); i< hashedSig.Length(); ++i) {
       
   256         bufPtr.AppendFormat(KMd5HexFormat,hashedSig[i]);
       
   257     }
       
   258     CleanupStack::PopAndDestroy(md5);
       
   259     return buf;
       
   260 }