phonebookui/Phonebook2/CommandsExtension/src/CPbk2RemoveSpeedDialCmd.cpp
changeset 0 e686773b3f54
child 15 e8e3147d53eb
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Remove speed dial command object.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2RemoveSpeedDialCmd.h"
       
    20 
       
    21 // From Phonebook 2
       
    22 #include "MPbk2CommandObserver.h"
       
    23 #include <CPbk2ApplicationServices.h>
       
    24 #include <MPbk2AppUi.h>
       
    25 #include <MPbk2ContactUiControl.h>
       
    26 #include <CPbk2PresentationContact.h>
       
    27 #include <CPbk2FieldPropertyArray.h>
       
    28 #include <CPbk2PresentationContactFieldCollection.h>
       
    29 #include <Pbk2CmdExtRes.rsg>
       
    30 #include <CPbk2AppUiBase.h>
       
    31 
       
    32 // From Virtual Phonebook
       
    33 #include <CVPbkContactIdConverter.h>
       
    34 #include <TVPbkContactStoreUriPtr.h>
       
    35 #include <CVPbkContactManager.h>
       
    36 #include <MVPbkContactStoreList.h>
       
    37 #include <MVPbkBaseContact.h>
       
    38 #include <MVPbkContactAttributeManager.h>
       
    39 #include <CVPbkSpeedDialAttribute.h>
       
    40 #include <MVPbkContactOperationBase.h>
       
    41 #include <MVPbkStoreContact.h>
       
    42 #include <MVPbkContactStore.h>
       
    43 #include <MVPbkContactLink.h>
       
    44 
       
    45 #include <coemain.h>
       
    46 #include <AknQueryDialog.h>
       
    47 
       
    48 /// Unnamed namespace for local definitons
       
    49 namespace {
       
    50 
       
    51 #ifdef _DEBUG
       
    52     enum TPanicCode
       
    53         {
       
    54         EPanicPreCond_Null_Pointer
       
    55         };
       
    56 
       
    57     void Panic(TInt aReason)
       
    58         {
       
    59         _LIT(KPanicText, "CPbk2RemoveSpeedDialCmd");
       
    60         User::Panic(KPanicText, aReason);
       
    61         }
       
    62 #endif  // _DEBUG
       
    63 
       
    64 } // namespace
       
    65 
       
    66 // ============================== MEMBER FUNCTIONS ==========================
       
    67 
       
    68 // --------------------------------------------------------------------------
       
    69 // CPbk2RemoveSpeedDialCmd::CPbk2RemoveSpeedDialCmd
       
    70 // --------------------------------------------------------------------------
       
    71 //
       
    72 CPbk2RemoveSpeedDialCmd::CPbk2RemoveSpeedDialCmd(
       
    73         MPbk2ContactUiControl& aUiControl) :
       
    74     iUiControl(&aUiControl)
       
    75     {
       
    76     }
       
    77 
       
    78 // --------------------------------------------------------------------------
       
    79 // CPbk2RemoveSpeedDialCmd::NewL
       
    80 // --------------------------------------------------------------------------
       
    81 //
       
    82 CPbk2RemoveSpeedDialCmd* CPbk2RemoveSpeedDialCmd::NewL(
       
    83         MPbk2ContactUiControl& aUiControl)
       
    84     {
       
    85     CPbk2RemoveSpeedDialCmd* self = new(ELeave) CPbk2RemoveSpeedDialCmd(
       
    86             aUiControl);
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL();
       
    89     CleanupStack::Pop( self );
       
    90     return self;
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CPbk2RemoveSpeedDialCmd::~CPbk2RemoveSpeedDialCmd
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 CPbk2RemoveSpeedDialCmd::~CPbk2RemoveSpeedDialCmd()
       
    98     {
       
    99     delete iStoreContact;
       
   100     delete iOperation;
       
   101     Release( iAppServices );
       
   102     
       
   103     delete iContactLink;
       
   104     }
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // CPbk2RemoveSpeedDialCmd::ConstructL
       
   108 // --------------------------------------------------------------------------
       
   109 //
       
   110 inline void CPbk2RemoveSpeedDialCmd::ConstructL()
       
   111     {
       
   112     iAppServices = CPbk2ApplicationServices::InstanceL();
       
   113     }
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // CPbk2RemoveSpeedDialCmd::ExecuteLD
       
   117 // --------------------------------------------------------------------------
       
   118 //    
       
   119 void CPbk2RemoveSpeedDialCmd::ExecuteLD()
       
   120     {
       
   121     CleanupStack::PushL(this);
       
   122     
       
   123     iContactLink = iUiControl->FocusedContactL()->CreateLinkLC();
       
   124     CleanupStack::Pop(); // iContactLink
       
   125     iContactLink->ContactStore().OpenL( *this );
       
   126 
       
   127     iContactLink->ContactStore().Close( *this );
       
   128     
       
   129     if ( iUiControl )
       
   130         {
       
   131         iOperation = iAppServices->ContactManager().RetrieveContactL(
       
   132                 *iContactLink, *this);
       
   133         }
       
   134     else
       
   135         {
       
   136         FinishCommand( KErrNotFound );
       
   137         }
       
   138     
       
   139     delete iContactLink;
       
   140     iContactLink = NULL;
       
   141 
       
   142     CleanupStack::Pop(this);
       
   143     }
       
   144 
       
   145 // --------------------------------------------------------------------------
       
   146 // CPbk2RemoveSpeedDialCmd::AddObserver
       
   147 // --------------------------------------------------------------------------
       
   148 //    
       
   149 void CPbk2RemoveSpeedDialCmd::AddObserver(MPbk2CommandObserver& aObserver)
       
   150     {
       
   151     iCommandObserver = &aObserver;
       
   152     }
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // CPbk2RemoveSpeedDialCmd::ResetUiControl
       
   156 // --------------------------------------------------------------------------
       
   157 //   
       
   158 void CPbk2RemoveSpeedDialCmd::ResetUiControl(
       
   159         MPbk2ContactUiControl& aUiControl)
       
   160     {
       
   161     if (iUiControl == &aUiControl)
       
   162         {
       
   163         iUiControl = NULL;
       
   164         }
       
   165     }
       
   166 
       
   167 // --------------------------------------------------------------------------
       
   168 // CPbk2RemoveSpeedDialCmd::StoreReady
       
   169 // --------------------------------------------------------------------------
       
   170 //
       
   171 void CPbk2RemoveSpeedDialCmd::StoreReady( 
       
   172 		MVPbkContactStore& /*aContactStore*/ )
       
   173 	{
       
   174 	}
       
   175 
       
   176 // --------------------------------------------------------------------------
       
   177 // CPbk2RemoveSpeedDialCmd::StoreUnavailable
       
   178 // --------------------------------------------------------------------------
       
   179 //
       
   180 void CPbk2RemoveSpeedDialCmd::StoreUnavailable( 
       
   181 		MVPbkContactStore& /*aContactStore*/, 
       
   182         TInt /*aReason*/ )
       
   183 	{
       
   184 	}
       
   185 
       
   186 // --------------------------------------------------------------------------
       
   187 // CPbk2RemoveSpeedDialCmd::HandleStoreEventL
       
   188 // --------------------------------------------------------------------------
       
   189 //
       
   190 void CPbk2RemoveSpeedDialCmd::HandleStoreEventL( 
       
   191         MVPbkContactStore& /*aContactStore*/, 
       
   192         TVPbkContactStoreEvent aStoreEvent )
       
   193 	{
       
   194 	if ( iContactLink && 
       
   195 		aStoreEvent.iContactLink->IsSame( *iContactLink ) )
       
   196 		{
       
   197 		switch( aStoreEvent.iEventType )
       
   198 			{
       
   199 			case TVPbkContactStoreEvent::EContactDeleted: // FALLTHROUGH
       
   200 			case TVPbkContactStoreEvent::EContactChanged: // FALLTHROUGH
       
   201 				
       
   202 			default:
       
   203 				break;
       
   204 			}
       
   205 		}
       
   206 	}
       
   207 
       
   208 // --------------------------------------------------------------------------
       
   209 // CPbk2RemoveSpeedDialCmd::VPbkSingleContactOperationComplete
       
   210 // --------------------------------------------------------------------------
       
   211 //   
       
   212 void CPbk2RemoveSpeedDialCmd::VPbkSingleContactOperationComplete(
       
   213         MVPbkContactOperationBase& /*aOperation*/,
       
   214         MVPbkStoreContact* aContact)
       
   215     {
       
   216     delete iOperation;
       
   217     iOperation = NULL;
       
   218 
       
   219     delete iStoreContact;    
       
   220     iStoreContact = aContact;
       
   221     
       
   222     TRAPD(error, iStoreContact->LockL(*this));
       
   223     if (error != KErrNone)
       
   224         {
       
   225         FinishCommand(error);
       
   226         }
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // CPbk2RemoveSpeedDialCmd::VPbkSingleContactOperationFailed
       
   231 // --------------------------------------------------------------------------
       
   232 //    
       
   233 void CPbk2RemoveSpeedDialCmd::VPbkSingleContactOperationFailed(
       
   234         MVPbkContactOperationBase& /*aOperation*/, 
       
   235         TInt aError)
       
   236     {
       
   237     delete iOperation;
       
   238     iOperation = NULL;
       
   239     
       
   240     FinishCommand(aError);
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CPbk2RemoveSpeedDialCmd::ContactOperationCompleted
       
   245 // --------------------------------------------------------------------------
       
   246 //    
       
   247 void CPbk2RemoveSpeedDialCmd::ContactOperationCompleted
       
   248     (TContactOpResult aResult)
       
   249     {
       
   250     if (aResult.iOpCode == EContactCommit)
       
   251         {
       
   252         FinishCommand(KErrNone);        
       
   253         }
       
   254     else if (aResult.iOpCode == EContactLock)
       
   255         {    
       
   256         TInt index = KErrNotFound;
       
   257         TInt err = KErrNone;
       
   258         if (iUiControl)
       
   259             {            
       
   260             // Convert presentation index to store index.
       
   261             TRAP( err, 
       
   262                 index = ToStoreFieldIndexL( iUiControl->FocusedFieldIndex(), 
       
   263                 	iStoreContact ) );
       
   264             }
       
   265 
       
   266         if ( index != KErrNotFound )
       
   267             {
       
   268             MVPbkStoreContactField& field = 
       
   269                 iStoreContact->Fields().FieldAt( index );            
       
   270 
       
   271             TBool hasAttribute = EFalse;
       
   272             TRAP(err, hasAttribute = 
       
   273                 iAppServices->ContactManager().
       
   274                     ContactAttributeManagerL().
       
   275                 HasFieldAttributeL(CVPbkSpeedDialAttribute::Uid(), field));
       
   276                 
       
   277             if (hasAttribute)
       
   278                 {
       
   279                 TRAP(err, iOperation = iAppServices->ContactManager().
       
   280                     ContactAttributeManagerL().RemoveFieldAttributeL(
       
   281                     field, CVPbkSpeedDialAttribute::Uid(), *this) );
       
   282                 }
       
   283             else
       
   284                 {
       
   285                 FinishCommand( err );
       
   286                 }
       
   287             }        
       
   288             
       
   289         if (err != KErrNone)
       
   290             {
       
   291             FinishCommand( err );
       
   292             }        
       
   293         }
       
   294     }
       
   295 
       
   296 // --------------------------------------------------------------------------
       
   297 // CPbk2RemoveSpeedDialCmd::ContactOperationFailed
       
   298 // --------------------------------------------------------------------------
       
   299 //    
       
   300 void CPbk2RemoveSpeedDialCmd::ContactOperationFailed
       
   301     (TContactOp /*aOpCode*/, TInt aErrorCode, TBool /*aErrorNotified*/)
       
   302     {
       
   303     delete iOperation;
       
   304     iOperation = NULL;
       
   305     
       
   306     FinishCommand(aErrorCode);    
       
   307     }    
       
   308 
       
   309 // --------------------------------------------------------------------------
       
   310 // CPbk2RemoveSpeedDialCmd::AttributeOperationComplete
       
   311 // --------------------------------------------------------------------------
       
   312 //
       
   313 void CPbk2RemoveSpeedDialCmd::AttributeOperationComplete(
       
   314         MVPbkContactOperationBase& /*aOperation*/)
       
   315     {
       
   316     delete iOperation;
       
   317     iOperation = NULL;
       
   318 
       
   319     TRAP_IGNORE(iStoreContact->CommitL(*this));
       
   320     }
       
   321 
       
   322 // --------------------------------------------------------------------------
       
   323 // CPbk2RemoveSpeedDialCmd::AttributeOperationFailed
       
   324 // --------------------------------------------------------------------------
       
   325 //    
       
   326 void CPbk2RemoveSpeedDialCmd::AttributeOperationFailed(
       
   327     MVPbkContactOperationBase& /*aOperation*/, TInt aError)
       
   328     {
       
   329     delete iOperation;
       
   330     iOperation = NULL;
       
   331     
       
   332     FinishCommand(aError);
       
   333     }
       
   334 
       
   335 // --------------------------------------------------------------------------
       
   336 // CPbk2RemoveSpeedDialCmd::FinishCommand
       
   337 // --------------------------------------------------------------------------
       
   338 //        
       
   339 void CPbk2RemoveSpeedDialCmd::FinishCommand(TInt aError)
       
   340     {
       
   341     __ASSERT_DEBUG(iCommandObserver, Panic(EPanicPreCond_Null_Pointer));
       
   342     
       
   343     TInt error( aError );
       
   344     if (iUiControl && error == KErrNone)
       
   345         {
       
   346         CPbk2AppUiBase<CAknViewAppUi>& appUi = static_cast<CPbk2AppUiBase<CAknViewAppUi>&>
       
   347                                                ( *CEikonEnv::Static()->EikAppUi() );
       
   348         // If appUi is not in foreground, It means phonebook2 has hidden itself in background,
       
   349         // no need to reload its contact, only phonebook2 in foreground case, need to reload
       
   350         // call SetFocusedContactL        
       
   351         if ( appUi.IsForeground() )
       
   352             {
       
   353             TRAP( error, 
       
   354                  iUiControl->SetFocusedContactL(
       
   355                       *iUiControl->FocusedStoreContact() ) );
       
   356                 
       
   357             iUiControl->SetFocusedFieldIndex( iUiControl->FocusedFieldIndex() );
       
   358             iUiControl->UpdateAfterCommandExecution();	
       
   359             }
       
   360         }
       
   361 
       
   362     iCommandObserver->CommandFinished(*this);
       
   363 
       
   364 	if (error != KErrNone)	
       
   365 		{
       
   366 		CCoeEnv::Static()->HandleError(error);
       
   367 		}        
       
   368     }    
       
   369 
       
   370 // --------------------------------------------------------------------------
       
   371 // CPbk2RemoveSpeedDialCmd::ToStoreFieldIndexL
       
   372 // --------------------------------------------------------------------------
       
   373 //  
       
   374 TInt CPbk2RemoveSpeedDialCmd::ToStoreFieldIndexL(
       
   375 		TInt aPresIndex,
       
   376 		const MVPbkStoreContact* aStoreContact )
       
   377     {
       
   378     // It is ok to remove const, we don't make any changes
       
   379     MVPbkStoreContact* storeContact =
       
   380         const_cast<MVPbkStoreContact*>( aStoreContact );
       
   381 
       
   382     TInt index( KErrNotFound );
       
   383     CPbk2PresentationContact* presentationContact = 
       
   384         CPbk2PresentationContact::NewL( 
       
   385             *storeContact,
       
   386             iAppServices->FieldProperties() );
       
   387     CleanupStack::PushL( presentationContact );
       
   388 
       
   389     index = presentationContact->PresentationFields().StoreIndexOfField
       
   390                 ( aPresIndex );
       
   391     
       
   392     CleanupStack::PopAndDestroy( presentationContact );
       
   393     
       
   394     return index;
       
   395     } 
       
   396 
       
   397 // --------------------------------------------------------------------------
       
   398 // CPbk2CommandHandler::HasSpeedDialL
       
   399 // --------------------------------------------------------------------------
       
   400 //
       
   401 TBool CPbk2RemoveSpeedDialCmd::HasSpeedDialL()
       
   402     {
       
   403     TBool hasAttribute(EFalse);
       
   404 
       
   405     const MVPbkStoreContact* storeContact =
       
   406             iUiControl->FocusedStoreContact();
       
   407 
       
   408     // Convert presentation index to store index.
       
   409     TInt index( ToStoreFieldIndexL(
       
   410                     iUiControl->FocusedFieldIndex(),
       
   411                     storeContact ) );
       
   412 
       
   413     if ( index != KErrNotFound )
       
   414         {
       
   415         const MVPbkStoreContactField& field =
       
   416             storeContact->Fields().FieldAt( index );
       
   417 
       
   418         hasAttribute =
       
   419         	iAppServices->ContactManager().ContactAttributeManagerL().
       
   420                 HasFieldAttributeL(
       
   421                     CVPbkSpeedDialAttribute::Uid(),
       
   422                     field );
       
   423         }
       
   424     return hasAttribute;
       
   425     }
       
   426 
       
   427 // End of File