|
1 /* |
|
2 * Copyright (c) 2005-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 |
|
19 #include <e32def.h> |
|
20 |
|
21 #include "DataDealer.h" |
|
22 #include "BreathSaver.h" |
|
23 |
|
24 CBreathSaver* CBreathSaver::NewL() |
|
25 { |
|
26 CBreathSaver* self = new (ELeave) CBreathSaver(); |
|
27 CleanupStack::PushL(self); |
|
28 self->ConstructL(); |
|
29 CleanupStack::Pop(self); |
|
30 return self; |
|
31 } |
|
32 |
|
33 CBreathSaver::~CBreathSaver() |
|
34 { |
|
35 // Free DataDealer |
|
36 if (iDataDealer) |
|
37 { |
|
38 delete iDataDealer; |
|
39 iDataDealer = 0; |
|
40 } |
|
41 // Close handle to File server session |
|
42 iRFsession.Close(); |
|
43 } |
|
44 |
|
45 /** |
|
46 * Start function for data dealer |
|
47 * @param aKey Data dealer key |
|
48 * @return TInt. Error code. |
|
49 */ |
|
50 TInt CBreathSaver::StartDealer(const TUint aKey) |
|
51 { |
|
52 TInt error = KErrNone; |
|
53 // return error if DataDealer is already started |
|
54 if (iDealerStarted != EFalse) |
|
55 { |
|
56 error = KErrLocked; |
|
57 } |
|
58 else |
|
59 { |
|
60 // iDataDealer is deleted in End-function or in destructor and |
|
61 // Start cannot be called again before End is called first |
|
62 TRAP (error, iDataDealer = CDataDealer::NewL(ETrue, aKey)); |
|
63 // NewL failed |
|
64 if (error == KErrNone) |
|
65 { |
|
66 // DataDealer is started |
|
67 iDealerStarted = ETrue; |
|
68 } |
|
69 } |
|
70 |
|
71 return error; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Start function for file saving. This function replaces the file if it already exists |
|
76 * @param aFileName File name |
|
77 * @return TInt. Error code. |
|
78 */ |
|
79 TInt CBreathSaver::StartFile(const TDesC &aFileName) |
|
80 { |
|
81 TInt error = KErrNone; |
|
82 // Check that start is not called when somebody else have already called start |
|
83 // but not end |
|
84 if (iFileStarted != EFalse) |
|
85 { |
|
86 error = KErrLocked; |
|
87 } |
|
88 else |
|
89 { |
|
90 error = iFile.Replace(iRFsession,aFileName,EFileWrite|EFileStream); |
|
91 // Check that file replase was successful |
|
92 if (error == KErrNone) |
|
93 { |
|
94 // All must be fine so lets start file |
|
95 iFileStarted = ETrue; |
|
96 } |
|
97 } |
|
98 return error; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Appends Buffer into a started file. |
|
103 * @param aBuffer Buffer to be appened. |
|
104 * @return TInt. Error code. |
|
105 */ |
|
106 TInt CBreathSaver::Append(TDes8 &aBuffer) |
|
107 { |
|
108 // If nothing is started then nothing should be appended |
|
109 if (iFileStarted == EFalse && iDealerStarted == EFalse) |
|
110 { |
|
111 return KErrNotFound; |
|
112 } |
|
113 |
|
114 TInt error = KErrNone; |
|
115 |
|
116 // Check if file is started |
|
117 if (iFileStarted) |
|
118 { |
|
119 // Add more to existing file |
|
120 error = iFile.Write(aBuffer); |
|
121 if (error != KErrNone) |
|
122 { |
|
123 return error; |
|
124 } |
|
125 } |
|
126 // Check if DataDealer is started |
|
127 if (iDealerStarted) |
|
128 { |
|
129 // ProduceDataL waits until consumer has devoured all the previously |
|
130 // produced data |
|
131 TRAP (error, iDataDealer->ProduceDataL(aBuffer)); |
|
132 if (error != KErrNone) |
|
133 { |
|
134 return error; |
|
135 } |
|
136 } |
|
137 // more types can be added here |
|
138 return error; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Ends function for file saving. |
|
143 * @return TInt. Error code. |
|
144 */ |
|
145 TInt CBreathSaver::End() |
|
146 { |
|
147 TInt error = KErrNone; |
|
148 // If nothing is started then nothing should be ended |
|
149 if (iFileStarted == EFalse && iDealerStarted == EFalse) |
|
150 { |
|
151 error = KErrNotFound; |
|
152 } |
|
153 else |
|
154 { |
|
155 // Check if file is started |
|
156 if (iFileStarted) |
|
157 { |
|
158 // Flush and close file |
|
159 error = iFile.Flush(); |
|
160 iFile.Close(); |
|
161 // Now new file can be started |
|
162 iFileStarted = EFalse; |
|
163 } |
|
164 // Check if DataDealer is started |
|
165 if (iDealerStarted) |
|
166 { |
|
167 // free DataDealer, new dealer is born(captured?) in start function |
|
168 delete iDataDealer; |
|
169 iDataDealer = 0; |
|
170 // Now new data dealer can be started |
|
171 iDealerStarted = EFalse; |
|
172 } |
|
173 } |
|
174 return error; |
|
175 } |
|
176 |
|
177 CBreathSaver::CBreathSaver() |
|
178 { |
|
179 iFileStarted = EFalse; |
|
180 iDealerStarted = EFalse; |
|
181 iDataDealer = 0; |
|
182 } |
|
183 |
|
184 void CBreathSaver::ConstructL() |
|
185 { |
|
186 // Open file server session |
|
187 // Session is closed in destructor |
|
188 User::LeaveIfError(iRFsession.Connect()); |
|
189 } |
|
190 |