phoneclientserver/phoneserver/Src/ImageHandler/cphsrvimagesaver.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2008 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:  Image saver implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cphsrvimagesaver.h"
       
    20 #include "mphsrvimagesaverobserver.h"
       
    21 #include <imageconversion.h>
       
    22 #include <f32file.h>
       
    23 #include <bitmaptransforms.h>
       
    24 
       
    25 // primary file name for VT image
       
    26 _LIT( KPhSrvPrimaryVtImageFileName, "vtstillimage1" );
       
    27 
       
    28 // file name used if primary file is in use while save occurs
       
    29 _LIT( KPhSrvAlternateVtImageFileName, "vtstillimage2" );
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // 1st constructor
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CPhSrvImageSaver* CPhSrvImageSaver::NewL( 
       
    38         RFs& aFs,
       
    39         const TFileName& aPrivatePath, 
       
    40         MPhSrvImageSaverObserver& aObserver )
       
    41     {
       
    42     CPhSrvImageSaver* saver = new ( ELeave ) CPhSrvImageSaver( 
       
    43         aFs, aPrivatePath, aObserver );
       
    44     CleanupStack::PushL( saver );
       
    45     saver->ConstructL();
       
    46     CleanupStack::Pop();
       
    47     return saver;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // destructor
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CPhSrvImageSaver::~CPhSrvImageSaver()
       
    55     {
       
    56     delete iImageDecoder;
       
    57     delete iFileManager;
       
    58     delete iScaler;
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Saves image
       
    63 // 1) decode from file
       
    64 // 2a) scale (for single frame images) and save to phone server private dir, or
       
    65 // 2b) multiframe image - copy file as is to phone server private directory
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 void CPhSrvImageSaver::SaveImageL( const TDesC& aFileName )
       
    69     {
       
    70     if ( IsActive() )
       
    71         {
       
    72         User::Leave( KErrInUse );
       
    73         }
       
    74     iImageDecoder = CImageDecoder::FileNewL( iFs, aFileName );
       
    75     const TInt frameCount = iImageDecoder->FrameCount();
       
    76     if ( frameCount > 1 )
       
    77         {
       
    78         // decoder not needed because image is copied as is
       
    79         delete iImageDecoder;
       
    80         iImageDecoder = NULL;
       
    81         CopyImageL( aFileName );
       
    82         }
       
    83     else if ( frameCount == 1 )
       
    84         {
       
    85         ScaleImageL();
       
    86         }
       
    87     else
       
    88         {
       
    89         // invalid image?
       
    90         User::Leave( KErrCorrupt );
       
    91         }
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // Copies the file
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CPhSrvImageSaver::CopyImageL( const TDesC& aFileName )
       
    99     {
       
   100     TName targetFileName;
       
   101     ConstructVtFileSaveName( aFileName, targetFileName );
       
   102     iFileManager = CFileMan::NewL( iFs );
       
   103     iFileManager->Copy( aFileName, targetFileName, 
       
   104         CFileMan::EOverWrite, iStatus );    
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // ScaleImageL
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CPhSrvImageSaver::ScaleImageL()
       
   112     {
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Checks if primary file is in use.
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 TInt CPhSrvImageSaver::IsPrimaryFileInUse( TBool& aIsInUse )
       
   120     {
       
   121     CDir* entries = NULL;
       
   122     TInt res = iFs.GetDir( iPath, KEntryAttNormal, EDirsLast, entries );
       
   123     if ( res == KErrNone && entries )
       
   124         {
       
   125         const TInt entryCount( entries->Count() );
       
   126         for ( TInt index = 0; index < entryCount; index++ )
       
   127             {
       
   128             const TEntry& entry = ( *entries )[index];            
       
   129             if ( entry.iName.Compare( KPhSrvPrimaryVtImageFileName() ) >= 0 )
       
   130                 {
       
   131                 // primary file name matches with directory entry (extension
       
   132                 // depends on file format)
       
   133                 res = iFs.IsFileOpen( entry.iName, aIsInUse );
       
   134                 index = entryCount; // break loop
       
   135                 }
       
   136             }
       
   137         }
       
   138     delete entries;
       
   139     return res;
       
   140     }
       
   141     
       
   142 // ---------------------------------------------------------------------------
       
   143 // Constructs target file name for VT still image. Alternate name is used if
       
   144 // file with primary name is open.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 TInt CPhSrvImageSaver::ConstructVtFileSaveName(
       
   148     const TDesC& aSourceFileName, 
       
   149     TName& aTargetFileName )
       
   150     {
       
   151     TBool inUse( EFalse );
       
   152     TInt res( IsPrimaryFileInUse( inUse ) );
       
   153     if ( res == KErrNone )
       
   154         {
       
   155         TParse sourceName;
       
   156         sourceName.Set( aSourceFileName, NULL, NULL );
       
   157         aTargetFileName = iPath;        
       
   158         if ( inUse )
       
   159             {
       
   160             // primary file in use (open), use alternalte name
       
   161             aTargetFileName.Append( KPhSrvAlternateVtImageFileName() );
       
   162             }
       
   163         else
       
   164             {
       
   165             aTargetFileName.Append( KPhSrvPrimaryVtImageFileName() );
       
   166             }
       
   167         aTargetFileName.Append( sourceName.Ext() );
       
   168         }
       
   169     return res;
       
   170     }
       
   171     
       
   172 // ---------------------------------------------------------------------------
       
   173 // CPhSrvImageSaver::RunL
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CPhSrvImageSaver::RunL()
       
   177     {
       
   178     iObserver.ImageSaved( iStatus.Int() );
       
   179     }
       
   180     
       
   181 // ---------------------------------------------------------------------------
       
   182 // CPhSrvImageSaver::DoCancel
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 void CPhSrvImageSaver::DoCancel()
       
   186     {
       
   187     iObserver.ImageSaved( KErrCancel );
       
   188     }
       
   189     
       
   190 // ---------------------------------------------------------------------------
       
   191 // CPhSrvImageSaver::ConstructL
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CPhSrvImageSaver::ConstructL()
       
   195     {
       
   196     User::Leave( KErrNotSupported );
       
   197     }
       
   198     
       
   199 // ---------------------------------------------------------------------------
       
   200 // c++ constructor
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 CPhSrvImageSaver::CPhSrvImageSaver( RFs& aFs, const TFileName& aPrivatePath,
       
   204     MPhSrvImageSaverObserver& aObserver) 
       
   205     : CActive( CActive::EPriorityStandard ), 
       
   206     iFs( aFs ), 
       
   207     iPath( aPrivatePath ),
       
   208     iObserver( aObserver )
       
   209     {
       
   210     }