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