camerauis/cameraxui/cxengine/src/cxefilesavethreaddesktop.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 2010 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 
       
    19 #include "cxefilesavethreaddesktop.h"
       
    20 #include "cxeimagedataitem.h"  // item
       
    21 #include "cxutils.h" // debug
       
    22 
       
    23 /*!
       
    24     \class CxeFileSaveThreadDesktop
       
    25     \brief Still image saving thread    
       
    26 */
       
    27 
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 CxeFileSaveThreadDesktop::CxeFileSaveThreadDesktop(QObject *parent)
       
    32     : CxeFileSaveThread(parent), mExitThread(false), mExit(false)
       
    33 {
       
    34     start(IdlePriority);
       
    35 }
       
    36 
       
    37 CxeFileSaveThreadDesktop::~CxeFileSaveThreadDesktop()
       
    38 {
       
    39     CX_DEBUG_ENTER_FUNCTION();
       
    40     mMutex.lock();
       
    41     mExitThread = true;
       
    42     mDataToSave.wakeOne();
       
    43     mMutex.unlock();
       
    44 
       
    45     wait(); // until the thread has finished execution.
       
    46     qDeleteAll(mDataList);  // Ensure destruction
       
    47     mDataList.clear();
       
    48     CX_DEBUG_EXIT_FUNCTION();
       
    49 }
       
    50 
       
    51 void CxeFileSaveThreadDesktop::save(CxeImageDataItem *data)
       
    52 {
       
    53     CX_DEBUG_ENTER_FUNCTION();
       
    54 
       
    55     // Ensure safe data adding. 
       
    56     // Saving thread will wait if needed, in read method, until mutex is unlocked
       
    57     mMutex.lock();
       
    58     mDataList.append(data);
       
    59     // Wake up saving thread if it's sleeping
       
    60     mDataToSave.wakeOne(); 
       
    61     mMutex.unlock();
       
    62 
       
    63     CX_DEBUG_EXIT_FUNCTION();
       
    64 }
       
    65 
       
    66 void CxeFileSaveThreadDesktop::handleVideoSaved(CxeError::Id status, const QString &filename)
       
    67 {
       
    68     Q_UNUSED(status);
       
    69     Q_UNUSED(filename);
       
    70 }
       
    71 
       
    72 void CxeFileSaveThreadDesktop::handleSnapshotReady(CxeError::Id status, const QImage &snapshot, const QString &filename)
       
    73 {
       
    74     Q_UNUSED(status);
       
    75     Q_UNUSED(snapshot);
       
    76     Q_UNUSED(filename);
       
    77 }
       
    78 
       
    79 void CxeFileSaveThreadDesktop::handleSnapshotReady(CxeError::Id status, const QImage &snapshot, int id)
       
    80 {
       
    81     Q_UNUSED(status);
       
    82     Q_UNUSED(snapshot);
       
    83     Q_UNUSED(id);
       
    84 }
       
    85 
       
    86 void CxeFileSaveThreadDesktop::read()
       
    87 {
       
    88     mMutex.lock();
       
    89     mCount = mDataList.count();
       
    90     mExit = mExitThread;
       
    91     mMutex.unlock();
       
    92 }
       
    93 
       
    94 void CxeFileSaveThreadDesktop::run()
       
    95 {
       
    96     CX_DEBUG_ENTER_FUNCTION();
       
    97     
       
    98     CxeImageDataItem *item = NULL;
       
    99     int i = 0;
       
   100     // Check if there is data to save.
       
   101     // There should not be any, because the thread is just contructed
       
   102     read();
       
   103 
       
   104     while (!mExit || i < mCount) { // Complete save before exit
       
   105         CX_DEBUG(("CxeFileSaveThreadDesktop: index %d", i));
       
   106         // Wait data
       
   107         if (!mExit && i >= mCount) {
       
   108             // If there isn't any data to save, put the thread sleeping
       
   109             CX_DEBUG(("CxeFileSaveThreadDesktop: set thread sleeping"));
       
   110             mMutex.lock();
       
   111             //! @todo: read datalist count here before clearing, because there is some machine cycles after previous read 
       
   112             // Clear the datalist, since all items are saved
       
   113             mDataList.clear();
       
   114             i = 0;
       
   115             mDataToSave.wait(&mMutex); // waiting "wakeOne"
       
   116             mMutex.unlock();
       
   117             CX_DEBUG(("CxeFileSaveThreadDesktop: woken up"));
       
   118         }
       
   119 
       
   120         // There should be data now, because the thread is woken up
       
   121         read();
       
   122 
       
   123         if (i < mCount) { 
       
   124             item = mDataList[i];
       
   125         } else {
       
   126             CX_DEBUG(("CxeFileSaveThreadDesktop: ERROR - woke up without data"));
       
   127             continue;
       
   128         }
       
   129 
       
   130         // Check that item is not saved already
       
   131         if (item && item->state() == CxeImageDataItem::SavePending) {
       
   132             item->save();// Error ignored since it is emitted from CxeImageDataItemSymbian
       
   133             // Delete item, since we own it
       
   134             delete item; 
       
   135             item = NULL;
       
   136         }
       
   137 
       
   138         // Saving takes some seconds, there might be new data available.
       
   139         read();
       
   140         ++i;
       
   141     }
       
   142   
       
   143     CX_DEBUG_EXIT_FUNCTION();
       
   144 }