[Release] wrt-setting_0.0.50
[apps/home/wrt-setting.git] / webapp-detail / advancedview.cpp
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "advancedview.h"
18
19 #include <vector>
20 #include <map>
21 #include <string>
22 #include <cstring>
23 #include <Elementary.h>
24
25 #include <dpl/foreach.h>
26 #include <dpl/assert.h>
27 #include <dpl/wrt-dao-ro/WrtDatabase.h>
28 #include <dpl/wrt-dao-rw/widget_dao.h>
29 #include "exceptionsview.h"
30 #include "util.h"
31
32 namespace WebAppDetailSetting {
33
34 namespace  {
35 const char* const ELM_STYLE_CHECKBOX = "checkbox";
36 const char* const ELM_PART_ICON = "elm.icon";
37 const char* const EVAS_SMART_CALLBACK_CHANGED = "changed";
38 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_GROUPINDEX = "groupindex";
39 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT = "1text";
40 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6 = "1text.1icon.2";
41 const char* const TEXT_NAVIFRAME_TITLE = "ALLOW LIST";
42 }
43
44 AdvancedView::AdvancedView(Evas_Object *navi, DPL::String appID) :
45     m_naviframe(navi),
46     m_appID(appID)
47 {
48     m_groupMenuMap[GROUP_IDX_SECURITY_SETTINGS] = "Security Settings";
49     m_groupMenuMap[GROUP_IDX_DEVELOPER_OPTIONS] = "Developer Options";
50
51     m_menuMap[SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE] =
52         "Show security warnings";
53     m_menuMap[SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE] =
54         "Location";
55     m_menuMap[SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE] =
56         "Web notification";
57     m_menuMap[SECURITY_SETTINGS_IDX_WEB_STORAGE_USAGE] =
58         "Web storage";
59     m_menuMap[SECURITY_SETTINGS_IDX_EXCEPTIONS_SETTINGS] =
60         "Exceptions Settings";
61     m_menuMap[DEVELOPER_OPTIONS_IDX_MEMORY_SAVING_MODE] =
62         "Memory Saving Mode";
63 }
64
65 AdvancedView::~AdvancedView(void)
66 {
67 }
68
69 char* AdvancedView::getGroupText(void* data,
70                                  Evas_Object* /*obj*/,
71                                  const char* /*part*/)
72 {
73     Assert(data);
74     AdvancedSettingsGroupData* advancedSettingsData =
75         static_cast<AdvancedSettingsGroupData*>(data);
76     return strdup(advancedSettingsData->m_menuText.c_str());
77 }
78
79 void AdvancedView::delGroupData(void* data,
80                                 Evas_Object* /*obj*/)
81 {
82     Assert(data);
83     AdvancedSettingsGroupData* advancedSettingsData =
84         static_cast<AdvancedSettingsGroupData*>(data);
85     delete advancedSettingsData;
86 }
87
88 char* AdvancedView::getSettingText(void *data,
89                                            Evas_Object* /*obj*/,
90                                            const char* /*part*/)
91 {
92     Assert(data);
93     SettingData* settingData = static_cast<SettingData*>(data);
94     return strdup(settingData->m_menuText.c_str());
95 }
96
97 Evas_Object* AdvancedView::getSettingContent(void* data,
98                                                      Evas_Object* obj,
99                                                      const char* /*part*/)
100 {
101     Assert(data);
102     SettingData* settingData =
103         static_cast<SettingData*>(data);
104
105     Assert(obj);
106     Evas_Object* icon = elm_check_add(obj);
107     elm_check_state_set(icon, EINA_TRUE);
108     elm_object_style_set(icon, ELM_STYLE_CHECKBOX);
109     settingData->m_iconPart = ELM_PART_ICON;
110
111     evas_object_propagate_events_set(
112         icon,
113         EINA_FALSE);
114     elm_check_state_set(icon, settingData->isEnabled() ? EINA_TRUE : EINA_FALSE);
115     evas_object_smart_callback_add(icon,
116                                    EVAS_SMART_CALLBACK_CHANGED,
117                                    settingChangedCallback,
118                                    data);
119
120     return icon;
121 }
122
123 void AdvancedView::delSettingData(void* data,
124                                           Evas_Object* /*obj*/)
125 {
126     Assert(data);
127     SettingData* settingData = static_cast<SettingData*>(data);
128     delete settingData;
129 }
130
131 void AdvancedView::settingSelectedCallback(void* data,
132                                                    Evas_Object* obj,
133                                                    void* eventInfo)
134 {
135     Assert(eventInfo);
136     Elm_Object_Item* item = static_cast<Elm_Object_Item*>(eventInfo);
137     elm_genlist_item_selected_set(item, EINA_FALSE);
138
139     SettingData* settingData =
140         static_cast<SettingData*>(elm_object_item_data_get(item));
141     Assert(settingData);
142
143     Evas_Object* check =
144         elm_object_item_part_content_get(item,
145                                          settingData->m_iconPart.c_str());
146     Assert(check);
147     bool state = !elm_check_state_get(check);
148     settingData->saveChange(state);
149     elm_check_state_set(check, state);
150 }
151
152 void AdvancedView::settingChangedCallback(void* data,
153                                                   Evas_Object* obj,
154                                                   void* eventInfo)
155 {
156     Assert(data);
157     SettingData* settingData =
158         static_cast<SettingData*>(data);
159     Assert(obj);
160     settingData->saveChange(elm_check_state_get(obj));
161 }
162
163 char* AdvancedView::getSecurityExceptionsSettingsText(void *data,
164                                                       Evas_Object *obj,
165                                                       const char *part)
166 {
167     Assert(data);
168     return strdup("Exceptions Settings");
169 }
170
171 void AdvancedView::securityExceptionsSettingsSelectedCallback(void* data,
172                                                               Evas_Object* obj,
173                                                               void* eventInfo)
174 {
175     Assert(eventInfo);
176     Elm_Object_Item* item = static_cast<Elm_Object_Item*>(eventInfo);
177     elm_genlist_item_selected_set(item, EINA_FALSE);
178
179     AdvancedView* This =
180         static_cast<AdvancedView*>(elm_object_item_data_get(item));
181     Assert(This);
182     This->m_exceptionsView.Reset(
183         new ExceptionsView(This->m_naviframe, This->m_appID));
184     if (!This->m_exceptionsView->loadView()) {
185         This->m_exceptionsView.Reset();
186     }
187 }
188
189 bool AdvancedView::loadView(void)
190 {
191     m_exceptionsView.Reset(
192         new ExceptionsView(m_naviframe, m_appID));
193     if (!m_exceptionsView->loadView()) {
194         m_exceptionsView.Reset();
195     }
196     return true;
197 }
198
199 void AdvancedView::loadSecuritySettingsMenu(Evas_Object* genlist)
200 {
201     for (int index = SECURITY_SETTINGS_IDX_START; index <= SECURITY_SETTINGS_IDX_END; index++) {
202         if (index == SECURITY_SETTINGS_IDX_EXCEPTIONS_SETTINGS) {
203             // create exceptions security settings menu
204             static Elm_Genlist_Item_Class itcSecurityExceptionsSettings = {0,};
205             itcSecurityExceptionsSettings.item_style =
206                 ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT;
207             itcSecurityExceptionsSettings.func.text_get = getSecurityExceptionsSettingsText;
208             itcSecurityExceptionsSettings.func.content_get = NULL;
209             itcSecurityExceptionsSettings.func.state_get = NULL;
210             itcSecurityExceptionsSettings.func.del = NULL;
211             elm_genlist_item_append(genlist,
212                                     &itcSecurityExceptionsSettings,
213                                     static_cast<void*>(this),
214                                     NULL,
215                                     ELM_GENLIST_ITEM_NONE,
216                                     securityExceptionsSettingsSelectedCallback,
217                                     NULL);
218
219         } else {
220             // create usage menu
221             static Elm_Genlist_Item_Class itcSecuritySettings = {0,};
222             itcSecuritySettings.item_style =
223                 ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6;
224             itcSecuritySettings.func.text_get = getSettingText;
225             itcSecuritySettings.func.content_get = getSettingContent;
226             itcSecuritySettings.func.state_get = NULL;
227             itcSecuritySettings.func.del = delSettingData;
228
229             elm_genlist_item_append(genlist,
230                                     &itcSecuritySettings,
231                                     static_cast<void*>(
232                                         new SettingData(
233                                             m_appID, index, m_menuMap[index])),
234                                     NULL,
235                                     ELM_GENLIST_ITEM_NONE,
236                                     settingSelectedCallback,
237                                     NULL);
238         }
239     }
240 }
241
242 void AdvancedView::loadDeveloperOptionsMenu(Evas_Object* genlist)
243 {
244     static Elm_Genlist_Item_Class itcDeveloperOptions = {0,};
245     itcDeveloperOptions.item_style =
246         ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6;
247     itcDeveloperOptions.func.text_get = getSettingText;
248     itcDeveloperOptions.func.content_get = getSettingContent;
249     itcDeveloperOptions.func.state_get = NULL;
250     itcDeveloperOptions.func.del = delSettingData;
251
252     for (int index = DEVELOPER_OPTIONS_IDX_START; index <= DEVELOPER_OPTIONS_IDX_END; index++) {
253         elm_genlist_item_append(genlist,
254                                 &itcDeveloperOptions,
255                                 static_cast<void*>(
256                                     new SettingData(
257                                         m_appID, index, m_menuMap[index])),
258                                 NULL,
259                                 ELM_GENLIST_ITEM_NONE,
260                                 settingSelectedCallback,
261                                 NULL);
262     }
263 }
264
265 SettingData::SettingData(DPL::String appID, int index, std::string menuText) :
266     m_index(static_cast<MenuIndex>(index)),
267     m_menuText(menuText)
268 {
269     m_dao.Reset(new WrtDB::WidgetDAO(appID));
270 }
271
272 bool SettingData::isEnabled()
273 {
274     using namespace WrtDB;
275     SettingsType ret = SETTINGS_TYPE_UNKNOWN;
276     if (m_index == SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE) {
277         ret = m_dao->getSecurityPopupUsage();
278     } else if (m_index == SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE) {
279         ret = m_dao->getSecurityPopupUsage();
280     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE) {
281         ret = m_dao->getSecurityPopupUsage();
282     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_STORAGE_USAGE) {
283         ret = m_dao->getSecurityPopupUsage();
284     } else {
285         Assert("Wrong menu index");
286     }
287
288     if (ret == SETTINGS_TYPE_ON) {
289         return true;
290     } else if (ret == SETTINGS_TYPE_OFF) {
291         return false;
292     }
293
294     Assert("Unsupported value");
295     // For preventing compile warning, even this code isn't necessary.
296     return false;
297 }
298
299 void SettingData::saveChange(bool state)
300 {
301     using namespace WrtDB;
302     SettingsType result = state ? SETTINGS_TYPE_ON : SETTINGS_TYPE_OFF;
303
304     if (m_index == SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE) {
305         m_dao->setSecurityPopupUsage(result);
306     } else if (m_index == SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE) {
307         m_dao->setGeolocationUsage(result);
308     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE) {
309         m_dao->setWebNotificationUsage(result);
310     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_STORAGE_USAGE) {
311         m_dao->setWebDatabaseUsage(result);
312     } else {
313         Assert("Wrong menu index");
314     }
315 }
316
317 } /* WebAppDetailSetting */