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