[content] Detour DateTime parsing exception
[platform/framework/native/content.git] / src / FCnt_ContentManagerImpl.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_ContentManagerImpl.cpp
18  * @brief               This is the implementation file for the %_ContentManagerImpl class.
19  *
20  * This file contains implementation of the %_ContentManagerImpl class.
21  */
22
23 #include <new>
24 #include <stdlib.h>
25 #include <aul/aul.h>
26 #include <FAppApp.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseDateTime.h>
29 #include <FBaseInteger.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseUtilStringTokenizer.h>
32 #include <FCntContentManager.h>
33 #include <FCntContentManagerUtil.h>
34 #include <FCntImageContentInfo.h>
35 #include <FCntAudioContentInfo.h>
36 #include <FCntVideoContentInfo.h>
37 #include <FCntOtherContentInfo.h>
38 #include <FCntImageMetadata.h>
39 #include <FCntAudioMetadata.h>
40 #include <FCntVideoMetadata.h>
41 #include <FCntIContentScanListener.h>
42 #include <FCntIContentUpdateEventListener.h>
43 #include <FIoDirectory.h>
44 #include <FSysEnvironment.h>
45 #include <FApp_AppInfo.h>
46 #include <FBase_StringConverter.h>
47 #include <FIo_FileImpl.h>
48 #include "FCnt_ContentManagerImpl.h"
49 #include "FCnt_ContentManagerUtilImpl.h"
50 #include "FCnt_ContentInfoImpl.h"
51 #include "FCnt_ImageContentInfoImpl.h"
52 #include "FCnt_AudioContentInfoImpl.h"
53 #include "FCnt_VideoContentInfoImpl.h"
54 #include "FCnt_OtherContentInfoImpl.h"
55 #include "FCnt_ContentInfoHelper.h"
56
57 using namespace std;
58 using namespace Tizen::App;
59 using namespace Tizen::Base;
60 using namespace Tizen::Base::Collection;
61 using namespace Tizen::Base::Utility;
62 using namespace Tizen::Io;
63 using namespace Tizen::Locations;
64 using namespace Tizen::System;
65
66 namespace Tizen { namespace Content
67 {
68
69 static const int SYSTEM_TYPE_IMAGE = 0;
70 static const int SYSTEM_TYPE_VIDEO = 1;
71 static const int SYSTEM_TYPE_SOUND = 2;
72 static const int SYSTEM_TYPE_MUSIC = 3;
73 static const int SYSTEM_TYPE_OTHER = 4;
74 static const double DEFAULT_COORDINATE = -200.0;
75
76 // For extern declaration in FCntTypes.h
77 const wchar_t OSP_HOME[] = L"/Home/";
78 const wchar_t OSP_HOME_EXT[] = L"/HomeExt/";
79 const wchar_t OSP_MEDIA_PHONE[] = L"/Media/";
80 const wchar_t OSP_MEDIA_MMC[] = L"/Storagecard/Media/";
81
82 static result
83 ConvertError(int error)
84 {
85         result r = E_SUCCESS;
86
87         switch (error)
88         {
89         case MEDIA_CONTENT_ERROR_NONE:
90                 r = E_SUCCESS;
91                 break;
92
93         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
94                 r = E_INVALID_ARG;
95                 break;
96
97         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
98                 r = E_OUT_OF_MEMORY;
99                 break;
100
101         case MEDIA_CONTENT_ERROR_DB_BUSY:
102                 r = E_SERVICE_BUSY;
103                 break;
104
105         case MEDIA_CONTENT_ERROR_DB_FAILED:
106                 r = E_SYSTEM;
107                 break;
108
109         default:
110                 r = E_SYSTEM;
111                 break;
112         }
113
114         return r;
115 }
116
117 static void
118 OnScanCompleted(media_content_error_e error, void* user_data)
119 {
120         SysLog(NID_CNT, "OnScanCompleted callback method is called.");
121
122         ClearLastResult();
123         result r = E_SUCCESS;
124         String scanPath(L"");
125         IContentScanListener* pListener = null;
126         RequestId reqId;
127         unique_ptr< _ScanResult > pScanResult;
128
129         SysTryLogCatch(NID_CNT, user_data != null, , "OnScanCompleted failed.");
130
131         pScanResult = unique_ptr< _ScanResult >(static_cast< _ScanResult* >(user_data));
132
133         SysTryLogCatch(NID_CNT, pScanResult != null, , "OnScanCompleted failed.");
134         SysTryLogCatch(NID_CNT, pScanResult->pScanListener != null, , "Listener is null. OnScanCompleted succeeded.");
135
136         scanPath = pScanResult->scanPath;
137         pListener = pScanResult->pScanListener;
138         reqId = pScanResult->requestId;
139
140         r = ConvertError(error);
141
142         pListener->OnContentScanCompleted(reqId, scanPath, r);
143         SysLog(NID_CNT, "OnContentScanCompleted fired.");
144
145 CATCH:
146         int val = media_content_disconnect();
147         SysTryLog(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, "The disconnection failed[%d].", val);
148 }
149
150 static void
151 OnContentUpdateCompleted(media_content_error_e error, int pid, media_content_db_update_item_type_e update_item,
152                 media_content_db_update_type_e update_type, media_content_type_e media_type, char* uuid, char* path, char* mime_type, void* user_data)
153 {
154         SysLog(NID_CNT, "OnContentUpdateCompleted callback method is called.");
155
156         result r = E_SUCCESS;
157
158         SysTryLogReturn(NID_CNT, user_data != null, , "OnContentUpdateCompleted failed.");
159
160         _ContentManagerImpl* pTempManagerImpl = static_cast< _ContentManagerImpl* >(user_data);
161         SysTryLogReturn(NID_CNT, pTempManagerImpl != null, , "pTempManagerImpl is null.");
162
163         IContentUpdateEventListener* pListener = pTempManagerImpl->GetListener();
164         SysTryLogReturn(NID_CNT, pListener != null, , "IContentUpdateEventListener is null.");
165
166         if (error != MEDIA_CONTENT_ERROR_NONE)
167         {
168                 r = E_SYSTEM;
169         }
170
171         switch (update_item)
172         {
173         case MEDIA_ITEM_FILE:
174         {
175                 ContentType contentType = CONTENT_TYPE_UNKNOWN;
176
177                 switch (media_type)
178                 {
179                 case MEDIA_CONTENT_TYPE_IMAGE:
180                         contentType = CONTENT_TYPE_IMAGE;
181                         break;
182
183                 case MEDIA_CONTENT_TYPE_VIDEO:
184                         contentType = CONTENT_TYPE_VIDEO;
185                         break;
186
187                 case MEDIA_CONTENT_TYPE_SOUND:
188                         // fall through
189                 case MEDIA_CONTENT_TYPE_MUSIC:
190                         contentType = CONTENT_TYPE_AUDIO;
191                         break;
192
193                 case MEDIA_CONTENT_TYPE_OTHERS:
194                         contentType = CONTENT_TYPE_OTHER;
195                         break;
196
197                 default:
198                         r = E_SYSTEM;
199                         SysLog(NID_CNT, "media_type is invalid.");
200                         break;
201                 }
202
203                 String str(uuid);
204                 ContentId contentId;
205
206                 result res = UuId::Parse(str, contentId);
207                 SysTryLogReturn(NID_CNT, !IsFailed(res), , "Failed to parse to the content ID.");
208
209                 switch (update_type)
210                 {
211                 case MEDIA_CONTENT_INSERT:
212                         pListener->OnContentFileCreated(contentId, contentType, r);
213                         break;
214
215                 case MEDIA_CONTENT_DELETE:
216                         pListener->OnContentFileDeleted(contentId, contentType, r);
217                         break;
218
219                 case MEDIA_CONTENT_UPDATE:
220                         pListener->OnContentFileUpdated(contentId, contentType, r);
221                         break;
222
223                 default:
224                         SysLog(NID_CNT, "update_type is invalid.");
225                         break;
226                 }
227                 break;
228         }
229
230         case MEDIA_ITEM_DIRECTORY:
231         {
232                 String directoryPath(path);
233
234                 pListener->OnContentDirectoryScanCompleted(directoryPath, r);
235                 break;
236         }
237
238         default:
239                 SysLog(NID_CNT, "update_item is invalid.");
240                 break;
241         }
242
243         SysLog(NID_CNT, "Fire the OnContentUpdateCompleted method.");
244 }
245
246 _ContentManagerImpl::_ContentManagerImpl(void)
247         : __isConnected(false)
248         , __pListener(null)
249 {
250
251 }
252
253 _ContentManagerImpl::~_ContentManagerImpl(void)
254 {
255         int val;
256
257         if (GetListener())
258         {
259                 val = media_content_unset_db_updated_cb();
260                 SysLog(NID_CNT, "media_content_unset_db_updated_cb result[%d].", val);
261         }
262
263         val = media_content_disconnect();
264         SysLog(NID_CNT, "media_content_disconnect result[%d].", val);
265 }
266
267 _ContentManagerImpl*
268 _ContentManagerImpl::GetInstance(ContentManager& contentManager)
269 {
270         return contentManager.__pContentManagerImpl;
271 }
272
273 const _ContentManagerImpl*
274 _ContentManagerImpl::GetInstance(const ContentManager& contentManager)
275 {
276         return contentManager.__pContentManagerImpl;
277 }
278
279 result
280 _ContentManagerImpl::Construct(void)
281 {
282         int val = media_content_connect();
283         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM,
284                         "The connection failed[%d].", val);
285
286         SysLog(NID_CNT, "media_content_connect result[%d].", val);
287
288         __isConnected = true;
289
290         return E_SUCCESS;
291 }
292
293 ContentId
294 _ContentManagerImpl::CreateContent(const ContentInfo& contentInfo)
295 {
296         ClearLastResult();
297
298         _ContentInfoImpl* pInfoImpl = const_cast< _ContentInfoImpl* >(_ContentInfoImpl::GetInstance(contentInfo));
299         SysTryReturn(NID_CNT, pInfoImpl != null, UuId::GetInvalidUuId(), E_INVALID_ARG,
300                         "[E_INVALID_ARG] Invalid argument is used. ContentInfo is invalid.");
301
302         SysTryReturn(NID_CNT, VerifyMediaFilePathCompatibility(pInfoImpl->GetContentPath(), true), UuId::GetInvalidUuId(),
303                         E_INVALID_ARG, "[E_INVALID_ARG] The contentPath is not compatible.");
304         SysTryReturn(NID_CNT, _FileImpl::IsFileExist(pInfoImpl->GetContentPath()), UuId::GetInvalidUuId(), E_FILE_NOT_FOUND,
305                         "[E_FILE_NOT_FOUND] The file corresponding to contentInfo could not be found.");
306         SysTryReturn(NID_CNT, pInfoImpl->GetContentId() == UuId::GetInvalidUuId(), UuId::GetInvalidUuId(),
307                         E_INVALID_ARG, "[E_INVALID_ARG] The contentId is not empty.");
308
309         // Compare the type of input parameter and system
310         ContentType inputType = pInfoImpl->GetContentType();
311         ContentType sysType = _ContentManagerUtilImpl::CheckContentType(pInfoImpl->GetContentPath(), true);
312
313         ClearLastResult();
314
315         if (inputType != sysType)
316         {
317                 if (!(inputType == CONTENT_TYPE_OTHER && sysType == CONTENT_TYPE_UNKNOWN))
318                 {
319                         SysLogException(NID_CNT, E_INVALID_ARG,
320                                         "[E_INVALID_ARG] The type is not match[%d, %d].", inputType, sysType);
321                         return UuId::GetInvalidUuId();
322                 }
323         }
324
325         // Save data to database with contentPath.
326         unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(contentInfo.GetContentPath()));
327         SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
328                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
329
330         media_info_h tempMediaInfo = null;
331         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
332
333         int val = media_info_insert_to_db(pStr.get(), &tempMediaInfo);
334         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
335                         "The registration to database failed[%d].", val);
336
337         pMediaInfo.reset(tempMediaInfo);
338
339         ContentId contentId = SaveDataToDatabase(pMediaInfo.get(), pInfoImpl);
340         result r = GetLastResult();
341         SysTryCatch(NID_CNT, contentId != UuId::GetInvalidUuId(), , r,
342                         "[%s] The registration to database failed.", GetErrorMessage(r));
343
344         return contentId;
345
346 CATCH:
347         // There are two steps(insert and update) for content registration.
348         // If the update failed after inserting, the inserted data SHOULD be deleted from here.
349         val = media_info_delete_from_db(pStr.get());
350         SysLog(NID_CNT, "The result of deletion from database[%d].", val);
351
352         return UuId::GetInvalidUuId();
353 }
354
355 ContentId
356 _ContentManagerImpl::CreateContent(const ByteBuffer& byteBuffer, const String& destinationPath,
357                                             const ContentInfo* pContentInfo)
358 {
359         ClearLastResult();
360
361         // Check parameters(for length)
362         SysTryReturn(NID_CNT, byteBuffer.GetRemaining() > 0, UuId::GetInvalidUuId(), E_INVALID_ARG,
363                         "[E_INVALID_ARG] byteBuffer is invalid.");
364
365         // Check parameters(for path compatibility)
366         SysTryReturn(NID_CNT, VerifyMediaFilePathCompatibility(destinationPath, false), UuId::GetInvalidUuId(),
367                         E_INVALID_ARG, "[E_INVALID_ARG] %ls is not compatible.", destinationPath.GetPointer());
368         SysTryReturn(NID_CNT, !_FileImpl::IsFileExist(destinationPath), UuId::GetInvalidUuId(), E_FILE_ALREADY_EXIST,
369                         "[E_FILE_ALREADY_EXIST] The specified file already exists.");
370
371         // Create a file with bytebuffer
372         unique_ptr<File> pFile(new (nothrow) File);
373         SysTryReturn(NID_CNT, pFile != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
374                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
375
376         result r = pFile->Construct(destinationPath, L"w+");
377         SysTryReturn(NID_CNT, !IsFailed(r), UuId::GetInvalidUuId(), r,
378                         "[%s] The destination file can not be created.", GetErrorMessage(r));
379
380         _FileImpl* pFileImpl = _FileImpl::GetInstance(*pFile);
381         SysTryReturn(NID_CNT, pFileImpl != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
382                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
383
384         r = pFileImpl->Write(byteBuffer);
385         SysTryReturn(NID_CNT, !(IsFailed(r)), UuId::GetInvalidUuId(), r,
386                         "[%s] The data can not be written in the destination file.", GetErrorMessage(r));
387
388         // for release file pointer
389         pFile.reset();
390
391         int val = 0;
392         ContentId contentId;
393         _ContentInfoImpl* pInfoImpl = null;
394         _ImageContentInfoImpl imageContentInfoImpl;
395         _AudioContentInfoImpl audioContentInfoImpl;
396         _VideoContentInfoImpl videoContentInfoImpl;
397         _OtherContentInfoImpl otherContentInfoImpl;
398         media_info_h tempMediaInfo = null;
399         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
400         unique_ptr<char[]> pStr(null);
401
402         if (pContentInfo != null)
403         {
404                 pInfoImpl = const_cast< _ContentInfoImpl* >(_ContentInfoImpl::GetInstance(*pContentInfo));
405                 SysTryCatch(NID_CNT, pInfoImpl != null, , E_INVALID_ARG,
406                                 "[E_INVALID_ARG] Invalid argument is used. ContentInfo is invalid.");
407
408                 SysTryCatch(NID_CNT, pInfoImpl->GetContentId() == UuId::GetInvalidUuId(), , E_INVALID_ARG,
409                                 "[E_INVALID_ARG] The content already exists in database.");
410
411                 // Compare the type of input parameter and system
412                 // CheckContentType() need actual file. so it should be checked after creating the file.
413                 ContentType sysType = _ContentManagerUtilImpl::CheckContentType(destinationPath, true);
414                 ContentType inputType = pInfoImpl->GetContentType();
415
416                 ClearLastResult();
417
418                 if (inputType != sysType)
419                 {
420                         if (!(inputType == CONTENT_TYPE_OTHER && sysType == CONTENT_TYPE_UNKNOWN))
421                         {
422                                 SysLogException(NID_CNT, E_INVALID_ARG,
423                                                 "[E_INVALID_ARG] The type is not match[%d, %d].", inputType, sysType);
424                                 goto CATCH;
425                         }
426                 }
427
428                 // Sets the content path
429                 pInfoImpl->SetContentPath(destinationPath);
430         }
431         else
432         {
433                 ContentType contentType = _ContentManagerUtilImpl::CheckContentType(destinationPath, true);
434
435                 ClearLastResult();
436
437                 // Set the content path
438                 if (contentType == CONTENT_TYPE_IMAGE)
439                 {
440                         r = imageContentInfoImpl.Construct(&destinationPath);
441                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
442                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for ImageContentInfoImpl.");
443
444                         imageContentInfoImpl.SetContentPath(destinationPath);
445                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&imageContentInfoImpl);
446                 }
447                 else if (contentType == CONTENT_TYPE_AUDIO)
448                 {
449                         r = audioContentInfoImpl.Construct(&destinationPath);
450                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
451                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for AudioContentInfoImpl.");
452
453                         audioContentInfoImpl.SetContentPath(destinationPath);
454                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&audioContentInfoImpl);
455                 }
456                 else if (contentType == CONTENT_TYPE_VIDEO)
457                 {
458                         r = videoContentInfoImpl.Construct(&destinationPath);
459                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
460                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for VideoContentInfoImpl.");
461
462                         videoContentInfoImpl.SetContentPath(destinationPath);
463                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&videoContentInfoImpl);
464                 }
465                 else
466                 {
467                         r = otherContentInfoImpl.Construct(&destinationPath);
468                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
469                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for OtherContentInfoImpl.");
470
471                         otherContentInfoImpl.SetContentPath(destinationPath);
472                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&otherContentInfoImpl);
473                 }
474         }
475
476         // Register the content to database directly.
477         pStr.reset(_StringConverter::CopyToCharArrayN(destinationPath));
478         SysTryCatch(NID_CNT, pStr != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
479
480         val = media_info_insert_to_db(pStr.get(), &tempMediaInfo);
481         SysTryCatch(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, , ConvertError(val),
482                         "media_info_insert_to_db failed[%d].", val);
483
484         pMediaInfo.reset(tempMediaInfo);
485
486         contentId = SaveDataToDatabase(pMediaInfo.get(), pInfoImpl);
487         r = GetLastResult();
488         SysTryCatch(NID_CNT, contentId != UuId::GetInvalidUuId(), , r,
489                         "[%s] SaveDataToDatabase failed.", GetErrorMessage(r));
490
491         return contentId;
492
493 CATCH:
494         result saveResult = GetLastResult();
495
496         // If the destination file is made by this method, it should be deleted when error occurs.
497         r = _FileImpl::Remove(destinationPath);
498         SysLog(NID_CNT, "Remove[%s].", GetErrorMessage(r));
499
500         if (pMediaInfo != null)
501         {
502                 val = media_info_delete_from_db(pStr.get());
503                 SysLog(NID_CNT, "The result of deletion from database[%d].", val);
504         }
505
506         SetLastResult(saveResult);
507         return UuId::GetInvalidUuId();
508 }
509
510 ContentId
511 _ContentManagerImpl::CreateContent(const String& sourcePath, const String& destinationPath, bool deleteSource,
512                                             const ContentInfo* pContentInfo)
513 {
514         ClearLastResult();
515
516         // Check parameters(for type)
517         SysTryReturn(NID_CNT, _FileImpl::GetFileExtension(sourcePath) == _FileImpl::GetFileExtension(destinationPath),
518                         UuId::GetInvalidUuId(), E_INVALID_ARG, "[E_INVALID_ARG] There is a mismatch between the type of source and dest path.");
519
520         // Check parameters(for path compatibility)
521         SysTryReturn(NID_CNT, VerifyHomeFilePathCompatibility(sourcePath), UuId::GetInvalidUuId(), E_INVALID_ARG,
522                         "[E_INVALID_ARG] %ls is not compatible.", sourcePath.GetPointer());
523         SysTryReturn(NID_CNT, VerifyMediaFilePathCompatibility(destinationPath, false), UuId::GetInvalidUuId(),
524                         E_INVALID_ARG, "[E_INVALID_ARG] %ls is not compatible.", destinationPath.GetPointer());
525
526         result r = E_SUCCESS;
527         bool isCopy = false;
528
529         if (deleteSource) // move
530         {
531                 r = _FileImpl::Move(sourcePath, destinationPath);
532                 SysTryReturn(NID_CNT, !IsFailed(r), UuId::GetInvalidUuId(), r, "[%s] Moving the file failed.", GetErrorMessage(r));
533         }
534         else // copy
535         {
536                 r = _FileImpl::Copy(sourcePath, destinationPath, true);
537                 SysTryReturn(NID_CNT, !IsFailed(r), UuId::GetInvalidUuId(), r, "[%s] Copying the file failed.", GetErrorMessage(r));
538
539                 isCopy = true;
540         }
541
542         int val = 0;
543         ContentId contentId;
544         _ContentInfoImpl* pInfoImpl = null;
545         _ImageContentInfoImpl imageContentInfoImpl;
546         _AudioContentInfoImpl audioContentInfoImpl;
547         _VideoContentInfoImpl videoContentInfoImpl;
548         _OtherContentInfoImpl otherContentInfoImpl;
549         media_info_h tempMediaInfo = null;
550         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
551         unique_ptr<char[]> pStr(null);
552
553         if (pContentInfo != null)
554         {
555                 pInfoImpl = const_cast< _ContentInfoImpl* >(_ContentInfoImpl::GetInstance(*pContentInfo));
556                 SysTryCatch(NID_CNT, pInfoImpl != null, , E_INVALID_ARG,
557                                 "[E_INVALID_ARG] Invalid argument is used. ContentInfo is invalid.");
558
559                 SysTryCatch(NID_CNT, pInfoImpl->GetContentId() == UuId::GetInvalidUuId(), , E_INVALID_ARG,
560                                 "[E_INVALID_ARG] The content already exists in database.");
561
562                 // Compare the type of input parameter and system
563                 // CheckContentType() need actual file. so it should be checked after creating the file.
564                 ContentType sysType = _ContentManagerUtilImpl::CheckContentType(destinationPath, true);
565                 ContentType inputType = pInfoImpl->GetContentType();
566
567                 ClearLastResult();
568
569                 if (inputType != sysType)
570                 {
571                         if (!(inputType == CONTENT_TYPE_OTHER && sysType == CONTENT_TYPE_UNKNOWN))
572                         {
573                                 SysLogException(NID_CNT, E_INVALID_ARG,
574                                                  "[E_INVALID_ARG] The type is not match[%d, %d].", inputType, sysType);
575                                 goto CATCH;
576                         }
577                 }
578
579                 // Set the content path
580                 pInfoImpl->SetContentPath(destinationPath);
581         }
582         else
583         {
584                 ContentType contentType = _ContentManagerUtilImpl::CheckContentType(destinationPath, true);
585
586                 ClearLastResult();
587
588                 // Set the content path
589                 if (contentType == CONTENT_TYPE_IMAGE)
590                 {
591                         r = imageContentInfoImpl.Construct(&destinationPath);
592                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
593                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for ImageContentInfoImpl.");
594
595                         imageContentInfoImpl.SetContentPath(destinationPath);
596                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&imageContentInfoImpl);
597                 }
598                 else if (contentType == CONTENT_TYPE_AUDIO)
599                 {
600                         r = audioContentInfoImpl.Construct(&destinationPath);
601                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
602                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for AudioContentInfoImpl.");
603
604                         audioContentInfoImpl.SetContentPath(destinationPath);
605                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&audioContentInfoImpl);
606                 }
607                 else if (contentType == CONTENT_TYPE_VIDEO)
608                 {
609                         r = videoContentInfoImpl.Construct(&destinationPath);
610                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
611                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for VideoContentInfoImpl.");
612
613                         videoContentInfoImpl.SetContentPath(destinationPath);
614                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&videoContentInfoImpl);
615                 }
616                 else
617                 {
618                         r = otherContentInfoImpl.Construct(&destinationPath);
619                         SysTryCatch(NID_CNT, !IsFailed(r), , E_OUT_OF_MEMORY,
620                                         "[E_OUT_OF_MEMORY] Failed to perform Construct for OtherContentInfoImpl.");
621
622                         otherContentInfoImpl.SetContentPath(destinationPath);
623                         pInfoImpl = dynamic_cast< _ContentInfoImpl* >(&otherContentInfoImpl);
624                 }
625         }
626
627         pStr.reset(_StringConverter::CopyToCharArrayN(destinationPath));
628         SysTryCatch(NID_CNT, pStr != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
629
630         val = media_info_insert_to_db(pStr.get(), &tempMediaInfo);
631         SysTryCatch(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, , ConvertError(val),
632                         "media_info_insert_to_db failed[%d].", val);
633
634         pMediaInfo.reset(tempMediaInfo);
635
636         contentId = SaveDataToDatabase(pMediaInfo.get(), pInfoImpl);
637         r = GetLastResult();
638         SysTryCatch(NID_CNT, contentId != UuId::GetInvalidUuId(), , r,
639                         "[%s] SaveDataToDatabase failed.", GetErrorMessage(r));
640
641         return contentId;
642
643 CATCH:
644         result saveResult = GetLastResult();
645
646         if (isCopy)
647         {
648                 r = _FileImpl::Remove(destinationPath);
649                 SysLog(NID_CNT, "Remove[%s].", GetErrorMessage(r));
650         }
651         else
652         {
653                 r = _FileImpl::Move(destinationPath, sourcePath);
654                 SysLog(NID_CNT, "Move[%s].", GetErrorMessage(r));
655         }
656
657         if (pMediaInfo != null)
658         {
659                 val = media_info_delete_from_db(pStr.get());
660                 SysLog(NID_CNT, "The result of deletion from database[%d].", val);
661         }
662
663         SetLastResult(saveResult);
664         return UuId::GetInvalidUuId();
665 }
666
667 ContentInfo*
668 _ContentManagerImpl::GetContentInfoN(const ContentId& contentId) const
669 {
670         ClearLastResult();
671
672         SysTryReturn(NID_CNT, contentId != UuId::GetInvalidUuId(), null, E_INVALID_ARG,
673                         "[E_INVALID_ARG] The contentId is invalid.");
674
675         unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(contentId.ToString()));
676         SysTryReturn(NID_CNT, pStr != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
677
678         media_info_h tempMediaInfo = null;
679         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
680         int val = media_info_get_media_from_db(pStr.get(), &tempMediaInfo);
681         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, null, ConvertError(val),
682                         "media_info_get_media_from_db failed[%d].", val);
683
684         pMediaInfo.reset(tempMediaInfo);
685
686         media_content_type_e systemType = MEDIA_CONTENT_TYPE_IMAGE;
687         val = media_info_get_media_type(pMediaInfo.get(), &systemType);
688         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, null, ConvertError(val),
689                         "media_info_get_media_type failed[%d].", val);
690
691         char* pTempFilePath = null;
692         val = media_info_get_file_path(pMediaInfo.get(), &pTempFilePath);
693         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, null, ConvertError(val),
694                         "media_info_get_file_path failed[%d].", val);
695
696         unique_ptr<char, CharDeleter> pFilePath(pTempFilePath);
697         String contentPath(pFilePath.get());
698
699         SysTryReturn(NID_CNT, _FileImpl::IsFileExist(contentPath), null, E_FILE_NOT_FOUND,
700                         "[E_FILE_NOT_FOUND] The file corresponding to contentId could not be found.");
701
702         result r = E_SUCCESS;
703
704         if (systemType == SYSTEM_TYPE_IMAGE)
705         {
706                 unique_ptr< ImageContentInfo > pImageContentInfo(new (nothrow) ImageContentInfo);
707                 SysTryReturn(NID_CNT, pImageContentInfo != null, null, E_OUT_OF_MEMORY,
708                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
709
710                 r = pImageContentInfo->Construct(&contentPath);
711                 r = ConvertErrorToResult(r);
712                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
713                                 "[%s] Failed to perform Construct operation to ImageContentInfo.", GetErrorMessage(r));
714
715                 _ImageContentInfoImpl* pImageContentInfoImpl = _ImageContentInfoImpl::GetInstance(*(pImageContentInfo.get()));
716                 SysTryReturn(NID_CNT, pImageContentInfoImpl != null, null, E_OUT_OF_MEMORY,
717                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
718
719                 r = MakeContentInfo(pMediaInfo.get(), systemType, pImageContentInfoImpl);
720                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] MakeContentInfo failed.", GetErrorMessage(r));
721
722                 return pImageContentInfo.release();
723         }
724         else if (systemType == SYSTEM_TYPE_SOUND || systemType == SYSTEM_TYPE_MUSIC)
725         {
726                 unique_ptr< AudioContentInfo > pAudioContentInfo(new (nothrow) AudioContentInfo);
727                 SysTryReturn(NID_CNT, pAudioContentInfo != null, null, E_OUT_OF_MEMORY,
728                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
729
730                 r = pAudioContentInfo->Construct(&contentPath);
731                 r = ConvertErrorToResult(r);
732                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
733                                 "[%s] Failed to perform Construct operation to AudioContentInfo.", GetErrorMessage(r));
734
735                 _AudioContentInfoImpl* pAudioContentInfoImpl = _AudioContentInfoImpl::GetInstance(*(pAudioContentInfo.get()));
736                 SysTryReturn(NID_CNT, pAudioContentInfoImpl != null, null, E_OUT_OF_MEMORY,
737                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
738
739                 r = MakeContentInfo(pMediaInfo.get(), systemType, pAudioContentInfoImpl);
740                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] MakeContentInfo failed.", GetErrorMessage(r));
741
742                 return pAudioContentInfo.release();
743         }
744         else if (systemType == SYSTEM_TYPE_VIDEO)
745         {
746                 unique_ptr< VideoContentInfo > pVideoContentInfo(new (nothrow) VideoContentInfo);
747                 SysTryReturn(NID_CNT, pVideoContentInfo != null, null, E_OUT_OF_MEMORY,
748                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
749
750                 r = pVideoContentInfo->Construct(&contentPath);
751                 r = ConvertErrorToResult(r);
752                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
753                                 "[%s] Failed to perform Construct operation to VideoContentInfo.", GetErrorMessage(r));
754
755                 _VideoContentInfoImpl* pVideoContentInfoImpl = _VideoContentInfoImpl::GetInstance(*(pVideoContentInfo.get()));
756                 SysTryReturn(NID_CNT, pVideoContentInfoImpl != null, null, E_OUT_OF_MEMORY,
757                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
758
759                 r = MakeContentInfo(pMediaInfo.get(), systemType, pVideoContentInfoImpl);
760                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] MakeContentInfo failed.", GetErrorMessage(r));
761
762                 return pVideoContentInfo.release();
763         }
764         else if (systemType == SYSTEM_TYPE_OTHER)
765         {
766                 unique_ptr< OtherContentInfo > pOtherContentInfo(new (nothrow) OtherContentInfo);
767                 SysTryReturn(NID_CNT, pOtherContentInfo != null, null, E_OUT_OF_MEMORY,
768                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
769
770                 r = pOtherContentInfo->Construct(&contentPath);
771                 r = ConvertErrorToResult(r);
772                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
773                                 "[%s] Failed to perform Construct operation to OtherContentInfo.", GetErrorMessage(r));
774
775                 _OtherContentInfoImpl* pOtherContentInfoImpl = _OtherContentInfoImpl::GetInstance(*(pOtherContentInfo.get()));
776                 SysTryReturn(NID_CNT, pOtherContentInfoImpl != null, null, E_OUT_OF_MEMORY,
777                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
778
779                 r = MakeContentInfo(pMediaInfo.get(), systemType, pOtherContentInfoImpl);
780                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] MakeContentInfo failed.", GetErrorMessage(r));
781
782                 return pOtherContentInfo.release();
783         }
784         else
785         {
786                 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported type");
787                 return null;
788         }
789 }
790
791 result
792 _ContentManagerImpl::UpdateContent(const ContentInfo& contentInfo)
793 {
794         ContentId contentId = contentInfo.GetContentId();
795         SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), E_INVALID_ARG, "The content id is invalid.");
796         SysTryReturnResult(NID_CNT, _FileImpl::IsFileExist(contentInfo.GetContentPath()), E_FILE_NOT_FOUND,
797                         "The file corresponding to contentInfo could not be found.");
798
799         const _ContentInfoImpl* pInfoImpl = _ContentInfoImpl::GetInstance(contentInfo);
800         SysTryReturnResult(NID_CNT, pInfoImpl != null, E_INVALID_ARG, "Invalid argument is used. ContentInfo is invalid.");
801
802         result r = UpdateDataToDatabase(pInfoImpl);
803         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "UpdateDataToDatabase failed.");
804
805         return r;
806 }
807
808 result
809 _ContentManagerImpl::DeleteContent(const ContentId& contentId)
810 {
811         SysTryReturnResult(NID_CNT, contentId != UuId::GetInvalidUuId(), E_INVALID_ARG, "The contentId is invalid.");
812
813         unique_ptr<char[]> pContentId(_StringConverter::CopyToCharArrayN(contentId.ToString()));
814         SysTryReturnResult(NID_CNT, pContentId != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
815
816         result r = E_SUCCESS;
817         int val = 0;
818         media_info_h tempMediaInfo = null;
819         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
820
821         val = media_info_get_media_from_db(pContentId.get(), &tempMediaInfo);
822         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
823                         "media_info_get_media_from_db failed[%d].", val);
824
825         pMediaInfo.reset(tempMediaInfo);
826
827         char* pTempPath = null;
828
829         val = media_info_get_file_path(pMediaInfo.get(), &pTempPath);
830         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
831                         "media_info_get_file_path failed[%d].", val);
832
833         unique_ptr<char, CharDeleter> pContentPath(pTempPath);
834
835         String contentPath = String(pContentPath.get());
836         SysTryReturnResult(NID_CNT, _FileImpl::IsFileExist(contentPath), E_FILE_NOT_FOUND,
837                         "The file corresponding to contentId could not be found.");
838
839         val = media_info_delete_from_db(pContentId.get());
840         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
841                         "media_info_delete_From_db failed[%d].", val);
842
843         r = _FileImpl::Remove(contentPath);
844         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "The file is not deleted.");
845
846         return r;
847 }
848
849 result
850 _ContentManagerImpl::AddContentUpdateEventListener(IContentUpdateEventListener& listener)
851 {
852         SysTryReturnResult(NID_CNT, GetListener() == null, E_OBJ_ALREADY_EXIST, "IContentUpdateEventListener is already set.");
853
854         SetListener(&listener);
855
856         int val = media_content_set_db_updated_cb(OnContentUpdateCompleted, this);
857         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "media_content_set_db_updated_cb failed[%d].", val);
858
859         return E_SUCCESS;
860 }
861
862 result
863 _ContentManagerImpl::RemoveContentUpdateEventListener(IContentUpdateEventListener& listener)
864 {
865         SysTryReturnResult(NID_CNT, GetListener() == &listener, E_OBJ_NOT_FOUND, "The input listener is not equal to the registered listener.");
866
867         int val = media_content_unset_db_updated_cb();
868         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "media_content_unset_db_updated_cb failed[%d].", val);
869
870         SetListener(null);
871
872         return E_SUCCESS;
873 }
874
875 result
876 _ContentManagerImpl::ScanFile(const Tizen::Base::String& contentPath)
877 {
878         SysSecureLog(NID_CNT, "The scan path is [%ls].", contentPath.GetPointer());
879
880         unique_ptr<char[]> pContentPath(_StringConverter::CopyToCharArrayN(contentPath));
881         SysTryReturnResult(NID_CNT, pContentPath, E_SYSTEM, "pContentPath is NULL.");
882
883         int val = media_content_connect();
884         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "The connection failed[%d].", val);
885
886         val = media_content_scan_file(pContentPath.get());
887         result r = ConvertError(val);
888         SysTryCatch(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, , r, "[%s] media_content_scan_file failed[%d].", GetErrorMessage(r), val);
889
890         val = media_content_disconnect();
891         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "The disconnection failed[%d].", val);
892
893         return E_SUCCESS;
894
895 CATCH:
896         val = media_content_disconnect();
897         SysTryLog(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, "The disconnection failed[%d].", val);
898
899         return r;
900 }
901
902 result
903 _ContentManagerImpl::ScanDirectory(const Tizen::Base::String& directoryPath, bool recursive, IContentScanListener* pListener, RequestId& reqId)
904 {
905         static RequestId requestId = 0;
906
907         SysLog(NID_CNT, "The scan path is [%ls].", directoryPath.GetPointer());
908
909         unique_ptr<char[]> pDirPath(_StringConverter::CopyToCharArrayN(directoryPath));
910         SysTryReturnResult(NID_CNT, pDirPath, E_SYSTEM, "pDirPath is NULL.");
911
912         int val = media_content_connect();
913         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, E_SYSTEM, "The connection failed[%d].", val);
914
915         unique_ptr< _ScanResult > pScanResult(new (nothrow) _ScanResult);
916         SysTryReturnResult(NID_CNT, pScanResult != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
917
918         reqId = requestId++;
919
920         pScanResult->scanPath = directoryPath;
921         pScanResult->pScanListener = pListener;
922         pScanResult->requestId = reqId;
923
924         val = media_content_scan_folder(pDirPath.get(), recursive, OnScanCompleted, pScanResult.release());
925         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val), "media_content_scan_folder failed[%d].", val);
926
927         return E_SUCCESS;
928 }
929
930 result
931 _ContentManagerImpl::UpdateDataToDatabase(const _ContentInfoImpl* pContentInfoImpl) const
932 {
933         SysTryReturnResult(NID_CNT, pContentInfoImpl != null, E_INVALID_ARG, "pContentInfoImpl is null.");
934
935         media_info_h tempMediaInfo = null;
936         unique_ptr<media_info_s, _MediaInfoDeleter> pMediaInfo(null);
937         unique_ptr<char[]> pContentId(_StringConverter::CopyToCharArrayN((pContentInfoImpl->GetContentId()).ToString()));
938         SysTryReturnResult(NID_CNT, pContentId != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
939
940         int val = media_info_get_media_from_db(pContentId.get(), &tempMediaInfo);
941         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
942                         "media_info_get_media_from_db failed[%d].", val);
943
944         pMediaInfo.reset(tempMediaInfo);
945
946         result r = E_SUCCESS;
947         unique_ptr<char[]> pValue(null);
948
949         if (!pContentInfoImpl->GetAuthor().IsEmpty())
950         {
951                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetAuthor()));
952                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
953
954                 val = media_info_set_author(pMediaInfo.get(), pValue.get());
955                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
956                                 "media_info_set_author failed[%d].", val);
957         }
958         if (!pContentInfoImpl->GetCategory().IsEmpty())
959         {
960                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetCategory()));
961                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
962
963                 val = media_info_set_category(pMediaInfo.get(), pValue.get());
964                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
965                                 "media_info_set_category failed[%d].", val);
966         }
967         if (!pContentInfoImpl->GetContentName().IsEmpty())
968         {
969                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetContentName()));
970                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
971
972                 val = media_info_set_content_name(pMediaInfo.get(), pValue.get());
973                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
974                                 "media_info_set_content_name failed[%d].", val);
975         }
976         if (!pContentInfoImpl->GetDescription().IsEmpty())
977         {
978                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetDescription()));
979                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
980
981                 val = media_info_set_description(pMediaInfo.get(), pValue.get());
982                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
983                                 "media_info_set_description failed[%d].", val);
984         }
985         if (!pContentInfoImpl->GetKeyword().IsEmpty())
986         {
987                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetKeyword()));
988                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
989
990                 val = media_info_set_keyword(pMediaInfo.get(), pValue.get());
991                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
992                                 "media_info_set_keyword failed[%d].", val);
993         }
994         if (!pContentInfoImpl->GetLocationTag().IsEmpty())
995         {
996                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetLocationTag()));
997                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
998
999                 val = media_info_set_location_tag(pMediaInfo.get(), pValue.get());
1000                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1001                                 "media_info_set_location_tag failed[%d].", val);
1002         }
1003         if (!pContentInfoImpl->GetProvider().IsEmpty())
1004         {
1005                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetProvider()));
1006                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1007
1008                 val = media_info_set_provider(pMediaInfo.get(), pValue.get());
1009                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1010                                 "media_info_set_provider failed[%d].", val);
1011         }
1012         if (!pContentInfoImpl->GetRating().IsEmpty())
1013         {
1014                 pValue.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetRating()));
1015                 SysTryReturnResult(NID_CNT, pValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1016
1017                 val = media_info_set_age_rating(pMediaInfo.get(), pValue.get());
1018                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1019                                 "media_info_set_age_rating failed[%d].", val);
1020         }
1021         if (Double::Compare(pContentInfoImpl->GetLatitude(), DEFAULT_COORDINATE) != 0 &&
1022                         Double::Compare(pContentInfoImpl->GetLongitude(), DEFAULT_COORDINATE) != 0)
1023         {
1024                 val = media_info_set_latitude(pMediaInfo.get(), pContentInfoImpl->GetLatitude());
1025                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1026                                 "media_info_set_latitude failed[%d].", val);
1027
1028                 val = media_info_set_longitude(pMediaInfo.get(), pContentInfoImpl->GetLongitude());
1029                 SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1030                                 "media_info_set_longitude failed[%d].", val);
1031
1032                 if (Double::Compare(pContentInfoImpl->GetAltitude(), DEFAULT_COORDINATE) != 0)
1033                 {
1034                         val = media_info_set_altitude(pMediaInfo.get(), pContentInfoImpl->GetAltitude());
1035                         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1036                                         "media_info_set_altitude failed[%d].", val);
1037                 }
1038         }
1039
1040         val = media_info_update_to_db(pMediaInfo.get());
1041         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1042                         "media_info_update_to_db failed[%d].", val);
1043
1044         return r;
1045 }
1046
1047 ContentId
1048 _ContentManagerImpl::SaveDataToDatabase(const media_info_h pMediaInfo,
1049                                                                                                 _ContentInfoImpl* pContentInfoImpl) const
1050 {
1051         ClearLastResult();
1052
1053         SysTryReturn(NID_CNT, pContentInfoImpl != null, UuId::GetInvalidUuId(), E_INVALID_ARG,
1054                         "[E_INVALID_ARG] Invalid argument is used. ContentInfo is invalid.");
1055
1056         String mimeType(L"");
1057         String extension = _FileImpl::GetFileExtension(pContentInfoImpl->GetContentPath());
1058         result r = GetLastResult();
1059         if (IsFailed(r))
1060         {
1061                 ClearLastResult();
1062
1063                 SysLog(NID_CNT, "[%s] Failed to perform GetFileExtension operation.", GetErrorMessage(r));
1064
1065                 unique_ptr<char[]> pTempPath(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetContentPath()));
1066                 SysTryReturn(NID_CNT, pTempPath != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1067                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1068
1069                 char tempType[255] = {0, };
1070                 int ret = aul_get_mime_from_file(pTempPath.get(), tempType, sizeof(tempType));
1071                 SysTryReturn(NID_CNT, ret == AUL_R_OK, UuId::GetInvalidUuId(), E_INVALID_ARG,
1072                                 "[E_INVALID_ARG] Failed to perform aul_get_mime_from_file operation.");
1073
1074                 r = mimeType.Append(tempType);
1075                 SysTryReturn(NID_CNT, !IsFailed(r), UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1076                                 "[E_OUT_OF_MEMORY] Failed to perform Append operation.");
1077
1078         }
1079
1080         // If the content format is JPG and it has GPS data, it will be saved in database automatically.
1081         if (extension == L"jpg" || extension == L"jpeg" || extension == L"JPG" || extension == L"JPEG" || mimeType.Contains(L"jpeg"))
1082         {
1083                 SysLog(NID_CNT, "The format of content is jpg.");
1084
1085                 ImageMetadata* pImageMetadata = _ContentManagerUtilImpl::GetImageMetaN(pContentInfoImpl->GetContentPath(), true);
1086                 if (pImageMetadata != null)
1087                 {
1088                         pContentInfoImpl->SetLatitude(pImageMetadata->GetLatitude());
1089                         pContentInfoImpl->SetLongitude(pImageMetadata->GetLongitude());
1090
1091                         delete pImageMetadata;
1092                 }
1093
1094                 ClearLastResult();
1095         }
1096
1097         int val = 0;
1098         unique_ptr<char[]> pStr(null);
1099
1100         if (pContentInfoImpl->GetAuthor() != null)
1101         {
1102                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetAuthor()));
1103                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1104                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1105
1106                 val = media_info_set_author(pMediaInfo, pStr.get());
1107                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1108                                 "media_info_set_author failed[%d].", val);
1109         }
1110         if (pContentInfoImpl->GetCategory() != null)
1111         {
1112                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetCategory()));
1113                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1114                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1115
1116                 val = media_info_set_category(pMediaInfo, pStr.get());
1117                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1118                                 "media_info_set_category failed[%d].", val);
1119         }
1120         if (pContentInfoImpl->GetContentName() != null)
1121         {
1122                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetContentName()));
1123                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1124                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1125
1126                 val = media_info_set_content_name(pMediaInfo, pStr.get());
1127                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1128                                 "media_info_set_content_name failed[%d].", val);
1129         }
1130         if (pContentInfoImpl->GetDescription() != null)
1131         {
1132                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetDescription()));
1133                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1134                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1135
1136                 val = media_info_set_description(pMediaInfo, pStr.get());
1137                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1138                                 "media_info_set_description failed[%d].", val);
1139         }
1140         if (pContentInfoImpl->GetKeyword() != null)
1141         {
1142                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetKeyword()));
1143                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1144                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1145
1146                 val = media_info_set_keyword(pMediaInfo, pStr.get());
1147                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1148                                 "media_info_set_keyword failed[%d].", val);
1149         }
1150         if (pContentInfoImpl->GetLocationTag() != null)
1151         {
1152                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetLocationTag()));
1153                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1154                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1155
1156                 val = media_info_set_location_tag(pMediaInfo, pStr.get());
1157                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1158                                 "media_info_set_location_tag failed[%d].", val);
1159         }
1160         if (pContentInfoImpl->GetProvider() != null)
1161         {
1162                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetProvider()));
1163                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1164                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1165
1166                 val = media_info_set_provider(pMediaInfo, pStr.get());
1167                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1168                                 "media_info_set_provider failed[%d].", val);
1169         }
1170         if (pContentInfoImpl->GetRating() != null)
1171         {
1172                 pStr.reset(_StringConverter::CopyToCharArrayN(pContentInfoImpl->GetRating()));
1173                 SysTryReturn(NID_CNT, pStr != null, UuId::GetInvalidUuId(), E_OUT_OF_MEMORY,
1174                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
1175
1176                 val = media_info_set_age_rating(pMediaInfo, pStr.get());
1177                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1178                                 "media_info_set_age_rating failed[%d].", val);
1179         }
1180         if (Double::Compare(pContentInfoImpl->GetLatitude(), DEFAULT_COORDINATE) != 0 &&
1181                         Double::Compare(pContentInfoImpl->GetLongitude(), DEFAULT_COORDINATE) != 0)
1182         {
1183                 val = media_info_set_latitude(pMediaInfo, pContentInfoImpl->GetLatitude());
1184                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1185                                 "media_info_set_latitude failed[%d].", val);
1186
1187                 val = media_info_set_longitude(pMediaInfo, pContentInfoImpl->GetLongitude());
1188                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1189                                 "media_info_set_longitude failed[%d].", val);
1190
1191                 if (Double::Compare(pContentInfoImpl->GetAltitude(), DEFAULT_COORDINATE) != 0)
1192                 {
1193                         val = media_info_set_altitude(pMediaInfo, pContentInfoImpl->GetAltitude());
1194                         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1195                                         "media_info_set_altitude failed[%d].", val);
1196                 }
1197         }
1198
1199         val = media_info_update_to_db(pMediaInfo);
1200         SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1201                         "media_info_update_to_db failed[%d].", val);
1202
1203         char* pTempMediaId = null;
1204         unique_ptr<char, CharDeleter> pMediaId(null);
1205
1206         val = media_info_get_media_id(pMediaInfo, &pTempMediaId);
1207         if (pTempMediaId != null)
1208         {
1209                 pMediaId.reset(pTempMediaId);
1210         }
1211         else
1212         {
1213                 SysTryReturn(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, UuId::GetInvalidUuId(), ConvertError(val),
1214                                 "media_info_get_media_id failed[%d].", val);
1215         }
1216
1217         String tempContentId(pMediaId.get());
1218         ContentId contentId;
1219
1220         r = UuId::Parse(tempContentId, contentId);
1221         SysTryReturn(NID_CNT, !IsFailed(r), UuId::GetInvalidUuId(), E_INVALID_ARG,
1222                         "[E_INVALID_ARG] UuId::Parse failed.");
1223
1224         return contentId;
1225 }
1226
1227 result
1228 _ContentManagerImpl::MakeContentInfo(const media_info_h pMediaInfo, int systemType, void* pInfoImpl) const
1229 {
1230         SysTryReturnResult(NID_CNT, pMediaInfo != null && pInfoImpl != null, E_INVALID_ARG,
1231                         "The specified parameter is invalid.");
1232
1233         _ContentInfoImpl* pContentInfoImpl = static_cast< _ContentInfoImpl* >(pInfoImpl);
1234
1235         result r = E_SUCCESS;
1236         char* pTempValue = null;
1237         unique_ptr<char, CharDeleter> pStrValue(null);
1238
1239         // contentId
1240         int val = media_info_get_media_id(pMediaInfo, &pTempValue);
1241         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1242                         "media_info_get_media_id failed[%d].", val);
1243
1244         if (pTempValue != null)
1245         {
1246                 pStrValue.reset(pTempValue);
1247
1248                 String strContentId(pStrValue.get());
1249                 ContentId contentId;
1250
1251                 r = UuId::Parse(strContentId, contentId);
1252                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "The content id can not be parsed.");
1253
1254                 pContentInfoImpl->SetContentId(contentId);
1255
1256                 SysLog(NID_CNT, "INFO: contentId[%ls]", strContentId.GetPointer());
1257         }
1258
1259         // contentPath
1260         val = media_info_get_file_path(pMediaInfo, &pTempValue);
1261         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1262                         "media_info_get_file_path failed[%d].", val);
1263
1264         if (pTempValue != null)
1265         {
1266                 pStrValue.reset(pTempValue);
1267
1268                 String strFilePath(pStrValue.get());
1269
1270                 // If the api version is 2.0, the content path has to be changed.
1271                 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1272                 {
1273                         if (strFilePath.StartsWith(Environment::GetMediaPath(), 0))
1274                         {
1275                                 r = strFilePath.Replace(Environment::GetMediaPath(), OSP_MEDIA_PHONE);
1276                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "The content path is not changed.");
1277                         }
1278                         else if (strFilePath.StartsWith(Environment::GetExternalStoragePath(), 0))
1279                         {
1280                                 r = strFilePath.Replace(Environment::GetExternalStoragePath(), OSP_MEDIA_MMC);
1281                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Replace failed.");
1282                         }
1283                         else
1284                         {
1285                                 SysLogException(NID_CNT, E_INVALID_ARG,
1286                                                 "[E_INVALID_ARG] The content path is not supported.");
1287                                 return E_INVALID_ARG;
1288                         }
1289                 }
1290
1291                 pContentInfoImpl->SetContentPath(strFilePath);
1292
1293                 SysSecureLog(NID_CNT, "INFO: contentPath[%ls]", strFilePath.GetPointer());
1294         }
1295
1296         // mimeType
1297         val = media_info_get_mime_type(pMediaInfo, &pTempValue);
1298         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1299                         "media_info_get_mime_type failed[%d].", val);
1300
1301         if (pTempValue != null)
1302         {
1303                 pStrValue.reset(pTempValue);
1304
1305                 pContentInfoImpl->SetMimeType(pStrValue.get());
1306
1307                 SysLog(NID_CNT, "INFO: mimeType[%ls]", (pContentInfoImpl->GetMimeType()).GetPointer());
1308         }
1309
1310         // contentSize
1311         unsigned long long longlongValue = 0;
1312         val = media_info_get_size(pMediaInfo, &longlongValue);
1313         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1314                         "media_info_get_size failed[%d].", val);
1315         pContentInfoImpl->SetContentSize(longlongValue);
1316         SysLog(NID_CNT, "INFO: contentSize[%llu]", longlongValue);
1317
1318         // storageType
1319         media_content_storage_e storageType;
1320         val = media_info_get_storage_type(pMediaInfo, &storageType);
1321         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1322                         "media_info_get_storage_type failed[%d].", val);
1323         pContentInfoImpl->SetStorageType(storageType);
1324         SysLog(NID_CNT, "INFO: storageType[%d]", storageType);
1325
1326         // isDrm
1327         bool tempDrm = false;
1328
1329         val = media_info_is_drm(pMediaInfo, &tempDrm);
1330         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1331                         "media_info_is_drm failed[%d].", val);
1332
1333         pContentInfoImpl->SetDrmProtected(tempDrm);
1334
1335         SysLog(NID_CNT, "INFO: isDrm[%d]", pContentInfoImpl->IsDrmProtected());
1336
1337         // dateTime
1338         time_t addedTime = 0;
1339         val = media_info_get_added_time(pMediaInfo, &addedTime);
1340         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1341                         "media_info_get_added_time failed[%d].", val);
1342
1343         DateTime dt;
1344         r = dt.SetValue(1970, 1, 1);
1345         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform datetime.SetValue operation.");
1346         r = dt.AddSeconds(addedTime);// need to check addedTime is secs/millisec
1347         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform datetime.AddSeconds operation.");
1348
1349         pContentInfoImpl->SetDateTime(dt);
1350         SysLog(NID_CNT, "INFO: dateTime[%ls]", ((pContentInfoImpl->GetDateTime()).ToString()).GetPointer());
1351
1352         // modifiedTime
1353         time_t modifiedTime = 0;
1354         val = media_info_get_modified_time(pMediaInfo, &modifiedTime);
1355         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1356                         "media_info_get_modified_time failed[%d].", val);
1357
1358         r = dt.SetValue(1970, 1, 1);
1359         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform datetime.SetValue operation.");
1360         r = dt.AddSeconds(modifiedTime);
1361         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform datetime.AddSeconds operation.");
1362
1363         pContentInfoImpl->SetModifiedTime(dt);
1364         SysLog(NID_CNT, "INFO: modifiedTime[%ls]", ((pContentInfoImpl->GetModifiedTime()).ToString()).GetPointer());
1365
1366         // thumbnailPath
1367         val = media_info_get_thumbnail_path(pMediaInfo, &pTempValue);
1368         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1369                         "media_info_get_thumbnail_path failed[%d].", val);
1370
1371         if (pTempValue != null)
1372         {
1373                 pStrValue.reset(pTempValue);
1374                 String thumbnailPath(pStrValue.get());
1375
1376                 pContentInfoImpl->SetThumbnailPath(thumbnailPath);
1377
1378                 SysLog(NID_CNT, "INFO: thumbnailPath[%ls]", (pContentInfoImpl->GetThumbnailPath()).GetPointer());
1379         }
1380
1381         // author
1382         val = media_info_get_author(pMediaInfo, &pTempValue);
1383         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1384                         "media_info_get_author failed[%d].", val);
1385
1386         if (pTempValue != null)
1387         {
1388                 pStrValue.reset(pTempValue);
1389
1390                 pContentInfoImpl->SetAuthor(String(pStrValue.get()));
1391
1392                 SysLog(NID_CNT, "INFO: author[%ls]", (pContentInfoImpl->GetAuthor()).GetPointer());
1393         }
1394
1395         // category
1396         val = media_info_get_category(pMediaInfo, &pTempValue);
1397         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1398                         "media_info_get_category failed[%d].", val);
1399
1400         if (pTempValue != null)
1401         {
1402                 pStrValue.reset(pTempValue);
1403
1404                 pContentInfoImpl->SetCategory(String(pStrValue.get()));
1405
1406                 SysLog(NID_CNT, "INFO: category[%ls]", (pContentInfoImpl->GetCategory()).GetPointer());
1407         }
1408
1409         // contentName
1410         val = media_info_get_content_name(pMediaInfo, &pTempValue);
1411         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1412                         "media_info_get_content_name failed[%d].", val);
1413
1414         if (pTempValue != null)
1415         {
1416                 pStrValue.reset(pTempValue);
1417
1418                 pContentInfoImpl->SetContentName(String(pStrValue.get()));
1419
1420                 SysSecureLog(NID_CNT, "INFO: contentName[%ls]", (pContentInfoImpl->GetContentName()).GetPointer());
1421         }
1422
1423         // description
1424         val = media_info_get_description(pMediaInfo, &pTempValue);
1425         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1426                         "media_info_get_description failed[%d].", val);
1427
1428         if (pTempValue != null)
1429         {
1430                 pStrValue.reset(pTempValue);
1431
1432                 pContentInfoImpl->SetDescription(String(pStrValue.get()));
1433
1434                 SysLog(NID_CNT, "INFO: description[%ls]", (pContentInfoImpl->GetDescription()).GetPointer());
1435         }
1436
1437         // keyword
1438         val = media_info_get_keyword(pMediaInfo, &pTempValue);
1439         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1440                         "media_info_get_keyword failed[%d].", val);
1441
1442         if (pTempValue != null)
1443         {
1444                 pStrValue.reset(pTempValue);
1445
1446                 pContentInfoImpl->SetKeyword(String(pStrValue.get()));
1447
1448                 SysLog(NID_CNT, "INFO: keyword[%ls]", (pContentInfoImpl->GetKeyword()).GetPointer());
1449         }
1450
1451         // locationTag
1452         val = media_info_get_location_tag(pMediaInfo, &pTempValue);
1453         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1454                         "media_info_get_location_tag failed[%d].", val);
1455
1456         if (pTempValue != null)
1457         {
1458                 pStrValue.reset(pTempValue);
1459
1460                 pContentInfoImpl->SetLocationTag(String(pStrValue.get()));
1461
1462                 SysSecureLog(NID_CNT, "INFO: locationTag[%ls]", (pContentInfoImpl->GetLocationTag()).GetPointer());
1463         }
1464
1465         // provider
1466         val = media_info_get_provider(pMediaInfo, &pTempValue);
1467         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1468                         "media_info_get_provider failed[%d].", val);
1469
1470         if (pTempValue != null)
1471         {
1472                 pStrValue.reset(pTempValue);
1473
1474                 pContentInfoImpl->SetProvider(String(pStrValue.get()));
1475
1476                 SysLog(NID_CNT, "INFO: provider[%ls]", (pContentInfoImpl->GetProvider()).GetPointer());
1477         }
1478
1479         // rating
1480         val = media_info_get_age_rating(pMediaInfo, &pTempValue);
1481         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1482                         "media_info_get_age_rating failed[%d].", val);
1483
1484         if (pTempValue != null)
1485         {
1486                 pStrValue.reset(pTempValue);
1487
1488                 pContentInfoImpl->SetRating(String(pStrValue.get()));
1489
1490                 SysLog(NID_CNT, "INFO: rating[%ls]", (pContentInfoImpl->GetRating()).GetPointer());
1491         }
1492
1493         // coordinates
1494         Coordinates coordinates;
1495         double doubleValue = 0;
1496         val = media_info_get_latitude(pMediaInfo, &doubleValue);
1497         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1498                         "media_info_get_latitude failed[%d].", val);
1499
1500         if (Double::Compare(doubleValue, DEFAULT_COORDINATE) != 0)
1501         {
1502                 pContentInfoImpl->SetLatitude(doubleValue);
1503                 coordinates.SetLatitude(doubleValue);
1504         }
1505         SysSecureLog(NID_CNT, "INFO: latitude[%f]", pContentInfoImpl->GetLatitude());
1506
1507         val = media_info_get_longitude(pMediaInfo, &doubleValue);
1508         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1509                         "media_info_get_longitude failed[%d].", val);
1510
1511         if (Double::Compare(doubleValue, DEFAULT_COORDINATE) != 0)
1512         {
1513                 pContentInfoImpl->SetLongitude(doubleValue);
1514                 coordinates.SetLongitude(doubleValue);
1515         }
1516         SysSecureLog(NID_CNT, "INFO: longitude[%f]", pContentInfoImpl->GetLongitude());
1517
1518         val = media_info_get_altitude(pMediaInfo, &doubleValue);
1519         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1520                         "media_info_get_altitude failed[%d].", val);
1521
1522         if (Double::Compare(doubleValue, DEFAULT_COORDINATE) != 0)
1523         {
1524                 pContentInfoImpl->SetAltitude(doubleValue);
1525                 coordinates.SetAltitude(doubleValue);
1526         }
1527         SysLog(NID_CNT, "INFO: altitude[%f]", pContentInfoImpl->GetAltitude());
1528
1529         pContentInfoImpl->SetCoordinates(coordinates);
1530
1531         // contentType and metadata
1532         if (systemType == SYSTEM_TYPE_IMAGE)
1533         {
1534                 r = MakeImageContentInfo(pMediaInfo, pInfoImpl);
1535                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform MakeImageContentInfo operation.");
1536         }
1537         else if (systemType == SYSTEM_TYPE_SOUND || systemType == SYSTEM_TYPE_MUSIC)
1538         {
1539                 r = MakeAudioContentInfo(pMediaInfo, pInfoImpl);
1540                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform MakeAudioContentInfo operation.");
1541         }
1542         else if (systemType == MEDIA_CONTENT_TYPE_VIDEO)
1543         {
1544                 r = MakeVideoContentInfo(pMediaInfo, pInfoImpl);
1545                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform MakeVideoContentInfo operation.");
1546         }
1547         else if (systemType == SYSTEM_TYPE_OTHER)
1548         {
1549                 pContentInfoImpl->SetContentType(CONTENT_TYPE_OTHER);
1550                 SysLog(NID_CNT, "META: ContentType[%d]", pContentInfoImpl->GetContentType());
1551         }
1552
1553         return r;
1554 }
1555
1556 result
1557 _ContentManagerImpl::MakeImageContentInfo(const media_info_h pMediaInfo, void* pInfoImpl) const
1558 {
1559         result r = E_SUCCESS;
1560         int intValue = 0;
1561         bool boolValue = false;
1562         image_meta_h pTempMeta = null;
1563         unique_ptr<image_meta_h, _ImageMetaDeleter> pImageMeta(null);
1564
1565         SysTryReturnResult(NID_CNT, pInfoImpl != null, E_OUT_OF_MEMORY, "pInfoImpl is null.");
1566
1567         _ContentInfoImpl* pContentInfoImpl = static_cast< _ContentInfoImpl* >(pInfoImpl);
1568         SysTryReturnResult(NID_CNT, pContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _ContentInfoImpl.");
1569
1570         pContentInfoImpl->SetContentType(CONTENT_TYPE_IMAGE);
1571         SysLog(NID_CNT, "META: ContentType[%d]", pContentInfoImpl->GetContentType());
1572
1573         int val = media_info_get_image(pMediaInfo, &pTempMeta);
1574         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1575                         "media_info_get_image failed[%d].", val);
1576
1577         pImageMeta.reset(&pTempMeta);
1578
1579         _ImageContentInfoImpl* pImageContentInfoImpl = static_cast< _ImageContentInfoImpl* >(pInfoImpl);
1580         SysTryReturnResult(NID_CNT, pImageContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _ImageContentInfoImpl.");
1581
1582         // width
1583         val = image_meta_get_width(*(pImageMeta.get()), &intValue);
1584         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1585                         "image_meta_get_width failed[%d].", val);
1586
1587         pImageContentInfoImpl->SetWidth(intValue);
1588         SysLog(NID_CNT, "META: width[%d]", intValue);
1589
1590         // height
1591         val = image_meta_get_height(*(pImageMeta.get()), &intValue);
1592         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1593                         "image_meta_get_height failed[%d].", val);
1594
1595         pImageContentInfoImpl->SetHeight(intValue);
1596         SysLog(NID_CNT, "META: height[%d]", intValue);
1597
1598         // orientation
1599         media_content_orientation_e orientation;
1600         val = image_meta_get_orientation(*(pImageMeta.get()), &orientation);
1601         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1602                         "image_meta_get_orientation failed[%d].", val);
1603
1604         pImageContentInfoImpl->SetOrientation(static_cast< ImageOrientationType >(orientation));
1605         SysLog(NID_CNT, "META: orientation[%d]", orientation);
1606
1607         char* pTempValue = null;
1608         unique_ptr<char, CharDeleter> pStrValue(null);
1609
1610         // title
1611         val = media_info_get_display_name(pMediaInfo, &pTempValue);
1612         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1613                         "media_info_get_display_name failed[%d].", val);
1614
1615         if (pTempValue != null)
1616         {
1617                 pStrValue.reset(pTempValue);
1618
1619                 int pos = 0;
1620                 String fileName;
1621                 String strTitle(pStrValue.get());
1622
1623                 result r = strTitle.LastIndexOf(L'.', strTitle.GetLength() - 1, pos);
1624                 if (r == E_SUCCESS)
1625                 {
1626                         r = strTitle.SubString(0, pos, fileName);
1627                         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "The title is invalid.");
1628                 }
1629                 else
1630                 {
1631                         // Without extension
1632                         r = E_SUCCESS;
1633                         fileName = strTitle;
1634                 }
1635
1636                 pImageContentInfoImpl->SetTitle(fileName);
1637
1638                 SysLog(NID_CNT, "META: title[%ls]", fileName.GetPointer());
1639         }
1640
1641         // dateTaken
1642         val = image_meta_get_date_taken(*(pImageMeta.get()), &pTempValue);
1643         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1644                         "image_meta_get_date_taken failed[%d].", val);
1645
1646         DateTime dt;
1647
1648         if (pTempValue != null)
1649         {
1650                 pStrValue.reset(pTempValue);
1651                 String dateTaken(pStrValue.get());
1652                 String newDateTaken(L"");
1653
1654                 // detour the unexpected datetaken format
1655                 String tempDelim(L"+-Z");
1656                 String token;
1657
1658                 StringTokenizer tempStrTok(dateTaken, tempDelim);
1659
1660                 r = tempStrTok.GetNextToken(token);
1661                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
1662
1663                 dateTaken = token;
1664
1665                 String delim(L": ");
1666                 String year(L"");
1667
1668                 StringTokenizer strTok(dateTaken, delim);
1669
1670                 r = strTok.SetDelimiters(delim);
1671                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform SetDelimiters operation.");
1672
1673                 r = strTok.GetNextToken(token);
1674                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
1675
1676                 year = token;
1677
1678                 // Append month
1679                 r = strTok.GetNextToken(token);
1680                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
1681
1682                 r = newDateTaken.Append(token);
1683                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1684
1685                 r = newDateTaken.Append(L"/");
1686                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1687
1688                 // Append day
1689                 r = strTok.GetNextToken(token);
1690                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
1691
1692                 r = newDateTaken.Append(token);
1693                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1694
1695                 r = newDateTaken.Append(L"/");
1696                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1697
1698                 // Append year
1699                 r = newDateTaken.Append(year);
1700                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1701
1702                 r = newDateTaken.Append(L" ");
1703                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1704
1705                 String newDelim(L" ");
1706
1707                 r = strTok.SetDelimiters(newDelim);
1708                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform SetDelimiters operation.");
1709
1710                 r = strTok.GetNextToken(token);
1711                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform GetNextToken operation.");
1712
1713                 r = newDateTaken.Append(token);
1714                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_OUT_OF_MEMORY, "Failed to perform Append operation.");
1715
1716                 r = DateTime::Parse(newDateTaken, dt);
1717                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Failed to perform Parse operation for DateTime.");
1718         }
1719         else
1720         {
1721                 dt = DateTime::GetMinValue();
1722         }
1723
1724         pImageContentInfoImpl->SetImageTakenDate(dt);
1725         SysLog(NID_CNT, "META: dateTaken[%ls]", dt.ToString().GetPointer());
1726
1727         // isBurstShot
1728         val = image_meta_is_burst_shot(*(pImageMeta.get()), &boolValue);
1729         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1730                         "image_meta_is_burst_shot failed[%d].", val);
1731
1732         pImageContentInfoImpl->SetBurstShot(boolValue);
1733         SysLog(NID_CNT, "META: isBurstShot[%d]", boolValue);
1734
1735         // burstShotId
1736         val = image_meta_get_burst_id(*(pImageMeta.get()), &pTempValue);
1737         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1738                         "image_meta_get_burst_id failed[%d].", val);
1739
1740         if (pTempValue != null)
1741         {
1742                 pStrValue.reset(pTempValue);
1743                 String burstShotId(pStrValue.get());
1744
1745                 pImageContentInfoImpl->SetBurstShotId(burstShotId);
1746                 SysLog(NID_CNT, "META: burstShotId[%ls]", burstShotId.GetPointer());
1747         }
1748
1749         return E_SUCCESS;
1750 }
1751
1752 result
1753 _ContentManagerImpl::MakeAudioContentInfo(const media_info_h pMediaInfo, void* pInfoImpl) const
1754 {
1755         int intValue = 0;
1756         audio_meta_h pTempMeta = null;
1757         unique_ptr<audio_meta_h, _AudioMetaDeleter> pAudioMeta(null);
1758
1759         SysTryReturnResult(NID_CNT, pInfoImpl != null, E_OUT_OF_MEMORY, "pInfoImpl is null.");
1760
1761         _ContentInfoImpl* pContentInfoImpl = static_cast< _ContentInfoImpl* >(pInfoImpl);
1762         SysTryReturnResult(NID_CNT, pContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _ContentInfoImpl.");
1763
1764         pContentInfoImpl->SetContentType(CONTENT_TYPE_AUDIO);
1765         SysLog(NID_CNT, "META: ContentType[%d]", pContentInfoImpl->GetContentType());
1766
1767         int val = media_info_get_audio(pMediaInfo, &pTempMeta);
1768         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1769                         "media_info_get_audio failed[%d].", val);
1770
1771         pAudioMeta.reset(&pTempMeta);
1772
1773         _AudioContentInfoImpl* pAudioContentInfoImpl = static_cast< _AudioContentInfoImpl* >(pInfoImpl);
1774         SysTryReturnResult(NID_CNT, pAudioContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _AudioContentInfoImpl.");
1775
1776         // bitrate
1777         val = audio_meta_get_bit_rate(*(pAudioMeta.get()), &intValue);
1778         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1779                         "audio_meta_get_bit_rate failed[%d].", val);
1780
1781         pAudioContentInfoImpl->SetBitrate(intValue);
1782         SysLog(NID_CNT, "META: bitrate[%d]", intValue);
1783
1784         char* pTempValue = null;
1785         unique_ptr<char, CharDeleter> pStrValue(null);
1786
1787         // releaseYear
1788         val = audio_meta_get_year(*(pAudioMeta.get()), &pTempValue);
1789         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1790                         "audio_meta_get_year failed[%d].", val);
1791
1792         if (pTempValue != null)
1793         {
1794                 pStrValue.reset(pTempValue);
1795
1796                 String strYear(pStrValue.get());
1797
1798                 if (strYear.CompareTo(L"Unknown") != 0)
1799                 {
1800                         result r = Integer::Parse(strYear, intValue);
1801                         if (IsFailed(r))
1802                         {
1803                                 // It is one of the metadata. If error occurs, skip it for other metadata.
1804                                 intValue = 0;
1805                                 r = E_SUCCESS;
1806                                 SysLog(NID_CNT, "META: releaseYear - invalid data[%ls]", strYear.GetPointer());
1807                         }
1808
1809                         pAudioContentInfoImpl->SetReleaseYear(intValue);
1810                         SysLog(NID_CNT, "META: releaseYear[%d]", intValue);
1811                 }
1812         }
1813
1814         // title
1815         val = audio_meta_get_title(*(pAudioMeta.get()), &pTempValue);
1816         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1817                         "audio_meta_get_title failed[%d].", val);
1818
1819         if (pTempValue != null)
1820         {
1821                 pStrValue.reset(pTempValue);
1822
1823                 pAudioContentInfoImpl->SetTitle(String(pStrValue.get()));
1824
1825                 SysLog(NID_CNT, "META: title[%ls]", (String(pStrValue.get())).GetPointer());
1826         }
1827
1828         // albumName
1829         val = audio_meta_get_album(*(pAudioMeta.get()), &pTempValue);
1830         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1831                         "audio_meta_get_album failed[%d].", val);
1832
1833         if (pTempValue != null)
1834         {
1835                 pStrValue.reset(pTempValue);
1836
1837                 pAudioContentInfoImpl->SetAlbumName(String(pStrValue.get()));
1838
1839                 SysLog(NID_CNT, "META: albumName[%ls]", (String(pStrValue.get())).GetPointer());
1840         }
1841
1842         // artist
1843         val = audio_meta_get_artist(*(pAudioMeta.get()), &pTempValue);
1844         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1845                         "audio_meta_get_artist failed[%d].", val);
1846
1847         if (pTempValue != null)
1848         {
1849                 pStrValue.reset(pTempValue);
1850
1851                 pAudioContentInfoImpl->SetArtist(String(pStrValue.get()));
1852
1853                 SysLog(NID_CNT, "META: artist[%ls]", (String(pStrValue.get())).GetPointer());
1854         }
1855
1856         // composer
1857         val = audio_meta_get_composer(*(pAudioMeta.get()), &pTempValue);
1858         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1859                         "audio_meta_get_composer failed[%d].", val);
1860
1861         if (pTempValue != null)
1862         {
1863                 pStrValue.reset(pTempValue);
1864
1865                 pAudioContentInfoImpl->SetComposer(String(pStrValue.get()));
1866
1867                 SysLog(NID_CNT, "META: composer[%ls]", (String(pStrValue.get())).GetPointer());
1868         }
1869
1870         // genre
1871         val = audio_meta_get_genre(*(pAudioMeta.get()), &pTempValue);
1872         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1873                         "audio_meta_get_genre failed[%d].", val);
1874
1875         if (pTempValue != null)
1876         {
1877                 pStrValue.reset(pTempValue);
1878
1879                 pAudioContentInfoImpl->SetGenre(String(pStrValue.get()));
1880
1881                 SysLog(NID_CNT, "META: genre[%ls]", (String(pStrValue.get())).GetPointer());
1882         }
1883
1884         // copyright
1885         val = audio_meta_get_copyright(*(pAudioMeta.get()), &pTempValue);
1886         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1887                         "audio_meta_get_copyright failed[%d].", val);
1888
1889         if (pTempValue != null)
1890         {
1891                 pStrValue.reset(pTempValue);
1892
1893                 pAudioContentInfoImpl->SetCopyright(String(pStrValue.get()));
1894
1895                 SysLog(NID_CNT, "META: copyright[%ls]", (String(pStrValue.get())).GetPointer());
1896         }
1897
1898         // trackInfo
1899         val = audio_meta_get_track_num(*(pAudioMeta.get()), &pTempValue);
1900         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1901                         "audio_meta_get_track_num failed[%d].", val);
1902
1903         if (pTempValue != null)
1904         {
1905                 pStrValue.reset(pTempValue);
1906
1907                 pAudioContentInfoImpl->SetTrackInfo(String(pStrValue.get()));
1908
1909                 SysLog(NID_CNT, "META: trackInfo[%ls]", (String(pStrValue.get())).GetPointer());
1910         }
1911
1912         // duration
1913         val = audio_meta_get_duration(*(pAudioMeta.get()), &intValue);
1914         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1915                         "audio_meta_get_duration failed[%d].", val);
1916
1917         pAudioContentInfoImpl->SetDuration(intValue);
1918         SysLog(NID_CNT, "META: duration[%d]", intValue);
1919
1920         return E_SUCCESS;
1921 }
1922
1923 result
1924 _ContentManagerImpl::MakeVideoContentInfo(const media_info_h pMediaInfo, void* pInfoImpl) const
1925 {
1926         int intValue = 0;
1927         video_meta_h pTempMeta = null;
1928         unique_ptr<video_meta_h, _VideoMetaDeleter> pVideoMeta(null);
1929
1930         SysTryReturnResult(NID_CNT, pInfoImpl != null, E_OUT_OF_MEMORY, "pInfoImpl is null.");
1931
1932         _ContentInfoImpl* pContentInfoImpl = static_cast< _ContentInfoImpl* >(pInfoImpl);
1933         SysTryReturnResult(NID_CNT, pContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _ContentInfoImpl.");
1934
1935         pContentInfoImpl->SetContentType(CONTENT_TYPE_VIDEO);
1936         SysLog(NID_CNT, "META: ContentType[%d]", pContentInfoImpl->GetContentType());
1937
1938         int val = media_info_get_video(pMediaInfo, &pTempMeta);
1939         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1940                         "media_info_get_video failed[%d].", val);
1941
1942         pVideoMeta.reset(&pTempMeta);
1943
1944         _VideoContentInfoImpl* pVideoContentInfoImpl = static_cast< _VideoContentInfoImpl* >(pInfoImpl);
1945         SysTryReturnResult(NID_CNT, pVideoContentInfoImpl != null, E_OUT_OF_MEMORY, "Failed to perform static cast for _VideoContentInfoImpl.");
1946
1947         // width
1948         val = video_meta_get_width(*(pVideoMeta.get()), &intValue);
1949         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1950                         "video_meta_get_width failed[%d].", val);
1951
1952         pVideoContentInfoImpl->SetWidth(intValue);
1953         SysLog(NID_CNT, "META: width[%d]", intValue);
1954
1955         // height
1956         val = video_meta_get_height(*(pVideoMeta.get()), &intValue);
1957         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1958                         "video_meta_get_height failed[%d].", val);
1959
1960         pVideoContentInfoImpl->SetHeight(intValue);
1961         SysLog(NID_CNT, "META: height[%d]", intValue);
1962
1963         char* pTempValue = null;
1964         unique_ptr<char, CharDeleter> pStrValue(null);
1965
1966         // artist
1967         val = video_meta_get_artist(*(pVideoMeta.get()), &pTempValue);
1968         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1969                         "video_meta_get_artist failed[%d].", val);
1970
1971         if (pTempValue != null)
1972         {
1973                 pStrValue.reset(pTempValue);
1974
1975                 pVideoContentInfoImpl->SetArtist(String(pStrValue.get()));
1976
1977                 SysLog(NID_CNT, "META: artist[%ls]", (String(pStrValue.get())).GetPointer());
1978         }
1979
1980         // genre
1981         val = video_meta_get_genre(*(pVideoMeta.get()), &pTempValue);
1982         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1983                         "video_meta_get_genre failed[%d].", val);
1984
1985         if (pTempValue != null)
1986         {
1987                 pStrValue.reset(pTempValue);
1988
1989                 pVideoContentInfoImpl->SetGenre(String(pStrValue.get()));
1990
1991                 SysLog(NID_CNT, "META: genre[%ls]", (String(pStrValue.get())).GetPointer());
1992         }
1993
1994         // title
1995         val = video_meta_get_title(*(pVideoMeta.get()), &pTempValue);
1996         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
1997                         "video_meta_get_title failed[%d].", val);
1998
1999         if (pTempValue != null)
2000         {
2001                 pStrValue.reset(pTempValue);
2002
2003                 pVideoContentInfoImpl->SetTitle(String(pStrValue.get()));
2004
2005                 SysLog(NID_CNT, "META: title[%ls]", (String(pStrValue.get())).GetPointer());
2006         }
2007
2008         // albumName
2009         val = video_meta_get_album(*(pVideoMeta.get()), &pTempValue);
2010         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
2011                         "video_meta_get_album failed[%d].", val);
2012
2013         if (pTempValue != null)
2014         {
2015                 pStrValue.reset(pTempValue);
2016
2017                 pVideoContentInfoImpl->SetAlbumName(String(pStrValue.get()));
2018
2019                 SysLog(NID_CNT, "META: albumName[%ls]", (String(pStrValue.get())).GetPointer());
2020         }
2021
2022         // duration
2023         val = video_meta_get_duration(*(pVideoMeta.get()), &intValue);
2024         SysTryReturnResult(NID_CNT, val == MEDIA_CONTENT_ERROR_NONE, ConvertError(val),
2025                         "video_meta_get_duration failed[%d].", val);
2026
2027         pVideoContentInfoImpl->SetDuration(intValue);
2028         SysLog(NID_CNT, "META: duration[%d]", intValue);
2029
2030         // Get from metadata extractor (framerate, audio bitrate, video bitrate)
2031         VideoMetadata* pVideoMetadata = ContentManagerUtil::GetVideoMetaN(pVideoContentInfoImpl->GetContentPath());
2032         result r = GetLastResult();
2033         SysTryReturnResult(NID_CNT, pVideoMetadata != null, r, "GetVideoMetadata() failed.");
2034
2035         pVideoContentInfoImpl->SetFramerate(pVideoMetadata->GetFramerate());
2036         pVideoContentInfoImpl->SetAudioBitrate(pVideoMetadata->GetAudioBitrate());
2037         pVideoContentInfoImpl->SetVideoBitrate(pVideoMetadata->GetVideoBitrate());
2038
2039         return E_SUCCESS;
2040 }
2041
2042 bool
2043 _ContentManagerImpl::VerifyHomeFilePathCompatibility(const String& contentPath) const
2044 {
2045         if (!_AppInfo::IsOspCompat())
2046         {
2047                 if (contentPath.StartsWith(OSP_HOME, 0) || contentPath.StartsWith(OSP_HOME_EXT, 0))
2048                 {
2049                         SysLogException(NID_CNT, E_INVALID_ARG,
2050                                         "[E_INVALID_ARG] /Home/ or /HomeExt/ is not supported from Tizen 2.0.");
2051                         return false;
2052                 }
2053                 if (!(contentPath.StartsWith(App::App::GetInstance()->GetAppRootPath(), 0) ||
2054                                 contentPath.StartsWith(Environment::GetExternalStoragePath(), 0)))
2055                 {
2056                         SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] The path is not supported.");
2057                         return false;
2058                 }
2059         }
2060         else
2061         {
2062                 SysTryReturn(NID_CNT, contentPath.StartsWith(OSP_HOME, 0) || contentPath.StartsWith(OSP_HOME_EXT, 0),
2063                                 false, E_INVALID_ARG, "[E_INVALID_ARG] The path should start with /Home or /HomeExt.");
2064         }
2065
2066         return true;
2067 }
2068
2069 bool
2070 _ContentManagerImpl::VerifyMediaFilePathCompatibility(const String& contentPath, bool checkVersion) const
2071 {
2072         ClearLastResult();
2073
2074         result r = E_SUCCESS;
2075
2076         if (!_AppInfo::IsOspCompat())
2077         {
2078                 if (contentPath.StartsWith(OSP_MEDIA_PHONE, 0) || contentPath.StartsWith(OSP_MEDIA_MMC, 0))
2079                 {
2080                         SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] /Media or /Storagecard/Media is not supported.");
2081                         return false;
2082                 }
2083                 if (!(contentPath.StartsWith(Environment::GetMediaPath(), 0) ||
2084                                 contentPath.StartsWith(Environment::GetExternalStoragePath(), 0)))
2085                 {
2086                         SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] This path is not supported.");
2087                         return false;
2088                 }
2089         }
2090         else
2091         {
2092                 String tempPath(contentPath);
2093
2094                 // prior to 2.0
2095                 if (contentPath.StartsWith(OSP_MEDIA_PHONE, 0))
2096                 {
2097                         r = tempPath.Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
2098                         SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Replace failed.");
2099                 }
2100                 else if (contentPath.StartsWith(OSP_MEDIA_MMC, 0))
2101                 {
2102                         r = tempPath.Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
2103                         SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Replace failed.");
2104                 }
2105                 else
2106                 {
2107                         // CreateContent(const ByteBuffer&, ...) and CreateContent (const Sring&, ...) can receive old path like /Media/.
2108                         // but CreateContent(const ContentInfo&) always receive new path like /opt/media/ because ContentInfo class convert the path from /Media/ to /opt/media.
2109                         if (!checkVersion)
2110                         {
2111                                 SysLogException(NID_CNT, E_INVALID_ARG,
2112                                                 "[E_INVALID_ARG] The path should start with /Home, /Media, or /Storagecard/Media.");
2113                                 return false;
2114                         }
2115                 }
2116         }
2117
2118         return true;
2119 }
2120
2121 void
2122 _ContentManagerImpl::SetListener(IContentUpdateEventListener* pListener)
2123 {
2124         __pListener = pListener;
2125 }
2126
2127 IContentUpdateEventListener*
2128 _ContentManagerImpl::GetListener(void) const
2129 {
2130         return __pListener;
2131 }
2132
2133 result
2134 _ContentManagerImpl::ConvertErrorToResult(result res) const
2135 {
2136         result r = E_SUCCESS;
2137
2138         switch (res)
2139         {
2140         case E_IO:
2141                 r = E_SYSTEM;
2142                 break;
2143
2144         default:
2145                 r = res;
2146                 break;
2147         }
2148         return r;
2149 }
2150
2151 }}