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