upnpsharing/upnpcontentserver/src/upnpcontentsharerao.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     1 /*
       
     2  * Copyright (c) 2006-2007 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:      CUpnpContentSharerAo class implementation
       
    15  *
       
    16  */
       
    17 //  Include Files
       
    18 
       
    19 #include "upnpcontentsharerao.h"
       
    20 #include "upnpcontentserverdefs.h"
       
    21 #include "upnpselectionwriter.h"
       
    22 
       
    23 _LIT( KComponentLogfile, "contentserver.txt");
       
    24 #include "upnplog.h"
       
    25 
       
    26 // FORWARD DECLARATIONS
       
    27 // CONSTANTS
       
    28 
       
    29 const TInt KPercent = 100;
       
    30 
       
    31 using namespace UpnpContentServer;
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS =============================
       
    34 
       
    35 // --------------------------------------------------------------------------
       
    36 // CUpnpContentSharerAo::CUpnpContentSharerAo
       
    37 // C++ default constructor can NOT contain any code, that
       
    38 // might leave.
       
    39 // --------------------------------------------------------------------------
       
    40 //
       
    41 CUpnpContentSharerAo::CUpnpContentSharerAo( 
       
    42     MUpnpSharingCallback* aEngine,
       
    43     MUpnpSharingAlgorithm* aSharingAlgorithm ) 
       
    44     :
       
    45     CActive(CActive::EPriorityIdle),
       
    46     iSharingRequest( NULL )
       
    47     {
       
    48     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    49 
       
    50     iSharingEngine = aEngine;
       
    51     iSharingAlgorithm = aSharingAlgorithm;
       
    52 
       
    53     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    54     }
       
    55 
       
    56 // --------------------------------------------------------------------------
       
    57 // CUpnpContentSharerAo::NewL
       
    58 // Two-phased constructor.
       
    59 // --------------------------------------------------------------------------
       
    60 //
       
    61 CUpnpContentSharerAo* CUpnpContentSharerAo::NewL( 
       
    62         MUpnpSharingCallback* aEngine,
       
    63         MUpnpSharingAlgorithm* aSharingAlgorithm )
       
    64     {
       
    65     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    66 
       
    67     CUpnpContentSharerAo* self = 
       
    68         new (ELeave) CUpnpContentSharerAo( aEngine, aSharingAlgorithm );
       
    69     CleanupStack::PushL(self);
       
    70     self->ConstructL();
       
    71     CleanupStack::Pop(self);
       
    72     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------
       
    77 // CUpnpContentSharerAo::ConstructL
       
    78 // Symbian 2nd phase constructor can leave.
       
    79 // --------------------------------------------------------------------------
       
    80 //
       
    81 void CUpnpContentSharerAo::ConstructL()
       
    82     {
       
    83     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    84     
       
    85     CActiveScheduler::Add(this);
       
    86 
       
    87     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
    88     }
       
    89 
       
    90 // --------------------------------------------------------------------------
       
    91 // CUpnpContentSharerAo::~CUpnpContentSharerAo
       
    92 // Destructor
       
    93 // --------------------------------------------------------------------------
       
    94 //
       
    95 CUpnpContentSharerAo::~CUpnpContentSharerAo()
       
    96     {
       
    97     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
    98 
       
    99     Cancel();
       
   100     if ( iIdle )
       
   101         {
       
   102         delete iIdle;
       
   103         iIdle = NULL;
       
   104         }
       
   105 
       
   106     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CUpnpContentSharerAo::RunL
       
   111 // Called when asyncronous request is ready
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 
       
   115 void CUpnpContentSharerAo::RunL()
       
   116     {
       
   117     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   118 
       
   119     __LOG8_1( "CUpnpContentSharerAo::RunL, iStatus=%d", iStatus.Int() );
       
   120     
       
   121     if ( iCancel )
       
   122         {
       
   123         // if Cancel wanted, then put iError as KErrCancel
       
   124         iError = KErrCancel;
       
   125         }    
       
   126     else if ( iStatus.Int() == KErrCorrupt || 
       
   127             iStatus.Int() == KErrDiskFull || 
       
   128             iStatus.Int() == KErrNoMemory )
       
   129         {
       
   130         // if writing sharing status failed or diskspace is full or no 
       
   131         // memory, then stop the process 
       
   132         iError = iStatus.Int();
       
   133         }
       
   134                
       
   135     if( !iError && 
       
   136         ( iShareIndex < iSharingRequest->iShareArr->Count() ||
       
   137           iUnshareIndex < iSharingRequest->iUnshareArr->Count() ) )
       
   138         {
       
   139         // handle next single file share or unshare
       
   140         TInt err = DoSingleOperation();
       
   141         
       
   142         // set progress so that Ui knows the current sharing progress.
       
   143         SetProgressL(); 
       
   144         
       
   145         //start another round
       
   146         TRequestStatus* status = &iStatus; 
       
   147         User::RequestComplete( status, err );
       
   148         SetActive();
       
   149         }
       
   150     else
       
   151         { 
       
   152         //we are done, process complete using CIdle
       
   153         if ( iIdle )
       
   154             {
       
   155             delete iIdle;
       
   156             iIdle = NULL;
       
   157             }
       
   158         iIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   159         iIdle->Start( TCallBack( CompleteL, this ) );
       
   160         }
       
   161     
       
   162     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CUpnpContentSharerAo::DoSingleOperation
       
   167 // Handles one file sharing or unsharing
       
   168 // --------------------------------------------------------------------------
       
   169 //
       
   170 TInt CUpnpContentSharerAo::DoSingleOperation()
       
   171     {
       
   172     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   173     TInt err = KErrNone;
       
   174     
       
   175     // first we go through the whole share list and then unsharing 
       
   176     if ( iShareIndex < iSharingRequest->iShareArr->Count() )
       
   177         {
       
   178         
       
   179         TFileName fileName = (*(iSharingRequest->iShareArr))[iShareIndex];
       
   180         TRAP( err, iSharingAlgorithm->ShareFileL( fileName ) );
       
   181         iShareIndex++;
       
   182         if (err != KErrNone)
       
   183             {
       
   184             // Sharing failed for some reason, not doing break here.
       
   185             // Just log and try next one.
       
   186             __LOG8_1( "Sharing item error: ", err );
       
   187             }
       
   188         }
       
   189     
       
   190     // share list done, next process the unshare list one by one 
       
   191     if ( iShareIndex == iSharingRequest->iShareArr->Count() &&
       
   192             iUnshareIndex < iSharingRequest->iUnshareArr->Count() )
       
   193         {
       
   194         TFileName fileName = 
       
   195             (*(iSharingRequest->iUnshareArr))[iUnshareIndex];
       
   196         TRAP( err, iSharingAlgorithm->UnshareFileL( fileName ) );
       
   197         iUnshareIndex++;
       
   198         if (err != KErrNone)
       
   199             {
       
   200             // Unsharing failed for some reason, not doing break here.
       
   201             // Just log and try next one.
       
   202             __LOG8_1( "Unsharing item error: ", err );
       
   203             }
       
   204         }
       
   205     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   206     return err;
       
   207     }
       
   208 // --------------------------------------------------------------------------
       
   209 // CUpnpContentSharerAo::DoCancel
       
   210 // Cancels active object
       
   211 // --------------------------------------------------------------------------
       
   212 //
       
   213 void CUpnpContentSharerAo::DoCancel()
       
   214     {
       
   215     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   216 
       
   217     // do nothing
       
   218     
       
   219     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   220     }
       
   221 
       
   222 // --------------------------------------------------------------------------
       
   223 // CUpnpContentSharerAo::RunError
       
   224 // Cancels active object
       
   225 // --------------------------------------------------------------------------
       
   226 //
       
   227 TInt CUpnpContentSharerAo::RunError(TInt aError)
       
   228     {
       
   229     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   230     __LOG8_1( "RunError = ", aError );
       
   231     
       
   232     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   233     return KErrNone;
       
   234     }
       
   235 
       
   236 // --------------------------------------------------------------------------
       
   237 // CUpnpContentSharerAo::StartSharing
       
   238 // Starts file sharing
       
   239 // --------------------------------------------------------------------------
       
   240 //
       
   241 void CUpnpContentSharerAo::StartSharing( 
       
   242     const CUpnpSharingRequest* aSharingRequest )
       
   243     {
       
   244     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   245 
       
   246     if ( IsActive() )
       
   247         {
       
   248         Cancel();
       
   249         }
       
   250     
       
   251     // initialize values for variables
       
   252     iSharingRequest = aSharingRequest;
       
   253     iCancel = EFalse;
       
   254     iError = KErrNone;
       
   255     iShareIndex = 0;
       
   256     iUnshareIndex = 0;
       
   257     iCurrentProgressPercent = 0;
       
   258     
       
   259     TInt err( KErrNone );
       
   260     if ( iSharingRequest->iShareArr && iSharingRequest->iUnshareArr )
       
   261         {
       
   262         __LOG2( "CUpnpContentSharerAo::StartSharing \
       
   263             shareArr.Count=%d, unshareArr.Count=%d", 
       
   264             iSharingRequest->iShareArr->Count(),
       
   265             iSharingRequest->iUnshareArr->Count() );
       
   266 
       
   267         // write the sharing status to cenrep and clf ids to file
       
   268         TRAP( err, WriteSharingStatusL() );
       
   269 
       
   270         // if writing sharing status failed, map all errors to KErrCorrupt
       
   271         // so that RunL handles the error
       
   272         if ( err )
       
   273             {
       
   274             err = KErrCorrupt;
       
   275             }
       
   276         }
       
   277     else
       
   278         {
       
   279         err = KErrCorrupt;
       
   280         }
       
   281 
       
   282     TRequestStatus* status = &iStatus;
       
   283     User::RequestComplete( status, err );
       
   284 
       
   285     SetActive();
       
   286     
       
   287     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   288     }
       
   289 
       
   290 // --------------------------------------------------------------------------
       
   291 // CUpnpContentSharerAo::SetProgressL
       
   292 // Sets progress to engine
       
   293 // --------------------------------------------------------------------------
       
   294 //
       
   295 void CUpnpContentSharerAo::SetProgressL()
       
   296     {
       
   297     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   298     TInt progress(0);
       
   299     
       
   300     // Summarize total count of shared and unshared files
       
   301     TInt totalCount = iSharingRequest->iShareArr->Count() + 
       
   302                             iSharingRequest->iUnshareArr->Count();
       
   303     // Find out how many of the files are processed already 
       
   304     TInt currentCount = iShareIndex + iUnshareIndex;
       
   305     
       
   306     // No div by zero
       
   307     if( totalCount > 0 )
       
   308         {
       
   309         // calculate percent for sharing progress
       
   310         progress = currentCount * KPercent / totalCount ;
       
   311         }
       
   312     
       
   313     if ( iCurrentProgressPercent != progress )
       
   314         {
       
   315         // Set the progress information
       
   316         iCurrentProgressPercent = progress;
       
   317         iSharingEngine->SetProgressL( progress );
       
   318         }
       
   319     else
       
   320         {
       
   321         // progress percent not changed, no need to set it
       
   322         }
       
   323     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   324     }
       
   325 
       
   326 // --------------------------------------------------------------------------
       
   327 // CUpnpContentSharerAo::WriteSharingStatusL
       
   328 // Writes the sharing status to cenrep and files
       
   329 // --------------------------------------------------------------------------
       
   330 //
       
   331 void CUpnpContentSharerAo::WriteSharingStatusL()
       
   332     {
       
   333     __LOG8_1( "%s start.", __PRETTY_FUNCTION__ );
       
   334 
       
   335     // Create selection writer
       
   336     CUpnpSelectionWriter* writer = 
       
   337             CUpnpSelectionWriter::NewL( iSharingRequest->iMediaType );
       
   338     CleanupStack::PushL( writer );
       
   339     
       
   340     // if sharing request is EShareMany, append clf ids to writer
       
   341     if( iSharingRequest->iSharingType == EShareMany )
       
   342         {
       
   343         if ( iSharingRequest->iClfIds )
       
   344             {
       
   345             for ( TInt i(0); i < iSharingRequest->iClfIds->MdcaCount(); i++ )
       
   346                 {
       
   347                 TPtrC collId = iSharingRequest->iClfIds->MdcaPoint(i);
       
   348                 writer->AppendItemL( collId );
       
   349                 }
       
   350             }
       
   351         else
       
   352             {
       
   353             // no clf ids, leave
       
   354             User::Leave( KErrCorrupt );
       
   355             }
       
   356         }
       
   357     
       
   358     // write sharing status to cenrep and/or file
       
   359     writer->SaveSharingStateL( iSharingRequest->iSharingType );
       
   360     
       
   361     CleanupStack::PopAndDestroy( writer );
       
   362     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   363     }
       
   364 
       
   365 // --------------------------------------------------------------------------
       
   366 // CUpnpContentSharerAo::SharingOngoing
       
   367 // Checks whether sharing is ongoing or not.
       
   368 // --------------------------------------------------------------------------
       
   369 //
       
   370 TBool CUpnpContentSharerAo::SharingOngoing()
       
   371     {
       
   372     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   373     TBool ret(EFalse); 
       
   374     if ( iSharingRequest && 
       
   375         iSharingRequest->iShareArr &&
       
   376         iSharingRequest->iUnshareArr )
       
   377         {
       
   378         TInt totalCount = iSharingRequest->iShareArr->Count() + 
       
   379                                 iSharingRequest->iUnshareArr->Count();
       
   380         TInt currentCount = iShareIndex + iUnshareIndex;
       
   381         if ( currentCount < totalCount )
       
   382             {
       
   383             // sharing still ongoing
       
   384             ret = ETrue;
       
   385             }
       
   386         }
       
   387     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   388     return ret;
       
   389     }
       
   390 
       
   391 // --------------------------------------------------------------------------
       
   392 // CUpnpContentSharerAo::StopSharing
       
   393 // Stops the sharing.
       
   394 // --------------------------------------------------------------------------
       
   395 //
       
   396 void CUpnpContentSharerAo::StopSharing()
       
   397     {
       
   398     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   399 
       
   400     iCancel = ETrue;
       
   401 
       
   402     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   403     }
       
   404     
       
   405 // --------------------------------------------------------------------------
       
   406 // CUpnpContentSharerAo::CompleteL
       
   407 // Operation complete.
       
   408 // --------------------------------------------------------------------------
       
   409 //
       
   410 TInt CUpnpContentSharerAo::CompleteL( TAny* aPtr )
       
   411     {
       
   412     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   413 
       
   414     CUpnpContentSharerAo* sharer = 
       
   415         static_cast<CUpnpContentSharerAo*>( aPtr );
       
   416     // callback for operation completed.
       
   417     sharer->iSharingEngine->CompleteSharingOperationL( sharer->iError, 
       
   418         sharer->iSharingRequest->iMediaType );
       
   419 
       
   420     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   421     return KErrNone;
       
   422     }
       
   423 
       
   424 // --------------------------------------------------------------------------
       
   425 // CUpnpContentSharerAo::ForceSetProgress
       
   426 // Forces itself to set progress in next file operation
       
   427 // --------------------------------------------------------------------------
       
   428 //
       
   429 void CUpnpContentSharerAo::ForceSetProgress()
       
   430     {
       
   431     __LOG8_1( "%s begin.", __PRETTY_FUNCTION__ );
       
   432 
       
   433     // let's change the value of the current sharing progress so that 
       
   434     // the progress property is set in next file sharing operation.
       
   435     iCurrentProgressPercent--;
       
   436 
       
   437     __LOG8_1( "%s end.", __PRETTY_FUNCTION__ );
       
   438     }
       
   439     
       
   440 // End of file