[content] Fix compat TC fails
[platform/framework/native/content.git] / src / FCnt_ContentUtility.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                FCnt_ContentUtility.cpp
18  * @brief               This is the implementation file for the %_ContentUtility class.
19  *
20  * This file contains implementation of the %_ContentUtility class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseDateTime.h>
26 #include <FBaseInteger.h>
27 #include <FBaseLongLong.h>
28 #include <FBaseFloat.h>
29 #include <FBaseUtilStringTokenizer.h>
30 #include <FCntContentManagerUtil.h>
31 #include <FCntVideoMetadata.h>
32 #include "FCnt_ContentUtility.h"
33 #include "FCnt_ContentInfoImpl.h"
34 #include "FCnt_ImageContentInfoImpl.h"
35 #include "FCnt_AudioContentInfoImpl.h"
36 #include "FCnt_VideoContentInfoImpl.h"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Locations;
42
43 namespace Tizen { namespace Content
44 {
45
46 static const double DEFAULT_COORDINATE = -200.0;
47
48 // Default constructor
49 _ContentUtility::_ContentUtility(void)
50 {
51
52 }
53
54
55 // Default destructor
56 _ContentUtility::~_ContentUtility(void)
57 {
58
59 }
60
61 void
62 _ContentUtility::DoSafeFree(char* pSrc)
63 {
64         if (pSrc != NULL)
65         {
66                 free(pSrc);
67         }
68 }
69
70 // Fills ContentInfo information in the content data object.
71 result
72 _ContentUtility::FillContentData(media_info_h mediaHandle, _ContentInfoImpl* pInfoImpl)
73 {
74         SysTryReturnResult(NID_CNT, mediaHandle != null && pInfoImpl != null, E_INVALID_ARG, "mediaHandle or pInfoImpl is null.");
75
76         int ret  = MEDIA_CONTENT_ERROR_NONE;
77         result r = E_SUCCESS;
78
79         std::unique_ptr<char, UtilCharDeleter> pMediaId;
80         std::unique_ptr<char, UtilCharDeleter> pContentName;
81         std::unique_ptr<char, UtilCharDeleter> pMediaPath;
82         std::unique_ptr<char, UtilCharDeleter> pThumbnailPath;
83         std::unique_ptr<char, UtilCharDeleter> pAuthor;
84         std::unique_ptr<char, UtilCharDeleter> pCategory;
85         std::unique_ptr<char, UtilCharDeleter> pDescription;
86         std::unique_ptr<char, UtilCharDeleter> pLocationTag;
87         std::unique_ptr<char, UtilCharDeleter> pProvider;
88         std::unique_ptr<char, UtilCharDeleter> pKeyword;
89         std::unique_ptr<char, UtilCharDeleter> pContentRating;
90         std::unique_ptr<char, UtilCharDeleter> pMimeType;
91
92         media_content_type_e mediaType;
93         media_content_storage_e storageType;
94
95         unsigned long long contentSize = 0;
96         time_t addedTime        = 0;
97         time_t modifiedTime     = 0;
98         double latitude         = 0;
99         double longitude        = 0;
100         double altitude         = 0;
101         Coordinates coordinates;
102
103         bool isDrm = 0;
104
105         char* pGetMediaValue = null;
106         ret = media_info_get_media_id(mediaHandle, &pGetMediaValue);
107         if (pGetMediaValue != null)
108         {
109                 pMediaId.reset(pGetMediaValue);
110         }
111         else
112         {
113                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_media_id operation.");
114         }
115
116         ret = media_info_get_content_name(mediaHandle, &pGetMediaValue);
117         if (pGetMediaValue != null)
118         {
119                 pContentName.reset(pGetMediaValue);
120         }
121         else
122         {
123                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_content_name operation.");
124         }
125
126         ret = media_info_get_file_path(mediaHandle, &pGetMediaValue);
127         if (pGetMediaValue != null)
128         {
129                 pMediaPath.reset(pGetMediaValue);
130         }
131         else
132         {
133                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_file_path operation.");
134         }
135
136         ret = media_info_get_thumbnail_path(mediaHandle, &pGetMediaValue);
137         if (pGetMediaValue != null)
138         {
139                 pThumbnailPath.reset(pGetMediaValue);
140         }
141         else
142         {
143                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_thumbnail_path operation.");
144         }
145
146         ret = media_info_get_author(mediaHandle, &pGetMediaValue);
147         if (pGetMediaValue != null)
148         {
149                 pAuthor.reset(pGetMediaValue);
150         }
151         else
152         {
153                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_author operation.");
154         }
155
156         ret = media_info_get_category(mediaHandle, &pGetMediaValue);
157         if (pGetMediaValue != null)
158         {
159                 pCategory.reset(pGetMediaValue);
160         }
161         else
162         {
163                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_category operation.");
164         }
165
166         ret = media_info_get_description(mediaHandle, &pGetMediaValue);
167         if (pGetMediaValue != null)
168         {
169                 pDescription.reset(pGetMediaValue);
170         }
171         else
172         {
173                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_description operation.");
174         }
175
176         ret = media_info_get_location_tag(mediaHandle, &pGetMediaValue);
177         if (pGetMediaValue != null)
178         {
179                 pLocationTag.reset(pGetMediaValue);
180         }
181         else
182         {
183                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_location_tag operation.");
184         }
185
186         ret = media_info_get_provider(mediaHandle, &pGetMediaValue);
187         if (pGetMediaValue != null)
188         {
189                 pProvider.reset(pGetMediaValue);
190         }
191         else
192         {
193                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_provider operation.");
194         }
195
196         ret = media_info_get_size(mediaHandle, &contentSize);
197         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_size operation.");
198
199
200         ret = media_info_get_latitude(mediaHandle, &latitude);
201         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_latitude operation.");
202
203         ret = media_info_get_longitude(mediaHandle, &longitude);
204         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_longitude operation.");
205
206         ret = media_info_get_altitude(mediaHandle, &altitude);
207         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_altitude operation.");
208
209         ret = media_info_get_age_rating(mediaHandle, &pGetMediaValue);
210         if (pGetMediaValue != null)
211         {
212                 pContentRating.reset(pGetMediaValue);
213         }
214         else
215         {
216                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_age_rating operation.");
217         }
218
219         ret = media_info_is_drm(mediaHandle, &isDrm);
220         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_is_drm operation.");
221
222         ret = media_info_get_added_time(mediaHandle, &addedTime);
223         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_added_time operation.");
224
225         ret = media_info_get_modified_time(mediaHandle, &modifiedTime);
226         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_modified_time operation.");
227
228         ret = media_info_get_storage_type(mediaHandle, &storageType);
229         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_storage_type operation.");
230
231         ret = media_info_get_media_type(mediaHandle, &mediaType);
232         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_media_type operation.");
233
234         ret = media_info_get_mime_type(mediaHandle, &pGetMediaValue);
235         if (pGetMediaValue != null)
236         {
237                 pMimeType.reset(pGetMediaValue);
238         }
239         else
240         {
241                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_mime_type operation.");
242         }
243
244         ret = media_info_get_keyword(mediaHandle, &pGetMediaValue);
245         if (pGetMediaValue != null)
246         {
247                 pKeyword.reset(pGetMediaValue);
248         }
249         else
250         {
251                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_keyword operation.");
252         }
253
254         if (pMediaId.get() != null)
255         {
256                 ContentId contentId;
257
258                 r = UuId::Parse(String(pMediaId.get()), contentId);
259                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform UuId::Parse operation.");
260
261                 pInfoImpl->SetContentId(contentId);
262         }
263
264         pInfoImpl->SetDrmProtected(isDrm);
265         SysLog(NID_CNT, "pContentInfoImpl->isDrm = %d", pInfoImpl->IsDrmProtected());
266
267         pInfoImpl->SetContentSize(contentSize);
268         SysLog(NID_CNT, "pContentInfoImpl->contentSize = %lu", pInfoImpl->GetContentSize());
269
270         if (Double::Compare(latitude, DEFAULT_COORDINATE) != 0)
271         {
272                 pInfoImpl->SetLatitude(latitude);
273                 coordinates.SetLatitude(latitude);
274         }
275         if (Double::Compare(longitude, DEFAULT_COORDINATE) != 0)
276         {
277                 pInfoImpl->SetLongitude(longitude);
278                 coordinates.SetLongitude(longitude);
279         }
280         if (Double::Compare(altitude, DEFAULT_COORDINATE) != 0)
281         {
282                 pInfoImpl->SetAltitude(altitude);
283                 coordinates.SetAltitude(altitude);
284         }
285
286         pInfoImpl->SetCoordinates(coordinates);
287
288         if (pMediaPath.get() != NULL)
289         {
290                 String contentPath(pMediaPath.get());
291
292                 pInfoImpl->SetContentPath(contentPath);
293                 SysSecureLog(NID_CNT, "pContentInfoImpl->contentPath = %ls", (pInfoImpl->GetContentPath()).GetPointer());
294         }
295
296         if (pThumbnailPath.get() != NULL)
297         {
298                 String thumbnailPath(pThumbnailPath.get());
299
300                 pInfoImpl->SetThumbnailPath(thumbnailPath);
301                 SysSecureLog(NID_CNT, "pContentInfoImpl->pThumbnailPath = %ls", (pInfoImpl->GetThumbnailPath()).GetPointer());
302         }
303
304         if (pAuthor.get() != NULL)
305         {
306                 String author(pAuthor.get());
307
308                 pInfoImpl->SetAuthor(author);
309                 SysLog(NID_CNT, "pContentInfoImpl->pAuthor = %ls", (pInfoImpl->GetAuthor()).GetPointer());
310         }
311
312         if (pCategory.get() != NULL)
313         {
314                 String category(pCategory.get());
315
316                 pInfoImpl->SetCategory(category);
317                 SysLog(NID_CNT, "pContentInfoImpl->pCategory = %ls", (pInfoImpl->GetCategory()).GetPointer());
318         }
319
320         if (pContentName.get() != NULL)
321         {
322                 String contentName(pContentName.get());
323
324                 pInfoImpl->SetContentName(contentName);
325                 SysLog(NID_CNT, "pContentInfoImpl->pContentName = %ls", (pInfoImpl->GetContentName()).GetPointer());
326         }
327
328         if (pDescription.get() != NULL)
329         {
330                 String description(pDescription.get());
331
332                 pInfoImpl->SetDescription(description);
333                 SysLog(NID_CNT, "pContentInfoImpl->pDescription = %ls", (pInfoImpl->GetDescription()).GetPointer());
334         }
335
336         if (pLocationTag.get() != NULL)
337         {
338                 String locationTag(pLocationTag.get());
339
340                 pInfoImpl->SetLocationTag(locationTag);
341                 SysLog(NID_CNT, "pContentInfoImpl->pLocationTag = %ls", (pInfoImpl->GetLocationTag()).GetPointer());
342         }
343
344         if (pProvider.get() != NULL)
345         {
346                 String provider(pProvider.get());
347
348                 pInfoImpl->SetProvider(provider);
349                 SysLog(NID_CNT, "pContentInfoImpl->pProvider = %ls", (pInfoImpl->GetProvider()).GetPointer());
350         }
351
352         if (pContentRating.get() != NULL)
353         {
354                 String rating(pContentRating.get());
355
356                 pInfoImpl->SetRating(rating);
357                 SysLog(NID_CNT, "pContentInfoImpl->pRating = %ls", (pInfoImpl->GetRating()).GetPointer());
358         }
359
360         if (pKeyword.get() != NULL)
361         {
362                 String keyword(pKeyword.get());
363
364                 pInfoImpl->SetKeyword(keyword);
365                 SysLog(NID_CNT, "pContentInfoImpl->pKeyword = %ls", (pInfoImpl->GetKeyword()).GetPointer());
366         }
367
368         if (pMimeType.get() != NULL)
369         {
370                 String mimeType(pMimeType.get());
371
372                 pInfoImpl->SetMimeType(mimeType);
373                 SysLog(NID_CNT, "pContentInfoImpl->mimeType = %ls", (pInfoImpl->GetMimeType()).GetPointer());
374         }
375
376         switch (storageType)
377         {
378         case MEDIA_CONTENT_STORAGE_INTERNAL:
379                 pInfoImpl->SetStorageType(0);
380                 break;
381         case MEDIA_CONTENT_STORAGE_EXTERNAL:
382                 pInfoImpl->SetStorageType(1);
383                 break;
384         default:
385                 break;
386         }
387         switch (mediaType)
388         {
389         case MEDIA_CONTENT_TYPE_OTHERS:
390                 pInfoImpl->SetContentType(CONTENT_TYPE_OTHER);
391                 break;
392         case MEDIA_CONTENT_TYPE_IMAGE:
393                 pInfoImpl->SetContentType(CONTENT_TYPE_IMAGE);
394                 break;
395         case MEDIA_CONTENT_TYPE_SOUND:
396                 //fall through
397         case MEDIA_CONTENT_TYPE_MUSIC:
398                 pInfoImpl->SetContentType(CONTENT_TYPE_AUDIO);
399                 break;
400         case MEDIA_CONTENT_TYPE_VIDEO:
401                 pInfoImpl->SetContentType(CONTENT_TYPE_VIDEO);
402                 break;
403         default:
404                 break;
405         }
406
407         // addedTime
408         DateTime dt;
409
410         r = dt.SetValue(1970, 1, 1);
411         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform datetime.SetValue operation.");
412
413         r = dt.AddSeconds(addedTime);// need to check addedTime is secs/millisec
414         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform datetime.AddSeconds operation.");
415
416         pInfoImpl->SetDateTime(dt);
417
418         SysLog(NID_CNT, "addedTime = %ld", addedTime);
419         SysLog(NID_CNT, "pContentInfoImpl->dateTime : %ls", (pInfoImpl->GetDateTime()).ToString().GetPointer());
420
421         // modifiedTime
422         r = dt.SetValue(1970, 1, 1);
423         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform datetime.SetValue operation.");
424
425         r = dt.AddSeconds(modifiedTime);// need to check addedTime is secs/millisec
426         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform datetime.AddSeconds operation.");
427
428         pInfoImpl->SetModifiedTime(dt);
429
430         SysLog(NID_CNT, "modifiedTime = %ld", modifiedTime);
431         SysLog(NID_CNT, "pContentInfoImpl->modifiedTime : %ls", (pInfoImpl->GetModifiedTime()).ToString().GetPointer());
432
433         return r;
434 }
435
436 // Fills ImageContentInfo information in the image content data object .
437 result
438 _ContentUtility::FillImageContentData(media_info_h mediaHandle, _ImageContentInfoImpl* pImageContentInfoImpl)
439 {
440         SysTryReturnResult(NID_CNT, mediaHandle != null && pImageContentInfoImpl != null, E_INVALID_ARG, "mediaHandle or pImageContentInfoImpl is null.");
441
442         int ret  = MEDIA_CONTENT_ERROR_NONE;
443         result r = E_SUCCESS;
444         int width  = 0;
445         int height = 0;
446         bool isBurst = false;
447         media_content_orientation_e orientation;
448         std::unique_ptr<char, UtilCharDeleter> pStrValue;
449
450         image_meta_h tempMeta = NULL;
451
452         ret = media_info_get_image(mediaHandle, &tempMeta);
453         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_image operation.");
454
455         std::unique_ptr<image_meta_s, ImageMetaHandleDeleter> pImageHandle(tempMeta);
456         SysTryReturnResult(NID_CNT, pImageHandle != null, E_OUT_OF_MEMORY, "pImageHandle is null.");
457
458         ret = image_meta_get_width(pImageHandle.get(), &width);
459         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform image_meta_get_width operation.");
460
461         ret = image_meta_get_orientation(pImageHandle.get(), &orientation);
462         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform image_meta_get_orientation operation.");
463
464         ret = image_meta_get_height(pImageHandle.get(), &height);
465         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform image_meta_get_height operation.");
466
467         ret = image_meta_is_burst_shot(pImageHandle.get(), &isBurst);
468         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform image_meta_is_burst_shot operation.");
469
470         pImageContentInfoImpl->SetWidth(width);
471         pImageContentInfoImpl->SetHeight(height);
472         pImageContentInfoImpl->SetOrientation(static_cast< ImageOrientationType >(orientation));
473         pImageContentInfoImpl->SetBurstShot(isBurst);
474
475         // title
476         char* pTempValue = null;
477         ret = media_info_get_display_name(mediaHandle, &pTempValue);
478         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_display_name operation.");
479
480         if (pTempValue != NULL)
481         {
482                 pStrValue.reset(pTempValue);
483
484                 int pos = 0;
485                 String fileName = L"";
486                 String strTitle(pStrValue.get());
487
488                 r = strTitle.LastIndexOf(L'.', strTitle.GetLength() - 1, pos);
489                 if (r == E_SUCCESS)
490                 {
491                         r = strTitle.SubString(0, pos, fileName);
492                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform string.SubString operation.");
493                 }
494                 else
495                 {
496                         // Without extension
497                         r = E_SUCCESS;
498                         fileName = strTitle;
499                 }
500                 pImageContentInfoImpl->SetTitle(fileName);
501                 SysLog(NID_CNT, "pImageContentInfoImpl->title = %ls", (pImageContentInfoImpl->GetTitle()).GetPointer());
502         }
503
504         // dateTaken
505         ret = image_meta_get_date_taken(pImageHandle.get(), &pTempValue);
506         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret),
507                         "Failed to perform image_meta_get_date_taken operation.");
508
509         DateTime dt;
510
511         if (pTempValue != NULL)
512         {
513                 pStrValue.reset(pTempValue);
514                 String dateTaken(pStrValue.get());
515                 String newDateTaken(L"");
516
517                 // detour the unexpected datetaken format
518                 String tempDelim(L"+-Z");
519                 String token;
520
521                 StringTokenizer tempStrTok(dateTaken, tempDelim);
522
523                 r = tempStrTok.GetNextToken(token);
524                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
525
526                 dateTaken = token;
527
528                 String delim(L": ");
529                 String year(L"");
530
531                 StringTokenizer strTok(dateTaken, delim);
532
533                 r = strTok.SetDelimiters(delim);
534                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform SetDelimiters operation.");
535
536                 r = strTok.GetNextToken(token);
537                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
538
539                 year = token;
540
541                 // Append month
542                 r = strTok.GetNextToken(token);
543                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
544
545                 r = newDateTaken.Append(token);
546                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
547
548                 r = newDateTaken.Append(L"/");
549                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
550
551                 // Append day
552                 r = strTok.GetNextToken(token);
553                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
554
555                 r = newDateTaken.Append(token);
556                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
557
558                 r = newDateTaken.Append(L"/");
559                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
560
561                 // Append year
562                 r = newDateTaken.Append(year);
563                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
564
565                 r = newDateTaken.Append(L" ");
566                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
567
568                 String newDelim(L" ");
569
570                 r = strTok.SetDelimiters(newDelim);
571                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform SetDelimiters operation.");
572
573                 r = strTok.GetNextToken(token);
574                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
575
576                 r = newDateTaken.Append(token);
577                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
578
579                 r = DateTime::Parse(newDateTaken, dt);
580                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform Parse operation for DateTime.");
581         }
582         else
583         {
584                 dt = DateTime::GetMinValue();
585         }
586
587         pImageContentInfoImpl->SetImageTakenDate(dt);
588         SysLog(NID_CNT, "pImageContentInfoImpl->takenDate = %ls", dt.ToString().GetPointer());
589
590         // burstShotId
591         ret = image_meta_get_burst_id(pImageHandle.get(), &pTempValue);
592         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret),
593                         "Failed to perform image_meta_get_burst_id operation.");
594
595         if (pTempValue != NULL)
596         {
597                 pStrValue.reset(pTempValue);
598                 String burstShotId(pStrValue.get());
599
600                 pImageContentInfoImpl->SetBurstShotId(burstShotId);
601                 SysLog(NID_CNT, "pImageContentInfoImpl->burstShotId = %ls", burstShotId.GetPointer());
602         }
603
604         return r;
605 }
606
607 // Fills AudioContentInfo information in the audio content data object.
608 result
609 _ContentUtility::FillAudioContentData(media_info_h mediaHandle, _AudioContentInfoImpl* pAudioContentInfoImpl)
610 {
611         SysTryReturnResult(NID_CNT, mediaHandle != null && pAudioContentInfoImpl != null, E_INVALID_ARG, "mediaHandle or pAudioContentInfoImpl is null.");
612
613         int ret  = MEDIA_CONTENT_ERROR_NONE;
614         result r = E_SUCCESS;
615         int bitrate = 0;
616         int duration = 0;
617
618         std::unique_ptr<char, UtilCharDeleter> pTitle;
619         std::unique_ptr<char, UtilCharDeleter> pAlbumName;
620         std::unique_ptr<char, UtilCharDeleter> pArtistName;
621         std::unique_ptr<char, UtilCharDeleter> pGenreName;
622         std::unique_ptr<char, UtilCharDeleter> pComposerName;
623         std::unique_ptr<char, UtilCharDeleter> pYear;
624         std::unique_ptr<char, UtilCharDeleter> pCopyRight;
625         std::unique_ptr<char, UtilCharDeleter> pTrackNum;
626
627         audio_meta_h tempMeta = NULL;
628
629         ret = media_info_get_audio(mediaHandle, &tempMeta);
630         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_audio operation.");
631
632         std::unique_ptr<audio_meta_s, AudioMetaHandleDeleter> pAudioHandle(tempMeta);
633         SysTryReturnResult(NID_CNT, pAudioHandle != null, E_OUT_OF_MEMORY, "pAudioHandle is null.");
634
635         ret = audio_meta_get_bit_rate(pAudioHandle.get(), &bitrate);
636         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_bit_rate operation.");
637
638         char* pAudioMetaValue = null;
639         ret = audio_meta_get_title(pAudioHandle.get(), &pAudioMetaValue);
640         if (pAudioMetaValue != null)
641         {
642                 pTitle.reset(pAudioMetaValue);
643         }
644         else
645         {
646                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_title operation.");
647         }
648
649         ret = audio_meta_get_album(pAudioHandle.get(), &pAudioMetaValue);
650         if (pAudioMetaValue != null)
651         {
652                 pAlbumName.reset(pAudioMetaValue);
653         }
654         else
655         {
656                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_album operation.");
657         }
658
659         ret = audio_meta_get_artist(pAudioHandle.get(), &pAudioMetaValue);
660         if (pAudioMetaValue != null)
661         {
662                 pArtistName.reset(pAudioMetaValue);
663         }
664         else
665         {
666                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_artist operation.");
667         }
668
669         ret = audio_meta_get_genre(pAudioHandle.get(), &pAudioMetaValue);
670         if (pAudioMetaValue != null)
671         {
672                 pGenreName.reset(pAudioMetaValue);
673         }
674         else
675         {
676                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_genre operation.");
677         }
678
679         ret = audio_meta_get_composer(pAudioHandle.get(), &pAudioMetaValue);
680         if (pAudioMetaValue != null)
681         {
682                 pComposerName.reset(pAudioMetaValue);
683         }
684         else
685         {
686                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_composer operation.");
687         }
688
689         ret = audio_meta_get_year(pAudioHandle.get(), &pAudioMetaValue);
690         if (pAudioMetaValue != null)
691         {
692                 pYear.reset(pAudioMetaValue);
693         }
694         else
695         {
696                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_year operation.");
697         }
698
699         ret = audio_meta_get_copyright(pAudioHandle.get(), &pAudioMetaValue);
700         if (pAudioMetaValue != null)
701         {
702                 pCopyRight.reset(pAudioMetaValue);
703         }
704         else
705         {
706                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_copyright operation.");
707         }
708
709         ret = audio_meta_get_track_num(pAudioHandle.get(), &pAudioMetaValue);
710         if (pAudioMetaValue != null)
711         {
712                 pTrackNum.reset(pAudioMetaValue);
713         }
714         else
715         {
716                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_track_num operation.");
717         }
718
719         ret = audio_meta_get_duration(pAudioHandle.get(), &duration);
720         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_duration operation.");
721
722         pAudioContentInfoImpl->SetBitrate(bitrate);
723
724         if (pYear.get() != null)
725         {
726                 SysLog(NID_CNT, "pAudioContentInfoImpl->pYear  = %s", pYear.get());
727
728                 String strYear(pYear.get());
729                 if (strYear.CompareTo(L"Unknown") != 0)
730                 {
731                         int releaseYear = 0;
732
733                         r = Integer::Parse(strYear, releaseYear);
734                         if (IsFailed(r))
735                         {
736                                 // It is one of the metadata. If error occurs, skip it for other metadata.
737                                 pAudioContentInfoImpl->SetReleaseYear(0);
738                                 r = E_SUCCESS;
739                                 SysLog(NID_CNT, "pAudioContentInfoImpl->pYear(invalid data) = %ls", strYear.GetPointer());
740                         }
741
742                         pAudioContentInfoImpl->SetReleaseYear(releaseYear);
743                 }
744         }
745
746         if (pTitle.get() != NULL)
747         {
748                 pAudioContentInfoImpl->SetTitle(String(pTitle.get()));
749                 SysLog(NID_CNT, "pAudioContentInfoImpl->pTitle  = %ls", (pAudioContentInfoImpl->GetTitle()).GetPointer());
750         }
751
752         if (pArtistName.get() != NULL)
753         {
754                 pAudioContentInfoImpl->SetArtist(String(pArtistName.get()));
755                 SysLog(NID_CNT, "pAudioContentInfoImpl->pArtist = %ls", (pAudioContentInfoImpl->GetArtist()).GetPointer());
756         }
757
758         if (pGenreName.get() != NULL)
759         {
760                 pAudioContentInfoImpl->SetGenre(String(pGenreName.get()));
761                 SysLog(NID_CNT, "pAudioContentInfoImpl->pGenre = %ls", (pAudioContentInfoImpl->GetGenre()).GetPointer());
762         }
763
764         if (pComposerName.get() != NULL)
765         {
766                 pAudioContentInfoImpl->SetComposer(String(pComposerName.get()));
767                 SysLog(NID_CNT, "pAudioContentInfoImpl->pComposer = %ls", (pAudioContentInfoImpl->GetComposer()).GetPointer());
768         }
769
770         if (pAlbumName.get() != NULL)
771         {
772                 pAudioContentInfoImpl->SetAlbumName(String(pAlbumName.get()));
773                 SysLog(NID_CNT, "pAudioContentInfoImpl->pAlbumName  = %ls", (pAudioContentInfoImpl->GetAlbumName()).GetPointer());
774         }
775
776         if (pCopyRight.get() != NULL)
777         {
778                 pAudioContentInfoImpl->SetCopyright(String(pCopyRight.get()));
779                 SysLog(NID_CNT, "pAudioContentInfoImpl->pCopyright  = %ls", (pAudioContentInfoImpl->GetCopyright()).GetPointer());
780         }
781
782         if (pTrackNum.get() != NULL)
783         {
784                 pAudioContentInfoImpl->SetTrackInfo(String(pTrackNum.get()));
785                 SysLog(NID_CNT, "pAudioContentInfoImpl->pTrackInfo  = %ls", (pAudioContentInfoImpl->GetTrackInfo()).GetPointer());
786         }
787
788         pAudioContentInfoImpl->SetDuration(duration);
789         SysLog(NID_CNT,"pAudioContentInfoImpl->duration = %d", pAudioContentInfoImpl->GetDuration());
790
791         return r;
792 }
793
794 // Fills VideoContentInfo information in the video content data object.
795 result
796 _ContentUtility::FillVideoContentData(media_info_h mediaHandle, _VideoContentInfoImpl* pVideoContentInfoImpl)
797 {
798         SysTryReturnResult(NID_CNT, mediaHandle != null && pVideoContentInfoImpl != null, E_INVALID_ARG, "mediaHandle or pVideoContentInfoImpl is null.");
799
800         int ret  = MEDIA_CONTENT_ERROR_NONE;
801         result r = E_SUCCESS;
802         int width  = 0;
803         int height = 0;
804         std::unique_ptr<char, UtilCharDeleter> pArtistName;
805         std::unique_ptr<char, UtilCharDeleter> pGenreName;
806         std::unique_ptr<char, UtilCharDeleter> pTitle;
807         std::unique_ptr<char, UtilCharDeleter> pAlbumName;
808         int duration = 0;
809
810         video_meta_h tempMeta = NULL;
811
812         ret = media_info_get_video(mediaHandle, &tempMeta);
813         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_video operation.");
814
815         std::unique_ptr<video_meta_s, VideoMetaHandleDeleter> pVideoHandle(tempMeta);
816         SysTryReturnResult(NID_CNT, pVideoHandle != null, E_OUT_OF_MEMORY, "pVideoHandle is null.");
817
818         ret = video_meta_get_width(pVideoHandle.get(), &width);
819         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_width operation.");
820
821         ret = video_meta_get_height(pVideoHandle.get(), &height);
822         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_height operation.");
823
824         char* pVideoMetaValue = null;
825         ret = video_meta_get_artist(pVideoHandle.get(), &pVideoMetaValue);
826         if (pVideoMetaValue != null)
827         {
828                 pArtistName.reset(pVideoMetaValue);
829         }
830         else
831         {
832                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_artist operation.");
833         }
834
835         ret = video_meta_get_genre(pVideoHandle.get(), &pVideoMetaValue);
836         if (pVideoMetaValue != null)
837         {
838                 pGenreName.reset(pVideoMetaValue);
839         }
840         else
841         {
842                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_genre operation.");
843         }
844
845         ret = video_meta_get_title(pVideoHandle.get(), &pVideoMetaValue);
846         if (pVideoMetaValue != null)
847         {
848                 pTitle.reset(pVideoMetaValue);
849         }
850         else
851         {
852                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_title operation.");
853         }
854
855         ret = video_meta_get_album(pVideoHandle.get(), &pVideoMetaValue);
856         if (pVideoMetaValue != null)
857         {
858                 pAlbumName.reset(pVideoMetaValue);
859         }
860         else
861         {
862                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_album operation.");
863         }
864
865         ret = video_meta_get_duration(pVideoHandle.get(), &duration);
866         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_duration operation.");
867
868         pVideoContentInfoImpl->SetWidth(width);
869         pVideoContentInfoImpl->SetHeight(height);
870
871         if (pTitle.get() != NULL)
872         {
873                 pVideoContentInfoImpl->SetTitle(String(pTitle.get()));
874                 SysLog(NID_CNT, "pVideoContentInfoImpl->pTitle = %ls", (pVideoContentInfoImpl->GetTitle()).GetPointer());
875         }
876
877         if (pArtistName.get() != NULL)
878         {
879                 pVideoContentInfoImpl->SetArtist(String(pArtistName.get()));
880                 SysLog(NID_CNT, "pVideoContentInfoImpl->pArtist = %ls", (pVideoContentInfoImpl->GetArtist()).GetPointer());
881         }
882
883         if (pGenreName.get() != NULL)
884         {
885                 pVideoContentInfoImpl->SetGenre(String(pGenreName.get()));
886                 SysLog(NID_CNT, "pVideoContentInfoImpl->pGenre = %ls", (pVideoContentInfoImpl->GetGenre()).GetPointer());
887         }
888
889         if (pAlbumName.get() != NULL)
890         {
891                 pVideoContentInfoImpl->SetAlbumName(String(pAlbumName.get()));
892                 SysLog(NID_CNT, "pVideoContentInfoImpl->pAlbumName = %ls", (pVideoContentInfoImpl->GetAlbumName()).GetPointer());
893         }
894
895         pVideoContentInfoImpl->SetDuration(duration);
896         SysLog(NID_CNT,"pVideoContentInfoImpl->duration = %d", pVideoContentInfoImpl->GetDuration());
897
898         return r;
899 }
900
901 // Conversion of system errors to native errors
902 result
903 _ContentUtility::MapCoreErrorToNativeResult(int reason)
904 {
905         result r = E_SUCCESS;
906         switch (reason)
907         {
908         case MEDIA_CONTENT_ERROR_NONE:
909                 r = E_SUCCESS;
910                 break;
911         case MEDIA_CONTENT_ERROR_DB_BUSY:
912                 r = E_SERVICE_BUSY;
913                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
914                 break;
915         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
916                 r = E_INVALID_ARG;
917                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
918                 break;
919         case MEDIA_CONTENT_ERROR_DB_FAILED:
920                 r = E_DATABASE;
921                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
922                 break;
923         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
924                 r = E_OUT_OF_MEMORY;
925                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
926                 break;
927         default:
928                 SysLog(NID_CNT, "default");
929                 r = E_SYSTEM;
930                 break;
931         }
932         return r;
933 }
934
935 }}