Apply the Listener
[profile/tv/apps/native/videoplayer.git] / src / main.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 <app.h>
18 #include <Elementary.h>
19 #include <dbg.h>
20 #include <AppCommon.h>
21 #include <BaseApp.h>
22 #include <BaseView.h>
23 #include <ViewMgr.h>
24 #include "define.h"
25 #include "view_player.h"
26 #include "i18n.h"
27
28 #define WIN_TITLE "Video Player"
29 #define PARAM_URI "uri"
30 #define PARAM_PLAYLIST_ID "playlist_id"
31
32 struct SAppData {
33         const char *name;
34         Evas_Object *win;
35 };
36
37 class CVideoPlayerApp : public CBaseApp {
38 private:
39         SAppData *m_pApp;
40         CViewMgr *m_pVmgr;
41         CPlayerView *m_pPlayerView;
42
43 private:
44         static Evas_Object *sm_AddWin(const char *name);
45
46 public:
47         CVideoPlayerApp(void);
48         virtual ~CVideoPlayerApp(void);
49
50         virtual bool OnCreate(void);
51         virtual void OnPause(void);
52         virtual void OnResume(void);
53         virtual void OnTerminate(void);
54         virtual void OnService(service_h service);
55
56         virtual int Run(int argc, char **argv);
57 };
58
59 CVideoPlayerApp::CVideoPlayerApp(void)
60 {
61         m_pApp = new SAppData;
62 }
63
64 CVideoPlayerApp::~CVideoPlayerApp(void)
65 {
66         delete m_pApp;
67         m_pApp = NULL;
68 }
69
70 Evas_Object *CVideoPlayerApp::sm_AddWin(const char *name)
71 {
72         Evas_Object *win;
73         Evas_Object *obj;
74         Evas *evas;
75
76         if (!name)
77                 return NULL;
78
79         win = elm_win_add(NULL, name, ELM_WIN_BASIC);
80         if (!win)
81                 return NULL;
82
83         elm_win_title_set(win, WIN_TITLE);
84
85         evas_object_show(win);
86
87         evas = evas_object_evas_get(win);
88         obj = evas_object_rectangle_add(evas);
89         evas_object_color_set(obj, 0, 0, 0, 0);
90         evas_object_render_op_set(obj, EVAS_RENDER_COPY);
91         elm_win_resize_object_add(win, obj);
92         evas_object_show(obj);
93
94         return win;
95 }
96
97 bool CVideoPlayerApp::OnCreate(void)
98 {
99         ASSERT(m_pApp);
100
101         Evas_Object *win;
102
103         elm_theme_overlay_add(NULL, THEMEFILE);
104         elm_config_focus_move_policy_set(ELM_FOCUS_MOVE_POLICY_CLICK);
105
106         win = sm_AddWin(m_pApp->name);
107         if (!win) {
108                 _ERR("failed to add window");
109                 return false;
110         }
111
112         elm_win_focus_highlight_enabled_set(win, EINA_TRUE);
113         elm_win_focus_highlight_style_set(win, STYLE_INVISIBLE);
114
115         CViewMgr::Initialize(win, NULL);
116         m_pVmgr = CViewMgr::GetInstance();
117         if (!m_pVmgr) {
118                 _ERR("failed init view manager");
119                 return false;
120         }
121
122         m_pApp->win = win;
123
124         return true;
125 }
126
127 void CVideoPlayerApp::OnTerminate(void)
128 {
129         ASSERT(m_pApp);
130
131         CViewMgr::Finalize();
132
133         if (m_pApp->win)
134                 evas_object_del(m_pApp->win);
135 }
136
137 void CVideoPlayerApp::OnPause(void)
138 {
139         ASSERT(m_pApp);
140
141         m_pVmgr->Pause();
142 }
143
144 void CVideoPlayerApp::OnResume(void)
145 {
146         ASSERT(m_pApp);
147
148         m_pVmgr->Resume();
149 }
150
151 void CVideoPlayerApp::OnService(service_h service)
152 {
153         ASSERT(m_pApp);
154
155         SPlayerParam param;
156         char *uri;
157         char *playlist;
158         int r;
159
160         r = service_get_extra_data(service, PARAM_URI, &uri);
161         if (r != SERVICE_ERROR_NONE)
162                 uri = NULL;
163
164         r = service_get_extra_data(service, PARAM_PLAYLIST_ID, &playlist);
165         if (r != SERVICE_ERROR_NONE)
166                 playlist = NULL;
167
168         param.id = VIEW_ID_PLAYER;
169         param.filepath = uri;
170         param.playlist = playlist;
171
172         m_pPlayerView = new CPlayerView(VIEW_ID_PLAYER);
173         m_pVmgr->AddView(m_pPlayerView);
174
175         if (!m_pVmgr->PushView(VIEW_ID_PLAYER, &param)) {
176                 _ERR("View push failed");
177                 CViewMgr::Finalize();
178                 free(uri);
179                 free(playlist);
180
181                 elm_exit();
182         }
183
184         free(uri);
185         free(playlist);
186 }
187
188 int CVideoPlayerApp::Run(int argc, char **argv)
189 {
190         ASSERT(m_pApp);
191
192         m_pApp->name = PACKAGE;
193
194         return CBaseApp::Run(argc, argv);
195 }
196
197 int main(int argc, char **argv)
198 {
199         CVideoPlayerApp app;
200
201         app.Run(argc, argv);
202
203         return 0;
204 }