phonebookui/Phonebook2/ServerApplication/src/CPbk2ContactRingingToneAssigner.cpp
changeset 0 e686773b3f54
child 68 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Phonebook 2 contact ringing tone assigner.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactRingingToneAssigner.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ContactAssignerObserver.h"
       
    23 #include <CPbk2DrmManager.h>
       
    24 #include <Pbk2ServerApp.rsg>
       
    25 
       
    26 // Virtual Phonebook
       
    27 #include <MVPbkStoreContact.h>
       
    28 #include <MVPbkContactFieldTextData.h>
       
    29 
       
    30 // System includes
       
    31 #include <centralrepository.h>
       
    32 #include <ProfileEngineDomainCRKeys.h> // KProEngRingingToneMaxSize
       
    33 #include <f32file.h>
       
    34 #include <StringLoader.h>
       
    35 #include <aknnotewrappers.h>
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // CPbk2ContactRingingToneAssigner::CPbk2ContactRingingToneAssigner
       
    39 // --------------------------------------------------------------------------
       
    40 //
       
    41 CPbk2ContactRingingToneAssigner::CPbk2ContactRingingToneAssigner
       
    42         ( MPbk2ContactAssignerObserver& aObserver ):
       
    43             CActive( EPriorityIdle ), iObserver( aObserver ),
       
    44             iIndex( KErrNotSupported )
       
    45     {
       
    46     CActiveScheduler::Add( this );
       
    47     }
       
    48 
       
    49 // --------------------------------------------------------------------------
       
    50 // CPbk2ContactRingingToneAssigner::~CPbk2ContactRingingToneAssigner
       
    51 // --------------------------------------------------------------------------
       
    52 //
       
    53 CPbk2ContactRingingToneAssigner::~CPbk2ContactRingingToneAssigner()
       
    54     {
       
    55     Cancel();
       
    56     delete iDrmManager;
       
    57     }
       
    58 
       
    59 // --------------------------------------------------------------------------
       
    60 // CPbk2ContactRingingToneAssigner::NewL
       
    61 // --------------------------------------------------------------------------
       
    62 //
       
    63 CPbk2ContactRingingToneAssigner* CPbk2ContactRingingToneAssigner::NewL
       
    64         ( MPbk2ContactAssignerObserver& aObserver )
       
    65     {
       
    66     CPbk2ContactRingingToneAssigner* self =
       
    67         new ( ELeave ) CPbk2ContactRingingToneAssigner( aObserver );
       
    68     CleanupStack::PushL( self );
       
    69     self->ConstructL();
       
    70     CleanupStack::Pop( self );
       
    71     return self;
       
    72     }
       
    73 
       
    74 // --------------------------------------------------------------------------
       
    75 // CPbk2ContactRingingToneAssigner::ConstructL
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 void CPbk2ContactRingingToneAssigner::ConstructL()
       
    79     {
       
    80     iDrmManager = CPbk2DrmManager::NewL();
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------
       
    84 // CPbk2ContactRingingToneAssigner::AssignDataL
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 void CPbk2ContactRingingToneAssigner::AssignDataL
       
    88         ( MVPbkStoreContact& aStoreContact,
       
    89           MVPbkStoreContactField* aContactField,
       
    90           const MVPbkFieldType* aFieldType, const HBufC* aDataBuffer )
       
    91     {
       
    92     TBool isProtected( ETrue );
       
    93     TInt error = PassesDrmCheckL( aDataBuffer, isProtected );
       
    94     
       
    95     if ( !isProtected && error == KErrNone && PassesSizeCheckL( aDataBuffer ) )
       
    96         {
       
    97         if ( !aContactField )
       
    98             {
       
    99             MVPbkStoreContactField* field =
       
   100                 aStoreContact.CreateFieldLC( *aFieldType );
       
   101             InsertDataL( *field, *aDataBuffer );
       
   102             CleanupStack::Pop(); // field
       
   103             iIndex = aStoreContact.AddFieldL( field ); // takes ownership
       
   104             }
       
   105         else
       
   106             {
       
   107             InsertDataL( *aContactField, *aDataBuffer );
       
   108             }
       
   109 
       
   110         iState = EStopping;
       
   111         // Notify observer asynchronously
       
   112         IssueRequest();
       
   113         }
       
   114     else
       
   115         {
       
   116         // Notify observer asynchronously
       
   117         iState = EDrmProtected;
       
   118         iError = error;
       
   119         IssueRequest();
       
   120         }
       
   121     }
       
   122 
       
   123 // --------------------------------------------------------------------------
       
   124 // CPbk2ContactRingingToneAssigner::AssignAttributeL
       
   125 // --------------------------------------------------------------------------
       
   126 //
       
   127 void CPbk2ContactRingingToneAssigner::AssignAttributeL
       
   128         ( MVPbkStoreContact& /*aStoreContact*/,
       
   129           MVPbkStoreContactField* /*aContactField*/,
       
   130           TPbk2AttributeAssignData /*aAttributeAssignData*/ )
       
   131     {
       
   132     // Not supported
       
   133     User::Leave( KErrNotSupported );
       
   134     }
       
   135 
       
   136 // --------------------------------------------------------------------------
       
   137 // CPbk2ContactRingingToneAssigner::RunL
       
   138 // --------------------------------------------------------------------------
       
   139 //
       
   140 void CPbk2ContactRingingToneAssigner::RunL()
       
   141     {
       
   142     switch ( iState )
       
   143         {
       
   144         case EStopping:
       
   145             {
       
   146             if ( iError == KErrNone )
       
   147                 {
       
   148                 // Just notify the observer
       
   149                 iObserver.AssignComplete( *this, iIndex );
       
   150                 }
       
   151             else
       
   152                 {
       
   153                 iObserver.AssignFailed( *this, iError );
       
   154                 }
       
   155             break;
       
   156             }
       
   157         case EDrmProtected:
       
   158             {
       
   159             iObserver.AssignFailed( *this, iError );
       
   160             break;
       
   161             }            
       
   162         default:
       
   163             {
       
   164             // Do nothing
       
   165             break;
       
   166             }
       
   167         }        
       
   168     }
       
   169 
       
   170 // --------------------------------------------------------------------------
       
   171 // CPbk2ContactRingingToneAssigner::DoCancel
       
   172 // --------------------------------------------------------------------------
       
   173 //
       
   174 void CPbk2ContactRingingToneAssigner::DoCancel()
       
   175     {
       
   176     // Nothing to do
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CPbk2ContactRingingToneAssigner::RunError
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 TInt CPbk2ContactRingingToneAssigner::RunError( TInt /*aError*/ )
       
   184     {
       
   185     // No leaving code in RunL
       
   186     return KErrNone;
       
   187     }
       
   188 
       
   189 // --------------------------------------------------------------------------
       
   190 // CPbk2ContactRingingToneAssigner::InsertDataL
       
   191 // --------------------------------------------------------------------------
       
   192 //
       
   193 void CPbk2ContactRingingToneAssigner::InsertDataL
       
   194         ( MVPbkStoreContactField& aField, const HBufC& aDataBuffer )
       
   195     {
       
   196     MVPbkContactFieldTextData::Cast( aField.FieldData() ).
       
   197         SetTextL( aDataBuffer );
       
   198     }
       
   199 
       
   200 // --------------------------------------------------------------------------
       
   201 // CPbk2ContactRingingToneAssigner::IssueRequest
       
   202 // --------------------------------------------------------------------------
       
   203 //
       
   204 void CPbk2ContactRingingToneAssigner::IssueRequest()
       
   205     {
       
   206     TRequestStatus* status = &iStatus;
       
   207     User::RequestComplete( status, KErrNone );
       
   208     SetActive();
       
   209     }
       
   210 
       
   211 // --------------------------------------------------------------------------
       
   212 // CPbk2ContactImageAssigner::PassesDrmCheckL
       
   213 // --------------------------------------------------------------------------
       
   214 //
       
   215 TInt CPbk2ContactRingingToneAssigner::PassesDrmCheckL( 
       
   216         const HBufC* aDataBuffer,
       
   217         TBool& aIsProtected )
       
   218     {
       
   219     aIsProtected = ETrue;
       
   220     TInt error( KErrNone );
       
   221 
       
   222     if ( aDataBuffer )
       
   223         {
       
   224         error = iDrmManager->IsRingingToneForbidden( *aDataBuffer, aIsProtected );
       
   225         }
       
   226 
       
   227     return error;
       
   228     }    
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CPbk2ContactImageAssigner::PassesSizeCheckL
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 TBool CPbk2ContactRingingToneAssigner::PassesSizeCheckL
       
   235         ( const HBufC* aDataBuffer )
       
   236     {
       
   237     TBool dataInvalid = EFalse;
       
   238 
       
   239     if ( aDataBuffer )
       
   240         {
       
   241         TInt sizeLimit = KErrNone;
       
   242         MaxToneFileSizeL( sizeLimit );
       
   243         TInt rtSizeLimitError = CheckFileSizeLimit
       
   244             ( *aDataBuffer, sizeLimit );
       
   245         if ( rtSizeLimitError )
       
   246             {
       
   247             dataInvalid = ETrue;
       
   248             }
       
   249         }
       
   250 
       
   251     return !dataInvalid;
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CPbk2ContactRingingToneAssigner::MaxToneFileSizeL
       
   256 // --------------------------------------------------------------------------
       
   257 //
       
   258 void CPbk2ContactRingingToneAssigner::MaxToneFileSizeL
       
   259         ( TInt& aMaxSizeKB ) const
       
   260     {
       
   261     CRepository* cenrep = CRepository::NewL( KCRUidProfileEngine );
       
   262     CleanupStack::PushL( cenrep );
       
   263     TInt error = cenrep->Get( KProEngRingingToneMaxSize, aMaxSizeKB );
       
   264     CleanupStack::PopAndDestroy( cenrep );
       
   265     if ( error != KErrNone )
       
   266         {
       
   267         aMaxSizeKB = 0;
       
   268         }
       
   269     if ( aMaxSizeKB < 0 )
       
   270         {
       
   271         aMaxSizeKB = 0;
       
   272         }
       
   273     }
       
   274 
       
   275 // --------------------------------------------------------------------------
       
   276 // CPbk2ContactRingingToneAssigner::CheckFileSizeLimitL
       
   277 // --------------------------------------------------------------------------
       
   278 //
       
   279 TInt CPbk2ContactRingingToneAssigner::CheckFileSizeLimit
       
   280         ( const TDesC& aFileName, TInt aSizeLimit )
       
   281     {
       
   282     TInt error = KErrNone;
       
   283 
       
   284     if ( aSizeLimit )
       
   285         {
       
   286         if ( CheckToneFileSize( aFileName, aSizeLimit) != KErrNone )
       
   287             {
       
   288             TRAP_IGNORE(
       
   289                 ShowSizeErrorNoteL( aSizeLimit ) );
       
   290             error = KErrTooBig;
       
   291             }
       
   292         }
       
   293 
       
   294     return error;
       
   295     }
       
   296 
       
   297 // --------------------------------------------------------------------------
       
   298 // CPbk2ContactRingingToneAssigner::CheckToneFileSizeL
       
   299 // --------------------------------------------------------------------------
       
   300 //
       
   301 TInt CPbk2ContactRingingToneAssigner::CheckToneFileSize
       
   302         ( const TDesC& aFile, TInt aSizeLimitKB )
       
   303     {
       
   304     // Get file size
       
   305     TInt size = 0;
       
   306     TInt error = KErrNone;
       
   307     RFs fs;
       
   308     error = fs.Connect();
       
   309     if ( !error )
       
   310         {
       
   311         TEntry entry;
       
   312         error = fs.Entry( aFile, entry );
       
   313         if ( !error )
       
   314             {
       
   315             size = entry.iSize;
       
   316             }
       
   317         fs.Close();
       
   318         }
       
   319 
       
   320     // Note: if file size can't be determined, the check fails
       
   321     aSizeLimitKB *= KKilo;
       
   322     if ( aSizeLimitKB  &&  size > aSizeLimitKB )
       
   323         {
       
   324         error = KErrTooBig;
       
   325         }
       
   326 
       
   327     return error;
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CPbk2ContactRingingToneAssigner::ShowSizeErrorNoteL
       
   332 // --------------------------------------------------------------------------
       
   333 //
       
   334 void CPbk2ContactRingingToneAssigner::ShowSizeErrorNoteL
       
   335         ( TInt aSizeLimitKB ) const
       
   336     {
       
   337     HBufC* errorText = StringLoader::LoadLC
       
   338         ( R_PROFILE_TEXT_TONE_MAXSIZE_ERROR, aSizeLimitKB );
       
   339     CAknInformationNote* note = new ( ELeave ) CAknInformationNote( ETrue );
       
   340     note->ExecuteLD( *errorText );
       
   341     CleanupStack::PopAndDestroy( errorText );
       
   342     }
       
   343 
       
   344 // End of File