mpx/collectionframework/collectionserver/src/mpxcollectionserversession.cpp
changeset 0 a2952bb97e68
child 19 51035f0751c2
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Collection server session
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <e32svr.h>
       
    20 #include <bamdesca.h>
       
    21 #include <mpxlog.h>
       
    22 #include <mpxclientlist.h>
       
    23 #include <mpxcmn.h>
       
    24 #include <mpxcollectionpath.h>
       
    25 #include <mpxcollectiontype.h>
       
    26 #include <mpxmessagequeue.h>
       
    27 #include <mpxmediaarray.h>
       
    28 #include <mpxcollectioncommanddefs.h>
       
    29 #include <mpxcommandgeneraldefs.h>
       
    30 #include <mpxmediaarray.h>
       
    31 #include <mpxmediacontainerdefs.h>
       
    32 #include "mpxcollectionserver.h"
       
    33 #include "mpxcollectionengine.h"
       
    34 #include "mpxcollectionclientcontext.h"
       
    35 #include "mpxcollectionserversession.h"
       
    36 
       
    37 // ============================ LOCAL FUNCTIONS ==============================
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // Panic client
       
    41 // ----------------------------------------------------------------------------
       
    42 //
       
    43 LOCAL_C void PanicClient(const RMessage2 &aMessage,TInt aPanic)
       
    44     {
       
    45     _LIT(KTxtServer,"Collection server Session");
       
    46     aMessage.Panic(KTxtServer,aPanic);
       
    47     }
       
    48 
       
    49 // ----------------------------------------------------------------------------
       
    50 // Two-phased constructor.
       
    51 // ----------------------------------------------------------------------------
       
    52 //
       
    53 CMPXCollectionSession* CMPXCollectionSession::NewL(
       
    54     CMPXCollectionEngine& aEngine)
       
    55     {
       
    56     CMPXCollectionSession* s = new(ELeave)CMPXCollectionSession(aEngine);
       
    57     CleanupStack::PushL(s);
       
    58     s->ConstructL();
       
    59     CleanupStack::Pop(s);
       
    60     return s;
       
    61     }
       
    62 
       
    63 // ----------------------------------------------------------------------------
       
    64 // C++ constructor can NOT contain any code that might leave.
       
    65 // ----------------------------------------------------------------------------
       
    66 //
       
    67 CMPXCollectionSession::CMPXCollectionSession(CMPXCollectionEngine& aEngine)
       
    68     : iEngine(aEngine)
       
    69     {
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // Symbian 2nd phase constructor can leave.
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 void CMPXCollectionSession::ConstructL()
       
    77     {
       
    78     MPX_FUNC_EX("CMPXCollectionSession::ConstructL");
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // Destructor
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 CMPXCollectionSession::~CMPXCollectionSession()
       
    86     {
       
    87     MPX_DEBUG2("CMPXCollectionSession::~CMPXCollectionSession this %08x", this);
       
    88     CancelRequests();
       
    89     if (iContext)
       
    90         {
       
    91         iContext->RemoveClient(*iMessageQueue);
       
    92         }
       
    93     static_cast<CMPXCollectionServer*>(
       
    94             const_cast<CServer2*>(Server()))->RemoveClient();
       
    95     delete iAsyncBuffer;
       
    96     delete iSyncBuffer;
       
    97     delete iSyncMedia;
       
    98     delete iAsyncMedia;
       
    99     delete iMessageQueue;
       
   100     delete iRootMediaArray;
       
   101     delete iMediaCommand;
       
   102     delete iMediaPath;
       
   103     }
       
   104 
       
   105 // ----------------------------------------------------------------------------
       
   106 // Service request
       
   107 // ----------------------------------------------------------------------------
       
   108 //
       
   109 void CMPXCollectionSession::ServiceL(const RMessage2& aMessage)
       
   110     {
       
   111     // by default - change for async in helper methods if required
       
   112     iCompleteRequest=ETrue;
       
   113     TInt r=KErrNone;
       
   114     MPX_TRAPD(err,r = DispatchMessageL(aMessage));
       
   115     TBool isErr=(err!=KErrNone);
       
   116     // If it's not async, complete now (or an async helper method leaves)
       
   117     if(iCompleteRequest)
       
   118         {
       
   119         aMessage.Complete(isErr ? err : r);
       
   120         }
       
   121     else  if (isErr) // Async and error,  remove message from message queue
       
   122         {
       
   123         CompleteAsync(err);
       
   124         }
       
   125     }
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // Message dispatcher
       
   129 // ----------------------------------------------------------------------------
       
   130 //
       
   131 TInt CMPXCollectionSession::DispatchMessageL(const RMessage2& aMessage)
       
   132     {
       
   133     TInt r = KErrNone;
       
   134     TInt op=aMessage.Function();
       
   135     MPX_DEBUG3("-->CMPXCollectionSession::DispatchMessageL %d, this 0x%08x",
       
   136                op, this);
       
   137 
       
   138     if (op == EMcsSetMode)
       
   139         {
       
   140         SetModeL(aMessage); // This is the only operation when the iContext
       
   141                             // hasn't been defined.
       
   142         }
       
   143     else if (!iContext)
       
   144         {
       
   145         MPX_DEBUG1("CMPXCollectionSession::DispatchMessageL no context");
       
   146         User::Leave(KErrNotReady);
       
   147         }
       
   148     else
       
   149         {
       
   150         CBufBase* buffer(NULL);
       
   151         CMPXMedia* media( NULL );
       
   152         switch(op)
       
   153             {
       
   154             case EMcsOpenPath:
       
   155                 {
       
   156                 SetAsyncL(aMessage);
       
   157                 MPXUser::CreateBufferL(aMessage, 3, buffer);
       
   158                 CleanupStack::PushL(buffer);
       
   159                 RBufReadStream readStream( *buffer );
       
   160                 CleanupClosePushL(readStream);
       
   161                 CMPXCollectionPath* path = CMPXCollectionPath::NewL(readStream);
       
   162                 CleanupStack::PushL(path);
       
   163                 // Internalize open mode
       
   164                 TInt mode = readStream.ReadInt32L();
       
   165                 if (path->Levels() >0 )
       
   166                     {
       
   167                     // Internalize attributes
       
   168                     RArray<TMPXAttribute> attrs;
       
   169                     CleanupClosePushL(attrs);
       
   170                     ::InternalizeL(attrs, readStream);
       
   171                     path->SetL(attrs.Array());
       
   172                     CleanupStack::PopAndDestroy(&attrs);
       
   173                     }
       
   174                 // ownership of path transferred
       
   175                 iContext->OpenL(path, mode, this);
       
   176                 CleanupStack::Pop(path);
       
   177                 CleanupStack::PopAndDestroy(&readStream);
       
   178                 CleanupStack::PopAndDestroy(buffer);
       
   179                 break;
       
   180                 }
       
   181             case EMcsOpenIndex:
       
   182                 {
       
   183                 SetAsyncL(aMessage);
       
   184                 TPckgBuf<TInt> indexPkg;
       
   185                 aMessage.ReadL(1,indexPkg);
       
   186                 MPXUser::CreateBufferL(aMessage, 3, buffer);
       
   187                 CleanupStack::PushL(buffer);
       
   188                 RBufReadStream readStream( *buffer );
       
   189                 CleanupClosePushL(readStream);
       
   190                 // Internalize mode
       
   191                 TInt mode = readStream.ReadInt32L();
       
   192                 // Internalize attributes
       
   193                 RArray<TMPXAttribute> attrs;
       
   194                 CleanupClosePushL(attrs);
       
   195                 ::InternalizeL(attrs, readStream);
       
   196                 iContext->OpenL(indexPkg(), mode, attrs.Array(), this);
       
   197                 CleanupStack::PopAndDestroy(&attrs);
       
   198                 CleanupStack::PopAndDestroy(&readStream);
       
   199                 CleanupStack::PopAndDestroy(buffer);
       
   200                 break;
       
   201                 }
       
   202             case EMcsOpenByUids:
       
   203                 {
       
   204                 SetAsyncL(aMessage);
       
   205                 MPXUser::CreateBufferL(aMessage, 3, buffer);
       
   206                 CleanupStack::PushL(buffer);
       
   207                 RBufReadStream readStream(*buffer);
       
   208                 CleanupClosePushL(readStream);
       
   209                 TInt mode = readStream.ReadInt32L();
       
   210                 RArray<TUid> uids;
       
   211                 CleanupClosePushL(uids);
       
   212                 ::InternalizeL(uids, readStream);
       
   213                 iContext->OpenL(uids.Array(), mode, this);
       
   214                 CleanupStack::PopAndDestroy(&uids);
       
   215                 CleanupStack::PopAndDestroy(&readStream);
       
   216                 CleanupStack::PopAndDestroy(buffer);
       
   217                 break;
       
   218                 }
       
   219             case EMcsOpen:
       
   220                 {
       
   221                 SetAsyncL(aMessage);
       
   222                 iContext->OpenL(aMessage.Int3(), this);
       
   223                 break;
       
   224                 }
       
   225             case EMcsMediaByPath:
       
   226                 {
       
   227                 SetAsyncL(aMessage);
       
   228                 // Reset previous media command and result
       
   229                 delete iRootMediaArray;
       
   230                 iRootMediaArray = NULL;
       
   231                 delete iMediaCommand;
       
   232                 iMediaCommand = NULL;
       
   233                 iMediaCommand = CMPXCommand::NewL(aMessage.Int1());
       
   234                 // Extract client's capabilites from the RMessage
       
   235                 TCapabilitySet theCaps =  TSecurityInfo(aMessage).iCaps ;
       
   236                 iMediaCommand->SetTObjectValueL<TCapabilitySet>(
       
   237                                         KMPXCommandMediaCapbilitySet, theCaps);
       
   238                 MPX_ASSERT(iMediaCommand->IsSupported(KMPXCommandGeneralTargetIds));
       
   239                 delete iMediaPath;
       
   240                 iMediaPath = NULL;
       
   241                 iMediaPath = iMediaCommand->ValueCObjectL<CMPXCollectionPath>(
       
   242                                                    KMPXCommandGeneralTargetIds);
       
   243                 MPX_ASSERT(iMediaPath->Levels()>0);
       
   244                 if (iMediaPath->Selection().Count()>0 && iMediaPath->Levels() == 1)
       
   245                     { // multiple selection at root level
       
   246                     iRootMediaArray=CMPXMediaArray::NewL(); // array of media result
       
   247                     iMediaCommand->SetTObjectValueL<TInt>(KMPXCommandMediaIndex, 0);
       
   248                     iMediaPath->Set(iMediaPath->Selection()[0]);
       
   249                     // update the collection path in the command
       
   250                     iMediaCommand->SetCObjectValueL<CMPXCollectionPath>(KMPXCommandGeneralTargetIds,
       
   251                                                                         iMediaPath);
       
   252                     }
       
   253                 iContext->MediaL(*iMediaCommand, this);
       
   254                 break;
       
   255                 }
       
   256             case EMcsBack:
       
   257                 {
       
   258                 SetAsyncL(aMessage);
       
   259                 iContext->BackL(this);
       
   260                 break;
       
   261                 }
       
   262             case EMcsRemovePath:
       
   263                 {
       
   264                 SetAsyncL(aMessage);
       
   265                 CMPXCollectionPath* path( NULL );
       
   266                 ::NewFromMessageL(aMessage, 0, path);
       
   267                 CleanupStack::PushL( path );
       
   268                 MPX_ASSERT(path);
       
   269                 MPX_ASSERT(path->Levels()>0);
       
   270                 iContext->RemoveL(path, this);
       
   271                 CleanupStack::Pop( path ); // ownership transferred
       
   272                 break;
       
   273                 }
       
   274             case EMcsGetUid:
       
   275                 {
       
   276                 MPX_DEBUG1("CMPXCollectionSession::DispatchMessageL EMcsGetUid");
       
   277                 TPckgC<TInt> uidPkg(iContext->PluginId().iUid);
       
   278                 aMessage.Write(0,uidPkg);
       
   279                 break;
       
   280                 }
       
   281             case EMcsGetPath:
       
   282                 {
       
   283                 MPX_DEBUG1("CMPXCollectionSession::DispatchMessageL EMcsGetPath");
       
   284                 ::CreateBufferL<CMPXCollectionPath>(iContext->Path(), iSyncBuffer);
       
   285                 r = iSyncBuffer->Size();
       
   286                 break;
       
   287                 }
       
   288             case EMcsGetSyncBuffer:
       
   289                 {
       
   290                 MPX_DEBUG1("CMPXCollectionSession::DispatchMessageL EMcsGetSyncBuffer");
       
   291                 aMessage.WriteL(0,iSyncBuffer->Ptr(0));
       
   292                 delete iSyncBuffer;
       
   293                 iSyncBuffer = NULL;
       
   294                 break;
       
   295                 }
       
   296             case EMcsGetAsyncBuffer:
       
   297                 {
       
   298                 MPX_DEBUG1("CMPXCollectionSession::DispatchMessageL EMcsGetAsyncBuffer");
       
   299                 aMessage.WriteL(0,iAsyncBuffer->Ptr(0));
       
   300                 delete iAsyncBuffer;
       
   301                 iAsyncBuffer = NULL;
       
   302                 break;
       
   303                 }
       
   304             case EMcsCommandExt:
       
   305                 {
       
   306                 if (aMessage.Int0())
       
   307                     { // async command
       
   308                     SetAsyncL(aMessage);
       
   309                     }
       
   310                 CMPXCommand* cmd = CMPXCommand::NewL(aMessage.Int1());
       
   311                 CleanupStack::PushL(cmd);
       
   312                 iContext->CommandL(*cmd, this, *iMessageQueue);
       
   313                 CleanupStack::PopAndDestroy(cmd);
       
   314                 break;
       
   315                 }
       
   316             case EMcsAddItem:
       
   317                 {
       
   318                 ::NewFromMessageL<CMPXMedia>(aMessage,0,media);
       
   319                 CleanupStack::PushL(media);
       
   320                 iContext->AddL(*media);
       
   321                 CleanupStack::PopAndDestroy(media);
       
   322                 break;
       
   323                 }
       
   324             case EMcsRemoveItem:
       
   325                 {
       
   326                 ::NewFromMessageL<CMPXMedia>(aMessage,0,media);
       
   327                 CleanupStack::PushL(media);
       
   328                 iContext->RemoveL(*media);
       
   329                 CleanupStack::PopAndDestroy(media);
       
   330                 break;
       
   331                 }
       
   332             case EMcsSetMedia:
       
   333                 {
       
   334                 ::NewFromMessageL<CMPXMedia>(aMessage,0,media);
       
   335                 CleanupStack::PushL(media);
       
   336                 iContext->SetL(*media);
       
   337                 CleanupStack::PopAndDestroy(media);
       
   338                 break;
       
   339                 }
       
   340             case EMcsSetFilter:
       
   341                 {
       
   342                 CMPXFilter* filter=NULL;
       
   343                 if (aMessage.GetDesLengthL(0))
       
   344                     {
       
   345                     ::NewFromMessageL<CMPXFilter>(aMessage,0,filter);
       
   346                     }
       
   347                 if (filter)
       
   348                     {
       
   349                     CleanupStack::PushL(filter);
       
   350                     iContext->SetFilterL(filter);
       
   351                     CleanupStack::PopAndDestroy(filter);
       
   352                     }
       
   353                 else
       
   354                     {
       
   355                     iContext->SetFilterL(NULL);
       
   356                     }
       
   357                 break;
       
   358                 }
       
   359             case EMcsCommand:
       
   360                 {
       
   361                 iContext->CommandL((TMPXCollectionCommand) aMessage.Int0(),
       
   362                                      aMessage.Int1());
       
   363                 break;
       
   364                 }
       
   365             case EMcsFindAll:
       
   366                 {
       
   367                 CMPXMedia* media = CMPXMedia::NewL(aMessage.Int2());
       
   368                 CleanupStack::PushL(media);
       
   369                 MPXUser::CreateBufferL( aMessage, 0, buffer );
       
   370                 CleanupStack::PushL(buffer);
       
   371                 if (aMessage.Int1())
       
   372                     { //Sync find
       
   373                     delete iSyncMedia;
       
   374                     iSyncMedia = NULL;
       
   375                     iSyncMedia = iContext->FindAllSyncL(*media, *buffer);
       
   376                     if (iSyncMedia)
       
   377                         {
       
   378                         ::CreateBufferL<CMPXMedia>(*iSyncMedia,iSyncBuffer);
       
   379                         r = iSyncBuffer->Size();
       
   380                         }
       
   381                     else
       
   382                         {
       
   383                         r = KErrNotFound;
       
   384                         }
       
   385                     CleanupStack::PopAndDestroy(buffer);
       
   386                     }
       
   387                 else
       
   388                     { // Async
       
   389                     SetAsyncL(aMessage);
       
   390                     iContext->FindAllL(*media, buffer, this);
       
   391                     CleanupStack::Pop(buffer); // ownership transferred
       
   392                     }
       
   393                 CleanupStack::PopAndDestroy(media);
       
   394                 break;
       
   395                 }
       
   396             case EMcsNotifyEvent:
       
   397                 {
       
   398                 iEngine.NotifyL( (TMPXCollectionBroadCastMsg) aMessage.Int0(), aMessage.Int1() );
       
   399                 break;
       
   400                 }
       
   401             case EMcsGetSupportedTypes:
       
   402                 {
       
   403                 RPointerArray<CMPXCollectionType> array;
       
   404                 iEngine.GetSupportedTypesL( array );
       
   405                 ::CreateBufferL(array.Array() , iSyncBuffer);
       
   406                 r = iSyncBuffer->Size();
       
   407                 array.ResetAndDestroy();
       
   408                 array.Close();
       
   409                 break;
       
   410                 }
       
   411             case EMcsGetCapabilities:
       
   412                 {
       
   413                 // Get the capabilities from the client context
       
   414                 TCollectionCapability cap = iContext->GetCapabilities();
       
   415                 TPckgC<TCollectionCapability> p(cap);
       
   416                 aMessage.Write(0,p);
       
   417                 break;
       
   418                 }
       
   419             case EMcsCancelRequest:
       
   420                 {
       
   421                 CancelRequests();
       
   422                 break;
       
   423                 }
       
   424             case EMcsCollectionIdLookup:
       
   425                 {
       
   426                 // Recreate buffer
       
   427                 CBufBase* buffer(NULL);
       
   428                 MPXUser::CreateBufferL( aMessage, 0, buffer );
       
   429                 CleanupStack::PushL( buffer );
       
   430 
       
   431                 // Array from buffer
       
   432                 RArray<TUid> uids;
       
   433                 CleanupClosePushL( uids );
       
   434 
       
   435                 RBufReadStream rs( *buffer );
       
   436                 CleanupClosePushL( rs );
       
   437                 ::InternalizeL<TUid>( uids, rs );
       
   438                 CleanupStack::PopAndDestroy( &rs );
       
   439 
       
   440                 // Resolve and return
       
   441                 TUid pluginId = iEngine.ResolvePluginUid( uids.Array() );
       
   442                 TPckgC<TUid> p( pluginId );
       
   443                 aMessage.WriteL(1,p);
       
   444                 CleanupStack::PopAndDestroy( 2, buffer ); // buffer, uids
       
   445                 break;
       
   446                 }
       
   447             case EMcsCollectionID:
       
   448                 {
       
   449                 // This is a placeholder function,
       
   450                 //
       
   451                 TPckgBuf<TUid> id;
       
   452                 aMessage.ReadL(0,id);
       
   453 
       
   454                 // Lookup id and write back to client
       
   455                 TUid realId = iEngine.LookupCollectionPluginID( id() );
       
   456                 TPckgC<TUid> p(realId);
       
   457                 aMessage.WriteL(0,p);
       
   458                 break;
       
   459                 }
       
   460             case EMcsFilter:
       
   461                 {
       
   462                 const CMPXFilter* filter=iContext->Filter();
       
   463                 if (filter)
       
   464                     {
       
   465                     ::CreateBufferL<CMPXFilter>(*filter,iSyncBuffer);
       
   466                     r=iSyncBuffer->Size();
       
   467                     }
       
   468                 break;
       
   469                 }
       
   470             case EMcsGetNextMessage:
       
   471                 {
       
   472                 MPX_ASSERT(iMessageQueue);
       
   473                 iMessageQueue->SendNext(aMessage);
       
   474                 iCompleteRequest=EFalse;
       
   475                 break;
       
   476                 }
       
   477             case EMcsCancelGetMessage:
       
   478                 {
       
   479                 MPX_ASSERT(iMessageQueue);
       
   480                 iMessageQueue->Reset();
       
   481                 break;
       
   482                 }
       
   483             default:
       
   484                 {
       
   485                 PanicClient(aMessage, KErrNotSupported);
       
   486                 break;
       
   487                 }
       
   488             }
       
   489         }
       
   490     MPX_DEBUG3("<--CMPXCollectionSession::DispatchMessageL %d, this 0x%08x",
       
   491                op, this);
       
   492     return r;
       
   493     }
       
   494 
       
   495 // ----------------------------------------------------------------------------
       
   496 // Get current selection
       
   497 // ----------------------------------------------------------------------------
       
   498 //
       
   499 void CMPXCollectionSession::GetSelectionL(const RMessage2& aMessage)
       
   500     {
       
   501     (void)aMessage; 
       
   502     }
       
   503 
       
   504 // ----------------------------------------------------------------------------
       
   505 // Queue the message and complete async
       
   506 // ----------------------------------------------------------------------------
       
   507 //
       
   508 void CMPXCollectionSession::SetAsyncL(const RMessage2& aMessage)
       
   509     {
       
   510     MPX_ASSERT(iMessage.IsNull());
       
   511     iMessage = aMessage;
       
   512     iCompleteRequest=EFalse;
       
   513     }
       
   514 
       
   515 // ----------------------------------------------------------------------------
       
   516 // Complete queued message
       
   517 // ----------------------------------------------------------------------------
       
   518 //
       
   519 void CMPXCollectionSession::CompleteAsync(
       
   520     TInt aErr,
       
   521     TInt aSlot1, const TDesC8* aVal1,
       
   522     TInt aSlot2, const TDesC8* aVal2,
       
   523     TInt aSlot3, const TDesC8* aVal3)
       
   524     {
       
   525     MPX_ASSERT(!iMessage.IsNull());
       
   526     TInt err(KErrNone);
       
   527     if (aErr>=0)
       
   528         {
       
   529         err = DoWriteData(aSlot1, aVal1, aSlot2, aVal2, aSlot3, aVal3);
       
   530         if (err)
       
   531             {// Set to new error
       
   532             aErr=err;
       
   533             }
       
   534         }
       
   535     MPX_DEBUG4("CMPXCollectionSession::CompleteAsync 0x%08x task %d err %d",
       
   536                this, iMessage.Function(), aErr);
       
   537     iMessage.Complete(aErr);
       
   538     }
       
   539 
       
   540 // ----------------------------------------------------------------------------
       
   541 // Write data back to client
       
   542 // ----------------------------------------------------------------------------
       
   543 //
       
   544 TInt CMPXCollectionSession::DoWriteData(
       
   545     TInt aSlot1, const TDesC8* aVal1,
       
   546     TInt aSlot2, const TDesC8* aVal2,
       
   547     TInt aSlot3, const TDesC8* aVal3)
       
   548     {
       
   549     TInt ret(KErrNone);
       
   550     if (aVal1)
       
   551         {
       
   552         ret=iMessage.Write(aSlot1,*aVal1);
       
   553         }
       
   554 
       
   555     if (aVal2 && KErrNone==ret)
       
   556         {
       
   557         ret=iMessage.Write(aSlot2,*aVal2);
       
   558         }
       
   559 
       
   560     if (aVal3 && KErrNone==ret)
       
   561         {
       
   562         ret=iMessage.Write(aSlot3,*aVal3);
       
   563         }
       
   564     return ret;
       
   565     }
       
   566 
       
   567 // ----------------------------------------------------------------------------
       
   568 // Cancel all outstanding requests on this session
       
   569 // ----------------------------------------------------------------------------
       
   570 //
       
   571 void CMPXCollectionSession::CancelRequests()
       
   572     {
       
   573     if (!iMessage.IsNull())
       
   574         {
       
   575         iMessage.Complete(KErrCancel);
       
   576         }
       
   577     if (iContext)
       
   578         {
       
   579         iContext->CancelRequest(this);
       
   580         }
       
   581     }
       
   582 
       
   583 // ----------------------------------------------------------------------------
       
   584 // The thread ID of the client thread
       
   585 // ----------------------------------------------------------------------------
       
   586 //
       
   587 TThreadId CMPXCollectionSession::ClientIdL(const RMessage2& aMessage)
       
   588     {
       
   589     RThread t;
       
   590     aMessage.ClientL(t);
       
   591     TThreadId tid=t.Id();
       
   592     t.Close();
       
   593     return tid;
       
   594     }
       
   595 
       
   596 // ----------------------------------------------------------------------------
       
   597 // Set collection mode
       
   598 // ----------------------------------------------------------------------------
       
   599 //
       
   600 void CMPXCollectionSession::SetModeL(const RMessage2& aMessage)
       
   601     {
       
   602     if (!iMessageQueue)
       
   603         {
       
   604         iMessageQueue = CMPXMessageQueue::NewL();
       
   605         }
       
   606     iContext = &iEngine.SessionInitL(TUid::Uid(aMessage.Int0()),
       
   607                                      ClientIdL(aMessage),
       
   608                                      iMessageQueue);
       
   609     }
       
   610 
       
   611 // ----------------------------------------------------------------------------
       
   612 // Handles the collection entries being opened.
       
   613 // ----------------------------------------------------------------------------
       
   614 //
       
   615 void CMPXCollectionSession::HandleOpen(
       
   616     CMPXMedia* aMedia,
       
   617     TInt aIndex,
       
   618     TBool aComplete,
       
   619     TInt aError)
       
   620     {
       
   621     MPX_DEBUG2("CMPXCollectionSession::HandleOpen with media err %d", aError);
       
   622     TInt op(iMessage.Function());
       
   623     MPX_ASSERT((!iMessage.IsNull() &&
       
   624         (EMcsOpen == op || EMcsOpenIndex == op ||
       
   625          EMcsOpenPath == op || EMcsBack == op || EMcsOpenByUids == op)));
       
   626     TInt size(0);
       
   627     if (aError > 0 && aError != KMPXPathUpdated &&
       
   628         aError != KMPXCollectionPath && aMedia)
       
   629         {
       
   630         MPX_DEBUG2("CMPXCollectionSession::HandleOpen media count %d",
       
   631                    aMedia->Count());
       
   632         if (aMedia)
       
   633             {
       
   634             delete iAsyncMedia;
       
   635             iAsyncMedia=NULL;
       
   636             TRAPD(err,iAsyncMedia=CMPXMedia::NewL(*aMedia));
       
   637             if(err == KErrNone)
       
   638                 {
       
   639                 TRAP(err, ::CreateBufferL(*iAsyncMedia,iAsyncBuffer));
       
   640                 }
       
   641             if (err)
       
   642                 {
       
   643                 aError = err;
       
   644                 }
       
   645             else
       
   646                 {
       
   647                 size = iAsyncBuffer->Size();
       
   648                 }
       
   649             MPX_DEBUG2("CMPXCollectionSession::HandleOpen buffer size %d", size);
       
   650             }
       
   651         }
       
   652     TPckgC<TInt> index(aIndex);
       
   653     TPckgC<TInt> complete(static_cast<TInt>(aComplete));
       
   654     TPckgC<TInt> sizePkg(size);
       
   655     MPX_DEBUG3("CMPXCollectionSession::HandleOpen completed with media: err %d, size %d",
       
   656                aError, sizePkg());
       
   657     CompleteAsync(aError,
       
   658                    0, &sizePkg,
       
   659                    1, &index,
       
   660                    2, &complete);
       
   661     }
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 // Handle completion of an async op
       
   665 // ----------------------------------------------------------------------------
       
   666 //
       
   667 void CMPXCollectionSession::HandleOpComplete( TInt aError )
       
   668     {
       
   669     CompleteAsync(aError);
       
   670     }
       
   671 
       
   672 // ----------------------------------------------------------------------------
       
   673 // Handle media request callback
       
   674 // ----------------------------------------------------------------------------
       
   675 //
       
   676 void CMPXCollectionSession::HandleMedia(
       
   677     CMPXMedia* aMedia,
       
   678     TInt aError)
       
   679     {
       
   680     TInt op(iMessage.Function());
       
   681     TInt size(0);
       
   682     TInt err(KErrNone);
       
   683     TBool done(EFalse);
       
   684     MPX_ASSERT(!iMessage.IsNull() &&
       
   685         (EMcsMediaByPath == op || EMcsMedia == op));
       
   686     if (iRootMediaArray)
       
   687         { // multiple selection at root level
       
   688         // Advance to next item
       
   689         TInt* pIndex = iMediaCommand->Value<TInt>(KMPXCommandMediaIndex);
       
   690         if (!pIndex)
       
   691             {
       
   692             aError = KErrNoMemory;
       
   693             }
       
   694         else
       
   695             {
       
   696             *pIndex = *pIndex + 1;
       
   697             }
       
   698         if (KErrNone == aError && aMedia)
       
   699             {
       
   700             MPX_TRAP(err, iRootMediaArray->AppendL(*aMedia));
       
   701             }
       
   702         else
       
   703             {// append dummy media for this item
       
   704             TRAP(err, iRootMediaArray->AppendL(CMPXMedia::NewL()));
       
   705             }
       
   706 
       
   707         if (err)
       
   708             {
       
   709             aError = err; // set to latest error code
       
   710             }
       
   711         if (KErrNone==aError)
       
   712             { // done for all root items or error happens on an item
       
   713             if (*pIndex == iMediaPath->Selection().Count())
       
   714                 {
       
   715                 done = ETrue;
       
   716                 delete iAsyncMedia;
       
   717                 iAsyncMedia=NULL;
       
   718                 TRAP(err, iAsyncMedia=CMPXMedia::NewL());
       
   719                 if(KErrNone==err)
       
   720                     {
       
   721                     TRAP(err,
       
   722                         iAsyncMedia->SetCObjectValueL<CMPXMediaArray>(
       
   723                             KMPXMediaArrayContents, iRootMediaArray);
       
   724                         iAsyncMedia->SetTObjectValueL<TInt>(KMPXMediaArrayCount, *pIndex));
       
   725                     }
       
   726 
       
   727                 if (KErrNone==err)
       
   728                     {
       
   729                     aError = KMPXCollectionMedia;
       
   730                     }
       
   731                 }
       
   732             else
       
   733                 { // Send next request
       
   734 
       
   735                 TRAP(err,
       
   736                     iMediaCommand->SetTObjectValueL<TInt>(KMPXCommandMediaIndex, *pIndex);
       
   737                     iMediaPath->Set(iMediaPath->Selection()[*pIndex]);
       
   738                     // update path in command
       
   739                     iMediaCommand->SetCObjectValueL<CMPXCollectionPath>(KMPXCommandGeneralTargetIds,
       
   740                                                               iMediaPath);
       
   741                     iContext->MediaL(*iMediaCommand, this));
       
   742                 }
       
   743             if (err)
       
   744                 {
       
   745                 aError = err;
       
   746                 }
       
   747             }
       
   748 
       
   749         if (aError<0)
       
   750             { // done with error
       
   751             done = ETrue;
       
   752             delete iAsyncMedia;
       
   753             iAsyncMedia = NULL;
       
   754             }
       
   755         }
       
   756     else
       
   757         {
       
   758         done = ETrue;
       
   759         if (KErrNone == aError && aMedia)
       
   760             {
       
   761             aError = KMPXCollectionMedia;
       
   762             delete iAsyncMedia;
       
   763             iAsyncMedia=NULL;
       
   764             TRAP_IGNORE(iAsyncMedia=CMPXMedia::NewL(*aMedia));
       
   765             }
       
   766         }
       
   767 
       
   768     if (done)
       
   769         {
       
   770         // Free resource
       
   771         delete iMediaPath;
       
   772         iMediaPath = NULL;
       
   773         if (iAsyncMedia && aError>=0)
       
   774             {
       
   775             TRAP(err, ::CreateBufferL<CMPXMedia>(*iAsyncMedia,iAsyncBuffer));
       
   776             if (iAsyncBuffer)
       
   777                 {
       
   778                 size = iAsyncBuffer->Size();
       
   779                 }
       
   780             }
       
   781         if (err)
       
   782             {
       
   783             aError = err;
       
   784             }
       
   785         TPckgC<TInt> sizePkg(size);
       
   786         CompleteAsync(aError,0, &sizePkg);
       
   787         }
       
   788     }
       
   789 
       
   790 // ----------------------------------------------------------------------------
       
   791 // Callback of async CommandL
       
   792 // ----------------------------------------------------------------------------
       
   793 //
       
   794  void CMPXCollectionSession::HandleCommandComplete(
       
   795     CMPXCommand* aCommandResult,
       
   796     TInt aError)
       
   797      {
       
   798      MPX_ASSERT(!iMessage.IsNull() && iMessage.Function() == EMcsCommandExt);
       
   799      TInt size(0);
       
   800      if( aError == KErrNone && aCommandResult)
       
   801          {
       
   802          delete iAsyncMedia;
       
   803          iAsyncMedia=NULL;
       
   804          TRAP(aError, iAsyncMedia=CMPXMedia::NewL(*aCommandResult));
       
   805          if (KErrNone==aError)
       
   806             {
       
   807             TRAP(aError, ::CreateBufferL<CMPXMedia>(*iAsyncMedia,iAsyncBuffer));
       
   808             }
       
   809 
       
   810          if (KErrNone==aError)
       
   811              {
       
   812              size = iAsyncBuffer->Size();
       
   813              }
       
   814          }
       
   815      TPckgC<TInt> sizePkg(size);
       
   816      CompleteAsync(aError, 2, &sizePkg);
       
   817      }
       
   818 
       
   819 // ----------------------------------------------------------------------------
       
   820 // Handle removing media by collection path
       
   821 // ----------------------------------------------------------------------------
       
   822 //
       
   823 void CMPXCollectionSession::HandleRemove(
       
   824     const CDesCArray& aUriArray,
       
   825     TInt aError)
       
   826     {
       
   827     MPX_ASSERT(!iMessage.IsNull() && iMessage.Function() == EMcsRemovePath);
       
   828     if( aError == KErrNone )
       
   829         {
       
   830         TRAPD(err, MPXUser::CreateBufferL(&aUriArray, iAsyncBuffer));
       
   831         aError = err == KErrNone ? iAsyncBuffer->Size() : err;
       
   832         }
       
   833     CompleteAsync(aError);
       
   834     }
       
   835 
       
   836 // ----------------------------------------------------------------------------
       
   837 // Handles find results
       
   838 // ----------------------------------------------------------------------------
       
   839 //
       
   840 void CMPXCollectionSession::HandleFindAll(CMPXMedia* aMedia, TInt aError)
       
   841     {
       
   842     MPX_ASSERT(!iMessage.IsNull() && iMessage.Function() == EMcsFindAll);
       
   843     if( aError == KErrNone && aMedia)
       
   844         {
       
   845         delete iAsyncMedia;
       
   846         iAsyncMedia=NULL;
       
   847         TRAPD(err, iAsyncMedia=CMPXMedia::NewL(*aMedia));
       
   848         if(KErrNone == err)
       
   849             {
       
   850             TRAP(err, ::CreateBufferL<CMPXMedia>(*iAsyncMedia,iAsyncBuffer));
       
   851             }
       
   852 
       
   853         aError = err == KErrNone ? iAsyncBuffer->Size() : err;
       
   854         }
       
   855     CompleteAsync(aError);
       
   856     }
       
   857 // End of file