Fix : Search APIs don't return exception despite the content in DB doesn't match...
[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 <new>
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 <FSec_AccessController.h>
32 #include "FCnt_ContentManagerImpl.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         : __pContentManagerImpl(null)
42 {
43
44 }
45
46 ContentManager::~ContentManager(void)
47 {
48         delete __pContentManagerImpl;
49 }
50
51 result
52 ContentManager::Construct(void)
53 {
54         result r = E_SUCCESS;
55
56         SysAssertf(__pContentManagerImpl == null,
57                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
58
59         _ContentManagerImpl* pContentManagerImpl = new (std::nothrow) _ContentManagerImpl();
60         SysTryReturn(NID_CNT, pContentManagerImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Construct failed.");
61
62         r = pContentManagerImpl->Construct();
63         SysTryCatch(NID_CNT, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
64
65         __pContentManagerImpl = pContentManagerImpl;
66
67         return r;
68
69 CATCH:
70         delete pContentManagerImpl;
71
72         return r;
73 }
74
75 ContentId
76 ContentManager::CreateContent(const ContentInfo& contentInfo)
77 {
78         ClearLastResult();
79         result r = E_SUCCESS;
80
81         // Checks the privilege
82         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
83         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
84                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
85
86         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
87
88         ContentId contentId = __pContentManagerImpl->CreateContent(contentInfo);
89         r = GetLastResult();
90         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
91
92         return contentId;
93 }
94
95 ContentId
96 ContentManager::CreateContent(const ByteBuffer& byteBuffer, const String& destinationPath, const ContentInfo* pContentInfo)
97 {
98         ClearLastResult();
99         result r = E_SUCCESS;
100
101         // Checks the privilege
102         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
103         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
104                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
105
106         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
107
108         ContentId contentId = __pContentManagerImpl->CreateContent(byteBuffer, destinationPath, pContentInfo);
109         r = GetLastResult();
110         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
111
112         return contentId;
113 }
114
115 ContentId
116 ContentManager::CreateContent(const String& sourcePath, const String& destinationPath, bool deleteSource,
117                                                           const ContentInfo* pContentInfo)
118 {
119         ClearLastResult();
120         result r = E_SUCCESS;
121
122         // Checks the privilege
123         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
124         SysTryReturn(NID_CNT, r == E_SUCCESS, UuId::GetInvalidUuId(), E_PRIVILEGE_DENIED,
125                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
126
127         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
128
129         ContentId contentId = __pContentManagerImpl->CreateContent(sourcePath, destinationPath, deleteSource, pContentInfo);
130         r = GetLastResult();
131         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), UuId::GetInvalidUuId(), r, "[%s] CreateContent failed.", GetErrorMessage(r));
132
133         return contentId;
134 }
135
136 ContentInfo*
137 ContentManager::GetContentInfoN(const ContentId& contentId) const
138 {
139         ClearLastResult();
140         result r = E_SUCCESS;
141         ContentInfo* pContentInfo = null;
142
143         // Checks the privilege
144         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_READ);
145         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_PRIVILEGE_DENIED,
146                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
147
148         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
149
150         pContentInfo = __pContentManagerImpl->GetContentInfoN(contentId);
151         r = GetLastResult();
152         SysTryReturn(NID_CNT, pContentInfo != null, null, r, "[%s] GetContentInfoN failed.", GetErrorMessage(r));
153
154         return pContentInfo;
155 }
156
157 result
158 ContentManager::UpdateContent(const ContentInfo& contentInfo)
159 {
160         ClearLastResult();
161         result r = E_SUCCESS;
162
163         // Checks the privilege
164         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
165         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
166                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
167
168         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
169
170         r = __pContentManagerImpl->UpdateContent(contentInfo);
171         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] UpdateContent failed.", GetErrorMessage(r));
172
173         return r;
174 }
175
176 result
177 ContentManager::DeleteContent(const ContentId& contentId)
178 {
179         ClearLastResult();
180         result r = E_SUCCESS;
181
182         // Checks the privilege
183         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
184         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
185                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
186
187         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
188
189         r = __pContentManagerImpl->DeleteContent(contentId);
190         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] DeleteContent failed.", GetErrorMessage(r));
191
192         return r;
193 }
194
195 result
196 ContentManager::AddContentUpdateEventListener(IContentUpdateEventListener& listener)
197 {
198         ClearLastResult();
199         result r = E_SUCCESS;
200
201         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
202
203         r = __pContentManagerImpl->AddContentUpdateEventListener(listener);
204         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "[%s] AddContentUpdateEventListener failed.", GetErrorMessage(r));
205
206         return r;
207 }
208
209 result
210 ContentManager::RemoveContentUpdateEventListener(IContentUpdateEventListener& listener)
211 {
212         ClearLastResult();
213         result r = E_SUCCESS;
214
215         SysAssertf(__pContentManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
216
217         r = __pContentManagerImpl->RemoveContentUpdateEventListener(listener);
218         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "[%s] RemoveContentUpdateEventListener failed.", GetErrorMessage(r));
219
220         return r;
221 }
222
223 result
224 ContentManager::ScanFile(const Tizen::Base::String& contentPath)
225 {
226         ClearLastResult();
227         result r = E_SUCCESS;
228
229         // Checks the privilege
230         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
231         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
232                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
233
234         r = _ContentManagerImpl::ScanFile(contentPath);
235         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] ScanFile failed.", GetErrorMessage(r));
236
237         return r;
238 }
239
240 result
241 ContentManager::ScanDirectory(const Tizen::Base::String& directoryPath, bool recursive, IContentScanListener* pListener, RequestId& reqId)
242 {
243         ClearLastResult();
244         result r = E_SUCCESS;
245
246         // Checks the privilege
247         r = _AccessController::CheckUserPrivilege(_PRV_CONTENT_WRITE);
248         SysTryReturn(NID_CNT, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
249                         "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
250
251         r = _ContentManagerImpl::ScanDirectory(directoryPath, recursive, pListener, reqId);
252         SysTryReturn(NID_CNT, !IsFailed(r), r, r, "[%s] ScanDirectory failed.", GetErrorMessage(r));
253
254         return r;
255 }
256
257 }}