camerauis/cameraxui/cxengine/src/cxeimagedataitemsymbian.cpp
changeset 19 d9aefe59d544
child 37 64817133cd1d
child 43 0e652f8f1fbd
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <bautils.h> // for deleting files
       
    19 #include "cxeimagedataitemsymbian.h"
       
    20 #include "cxeerrormappingsymbian.h"
       
    21 #include "cxesysutil.h"
       
    22 #include "cxutils.h"
       
    23 #include "cxestate.h"
       
    24 
       
    25 #include "OstTraceDefinitions.h"
       
    26 #ifdef OST_TRACE_COMPILER_IN_USE
       
    27 #include "cxeimagedataitemsymbianTraces.h"
       
    28 #endif
       
    29 
       
    30 
       
    31 CxeImageDataItemSymbian::CxeImageDataItemSymbian(QByteArray data,
       
    32                                                  QString filename,
       
    33                                                  int id,
       
    34                                                  CxeImageDataItem::State state)
       
    35   : CxeStateMachine("CxeImageDataItemSymbian"),
       
    36     mError(KErrNone),
       
    37     mId(id),
       
    38     mData(data),
       
    39     mPath(filename)
       
    40 {
       
    41     CX_DEBUG_ENTER_FUNCTION();
       
    42 
       
    43     qRegisterMetaType<CxeImageDataItem::State>();
       
    44     qRegisterMetaType<CxeError::Id>();
       
    45     // Init mState
       
    46     initializeStates();
       
    47     setInitialState(state);
       
    48     // Init delayer variables
       
    49     //mDelayedDelete = false;
       
    50     //mDelayedRename = false;
       
    51     //mDelayedFileName = NULL;
       
    52 
       
    53     CX_DEBUG_EXIT_FUNCTION();
       
    54 }
       
    55 
       
    56 CxeImageDataItemSymbian::~CxeImageDataItemSymbian()
       
    57 {
       
    58     CX_DEBUG_ENTER_FUNCTION();
       
    59 
       
    60     // Close file
       
    61     mFile.Close();
       
    62 
       
    63     // Close file system
       
    64     mFs.Close();
       
    65 
       
    66     CX_DEBUG_EXIT_FUNCTION();
       
    67 }
       
    68 
       
    69 /*
       
    70 void CxeImageDataItemSymbian::deleteImage()
       
    71     {
       
    72     CX_DEBUG_ENTER_FUNCTION();
       
    73 
       
    74     int err = KErrNone;
       
    75 
       
    76     //! @todo: make this function return a KErrNotReady if below
       
    77     if ( mState != CxeImageDataItem::Idle )
       
    78         {
       
    79         CX_DEBUG(("Error: This data item has no data..."));
       
    80         CX_DEBUG_ASSERT(0); // panics
       
    81         return;
       
    82         }
       
    83 
       
    84     // do delete or delayed delete
       
    85     if (( mState == CxeImageDataItem::Waiting ) || ( mState == CxeImageDataItem::Saving ))
       
    86         {
       
    87         // we are currently saving, so we have to delete later
       
    88         CX_DEBUG(("delayed delete"));
       
    89         mDelayedDelete = true;
       
    90         }
       
    91     else
       
    92         {
       
    93         // delete now
       
    94         CX_DEBUG(("deleting now..."));
       
    95         err = KErrNotFound;
       
    96 
       
    97 
       
    98         //! @todo: this is horrible for performance... there is no need to create multiple server sessions
       
    99         RFs fs;
       
   100         TInt connectError = fs.Connect();
       
   101         BaflUtils ba;
       
   102         if( !connectError && ba.FileExists( fs, *mPath ) )
       
   103             {
       
   104             err = KErrNone;
       
   105             ba.DeleteFile( fs, *mPath );
       
   106             }
       
   107         fs.Close();
       
   108         }
       
   109 
       
   110     CX_DEBUG(("err: %d", err));
       
   111 
       
   112     CX_DEBUG_EXIT_FUNCTION();
       
   113     //return err; //! @todo
       
   114     }
       
   115 
       
   116 void CxeImageDataItemSymbian::renameImage( const TDesC& newPath )
       
   117     {
       
   118     CX_DEBUG_ENTER_FUNCTION();
       
   119 
       
   120     int err = KErrNone;
       
   121 
       
   122     //! @todo: make this function return a KErrNotReady if below
       
   123     if ( mState != CxeImageDataItem::Idle )
       
   124         {
       
   125         CX_DEBUG(("Error: This data item has no data..."));
       
   126         CX_DEBUG_ASSERT(0); // panics
       
   127         return;
       
   128         }
       
   129 
       
   130     // do rename or delayed rename
       
   131     if (( mState == CxeImageDataItem::Waiting ) || ( mState == CxeImageDataItem::Saving ))
       
   132         {
       
   133         // we are currently saving, so we have to rename later
       
   134         CX_DEBUG(("delayed rename"));
       
   135         mDelayedRename = true;
       
   136 
       
   137         TRAP( err,
       
   138             mDelayedFileName = HBufC::NewL( newPath.Length() );
       
   139             mDelayedFileName->Des().Append( newPath );
       
   140             );
       
   141         }
       
   142     else
       
   143         {
       
   144         // rename now
       
   145         CX_DEBUG(("delayed rename"));
       
   146         err = KErrNotFound;
       
   147         RFs fs;
       
   148         TInt connectError = fs.Connect();
       
   149         BaflUtils ba;
       
   150         if( !connectError && ba.FileExists( fs, *mPath ) )
       
   151             {
       
   152             err = KErrNone;
       
   153             ba.RenameFile( fs, *mPath, newPath );
       
   154             }
       
   155         fs.Close();
       
   156         }
       
   157 
       
   158     CX_DEBUG(("err: %d", err));
       
   159 
       
   160     CX_DEBUG_EXIT_FUNCTION();
       
   161     //return err; //! @todo
       
   162     }
       
   163 */
       
   164 
       
   165 CxeError::Id CxeImageDataItemSymbian::save()
       
   166 {
       
   167     CX_DEBUG_ENTER_FUNCTION();
       
   168 
       
   169     mError = KErrNone;
       
   170 
       
   171     CX_DEBUG(( "Starting to save %s", mPath.toAscii().constData() ));
       
   172 
       
   173     if (mPath.isEmpty()) {
       
   174         CX_DEBUG(("Filename not set !"));
       
   175         mError = KErrArgument;
       
   176     }
       
   177 
       
   178     TPtrC16 filename;
       
   179 
       
   180     if (!mError) {
       
   181         filename.Set(reinterpret_cast<const TUint16*>(mPath.utf16()));
       
   182         // Init
       
   183         mError = mFs.Connect();
       
   184         CX_DEBUG(("mFsSession.Connect mError=%d", mError));
       
   185     }
       
   186 
       
   187     // Get drive number
       
   188     TInt drive = 0;
       
   189     if (!mError) {
       
   190         mError = RFs::CharToDrive(filename[0], drive);
       
   191         CX_DEBUG(("CharToDrive mError=%d", mError));
       
   192     }
       
   193 
       
   194     // Check disk space
       
   195     if (!mError) {
       
   196         TBool fullDisk = EFalse;
       
   197         fullDisk = checkDiskSpace(&mFs, mData.size(), drive);
       
   198         if (fullDisk) {
       
   199             CX_DEBUG(("SysUtil::FullDisk"));
       
   200             mError = KErrDiskFull;
       
   201         }
       
   202     }
       
   203 
       
   204     // Attempt to create the file
       
   205     if (!mError) {
       
   206         // Note: In sake of MDS not starting harvesting here,
       
   207         // do not use RFile::Replace. If harvesting is started now,
       
   208         // our later call to harvest may be ignored and
       
   209         // file may be missing from "Captured" album.
       
   210         mError = mFile.Create(mFs, filename, EFileWrite);
       
   211         CX_DEBUG(("file.Create mError=%d", mError));
       
   212     }
       
   213 
       
   214     // Write the file
       
   215     if (!mError) {
       
   216         // Update state
       
   217         setState(CxeImageDataItem::Saving);
       
   218 
       
   219         CX_DEBUG(("about to write to file"));
       
   220         TPtrC8 data(reinterpret_cast<const TUint8*> (mData.constData()), mData.size());
       
   221         mError = mFile.Write(data); // synchronous
       
   222         saveCleanup();
       
   223         CX_DEBUG(("file write completed"));
       
   224     }
       
   225 
       
   226     mFile.Close(); //~400us
       
   227     mFs.Close();   //~450us
       
   228     OstTrace0(camerax_performance, CXEIMAGEDATAIMTEMSYMBIAN_SAVED, "msg: e_CX_SHOT_TO_SAVE 0");
       
   229 
       
   230     if (mError == KErrNone) {
       
   231         setState(CxeImageDataItem::Saved);
       
   232     } else {
       
   233         setState(CxeImageDataItem::SaveFailed);
       
   234     }
       
   235     emit imageSaved(CxeErrorHandlingSymbian::map(mError), mPath, mId);
       
   236 
       
   237     CX_DEBUG(("mError: %d", mError));
       
   238     CX_DEBUG_EXIT_FUNCTION();
       
   239     return CxeErrorHandlingSymbian::map(mError);
       
   240 }
       
   241 
       
   242 /**
       
   243 * Get the id number of this data item.
       
   244 */
       
   245 int CxeImageDataItemSymbian::id() const
       
   246 {
       
   247     return mId;
       
   248 }
       
   249 
       
   250 /**
       
   251 * Get the path of this data item.
       
   252 */
       
   253 QString CxeImageDataItemSymbian::path() const
       
   254 {
       
   255     return mPath;
       
   256 }
       
   257 
       
   258 
       
   259 int CxeImageDataItemSymbian::checkDiskSpace(RFs* aFs,
       
   260         TInt aBytesToWrite,
       
   261         TInt aDrive)
       
   262 {
       
   263     CX_DEBUG_ENTER_FUNCTION();
       
   264     int value = CxeSysUtil::DiskSpaceBelowCriticalLevel(
       
   265             aFs,
       
   266             aBytesToWrite,
       
   267             aDrive );
       
   268     return value;
       
   269 }
       
   270 
       
   271 void CxeImageDataItemSymbian::saveCleanup()
       
   272 {
       
   273     CX_DEBUG_ENTER_FUNCTION();
       
   274 
       
   275     CX_DEBUG_ASSERT( state() == CxeImageDataItem::Saving );
       
   276 
       
   277     // Flush file.
       
   278     if (!mError) {
       
   279         CX_DEBUG(("flushing..."));
       
   280         mError = mFile.Flush();
       
   281         CX_DEBUG(("flushed"));
       
   282     }
       
   283 
       
   284 
       
   285     /*
       
   286     // Delayed rename, if needed
       
   287     if (( !mError ) && ( mDelayedRename ))
       
   288         {
       
   289         CX_DEBUG(("doing delayed rename..."));
       
   290         mError = KErrNotFound;
       
   291         BaflUtils ba;
       
   292         if( ba.FileExists( mFs, *mPath ) )
       
   293             {
       
   294             mError = KErrNone;
       
   295             TPtrC newPath = *mDelayedFileName;
       
   296             ba.RenameFile( mFs, *mPath, newPath );
       
   297             }
       
   298         mDelayedRename = false;
       
   299         CX_DEBUG(("rename done, mError: %d", mError));
       
   300         }
       
   301 
       
   302     // Delayed delete, if needed
       
   303     if (( !mError ) && ( mDelayedDelete ))
       
   304         {
       
   305         CX_DEBUG(("doing delayed delete..."));
       
   306         mError = KErrNotFound;
       
   307         BaflUtils ba;
       
   308         if( ba.FileExists( mFs, *mPath ) )
       
   309             {
       
   310             mError = KErrNone;
       
   311             ba.DeleteFile( mFs, *mPath );
       
   312             }
       
   313         mDelayedDelete = false;
       
   314         CX_DEBUG(("delete done, mError: %d", mError));
       
   315         }*/
       
   316 
       
   317     CX_DEBUG_EXIT_FUNCTION();
       
   318 }
       
   319 
       
   320 CxeImageDataItem::State CxeImageDataItemSymbian::state() const
       
   321 {
       
   322     return static_cast<CxeImageDataItem::State> (stateId());
       
   323 }
       
   324 
       
   325 void CxeImageDataItemSymbian::handleStateChanged(int newStateId, CxeError::Id error)
       
   326 {
       
   327     emit stateChanged(static_cast<State> (newStateId), error);
       
   328 }
       
   329 
       
   330 void CxeImageDataItemSymbian::initializeStates()
       
   331 {
       
   332     // addState( id, name, allowed next states )
       
   333     addState(new CxeState(SavePending, "SavePending", Saving | SaveFailed));
       
   334     addState(new CxeState(Saving, "Saving", Saved | SaveFailed));
       
   335     addState(new CxeState(Saved, "Saved", 0));
       
   336     addState(new CxeState(SaveFailed, "SaveFailed", 0));
       
   337 }