upnpsharing/applicationengine/src/upnpcontentshareao.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2008 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:      CUpnpContentShareAO class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 
       
    25 #include "upnpcontentshareao.h"
       
    26 #include "upnpfilesharingengine.h"
       
    27 
       
    28 _LIT( KComponentLogfile, "applicationengine.txt");
       
    29 #include "upnplog.h"
       
    30 
       
    31 // --------------------------------------------------------------------------
       
    32 // CUpnpContentShareAO::CUpnpContentShareAO
       
    33 // C++ default constructor can NOT contain any code, that
       
    34 // might leave.
       
    35 // --------------------------------------------------------------------------
       
    36 //
       
    37 CUpnpContentShareAO::CUpnpContentShareAO( 
       
    38     CUPnPFileSharingEngine& aEngine,
       
    39     RUpnpContentServerClient& aContentServer ):
       
    40     CActive( CActive::EPriorityStandard ),
       
    41     iEngine( aEngine ),
       
    42     iContentServer( aContentServer )
       
    43     {
       
    44     CActiveScheduler::Add( this );
       
    45     }
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // CUpnpContentShareAO::ConstructL()
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 void CUpnpContentShareAO::ConstructL()
       
    52     {    
       
    53     __LOG( "CUpnpContentShareAO::ConstructL" );
       
    54     }
       
    55 
       
    56 // --------------------------------------------------------------------------
       
    57 // CUpnpContentShareAO::NewL
       
    58 // --------------------------------------------------------------------------
       
    59 //
       
    60 CUpnpContentShareAO* CUpnpContentShareAO::NewL( 
       
    61         CUPnPFileSharingEngine& aEngine, 
       
    62         RUpnpContentServerClient& aContentServer )
       
    63     {   
       
    64     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::NewL begin" );
       
    65     CUpnpContentShareAO* self = CUpnpContentShareAO::NewLC(
       
    66             aEngine, aContentServer );
       
    67     CleanupStack::Pop( self );
       
    68     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::NewL end" );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // --------------------------------------------------------------------------
       
    73 // CUpnpContentShareAO::NewLC
       
    74 // --------------------------------------------------------------------------
       
    75 //
       
    76 CUpnpContentShareAO* CUpnpContentShareAO::NewLC( 
       
    77         CUPnPFileSharingEngine& aEngine, 
       
    78         RUpnpContentServerClient& aContentServer )
       
    79     {    
       
    80     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::NewLC begin" );
       
    81     CUpnpContentShareAO* self = new( ELeave )
       
    82         CUpnpContentShareAO( aEngine, aContentServer );
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::NewLC end" );
       
    86     return self;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CUpnpContentShareAO::~CUpnpContentShareAO()
       
    91 // Destructor
       
    92 // --------------------------------------------------------------------------
       
    93 //    
       
    94 CUpnpContentShareAO::~CUpnpContentShareAO()
       
    95     {
       
    96     __LOG( "CUpnpContentShareAO::~CUpnpContentShareAO " );
       
    97 
       
    98     iQueuedSelections.ResetAndDestroy();
       
    99     iQueuedSelections.Close();
       
   100     iQueuedTypes.Close();
       
   101 
       
   102     Cancel();
       
   103     }
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CUpnpContentShareAO::ChangeSharedContentL()
       
   107 // Sends the selected indexes to server and starts sharing
       
   108 // --------------------------------------------------------------------------
       
   109 //
       
   110 void CUpnpContentShareAO::ChangeSharedContentL(
       
   111     UpnpContentServer::TUpnpMediaType aType,
       
   112     const CArrayFix<TInt>& aMarkedItems )
       
   113     {   
       
   114     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::\
       
   115 ChangeSharedContentL begin" );
       
   116 
       
   117     if ( !IsActive() )
       
   118         {
       
   119         __ASSERTD( iQueuedTypes.Count() == 0, __FILE__, __LINE__ );
       
   120         iContentServer.ChangeSharedContentL( aType, aMarkedItems, iStatus );
       
   121         iStatus = KRequestPending;
       
   122         SetActive();       
       
   123         }
       
   124     else
       
   125         {
       
   126         //remove all queued requests with this type
       
   127         RemoveQueuedObjectByType( aType );
       
   128         
       
   129         //queue this request
       
   130         CArrayFix<TInt>* selections = 
       
   131             new ( ELeave ) CArrayFixFlat<TInt>( aMarkedItems.Count() );
       
   132         CleanupStack::PushL( selections );
       
   133         for ( TInt i ( 0 ); i < aMarkedItems.Count(); i++ )
       
   134             {
       
   135             selections->AppendL( aMarkedItems.At(i) );
       
   136             }
       
   137         
       
   138         AppendQueuedObjectL( aType, selections );
       
   139         CleanupStack::Pop( selections );
       
   140         __ASSERTD( iQueuedSelections.Count() == iQueuedTypes.Count(),
       
   141                 __FILE__, __LINE__ );
       
   142         }
       
   143 
       
   144     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::\
       
   145 ChangeSharedContentL end" );
       
   146     }
       
   147 
       
   148 // --------------------------------------------------------------------------
       
   149 // CUpnpContentShareAO::RefreshSharedContentL()
       
   150 // Updates shared objects in mediaserver
       
   151 // --------------------------------------------------------------------------
       
   152 //
       
   153 void CUpnpContentShareAO::RefreshSharedContentL( 
       
   154     UpnpContentServer::TUpnpMediaType aType )
       
   155     {
       
   156     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::\
       
   157 RefreshSharedContentL begin" );
       
   158     if ( !IsActive() )
       
   159         {
       
   160         __ASSERTD( iQueuedTypes.Count() == 0, __FILE__, __LINE__ );
       
   161         iContentServer.RefreshSharedContentL( aType, iStatus );       
       
   162         iStatus = KRequestPending;
       
   163         SetActive();       
       
   164         }
       
   165     else
       
   166         {
       
   167         //remove all queued requests with this type
       
   168         RemoveQueuedObjectByType( aType );
       
   169 
       
   170         //queue this request
       
   171         CArrayFix<TInt>* selections( NULL );
       
   172         AppendQueuedObjectL( aType, selections );
       
   173         __ASSERTD( iQueuedSelections.Count() == iQueuedTypes.Count(),
       
   174                 __FILE__, __LINE__ );
       
   175         }
       
   176     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::\
       
   177 RefreshSharedContentL end" );
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CUpnpContentShareAO::DoCancel
       
   182 // Cancel the operation
       
   183 // --------------------------------------------------------------------------
       
   184 //
       
   185 void CUpnpContentShareAO::DoCancel()
       
   186     {    
       
   187     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::DoCancel" );
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CUpnpContentShareAO::RunL
       
   192 // Function is called when active request is ready
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 void CUpnpContentShareAO::RunL()
       
   196     {      
       
   197     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::RunL begin" );
       
   198     __LOG1( "[UPNP_ENGINE]\t\t status: %d", iStatus.Int() );
       
   199 
       
   200     TInt err = iStatus.Int();
       
   201     if ( err && 
       
   202          err != KErrServerBusy )
       
   203         {
       
   204         // KErrServerBusy is a response to the 
       
   205         // CUpnpContentServer::RefreshSharedContentL call. Therefore we must 
       
   206         // not leave without cleaning queued objects. 
       
   207         // Otherwise next call to CUpnpContentShareAO::RefreshSharedContentL 
       
   208         // will panic (in UDEB builds only!!!!). 
       
   209         
       
   210         // Clears queued objects and leaves.
       
   211         iQueuedSelections.ResetAndDestroy();
       
   212         iQueuedTypes.Reset();
       
   213         User::Leave( err );
       
   214         }
       
   215 
       
   216     //When get the responce from the content server then change the UI    
       
   217     iEngine.RequestSharingProgressL();
       
   218 
       
   219     if ( iQueuedTypes.Count() )
       
   220         {
       
   221         // Get next queued objects
       
   222         UpnpContentServer::TUpnpMediaType type = iQueuedTypes[ 0 ];
       
   223         CArrayFix<TInt>* selections = iQueuedSelections[ 0 ];
       
   224         
       
   225         // Clear arrays.
       
   226         iQueuedSelections.Remove( 0 );
       
   227         iQueuedTypes.Remove( 0 );
       
   228         
       
   229         if ( selections )
       
   230             {
       
   231             //share request
       
   232             CleanupStack::PushL( selections );
       
   233             iContentServer.ChangeSharedContentL( type, *selections, iStatus );
       
   234             CleanupStack::PopAndDestroy( selections );
       
   235             }
       
   236         else
       
   237             {
       
   238             //refresh request
       
   239             iContentServer.RefreshSharedContentL( type, iStatus );       
       
   240             }
       
   241         
       
   242         iStatus = KRequestPending;
       
   243         SetActive();       
       
   244         }
       
   245     __LOG( "[UPNP_ENGINE]\t CUpnpContentShareAO::RunL end" );
       
   246     }
       
   247 
       
   248 // --------------------------------------------------------------------------
       
   249 // CUpnpContentShareAO::RunError
       
   250 // Handles a leave occurring in the request completion
       
   251 // --------------------------------------------------------------------------
       
   252 //
       
   253 TInt CUpnpContentShareAO::RunError( TInt aError )
       
   254     {    
       
   255     __LOG( "CUpnpContentShareAO::RunError" );
       
   256     if ( iEngine.Observer() )
       
   257         {
       
   258         iEngine.Observer()->HandleSharingDone( iEngine, aError );
       
   259         }
       
   260     return KErrNone;
       
   261     }
       
   262 
       
   263 // --------------------------------------------------------------------------
       
   264 // CUpnpContentShareAO::RemoveQueuedObjects
       
   265 // Removed queued objects by index.
       
   266 // --------------------------------------------------------------------------
       
   267 //
       
   268 void CUpnpContentShareAO::RemoveQueuedObject( TInt aIndex )
       
   269     {
       
   270     // Checks argument in udeb build.
       
   271     // USER 130 panic will occure in urel builds if argument is
       
   272     // incorrent.
       
   273     TInt count = iQueuedTypes.Count();
       
   274     __ASSERTD( aIndex >= 0 && aIndex < iQueuedTypes.Count(),
       
   275             __FILE__, __LINE__ );
       
   276     
       
   277     iQueuedTypes.Remove( aIndex );
       
   278     if ( iQueuedSelections[ aIndex ] )
       
   279         {
       
   280         delete iQueuedSelections[ aIndex ];
       
   281         }
       
   282     iQueuedSelections.Remove( aIndex );
       
   283     }
       
   284 
       
   285 // --------------------------------------------------------------------------
       
   286 // CUpnpContentShareAO::RemoveQueuedObjectByType
       
   287 // Removed queued objects by type.
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 void CUpnpContentShareAO::RemoveQueuedObjectByType( 
       
   291         UpnpContentServer::TUpnpMediaType aType )
       
   292     {
       
   293     //remove all queued requests with this type
       
   294     for ( TInt i( iQueuedTypes.Count() - 1 ); i >= 0; i-- )
       
   295         {
       
   296         if ( iQueuedTypes[ i ] == aType )
       
   297             {
       
   298             RemoveQueuedObject( i );
       
   299             }
       
   300         }
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CUpnpContentShareAO::AppendQueuedObjectL
       
   305 // Appends given objects.
       
   306 // --------------------------------------------------------------------------
       
   307 //
       
   308 void CUpnpContentShareAO::AppendQueuedObjectL( 
       
   309         UpnpContentServer::TUpnpMediaType aType, 
       
   310         CArrayFix<TInt>* aSelections )
       
   311     {
       
   312     iQueuedTypes.AppendL( aType );
       
   313     TRAPD( err, iQueuedSelections.AppendL( aSelections ) );
       
   314     if ( err )
       
   315         {
       
   316         // Couldn't append array to the iQueuedSelections array.
       
   317         // Removes the type and leaves.
       
   318         iQueuedTypes.Remove( iQueuedTypes.Count() - 1 );
       
   319         User::Leave( err );
       
   320         }
       
   321     }