phonebookui/Phonebook2/Commands/src/CPbk2SetToneBase.cpp
changeset 0 e686773b3f54
child 21 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 set ringing tone command base.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CPbk2SetToneBase.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include <CPbk2RingingToneFetch.h>
       
    24 #include <CPbk2ContactRelocator.h>
       
    25 #include <MPbk2ContactUiControl.h>
       
    26 #include <MPbk2CommandObserver.h>
       
    27 #include <Pbk2ProcessDecoratorFactory.h>
       
    28 #include <TPbk2StoreContactAnalyzer.h>
       
    29 #include <MVPbkContactFieldTextData.h>
       
    30 #include <Pbk2Commands.rsg>
       
    31 #include <Pbk2UIControls.rsg>
       
    32 #include <CPbk2DriveSpaceCheck.h>
       
    33 #include <CPbk2AppUiBase.h>
       
    34 #include <MPbk2ApplicationServices.h>
       
    35 
       
    36 // VPbk includes
       
    37 #include <CVPbkContactLinkArray.h>
       
    38 #include <MVPbkContactOperationBase.h>
       
    39 #include <MVPbkStoreContact.h>
       
    40 #include <CVPbkContactManager.h>
       
    41 #include <TVPbkFieldTypeMapping.h>
       
    42 #include <MVPbkFieldType.h>
       
    43 #include <VPbkEng.rsg>
       
    44 
       
    45 // System includes
       
    46 #include <barsread.h>
       
    47 #include <coemain.h>
       
    48 
       
    49 /// Unnamed namespace for local definitions
       
    50 namespace {
       
    51 
       
    52 const TInt KFirstElement = 0;
       
    53     
       
    54 #ifdef _DEBUG
       
    55 
       
    56 enum TPanicCode
       
    57     {
       
    58     EPanicPreCond_NULL_Pointer = 1
       
    59     };
       
    60 
       
    61 void Panic( TPanicCode aPanic )
       
    62     {
       
    63     _LIT( KPanicCat, "CPbk2SetToneBase" );
       
    64     User::Panic( KPanicCat, aPanic );
       
    65     }
       
    66 
       
    67 #endif // _DEBUG    
       
    68 
       
    69 } /// namespace
       
    70 
       
    71 // --------------------------------------------------------------------------
       
    72 // CPbk2SetToneBase::CPbk2SetToneBase
       
    73 // --------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C CPbk2SetToneBase::CPbk2SetToneBase( 
       
    76         MPbk2ContactUiControl& aUiControl ):
       
    77     CActive( CActive::EPriorityIdle ), 
       
    78     iUiControl( &aUiControl )
       
    79     {
       
    80     CActiveScheduler::Add( this );
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------
       
    84 // CPbk2SetToneBase::~CPbk2SetToneBase
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 EXPORT_C CPbk2SetToneBase::~CPbk2SetToneBase()
       
    88     {
       
    89     Cancel();
       
    90     if( iUiControl )
       
    91         {
       
    92         iUiControl->RegisterCommand( NULL );
       
    93         }
       
    94     delete iToneFetch;
       
    95     delete iContactRelocator;
       
    96     delete iSelectedContacts;
       
    97     delete iRetrieveOperation;
       
    98     delete iStoreContact;
       
    99     }
       
   100 
       
   101 
       
   102 // --------------------------------------------------------------------------
       
   103 // CPbk2SetToneBase::BaseConstructL
       
   104 // --------------------------------------------------------------------------
       
   105 //
       
   106 EXPORT_C void CPbk2SetToneBase::BaseConstructL()
       
   107     {
       
   108     CPbk2DriveSpaceCheck* driveSpaceCheck = CPbk2DriveSpaceCheck::NewL( 
       
   109             CCoeEnv::Static()->FsSession() );
       
   110     CleanupStack::PushL( driveSpaceCheck );
       
   111     // check FFS situation
       
   112     driveSpaceCheck->DriveSpaceCheckL();
       
   113     CleanupStack::PopAndDestroy( driveSpaceCheck );
       
   114     iToneFetch = CPbk2RingingToneFetch::NewL();
       
   115     iUiControl->RegisterCommand( this );
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------
       
   119 // CPbk2SetToneBase::RunL
       
   120 // --------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C void CPbk2SetToneBase::RunL()
       
   123     {
       
   124     switch( iState )
       
   125         {
       
   126         case EFetchingRingingTone:
       
   127             {
       
   128             TBool retval =  EFalse;
       
   129             retval = FetchRingingToneL();
       
   130             if ( retval )
       
   131                 {
       
   132                 // User has selected a ringing tone
       
   133                 iState = ERetrievingContact;
       
   134                 IssueRequest();
       
   135                 }
       
   136             else
       
   137                 {
       
   138                 // User has pressed cancel
       
   139                 IssueStopRequest();
       
   140                 }
       
   141             break;
       
   142             }
       
   143 
       
   144         case ERetrievingContact:
       
   145             {
       
   146             if (iUiControl)
       
   147                 {
       
   148                 iSelectedContacts = 
       
   149                     iUiControl->SelectedContactsOrFocusedContactL();
       
   150                     
       
   151                 if ( iSelectedContacts )
       
   152                     {
       
   153                     const MVPbkContactLink& link =
       
   154                     iSelectedContacts->At( KFirstElement );
       
   155                     RetrieveContactL( link );
       
   156                     }
       
   157                 else
       
   158                     {
       
   159                     ProcessDismissed( KErrNotFound );    
       
   160                     }
       
   161                 }
       
   162             else
       
   163                 {
       
   164                 // dismiss process
       
   165                 ProcessDismissed( KErrNotFound );
       
   166                 }
       
   167             break;
       
   168             }
       
   169 
       
   170         case EStopping:
       
   171             {
       
   172             ProcessDismissed( KErrNone );
       
   173             break;
       
   174             }
       
   175 
       
   176         default:;
       
   177         }
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CPbk2SetToneBase::DoCancel
       
   182 // --------------------------------------------------------------------------
       
   183 //
       
   184 EXPORT_C void CPbk2SetToneBase::DoCancel()
       
   185     {
       
   186     delete iRetrieveOperation;
       
   187     iRetrieveOperation = NULL;
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CPbk2SetToneBase::RunError
       
   192 // --------------------------------------------------------------------------
       
   193 //
       
   194 EXPORT_C TInt CPbk2SetToneBase::RunError( 
       
   195         TInt aError )
       
   196     {
       
   197 	TInt ret = aError;
       
   198 
       
   199 	if ( aError != KLeaveExit )
       
   200 	    {
       
   201 	    ProcessDismissed( aError );
       
   202 	    
       
   203 	    // ProcessDismissed handled errors
       
   204 	    ret = KErrNone;
       
   205 	    }
       
   206 	
       
   207 	return ret;
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2SetToneBase::ExecuteLD
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 EXPORT_C void CPbk2SetToneBase::ExecuteLD()
       
   215     {
       
   216     // Start the command process
       
   217     IssueRequest();
       
   218     }
       
   219 
       
   220 // --------------------------------------------------------------------------
       
   221 // CPbk2SetToneBase::ResetUiControl
       
   222 // --------------------------------------------------------------------------
       
   223 //
       
   224 EXPORT_C void CPbk2SetToneBase::ResetUiControl(
       
   225         MPbk2ContactUiControl& aUiControl)
       
   226     {
       
   227     if (iUiControl == & aUiControl)
       
   228         {
       
   229         iUiControl = NULL;
       
   230         }
       
   231     }
       
   232 
       
   233 // --------------------------------------------------------------------------
       
   234 // CPbk2SetToneBase::AddObserver
       
   235 // --------------------------------------------------------------------------
       
   236 //
       
   237 EXPORT_C void CPbk2SetToneBase::AddObserver(
       
   238         MPbk2CommandObserver& aObserver )
       
   239     {
       
   240     iCommandObserver = &aObserver;
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CPbk2SetToneBase::ProcessDismissed
       
   245 // --------------------------------------------------------------------------
       
   246 //
       
   247 EXPORT_C void CPbk2SetToneBase::ProcessDismissed( 
       
   248         TInt aCancelCode )
       
   249     {
       
   250     __ASSERT_DEBUG( iCommandObserver, Panic(EPanicPreCond_NULL_Pointer));
       
   251     
       
   252     if ( aCancelCode != KErrCancel && iUiControl)
       
   253         {
       
   254         // Don't remove list markings if user canceled
       
   255         iUiControl->SetBlank( EFalse );
       
   256         iUiControl->UpdateAfterCommandExecution();
       
   257         }
       
   258 
       
   259     // Notify command owner that the command has finished
       
   260     iCommandObserver->CommandFinished( *this );
       
   261     }
       
   262 
       
   263 // --------------------------------------------------------------------------
       
   264 // CPbk2SetToneBase::RetrieveContactL
       
   265 // --------------------------------------------------------------------------
       
   266 //
       
   267 EXPORT_C void CPbk2SetToneBase::RetrieveContactL
       
   268         ( const MVPbkContactLink& aLink )
       
   269     {
       
   270     delete iRetrieveOperation;
       
   271     iRetrieveOperation = NULL;
       
   272 
       
   273     iRetrieveOperation =
       
   274         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   275             RetrieveContactL( aLink, *this );
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------
       
   279 // CPbk2SetToneBase::RelocateContactL
       
   280 // --------------------------------------------------------------------------
       
   281 //
       
   282 EXPORT_C TBool CPbk2SetToneBase::RelocateContactL
       
   283         ( MVPbkStoreContact* aContact )
       
   284     {
       
   285     TBool ret = EFalse;
       
   286 
       
   287     delete iContactRelocator;
       
   288     iContactRelocator = NULL;
       
   289     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   290 
       
   291     if ( !iContactRelocator->IsPhoneMemoryContact( *aContact ) &&
       
   292         iContactRelocator->IsPhoneMemoryInConfigurationL() )
       
   293         {
       
   294         // Asynchronously relocate contact
       
   295         iContactRelocator->RelocateContactL( aContact, *this,
       
   296             Pbk2ContactRelocator::EPbk2DisplayStoreDoesNotSupportQuery,
       
   297             CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   298         // relocator has taken the ownership
       
   299         aContact = NULL;
       
   300         ret = ETrue;        // Indicate that contact needs relocating
       
   301         }
       
   302 
       
   303     return ret;
       
   304     }
       
   305 
       
   306 // --------------------------------------------------------------------------
       
   307 // CPbk2SetToneBase::SetToneAndCommitL
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C void CPbk2SetToneBase::SetToneAndCommitL
       
   311         ( MVPbkStoreContact* aContact )
       
   312     {
       
   313     MVPbkStoreContactField* field = NULL;
       
   314     // First check if the field is available already
       
   315     TInt fieldIndex = KErrNotFound;
       
   316     TPbk2StoreContactAnalyzer analyzer(
       
   317         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager(),
       
   318         aContact );
       
   319     fieldIndex = analyzer.HasFieldL( R_PHONEBOOK2_RINGTONE_SELECTOR );
       
   320 
       
   321     if ( fieldIndex != KErrNotFound)
       
   322         {
       
   323         // Check if the iToneFilename is set
       
   324         if (iToneFilename.Length() <= 0)
       
   325             {
       
   326             // If filename is not set and the contact has the field
       
   327             // field can be removed.
       
   328             aContact->RemoveField( fieldIndex );
       
   329             }
       
   330         else
       
   331             {
       
   332             // If filename has been set and the contact has the field
       
   333             // field should be updated.
       
   334             field = &aContact->Fields().FieldAt( fieldIndex );
       
   335             MVPbkContactFieldTextData::Cast( field->FieldData() ).
       
   336                 SetTextL( iToneFilename );
       
   337             }
       
   338         }
       
   339     else
       
   340         {
       
   341         // Contact does not have the field, so create new one.
       
   342         if ( iToneFilename.Length() > 0 )
       
   343             {
       
   344             if ( !iFieldType )
       
   345                 {
       
   346                 ReadFieldTypeL();
       
   347                 }
       
   348             // iToneFilename is set
       
   349             field = aContact->CreateFieldLC( *iFieldType );
       
   350             MVPbkContactFieldTextData::Cast( field->FieldData() ).
       
   351                 SetTextL( iToneFilename );
       
   352             aContact->AddFieldL( field );
       
   353             CleanupStack::Pop(); // field
       
   354             }
       
   355         }
       
   356 
       
   357     aContact->CommitL( *this );
       
   358     }
       
   359 
       
   360 // --------------------------------------------------------------------------
       
   361 // CPbk2SetToneBase::IssueRequest
       
   362 // --------------------------------------------------------------------------
       
   363 //
       
   364 EXPORT_C void CPbk2SetToneBase::IssueRequest()
       
   365     {
       
   366     TRequestStatus* status = &iStatus;
       
   367     User::RequestComplete(status, KErrNone);
       
   368     SetActive();
       
   369     }
       
   370 
       
   371 // --------------------------------------------------------------------------
       
   372 // CPbk2SetToneBase::IssueStopRequest
       
   373 // --------------------------------------------------------------------------
       
   374 //
       
   375 EXPORT_C void CPbk2SetToneBase::IssueStopRequest()
       
   376     {
       
   377     iState = EStopping;
       
   378     if (!IsActive())
       
   379         {
       
   380         IssueRequest();
       
   381         }
       
   382     }
       
   383 
       
   384 // --------------------------------------------------------------------------
       
   385 // CPbk2SetToneBase::FetchRingingToneL
       
   386 // --------------------------------------------------------------------------
       
   387 //
       
   388 TBool CPbk2SetToneBase::FetchRingingToneL()
       
   389     {
       
   390     TBool retval = EFalse;
       
   391     RetrieveExistingRingingToneFilenameL( iToneFilename );
       
   392     if ( iToneFetch->FetchRingingToneL( iToneFilename ) )
       
   393         {
       
   394         retval = ETrue;
       
   395         }
       
   396     return retval;
       
   397     }
       
   398 
       
   399 // --------------------------------------------------------------------------
       
   400 // CPbk2SetToneBase::RetrieveExistingRingingToneFilenameL
       
   401 // --------------------------------------------------------------------------
       
   402 //
       
   403 void CPbk2SetToneBase::RetrieveExistingRingingToneFilenameL( 
       
   404         TFileName& aFilename )
       
   405     {
       
   406     if (iUiControl)
       
   407         {
       
   408         const MVPbkStoreContact* contact = iUiControl->FocusedStoreContact();
       
   409         if ( contact )
       
   410             {
       
   411             TInt fieldCount = contact->Fields().FieldCount();
       
   412             for ( TInt i = 0; i < fieldCount; ++i )
       
   413                 {
       
   414                 const MVPbkStoreContactField& field =
       
   415                     contact->Fields().FieldAt( i );
       
   416                 const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
   417                 if ( fieldType &&  ( fieldType->FieldTypeResId()
       
   418                         == R_VPBK_FIELD_TYPE_RINGTONE ) )
       
   419                     {
       
   420                     aFilename.Copy
       
   421                         ( MVPbkContactFieldTextData::Cast( field.FieldData() ).
       
   422                             Text() );
       
   423                     break;
       
   424                     }
       
   425                 }
       
   426             }
       
   427         }
       
   428     }
       
   429 
       
   430 // --------------------------------------------------------------------------
       
   431 // CPbk2SetToneBase::ReadFieldType
       
   432 // --------------------------------------------------------------------------
       
   433 //
       
   434 void CPbk2SetToneBase::ReadFieldTypeL()
       
   435     {
       
   436     TResourceReader reader;
       
   437     CCoeEnv::Static()->CreateResourceReaderLC( reader, R_RTONE_FIELD_TYPE );
       
   438     const TInt count = reader.ReadInt16();
       
   439 
       
   440     TVPbkFieldTypeMapping mapping(reader);
       
   441     iFieldType = mapping.FindMatch
       
   442         (Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   443             FieldTypes());
       
   444     if (!iFieldType)
       
   445         {
       
   446         User::Leave(KErrNotFound);
       
   447         }
       
   448     CleanupStack::PopAndDestroy(); // CreateResourceReaderLC
       
   449     }
       
   450 
       
   451 // End of File