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