fix exception handling
[profile/tv/apps/native/videoplayer.git] / src / mediadata.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <Elementary.h>
18 #include <Eina.h>
19 #include <media_content.h>
20 #include <AppCommon.h>
21 #include <dbg.h>
22 #include "define.h"
23 #include "mediadata.h"
24 #include "videodata.h"
25
26 struct SMediaData {
27         char *name;
28         char *path;
29         time_t time;
30         CVideoData *video;
31 };
32
33 bool CMediaData::sm_GetMediaInfo(media_info_h media_h, void *dt)
34 {
35         ASSERT(dt);
36
37         SMediaData *md;
38
39         md = (SMediaData *)dt;
40
41         media_info_get_display_name(media_h, &md->name);
42         media_info_get_file_path(media_h, &md->path);
43         media_info_get_modified_time(media_h, &md->time);
44
45         md->video = new CVideoData;
46         if (md->video)
47                 md->video->Update(media_h);
48
49         return true;
50 }
51
52 bool CMediaData::Create(const char *path)
53 {
54         ASSERT(!m);
55
56         filter_h filter;
57         char buf[1024];
58         int r;
59
60         r = media_filter_create(&filter);
61         if (r != MEDIA_CONTENT_ERROR_NONE) {
62                 _ERR("Media Filter Creation Failed");
63                 return false;
64         }
65
66         snprintf(buf, sizeof(buf), "MEDIA_PATH = \"%s\"", path);
67
68         r = media_filter_set_condition(filter, buf,
69                         MEDIA_CONTENT_COLLATE_DEFAULT);
70         if (r != MEDIA_CONTENT_ERROR_NONE) {
71                 _ERR("Fail to set filter condition");
72                 media_filter_destroy(filter);
73                 return false;
74         }
75
76         m = new SMediaData;
77         if (!m) {
78                 _ERR("allocation failed");
79                 media_filter_destroy(filter);
80                 return false;
81         }
82
83         m->name = NULL;
84         m->path = NULL;
85         m->video = NULL;
86
87         media_content_connect();
88
89         r = media_info_foreach_media_from_db(filter, sm_GetMediaInfo, m);
90         if (r != MEDIA_CONTENT_ERROR_NONE) {
91                 _ERR("MEDIA CONTENT ERROR: %d", r);
92                 media_filter_destroy(filter);
93                 media_content_disconnect();
94
95                 delete m;
96                 m = NULL;
97
98                 return false;
99         }
100
101         media_filter_destroy(filter);
102
103         return true;
104 }
105
106 void CMediaData::Destroy(void)
107 {
108         ASSERT(m);
109
110         media_content_disconnect();
111
112         free(m->name);
113         free(m->path);
114
115         delete m->video;
116
117         delete m;
118         m = NULL;
119 }
120
121 const char *CMediaData::GetName(void)
122 {
123         ASSERT(m);
124
125         return m->name;
126 }
127
128 const char *CMediaData::GetPath(void)
129 {
130         ASSERT(m);
131
132         return m->path;
133 }
134
135 time_t CMediaData::GetTime(void)
136 {
137         ASSERT(m);
138
139         return m->time;
140 }
141
142 int CMediaData::GetDuration(void)
143 {
144         ASSERT(m);
145
146         return m->video->GetDuration();
147 }
148
149 int CMediaData::GetPosition(void)
150 {
151         ASSERT(m);
152
153         return m->video->GetPosition();
154 }
155
156 int CMediaData::GetWidth(void)
157 {
158         ASSERT(m);
159
160         return m->video->GetWidth();
161 }
162
163 int CMediaData::GetHeight(void)
164 {
165         ASSERT(m);
166
167         return m->video->GetHeight();
168 }