phonebookui/Phonebook2/ServerApplication/src/CPbk2ContactImageAssigner.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 image assigner.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactImageAssigner.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ContactAssignerObserver.h"
       
    23 #include "Pbk2AssignValidateField.h"
       
    24 #include <CPbk2ImageManager.h>
       
    25 #include <CPbk2DrmManager.h>
       
    26 #include <CPbk2PresentationContact.h>
       
    27 #include <CPbk2FieldPropertyArray.h>
       
    28 
       
    29 // Virtual Phonebook
       
    30 #include <CVPbkContactManager.h>
       
    31 #include <MVPbkFieldType.h>
       
    32 #include <MVPbkContactFieldTextData.h>
       
    33 #include <VPbkEng.rsg>
       
    34 
       
    35 // System includes
       
    36 #include <featmgr.h>
       
    37 
       
    38 /// Unnamed namespace for local definitions
       
    39 namespace {
       
    40 
       
    41 #ifdef _DEBUG
       
    42 
       
    43 enum TPanicCode
       
    44     {
       
    45     EInvalidFieldIndex,
       
    46     };
       
    47 
       
    48 void Panic(TPanicCode aReason)
       
    49     {
       
    50     _LIT( KPanicText, "CPbk2ContactImageAssigner" );
       
    51     User::Panic( KPanicText, aReason );
       
    52     }
       
    53 #endif //_DEBUG
       
    54 
       
    55 } /// namespace
       
    56 
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // CPbk2ContactImageAssigner::CPbk2ContactImageAssigner
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62 CPbk2ContactImageAssigner::CPbk2ContactImageAssigner
       
    63         ( MPbk2ContactAssignerObserver& aObserver,
       
    64           CVPbkContactManager& aContactManager,
       
    65           CPbk2FieldPropertyArray& aFieldProperties ):
       
    66             CActive( EPriorityIdle ), iObserver( aObserver ),
       
    67             iContactManager( aContactManager ),
       
    68             iFieldProperties( aFieldProperties ),
       
    69             iIndex( KErrNotSupported )
       
    70     {
       
    71     CActiveScheduler::Add( this );
       
    72     }
       
    73 
       
    74 // --------------------------------------------------------------------------
       
    75 // CPbk2ContactImageAssigner::~CPbk2ContactImageAssigner
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 CPbk2ContactImageAssigner::~CPbk2ContactImageAssigner()
       
    79     {
       
    80     Cancel();
       
    81     delete iImageOperation;
       
    82     delete iImageManager;
       
    83     delete iDrmManager;
       
    84     }
       
    85 
       
    86 // --------------------------------------------------------------------------
       
    87 // CPbk2ContactImageAssigner::NewL
       
    88 // --------------------------------------------------------------------------
       
    89 //
       
    90 CPbk2ContactImageAssigner* CPbk2ContactImageAssigner::NewL
       
    91         ( MPbk2ContactAssignerObserver& aObserver,
       
    92           CVPbkContactManager& aContactManager,
       
    93           CPbk2FieldPropertyArray& aFieldProperties )
       
    94     {
       
    95     CPbk2ContactImageAssigner* self =
       
    96         new ( ELeave ) CPbk2ContactImageAssigner
       
    97             ( aObserver, aContactManager, aFieldProperties );
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL( aContactManager );
       
   100     CleanupStack::Pop( self );
       
   101     return self;
       
   102     }
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // CPbk2ContactImageAssigner::ConstructL
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 void CPbk2ContactImageAssigner::ConstructL
       
   109         ( CVPbkContactManager& aContactManager )
       
   110     {
       
   111     iImageManager = CPbk2ImageManager::NewL( aContactManager );
       
   112     iDrmManager = CPbk2DrmManager::NewL();
       
   113     }
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // CPbk2ContactImageAssigner::AssignDataL
       
   117 // --------------------------------------------------------------------------
       
   118 //
       
   119 void CPbk2ContactImageAssigner::AssignDataL
       
   120         ( MVPbkStoreContact& aStoreContact,
       
   121           MVPbkStoreContactField* /*aContactField*/,
       
   122           const MVPbkFieldType* aFieldType, const HBufC* aDataBuffer )
       
   123     {
       
   124     iContact = &aStoreContact;
       
   125     iFieldType = aFieldType;
       
   126     iDataBuffer = aDataBuffer;
       
   127 
       
   128     iState = EAssigningImage;
       
   129     IssueRequest();
       
   130     }
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // CPbk2ContactImageAssigner::AssignAttributeL
       
   134 // --------------------------------------------------------------------------
       
   135 //
       
   136 void CPbk2ContactImageAssigner::AssignAttributeL
       
   137         ( MVPbkStoreContact& /*aStoreContact*/,
       
   138           MVPbkStoreContactField* /*aContactField*/,
       
   139           TPbk2AttributeAssignData /*aAttributeAssignData*/ )
       
   140     {
       
   141     // Not supported
       
   142     User::Leave( KErrNotSupported );
       
   143     }
       
   144 
       
   145 // --------------------------------------------------------------------------
       
   146 // CPbk2ContactImageAssigner::RunL
       
   147 // --------------------------------------------------------------------------
       
   148 //
       
   149 void CPbk2ContactImageAssigner::RunL()
       
   150     {
       
   151    switch ( iState )
       
   152         {
       
   153         case EAssigningImage:
       
   154             {
       
   155             AssignImageL();
       
   156             break;
       
   157             }
       
   158         case EAssigningImageFileName:
       
   159             {
       
   160             AssignImageFileNameL();
       
   161             break;
       
   162             }
       
   163         case EStopping:
       
   164             {
       
   165             if ( iError == KErrNone )
       
   166                 {
       
   167                 iObserver.AssignComplete( *this, iIndex );
       
   168                 }
       
   169             else
       
   170                 {
       
   171                 iObserver.AssignFailed( *this, iError );
       
   172                 }
       
   173             break;
       
   174             }
       
   175         case EDrmProtected:
       
   176             {
       
   177             iObserver.AssignFailed( *this, iError );
       
   178             break;
       
   179             }            
       
   180         default:
       
   181             {
       
   182             // Do nothing
       
   183             break;
       
   184             }
       
   185         }
       
   186     }
       
   187 
       
   188 // --------------------------------------------------------------------------
       
   189 // CPbk2ContactImageAssigner::DoCancel
       
   190 // --------------------------------------------------------------------------
       
   191 //
       
   192 void CPbk2ContactImageAssigner::DoCancel()
       
   193     {
       
   194     // Nothing to do
       
   195     }
       
   196 
       
   197 // --------------------------------------------------------------------------
       
   198 // CPbk2ContactImageAssigner::RunError
       
   199 // --------------------------------------------------------------------------
       
   200 //
       
   201 TInt CPbk2ContactImageAssigner::RunError( TInt aError )
       
   202     {
       
   203     iObserver.AssignFailed( *this, aError );
       
   204     return KErrNone;
       
   205     }
       
   206 
       
   207 // --------------------------------------------------------------------------
       
   208 // CPbk2ContactImageAssigner::Pbk2ImageSetComplete
       
   209 // --------------------------------------------------------------------------
       
   210 //
       
   211 void CPbk2ContactImageAssigner::Pbk2ImageSetComplete
       
   212         ( MPbk2ImageOperation& /*aOperation*/ )
       
   213     {
       
   214     delete iImageOperation;
       
   215     iImageOperation = NULL;
       
   216 
       
   217     iState = EAssigningImageFileName;
       
   218     IssueRequest();
       
   219     }
       
   220 
       
   221 // --------------------------------------------------------------------------
       
   222 // CPbk2ContactImageAssigner::Pbk2ImageSetFailed
       
   223 // --------------------------------------------------------------------------
       
   224 //
       
   225 void CPbk2ContactImageAssigner::Pbk2ImageSetFailed
       
   226         ( MPbk2ImageOperation& /*aOperation*/, TInt aError )
       
   227     {
       
   228     delete iImageOperation;
       
   229     iImageOperation = NULL;
       
   230 
       
   231     iState = EStopping;
       
   232     iError = aError;
       
   233     IssueRequest();
       
   234     }
       
   235 
       
   236 // --------------------------------------------------------------------------
       
   237 // CPbk2ContactImageAssigner::PassesDrmCheckL
       
   238 // --------------------------------------------------------------------------
       
   239 //
       
   240 TInt CPbk2ContactImageAssigner::PassesDrmCheckL( 
       
   241         const HBufC* aDataBuffer,
       
   242         TBool& aIsProtected )
       
   243     {
       
   244     aIsProtected = ETrue;
       
   245     TInt error( KErrNone );
       
   246     
       
   247     if ( aDataBuffer )
       
   248         {
       
   249         error = iDrmManager->IsThumbnailForbidden( *aDataBuffer, aIsProtected );
       
   250         }
       
   251 
       
   252     return error;
       
   253     }
       
   254 
       
   255 // --------------------------------------------------------------------------
       
   256 // CPbk2ContactImageAssigner::AssignImageL
       
   257 // --------------------------------------------------------------------------
       
   258 //
       
   259 void CPbk2ContactImageAssigner::AssignImageL()
       
   260     {
       
   261     TBool isProtected( ETrue );
       
   262     TInt error = PassesDrmCheckL( iDataBuffer, isProtected );
       
   263     if ( !isProtected && error == KErrNone )
       
   264         {
       
   265         delete iImageOperation;
       
   266         iImageOperation = NULL;
       
   267 
       
   268         // First delete a previous thumbnail, if any
       
   269         iImageManager->RemoveImage( *iContact, *iFieldType );
       
   270 
       
   271         // Then set new image
       
   272         iImageOperation = iImageManager->SetImageAsyncL
       
   273             ( *iContact, *iFieldType, *this, *iDataBuffer );
       
   274         }
       
   275     else
       
   276         {
       
   277         // Notify observer asynchronously
       
   278         iError = error;
       
   279         iState = EDrmProtected;
       
   280         IssueRequest();
       
   281         }
       
   282     }
       
   283 
       
   284 // --------------------------------------------------------------------------
       
   285 // CPbk2ContactImageAssigner::AssignImageFileNameL
       
   286 // Assigns image file name, does not assign caller object text
       
   287 // --------------------------------------------------------------------------
       
   288 //
       
   289 void CPbk2ContactImageAssigner::AssignImageFileNameL()
       
   290     {
       
   291     const MVPbkFieldType* codFieldType = NULL;
       
   292     if ( FeatureManager::FeatureSupported( KFeatureIdCallImagetext ) )
       
   293         {
       
   294         codFieldType = iContactManager.FieldTypes().Find
       
   295             ( R_VPBK_FIELD_TYPE_CALLEROBJIMG );
       
   296         }
       
   297 
       
   298     if ( codFieldType )
       
   299         {
       
   300         CPbk2PresentationContact* contact = CPbk2PresentationContact::NewL
       
   301             ( *iContact, iFieldProperties );
       
   302         CleanupStack::PushL( contact );
       
   303 
       
   304         // Check is the field already used
       
   305         TInt fieldIndex = KErrNotFound;
       
   306         TInt result =
       
   307             Pbk2AssignValidateField::ValidateFieldTypeUsageInContactL
       
   308                 ( *contact, *codFieldType, iContactManager.FsSession(),
       
   309                   fieldIndex );
       
   310 
       
   311         MVPbkStoreContactField* field = NULL;
       
   312         if ( result == KErrAlreadyExists || result == KErrPathNotFound )
       
   313             {
       
   314             // If the contact has the field, then the field should be updated
       
   315             __ASSERT_DEBUG( fieldIndex > KErrNotFound,
       
   316                 Panic( EInvalidFieldIndex ) );
       
   317             field = &iContact->Fields().FieldAt( fieldIndex );
       
   318             if ( field )
       
   319                 {
       
   320                 InsertTextDataL( *field, *iDataBuffer );
       
   321                 }
       
   322             }
       
   323         else
       
   324             {
       
   325             field = iContact->CreateFieldLC( *codFieldType );
       
   326             InsertTextDataL( *field, *iDataBuffer );
       
   327             CleanupStack::Pop(); // field
       
   328             iIndex = iContact->AddFieldL( field ); // takes ownership
       
   329             }
       
   330 
       
   331         CleanupStack::PopAndDestroy( contact );
       
   332         }
       
   333 
       
   334     iState = EStopping;
       
   335     IssueRequest();
       
   336     }
       
   337 
       
   338 // --------------------------------------------------------------------------
       
   339 // CPbk2ContactImageAssigner::InsertTextDataL
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 void CPbk2ContactImageAssigner::InsertTextDataL
       
   343         ( MVPbkStoreContactField& aField, const HBufC& aDataBuffer ) const
       
   344     {
       
   345     MVPbkContactFieldTextData::Cast( aField.FieldData() ).
       
   346         SetTextL( aDataBuffer );
       
   347     }
       
   348 
       
   349 // --------------------------------------------------------------------------
       
   350 // CPbk2ContactImageAssigner::IssueRequest
       
   351 // --------------------------------------------------------------------------
       
   352 //
       
   353 void CPbk2ContactImageAssigner::IssueRequest()
       
   354     {
       
   355     TRequestStatus* status = &iStatus;
       
   356     User::RequestComplete( status, KErrNone );
       
   357     SetActive();
       
   358     }
       
   359 
       
   360 // End of File