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