[content] Fix remove item bug on PlayList
[platform/framework/native/content.git] / src / FCntVideoContentInfo.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                FCntVideoContentInfo.cpp
19  * @brief               This is the implementation file for the %VideoContentInfo class.
20  *
21  * This file contains implementation of the %VideoContentInfo class.
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FCntVideoContentInfo.h>
26 #include <FCntContentManagerUtil.h>
27 #include <FCntVideoMetadata.h>
28 #include <FIoFile.h>
29 #include <FIoDirectory.h>
30 #include <FSysEnvironment.h>
31 #include <FIo_FileImpl.h>
32 #include <FApp_AppInfo.h>
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Io;
36 using namespace Tizen::Locations;
37 using namespace Tizen::App;
38 using namespace Tizen::System;
39 using namespace std;
40
41 namespace Tizen { namespace Content
42 {
43
44 VideoContentInfo::VideoContentInfo(void)
45         : ContentInfo()
46         , __pVideoContentData(null)
47         , __pImpl(null)
48 {
49
50 }
51
52 VideoContentInfo::~VideoContentInfo(void)
53 {
54         if (__pVideoContentData != null)
55         {
56                 if (__pVideoContentData->pArtist != null)
57                 {
58                         delete __pVideoContentData->pArtist;
59                         __pVideoContentData->pArtist = null;
60                 }
61                 if (__pVideoContentData->pGenre != null)
62                 {
63                         delete __pVideoContentData->pGenre;
64                         __pVideoContentData->pGenre = null;
65                 }
66                 if (__pVideoContentData->pTitle != null)
67                 {
68                         delete __pVideoContentData->pTitle;
69                         __pVideoContentData->pTitle = null;
70                 }
71                 if (__pVideoContentData->pAlbumName != null)
72                 {
73                         delete __pVideoContentData->pAlbumName;
74                         __pVideoContentData->pAlbumName = null;
75                 }
76                 delete __pVideoContentData;
77                 __pVideoContentData = null;
78         }
79 }
80
81 /**
82  * E_SUCCESS
83  * E_FILE_NOT_FOUND
84  * E_INVALID_ARG
85  * E_OUT_OF_MEMORY
86  * - E_IO
87  */
88 result
89 VideoContentInfo::Construct(const String& contentPath, const String& thumbnailPath, bool setGps)
90 {
91         SysAssertf(__pVideoContentData == null,
92                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
93
94         ClearLastResult();
95
96         result r = E_SUCCESS;
97         _ContentData contentData;
98         int contentLength = 0;
99         FileAttributes attribute;
100
101         // checks parameters
102         contentLength = contentPath.GetLength();
103         SysTryReturnResult(NID_CNT, _FileImpl::IsMediaPath(contentPath), E_INVALID_ARG,
104                         "The contentPath should start with /Media or /Storagecard/Media(%ls).", contentPath.GetPointer());
105         SysTryReturnResult(NID_CNT, File::IsFileExist(contentPath), E_FILE_NOT_FOUND,
106                         "The file corresponding to contentPath could not be found.");
107
108         if (!thumbnailPath.IsEmpty())
109         {
110                 SysLog(NID_CNT,
111                                 "The thumbnailPath is not supported but you can get the thumbnail managed by Tizen from ContentInfo::GetThumbnailN().");
112         }
113
114         if (setGps)
115         {
116                 SysLog(NID_CNT, "The setGps is not supported.");
117         }
118
119         // Sets the content path
120         contentData.contentPath = contentPath;
121
122         // Sets the content type
123         contentData.contentType = CONTENT_TYPE_VIDEO;
124
125         // E_INVALID_ARG, E_OUT_OF_MEMORY
126         r = SetContentData(&contentData);
127         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
128
129         _VideoContentData* pVideoContentData = new (nothrow) _VideoContentData();
130         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
131
132         __pVideoContentData = pVideoContentData;
133
134         return r;
135 }
136
137 /**
138  * E_SUCCESS
139  * E_FILE_NOT_FOUND
140  * E_INVALID_ARG
141  * E_OUT_OF_MEMORY
142  * - E_IO
143  * - E_SYSTEM
144  */
145 result
146 VideoContentInfo::Construct(const String* pContentPath)
147 {
148         SysAssertf(__pVideoContentData == null,
149                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
150
151         ClearLastResult();
152
153         result r = E_SUCCESS;
154         _ContentData contentData;
155
156         if (pContentPath != null)
157         {
158                 if (!_AppInfo::IsOspCompat())
159                 {
160                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0) || pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
161                         {
162                                 SysLogException(NID_CNT, E_INVALID_ARG,
163                                                 "[E_INVALID_ARG] /Media/ and /Storagecard/Media/ are not supported from Tizen 2.0.");
164                                 return E_INVALID_ARG;
165                         }
166                         if (!(pContentPath->StartsWith(Environment::GetMediaPath(), 0)
167                                 || pContentPath->StartsWith(Environment::GetExternalStoragePath(), 0)))
168                         {
169                                 SysLogException(NID_CNT, E_INVALID_ARG,
170                                                 "[E_INVALID_ARG] %ls is not supported.", pContentPath->GetPointer());
171                                 return E_INVALID_ARG;
172                         }
173                 }
174                 else
175                 {
176                         // prior to 2.0
177                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0))
178                         {
179                                 // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
180                                 // it should be converted in 2.0.
181                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
182                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
183                         }
184                         else if (pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
185                         {
186                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
187                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
188                         }
189                         else
190                         {
191                                 SysLogException(NID_CNT, E_INVALID_ARG,
192                                                 "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
193                                 return E_INVALID_ARG;
194                         }
195                 }
196
197                 int length = pContentPath->GetLength();
198                 SysTryReturnResult(NID_CNT, length != 0, E_INVALID_ARG,
199                                 "The length of pContentPath is 0.");
200                 SysTryReturnResult(NID_CNT, File::IsFileExist(*pContentPath), E_FILE_NOT_FOUND,
201                                   "The file corresponding to pContentPath could not be found.");
202
203                 // Sets the content path
204                 contentData.contentPath = *pContentPath;
205
206                 // Sets the content type
207                 contentData.contentType = CONTENT_TYPE_VIDEO;
208
209                 r = SetContentData(&contentData);
210                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
211         }
212         else
213         {
214                 contentData.contentType = CONTENT_TYPE_VIDEO;
215
216                 r = SetContentData(&contentData);
217                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
218         }
219
220
221         _VideoContentData* pVideoContentData = new (nothrow) _VideoContentData();
222         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
223
224         __pVideoContentData = pVideoContentData;
225
226         return r;
227 }
228
229 result
230 VideoContentInfo::SetVideoContentData(const _VideoContentData* pVideoContentData)
231 {
232         ClearLastResult();
233
234         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_INVALID_ARG, "pVideoContentData is null.");
235
236         if (__pVideoContentData == null)
237         {
238                 __pVideoContentData = new (nothrow) _VideoContentData;
239                 SysTryReturnResult(NID_CNT, __pVideoContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
240         }
241
242         __pVideoContentData->width = pVideoContentData->width;
243         __pVideoContentData->height = pVideoContentData->height;
244         __pVideoContentData->framerate = pVideoContentData->framerate;
245         __pVideoContentData->audioBitrate = pVideoContentData->audioBitrate;
246         __pVideoContentData->videoBitrate = pVideoContentData->videoBitrate;
247         __pVideoContentData->duration = pVideoContentData->duration;
248
249         if (pVideoContentData->pArtist != null)
250         {
251                 __pVideoContentData->pArtist = new (nothrow) String(*(pVideoContentData->pArtist));
252                 SysTryReturnResult(NID_CNT, __pVideoContentData->pArtist != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
253         }
254         if (pVideoContentData->pGenre != null)
255         {
256                 __pVideoContentData->pGenre = new (nothrow) String(*(pVideoContentData->pGenre));
257                 SysTryReturnResult(NID_CNT, __pVideoContentData->pGenre != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
258         }
259         if (pVideoContentData->pTitle != null)
260         {
261                 __pVideoContentData->pTitle = new (nothrow) String(*(pVideoContentData->pTitle));
262                 SysTryReturnResult(NID_CNT, __pVideoContentData->pTitle != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
263         }
264         if (pVideoContentData->pAlbumName != null)
265         {
266                 __pVideoContentData->pAlbumName = new (nothrow) String(*(pVideoContentData->pAlbumName));
267                 SysTryReturnResult(NID_CNT, __pVideoContentData->pAlbumName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
268         }
269
270         return E_SUCCESS;
271 }
272
273 VideoContentInfo::_VideoContentData*
274 VideoContentInfo::GetVideoContentData(void)
275 {
276         if (__pVideoContentData == null)
277         {
278                 __pVideoContentData = new (nothrow) VideoContentInfo::_VideoContentData;
279                 SysTryReturn(NID_CNT, __pVideoContentData != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
280         }
281         return __pVideoContentData;
282 }
283
284 String
285 VideoContentInfo::GetGenre(void) const
286 {
287         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
288
289         if (__pVideoContentData->pGenre == null)
290         {
291                 SysLog(NID_CNT, "GetGenre() failed.");
292                 return String(L"Unknown");
293         }
294
295         return *(__pVideoContentData->pGenre);
296 }
297
298 String
299 VideoContentInfo::GetArtist(void) const
300 {
301         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
302
303         if (__pVideoContentData->pArtist == null)
304         {
305                 SysLog(NID_CNT, "GetArtist() failed.");
306                 return String(L"Unknown");
307         }
308
309         return *(__pVideoContentData->pArtist);
310 }
311
312 int
313 VideoContentInfo::GetBitrate(void) const
314 {
315         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
316
317         if (__pVideoContentData->audioBitrate == 0)
318         {
319                 // The audio bitrate is not managed in video table.
320                 if (this->GetVideoMetadata() != E_SUCCESS)
321                 {
322                         SysLog(NID_CNT, "GetBitrate() failed.");
323                         return 0;
324                 }
325         }
326
327         return __pVideoContentData->audioBitrate;
328 }
329
330 int
331 VideoContentInfo::GetAudioBitrate(void) const
332 {
333         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
334
335         if (__pVideoContentData->audioBitrate == 0)
336         {
337                 // The audio bitrate is not managed in video table.
338                 if (this->GetVideoMetadata() != E_SUCCESS)
339                 {
340                         SysLog(NID_CNT, "GetAudioBitrate() failed.");
341                         return 0;
342                 }
343         }
344
345         return __pVideoContentData->audioBitrate;
346 }
347
348 int
349 VideoContentInfo::GetVideoBitrate(void) const
350 {
351         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
352
353         if (__pVideoContentData->videoBitrate == 0)
354         {
355                 // The video bitrate is not managed in video table.
356                 if (this->GetVideoMetadata() != E_SUCCESS)
357                 {
358                         SysLog(NID_CNT, "GetVideoBitrate() failed.");
359                         return 0;
360                 }
361         }
362
363         return __pVideoContentData->videoBitrate;
364 }
365
366 int
367 VideoContentInfo::GetFramerate(void) const
368 {
369         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
370
371         if (__pVideoContentData->framerate == 0)
372         {
373                 // The framerate is not managed in video table.
374                 if (this->GetVideoMetadata() != E_SUCCESS)
375                 {
376                         SysLog(NID_CNT, "GetFramerate() failed.");
377                         return 0;
378                 }
379         }
380
381         return __pVideoContentData->framerate;
382 }
383
384 int
385 VideoContentInfo::GetWidth(void) const
386 {
387         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
388
389         return __pVideoContentData->width;
390 }
391
392 int
393 VideoContentInfo::GetHeight(void) const
394 {
395         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
396
397         return __pVideoContentData->height;
398 }
399
400 String
401 VideoContentInfo::GetTitle(void) const
402 {
403         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
404
405         if (__pVideoContentData->pTitle == null)
406         {
407                 SysLog(NID_CNT, "GetTitle() failed.");
408                 return String(L"Unknown");
409         }
410
411         return *(__pVideoContentData->pTitle);
412 }
413
414 String
415 VideoContentInfo::GetAlbumName(void) const
416 {
417         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
418
419         if (__pVideoContentData->pAlbumName == null)
420         {
421                 SysLog(NID_CNT, "GetAlbumName() failed.");
422                 return String(L"Unknown");
423         }
424
425         return *(__pVideoContentData->pAlbumName);
426 }
427
428 long
429 VideoContentInfo::GetDuration(void) const
430 {
431         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
432
433         return __pVideoContentData->duration;
434 }
435
436 result
437 VideoContentInfo::GetVideoMetadata(void) const
438 {
439         ClearLastResult();
440
441         result r = E_SUCCESS;
442
443         VideoMetadata* pVideoMetadata = ContentManagerUtil::GetVideoMetaN(this->GetContentPath());
444         r = GetLastResult();
445         SysTryReturn(NID_CNT, pVideoMetadata != null, r, r, "[%s] GetVideoMetadata() failed.", GetErrorMessage(r));
446
447         // framerate
448         __pVideoContentData->framerate = pVideoMetadata->GetFramerate();
449
450         // audioBitrate
451         __pVideoContentData->audioBitrate = pVideoMetadata->GetAudioBitrate();
452
453         // videoBitrate
454         __pVideoContentData->videoBitrate = pVideoMetadata->GetVideoBitrate();
455
456         delete pVideoMetadata;
457
458         return r;
459 }
460 }}