1 /* |
|
2 * Copyright (c) 2009-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 #include "cxeimagedataitemsymbian.h" |
|
19 #include "cxeerrormappingsymbian.h" |
|
20 #include "cxesysutil.h" |
|
21 #include "cxutils.h" |
|
22 #include "cxestate.h" |
|
23 |
|
24 #include "OstTraceDefinitions.h" |
|
25 #ifdef OST_TRACE_COMPILER_IN_USE |
|
26 #include "cxeimagedataitemsymbianTraces.h" |
|
27 #endif |
|
28 |
|
29 /*! |
|
30 * Constructor. |
|
31 */ |
|
32 CxeImageDataItemSymbian::CxeImageDataItemSymbian(QByteArray data, |
|
33 QString filename, |
|
34 int id, |
|
35 bool addLocation, |
|
36 CxeImageDataItem::State state) |
|
37 : CxeStateMachine("CxeImageDataItemSymbian"), |
|
38 mId(id), |
|
39 mData(data), |
|
40 mAddLocationInfo(addLocation), |
|
41 mPath(filename) |
|
42 { |
|
43 CX_DEBUG_ENTER_FUNCTION(); |
|
44 |
|
45 qRegisterMetaType<CxeImageDataItem::State>(); |
|
46 qRegisterMetaType<CxeError::Id>(); |
|
47 // Init mState |
|
48 initializeStates(); |
|
49 setInitialState(state); |
|
50 |
|
51 CX_DEBUG_EXIT_FUNCTION(); |
|
52 } |
|
53 |
|
54 /*! |
|
55 * Destructor. |
|
56 */ |
|
57 CxeImageDataItemSymbian::~CxeImageDataItemSymbian() |
|
58 { |
|
59 CX_DEBUG_ENTER_FUNCTION(); |
|
60 closeHandles(); |
|
61 CX_DEBUG_EXIT_FUNCTION(); |
|
62 } |
|
63 |
|
64 /*! |
|
65 * Save the data now. |
|
66 * @return Status code. |
|
67 */ |
|
68 CxeError::Id CxeImageDataItemSymbian::save() |
|
69 { |
|
70 CX_DEBUG_ENTER_FUNCTION(); |
|
71 |
|
72 CxeError::Id status(CxeError::None); |
|
73 try { |
|
74 setState(CxeImageDataItem::Saving); |
|
75 trySave(); |
|
76 setState(CxeImageDataItem::Saved); |
|
77 } catch (const std::exception &e) { |
|
78 closeHandles(); |
|
79 status = CxeErrorHandlingSymbian::map(qt_symbian_exception2Error(e)); |
|
80 setState(CxeImageDataItem::SaveFailed); |
|
81 } |
|
82 |
|
83 emit imageSaved(status, mPath, mId); |
|
84 |
|
85 CX_DEBUG_EXIT_FUNCTION(); |
|
86 return status; |
|
87 } |
|
88 |
|
89 /*! |
|
90 * Helper method for trying to save the data. |
|
91 * If any error is encountered during the saving process, exception is thrown. |
|
92 */ |
|
93 void CxeImageDataItemSymbian::trySave() |
|
94 { |
|
95 CX_DEBUG_ENTER_FUNCTION(); |
|
96 OstTrace0(camerax_performance, CXEIMAGEDATAITEMSYMBIAN_SAVE_IN, "msg: e_CX_IMAGEDATAITEM_SAVE 1"); |
|
97 |
|
98 CX_DEBUG(("CxeImageDataItemSymbian - Starting to save [%s]", qPrintable(mPath))); |
|
99 |
|
100 // Check we have the path set. |
|
101 if (mPath.isEmpty()) { |
|
102 CX_DEBUG(("CxeImageDataItemSymbian - Filename not set!")); |
|
103 qt_symbian_throwIfError(KErrArgument); |
|
104 } |
|
105 |
|
106 TPtrC16 filename; |
|
107 filename.Set(reinterpret_cast<const TUint16*>(mPath.utf16())); |
|
108 // Init |
|
109 CX_DEBUG(("CxeImageDataItemSymbian - connect to RFs..")); |
|
110 qt_symbian_throwIfError(mFs.Connect()); |
|
111 |
|
112 // Get drive number |
|
113 TInt drive = 0; |
|
114 CX_DEBUG(("CxeImageDataItemSymbian - Get drive number..")); |
|
115 qt_symbian_throwIfError(RFs::CharToDrive(filename[0], drive)); |
|
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); |
|
122 } |
|
123 |
|
124 // Attempt to create the file |
|
125 // Note: In sake of MDS not starting harvesting here, |
|
126 // do not use RFile::Replace. If harvesting is started now, |
|
127 // our later call to harvest may be ignored and |
|
128 // file may be missing from "Captured" album. |
|
129 CX_DEBUG(("CxeImageDataItemSymbian - Create the file..")); |
|
130 qt_symbian_throwIfError(mFile.Create(mFs, filename, EFileWrite)); |
|
131 |
|
132 // Write data to the file. |
|
133 CX_DEBUG(("CxeImageDataItemSymbian - Starting to write the file..")); |
|
134 TPtrC8 data(reinterpret_cast<const TUint8*> (mData.constData()), mData.size()); |
|
135 qt_symbian_throwIfError(mFile.Write(data)); // synchronous |
|
136 |
|
137 // Flush all the data to file now. |
|
138 // This may take a while depending on buffer sizes and file server load. |
|
139 OstTrace0(camerax_performance, CXEIMAGEDATAITEMSYMBIAN_FLUSH_1, "msg: e_CX_SAVE_FLUSH_FILE 1"); |
|
140 qt_symbian_throwIfError(mFile.Flush()); |
|
141 OstTrace0(camerax_performance, CXEIMAGEDATAITEMSYMBIAN_FLUSH_2, "msg: e_CX_SAVE_FLUSH_FILE 0"); |
|
142 |
|
143 // Close the file and server handles. |
|
144 closeHandles(); |
|
145 CX_DEBUG(("CxeImageDataItemSymbian - Saving to file completed..")); |
|
146 |
|
147 OstTrace0(camerax_performance, CXEIMAGEDATAITEMSYMBIAN_SAVED, "msg: e_CX_SHOT_TO_SAVE 0"); |
|
148 OstTrace0(camerax_performance, CXEIMAGEDATAITEMSYMBIAN_SAVE_OUT, "msg: e_CX_IMAGEDATAITEM_SAVE 0"); |
|
149 CX_DEBUG_EXIT_FUNCTION(); |
|
150 } |
|
151 |
|
152 /*! |
|
153 * Get the id number of this data item. |
|
154 */ |
|
155 int CxeImageDataItemSymbian::id() const |
|
156 { |
|
157 return mId; |
|
158 } |
|
159 |
|
160 /*! |
|
161 * Get the path of this data item. |
|
162 */ |
|
163 QString CxeImageDataItemSymbian::path() const |
|
164 { |
|
165 return mPath; |
|
166 } |
|
167 |
|
168 /*! |
|
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, |
|
176 TInt aBytesToWrite, |
|
177 TInt aDrive) |
|
178 { |
|
179 CX_DEBUG_ENTER_FUNCTION(); |
|
180 bool value = CxeSysUtil::DiskSpaceBelowCriticalLevel( |
|
181 aFs, |
|
182 aBytesToWrite, |
|
183 aDrive ); |
|
184 return value; |
|
185 } |
|
186 |
|
187 /*! |
|
188 * State of this item. |
|
189 * @return The state. |
|
190 */ |
|
191 CxeImageDataItem::State CxeImageDataItemSymbian::state() const |
|
192 { |
|
193 return static_cast<CxeImageDataItem::State> (stateId()); |
|
194 } |
|
195 |
|
196 /*! |
|
197 * Handle state change. |
|
198 */ |
|
199 void CxeImageDataItemSymbian::handleStateChanged(int newStateId, CxeError::Id error) |
|
200 { |
|
201 emit stateChanged(static_cast<State> (newStateId), error); |
|
202 } |
|
203 |
|
204 /*! |
|
205 * Is location tagging enabled for this item. |
|
206 * @return True if location tagging is enabled, false otherwise. |
|
207 */ |
|
208 bool CxeImageDataItemSymbian::isLocationEnabled() const |
|
209 { |
|
210 return mAddLocationInfo; |
|
211 } |
|
212 |
|
213 /*! |
|
214 * Init the state machine. |
|
215 */ |
|
216 void CxeImageDataItemSymbian::initializeStates() |
|
217 { |
|
218 // addState( id, name, allowed next states ) |
|
219 addState(new CxeState(SavePending, "SavePending", Saving | SaveFailed)); |
|
220 addState(new CxeState(Saving, "Saving", Saved | SaveFailed)); |
|
221 addState(new CxeState(Saved, "Saved", 0)); |
|
222 addState(new CxeState(SaveFailed, "SaveFailed", 0)); |
|
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 |
|