8adfcbeed914a75f7c2a27edbdba22987b950fb8
[profile/tv/apps/web/browser.git] / services / WebPageUI / WebPageUI.cpp
1 /*
2  * Copyright (c) 2015 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 #include <Elementary.h>
18 #include <memory>
19 #include <boost/format.hpp>
20 #include "WebPageUI.h"
21 #include "BrowserLogger.h"
22 #include "ServiceManager.h"
23 #include "BrowserAssert.h"
24
25 namespace tizen_browser {
26 namespace base_ui {
27
28 EXPORT_SERVICE(WebPageUI, "org.tizen.browser.webpageui")
29
30 WebPageUI::WebPageUI()
31     : m_parent(nullptr)
32     , m_mainLayout(nullptr)
33     , m_errorLayout(nullptr)
34     , m_progressBar(nullptr)
35     , m_URIEntry(new URIEntry())
36     , m_homePageActive(false)
37 {
38     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
39 }
40
41 WebPageUI::~WebPageUI()
42 {
43     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
44 }
45
46 void WebPageUI::init(Evas_Object* parent)
47 {
48     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
49     M_ASSERT(parent);
50     m_parent = parent;
51 }
52
53 Evas_Object* WebPageUI::getContent()
54 {
55     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
56     M_ASSERT(m_parent);
57     if (!m_mainLayout) {
58         createLayout();
59     }
60     return m_mainLayout;
61 }
62
63 void WebPageUI::showUI()
64 {
65     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
66 }
67
68 void WebPageUI::hideUI()
69 {
70     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
71     elm_object_focus_custom_chain_unset(m_mainLayout);
72 }
73
74 void WebPageUI::loadStarted()
75 {
76     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
77     showProgressBar();
78     elm_object_signal_emit(m_URIEntry->getContent(), "shiftright_uribg", "ui");
79     elm_object_signal_emit(m_mainLayout, "shiftright_uri", "ui");
80     m_leftButtonBar->setActionForButton("refresh_stop_button", m_stopLoading);
81 }
82
83 void WebPageUI::progressChanged(double progress)
84 {
85     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
86     if (progress == 1.0) {
87         hideProgressBar();
88     } else {
89         elm_progressbar_value_set(m_progressBar, progress);
90     }
91 }
92
93 void WebPageUI::loadFinished()
94 {
95     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
96     elm_object_signal_emit(m_mainLayout, "hide_progressbar_bg", "ui");
97     m_leftButtonBar->setActionForButton("refresh_stop_button", m_reload);
98 }
99
100 bool WebPageUI::isErrorPageActive()
101 {
102     return elm_object_part_content_get(m_mainLayout, "web_view") == m_errorLayout;
103 }
104
105 void WebPageUI::setMainContent(Evas_Object* content)
106 {
107     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
108     M_ASSERT(content);
109     hideWebView();
110     elm_object_part_content_set(m_mainLayout, "web_view", content);
111     evas_object_show(content);
112
113     // set custom focus chain
114     elm_object_focus_custom_chain_unset(m_mainLayout);
115     elm_object_focus_custom_chain_append(m_mainLayout, m_rightButtonBar->getContent(), NULL);
116     if (!isHomePageActive())
117         elm_object_focus_custom_chain_append(m_mainLayout, m_leftButtonBar->getContent(), NULL);
118     elm_object_focus_custom_chain_append(m_mainLayout, m_URIEntry->getContent(), NULL);
119 }
120
121 void WebPageUI::switchViewToErrorPage()
122 {
123     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
124     m_homePageActive = false;
125     setMainContent(m_errorLayout);
126     evas_object_show(m_leftButtonBar->getContent());
127     setErrorButtons();
128 }
129
130 void WebPageUI::switchViewToWebPage(Evas_Object* content, const std::string uri)
131 {
132     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
133     m_homePageActive = false;
134     setMainContent(content);
135     evas_object_show(m_leftButtonBar->getContent());
136     updateURIBar(uri);
137     elm_object_focus_custom_chain_append(m_mainLayout, content, NULL);
138 }
139
140 void WebPageUI::switchViewToQuickAccess(Evas_Object* content)
141 {
142     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
143     m_homePageActive = true;
144     setMainContent(content);
145
146     m_URIEntry->changeUri("");
147     m_leftButtonBar->setActionForButton("refresh_stop_button", m_reload);
148     evas_object_hide(m_leftButtonBar->getContent());
149     elm_object_signal_emit(m_mainLayout, "shiftback_uri", "ui");
150     elm_object_signal_emit(m_URIEntry->getContent(), "shiftback_uribg", "ui");
151     hideProgressBar();
152 }
153
154 void WebPageUI::faviconClicked(void* data, Evas_Object*, const char*, const char*)
155 {
156     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
157     WebPageUI* self = reinterpret_cast<WebPageUI*>(data);
158     if (!self->isHomePageActive() && !self->isErrorPageActive()) {
159         self->getURIEntry().clearFocus();
160     }
161 }
162
163 void WebPageUI::setTabsNumber(int tabs)
164 {
165     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
166     if (tabs == 0) {
167         elm_object_part_text_set(m_rightButtonBar->getContent(), "tabs_number", "");
168     } else {
169         elm_object_part_text_set(m_rightButtonBar->getContent(), "tabs_number", (boost::format("%1%") % tabs).str().c_str());
170     }
171 }
172
173 void WebPageUI::createLayout()
174 {
175     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
176     M_ASSERT(m_parent);
177     // create web layout
178     m_mainLayout = elm_layout_add(m_parent);
179     evas_object_size_hint_weight_set(m_mainLayout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
180     elm_layout_file_set(m_mainLayout, edjePath("WebPageUI/WebPageUI.edj").c_str(), "main_layout");
181
182     createErrorLayout();
183     createActions();
184
185     // left buttons
186     m_leftButtonBar = std::unique_ptr<ButtonBar>(new ButtonBar(m_mainLayout, "WebPageUI/LeftButtonBar.edj", "left_button_bar"));
187     m_leftButtonBar->addAction(m_back, "prev_button");
188     m_leftButtonBar->addAction(m_forward, "next_button");
189     m_leftButtonBar->addAction(m_reload, "refresh_stop_button");
190
191     //register action that will be used later by buttons"
192     m_leftButtonBar->registerEnabledChangedCallback(m_stopLoading, "refresh_stop_button");
193
194     // right buttons
195     m_rightButtonBar = std::unique_ptr<ButtonBar>(new ButtonBar(m_mainLayout, "WebPageUI/RightButtonBar.edj", "right_button_bar"));
196     m_rightButtonBar->addAction(m_tab, "tab_button");
197     m_rightButtonBar->addAction(m_showMoreMenu, "setting_button");
198
199     // progress bar
200     m_progressBar = elm_progressbar_add(m_mainLayout);
201     elm_object_style_set(m_progressBar, "play_buffer");
202
203     //URL bar (Evas Object is shipped by URIEntry object)
204     m_URIEntry->init(m_mainLayout);
205     elm_object_part_content_set(m_mainLayout, "uri_entry", m_URIEntry->getContent());
206     elm_object_part_content_set(m_mainLayout, "uri_bar_buttons_left", m_leftButtonBar->getContent());
207     elm_object_part_content_set(m_mainLayout, "uri_bar_buttons_right", m_rightButtonBar->getContent());
208
209     elm_layout_signal_callback_add(m_URIEntry->getContent(), "slide_websearch", "elm", faviconClicked, this);
210
211     connectActions();
212 }
213
214 void WebPageUI::createErrorLayout()
215 {
216     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
217     m_errorLayout =  elm_layout_add(m_mainLayout);
218     evas_object_size_hint_weight_set(m_errorLayout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
219     evas_object_size_hint_align_set(m_errorLayout, EVAS_HINT_FILL, EVAS_HINT_FILL);
220     elm_layout_file_set(m_errorLayout, edjePath("WebPageUI/ErrorMessage.edj").c_str(), "error_message");
221 }
222
223 void WebPageUI::createActions()
224 {
225     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
226     m_back = sharedAction(new Action("Back"));
227     m_back->setToolTip("Previous");
228     m_back->setIcon("browser/toolbar_prev");
229
230     m_forward = sharedAction(new Action("Next"));
231     m_forward->setToolTip("Next");
232     m_forward->setIcon("browser/toolbar_next");
233
234     m_stopLoading = sharedAction(new Action("Stop"));
235     m_stopLoading->setToolTip("Stop");
236     m_stopLoading->setIcon("browser/toolbar_stop");
237
238     m_reload = sharedAction(new Action("Reload"));
239     m_reload->setToolTip("Reload");
240     m_reload->setIcon("browser/toolbar_reload");
241     m_tab = sharedAction(new Action("Tabs"));
242     m_tab->setToolTip("Tab page");
243     m_tab->setIcon("browser/toolbar_tab");
244
245     m_showMoreMenu = sharedAction(new Action("Settings"));
246     m_showMoreMenu->setToolTip("Settings");
247     m_showMoreMenu->setIcon("browser/toolbar_setting");
248 }
249
250 void WebPageUI::connectActions()
251 {
252     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
253     //left bar
254     m_back->triggered.connect(boost::bind(&WebPageUI::backPageConnect, this));
255     m_forward->triggered.connect(boost::bind(&WebPageUI::forwardPageConnect, this));
256     m_stopLoading->triggered.connect(boost::bind(&WebPageUI::stopLoadingPageConnect, this));
257     m_reload->triggered.connect(boost::bind(&WebPageUI::reloadPageConnect, this));
258
259     //right bar
260     m_tab->triggered.connect(boost::bind(&WebPageUI::showTabUIConnect, this));
261     m_showMoreMenu->triggered.connect(boost::bind(&WebPageUI::showMoreMenuConnect, this));
262 }
263
264 void WebPageUI::showProgressBar()
265 {
266     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
267     elm_object_signal_emit(m_mainLayout, "show_progressbar_bg", "ui");
268     elm_object_part_content_set(m_mainLayout, "progress_bar", m_progressBar);
269 }
270
271 void WebPageUI::hideProgressBar()
272 {
273     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
274     elm_object_signal_emit(m_mainLayout, "hide_progressbar_bg", "ui");
275     elm_progressbar_value_set(m_progressBar, 0.0);
276     elm_object_part_content_unset(m_mainLayout, "progress_bar");
277     evas_object_hide(m_progressBar);
278 }
279
280 void WebPageUI::hideWebView()
281 {
282     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
283     evas_object_hide(elm_object_part_content_get(m_mainLayout, "web_view"));
284     elm_object_part_content_unset(m_mainLayout, "web_view");
285 }
286
287 void WebPageUI::setErrorButtons()
288 {
289     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
290     m_leftButtonBar->setActionForButton("refresh_stop_button", m_reload);
291     m_stopLoading->setEnabled(false);
292     m_reload->setEnabled(true);
293     m_forward->setEnabled(false);
294     evas_object_hide(m_progressBar);
295 }
296
297 void WebPageUI::updateURIBar(const std::string& uri)
298 {
299     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
300     m_URIEntry->changeUri(uri);
301     m_leftButtonBar->setActionForButton("refresh_stop_button", m_reload);
302
303     m_stopLoading->setEnabled(true);
304     m_reload->setEnabled(true);
305     hideProgressBar();
306 }
307
308 std::string WebPageUI::edjePath(const std::string& file)
309 {
310     return std::string(EDJE_DIR) + file;
311 }
312
313 void WebPageUI::showTabUIConnect()
314 {
315     hideUI();
316     showTabUI();
317 }
318 void WebPageUI::showMoreMenuConnect()
319 {
320     hideUI();
321     showMoreMenu();
322 }
323
324 }   // namespace tizen_browser
325 }   // namespace base_ui