phonebookui/Phonebook2/CommandsExtension/src/CPbk2CopyNumberToClipboardCmd.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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:  A command for clipboard copy
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CPbk2CopyNumberToClipboardCmd.h"
       
    21 
       
    22 #include <MVPbkStoreContact.h>
       
    23 #include <MVPbkContactOperationBase.h>
       
    24 #include <CVPbkContactManager.h>
       
    25 #include <MVPbkContactFieldTextData.h>
       
    26 #include <CPbk2ApplicationServices.h>
       
    27 #include <MPbk2AppUi.h>
       
    28 #include <MPbk2ContactUiControl.h>
       
    29 #include <MPbk2CommandObserver.h>
       
    30 #include <CPbk2PresentationContact.h>
       
    31 #include <CPbk2FieldPropertyArray.h>
       
    32 #include <CPbk2PresentationContactFieldCollection.h>
       
    33 #include <Pbk2Commands.rsg>
       
    34 #include <Pbk2CmdExtRes.rsg>
       
    35 
       
    36 #include <txtetext.h>   // CPlainText
       
    37 #include <baclipb.h>    // CClipboard
       
    38 #include <StringLoader.h>
       
    39 #include <aknnotewrappers.h>    // CAknInformationNote
       
    40 
       
    41 
       
    42 // --------------------------------------------------------------------------
       
    43 // CPbk2CopyNumberToClipboardCmd::NewL
       
    44 // --------------------------------------------------------------------------
       
    45 //
       
    46 CPbk2CopyNumberToClipboardCmd* CPbk2CopyNumberToClipboardCmd::NewL(
       
    47         MPbk2ContactUiControl& aUiControl   )
       
    48     {
       
    49     CPbk2CopyNumberToClipboardCmd* self = 
       
    50         new ( ELeave ) CPbk2CopyNumberToClipboardCmd( aUiControl );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop( self );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // --------------------------------------------------------------------------
       
    58 // CPbk2CopyNumberToClipboardCmd::CPbk2CopyNumberToClipboardCmd
       
    59 // --------------------------------------------------------------------------
       
    60 //
       
    61 CPbk2CopyNumberToClipboardCmd::CPbk2CopyNumberToClipboardCmd(
       
    62         MPbk2ContactUiControl& aUiControl) : 
       
    63     iUiControl(&aUiControl)
       
    64     {
       
    65     // do nothing
       
    66     }
       
    67 
       
    68 // --------------------------------------------------------------------------
       
    69 // CPbk2CopyNumberToClipboardCmd::~CPbk2CopyNumberToClipboardCmd
       
    70 // --------------------------------------------------------------------------
       
    71 //    
       
    72 CPbk2CopyNumberToClipboardCmd::~CPbk2CopyNumberToClipboardCmd()
       
    73     {
       
    74     delete iStoreContact;
       
    75     delete iRetrieveOperation;
       
    76     Release( iAppServices );
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CPbk2CopyNumberToClipboardCmd::ConstructL
       
    81 // --------------------------------------------------------------------------
       
    82 //
       
    83 inline void CPbk2CopyNumberToClipboardCmd::ConstructL()
       
    84     {
       
    85     iAppServices = CPbk2ApplicationServices::InstanceL();
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CPbk2CopyNumberToClipboardCmd::ExecuteLD
       
    90 // --------------------------------------------------------------------------
       
    91 //
       
    92 void CPbk2CopyNumberToClipboardCmd::ExecuteLD()
       
    93     {
       
    94     CleanupStack::PushL(this);
       
    95     
       
    96     if (iUiControl)
       
    97         {
       
    98         MVPbkContactLink* link = iUiControl->FocusedContactL()->CreateLinkLC();
       
    99         iRetrieveOperation = 
       
   100             iAppServices->ContactManager().RetrieveContactL(*link, *this);
       
   101         CleanupStack::PopAndDestroy(); // link
       
   102         }
       
   103     else
       
   104         {
       
   105         iObserver->CommandFinished(*this);                     
       
   106         }
       
   107     CleanupStack::Pop(this);
       
   108     }
       
   109 
       
   110 // --------------------------------------------------------------------------
       
   111 // CPbk2CopyNumberToClipboardCmd::AddObserver
       
   112 // --------------------------------------------------------------------------
       
   113 //    
       
   114 void CPbk2CopyNumberToClipboardCmd::AddObserver(MPbk2CommandObserver& aObserver)
       
   115     {
       
   116     iObserver = &aObserver;
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CPbk2CopyNumberToClipboardCmd::ResetUiControl
       
   121 // --------------------------------------------------------------------------
       
   122 //    
       
   123 void CPbk2CopyNumberToClipboardCmd::ResetUiControl(
       
   124         MPbk2ContactUiControl& aUiControl)
       
   125     {
       
   126     if (iUiControl == &aUiControl)
       
   127         {
       
   128         iUiControl = NULL;
       
   129         }
       
   130     }
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // CPbk2CopyNumberToClipboardCmd::VPbkSingleContactOperationComplete
       
   134 // --------------------------------------------------------------------------
       
   135 //
       
   136 void CPbk2CopyNumberToClipboardCmd::VPbkSingleContactOperationComplete(
       
   137     MVPbkContactOperationBase& aOperation,
       
   138     MVPbkStoreContact* aContact)
       
   139     {
       
   140     if (&aOperation == iRetrieveOperation)
       
   141         {
       
   142         delete iRetrieveOperation;
       
   143         iRetrieveOperation = NULL;
       
   144         
       
   145         delete iStoreContact;
       
   146         iStoreContact = aContact;
       
   147         
       
   148         TRAP_IGNORE(CopyNumberToClipboardL());
       
   149 
       
   150         if (iUiControl)
       
   151             {
       
   152             iUiControl->UpdateAfterCommandExecution();
       
   153             }
       
   154         iObserver->CommandFinished(*this);                     
       
   155         }    
       
   156     }
       
   157 
       
   158 // --------------------------------------------------------------------------
       
   159 // CPbk2CopyNumberToClipboardCmd::VPbkSingleContactOperationFailed
       
   160 // --------------------------------------------------------------------------
       
   161 //    
       
   162 void CPbk2CopyNumberToClipboardCmd::VPbkSingleContactOperationFailed(
       
   163     MVPbkContactOperationBase& aOperation, 
       
   164     TInt /*aError*/)
       
   165     {
       
   166     if (&aOperation == iRetrieveOperation)
       
   167         {
       
   168         delete iRetrieveOperation;
       
   169         iRetrieveOperation = NULL;
       
   170 
       
   171         if (iUiControl)
       
   172             {
       
   173             iUiControl->UpdateAfterCommandExecution();
       
   174             }
       
   175         iObserver->CommandFinished(*this);
       
   176         } 
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CPbk2CopyNumberToClipboardCmd::CopyNumberToClipboardL
       
   181 // --------------------------------------------------------------------------
       
   182 //    
       
   183 void CPbk2CopyNumberToClipboardCmd::CopyNumberToClipboardL()
       
   184     {
       
   185     // Construct a plain text from the number
       
   186     const TInt KBeginning = 0;
       
   187     
       
   188     MVPbkStoreContactField* field = FocusedFieldLC( *iStoreContact );
       
   189     TPtrC number = MVPbkContactFieldTextData::Cast(field->FieldData()).Text();   
       
   190     CleanupStack::PopAndDestroy(); // field 
       
   191     CPlainText* plainText = CPlainText::NewL();
       
   192     CleanupStack::PushL(plainText);
       
   193     plainText->InsertL(KBeginning, number);
       
   194 
       
   195     // Copy to clipboard
       
   196     CClipboard* cb = CClipboard::NewForWritingLC(
       
   197         iAppServices->ContactManager().FsSession());
       
   198     plainText->CopyToStoreL(cb->Store(), cb->StreamDictionary(),
       
   199         KBeginning, plainText->DocumentLength());
       
   200     cb->CommitL();
       
   201 
       
   202     CleanupStack::PopAndDestroy(2); // cb, plainText
       
   203     
       
   204     // Show a note
       
   205     HBufC* prompt = StringLoader::LoadLC( R_QTN_CCA_INFO_NOTE_NUMBER_COPIED_TO_CLIPBOARD );
       
   206     CAknInformationNote* dlg = new(ELeave) CAknInformationNote(ETrue);
       
   207     dlg->ExecuteLD(*prompt);
       
   208     CleanupStack::PopAndDestroy(); // prompt        
       
   209     }
       
   210     	
       
   211 MVPbkStoreContactField* CPbk2CopyNumberToClipboardCmd::FocusedFieldLC
       
   212     ( MVPbkStoreContact& aStoreContact )
       
   213     {
       
   214     TInt index = iUiControl->FocusedFieldIndex();
       
   215     if ( index != KErrNotFound )
       
   216         {
       
   217         // Use FieldAtLC to avoid the unvalidity of the field after new
       
   218         // FieldAt call.        
       
   219         return aStoreContact.Fields().FieldAtLC( index );
       
   220         }
       
   221     return NULL;
       
   222     }    
       
   223 
       
   224 // End of File