Temporary fix for asking geolocation permission popup crash.
[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/assert.h>
30 #include <dpl/wrt-dao-ro/common_dao_types.h>
31 #include <wrt-commons/certificate-dao/certificate_dao.h>
32 #include <widget_model.h>
33 #include <widget_string.h>
34 #include <common/view_logic_get_parent_window_util.h>
35
36 namespace ViewModule {
37
38 class CertificateSupportImplementation
39 {
40   private:
41     WidgetModel* m_model;
42     CertificateDB::CertificateDAOPtr m_certificateDAO;
43
44   public:
45     CertificateSupportImplementation(WidgetModel* widgetModel) :
46         m_model(NULL)
47     {
48         Assert(widgetModel);
49         m_model = widgetModel;
50     }
51
52     ~CertificateSupportImplementation()
53     {}
54
55     CertificateDB::CertificateDAO* getCertificateDAO(void)
56     {
57         Assert(m_model);
58         if (!m_certificateDAO) {
59             LogDebug("initialize CertificateDAO");
60             m_certificateDAO =
61                 CertificateDB::CertificateDAOPtr(
62                     new CertificateDB::CertificateDAO(m_model->TzPkgId.
63                                                                 Get()));
64             // initialize certificate result data. Remove allow, deny for
65             m_certificateDAO->removeCertificateData(
66                 CertificateDB::RESULT_ALLOW_ONCE);
67             m_certificateDAO->removeCertificateData(
68                 CertificateDB::RESULT_DENY_ONCE);
69         }
70         return m_certificateDAO.get();
71     }
72 };
73
74 CertificateSupport::CertificateSupport(WidgetModel* widgetModel) :
75     m_impl(new CertificateSupportImplementation(widgetModel))
76 {}
77
78 CertificateSupport::~CertificateSupport()
79 {}
80
81 CertificateDB::CertificateDAO* CertificateSupport::
82     getCertificateDAO(void)
83 {
84     return m_impl->getCertificateDAO();
85 }
86
87 Evas_Object* CertificateSupportUtil::createPopup(
88     Evas_Object* window,
89     const char* bodyText,
90     const char* checkText,
91     Evas_Smart_Cb
92     buttonCallback,
93     void* data)
94 {
95     LogDebug("createPopup");
96
97     Evas_Object* parentWindow = PopupUtil::getParentWindow(window);
98     Evas_Object* popup = elm_popup_add(window);
99     elm_object_style_set(popup, "popup/default");
100     evas_object_size_hint_weight_set(popup,
101                                      EVAS_HINT_EXPAND,
102                                      EVAS_HINT_EXPAND);
103
104     Evas_Object* layout = elm_layout_add(popup);
105     elm_layout_file_set(layout, WRT_EDJ_PATH, "popupWithCheck");
106     evas_object_size_hint_weight_set(layout,
107                                      EVAS_HINT_EXPAND,
108                                      EVAS_HINT_EXPAND);
109
110     Evas_Object* scroller = elm_scroller_add(layout);
111     elm_scroller_policy_set(scroller,
112                             ELM_SCROLLER_POLICY_OFF,
113                             ELM_SCROLLER_POLICY_AUTO);
114
115     Evas_Object* label = elm_label_add(scroller);
116     elm_label_line_wrap_set(label, ELM_WRAP_WORD);
117     elm_object_text_set(label, bodyText);
118
119     Evas_Object* check = elm_check_add(scroller);
120     elm_object_part_text_set(layout, "elm.text", checkText);
121     elm_object_part_content_set(layout, "elm.swallow.checkbox", check);
122
123     elm_object_content_set(scroller, label);
124     elm_object_part_content_set(layout, "elm.swallow.label", scroller);
125     elm_object_content_set(popup, layout);
126
127     Evas_Object* btn1 = elm_button_add(popup);
128     elm_object_text_set(btn1, WRT_OPT_ALLOW);
129     elm_object_part_content_set(popup, "button1", btn1);
130     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
131     Evas_Object* btn2 = elm_button_add(popup);
132     elm_object_text_set(btn2, WRT_SK_CANCEL);
133     elm_object_part_content_set(popup, "button2", btn2);
134     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
135
136     return popup;
137 }
138
139 Evas_Object* CertificateSupportUtil::getPopup(Evas_Object* button)
140 {
141     Assert(button);
142
143     Evas_Object* popup = button;
144     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
145         popup = elm_object_parent_widget_get(popup);
146         if (!popup) {
147             return NULL;
148         }
149     }
150     return popup;
151 }
152
153 Evas_Object* CertificateSupportUtil::getCheck(Evas_Object* popup)
154 {
155     Assert(popup);
156     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
157         return NULL;
158     }
159     Evas_Object* check = elm_object_part_content_get(
160             elm_object_content_get(popup),
161             "elm.swallow.end");
162     return check;
163 }
164
165 CertificateDB::Result CertificateSupportUtil::getResult(
166     Evas_Object* button)
167 {
168     using namespace CertificateDB;
169
170     Assert(button);
171     // get popup evas_object
172     Evas_Object* popup = getPopup(button);
173     if (popup == NULL) {
174         return RESULT_UNKNOWN;
175     }
176     bool allow = !strcmp(WRT_OPT_ALLOW, elm_object_text_get(button));
177
178     // get check evas_object
179     Evas_Object* check = getCheck(popup);
180     if (check == NULL) {
181         return RESULT_UNKNOWN;
182     }
183     if (allow) {
184         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
185                RESULT_ALLOW_ONCE;
186     } else {
187         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
188                RESULT_DENY_ONCE;
189     }
190 }
191 } // namespace ViewModule