phonebookui/Phonebook2/CommandsExtension/src/CPbk2AssignDefaultsCmd.cpp
changeset 0 e686773b3f54
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 assign defaults command.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2AssignDefaultsCmd.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include <MPbk2CommandObserver.h>
       
    24 #include <CPbk2MemoryEntryDefaultsDlg.h>
       
    25 #include <CPbk2PresentationContact.h>
       
    26 #include <CPbk2FieldPropertyArray.h>
       
    27 #include <CPbk2ContactRelocator.h>
       
    28 #include <MPbk2ContactUiControl.h>
       
    29 #include <CPbk2DriveSpaceCheck.h>
       
    30 #include <CPbk2AppUiBase.h>
       
    31 #include <CPbk2ApplicationServices.h>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <CVPbkContactManager.h>
       
    35 #include <MVPbkContactLink.h>
       
    36 #include <MVPbkStoreContact.h>
       
    37 #include <MVPbkContactOperationBase.h>
       
    38 
       
    39 // System includes
       
    40 #include <coemain.h>
       
    41 #include <eikenv.h>
       
    42 
       
    43 // Debugging headers
       
    44 #include <Pbk2Debug.h>
       
    45 
       
    46 /// Unnamed namespace for local definitions
       
    47 namespace {
       
    48 
       
    49 const TInt KFirstContact = 0;
       
    50 const TInt KOneContact = 1;
       
    51 
       
    52 #ifdef _DEBUG
       
    53 enum TPanicCode
       
    54     {
       
    55     EPanicPostCond_ConstructL = 1,
       
    56     EPanicPreCond_LockContactL,
       
    57     EPanicPreCond_Null_Pointer
       
    58     };
       
    59 
       
    60 void Panic(TPanicCode aReason)
       
    61     {
       
    62     _LIT(KPanicText, "CPbk2AssignDefaultsCmd");
       
    63     User::Panic(KPanicText,aReason);
       
    64     }
       
    65 #endif // _DEBUG
       
    66 
       
    67 } /// namespace
       
    68 
       
    69 
       
    70 // --------------------------------------------------------------------------
       
    71 // CPbk2AssignDefaultsCmd::CPbk2AssignDefaultsCmd
       
    72 // --------------------------------------------------------------------------
       
    73 //
       
    74 CPbk2AssignDefaultsCmd::CPbk2AssignDefaultsCmd( 
       
    75         MPbk2ContactUiControl& aUiControl ) :
       
    76     iUiControl( &aUiControl )
       
    77     {
       
    78     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
    79         ("CPbk2AssignDefaultsCmd::CPbk2AssignDefaultsCmd(0x%x)"), this);
       
    80     }
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CPbk2AssignDefaultsCmd::~CPbk2AssignDefaultsCmd
       
    84 // --------------------------------------------------------------------------
       
    85 //
       
    86 CPbk2AssignDefaultsCmd::~CPbk2AssignDefaultsCmd()
       
    87     {
       
    88     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
    89         ("CPbk2AssignDefaultsCmd::~CPbk2AssignDefaultsCmd(0x%x)"),
       
    90         this);
       
    91 
       
    92     if( iUiControl )
       
    93         {
       
    94         iUiControl->RegisterCommand( NULL );
       
    95         }
       
    96 
       
    97     delete iContactRetriever;
       
    98     delete iContactLinkArray;
       
    99     delete iContact;
       
   100     delete iContactRelocator;
       
   101     Release( iAppServices );
       
   102     }
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // CPbk2AssignDefaultsCmd::NewL
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 CPbk2AssignDefaultsCmd* CPbk2AssignDefaultsCmd::NewL
       
   109         ( MPbk2ContactUiControl& aUiControl )
       
   110     {
       
   111     CPbk2AssignDefaultsCmd* self =
       
   112         new ( ELeave ) CPbk2AssignDefaultsCmd( aUiControl );
       
   113     CleanupStack::PushL( self );
       
   114     self->ConstructL();
       
   115     CleanupStack::Pop( self );
       
   116     return self;
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CPbk2AssignDefaultsCmd::ConstructL
       
   121 // --------------------------------------------------------------------------
       
   122 //
       
   123 inline void CPbk2AssignDefaultsCmd::ConstructL()
       
   124     {
       
   125     CPbk2DriveSpaceCheck* driveSpaceCheck = CPbk2DriveSpaceCheck::NewL
       
   126         ( CCoeEnv::Static()->FsSession() );
       
   127     CleanupStack::PushL( driveSpaceCheck );
       
   128     // check FFS situation
       
   129     driveSpaceCheck->DriveSpaceCheckL();
       
   130     CleanupStack::PopAndDestroy( driveSpaceCheck );
       
   131 
       
   132     iAppServices = CPbk2ApplicationServices::InstanceL();
       
   133 
       
   134     iContactLinkArray = iUiControl->SelectedContactsOrFocusedContactL();
       
   135     iUiControl->RegisterCommand( this );
       
   136 
       
   137     __ASSERT_DEBUG( iContactLinkArray &&
       
   138         iContactLinkArray->Count() == KOneContact,
       
   139             Panic( EPanicPostCond_ConstructL ) );
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------
       
   143 // CPbk2AssignDefaultsCmd::ExecuteLD
       
   144 // --------------------------------------------------------------------------
       
   145 //
       
   146 void CPbk2AssignDefaultsCmd::ExecuteLD()
       
   147     {
       
   148     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   149         ("CPbk2AssignDefaultsCmd::ExecuteLD(0x%x)"), this);
       
   150 
       
   151     const TInt linkCount = iContactLinkArray->Count();
       
   152 
       
   153     // It is possible to assing defaults to only one contact at a time
       
   154     if ( linkCount == KOneContact )
       
   155         {
       
   156         CleanupStack::PushL( this );
       
   157         iContactRetriever = iAppServices->ContactManager().
       
   158             RetrieveContactL( iContactLinkArray->At( KFirstContact ),
       
   159                 *this );
       
   160         CleanupStack::Pop( this );
       
   161         }
       
   162     else
       
   163         {
       
   164         // Notify command owner that the command has finished
       
   165         ProcessFinished( KErrArgument );
       
   166         }
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CPbk2AssignDefaultsCmd::AddObserver
       
   171 // --------------------------------------------------------------------------
       
   172 //
       
   173 void CPbk2AssignDefaultsCmd::AddObserver
       
   174         (MPbk2CommandObserver& aObserver)
       
   175     {
       
   176     iCommandObserver = &aObserver;
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CPbk2AssignDefaultsCmd::ResetUiControl
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 void CPbk2AssignDefaultsCmd::ResetUiControl(
       
   184         MPbk2ContactUiControl& aUiControl)
       
   185     {
       
   186     if (iUiControl == & aUiControl)
       
   187         {
       
   188         iUiControl = NULL;
       
   189         }
       
   190     }
       
   191 
       
   192 // --------------------------------------------------------------------------
       
   193 // CPbk2AssignDefaultsCmd::VPbkSingleContactOperationComplete
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 void CPbk2AssignDefaultsCmd::VPbkSingleContactOperationComplete
       
   197         (MVPbkContactOperationBase& /*aOperation*/,
       
   198         MVPbkStoreContact* aContact)
       
   199     {
       
   200     if ( aContact )
       
   201         {
       
   202         iContact = aContact;
       
   203 
       
   204         TBool contactNeedsRelocation = EFalse;
       
   205         TInt err = KErrNone;
       
   206         TRAP( err, contactNeedsRelocation = RelocateContactL() );
       
   207         if (err != KErrNone)
       
   208             {
       
   209             ProcessFinished( err );
       
   210             }
       
   211 
       
   212         if ( !contactNeedsRelocation )
       
   213             {
       
   214             TRAP( err, LockContactL() );
       
   215             if ( err != KErrNone )
       
   216                 {
       
   217                 ProcessFinished( err );
       
   218                 }
       
   219             }
       
   220         }
       
   221     else
       
   222         {
       
   223         ProcessFinished( KErrGeneral );
       
   224         }
       
   225     }
       
   226 
       
   227 // --------------------------------------------------------------------------
       
   228 // CPbk2AssignDefaultsCmd::VPbkSingleContactOperationFailed
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 void CPbk2AssignDefaultsCmd::VPbkSingleContactOperationFailed
       
   232         (MVPbkContactOperationBase& /*aOperation*/, TInt aError)
       
   233     {
       
   234     ProcessFinished( aError );
       
   235     }
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CPbk2AssignDefaultsCmd::ContactOperationCompleted
       
   239 // --------------------------------------------------------------------------
       
   240 //
       
   241 void CPbk2AssignDefaultsCmd::ContactOperationCompleted
       
   242         (TContactOpResult aResult)
       
   243     {
       
   244     if (aResult.iOpCode == EContactLock)
       
   245         {
       
   246         TRAPD( err, LaunchAssignDialogL() );
       
   247         if ( err != KErrNone )
       
   248             {
       
   249             ProcessFinished( err );
       
   250             }
       
   251         }
       
   252     else if (aResult.iOpCode == EContactCommit)
       
   253         {
       
   254         // Now, its time to exit the command
       
   255         ProcessFinished( KErrNone );
       
   256         }
       
   257     }
       
   258 
       
   259 // --------------------------------------------------------------------------
       
   260 // CPbk2AssignDefaultsCmd::ContactOperationFailed
       
   261 // --------------------------------------------------------------------------
       
   262 //
       
   263 void CPbk2AssignDefaultsCmd::ContactOperationFailed
       
   264         ( TContactOp /*aOpCode*/, TInt aErrorCode,
       
   265           TBool /*aErrorNotified*/ )
       
   266     {
       
   267     ProcessFinished( aErrorCode );
       
   268     }
       
   269 
       
   270 // --------------------------------------------------------------------------
       
   271 // CPbk2AssignDefaultsCmd::ContactRelocatedL
       
   272 // --------------------------------------------------------------------------
       
   273 //
       
   274 void CPbk2AssignDefaultsCmd::ContactRelocatedL
       
   275         (MVPbkStoreContact* aRelocatedContact)
       
   276     {
       
   277     iContact = aRelocatedContact; // take ownership
       
   278     }
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CPbk2AssignDefaultsCmd::ContactRelocationFailed
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 void CPbk2AssignDefaultsCmd::ContactRelocationFailed
       
   285         (TInt aReason, MVPbkStoreContact* aContact)
       
   286     {
       
   287     // Take the contact back
       
   288     iContact = aContact;
       
   289     if ( aReason == KErrCancel )
       
   290         {
       
   291         // No error note is to be shown to the user when she
       
   292         // manually cancels the relocation process, therefore
       
   293         // the error code must be converted
       
   294         aReason = KErrNone;
       
   295         }    
       
   296     ProcessFinished( aReason );
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------
       
   300 // CPbk2AssignDefaultsCmd::ContactsRelocationFailed
       
   301 // --------------------------------------------------------------------------
       
   302 //
       
   303 void CPbk2AssignDefaultsCmd::ContactsRelocationFailed
       
   304         ( TInt /*aReason*/, CVPbkContactLinkArray* /*aContacts*/ )
       
   305     {
       
   306     // Do nothing
       
   307     }
       
   308 
       
   309 // --------------------------------------------------------------------------
       
   310 // CPbk2AssignDefaultsCmd::RelocationProcessComplete
       
   311 // --------------------------------------------------------------------------
       
   312 //
       
   313 void CPbk2AssignDefaultsCmd::RelocationProcessComplete()
       
   314     {
       
   315     TRAPD( res, LockContactL() );
       
   316     if ( res != KErrNone )
       
   317         {
       
   318         ProcessFinished( res );
       
   319         }
       
   320     }
       
   321 
       
   322 // --------------------------------------------------------------------------
       
   323 // CPbk2AssignDefaultsCmd::LockContactL
       
   324 // --------------------------------------------------------------------------
       
   325 //
       
   326 void CPbk2AssignDefaultsCmd::LockContactL()
       
   327     {
       
   328     __ASSERT_DEBUG(iContact, Panic(EPanicPreCond_LockContactL));
       
   329 
       
   330     // We must lock the contact for changes
       
   331     iContact->LockL(*this);
       
   332     }
       
   333 
       
   334 // --------------------------------------------------------------------------
       
   335 // CPbk2AssignDefaultsCmd::LaunchAssignDialogL
       
   336 // --------------------------------------------------------------------------
       
   337 //
       
   338 void CPbk2AssignDefaultsCmd::LaunchAssignDialogL()
       
   339     {
       
   340     CPbk2PresentationContact* presentation =
       
   341         CPbk2PresentationContact::NewL( *iContact,
       
   342         iAppServices->FieldProperties() );
       
   343     CleanupStack::PushL(presentation);
       
   344 
       
   345     // Launch the dialog
       
   346     CPbk2MemoryEntryDefaultsDlg* dlg =
       
   347         CPbk2MemoryEntryDefaultsDlg::NewL(*presentation,
       
   348         iAppServices->ContactManager());
       
   349     dlg->ExecuteLD();
       
   350 
       
   351     CleanupStack::PopAndDestroy(presentation); // presentation
       
   352 
       
   353     // Next, commit contact
       
   354     iContact->CommitL(*this);
       
   355     }
       
   356 
       
   357 // --------------------------------------------------------------------------
       
   358 // CPbk2AssignDefaultsCmd::RelocateContactL
       
   359 // --------------------------------------------------------------------------
       
   360 //
       
   361 inline TBool CPbk2AssignDefaultsCmd::RelocateContactL()
       
   362     {
       
   363     TBool ret = EFalse;
       
   364 
       
   365     delete iContactRelocator;
       
   366     iContactRelocator = NULL;
       
   367     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   368 
       
   369     if ( !iContactRelocator->IsPhoneMemoryContact( *iContact ) &&
       
   370         iContactRelocator->IsPhoneMemoryInConfigurationL() )
       
   371         {
       
   372         // Asynchronously relocate contact
       
   373         iContactRelocator->RelocateContactL( iContact, *this,
       
   374             Pbk2ContactRelocator::EPbk2DisplayStoreDoesNotSupportQuery,
       
   375             CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   376         iContact = NULL;    // Ownership was taken
       
   377         ret = ETrue;        // Indicate that contact needs relocating
       
   378         }
       
   379 
       
   380     return ret;
       
   381     }
       
   382 
       
   383 // --------------------------------------------------------------------------
       
   384 // CPbk2AssignDefaultsCmd::ProcessFinished
       
   385 // --------------------------------------------------------------------------
       
   386 //
       
   387 void CPbk2AssignDefaultsCmd::ProcessFinished( TInt aError )
       
   388     {
       
   389     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   390         ("CPbk2AssignDefaultsCmd::ProcessFinished()"));
       
   391         
       
   392     __ASSERT_DEBUG(iCommandObserver, Panic(EPanicPreCond_Null_Pointer));        
       
   393 
       
   394     if ( aError == KErrNone && iUiControl)
       
   395         {
       
   396         // UI control has to reload its contact,
       
   397         // so lets call SetFocusedContactL
       
   398         TRAP( aError, iUiControl->SetFocusedContactL( *iContact ) );
       
   399         iUiControl->UpdateAfterCommandExecution();
       
   400         }
       
   401 
       
   402     if ( aError != KErrNone )
       
   403         {
       
   404         CCoeEnv::Static()->HandleError( aError );
       
   405         }
       
   406 
       
   407     // Notify command owner that the command has finished
       
   408     iCommandObserver->CommandFinished( *this );
       
   409     }
       
   410 
       
   411 //  End of File