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