Enhance createPopup() API to support key callback
[platform/framework/web/wrt.git] / src / view / common / view_logic_certificate_support.cpp
1 /*
2  * Copyright (c) 2011 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  * @file    view_logic_certificate_origin_support.cpp
18  * @author  Leerang Song (leerang.song@samsung.com)
19  * @version 1.0
20  * @brief   Support certificate dao
21  */
22
23 #include "view_logic_certificate_support.h"
24
25 #include <memory>
26 #include <Evas.h>
27 #include <Elementary.h>
28 #include <efl_assist.h>
29 #include <dpl/log/log.h>
30 #include <dpl/log/secure_log.h>
31 #include <dpl/unused.h>
32 #include <dpl/assert.h>
33 #include <dpl/wrt-dao-ro/common_dao_types.h>
34 #include <wrt-commons/certificate-dao/certificate_dao.h>
35 #include <widget_model.h>
36 #include <widget_string.h>
37 #include <common/view_logic_get_parent_window_util.h>
38
39 namespace ViewModule {
40 namespace {
41 const double MAX_POPUP_HEIGHT = 0.80;
42 const double MAX_SCROLLER_HEIGHT = 0.5;
43
44 struct CallbackData {
45     Evas_Smart_Cb eaKeyCallback;
46 };
47
48 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
49 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo);
50
51 static void deleteCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
52 {
53     _D("called");
54
55     Assert(obj);
56
57     DPL_UNUSED_PARAM(e);
58     DPL_UNUSED_PARAM(eventInfo);
59
60     CallbackData* callbackData = static_cast<CallbackData*>(data);
61     if (callbackData) {
62         ea_object_event_callback_del(obj, EA_CALLBACK_BACK, callbackData->eaKeyCallback);
63         delete callbackData;
64     }
65     evas_object_event_callback_del(obj, EVAS_CALLBACK_RESIZE, resizeCallback);
66 }
67
68 static void resizeCallback(void* data, Evas* e, Evas_Object* obj, void* eventInfo)
69 {
70     _D("called");
71
72     Assert(obj);
73
74     DPL_UNUSED_PARAM(data);
75     DPL_UNUSED_PARAM(e);
76     DPL_UNUSED_PARAM(eventInfo);
77
78     Evas_Object* popup = obj;
79     int popupH;
80     evas_object_geometry_get(popup, 0, 0, 0, &popupH);
81
82     Evas_Object* parent = PopupUtil::getParentWindow(popup);
83     int parentW, parentH;
84     evas_object_geometry_get(parent, 0, 0, &parentW, &parentH);
85
86     // compare current popup height with screen height.
87     // To avoid popup is filled full screen, used magic number to be filled 80% of screen height.
88     // TODO: Automatically add scroller feature should implement in the elementary
89     double threshold = parentH * MAX_POPUP_HEIGHT;
90     double currentH = popupH;
91     if (threshold < currentH) {
92         _D("text is overflow popup height. add scroller");
93         Evas_Object* layout = elm_object_content_get(obj);
94         Evas_Object* label = elm_object_part_content_get(layout, "elm.swallow.label");
95
96         Evas_Object* scroller = elm_scroller_add(layout);
97         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_ON, ELM_SCROLLER_POLICY_AUTO);
98         evas_object_size_hint_weight_set(scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
99         evas_object_size_hint_align_set(scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
100         elm_scroller_content_min_limit(scroller, EINA_TRUE, EINA_TRUE);
101         int scrollerHeight = parentW > parentH ? parentH : parentW;
102         evas_object_size_hint_max_set(scroller, -1, scrollerHeight * MAX_SCROLLER_HEIGHT);
103
104         elm_object_part_content_unset(layout, "elm.swallow.label");
105         elm_object_content_set(scroller, label);
106         elm_object_part_content_set(layout, "elm.swallow.label", scroller);
107         evas_object_show(layout);
108     }
109 }
110 }
111
112 class CertificateSupportImplementation
113 {
114   private:
115     WidgetModel* m_model;
116     CertificateDB::CertificateDAOPtr m_certificateDAO;
117
118   public:
119     CertificateSupportImplementation(WidgetModel* widgetModel) :
120         m_model(NULL)
121     {
122         Assert(widgetModel);
123         m_model = widgetModel;
124     }
125
126     ~CertificateSupportImplementation()
127     {}
128
129     CertificateDB::CertificateDAO* getCertificateDAO(void)
130     {
131         Assert(m_model);
132         if (!m_certificateDAO) {
133             LogDebug("initialize CertificateDAO");
134             m_certificateDAO =
135                 CertificateDB::CertificateDAOPtr(
136                     new CertificateDB::CertificateDAO(m_model->TzPkgId.
137                                                                 Get()));
138             // initialize certificate result data. Remove allow, deny for
139             m_certificateDAO->removeCertificateData(
140                 CertificateDB::RESULT_ALLOW_ONCE);
141             m_certificateDAO->removeCertificateData(
142                 CertificateDB::RESULT_DENY_ONCE);
143         }
144         return m_certificateDAO.get();
145     }
146 };
147
148 CertificateSupport::CertificateSupport(WidgetModel* widgetModel) :
149     m_impl(new CertificateSupportImplementation(widgetModel))
150 {}
151
152 CertificateSupport::~CertificateSupport()
153 {}
154
155 CertificateDB::CertificateDAO* CertificateSupport::
156     getCertificateDAO(void)
157 {
158     return m_impl->getCertificateDAO();
159 }
160
161 Evas_Object* CertificateSupportUtil::createPopup(
162     Evas_Object* window,
163     const char* bodyText,
164     const char* checkText,
165     Evas_Smart_Cb buttonCallback,
166     Evas_Smart_Cb keyCallback,
167     void* data)
168 {
169     LogDebug("createPopup");
170
171     Evas_Object* parentWindow = PopupUtil::getParentWindow(window);
172     Evas_Object* popup = elm_popup_add(parentWindow);
173
174     CallbackData* callbackData = NULL;
175     if (keyCallback) {
176         CallbackData* callbackData = new CallbackData;
177         callbackData->eaKeyCallback = keyCallback;
178         ea_object_event_callback_add(popup, EA_CALLBACK_BACK, keyCallback, data);
179     }
180     evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, deleteCallback, static_cast<void*>(callbackData));
181     evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, resizeCallback, NULL);
182
183     elm_object_style_set(popup, "popup/default");
184     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
185     evas_object_size_hint_align_set(popup, EVAS_HINT_FILL, EVAS_HINT_FILL);
186
187     Evas_Object* layout = elm_layout_add(popup);
188     elm_layout_file_set(layout, WRT_EDJ_PATH, "popupWithCheck");
189     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
190     evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
191
192     Evas_Object* label = elm_label_add(popup);
193     elm_label_line_wrap_set(label, ELM_WRAP_WORD);
194     elm_object_text_set(label, bodyText);
195     evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
196     evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
197
198     Evas_Object* check = elm_check_add(layout);
199     elm_object_text_set(check, checkText);
200
201     elm_object_part_content_set(layout, "elm.swallow.label", label);
202     elm_object_part_content_set(layout, "elm.swallow.checkbox", check);
203     elm_object_content_set(popup, layout);
204
205     Evas_Object* btn1 = elm_button_add(popup);
206     elm_object_text_set(btn1, WRT_OPT_ALLOW);
207     elm_object_part_content_set(popup, "button1", btn1);
208     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
209     Evas_Object* btn2 = elm_button_add(popup);
210     elm_object_text_set(btn2, WRT_SK_CANCEL);
211     elm_object_part_content_set(popup, "button2", btn2);
212     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
213
214     return popup;
215 }
216
217 Evas_Object* CertificateSupportUtil::getPopup(Evas_Object* button)
218 {
219     Assert(button);
220
221     Evas_Object* popup = button;
222     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
223         popup = elm_object_parent_widget_get(popup);
224         if (!popup) {
225             return NULL;
226         }
227     }
228     return popup;
229 }
230
231 Evas_Object* CertificateSupportUtil::getCheck(Evas_Object* popup)
232 {
233     Assert(popup);
234     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
235         return NULL;
236     }
237     Evas_Object* check = elm_object_part_content_get(
238             elm_object_content_get(popup),
239             "elm.swallow.checkbox");
240     return check;
241 }
242
243 CertificateDB::Result CertificateSupportUtil::getResult(
244     Evas_Object* button)
245 {
246     using namespace CertificateDB;
247
248     Assert(button);
249     // get popup evas_object
250     Evas_Object* popup = getPopup(button);
251     if (popup == NULL) {
252         return RESULT_UNKNOWN;
253     }
254     bool allow = !strcmp(WRT_OPT_ALLOW, elm_object_text_get(button));
255
256     // get check evas_object
257     Evas_Object* check = getCheck(popup);
258     if (check == NULL) {
259         return RESULT_UNKNOWN;
260     }
261     if (allow) {
262         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
263                RESULT_ALLOW_ONCE;
264     } else {
265         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
266                RESULT_DENY_ONCE;
267     }
268 }
269 } // namespace ViewModule