69caba7b5a5987f3080bad66bcd7c4eba0225895
[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.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://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 #include <vconf.h>
25
26 #include <dpl/foreach.h>
27 #include <dpl/assert.h>
28 #include <dpl/wrt-dao-ro/WrtDatabase.h>
29 #include <dpl/wrt-dao-ro/vconf_config.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
31 #include "exceptionsview.h"
32 #include "util.h"
33
34 namespace WebAppDetailSetting {
35
36 namespace  {
37 const char* const ELM_STYLE_CHECKBOX = "checkbox";
38 const char* const ELM_PART_ICON = "elm.icon";
39 const char* const EVAS_SMART_CALLBACK_CHANGED = "changed";
40 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_GROUPINDEX = "groupindex";
41 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT = "1text";
42 const char* const ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6 = "1text.1icon.2";
43 const char* const TEXT_NAVIFRAME_TITLE = "ALLOW LIST";
44 }
45
46 AdvancedView::AdvancedView(Evas_Object *navi, DPL::String appID) :
47     m_naviframe(navi),
48     m_appID(appID)
49 {
50     m_groupMenuMap[GROUP_IDX_SECURITY_SETTINGS] = "Security Settings";
51     m_groupMenuMap[GROUP_IDX_DEVELOPER_OPTIONS] = "Developer Options";
52
53     m_menuMap[SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE] =
54         "Show security warnings";
55     m_menuMap[SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE] =
56         "Location";
57     m_menuMap[SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE] =
58         "Web notification";
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     resetBase();
192
193     Evas_Object* genlist = NULL;
194     genlist = elm_genlist_add(m_naviframe);
195
196     loadSecuritySettingsMenu(genlist);
197
198     elm_naviframe_item_push(m_naviframe,
199                             TEXT_NAVIFRAME_TITLE,
200                             NULL,
201                             NULL,
202                             genlist,
203                             NULL);
204     resetBase(genlist);
205     return true;
206 }
207
208 void AdvancedView::loadSecuritySettingsMenu(Evas_Object* genlist)
209 {
210     for (int index = SECURITY_SETTINGS_IDX_START; index <= SECURITY_SETTINGS_IDX_END; index++) {
211         if (index == SECURITY_SETTINGS_IDX_EXCEPTIONS_SETTINGS) {
212             // create exceptions security settings menu
213             static Elm_Genlist_Item_Class itcSecurityExceptionsSettings = {0,};
214             itcSecurityExceptionsSettings.item_style =
215                 ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT;
216             itcSecurityExceptionsSettings.func.text_get = getSecurityExceptionsSettingsText;
217             itcSecurityExceptionsSettings.func.content_get = NULL;
218             itcSecurityExceptionsSettings.func.state_get = NULL;
219             itcSecurityExceptionsSettings.func.del = NULL;
220             elm_genlist_item_append(genlist,
221                                     &itcSecurityExceptionsSettings,
222                                     static_cast<void*>(this),
223                                     NULL,
224                                     ELM_GENLIST_ITEM_NONE,
225                                     securityExceptionsSettingsSelectedCallback,
226                                     NULL);
227
228         } else {
229             // create usage menu
230             static Elm_Genlist_Item_Class itcSecuritySettings = {0,};
231             itcSecuritySettings.item_style =
232                 ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6;
233             itcSecuritySettings.func.text_get = getSettingText;
234             itcSecuritySettings.func.content_get = getSettingContent;
235             itcSecuritySettings.func.state_get = NULL;
236             itcSecuritySettings.func.del = delSettingData;
237
238             elm_genlist_item_append(genlist,
239                                     &itcSecuritySettings,
240                                     static_cast<void*>(
241                                         new SettingData(
242                                             m_appID, index, m_menuMap[index])),
243                                     NULL,
244                                     ELM_GENLIST_ITEM_NONE,
245                                     settingSelectedCallback,
246                                     NULL);
247         }
248     }
249 }
250
251 void AdvancedView::loadDeveloperOptionsMenu(Evas_Object* genlist)
252 {
253     static Elm_Genlist_Item_Class itcDeveloperOptions = {0,};
254     itcDeveloperOptions.item_style =
255         ELM_GENLIST_ITEM_CLASS_STYLE_1TEXT_1ICON_6;
256     itcDeveloperOptions.func.text_get = getSettingText;
257     itcDeveloperOptions.func.content_get = getSettingContent;
258     itcDeveloperOptions.func.state_get = NULL;
259     itcDeveloperOptions.func.del = delSettingData;
260
261     for (int index = DEVELOPER_OPTIONS_IDX_START; index <= DEVELOPER_OPTIONS_IDX_END; index++) {
262         elm_genlist_item_append(genlist,
263                                 &itcDeveloperOptions,
264                                 static_cast<void*>(
265                                     new SettingData(
266                                         m_appID, index, m_menuMap[index])),
267                                 NULL,
268                                 ELM_GENLIST_ITEM_NONE,
269                                 settingSelectedCallback,
270                                 NULL);
271     }
272 }
273
274 SettingData::SettingData(DPL::String appID, int index, std::string menuText) :
275     m_index(static_cast<MenuIndex>(index)),
276     m_menuText(menuText)
277 {
278     m_dao.Reset(new WrtDB::WidgetDAO(appID));
279 }
280
281 bool SettingData::isEnabled()
282 {
283     using namespace WrtDB;
284     std::string key;
285     if (m_index == SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE) {
286         key = VconfConfig::GetVconfKeyPopupUsage(m_dao->getTzAppId());
287     } else if (m_index == SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE) {
288         key = VconfConfig::GetVconfKeyGeolocationUsage(m_dao->getTzAppId());
289     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE) {
290         key = VconfConfig::GetVconfKeyWebNotificationUsage(
291             m_dao->getTzAppId());
292     } else if (m_index == DEVELOPER_OPTIONS_IDX_MEMORY_SAVING_MODE) {
293         key = VconfConfig::GetVconfKeyMemorySavingMode(m_dao->getTzAppId());
294     } else {
295         Assert("Wrong menu index");
296     }
297
298     int ret = 0;
299     vconf_get_int(key.c_str(), &ret);
300     if (static_cast<SettingsType>(ret) == SETTINGS_TYPE_ON) {
301         return true;
302     } else if (static_cast<SettingsType>(ret) == SETTINGS_TYPE_OFF) {
303         return false;
304     }
305
306     Assert("Unsupported value");
307     // For preventing compile warning, even this code isn't necessary.
308     return false;
309 }
310
311 void SettingData::saveChange(bool state)
312 {
313     using namespace WrtDB;
314     SettingsType result = state ? SETTINGS_TYPE_ON : SETTINGS_TYPE_OFF;
315
316     std::string key;
317     if (m_index == SECURITY_SETTINGS_IDX_SECURITY_POPUP_USAGE) {
318         key = VconfConfig::GetVconfKeyPopupUsage(m_dao->getTzAppId());
319     } else if (m_index == SECURITY_SETTINGS_IDX_GEOLOCATION_USAGE) {
320         key = VconfConfig::GetVconfKeyGeolocationUsage(m_dao->getTzAppId());
321     } else if (m_index == SECURITY_SETTINGS_IDX_WEB_NOTIFICATION_USAGE) {
322         key = VconfConfig::GetVconfKeyWebNotificationUsage(
323             m_dao->getTzAppId());
324     } else if (m_index == DEVELOPER_OPTIONS_IDX_MEMORY_SAVING_MODE) {
325         key = VconfConfig::GetVconfKeyMemorySavingMode(m_dao->getTzAppId());
326     } else {
327         Assert("Wrong menu index");
328     }
329     vconf_set_int(key.c_str(), static_cast<int>(result));
330 }
331
332 } /* WebAppDetailSetting */