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