[content] Remove Open Service Platform words
[platform/framework/native/content.git] / src / FCntAudioContentInfo.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 /**
18  * @file                FCntAudioContentInfo.cpp
19  * @brief               This is the implementation file for the %AudioContentInfo class.
20  *
21  * This file contains implementation of the %AudioContentInfo class.
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FCntAudioContentInfo.h>
26 #include <FIoFile.h>
27 #include <FIoDirectory.h>
28 #include <FSysEnvironment.h>
29 #include <FIo_FileImpl.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::App;
36 using namespace Tizen::System;
37 using namespace std;
38
39 namespace Tizen { namespace Content
40 {
41
42 AudioContentInfo::AudioContentInfo(void)
43         : ContentInfo()
44         , __pAudioContentData(null)
45         , __pImpl(null)
46 {
47
48 }
49
50 AudioContentInfo::~AudioContentInfo(void)
51 {
52         if (__pAudioContentData != null)
53         {
54                 if (__pAudioContentData->pTitle != null)
55                 {
56                         delete __pAudioContentData->pTitle;
57                         __pAudioContentData->pTitle = null;
58                 }
59                 if (__pAudioContentData->pAlbumName != null)
60                 {
61                         delete __pAudioContentData->pAlbumName;
62                         __pAudioContentData->pAlbumName = null;
63                 }
64                 if (__pAudioContentData->pArtist != null)
65                 {
66                         delete __pAudioContentData->pArtist;
67                         __pAudioContentData->pArtist = null;
68                 }
69                 if (__pAudioContentData->pComposer != null)
70                 {
71                         delete __pAudioContentData->pComposer;
72                         __pAudioContentData->pComposer = null;
73                 }
74                 if (__pAudioContentData->pGenre != null)
75                 {
76                         delete __pAudioContentData->pGenre;
77                         __pAudioContentData->pGenre = null;
78                 }
79                 if (__pAudioContentData->pCopyright != null)
80                 {
81                         delete __pAudioContentData->pCopyright;
82                         __pAudioContentData->pCopyright = null;
83                 }
84                 if (__pAudioContentData->pTrackInfo != null)
85                 {
86                         delete __pAudioContentData->pTrackInfo;
87                         __pAudioContentData->pTrackInfo = null;
88                 }
89
90                 delete __pAudioContentData;
91                 __pAudioContentData = null;
92         }
93 }
94
95 /**
96  * E_SUCCESS
97  * E_FILE_NOT_FOUND
98  * E_INVALID_ARG
99  * E_OUT_OF_MEMORY
100  * - E_IO
101  */
102 result
103 AudioContentInfo::Construct(const String& contentPath, const String& thumbnailPath, bool setGps)
104 {
105         ClearLastResult();
106
107         SysAssertf(__pAudioContentData == null,
108                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
109
110         result r = E_SUCCESS;
111         _ContentData contentData;
112         int contentLength = 0;
113         FileAttributes attribute;
114
115         // checks parameters
116         contentLength = contentPath.GetLength();
117         SysTryReturnResult(NID_CNT, _FileImpl::IsMediaPath(contentPath), E_INVALID_ARG,
118                         "The contentPath should start with /Media or /Storagecard/Media.");
119         SysTryReturnResult(NID_CNT, File::IsFileExist(contentPath), E_FILE_NOT_FOUND,
120                         "The file corresponding to contentPath could not be found.");
121
122         if (!thumbnailPath.IsEmpty())
123         {
124                 SysLog(NID_CNT,
125                                 "The thumbnailPath is not supported but you can get the thumbnail managed by Tizen from ContentInfo::GetThumbnailN().");
126         }
127
128         if (setGps)
129         {
130                 SysLog(NID_CNT, "The setGps is not supported.");
131         }
132
133         // Sets the content path
134         contentData.contentPath = contentPath;
135
136         // Sets the content type
137         contentData.contentType = CONTENT_TYPE_AUDIO;
138
139         // E_INVALID_ARG, E_OUT_OF_MEMORY
140         r = SetContentData(&contentData);
141         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
142
143         _AudioContentData* pAudioContentData = new (nothrow) _AudioContentData();
144         SysTryReturnResult(NID_CNT, pAudioContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
145
146         __pAudioContentData = pAudioContentData;
147
148         return r;
149 }
150
151 /**
152  * E_SUCCESS
153  * E_FILE_NOT_FOUND
154  * E_INVALID_ARG
155  * E_OUT_OF_MEMORY
156  * - E_IO
157  * - E_SYSTEM
158  */
159 result
160 AudioContentInfo::Construct(const String* pContentPath)
161 {
162         ClearLastResult();
163
164         SysAssertf(__pAudioContentData == null,
165                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
166
167         result r = E_SUCCESS;
168         _ContentData contentData;
169
170         if (pContentPath != null)
171         {
172                 if (!_AppInfo::IsOspCompat())
173                 {
174                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0) || pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
175                         {
176                                 SysLogException(NID_CNT, E_INVALID_ARG,
177                                                 "[E_INVALID_ARG] /Media/ and /Storagecard/Media/ are not supported from Tizen 2.0.");
178                                 return E_INVALID_ARG;
179                         }
180                         if (!(pContentPath->StartsWith(Environment::GetMediaPath(), 0)
181                                 || pContentPath->StartsWith(Environment::GetExternalStoragePath(), 0)))
182                         {
183                                 SysLogException(NID_CNT, E_INVALID_ARG,
184                                                 "[E_INVALID_ARG] %ls is not supported.", pContentPath->GetPointer());
185                                 return E_INVALID_ARG;
186                         }
187                 }
188                 else
189                 {
190                         // prior to 2.0
191                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0))
192                         {
193                                 // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
194                                 // it should be converted in 2.0.
195                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
196                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
197                         }
198                         else if (pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
199                         {
200                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
201                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
202                         }
203                         else
204                         {
205                                 SysLogException(NID_CNT, E_INVALID_ARG,
206                                                 "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
207                                 return E_INVALID_ARG;
208                         }
209                 }
210
211                 int length = pContentPath->GetLength();
212                 SysTryReturnResult(NID_CNT, length != 0, E_INVALID_ARG,
213                                 "The length of pContentPath is 0.");
214                 SysTryReturnResult(NID_CNT, File::IsFileExist(*pContentPath), E_FILE_NOT_FOUND,
215                                 "The file corresponding to pContentPath could not be found.");
216
217                 // Sets the content path
218                 contentData.contentPath = *pContentPath;
219
220                 // Sets the content type
221                 contentData.contentType = CONTENT_TYPE_AUDIO;
222
223                 r = SetContentData(&contentData);
224                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
225         }
226         else
227         {
228                 contentData.contentType = CONTENT_TYPE_AUDIO;
229
230                 r = SetContentData(&contentData);
231                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
232         }
233
234         _AudioContentData* pAudioContentData = new (nothrow) _AudioContentData();
235         SysTryReturnResult(NID_CNT, pAudioContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
236
237         __pAudioContentData = pAudioContentData;
238
239         return r;
240 }
241
242 /**
243  * E_SUCCESS
244  * E_INVALID_ARG
245  * E_OUT_OF_MEMORY
246  */
247 result
248 AudioContentInfo::SetAudioContentData(const _AudioContentData* pAudioContentData)
249 {
250         ClearLastResult();
251
252         SysTryReturnResult(NID_CNT, pAudioContentData != null, E_INVALID_ARG, "pAudioContentData is null.");
253
254         if (__pAudioContentData == null)
255         {
256                 __pAudioContentData = new (nothrow) _AudioContentData;
257                 SysTryReturnResult(NID_CNT, __pAudioContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
258         }
259
260         __pAudioContentData->bitrate = pAudioContentData->bitrate;
261         __pAudioContentData->releaseYear = pAudioContentData->releaseYear;
262         __pAudioContentData->duration = pAudioContentData->duration;
263
264         if (pAudioContentData->pTitle != null)
265         {
266                 __pAudioContentData->pTitle = new (nothrow) String(*(pAudioContentData->pTitle));
267                 SysTryReturnResult(NID_CNT, __pAudioContentData->pTitle != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
268         }
269         if (pAudioContentData->pAlbumName != null)
270         {
271                 __pAudioContentData->pAlbumName = new (nothrow) String(*(pAudioContentData->pAlbumName));
272                 SysTryReturnResult(NID_CNT, __pAudioContentData->pAlbumName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
273         }
274         if (pAudioContentData->pArtist != null)
275         {
276                 __pAudioContentData->pArtist = new (nothrow) String(*(pAudioContentData->pArtist));
277                 SysTryReturnResult(NID_CNT, __pAudioContentData->pArtist != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
278         }
279         if (pAudioContentData->pComposer != null)
280         {
281                 __pAudioContentData->pComposer = new (nothrow) String(*(pAudioContentData->pComposer));
282                 SysTryReturnResult(NID_CNT, __pAudioContentData->pComposer != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
283         }
284         if (pAudioContentData->pGenre != null)
285         {
286                 __pAudioContentData->pGenre = new (nothrow) String(*(pAudioContentData->pGenre));
287                 SysTryReturnResult(NID_CNT, __pAudioContentData->pGenre != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
288         }
289         if (pAudioContentData->pCopyright != null)
290         {
291                 __pAudioContentData->pCopyright = new (nothrow) String(*(pAudioContentData->pCopyright));
292                 SysTryReturnResult(NID_CNT, __pAudioContentData->pCopyright != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
293         }
294         if (pAudioContentData->pTrackInfo != null)
295         {
296                 __pAudioContentData->pTrackInfo = new (nothrow) String(*(pAudioContentData->pTrackInfo));
297                 SysTryReturnResult(NID_CNT, __pAudioContentData->pTrackInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
298         }
299
300         return E_SUCCESS;
301 }
302
303 AudioContentInfo::_AudioContentData*
304 AudioContentInfo::GetAudioContentData(void)
305 {
306         if (__pAudioContentData == null)
307         {
308                 __pAudioContentData = new (nothrow) AudioContentInfo::_AudioContentData;
309                 SysTryReturn(NID_CNT, __pAudioContentData != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
310         }
311         return __pAudioContentData;
312 }
313
314 String
315 AudioContentInfo::GetGenre(void) const
316 {
317         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
318
319         if (__pAudioContentData->pGenre == null)
320         {
321                 SysLog(NID_CNT, "GetGenre() failed.");
322                 return String(L"Unknown");
323         }
324
325         return *(__pAudioContentData->pGenre);
326 }
327
328 String
329 AudioContentInfo::GetArtist(void) const
330 {
331         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
332
333         if (__pAudioContentData->pArtist == null)
334         {
335                 SysLog(NID_CNT, "GetArtist() failed.");
336                 return String(L"Unknown");
337         }
338
339         return *(__pAudioContentData->pArtist);
340 }
341
342 String
343 AudioContentInfo::GetComposer(void) const
344 {
345         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
346
347         if (__pAudioContentData->pComposer == null)
348         {
349                 SysLog(NID_CNT, "GetComposer() failed.");
350                 return String(L"Unknown");
351         }
352
353         return *(__pAudioContentData->pComposer);
354 }
355
356 String
357 AudioContentInfo::GetAlbumName(void) const
358 {
359         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
360
361         if (__pAudioContentData->pAlbumName == null)
362         {
363                 SysLog(NID_CNT, "GetAlbumName() failed.");
364                 return String(L"Unknown");
365         }
366
367         return *(__pAudioContentData->pAlbumName);
368 }
369
370 int
371 AudioContentInfo::GetReleaseYear(void) const
372 {
373         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
374
375         return __pAudioContentData->releaseYear;
376 }
377
378 int
379 AudioContentInfo::GetBitrate(void) const
380 {
381         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
382
383         return __pAudioContentData->bitrate;
384 }
385
386 String
387 AudioContentInfo::GetTitle(void) const
388 {
389         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
390
391         if (__pAudioContentData->pTitle == null)
392         {
393                 SysLog(NID_CNT, "GetTitle() failed.");
394                 return String(L"Unknown");
395         }
396
397         return *(__pAudioContentData->pTitle);
398 }
399
400 String
401 AudioContentInfo::GetCopyright(void) const
402 {
403         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
404
405         if (__pAudioContentData->pCopyright == null)
406         {
407                 SysLog(NID_CNT, "GetCopyright() failed.");
408                 return String(L"Unknown");
409         }
410
411         return *(__pAudioContentData->pCopyright);
412 }
413
414 String
415 AudioContentInfo::GetTrackInfo(void) const
416 {
417         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
418
419         if (__pAudioContentData->pTrackInfo == null)
420         {
421                 SysLog(NID_CNT, "GetTrackInfo() failed.");
422                 return String(L"Unknown");
423         }
424
425         return *(__pAudioContentData->pTrackInfo);
426 }
427
428 long
429 AudioContentInfo::GetDuration(void) const
430 {
431         SysAssertf(__pAudioContentData != null, "Not yet constructed. Construct() should be called before use.");
432
433         return __pAudioContentData->duration;
434 }
435
436 }}