Add Extension Properties
[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                 String delim(L": ");
518                 String token;
519
520                 String year(L"");
521
522                 StringTokenizer strTok(dateTaken, delim);
523
524                 r = strTok.GetNextToken(token);
525                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
526
527                 year = token;
528
529                 // Append month
530                 r = strTok.GetNextToken(token);
531                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
532
533                 r = newDateTaken.Append(token);
534                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
535
536                 r = newDateTaken.Append(L"/");
537                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
538
539                 // Append day
540                 r = strTok.GetNextToken(token);
541                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
542
543                 r = newDateTaken.Append(token);
544                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
545
546                 r = newDateTaken.Append(L"/");
547                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
548
549                 // Append year
550                 r = newDateTaken.Append(year);
551                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
552
553                 r = newDateTaken.Append(L" ");
554                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
555
556                 String newDelim(L" ");
557
558                 r = strTok.SetDelimiters(newDelim);
559                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform SetDelimiters operation.");
560
561                 r = strTok.GetNextToken(token);
562                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
563
564                 r = newDateTaken.Append(token);
565                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
566
567                 r = DateTime::Parse(newDateTaken, dt);
568                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform Parse operation for DateTime.");
569         }
570         else
571         {
572                 dt = DateTime::GetMinValue();
573         }
574
575         pImageContentInfoImpl->SetImageTakenDate(dt);
576         SysLog(NID_CNT, "pImageContentInfoImpl->takenDate = %ls", dt.ToString().GetPointer());
577
578         // burstShotId
579         ret = image_meta_get_burst_id(pImageHandle.get(), &pTempValue);
580         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret),
581                         "Failed to perform image_meta_get_burst_id operation.");
582
583         if (pTempValue != NULL)
584         {
585                 pStrValue.reset(pTempValue);
586                 String burstShotId(pStrValue.get());
587
588                 pImageContentInfoImpl->SetBurstShotId(burstShotId);
589                 SysLog(NID_CNT, "pImageContentInfoImpl->burstShotId = %ls", burstShotId.GetPointer());
590         }
591
592         return r;
593 }
594
595 // Fills AudioContentInfo information in the audio content data object.
596 result
597 _ContentUtility::FillAudioContentData(media_info_h mediaHandle, _AudioContentInfoImpl* pAudioContentInfoImpl)
598 {
599         SysTryReturnResult(NID_CNT, mediaHandle != null && pAudioContentInfoImpl != null, E_INVALID_ARG, "mediaHandle or pAudioContentInfoImpl is null.");
600
601         int ret  = MEDIA_CONTENT_ERROR_NONE;
602         result r = E_SUCCESS;
603         int bitrate = 0;
604         int duration = 0;
605
606         std::unique_ptr<char, UtilCharDeleter> pTitle;
607         std::unique_ptr<char, UtilCharDeleter> pAlbumName;
608         std::unique_ptr<char, UtilCharDeleter> pArtistName;
609         std::unique_ptr<char, UtilCharDeleter> pGenreName;
610         std::unique_ptr<char, UtilCharDeleter> pComposerName;
611         std::unique_ptr<char, UtilCharDeleter> pYear;
612         std::unique_ptr<char, UtilCharDeleter> pCopyRight;
613         std::unique_ptr<char, UtilCharDeleter> pTrackNum;
614
615         audio_meta_h tempMeta = NULL;
616
617         ret = media_info_get_audio(mediaHandle, &tempMeta);
618         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_audio operation.");
619
620         std::unique_ptr<audio_meta_s, AudioMetaHandleDeleter> pAudioHandle(tempMeta);
621         SysTryReturnResult(NID_CNT, pAudioHandle != null, E_OUT_OF_MEMORY, "pAudioHandle is null.");
622
623         ret = audio_meta_get_bit_rate(pAudioHandle.get(), &bitrate);
624         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_bit_rate operation.");
625
626         char* pAudioMetaValue = null;
627         ret = audio_meta_get_title(pAudioHandle.get(), &pAudioMetaValue);
628         if (pAudioMetaValue != null)
629         {
630                 pTitle.reset(pAudioMetaValue);
631         }
632         else
633         {
634                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_title operation.");
635         }
636
637         ret = audio_meta_get_album(pAudioHandle.get(), &pAudioMetaValue);
638         if (pAudioMetaValue != null)
639         {
640                 pAlbumName.reset(pAudioMetaValue);
641         }
642         else
643         {
644                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_album operation.");
645         }
646
647         ret = audio_meta_get_artist(pAudioHandle.get(), &pAudioMetaValue);
648         if (pAudioMetaValue != null)
649         {
650                 pArtistName.reset(pAudioMetaValue);
651         }
652         else
653         {
654                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_artist operation.");
655         }
656
657         ret = audio_meta_get_genre(pAudioHandle.get(), &pAudioMetaValue);
658         if (pAudioMetaValue != null)
659         {
660                 pGenreName.reset(pAudioMetaValue);
661         }
662         else
663         {
664                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_genre operation.");
665         }
666
667         ret = audio_meta_get_composer(pAudioHandle.get(), &pAudioMetaValue);
668         if (pAudioMetaValue != null)
669         {
670                 pComposerName.reset(pAudioMetaValue);
671         }
672         else
673         {
674                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_composer operation.");
675         }
676
677         ret = audio_meta_get_year(pAudioHandle.get(), &pAudioMetaValue);
678         if (pAudioMetaValue != null)
679         {
680                 pYear.reset(pAudioMetaValue);
681         }
682         else
683         {
684                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_year operation.");
685         }
686
687         ret = audio_meta_get_copyright(pAudioHandle.get(), &pAudioMetaValue);
688         if (pAudioMetaValue != null)
689         {
690                 pCopyRight.reset(pAudioMetaValue);
691         }
692         else
693         {
694                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_copyright operation.");
695         }
696
697         ret = audio_meta_get_track_num(pAudioHandle.get(), &pAudioMetaValue);
698         if (pAudioMetaValue != null)
699         {
700                 pTrackNum.reset(pAudioMetaValue);
701         }
702         else
703         {
704                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_track_num operation.");
705         }
706
707         ret = audio_meta_get_duration(pAudioHandle.get(), &duration);
708         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform audio_meta_get_duration operation.");
709
710         pAudioContentInfoImpl->SetBitrate(bitrate);
711
712         if (pYear.get() != null)
713         {
714                 SysLog(NID_CNT, "pAudioContentInfoImpl->pYear  = %s", pYear.get());
715
716                 String strYear(pYear.get());
717                 if (strYear.CompareTo(L"Unknown") != 0)
718                 {
719                         int releaseYear = 0;
720
721                         r = Integer::Parse(strYear, releaseYear);
722                         if (IsFailed(r))
723                         {
724                                 // It is one of the metadata. If error occurs, skip it for other metadata.
725                                 pAudioContentInfoImpl->SetReleaseYear(0);
726                                 r = E_SUCCESS;
727                                 SysLog(NID_CNT, "pAudioContentInfoImpl->pYear(invalid data) = %ls", strYear.GetPointer());
728                         }
729
730                         pAudioContentInfoImpl->SetReleaseYear(releaseYear);
731                 }
732         }
733
734         if (pTitle.get() != NULL)
735         {
736                 pAudioContentInfoImpl->SetTitle(String(pTitle.get()));
737                 SysLog(NID_CNT, "pAudioContentInfoImpl->pTitle  = %ls", (pAudioContentInfoImpl->GetTitle()).GetPointer());
738         }
739
740         if (pArtistName.get() != NULL)
741         {
742                 pAudioContentInfoImpl->SetArtist(String(pArtistName.get()));
743                 SysLog(NID_CNT, "pAudioContentInfoImpl->pArtist = %ls", (pAudioContentInfoImpl->GetArtist()).GetPointer());
744         }
745
746         if (pGenreName.get() != NULL)
747         {
748                 pAudioContentInfoImpl->SetGenre(String(pGenreName.get()));
749                 SysLog(NID_CNT, "pAudioContentInfoImpl->pGenre = %ls", (pAudioContentInfoImpl->GetGenre()).GetPointer());
750         }
751
752         if (pComposerName.get() != NULL)
753         {
754                 pAudioContentInfoImpl->SetComposer(String(pComposerName.get()));
755                 SysLog(NID_CNT, "pAudioContentInfoImpl->pComposer = %ls", (pAudioContentInfoImpl->GetComposer()).GetPointer());
756         }
757
758         if (pAlbumName.get() != NULL)
759         {
760                 pAudioContentInfoImpl->SetAlbumName(String(pAlbumName.get()));
761                 SysLog(NID_CNT, "pAudioContentInfoImpl->pAlbumName  = %ls", (pAudioContentInfoImpl->GetAlbumName()).GetPointer());
762         }
763
764         if (pCopyRight.get() != NULL)
765         {
766                 pAudioContentInfoImpl->SetCopyright(String(pCopyRight.get()));
767                 SysLog(NID_CNT, "pAudioContentInfoImpl->pCopyright  = %ls", (pAudioContentInfoImpl->GetCopyright()).GetPointer());
768         }
769
770         if (pTrackNum.get() != NULL)
771         {
772                 pAudioContentInfoImpl->SetTrackInfo(String(pTrackNum.get()));
773                 SysLog(NID_CNT, "pAudioContentInfoImpl->pTrackInfo  = %ls", (pAudioContentInfoImpl->GetTrackInfo()).GetPointer());
774         }
775
776         pAudioContentInfoImpl->SetDuration(duration);
777         SysLog(NID_CNT,"pAudioContentInfoImpl->duration = %d", pAudioContentInfoImpl->GetDuration());
778
779         return r;
780 }
781
782 // Fills VideoContentInfo information in the video content data object.
783 result
784 _ContentUtility::FillVideoContentData(media_info_h mediaHandle, _VideoContentInfoImpl* pVideoContentInfoImpl)
785 {
786         SysTryReturnResult(NID_CNT, mediaHandle != null && pVideoContentInfoImpl != null, E_INVALID_ARG, "mediaHandle or pVideoContentInfoImpl is null.");
787
788         int ret  = MEDIA_CONTENT_ERROR_NONE;
789         result r = E_SUCCESS;
790         int width  = 0;
791         int height = 0;
792         std::unique_ptr<char, UtilCharDeleter> pArtistName;
793         std::unique_ptr<char, UtilCharDeleter> pGenreName;
794         std::unique_ptr<char, UtilCharDeleter> pTitle;
795         std::unique_ptr<char, UtilCharDeleter> pAlbumName;
796         int duration = 0;
797
798         video_meta_h tempMeta = NULL;
799
800         ret = media_info_get_video(mediaHandle, &tempMeta);
801         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform media_info_get_video operation.");
802
803         std::unique_ptr<video_meta_s, VideoMetaHandleDeleter> pVideoHandle(tempMeta);
804         SysTryReturnResult(NID_CNT, pVideoHandle != null, E_OUT_OF_MEMORY, "pVideoHandle is null.");
805
806         ret = video_meta_get_width(pVideoHandle.get(), &width);
807         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_width operation.");
808
809         ret = video_meta_get_height(pVideoHandle.get(), &height);
810         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_height operation.");
811
812         char* pVideoMetaValue = null;
813         ret = video_meta_get_artist(pVideoHandle.get(), &pVideoMetaValue);
814         if (pVideoMetaValue != null)
815         {
816                 pArtistName.reset(pVideoMetaValue);
817         }
818         else
819         {
820                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_artist operation.");
821         }
822
823         ret = video_meta_get_genre(pVideoHandle.get(), &pVideoMetaValue);
824         if (pVideoMetaValue != null)
825         {
826                 pGenreName.reset(pVideoMetaValue);
827         }
828         else
829         {
830                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_genre operation.");
831         }
832
833         ret = video_meta_get_title(pVideoHandle.get(), &pVideoMetaValue);
834         if (pVideoMetaValue != null)
835         {
836                 pTitle.reset(pVideoMetaValue);
837         }
838         else
839         {
840                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_title operation.");
841         }
842
843         ret = video_meta_get_album(pVideoHandle.get(), &pVideoMetaValue);
844         if (pVideoMetaValue != null)
845         {
846                 pAlbumName.reset(pVideoMetaValue);
847         }
848         else
849         {
850                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_album operation.");
851         }
852
853         ret = video_meta_get_duration(pVideoHandle.get(), &duration);
854         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, MapCoreErrorToNativeResult(ret), "Failed to perform video_meta_get_duration operation.");
855
856         pVideoContentInfoImpl->SetWidth(width);
857         pVideoContentInfoImpl->SetHeight(height);
858
859         if (pTitle.get() != NULL)
860         {
861                 pVideoContentInfoImpl->SetTitle(String(pTitle.get()));
862                 SysLog(NID_CNT, "pVideoContentInfoImpl->pTitle = %ls", (pVideoContentInfoImpl->GetTitle()).GetPointer());
863         }
864
865         if (pArtistName.get() != NULL)
866         {
867                 pVideoContentInfoImpl->SetArtist(String(pArtistName.get()));
868                 SysLog(NID_CNT, "pVideoContentInfoImpl->pArtist = %ls", (pVideoContentInfoImpl->GetArtist()).GetPointer());
869         }
870
871         if (pGenreName.get() != NULL)
872         {
873                 pVideoContentInfoImpl->SetGenre(String(pGenreName.get()));
874                 SysLog(NID_CNT, "pVideoContentInfoImpl->pGenre = %ls", (pVideoContentInfoImpl->GetGenre()).GetPointer());
875         }
876
877         if (pAlbumName.get() != NULL)
878         {
879                 pVideoContentInfoImpl->SetAlbumName(String(pAlbumName.get()));
880                 SysLog(NID_CNT, "pVideoContentInfoImpl->pAlbumName = %ls", (pVideoContentInfoImpl->GetAlbumName()).GetPointer());
881         }
882
883         pVideoContentInfoImpl->SetDuration(duration);
884         SysLog(NID_CNT,"pVideoContentInfoImpl->duration = %d", pVideoContentInfoImpl->GetDuration());
885
886         return r;
887 }
888
889 // Conversion of system errors to native errors
890 result
891 _ContentUtility::MapCoreErrorToNativeResult(int reason)
892 {
893         result r = E_SUCCESS;
894         switch (reason)
895         {
896         case MEDIA_CONTENT_ERROR_NONE:
897                 r = E_SUCCESS;
898                 break;
899         case MEDIA_CONTENT_ERROR_DB_BUSY:
900                 r = E_SERVICE_BUSY;
901                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
902                 break;
903         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
904                 r = E_INVALID_ARG;
905                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
906                 break;
907         case MEDIA_CONTENT_ERROR_DB_FAILED:
908                 r = E_DATABASE;
909                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
910                 break;
911         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
912                 r = E_OUT_OF_MEMORY;
913                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
914                 break;
915         default:
916                 SysLog(NID_CNT, "default");
917                 r = E_SYSTEM;
918                 break;
919         }
920         return r;
921 }
922
923 }}