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