Fix : The exception handling according to the situation
[platform/framework/native/content.git] / src / FCnt_VideoContentInfoImpl.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                FCntVideoContentInfo.cpp
18  * @brief               This is the implementation file for the %VideoContentInfo class.
19  *
20  * This file contains implementation of the %VideoContentInfo class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FCntContentManagerUtil.h>
26 #include <FCntVideoMetadata.h>
27 #include <FIoFile.h>
28 #include <FIoDirectory.h>
29 #include <FSysEnvironment.h>
30 #include <FIo_FileImpl.h>
31 #include <FApp_AppInfo.h>
32 #include "FCnt_VideoContentInfoImpl.h"
33
34 using namespace std;
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Io;
38 using namespace Tizen::Locations;
39 using namespace Tizen::System;
40
41 namespace Tizen { namespace Content
42 {
43
44 _VideoContentInfoImpl::_VideoContentInfoImpl(void)
45         : __width(0)
46         , __height(0)
47         , __framerate(0)
48         , __audioBitrate(0)
49         , __videoBitrate(0)
50         , __duration(0)
51         , __artist(L"")
52         , __genre(L"")
53         , __title(L"")
54         , __albumName(L"")
55 {
56
57 }
58
59 _VideoContentInfoImpl::~_VideoContentInfoImpl(void)
60 {
61
62 }
63
64 _VideoContentInfoImpl*
65 _VideoContentInfoImpl::GetInstance(VideoContentInfo& videoContentInfo)
66 {
67         return videoContentInfo.__pVideoContentInfoImpl;
68 }
69
70 const _VideoContentInfoImpl*
71 _VideoContentInfoImpl::GetInstance(const VideoContentInfo& videoContentInfo)
72 {
73         return videoContentInfo.__pVideoContentInfoImpl;
74 }
75
76 result
77 _VideoContentInfoImpl::Construct(const String& contentPath, const String& thumbnailPath, bool setGps)
78 {
79         result r = E_SUCCESS;
80         String tempPath(contentPath);
81
82         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
83         {
84                 if (contentPath.StartsWith(OSP_MEDIA_PHONE, 0))
85                 {
86                         // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
87                         // it should be converted in 2.0.
88                         r = tempPath.Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
89                         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
90                 }
91                 else if (contentPath.StartsWith(OSP_MEDIA_MMC, 0))
92                 {
93                         r = tempPath.Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
94                         SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
95                 }
96                 else
97                 {
98                         SysLogException(NID_CNT, E_INVALID_ARG,
99                                         "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
100                         return E_INVALID_ARG;
101                 }
102         }
103
104         // checks parameters
105         SysTryReturnResult(NID_CNT, _FileImpl::IsMediaPath(contentPath), E_INVALID_ARG,
106                         "The contentPath should start with /Media or /Storagecard/Media.");
107         SysTryReturnResult(NID_CNT, File::IsFileExist(contentPath), E_FILE_NOT_FOUND,
108                         "The file corresponding to contentPath could not be found.");
109
110         if (!thumbnailPath.IsEmpty())
111         {
112                 SysLog(NID_CNT,
113                                 "The thumbnailPath is not supported but you can get the thumbnail managed by Tizen from ContentInfo::GetThumbnailN().");
114         }
115
116         if (setGps)
117         {
118                 SysLog(NID_CNT, "The setGps is not supported.");
119         }
120
121         SetContentPath(tempPath);
122         SetContentType(CONTENT_TYPE_VIDEO);
123
124         return r;
125 }
126
127 result
128 _VideoContentInfoImpl::Construct(const String* pContentPath)
129 {
130         result r = E_SUCCESS;
131
132         if (pContentPath != null)
133         {
134                 String contentPath(*pContentPath);
135
136                 if (!_AppInfo::IsOspCompat())
137                 {
138                         if (!(contentPath.StartsWith(Environment::GetMediaPath(), 0)
139                                 || contentPath.StartsWith(Environment::GetExternalStoragePath(), 0)))
140                         {
141                                 SysLogException(NID_CNT, E_INVALID_ARG, "[E_INVALID_ARG] The path is not supported.");
142                                 return E_INVALID_ARG;
143                         }
144                 }
145                 else
146                 {
147                         // prior to 2.0
148                         if (contentPath.StartsWith(OSP_MEDIA_PHONE, 0))
149                         {
150                                 // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
151                                 // it should be converted in 2.0.
152                                 r = contentPath.Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
153                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
154                         }
155                         else if (contentPath.StartsWith(OSP_MEDIA_MMC, 0))
156                         {
157                                 r = contentPath.Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
158                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
159                         }
160                         else
161                         {
162                                 SysLogException(NID_CNT, E_INVALID_ARG,
163                                                 "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
164                                 return E_INVALID_ARG;
165                         }
166                 }
167
168                 int length = contentPath.GetLength();
169                 SysTryReturnResult(NID_CNT, length != 0, E_INVALID_ARG,
170                                 "The length of pContentPath is 0.");
171                 SysTryReturnResult(NID_CNT, File::IsFileExist(*pContentPath), E_FILE_NOT_FOUND,
172                                 "The file corresponding to pContentPath could not be found.");
173
174                 SetContentPath(contentPath);
175                 SetContentType(CONTENT_TYPE_VIDEO);
176         }
177         else
178         {
179                 SetContentType(CONTENT_TYPE_VIDEO);
180         }
181
182         return r;
183 }
184
185 String
186 _VideoContentInfoImpl::GetGenre(void) const
187 {
188         if (__genre.IsEmpty())
189         {
190                 SysLog(NID_CNT, "Genre is empty.");
191                 return String(L"Unknown");
192         }
193
194         return __genre;
195 }
196
197 String
198 _VideoContentInfoImpl::GetArtist(void) const
199 {
200         if (__artist.IsEmpty())
201         {
202                 SysLog(NID_CNT, "Artist is empty.");
203                 return String(L"Unknown");
204         }
205
206         return __artist;
207 }
208
209 int
210 _VideoContentInfoImpl::GetBitrate(void) const
211 {
212         return __audioBitrate;
213 }
214
215 int
216 _VideoContentInfoImpl::GetAudioBitrate(void) const
217 {
218         if (__audioBitrate == 0)
219         {
220                 result r = GetVideoMetadata();
221                 SysTryLogReturn(NID_CNT, !IsFailed(r), __audioBitrate, "GetVideoMetadata failed.");
222         }
223
224         return __audioBitrate;
225 }
226
227 int
228 _VideoContentInfoImpl::GetVideoBitrate(void) const
229 {
230         if (__videoBitrate == 0)
231         {
232                 result r = GetVideoMetadata();
233                 SysTryLogReturn(NID_CNT, !IsFailed(r), __videoBitrate, "GetVideoMetadata failed.");
234         }
235
236         return __videoBitrate;
237 }
238
239 int
240 _VideoContentInfoImpl::GetFramerate(void) const
241 {
242         if (__framerate == 0)
243         {
244                 result r = GetVideoMetadata();
245                 SysTryLogReturn(NID_CNT, !IsFailed(r), __framerate, "GetVideoMetadata failed.");
246         }
247
248         return __framerate;
249 }
250
251 int
252 _VideoContentInfoImpl::GetWidth(void) const
253 {
254         return __width;
255 }
256
257 int
258 _VideoContentInfoImpl::GetHeight(void) const
259 {
260         return __height;
261 }
262
263 String
264 _VideoContentInfoImpl::GetTitle(void) const
265 {
266         if (__title.IsEmpty())
267         {
268                 SysLog(NID_CNT, "Title is empty.");
269                 return String(L"Unknown");
270         }
271
272         return __title;
273 }
274
275 String
276 _VideoContentInfoImpl::GetAlbumName(void) const
277 {
278         if (__albumName.IsEmpty())
279         {
280                 SysLog(NID_CNT, "AlbumName is empty.");
281                 return String(L"Unknown");
282         }
283
284         return __albumName;
285 }
286
287 long
288 _VideoContentInfoImpl::GetDuration(void) const
289 {
290         return __duration;
291 }
292
293 void
294 _VideoContentInfoImpl::SetGenre(const Tizen::Base::String& genre)
295 {
296         __genre = genre;
297 }
298
299 void
300 _VideoContentInfoImpl::SetArtist(const Tizen::Base::String& artist)
301 {
302         __artist = artist;
303 }
304
305 void
306 _VideoContentInfoImpl::SetBitrate(int bitrate)
307 {
308         __audioBitrate = bitrate;
309 }
310
311 void
312 _VideoContentInfoImpl::SetAudioBitrate(int audioBitrate)
313 {
314         __audioBitrate = audioBitrate;
315 }
316
317 void
318 _VideoContentInfoImpl::SetVideoBitrate(int videoBitrate)
319 {
320         __videoBitrate = videoBitrate;
321 }
322
323 void
324 _VideoContentInfoImpl::SetFramerate(int framerate)
325 {
326         __framerate = framerate;
327 }
328
329 void
330 _VideoContentInfoImpl::SetWidth(int width)
331 {
332         __width = width;
333 }
334
335 void
336 _VideoContentInfoImpl::SetHeight(int height)
337 {
338         __height = height;
339 }
340
341 void
342 _VideoContentInfoImpl::SetTitle(const Tizen::Base::String& title)
343 {
344         __title = title;
345 }
346
347 void
348 _VideoContentInfoImpl::SetAlbumName(const Tizen::Base::String& albumName)
349 {
350         __albumName = albumName;
351 }
352
353 void
354 _VideoContentInfoImpl::SetDuration(long duration)
355 {
356         __duration = duration;
357 }
358
359 result
360 _VideoContentInfoImpl::GetVideoMetadata(void) const
361 {
362         unique_ptr< VideoMetadata > pVideoMetadata(ContentManagerUtil::GetVideoMetaN(GetContentPath()));
363         result r = GetLastResult();
364         SysTryReturn(NID_CNT, pVideoMetadata != null, r, r, "[%s] Failed to perform GetVideoMetadata operation.", GetErrorMessage(r));
365
366         // framerate
367         __framerate = pVideoMetadata->GetFramerate();
368
369         // audio bitrate
370         __audioBitrate = pVideoMetadata->GetAudioBitrate();
371
372         // video bitrate
373         __videoBitrate = pVideoMetadata->GetVideoBitrate();
374
375         return E_SUCCESS;
376 }
377
378 }}