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