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