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