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