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