94dcbfa1e895f21a8557bfe6d6999fefb17477a8
[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 CMediaData::CMediaData(void)
34 {
35         m = NULL;
36         media_content_connect();
37 }
38
39 CMediaData::~CMediaData(void)
40 {
41         media_content_disconnect();
42 }
43
44 bool CMediaData::sm_GetMediaInfo(media_info_h media_h, void *dt)
45 {
46         SMediaData *md;
47
48         md = (SMediaData *)dt;
49
50         media_info_get_display_name(media_h, &md->name);
51         media_info_get_file_path(media_h, &md->path);
52         media_info_get_modified_time(media_h, &md->time);
53
54         md->video = new CVideoData;
55         md->video->Update(media_h);
56
57         return true;
58 }
59
60 bool CMediaData::Create(const char *path)
61 {
62         ASSERT(!m);
63
64         filter_h filter;
65         char buf[1024];
66         int r;
67
68         r = media_filter_create(&filter);
69         if (r != MEDIA_CONTENT_ERROR_NONE) {
70                 _ERR("Media Filter Creation Failed");
71                 return false;
72         }
73
74         snprintf(buf, sizeof(buf), "MEDIA_PATH = \"%s\"", path);
75
76         r = media_filter_set_condition(filter, buf,
77                         MEDIA_CONTENT_COLLATE_DEFAULT);
78         if (r != MEDIA_CONTENT_ERROR_NONE) {
79                 _ERR("Fail to set filter condition");
80                 media_filter_destroy(filter);
81                 return false;
82         }
83
84         m = new SMediaData;
85         if (!m) {
86                 _ERR("allocation failed");
87                 return false;
88         }
89
90         r = media_info_foreach_media_from_db(filter, sm_GetMediaInfo, m);
91         if (r != MEDIA_CONTENT_ERROR_NONE) {
92                 _ERR("MEDIA CONTENT ERROR: %d", r);
93                 media_filter_destroy(filter);
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         free(m->name);
111         free(m->path);
112
113         delete m->video;
114         m->video = NULL;
115
116         delete m;
117         m = NULL;
118 }
119
120 const char *CMediaData::GetName(void)
121 {
122         ASSERT(m);
123
124         return m->name;
125 }
126
127 const char *CMediaData::GetPath(void)
128 {
129         ASSERT(m);
130
131         return m->path;
132 }
133
134 time_t CMediaData::GetTime(void)
135 {
136         ASSERT(m);
137
138         return m->time;
139 }
140
141 int CMediaData::GetDuration(void)
142 {
143         ASSERT(m);
144
145         return m->video->GetDuration();
146 }
147
148 int CMediaData::GetPosition(void)
149 {
150         ASSERT(m);
151
152         return m->video->GetPosition();
153 }
154
155 int CMediaData::GetWidth(void)
156 {
157         ASSERT(m);
158
159         return m->video->GetWidth();
160 }
161
162 int CMediaData::GetHeight(void)
163 {
164         ASSERT(m);
165
166         return m->video->GetHeight();
167 }