Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / UniversalSwitchPage.cpp
1 /*
2  * Copyright 2018 Samsung Electronics Co., Ltd
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 #include "UniversalSwitchPage.hpp"
18
19 #include "Popup.hpp"
20 #include "Singleton.hpp"
21 #include "setting-accessibility.h"
22
23 #include <app.h>
24
25 UniversalSwitchPage::UniversalSwitchPage()
26         : context_(Singleton<AppContext>::instance())
27 {
28         auto naviframe = context_.navContext_.getNaviframe();
29         auto genlist = Widget::make<Genlist>(naviframe);
30         genlist->setMode(ELM_LIST_COMPRESS);
31         genlist->setStyle("dialogue");
32         genlist->clear(); // TODO check if necessary
33
34         universalSwitchOffCheckbox_ = genlist->appendItem({"type1",
35                 {},
36                 {},
37                 [this](auto item) {
38                         auto state = item->getState();
39                         auto switch_count = setting_accessibility_universal_switch_dbus_config_get_switch_count(&context_.config);
40                         if (state && switch_count == 0) {
41                                 this->displayAddSwitchPopup();
42                                 item->setState(false);
43                         } else
44                                 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, state);
45                 },
46                 GenlistItem::WidgetType::toggle});
47
48         auto onUsVconfKeyChange = [this](auto state) {
49                 universalSwitchOffCheckbox_->setText(state ? "IDS_ACCS_UNIVERSAL_SWITCH_ON" : "IDS_ACCS_UNIVERSAL_SWITCH_OFF");
50                 universalSwitchOffCheckbox_->setState(state);
51         };
52         universalSwitchStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>({VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE}, false, onUsVconfKeyChange);
53
54         genlist->appendItem({"multiline", {}, "IDS_ACCS_UNIVERSAL_SWITCH_COMMENT", GenlistItem::Type::regular});
55
56         auto prevBtn = Widget::make<Button>(naviframe, [this, naviframe]() {
57                 naviframe->removeChild(usSettingsBtn_);
58                 naviframe->popBack();
59         },
60                 std::string{},
61                 Button::BACK_BUTTON_ARROW_STYLE);
62
63         naviframe->pushBack("IDS_ACCS_UNIVERSAL_SWITCH", genlist, [this]() {
64                 universalSwitchStateHandle_ = {};
65                 stopUniversalSwitch();
66                 universalSwitchOffCheckbox_ = nullptr;
67         },
68                 prevBtn);
69
70         std::string title_str = "<font_size=30>" + TranslatedString{"IDS_ACCS_SETTINGS_CAPS"}.str() + "</font_size>";
71         usSettingsBtn_ = Widget::make<Button>(naviframe, [this]() {
72                 universalSwitchSettingsPage_ = std::make_unique<UniversalSwitchSettingsPage>();
73         },
74                 title_str,
75                 "naviframe/title_right");
76         usSettingsBtn_->disable(true);
77         naviframe->setPartContent("title_right_btn", usSettingsBtn_);
78
79         startUniversalSwitch();
80 }
81
82 void UniversalSwitchPage::addNewSwitch()
83 {
84         addSwitchPage_ = std::make_unique<AddSwitchPage>();
85         addSwitchPage_->attachCallbackOnSwitchAttach(onNewSwitchAddition, this);
86 }
87
88 void UniversalSwitchPage::displayAddSwitchPopup()
89 {
90         auto popup = Widget::make<Popup>(context_.navContext_.getWindow());
91         popup->setText("To turn on Universal switch, tap ADD SWITCH and add at least one switch");
92         popup->setEextEventCallback(EEXT_CALLBACK_BACK, []() {});
93
94         auto addBtnCb = [popup, this]() {
95                 addNewSwitch();
96                 auto parent = popup->getParent();
97                 parent->removeChild(popup);
98         };
99         auto addBtn = Widget::make<Button>(popup, addBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_POPUP_ADD");
100         addBtn->setStyle("popup");
101         popup->setPartContent("button1", addBtn);
102
103         auto cancelBtnCb = [popup, this]() {
104                 universalSwitchOffCheckbox_->setState(false);
105                 DEBUG("Adding new switch refused");
106                 auto parent = popup->getParent();
107                 parent->removeChild(popup);
108         };
109         auto cancelBtn = Widget::make<Button>(popup, cancelBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_CANCEL");
110         cancelBtn->setStyle("popup");
111         popup->setPartContent("button2", cancelBtn);
112 }
113
114 void UniversalSwitchPage::onNewSwitchAddition(
115         void *data,
116         const std::string &switch_id,
117         const std::string &switch_action,
118         const std::string &switch_name)
119 {
120         RETURN_IF(!data, "Input parameter is NULL");
121         auto self = static_cast<UniversalSwitchPage *>(data);
122         // TODO Try to use service which is independent from specific view
123         setting_accessibility_universal_switch_dbus_config_addSwitchConfigurationItem(
124                 &self->context_.config, switch_id, switch_name, switch_action);
125         int switch_count = setting_accessibility_universal_switch_dbus_config_get_switch_count(&self->context_.config);
126
127         if (switch_count == 1)
128                 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, true);
129
130         DEBUG("New switch added. Id: %s, name: %s, action: %s. Switches count: %d",
131                 switch_id.c_str(),
132                 switch_name.c_str(),
133                 switch_action.c_str(),
134                 switch_count);
135 }
136
137 void UniversalSwitchPage::stopUniversalSwitch()
138 {
139         setting_accessibility_universal_switch_dbus_config_shutdown(&context_.config);
140
141         DEBUG("Stopping universal-switch");
142         Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, false);
143 }
144
145 void UniversalSwitchPage::startUniversalSwitch()
146 {
147         DEBUG("Starting universal-switch");
148         Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, true);
149         setting_accessibility_universal_switch_dbus_config_init(&context_.config, onUniversalSwitchStarting, this);
150 }
151
152 void UniversalSwitchPage::onUniversalSwitchStarting(void *data)
153 {
154         DEBUG("universal-switch started");
155         RETURN_IF(!data, "Data argument is NULL");
156         auto self = static_cast<UniversalSwitchPage *>(data);
157
158         self->universalSwitchOffCheckbox_->enable();
159
160         if (self->usSettingsBtn_)
161                 self->usSettingsBtn_->disable(false);
162 }
163
164 void UniversalSwitchPage::onVconfKeyChange(keynode_t *node, void *user_data)
165 {
166         auto self = static_cast<UniversalSwitchPage *>(user_data);
167
168         auto state = vconf_keynode_get_bool(node);
169         auto prevState = self->universalSwitchOffCheckbox_->getState();
170         if (state >= 0 && (static_cast<bool>(state) != prevState)) {
171                 self->universalSwitchOffCheckbox_->setText(
172                         state ? "IDS_ACCS_UNIVERSAL_SWITCH_ON" : "IDS_ACCS_UNIVERSAL_SWITCH_OFF");
173                 self->universalSwitchOffCheckbox_->setState(state);
174         } else {
175                 ERROR("vconf_keynode_get_bool(node) failed");
176         }
177 }