Tizen 2.1 base
[platform/framework/native/content.git] / src / FCntContentTransfer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FCntContentTransfer.cpp
19  * @brief               This is the implementation file for the %ContentTransfer class.
20  *
21  * This file contains implementation of the %ContentTransfer class.
22  */
23
24 // includes
25 #include <FBaseSysLog.h>
26 #include <FCntContentTransfer.h>
27 #include <FBaseColIList.h>
28 #include <FBaseUtilUri.h>
29 #include <FCnt_ContentTransferImpl.h>
30 #include <FSec_AccessController.h>
31
32 // using namespace
33 using namespace Tizen::Base;
34 using namespace Tizen::Security;
35
36 namespace Tizen { namespace Content
37 {
38 ContentTransfer::ContentTransfer(void)
39         : Object()
40         , __pImpl(null)
41 {
42
43 }
44
45 ContentTransfer::~ContentTransfer(void)
46 {
47         if (__pImpl != null)
48         {
49                 delete __pImpl;
50                 __pImpl = null;
51         }
52 }
53
54 result
55 ContentTransfer::Construct(IContentTransferListener& listener)
56 {
57         ClearLastResult();
58         result r = E_SUCCESS;
59
60         SysAssertf(__pImpl == null,
61                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
62
63         _ContentTransferImpl* pContentTransferImpl = new (std::nothrow) _ContentTransferImpl();
64         SysTryReturn(NID_CNT, pContentTransferImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Construct failed.");
65
66         r = pContentTransferImpl->Construct(listener);
67         SysTryCatch(NID_CNT, r == E_SUCCESS, , r, "[%S] Propagating.", GetErrorMessage(r));
68
69         __pImpl = pContentTransferImpl;
70
71         return E_SUCCESS;
72
73 CATCH:
74         delete pContentTransferImpl;
75         pContentTransferImpl = null;
76
77         return r;
78 }
79
80 result
81 ContentTransfer::Cancel(RequestId reqId)
82 {
83         ClearLastResult();
84         result r = E_SUCCESS;
85
86         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
87
88         r = __pImpl->Cancel(reqId);
89         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Cancel failed.", GetErrorMessage(r));
90
91         return r;
92 }
93
94 void
95 ContentTransfer::SetDefaultTimeout(int sec)
96 {
97         if (__pImpl == null)
98         {
99                 SysLog(NID_CNT, "__pImpl is null.");
100                 return;
101         }
102         __pImpl->SetDefaultTimeout(sec);
103 }
104
105 int
106 ContentTransfer::GetDefaultTimeout(void) const
107 {
108         if (__pImpl == null)
109         {
110                 SysLog(NID_CNT, "__pImpl is null.");
111                 return 0;
112         }
113         return __pImpl->GetDefaultTimeout();
114 }
115
116 result
117 ContentTransfer::Remove(RequestId reqId)
118 {
119         ClearLastResult();
120         result r = E_SUCCESS;
121
122         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
123
124         r = __pImpl->Remove(reqId);
125         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Remove failed.", GetErrorMessage(r));
126
127         return r;
128 }
129
130 result
131 ContentTransfer::RemoveAll(void)
132 {
133         ClearLastResult();
134         result r = E_SUCCESS;
135
136         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
137
138         r = __pImpl->RemoveAll();
139         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] RemoveAll failed.", GetErrorMessage(r));
140
141         return r;
142 }
143
144 result
145 ContentTransfer::CancelAll(void)
146 {
147         ClearLastResult();
148         result r = E_SUCCESS;
149
150         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
151
152         r = __pImpl->CancelAll();
153         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] CancelAll failed.", GetErrorMessage(r));
154
155         return r;
156 }
157
158 Collection::IList*
159 ContentTransfer::GetContentTransferInfoListN(void) const
160 {
161         if (__pImpl == null)
162         {
163                 SysLog(NID_CNT, "__pImpl is null.");
164                 return null;
165         }
166         return __pImpl->GetContentTransferInfoListN();
167 }
168
169 Collection::IList*
170 ContentTransfer::GetContentTransferInfoListInProgressN(void) const
171 {
172         if (__pImpl == null)
173         {
174                 SysLog(NID_CNT, "__pImpl is null.");
175                 return null;
176         }
177         return __pImpl->GetContentTransferInfoListInProgressN();
178 }
179
180 void
181 ContentTransfer::SetProgressIntervalByPercent(int percent)
182 {
183         if (__pImpl == null)
184         {
185                 SysLog(NID_CNT, "__pImpl is null.");
186                 return;
187         }
188         __pImpl->SetProgressIntervalByPercent(percent);
189 }
190
191 result
192 ContentTransfer::Download(const Utility::Uri& uri, int fileSize, const String& destFilePath, bool replace,
193                 RequestId& reqId, IContentTransferListener* pListener, int sec)
194 {
195         ClearLastResult();
196         result r = E_SUCCESS;
197
198         // Checks the privilege
199         r = _AccessController::CheckUserPrivilege(_PRV_DOWNLOAD);
200         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
201                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
202
203         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
204
205         r = __pImpl->Download(uri, fileSize, destFilePath, replace, reqId, pListener, sec);
206         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Download failed.", GetErrorMessage(r));
207
208         return r;
209 }
210
211 result
212 ContentTransfer::Download(const Utility::Uri& uri, const String& filePath,
213                                                   RequestId& reqId, bool replace, int timeout, int progressInterval)
214 {
215         ClearLastResult();
216         result r = E_SUCCESS;
217
218         // Checks the privilege
219         r = _AccessController::CheckUserPrivilege(_PRV_DOWNLOAD);
220         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
221                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
222
223         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
224
225         r = __pImpl->Download(uri, filePath, reqId, replace, timeout, progressInterval);
226         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] Download failed.", GetErrorMessage(r));
227
228         return r;
229 }
230
231 result
232 ContentTransfer::DownloadToBuffer(const Utility::Uri& uri, int fileSize, RequestId& reqId,IContentTransferListener* pListener, int sec)
233 {
234         ClearLastResult();
235         result r = E_SUCCESS;
236
237         r = _AccessController::CheckUserPrivilege(_PRV_DOWNLOAD);
238         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
239                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
240
241         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
242
243         r = __pImpl->DownloadToBuffer(uri, fileSize, reqId, pListener, sec);
244         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] DownloadToBuffer failed.", GetErrorMessage(r));
245
246         return r;
247 }
248
249 result
250 ContentTransfer::DownloadToBuffer(const Utility::Uri& uri, RequestId& reqId, int timeout, int progressInterval)
251 {
252         ClearLastResult();
253         result r = E_SUCCESS;
254
255         r = _AccessController::CheckUserPrivilege(_PRV_DOWNLOAD);
256         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
257                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
258
259         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
260
261         r = __pImpl->DownloadToBuffer(uri, reqId, timeout, progressInterval);
262         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] DownloadToBuffer failed.", GetErrorMessage(r));
263
264         return r;
265 }
266 }}