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