11711a65bfbc1e4fd7115482f5e67076fc9c784d
[platform/framework/web/wrt.git] / src / view / common / view_logic_security_origin_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_security_origin_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @version 1.0
20  * @brief   Support security origin dao
21  */
22
23 #include "view_logic_security_origin_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/security-origin-dao/security_origin_dao.h>
32 #include <widget_model.h>
33 #include <widget_string.h>
34
35 namespace ViewModule {
36
37 class SecurityOriginSupportImplementation
38 {
39   private:
40     WidgetModel* m_model;
41     SecurityOriginDB::SecurityOriginDAOPtr m_securityOriginDAO;
42
43   public:
44     SecurityOriginSupportImplementation(WidgetModel* widgetModel) :
45         m_model(NULL)
46     {
47         Assert(widgetModel);
48         m_model = widgetModel;
49     }
50
51     ~SecurityOriginSupportImplementation()
52     {}
53
54     SecurityOriginDB::SecurityOriginDAO* getSecurityOriginDAO(void)
55     {
56         Assert(m_model);
57         if (!m_securityOriginDAO) {
58             LogDebug("initialize securityOriginDAO");
59             m_securityOriginDAO =
60                 SecurityOriginDB::SecurityOriginDAOPtr(
61                     new SecurityOriginDB::SecurityOriginDAO(m_model->TzPkgId.
62                                                                 Get()));
63             // initialize security result data. Remove allow, deny for
64             m_securityOriginDAO->removeSecurityOriginData(
65                 SecurityOriginDB::RESULT_ALLOW_ONCE);
66             m_securityOriginDAO->removeSecurityOriginData(
67                 SecurityOriginDB::RESULT_DENY_ONCE);
68         }
69         return m_securityOriginDAO.get();
70     }
71 };
72
73 SecurityOriginSupport::SecurityOriginSupport(WidgetModel* widgetModel) :
74     m_impl(new SecurityOriginSupportImplementation(widgetModel))
75 {}
76
77 SecurityOriginSupport::~SecurityOriginSupport()
78 {}
79
80 SecurityOriginDB::SecurityOriginDAO* SecurityOriginSupport::
81     getSecurityOriginDAO(void)
82 {
83     return m_impl->getSecurityOriginDAO();
84 }
85
86 Evas_Object* SecurityOriginSupportUtil::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     Evas_Object* popup = elm_popup_add(window);
96     elm_object_style_set(popup, "popup/default");
97     evas_object_size_hint_weight_set(popup,
98                                      EVAS_HINT_EXPAND,
99                                      EVAS_HINT_EXPAND);
100
101     Evas_Object* layout = elm_layout_add(popup);
102     elm_layout_file_set(layout, WRT_EDJ_PATH, "popupWithCheck");
103     evas_object_size_hint_weight_set(layout,
104                                      EVAS_HINT_EXPAND,
105                                      EVAS_HINT_EXPAND);
106
107     Evas_Object* scroller = elm_scroller_add(layout);
108     elm_scroller_policy_set(scroller,
109                             ELM_SCROLLER_POLICY_OFF,
110                             ELM_SCROLLER_POLICY_AUTO);
111
112     Evas_Object* label = elm_label_add(scroller);
113     elm_label_line_wrap_set(label, ELM_WRAP_WORD);
114     elm_object_text_set(label, bodyText);
115
116     Evas_Object* check = elm_check_add(scroller);
117     elm_object_part_text_set(layout, "elm.text", checkText);
118     elm_object_part_content_set(layout, "elm.swallow.checkbox", check);
119
120     elm_object_content_set(scroller, label);
121     elm_object_part_content_set(layout, "elm.swallow.label", scroller);
122     elm_object_content_set(popup, layout);
123
124     Evas_Object* btn1 = elm_button_add(popup);
125     elm_object_text_set(btn1, WRT_SK_YES);
126     elm_object_part_content_set(popup, "button1", btn1);
127     evas_object_smart_callback_add(btn1, "clicked", buttonCallback, data);
128     Evas_Object* btn2 = elm_button_add(popup);
129     elm_object_text_set(btn2, WRT_SK_NO);
130     elm_object_part_content_set(popup, "button2", btn2);
131     evas_object_smart_callback_add(btn2, "clicked", buttonCallback, data);
132     return popup;
133 }
134
135 Evas_Object* SecurityOriginSupportUtil::getPopup(Evas_Object* button)
136 {
137     Assert(button);
138
139     Evas_Object* popup = button;
140     while (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
141         popup = elm_object_parent_widget_get(popup);
142         if (!popup) {
143             return NULL;
144         }
145     }
146     return popup;
147 }
148
149 Evas_Object* SecurityOriginSupportUtil::getCheck(Evas_Object* popup)
150 {
151     Assert(popup);
152     if (strcmp(elm_object_widget_type_get(popup), "elm_popup")) {
153         return NULL;
154     }
155     Evas_Object* check = elm_object_part_content_get(
156             elm_object_content_get(popup),
157             "elm.swallow.checkbox");
158     return check;
159 }
160
161 SecurityOriginDB::Result SecurityOriginSupportUtil::getResult(
162     Evas_Object* button)
163 {
164     using namespace SecurityOriginDB;
165
166     Assert(button);
167     // get popup evas_object
168     Evas_Object* popup = getPopup(button);
169     if (popup == NULL) {
170         return RESULT_UNKNOWN;
171     }
172     bool allow = !strcmp(WRT_SK_YES, elm_object_text_get(button));
173
174     // get check evas_object
175     Evas_Object* check = getCheck(popup);
176     if (check == NULL) {
177         return RESULT_UNKNOWN;
178     }
179     if (allow) {
180         return elm_check_state_get(check) ? RESULT_ALLOW_ALWAYS :
181                RESULT_ALLOW_ONCE;
182     } else {
183         return elm_check_state_get(check) ? RESULT_DENY_ALWAYS :
184                RESULT_DENY_ONCE;
185     }
186 }
187 } // namespace ViewModule