activityfw/storage/client/s60/src/afstorageclientimp.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 <fbs.h>
       
    18 #include <s32mem.h>
       
    19 #include "afstorageclientimp.h"
       
    20 #include "afstorageglobals.h"
       
    21 #include "afglobals.h"
       
    22 #include "afentry.h"
       
    23 #include "afasyncrequest_p.h"
       
    24 #include "afthumbnailrequest_p.h"
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 /**
       
    28  * Furst phase constructor 
       
    29  */
       
    30 RAfStorageClientImplementation::RAfStorageClientImplementation(MAfAsyncRequestObserver &observer)
       
    31 :
       
    32 mObserver(observer)
       
    33 {}
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 /**
       
    37  * Destructor
       
    38  */
       
    39 RAfStorageClientImplementation::~RAfStorageClientImplementation()
       
    40 {
       
    41     Close();
       
    42 }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 void RAfStorageClientImplementation::Close()
       
    46 {
       
    47     mAsyncTasks.ResetAndDestroy();
       
    48     RSessionBase::Close();
       
    49 }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 /**
       
    53  * Function establish connection to activity server
       
    54   */
       
    55 void RAfStorageClientImplementation::connectL()
       
    56 {
       
    57     const int asyncMessageSlots(12);
       
    58     const int maxRetry(4);
       
    59     TInt retry = maxRetry;
       
    60     TInt errNo(KErrNone);
       
    61     do {
       
    62         errNo = CreateSession(KActivityServerName, 
       
    63                               TVersion(0, 0, 0), 
       
    64                               asyncMessageSlots);
       
    65         if (KErrNotFound != errNo && KErrServerTerminated != errNo) {
       
    66             retry =0;
       
    67         } else {
       
    68             TRAP(errNo, startServerL());
       
    69             if (KErrNone != errNo && KErrAlreadyExists != errNo) {
       
    70                 retry =0;
       
    71                 errNo = CreateSession(KActivityServerName, 
       
    72                                       TVersion(0, 0, 0), 
       
    73                                       asyncMessageSlots);
       
    74             }
       
    75         }
       
    76     } while (--retry > 0);
       
    77     User::LeaveIfError(errNo);
       
    78 }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 void RAfStorageClientImplementation::startServerL()
       
    82 {
       
    83     RProcess server;
       
    84     const TUidType uid(KNullUid, KNullUid, KActivityServerUid);
       
    85     User::LeaveIfError(server.Create(KActivityServerName, KNullDesC, uid));
       
    86     TRequestStatus stat;
       
    87     server.Rendezvous(stat);
       
    88     if (stat != KRequestPending) {
       
    89         server.Kill(0);
       
    90     } else {
       
    91         server.Resume();
       
    92     }
       
    93     User::WaitForRequest(stat);
       
    94     int errNo = (EExitPanic == server.ExitType()) ?
       
    95                 KErrGeneral : stat.Int();
       
    96     if (KErrCancel == errNo) {
       
    97         errNo = KErrNone;
       
    98     }
       
    99     server.Close();
       
   100     User::LeaveIfError(errNo);
       
   101 }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 void RAfStorageClientImplementation::executeL(int function)
       
   105 {
       
   106     switch (function) {
       
   107     case WaitActivity:
       
   108     case NotifyChange:
       
   109         CAfAsyncRequestPrivate::NewLD(mObserver, *this, static_cast<ActivityCmd>(function));
       
   110         break;
       
   111     default:
       
   112         User::LeaveIfError(SendReceive(function, TIpcArgs()));
       
   113     }
       
   114 }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 void RAfStorageClientImplementation::executeL(int function, 
       
   118                                               const CAfEntry &entry,
       
   119                                               int userData)
       
   120 {
       
   121     TPckgBuf<TInt> data(userData);
       
   122     RBuf8 serializedEntry;
       
   123     CleanupClosePushL(serializedEntry);
       
   124     serializedEntry.CreateL(entry.Size());
       
   125     RDesWriteStream stream(serializedEntry);
       
   126     CleanupClosePushL(stream);
       
   127     stream << entry;
       
   128     CleanupStack::PopAndDestroy(&stream);
       
   129     User::LeaveIfError(SendReceive(function, TIpcArgs(&serializedEntry, &data)));
       
   130     CleanupStack::PopAndDestroy(&serializedEntry);
       
   131 }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 void RAfStorageClientImplementation::executeL(int function, RPointerArray<CAfEntry> &resultsList, const CAfEntry& templateEntry)
       
   135 {
       
   136     resultsList.ResetAndDestroy();
       
   137     RBuf8 buffer;
       
   138     CleanupClosePushL(buffer);
       
   139     CAfEntry::ReallocL(buffer, templateEntry.Size());
       
   140     RDesWriteStream writer(buffer);
       
   141     CleanupClosePushL(writer);
       
   142     writer << templateEntry;
       
   143     CleanupStack::PopAndDestroy(&writer);
       
   144     TPckgBuf<int> length(0), taskId(0);
       
   145     User::LeaveIfError(SendReceive(function,
       
   146                                    TIpcArgs(&buffer, &length, &taskId)));
       
   147     CAfEntry::ReallocL(buffer, length());
       
   148     User::LeaveIfError(SendReceive(GetData, TIpcArgs(&taskId, &buffer)));
       
   149     resultsList << buffer;
       
   150     CleanupStack::PopAndDestroy(&buffer);
       
   151 }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 void RAfStorageClientImplementation::getThumbnailL(TSize size, 
       
   155                                                    const TDesC& source, 
       
   156                                                    TAny *userData )
       
   157 {
       
   158     CAfThumbnailRequestPrivate::NewLD(mObserver, *this, size, source, userData);
       
   159 }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 void RAfStorageClientImplementation::PushL(CBase* asyncTask)
       
   163 {
       
   164     const TInt offset(mAsyncTasks.Find(asyncTask));
       
   165     (KErrNotFound == offset) ? mAsyncTasks.AppendL(asyncTask) : User::Leave(KErrAlreadyExists);
       
   166 }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 void RAfStorageClientImplementation::Pop(CBase* asyncTask)
       
   170 {
       
   171     const TInt offset(mAsyncTasks.Find(asyncTask));
       
   172     if (KErrNotFound != offset) {
       
   173         mAsyncTasks.Remove(offset);
       
   174     }
       
   175 }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 void RAfStorageClientImplementation::sendAsync(int function, 
       
   179                                               const TIpcArgs& args, 
       
   180                                               TRequestStatus& status)
       
   181 {
       
   182     SendReceive(function, args, status);
       
   183 }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 void RAfStorageClientImplementation::getDataL(int id, TDes8& buffer)
       
   187 {
       
   188     TPckgBuf<int> requestId(id);
       
   189     SendReceive(GetData, TIpcArgs(&requestId, &buffer));
       
   190 }