[content] Change sort mode
[platform/framework/native/content.git] / src / FCntContentInfo.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file                FCntContentInfo.cpp
18  * @brief               This is the implementation file for the %ContentInfo class.
19  *
20  * This file contains implementation of the %ContentInfo class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FCntContentInfo.h>
25 #include <FCntAudioMetadata.h>
26 #include <FCntContentManagerUtil.h>
27 #include <FIoFile.h>
28 #include <FGrpBitmap.h>
29 #include <FMediaImage.h>
30 #include <FApp_AppInfo.h>
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Io;
34 using namespace Tizen::Locations;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Media;
37 using namespace Tizen::App;
38 using namespace std;
39
40 namespace Tizen { namespace Content
41 {
42
43 static const int THUMBNAIL_IMAGE_WIDTH = 80;
44 static const int THUMBNAIL_IMAGE_HEIGHT = 60;
45 static const int MAX_CUSTOM_FIELD_COMMON = 45;
46 static const int MAX_CUSTOM_FIELD_DESCRIPTION = 140;
47 static const double DEFAULT_COORDINATE = -200.0;
48
49 ContentInfo::ContentInfo(void)
50         : Object()
51         , __pContentData(null)
52         , __pImpl(null)
53 {
54
55 }
56
57 ContentInfo::~ContentInfo(void)
58 {
59         if (__pContentData != null)
60         {
61                 if (__pContentData->pThumbnailPath != null)
62                 {
63                         delete __pContentData->pThumbnailPath;
64                         __pContentData->pThumbnailPath = null;
65                 }
66                 if (__pContentData->pAuthor != null)
67                 {
68                         delete __pContentData->pAuthor;
69                         __pContentData->pAuthor = null;
70                 }
71                 if (__pContentData->pCategory != null)
72                 {
73                         delete __pContentData->pCategory;
74                         __pContentData->pCategory = null;
75                 }
76                 if (__pContentData->pContentName)
77                 {
78                         delete __pContentData->pContentName;
79                         __pContentData->pContentName = null;
80                 }
81                 if (__pContentData->pDescription != null)
82                 {
83                         delete __pContentData->pDescription;
84                         __pContentData->pDescription = null;
85                 }
86                 if (__pContentData->pKeyword != null)
87                 {
88                         delete __pContentData->pKeyword;
89                         __pContentData->pKeyword = null;
90                 }
91                 if (__pContentData->pLocationTag != null)
92                 {
93                         delete __pContentData->pLocationTag;
94                         __pContentData->pLocationTag = null;
95                 }
96                 if (__pContentData->pProvider != null)
97                 {
98                         delete __pContentData->pProvider;
99                         __pContentData->pProvider = null;
100                 }
101                 if (__pContentData->pRating != null)
102                 {
103                         delete __pContentData->pRating;
104                         __pContentData->pRating = null;
105                 }
106
107                 delete __pContentData;
108                 __pContentData = null;
109         }
110 }
111
112 result
113 ContentInfo::SetContentData(const _ContentData* pContentData)
114 {
115         ClearLastResult();
116
117         SysTryReturnResult(NID_CNT, pContentData != null, E_INVALID_ARG, "pContentData is null.");
118
119         if (__pContentData == null)
120         {
121                 __pContentData = new (nothrow) _ContentData();
122                 SysTryReturnResult(NID_CNT, __pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
123         }
124
125         __pContentData->contentId = pContentData->contentId;
126         __pContentData->storageType = pContentData->storageType;
127         __pContentData->contentType = pContentData->contentType;
128         __pContentData->contentSize = pContentData->contentSize;
129         __pContentData->contentPath = pContentData->contentPath;
130         __pContentData->dateTime = pContentData->dateTime;
131         __pContentData->latitude = pContentData->latitude;
132         __pContentData->longitude = pContentData->longitude;
133         __pContentData->altitude = pContentData->altitude;
134         __pContentData->isDrm = pContentData->isDrm;
135
136         if (pContentData->pThumbnailPath != null)
137         {
138                 __pContentData->pThumbnailPath = new (nothrow) String(*(pContentData->pThumbnailPath));
139                 SysTryReturnResult(NID_CNT, __pContentData->pThumbnailPath != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
140         }
141         if (pContentData->pAuthor != null)
142         {
143                 __pContentData->pAuthor = new (nothrow) String(*(pContentData->pAuthor));
144                 SysTryReturnResult(NID_CNT, __pContentData->pAuthor != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
145         }
146         if (pContentData->pCategory != null)
147         {
148                 __pContentData->pCategory = new (nothrow) String(*(pContentData->pCategory));
149                 SysTryReturnResult(NID_CNT, __pContentData->pCategory != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
150         }
151         if (pContentData->pContentName != null)
152         {
153                 __pContentData->pContentName = new (nothrow) String(*(pContentData->pContentName));
154                 SysTryReturnResult(NID_CNT, __pContentData->pContentName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
155         }
156         if (pContentData->pDescription != null)
157         {
158                 __pContentData->pDescription = new (nothrow) String(*(pContentData->pDescription));
159                 SysTryReturnResult(NID_CNT, __pContentData->pDescription != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
160         }
161         if (pContentData->pKeyword != null)
162         {
163                 __pContentData->pKeyword = new (nothrow) String(*(pContentData->pKeyword));
164                 SysTryReturnResult(NID_CNT, __pContentData->pKeyword != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
165         }
166         if (pContentData->pLocationTag != null)
167         {
168                 __pContentData->pLocationTag = new (nothrow) String(*(pContentData->pLocationTag));
169                 SysTryReturnResult(NID_CNT, __pContentData->pLocationTag != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
170         }
171         if (pContentData->pProvider != null)
172         {
173                 __pContentData->pProvider = new (nothrow) String(*(pContentData->pProvider));
174                 SysTryReturnResult(NID_CNT, __pContentData->pProvider != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
175         }
176         if (pContentData->pRating != null)
177         {
178                 __pContentData->pRating = new (nothrow) String(*(pContentData->pRating));
179                 SysTryReturnResult(NID_CNT, __pContentData->pRating != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
180         }
181
182         return E_SUCCESS;
183 }
184
185 ContentInfo::_ContentData*
186 ContentInfo::GetContentData(void)
187 {
188         if (__pContentData == null)
189         {
190                 __pContentData = new (nothrow) ContentInfo::_ContentData;
191                 SysTryReturn(NID_CNT, __pContentData != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
192         }
193         return __pContentData;
194 }
195
196 ContentId
197 ContentInfo::GetContentId(void) const
198 {
199         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
200
201         if (__pContentData->contentId == UuId::GetInvalidUuId())
202         {
203                 SysLog(NID_CNT, "GetContentId() failed.");
204                 return UuId::GetInvalidUuId();
205         }
206         return __pContentData->contentId;
207 }
208
209 ContentType
210 ContentInfo::GetContentType(void) const
211 {
212         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
213
214         return __pContentData->contentType;
215 }
216
217 DateTime
218 ContentInfo::GetDateTime(void) const
219 {
220         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
221
222         return __pContentData->dateTime;
223 }
224
225 String
226 ContentInfo::GetMimeType(void) const
227 {
228         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
229
230         return __pContentData->mimeType;
231 }
232
233 unsigned long
234 ContentInfo::GetContentSize(void) const
235 {
236         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
237
238         return __pContentData->contentSize;
239 }
240
241 String
242 ContentInfo::GetContentName(void) const
243 {
244         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
245
246         if (__pContentData->pContentName == null)
247         {
248                 SysLog(NID_CNT, "GetContentName() failed.");
249                 return String();
250         }
251         return *(__pContentData->pContentName);
252 }
253
254 String
255 ContentInfo::GetContentPath(void) const
256 {
257         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
258
259         return __pContentData->contentPath;
260 }
261
262 const Coordinates&
263 ContentInfo::GetCoordinates(void) const
264 {
265         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
266
267         result r = E_SUCCESS;
268
269         if (Double::Compare(__pContentData->latitude, DEFAULT_COORDINATE) == 0 ||
270                 Double::Compare(__pContentData->longitude, DEFAULT_COORDINATE) == 0)
271         {
272                 SysLog(NID_CNT, "Invalid latitude or longitude");
273                 return coordinates;
274         }
275
276         if (Double::Compare(__pContentData->altitude, DEFAULT_COORDINATE) == 0)
277         {
278                 r = coordinates.SetLatitude(__pContentData->latitude);
279                 if (IsFailed(r))
280                 {
281                         SysLog(NID_CNT, "GetCoordinates() failed.");
282                         return coordinates;
283                 }
284
285                 r = coordinates.SetLongitude(__pContentData->longitude);
286                 if (IsFailed(r))
287                 {
288                         SysLog(NID_CNT, "GetCoordinates() failed.");
289                         return coordinates;
290                 }
291         }
292         else
293         {
294                 r = coordinates.Set(__pContentData->latitude, __pContentData->longitude, __pContentData->altitude);
295                 if (IsFailed(r))
296                 {
297                         SysLog(NID_CNT, "GetCoordinates() failed.");
298                         return coordinates;
299                 }
300         }
301
302         return coordinates;
303 }
304
305 result
306 ContentInfo::SetCoordinates(const Coordinates& coordinates)
307 {
308         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
309
310         ClearLastResult();
311
312         double latitude = coordinates.GetLatitude();
313         double longitude = coordinates.GetLongitude();
314         double altitude = coordinates.GetAltitude();
315
316         if (!Double::IsNaN(latitude))
317         {
318                 __pContentData->latitude = latitude;
319         }
320         if (!Double::IsNaN(longitude))
321         {
322                 __pContentData->longitude = longitude;
323         }
324         if (!Double::IsNaN(altitude))
325         {
326                 __pContentData->altitude = altitude;
327         }
328
329         return E_SUCCESS;
330 }
331
332 result
333 ContentInfo::SetLocationTag(const String& locationTag)
334 {
335         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
336
337         ClearLastResult();
338
339         SysTryReturnResult(NID_CNT, locationTag.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
340                         "The max input length is 45 characters.");
341
342         if (__pContentData->pLocationTag != null)
343         {
344                 // Deletes previous data
345                 delete __pContentData->pLocationTag;
346                 __pContentData->pLocationTag = null;
347         }
348
349         __pContentData->pLocationTag = new (nothrow) String(locationTag);
350         SysTryReturnResult(NID_CNT, __pContentData->pLocationTag != null, E_OUT_OF_MEMORY,
351                         "The memory is insufficient.");
352
353         return E_SUCCESS;
354 }
355
356 String
357 ContentInfo::GetLocationTag(void) const
358 {
359         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
360
361         if (__pContentData->pLocationTag == null)
362         {
363                 SysLog(NID_CNT, "GetLocationTag() failed.");
364                 return String();
365         }
366         return *(__pContentData->pLocationTag);
367 }
368
369 result
370 ContentInfo::SetRating(const String& rating)
371 {
372         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
373
374         ClearLastResult();
375
376         SysTryReturnResult(NID_CNT, rating.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
377                         "The max input length is 45 characters.");
378
379         if (__pContentData->pRating != null)
380         {
381                 // Deletes previous data
382                 delete __pContentData->pRating;
383                 __pContentData->pRating = null;
384         }
385
386         __pContentData->pRating = new (nothrow) String(rating);
387         SysTryReturnResult(NID_CNT, __pContentData->pRating != null, E_OUT_OF_MEMORY,
388                         "The memory is insufficient.");
389
390         return E_SUCCESS;
391 }
392
393 String
394 ContentInfo::GetRating(void) const
395 {
396         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
397
398         if (__pContentData->pRating == null)
399         {
400                 SysLog(NID_CNT, "GetRating() failed.");
401                 return String();
402         }
403         return *(__pContentData->pRating);
404 }
405
406 result
407 ContentInfo::SetCategory(const String& category)
408 {
409         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
410
411         ClearLastResult();
412
413         SysTryReturnResult(NID_CNT, category.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
414                         "The max input length is 45 characters.");
415
416         if (__pContentData->pCategory != null)
417         {
418                 // Deletes previous data
419                 delete __pContentData->pCategory;
420                 __pContentData->pCategory = null;
421         }
422
423         __pContentData->pCategory = new (nothrow) String(category);
424         SysTryReturnResult(NID_CNT, __pContentData->pCategory != null, E_OUT_OF_MEMORY,
425                         "The memory is insufficient.");
426
427         return E_SUCCESS;
428 }
429
430 String
431 ContentInfo::GetCategory(void) const
432 {
433         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
434
435         if (__pContentData->pCategory == null)
436         {
437                 SysLog(NID_CNT, "GetCategory() failed.");
438                 return String();
439         }
440         return *(__pContentData->pCategory);
441 }
442
443 result
444 ContentInfo::SetDescription(const String& description)
445 {
446         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
447
448         ClearLastResult();
449
450         SysTryReturnResult(NID_CNT, description.GetLength() <= MAX_CUSTOM_FIELD_DESCRIPTION, E_INVALID_ARG,
451                         "The max input length is 140 characters.");
452
453         if (__pContentData->pDescription != null)
454         {
455                 // Deletes previous data
456                 delete __pContentData->pDescription;
457                 __pContentData->pDescription = null;
458         }
459
460         __pContentData->pDescription = new (nothrow) String(description);
461         SysTryReturnResult(NID_CNT, __pContentData->pDescription != null, E_OUT_OF_MEMORY,
462                         "The memory is insufficient.");
463
464         return E_SUCCESS;
465 }
466
467 String
468 ContentInfo::GetDescription(void) const
469 {
470         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
471
472         if (__pContentData->pDescription == null)
473         {
474                 SysLog(NID_CNT, "GetDescription() failed.");
475                 return String();
476         }
477         return *(__pContentData->pDescription);
478 }
479
480 Bitmap*
481 ContentInfo::GetThumbnailN(void) const
482 {
483         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
484
485         ClearLastResult();
486
487         result r = E_SUCCESS;
488         Image image;
489         Bitmap* pBitmap = null;
490
491         SysTryReturn(NID_CNT, __pContentData->pThumbnailPath != null, null, E_DATA_NOT_FOUND,
492                         "[E_DATA_NOT_FOUND] GetThumbnailN() failed.");
493
494         if (__pContentData->contentType == CONTENT_TYPE_IMAGE || __pContentData->contentType == CONTENT_TYPE_AUDIO
495                 || __pContentData->contentType == CONTENT_TYPE_VIDEO)
496         {
497                 r = image.Construct();
498                 SysTryReturn(NID_CNT, !IsFailed(r), null, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN() failed.");
499
500                 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
501                 {
502                         pBitmap = image.DecodeN(*(__pContentData->pThumbnailPath), BITMAP_PIXEL_FORMAT_ARGB8888, THUMBNAIL_IMAGE_WIDTH, THUMBNAIL_IMAGE_HEIGHT);
503                 }
504                 else
505                 {
506                         pBitmap = image.DecodeN(*(__pContentData->pThumbnailPath), BITMAP_PIXEL_FORMAT_ARGB8888);
507                 }
508
509                 if (pBitmap == null)
510                 {
511                         r = GetLastResult();
512
513                         if (r == E_OUT_OF_MEMORY)
514                         {
515                                 SysLogException(NID_CNT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetThumbnailN() failed.");
516                                 return null;
517                         }
518                         else
519                         {
520                                 SysLogException(NID_CNT, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN() failed.");
521                                 return null;
522                         }
523                 }
524         }
525         else
526         {
527                 SysLogException(NID_CNT, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN() failed.");
528                 return null;
529         }
530
531         return pBitmap;
532 }
533
534 bool
535 ContentInfo::IsDrmProtected(void) const
536 {
537         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
538
539         return __pContentData->isDrm;
540 }
541
542 String
543 ContentInfo::GetKeyword(void) const
544 {
545         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
546
547         if (__pContentData->pKeyword == null)
548         {
549                 SysLog(NID_CNT, "GetKeyword() failed.");
550                 return String();
551         }
552         return *(__pContentData->pKeyword);
553 }
554
555 result
556 ContentInfo::SetContentName(const String& contentName)
557 {
558         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
559
560         ClearLastResult();
561
562         SysTryReturnResult(NID_CNT, contentName.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
563                         "The max input length is 45 characters.");
564
565         if (__pContentData->pContentName != null)
566         {
567                 // Deletes previous data
568                 delete __pContentData->pContentName;
569                 __pContentData->pContentName = null;
570         }
571
572         __pContentData->pContentName = new (nothrow) String(contentName);
573         SysTryReturnResult(NID_CNT, __pContentData->pContentName != null, E_OUT_OF_MEMORY,
574                         "The memory is insufficient.");
575
576         return E_SUCCESS;
577 }
578
579 result
580 ContentInfo::SetKeyword(const String& keyword)
581 {
582         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
583
584         ClearLastResult();
585
586         SysTryReturnResult(NID_CNT, keyword.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
587                         "The max input length is 45 characters.");
588
589         if (__pContentData->pKeyword != null)
590         {
591                 // Deletes previous data
592                 delete __pContentData->pKeyword;
593                 __pContentData->pKeyword = null;
594         }
595
596         __pContentData->pKeyword = new (nothrow) String(keyword);
597         SysTryReturnResult(NID_CNT, __pContentData->pKeyword != null, E_OUT_OF_MEMORY,
598                         "SetKeyword() failed.");
599
600         return E_SUCCESS;
601 }
602
603 result
604 ContentInfo::SetAuthor(const String& author)
605 {
606         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
607
608         ClearLastResult();
609
610         SysTryReturnResult(NID_CNT, author.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
611                         "The max input length is 45 characters.");
612
613         if (__pContentData->pAuthor != null)
614         {
615                 // Deletes previous data
616                 delete __pContentData->pAuthor;
617                 __pContentData->pAuthor = null;
618         }
619
620         __pContentData->pAuthor = new (nothrow) String(author);
621         SysTryReturnResult(NID_CNT, __pContentData->pAuthor != null, E_OUT_OF_MEMORY,
622                         "SetAuthor() failed.");
623
624         return E_SUCCESS;
625 }
626
627 String
628 ContentInfo::GetAuthor(void) const
629 {
630         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
631
632         if (__pContentData->pAuthor == null)
633         {
634                 SysLog(NID_CNT, "GetAuthor() failed.");
635                 return String();
636         }
637         return *(__pContentData->pAuthor);
638 }
639
640 result
641 ContentInfo::SetProvider(const String& provider)
642 {
643         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
644
645         ClearLastResult();
646
647         SysTryReturnResult(NID_CNT, provider.GetLength() <= MAX_CUSTOM_FIELD_COMMON, E_INVALID_ARG,
648                         "The max input length is 45 characters.");
649
650         if (__pContentData->pProvider != null)
651         {
652                 // Deletes previous data
653                 delete __pContentData->pProvider;
654                 __pContentData->pProvider = null;
655         }
656
657         __pContentData->pProvider = new (nothrow) String(provider);
658         SysTryReturnResult(NID_CNT, __pContentData->pProvider != null, E_OUT_OF_MEMORY,
659                         "SetProvider() failed.");
660
661         return E_SUCCESS;
662 }
663
664 String
665 ContentInfo::GetProvider(void) const
666 {
667         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
668
669         if (__pContentData->pProvider == null)
670         {
671                 SysLog(NID_CNT, "GetProvider() failed.");
672                 return String();
673         }
674         return *(__pContentData->pProvider);
675 }
676
677 String
678 ContentInfo::GetMediaFormat(void) const
679 {
680         SysAssertf(__pContentData != null, "Not yet constructed. Construct() should be called before use.");
681
682         return File::GetFileExtension(__pContentData->contentPath);
683 }
684
685 }}