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