phonebookui/Phonebook2/CommandsExtension/src/CPbk2CopyDetailToClipboardCmd.cpp
changeset 0 e686773b3f54
child 7 b3431bff8c19
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:  A command for clipboard copy
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include "CPbk2CopyDetailToClipboardCmd.h"
       
    20 
       
    21 #include <MVPbkStoreContact.h>
       
    22 #include <MVPbkContactOperationBase.h>
       
    23 #include <CVPbkContactManager.h>
       
    24 #include <MVPbkContactFieldTextData.h>
       
    25 #include <MVPbkContactFieldDateTimeData.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 
       
    37 #include <txtetext.h>   // CPlainText
       
    38 #include <baclipb.h>    // CClipboard
       
    39 #include <StringLoader.h>
       
    40 #include <aknnotewrappers.h>    // CAknInformationNote
       
    41 
       
    42 
       
    43 // --------------------------------------------------------------------------
       
    44 // CPbk2CopyDetailToClipboardCmd::NewL
       
    45 // --------------------------------------------------------------------------
       
    46 //
       
    47 CPbk2CopyDetailToClipboardCmd* CPbk2CopyDetailToClipboardCmd::NewL(
       
    48         MPbk2ContactUiControl& aUiControl   )
       
    49     {
       
    50     CPbk2CopyDetailToClipboardCmd* self = 
       
    51         new ( ELeave ) CPbk2CopyDetailToClipboardCmd( aUiControl );
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop( self );
       
    55     return self;
       
    56     }
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // CPbk2CopyDetailToClipboardCmd::CPbk2CopyDetailToClipboardCmd
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62 CPbk2CopyDetailToClipboardCmd::CPbk2CopyDetailToClipboardCmd(
       
    63         MPbk2ContactUiControl& aUiControl) : 
       
    64     iUiControl(&aUiControl)
       
    65     {
       
    66     // do nothing
       
    67     }
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // CPbk2CopyDetailToClipboardCmd::~CPbk2CopyDetailToClipboardCmd
       
    71 // --------------------------------------------------------------------------
       
    72 //    
       
    73 CPbk2CopyDetailToClipboardCmd::~CPbk2CopyDetailToClipboardCmd()
       
    74     {
       
    75     delete iStoreContact;
       
    76     delete iRetrieveOperation;
       
    77     Release( iAppServices );
       
    78     }
       
    79 
       
    80 // --------------------------------------------------------------------------
       
    81 // CPbk2CopyDetailToClipboardCmd::ConstructL
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 inline void CPbk2CopyDetailToClipboardCmd::ConstructL()
       
    85     {
       
    86     iAppServices = CPbk2ApplicationServices::InstanceL();
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CPbk2CopyDetailToClipboardCmd::ExecuteLD
       
    91 // --------------------------------------------------------------------------
       
    92 //
       
    93 void CPbk2CopyDetailToClipboardCmd::ExecuteLD()
       
    94     {
       
    95     CleanupStack::PushL(this);
       
    96     
       
    97     if (iUiControl)
       
    98         {
       
    99         MVPbkContactLink* link = iUiControl->FocusedContactL()->CreateLinkLC();
       
   100         iRetrieveOperation = 
       
   101             iAppServices->ContactManager().RetrieveContactL(*link, *this);
       
   102         CleanupStack::PopAndDestroy(); // link
       
   103         }
       
   104     else
       
   105         {
       
   106         iObserver->CommandFinished(*this);                     
       
   107         }
       
   108     CleanupStack::Pop(this);
       
   109     }
       
   110 
       
   111 // --------------------------------------------------------------------------
       
   112 // CPbk2CopyDetailToClipboardCmd::AddObserver
       
   113 // --------------------------------------------------------------------------
       
   114 //    
       
   115 void CPbk2CopyDetailToClipboardCmd::AddObserver(MPbk2CommandObserver& aObserver)
       
   116     {
       
   117     iObserver = &aObserver;
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CPbk2CopyDetailToClipboardCmd::ResetUiControl
       
   122 // --------------------------------------------------------------------------
       
   123 //    
       
   124 void CPbk2CopyDetailToClipboardCmd::ResetUiControl(
       
   125         MPbk2ContactUiControl& aUiControl)
       
   126     {
       
   127     if (iUiControl == &aUiControl)
       
   128         {
       
   129         iUiControl = NULL;
       
   130         }
       
   131     }
       
   132 
       
   133 // --------------------------------------------------------------------------
       
   134 // CPbk2CopyDetailToClipboardCmd::VPbkSingleContactOperationComplete
       
   135 // --------------------------------------------------------------------------
       
   136 //
       
   137 void CPbk2CopyDetailToClipboardCmd::VPbkSingleContactOperationComplete(
       
   138     MVPbkContactOperationBase& aOperation,
       
   139     MVPbkStoreContact* aContact)
       
   140     {
       
   141     if (&aOperation == iRetrieveOperation)
       
   142         {
       
   143         delete iRetrieveOperation;
       
   144         iRetrieveOperation = NULL;
       
   145         
       
   146         delete iStoreContact;
       
   147         iStoreContact = aContact;
       
   148         // should not execute copyDetail command when
       
   149         // there is no UI Control
       
   150         if ( iUiControl )
       
   151             {
       
   152             TRAP_IGNORE(CopyDetailToClipboardL());
       
   153             iUiControl->UpdateAfterCommandExecution();
       
   154             }
       
   155         iObserver->CommandFinished(*this);                     
       
   156         }    
       
   157     }
       
   158 
       
   159 // --------------------------------------------------------------------------
       
   160 // CPbk2CopyDetailToClipboardCmd::VPbkSingleContactOperationFailed
       
   161 // --------------------------------------------------------------------------
       
   162 //    
       
   163 void CPbk2CopyDetailToClipboardCmd::VPbkSingleContactOperationFailed(
       
   164     MVPbkContactOperationBase& aOperation, 
       
   165     TInt /*aError*/)
       
   166     {
       
   167     if (&aOperation == iRetrieveOperation)
       
   168         {
       
   169         delete iRetrieveOperation;
       
   170         iRetrieveOperation = NULL;
       
   171 
       
   172         if (iUiControl)
       
   173             {
       
   174             iUiControl->UpdateAfterCommandExecution();
       
   175             }
       
   176         iObserver->CommandFinished(*this);
       
   177         } 
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CPbk2CopyDetailToClipboardCmd::CopyDetailToClipboardL
       
   182 // --------------------------------------------------------------------------
       
   183 //    
       
   184 void CPbk2CopyDetailToClipboardCmd::CopyDetailToClipboardL()
       
   185     {
       
   186     // Construct a plain text from the detail
       
   187     const TInt KBeginning = 0;
       
   188     // Fetch field from current UI control
       
   189     const MVPbkBaseContactField* field = iUiControl->FocusedField();
       
   190     TPtrC detail;
       
   191     TVPbkFieldStorageType fieldType = field->FieldData().DataType();
       
   192     
       
   193     switch(fieldType)
       
   194         {
       
   195         case EVPbkFieldStorageTypeText:
       
   196             {
       
   197             detail.Set(MVPbkContactFieldTextData::Cast(field->FieldData()).Text());
       
   198             break;
       
   199             }
       
   200         case EVPbkFieldStorageTypeDateTime:
       
   201             {
       
   202             TLocale locale;
       
   203             TBuf<64> dateBuffer;
       
   204 
       
   205             const MVPbkContactFieldDateTimeData& date = 
       
   206                 MVPbkContactFieldDateTimeData::Cast(field->FieldData());
       
   207 
       
   208             TTime time(date.DateTime());
       
   209 
       
   210             HBufC* dateFormat = CCoeEnv::Static()->AllocReadResourceLC
       
   211                 (R_QTN_DATE_USUAL_WITH_ZERO); // in avkon.rsg
       
   212             
       
   213             time.FormatL(dateBuffer, *dateFormat, locale);
       
   214             CleanupStack::PopAndDestroy(dateFormat);           
       
   215             
       
   216             detail.Set(dateBuffer);
       
   217             break;
       
   218             }
       
   219         }
       
   220 		
       
   221     CPlainText* plainText = CPlainText::NewL();
       
   222     CleanupStack::PushL(plainText);
       
   223     plainText->InsertL(KBeginning, detail);
       
   224 
       
   225     // Copy to clipboard
       
   226     CClipboard* cb = CClipboard::NewForWritingLC(
       
   227         iAppServices->ContactManager().FsSession());
       
   228     plainText->CopyToStoreL(cb->Store(), cb->StreamDictionary(),
       
   229         KBeginning, plainText->DocumentLength());
       
   230     cb->CommitL();
       
   231 
       
   232     CleanupStack::PopAndDestroy(2); // cb, plainText
       
   233 
       
   234     // Show a note
       
   235     HBufC* prompt = StringLoader::LoadLC(R_QTN_CCA_INFO_NOTE_COPIED_TO_CLIPBOARD);
       
   236     CAknInformationNote* dlg = new(ELeave) CAknInformationNote(ETrue);
       
   237     dlg->ExecuteLD(*prompt);
       
   238     CleanupStack::PopAndDestroy(prompt);
       
   239     }
       
   240 
       
   241 // End of File