phonebookui/Phonebook2/UIControls/src/Pbk2RingtoneCommands.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 ringing tone commands.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <e32base.h>
       
    21 #include "Pbk2RingtoneCommands.h"
       
    22 
       
    23 // Phonebook 2
       
    24 #include <CPbk2RingingToneFetch.h>
       
    25 #include "MPbk2BaseCommand.h"
       
    26 
       
    27 #include <MVPbkContactFieldTextData.h>
       
    28 #include <CPbk2AppUiBase.h>
       
    29 #include <MPbk2ApplicationServices.h>
       
    30 #include <TPbk2StoreContactAnalyzer.h>
       
    31 
       
    32 #include <Pbk2CommonUi.rsg>
       
    33 #include <Pbk2UIControls.rsg>
       
    34 #include <Pbk2Commands.rsg>
       
    35 
       
    36 // VPbk includes
       
    37 #include <MVPbkStoreContact.h>
       
    38 #include <MVPbkStoreContactField.h>
       
    39 #include <CVPbkContactManager.h>
       
    40 #include <TVPbkFieldTypeMapping.h>
       
    41 #include <MVPbkFieldType.h>
       
    42 #include <VPbkEng.rsg>
       
    43 
       
    44 // System includes
       
    45 #include <AknQueryDialog.h>
       
    46 #include <barsread.h>
       
    47 #include <coemain.h>
       
    48 #include <sysutil.h>
       
    49 
       
    50 /// Unnamed namespace for local definitions
       
    51 namespace {
       
    52 
       
    53 #ifdef _DEBUG
       
    54 
       
    55 enum TPanicCode
       
    56     {
       
    57     EPanicPreCond_NULL_Contact = 1
       
    58     };
       
    59 
       
    60 void Panic( TPanicCode aPanic )
       
    61     {
       
    62     _LIT( KPanicCat, "MPbk2RingtoneCommands" );
       
    63     User::Panic( KPanicCat, aPanic );
       
    64     }
       
    65 #endif // _DEBUG    
       
    66 
       
    67 // local functions
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // ReadFieldTypeL
       
    71 // --------------------------------------------------------------------------
       
    72 const MVPbkFieldType* ReadFieldTypeL()
       
    73     {
       
    74     return Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
    75     		FieldTypes().Find( R_VPBK_FIELD_TYPE_RINGTONE );
       
    76     }
       
    77 
       
    78 // --------------------------------------------------------------------------
       
    79 // Copies to aFilename ringtone field content if the field exists
       
    80 // in the store contact.
       
    81 // --------------------------------------------------------------------------
       
    82 inline void RetrieveExistingRingingToneFilenameL( 
       
    83         TFileName& aFilename,
       
    84         const MVPbkStoreContact& aContact )
       
    85     {
       
    86     TPbk2StoreContactAnalyzer analyzer(
       
    87         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager(),
       
    88         &aContact );
       
    89     TInt fieldIndex = analyzer.HasFieldL( R_PHONEBOOK2_RINGTONE_SELECTOR );
       
    90     if ( fieldIndex != KErrNotFound )
       
    91     	{
       
    92     	const MVPbkStoreContactField& field =
       
    93     		aContact.Fields().FieldAt( fieldIndex );
       
    94     	aFilename.Copy(
       
    95     			MVPbkContactFieldTextData::Cast( field.FieldData() ).Text() );    	
       
    96     	}
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // Shows select ringtone dialog. aContact is used to preselect assigned
       
   101 // ringtone file.
       
   102 // Returns ETrue if user selected a ringtone file.
       
   103 // It is returned in aToneFilename. If user selects default ringtone,
       
   104 // than aToneFilename will be empty
       
   105 // --------------------------------------------------------------------------
       
   106 //
       
   107 inline TBool FetchRingingToneL( TFileName& aToneFilename,
       
   108 		const MVPbkStoreContact& aContact )
       
   109     {
       
   110     TBool retval = EFalse;
       
   111     RetrieveExistingRingingToneFilenameL( aToneFilename, aContact );
       
   112     CPbk2RingingToneFetch* toneFetch = CPbk2RingingToneFetch::NewL();
       
   113     CleanupStack::PushL( toneFetch );
       
   114     // Run select ringtone dialog
       
   115     if ( toneFetch->FetchRingingToneL( aToneFilename ) )
       
   116         {
       
   117         retval = ETrue;
       
   118         }
       
   119     CleanupStack::PopAndDestroy( toneFetch );
       
   120     return retval;
       
   121     }
       
   122 
       
   123 } /// namespace
       
   124 
       
   125 // --------------------------------------------------------------------------
       
   126 // MPbk2RingtoneCommands::SetRingtoneL
       
   127 // --------------------------------------------------------------------------
       
   128 //
       
   129 MPbk2BaseCommand* Pbk2RingtoneCommands::SetRingtoneL(
       
   130 		MVPbkStoreContact* aContact )
       
   131 	{
       
   132 	__ASSERT_DEBUG( aContact, Panic( EPanicPreCond_NULL_Contact ) );
       
   133 	
       
   134     TBool retval =  EFalse;
       
   135     TFileName ringtoneFilename;
       
   136 
       
   137     RFs fs = CCoeEnv::Static()->FsSession();
       
   138     if ( SysUtil::FFSSpaceBelowCriticalLevelL( &fs ) )
       
   139         {
       
   140         User::Leave( KErrDiskFull );
       
   141         }
       
   142     
       
   143     retval = FetchRingingToneL( ringtoneFilename, *aContact );
       
   144     if ( retval )
       
   145         {
       
   146         MVPbkStoreContactField* field = NULL;
       
   147         // First check if the field is available already
       
   148         TPbk2StoreContactAnalyzer analyzer(
       
   149             Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager(),
       
   150             aContact );
       
   151         TInt fieldIndex = analyzer.HasFieldL( R_PHONEBOOK2_RINGTONE_SELECTOR );
       
   152 
       
   153         if ( fieldIndex != KErrNotFound)
       
   154             {
       
   155             // Check if the iToneFilename is set
       
   156             if (ringtoneFilename.Length() <= 0)
       
   157                 {
       
   158                 // If filename is not set and the contact has the field
       
   159                 // field can be removed.
       
   160                 //aContact->RemoveField( fieldIndex );
       
   161                 //Instead of above we don't remove the field, just zero it's data
       
   162                 MVPbkContactFieldData& data(aContact->Fields().
       
   163                     FieldAt( fieldIndex ).FieldData());
       
   164                 MVPbkContactFieldTextData::Cast( data ).SetTextL(KNullDesC);
       
   165                 }
       
   166             else
       
   167                 {
       
   168                 // If filename has been set and the contact has the field
       
   169                 // field should be updated.
       
   170                 field = &aContact->Fields().FieldAt( fieldIndex );
       
   171                 MVPbkContactFieldTextData::Cast( field->FieldData() ).
       
   172                     SetTextL( ringtoneFilename );
       
   173                 }
       
   174             }
       
   175         else
       
   176             {
       
   177             // Contact does not have the field, so create new one.
       
   178             if ( ringtoneFilename.Length() > 0 )
       
   179                 {
       
   180                 const MVPbkFieldType* fieldType = ReadFieldTypeL();
       
   181                 if ( !fieldType )
       
   182                     {
       
   183                     ReadFieldTypeL();
       
   184                     }
       
   185                 User::LeaveIfNull( fieldType );
       
   186                 // iToneFilename is set
       
   187                 field = aContact->CreateFieldLC( *fieldType );
       
   188                 MVPbkContactFieldTextData::Cast( field->FieldData() ).
       
   189                     SetTextL( ringtoneFilename );
       
   190                 aContact->AddFieldL( field );
       
   191                 CleanupStack::Pop(); // field
       
   192                 }
       
   193             }
       
   194         }
       
   195     return NULL; // synchronous operation
       
   196 	}
       
   197 
       
   198 // --------------------------------------------------------------------------
       
   199 // MPbk2RingtoneCommands::RemoveRingtoneL
       
   200 // --------------------------------------------------------------------------
       
   201 //
       
   202 MPbk2BaseCommand* Pbk2RingtoneCommands::RemoveRingtoneL(
       
   203 		MVPbkStoreContact* aContact )
       
   204 	{
       
   205 	__ASSERT_DEBUG( aContact, Panic( EPanicPreCond_NULL_Contact ) );
       
   206 	
       
   207     if( CAknQueryDialog::NewL()->ExecuteLD(
       
   208             R_PHONEBOOK2_REMOVE_RINGTONE_CONFIRMATION_DIALOG ) )
       
   209         {
       
   210         TPbk2StoreContactAnalyzer analyzer
       
   211         	( Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager(),
       
   212         			aContact );
       
   213         TInt fieldIndex( KErrNotFound );
       
   214         fieldIndex = analyzer.HasFieldL( R_PHONEBOOK2_RINGTONE_SELECTOR );
       
   215 	    if ( fieldIndex != KErrNotFound )
       
   216 			{
       
   217 		    // aContact->RemoveField (fieldIndex);
       
   218 			//Instead of above we don't remove the field, just zero it's data
       
   219 			MVPbkContactFieldData& data(aContact->Fields().
       
   220 			    FieldAt( fieldIndex ).FieldData());
       
   221             MVPbkContactFieldTextData::Cast( data ).SetTextL(KNullDesC);
       
   222 			}
       
   223         }
       
   224     return NULL; // synchronous operation
       
   225 	}
       
   226 
       
   227 // --------------------------------------------------------------------------
       
   228 // MPbk2RingtoneCommands::IsRingtoneFieldL
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 TBool Pbk2RingtoneCommands::IsRingtoneFieldL(
       
   232 		const MVPbkStoreContactField& aField )
       
   233 	{
       
   234     TPbk2StoreContactAnalyzer analyzer(
       
   235     		Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager(),
       
   236     		NULL );
       
   237     return analyzer.IsFieldTypeIncludedL(
       
   238     		aField, R_PHONEBOOK2_RINGTONE_SELECTOR );    
       
   239 	}
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // MPbk2RingtoneCommands::IsRingtoneFieldValidL
       
   243 // --------------------------------------------------------------------------
       
   244 //
       
   245 TBool Pbk2RingtoneCommands::IsRingtoneFieldValidL(
       
   246 		const MVPbkStoreContactField& aField )
       
   247 	{
       
   248     const MVPbkContactFieldTextData& textData = 
       
   249         MVPbkContactFieldTextData::Cast( aField.FieldData() );
       
   250     TEntry ignore;
       
   251     return CCoeEnv::Static()->FsSession().Entry( textData.Text(), ignore ) == KErrNone;
       
   252 	}
       
   253 
       
   254 // End of File