Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / AccessoriesSwitchesPage.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 "AccessoriesSwitchesPage.hpp"
18
19 #include "AccessibilitySettingLog.hpp"
20 #include "Button.hpp"
21 #include "Entry.hpp"
22 #include "Layout.hpp"
23 #include "Popup.hpp"
24 #include "Singleton.hpp"
25
26 AccessoriesSwitchesPage::AccessoriesSwitchesPage(NaviframeItem targetItem)
27         : context_(Singleton<AppContext>::instance()), targetItem_(targetItem)
28 {
29         auto naviframe = context_.navContext_.getNaviframe();
30         auto layout = Widget::make<Layout>(naviframe, EDJ_ACCESSORY, GRP_ACCESSORY);
31         layout->setText(PRT_ACCESSORY_LABEL, "IDS_ACCS_UNIVERSAL_SWITCH_ADD_SWITCH_ACCESSORIES_DESC");
32
33         naviframe->pushBack("IDS_ACCS_UNIVERSAL_SWITCH_ADD_SWITCH_ACCESSORIES_TITLE", layout, [this]() {
34                 setting_accessibility_universal_switch_dbus_config_cancelCaptureSwitch(&context_.config);
35                 switchId = {};
36         });
37
38         auto prevBtn = Widget::make<Button>(
39                 naviframe, [naviframe]() { naviframe->popBack(); }, std::string{}, Button::BACK_BUTTON_ARROW_STYLE);
40         naviframe->setPartContent("prev_btn", prevBtn);
41
42         setting_accessibility_universal_switch_dbus_config_captureSwitch(
43                 &context_.config, ACCESSIBILITY_UNIVERSAL_SWITCH_ACCESSORIES_SWITCH_PROVIDER, -1, captureSwitchCb, this);
44 }
45
46 void AccessoriesSwitchesPage::attachActionCallback(UniversalSwitchCb cb, void *cbData)
47 {
48         accessories_switch_added_cb = cb;
49         accessories_switch_added_cb_data = cbData;
50 }
51
52 void AccessoriesSwitchesPage::captureSwitchCb(void *user_data, const std::string &switch_id)
53 {
54         RETURN_IF(!user_data, "Input argument user_data is NULL");
55         RETURN_IF(switch_id.empty(), "Input argument switch_id is an empty string");
56
57         auto self = static_cast<AccessoriesSwitchesPage *>(user_data);
58
59         for (auto &it : self->context_.config.configuration_items) {
60                 if (switch_id == it->switch_id) {
61                         self->alreadyMappedSwitchPopup();
62                         return;
63                 }
64         }
65         self->switchId = switch_id;
66         self->newSwitchPopup();
67 }
68
69 void AccessoriesSwitchesPage::newSwitchPopup()
70 {
71         auto popup = Widget::make<Popup>(context_.navContext_.getNaviframe(), "IDS_ACCS_UNIVERSAL_SWITCH_ADD_SWITCH");
72         popup->setEextEventCallback(EEXT_CALLBACK_BACK, []() {});
73
74         auto layout = Widget::make<Layout>(popup, EDJ_ACCESSORY_POPUP, GRP_ACCESSORY_POPUP);
75         popup->setContent(layout);
76
77         auto entry = Widget::make<Entry>(layout);
78         entry->setWeightHint(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
79         entry->setPartText("guide", "IDS_ACCS_UNIVERSAL_SWITCH_ADD_SWITCH_NAME");
80         layout->setPartContent(PRT_ACCESSORY_POPUP_ENTRY, entry);
81
82         auto cancelBtnCb = [popup, this]() {
83                 auto parent = popup->getParent();
84                 parent->removeChild(popup);
85                 setting_accessibility_universal_switch_dbus_config_captureSwitch(
86                         &context_.config, ACCESSIBILITY_UNIVERSAL_SWITCH_ACCESSORIES_SWITCH_PROVIDER, -1, captureSwitchCb, this);
87         };
88
89         auto cancelBtn = Widget::make<Button>(popup, cancelBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_CANCEL");
90         cancelBtn->setStyle("popup");
91         popup->setPartContent("button1", cancelBtn);
92
93         auto saveBtnCb = [popup, entry, this]() {
94                 if ((switchName_ = entry->getEntryText()).empty()) {
95                         DEBUG("Empty switch name entry");
96                         return;
97                 }
98
99                 actionPage_ = std::make_unique<ActionPage>(targetItem_);
100                 actionPage_->attachCallback(accessorySwitchActionCb, this);
101                 elm_naviframe_item_pop_cb_set(
102                         elm_naviframe_top_item_get(context_.navContext_.getNaviframe()->getObject()), renewCaptureSwitchCb, this); // TODO
103
104                 auto parent = popup->getParent();
105                 parent->removeChild(popup);
106         };
107
108         auto saveBtn = Widget::make<Button>(popup, saveBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_SAVE");
109         saveBtn->setStyle("popup");
110         popup->setPartContent("button2", saveBtn);
111 }
112
113 void AccessoriesSwitchesPage::alreadyMappedSwitchPopup()
114 {
115         auto popup = Widget::make<Popup>(context_.navContext_.getNaviframe(), "IDS_ACCS_UNIVERSAL_SWITCH_UNABLE_TO_ADD");
116         popup->setEextEventCallback(EEXT_CALLBACK_BACK, []() {});
117
118         auto okBtnCb = [popup, this]() {
119                 setting_accessibility_universal_switch_dbus_config_captureSwitch(
120                         &context_.config, ACCESSIBILITY_UNIVERSAL_SWITCH_ACCESSORIES_SWITCH_PROVIDER, -1, captureSwitchCb, this);
121                 auto parent = popup->getParent();
122                 parent->removeChild(popup);
123         };
124
125         auto okBtn = Widget::make<Button>(popup, okBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_OK");
126         okBtn->setStyle("popup");
127         popup->setPartContent("button1", okBtn);
128
129         auto layout = Widget::make<Layout>(popup, EDJ_ACCESSORY_POPUP, GRP_ACCESSORY_POPUP);
130         layout->setPartText(PRT_ACCESSORY_POPUP_LABEL, "IDS_ACCS_UNIVERSAL_SWITCH_ALREADY_ADDED");
131         popup->setContent(layout);
132 }
133
134 void AccessoriesSwitchesPage::accessorySwitchActionCb(const std::string &action, void *user_data)
135 {
136         auto self = static_cast<AccessoriesSwitchesPage *>(user_data);
137
138         if (self->accessories_switch_added_cb)
139                 self->accessories_switch_added_cb(self->accessories_switch_added_cb_data,
140                         self->switchId.c_str(),
141                         action,
142                         self->switchName_.c_str());
143 }
144
145 Eina_Bool AccessoriesSwitchesPage::renewCaptureSwitchCb(void *data, Elm_Object_Item *it)
146 {
147         RETURN_DEFAULT_IF(!data, "Input is empty");
148         auto self = static_cast<AccessoriesSwitchesPage *>(data);
149         setting_accessibility_universal_switch_dbus_config_captureSwitch(
150                 &self->context_.config, ACCESSIBILITY_UNIVERSAL_SWITCH_ACCESSORIES_SWITCH_PROVIDER, -1, captureSwitchCb, self);
151
152         return EINA_TRUE;
153 }