Fix an issue not to call download_destroy()
[platform/framework/native/content.git] / src / FCntContentManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file                FCntContentManager.cpp
18  * @brief               This is the implementation file for the %ContentManager class.
19  *
20  * This file contains implementation of the %ContentManager class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseByteBuffer.h>
25 #include <FCntContentManager.h>
26 #include <FCntImageContentInfo.h>
27 #include <FCntAudioContentInfo.h>
28 #include <FCntVideoContentInfo.h>
29 #include <FCntOtherContentInfo.h>
30 #include <FCnt_ContentManagerImpl.h>
31 #include <FSec_AccessController.h>
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Security;
35
36 namespace Tizen { namespace Content
37 {
38
39 ContentManager::ContentManager(void)
40         : Object()
41         , __pImpl(null)
42 {
43
44 }
45
46 ContentManager::~ContentManager(void)
47 {
48         if (__pImpl != null)
49         {
50                 delete __pImpl;
51                 __pImpl = null;
52         }
53 }
54
55 result
56 ContentManager::Construct(void)
57 {
58         ClearLastResult();
59         result r = E_SUCCESS;
60
61         SysAssertf(__pImpl == null,
62                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63
64         _ContentManagerImpl* pContentManagerImpl = new (std::nothrow) _ContentManagerImpl();
65         SysTryReturn(NID_CNT, pContentManagerImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Construct failed.");
66
67         r = pContentManagerImpl->Construct();
68         SysTryCatch(NID_CNT, r == E_SUCCESS, , r, "[%S] Propagating.", GetErrorMessage(r));
69
70         __pImpl = pContentManagerImpl;
71
72         return r;
73
74 CATCH:
75         delete pContentManagerImpl;
76         pContentManagerImpl = null;
77
78         return r;
79 }
80
81 ContentId
82 ContentManager::CreateContent(const ContentInfo& contentInfo)
83 {
84         ClearLastResult();
85         result r = E_SUCCESS;
86
87         // Checks the privilege
88         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
89         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
90                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
91
92         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
93
94         ContentId contentId = __pImpl->CreateContent(contentInfo);
95         r = GetLastResult();
96         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
97
98         return contentId;
99 }
100
101 ContentId
102 ContentManager::CreateContent(const ByteBuffer& byteBuffer, const String& destinationPath, const ContentInfo* pContentInfo)
103 {
104         ClearLastResult();
105         result r = E_SUCCESS;
106
107         // Checks the privilege
108         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
109         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
110                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
111
112         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
113
114         ContentId contentId = __pImpl->CreateContent(byteBuffer, destinationPath, pContentInfo);
115         r = GetLastResult();
116         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
117
118         return contentId;
119 }
120
121 ContentId
122 ContentManager::CreateContent(const String& sourcePath, const String& destinationPath, bool deleteSource,
123                                                           const ContentInfo* pContentInfo)
124 {
125         ClearLastResult();
126         result r = E_SUCCESS;
127
128         // Checks the privilege
129         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
130         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
131                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
132
133         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
134
135         ContentId contentId = __pImpl->CreateContent(sourcePath, destinationPath, deleteSource, pContentInfo);
136         r = GetLastResult();
137         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
138
139         return contentId;
140 }
141
142 ContentInfo*
143 ContentManager::GetContentInfoN(const ContentId& contentId) const
144 {
145         ClearLastResult();
146         result r = E_SUCCESS;
147
148         ContentInfo* pContentInfo = null;
149         ImageContentInfo* pImageContentInfo = null;
150         AudioContentInfo* pAudioContentInfo = null;
151         VideoContentInfo* pVideoContentInfo = null;
152         OtherContentInfo* pOtherContentInfo = null;
153         ContentType contentType;
154
155         // Checks the privilege
156         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_READ);
157         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_PRIVILEGE_DENIED,
158                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
159
160         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
161
162         pContentInfo = __pImpl->GetContentInfoN(contentId);
163         r = GetLastResult();
164         SysTryCatch(NID_CNT, pContentInfo != null, , r, "[%s] GetContentInfoN failed.", GetErrorMessage(r));
165
166         contentType = pContentInfo->GetContentType();
167
168         if (contentType == CONTENT_TYPE_IMAGE)
169         {
170                 pImageContentInfo = dynamic_cast <ImageContentInfo*>(pContentInfo);
171                 SysTryCatch(NID_CNT, pImageContentInfo != null, , E_SYSTEM, "[E_SYSTEM] GetContentInfoN failed.");
172
173                 return pImageContentInfo;
174         }
175         else if (contentType == CONTENT_TYPE_AUDIO)
176         {
177                 pAudioContentInfo = dynamic_cast <AudioContentInfo*>(pContentInfo);
178                 SysTryCatch(NID_CNT, pAudioContentInfo != null, , E_SYSTEM, "[E_SYSTEM] GetContentInfoN failed.");
179
180                 return pAudioContentInfo;
181         }
182         else if (contentType == CONTENT_TYPE_VIDEO)
183         {
184                 pVideoContentInfo = dynamic_cast <VideoContentInfo*>(pContentInfo);
185                 SysTryCatch(NID_CNT, pVideoContentInfo != null, , E_SYSTEM, "[E_SYSTEM] GetContentInfoN failed.");
186
187                 return pVideoContentInfo;
188         }
189         else if (contentType == CONTENT_TYPE_OTHER)
190         {
191                 pOtherContentInfo = dynamic_cast <OtherContentInfo*>(pContentInfo);
192                 SysTryCatch(NID_CNT, pOtherContentInfo != null, , E_SYSTEM, "[E_SYSTEM] GetContentInfoN failed.");
193
194                 return pOtherContentInfo;
195         }
196         else
197         {
198                 SysLogException(NID_CNT, E_SYSTEM, "[E_SYSTEM] contentType is CONTENT_TYPE_UNKNOWN.");
199                 goto CATCH;
200         }
201
202 CATCH:
203         if (pContentInfo != null)
204         {
205                 delete pContentInfo;
206                 pContentInfo = null;
207         }
208
209         return null;
210 }
211
212 result
213 ContentManager::UpdateContent(const ContentInfo& contentInfo)
214 {
215         ClearLastResult();
216         result r = E_SUCCESS;
217
218         // Checks the privilege
219         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
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->UpdateContent(contentInfo);
226         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] UpdateContent failed.", GetErrorMessage(r));
227
228         return r;
229 }
230
231 result
232 ContentManager::DeleteContent(const ContentId& contentId)
233 {
234         ClearLastResult();
235         result r = E_SUCCESS;
236
237         // Checks the privilege
238         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
239         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
240                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
241
242         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
243
244         r = __pImpl->DeleteContent(contentId);
245         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] DeleteContent failed.", GetErrorMessage(r));
246
247         return r;
248 }
249
250 result
251 ContentManager::AddContentUpdateEventListener(IContentUpdateEventListener& listener)
252 {
253         ClearLastResult();
254         result r = E_SUCCESS;
255
256         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
257
258         r = __pImpl->AddContentUpdateEventListener(listener);
259         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "[%s] AddContentUpdateEventListener failed.", GetErrorMessage(r));
260
261         return r;
262 }
263
264 result
265 ContentManager::RemoveContentUpdateEventListener(IContentUpdateEventListener& listener)
266 {
267         ClearLastResult();
268         result r = E_SUCCESS;
269
270         SysAssertf(__pImpl != null, "Not yet constructed. Construct() should be called before use.");
271
272         r = __pImpl->RemoveContentUpdateEventListener(listener);
273         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "[%s] RemoveContentUpdateEventListener failed.", GetErrorMessage(r));
274
275         return r;
276 }
277
278 result
279 ContentManager::ScanFile(const Tizen::Base::String& contentPath)
280 {
281         ClearLastResult();
282         result r = E_SUCCESS;
283
284         // Checks the privilege
285         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
286         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
287                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
288
289         r = _ContentManagerImpl::ScanFile(contentPath);
290         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] ScanFile failed.", GetErrorMessage(r));
291
292         return r;
293 }
294
295 result
296 ContentManager::ScanDirectory(const Tizen::Base::String& directoryPath, bool recursive, IContentScanListener* pListener, RequestId& reqId)
297 {
298         ClearLastResult();
299         result r = E_SUCCESS;
300
301         // Checks the privilege
302         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
303         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
304                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
305
306         r = _ContentManagerImpl::ScanDirectory(directoryPath, recursive, pListener, reqId);
307         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] ScanDirectory failed.", GetErrorMessage(r));
308
309         return r;
310 }
311
312 }}