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