Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / RemoveSwitchPage.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 "RemoveSwitchPage.hpp"
18
19 #include "AccessibilitySettingLog.hpp"
20 #include "Button.hpp"
21 #include "Genlist.hpp"
22 #include "Popup.hpp"
23 #include "Singleton.hpp"
24 #include "VConf.hpp"
25 #include "setting-accessibility.h"
26
27 #include <app.h>
28 #include <efl_extension.h>
29
30 #define SWITCH_COUNTER_MAX 99
31 #define SWITCH_COUNTER_TITLE_LENGTH 4
32
33 RemoveSwitchPage::RemoveSwitchPage()
34         : context_(Singleton<AppContext>::instance())
35 {
36         auto naviframe = context_.navContext_.getNaviframe();
37         layout_ = Widget::make<Layout>(naviframe, "edje/accessibility-settings-set-value.edj", "set-value-switches");
38         genlist_ = Widget::make<Genlist>(layout_);
39         genlist_->setMode(ELM_LIST_COMPRESS);
40         genlist_->setStyle("dialogue");
41         layout_->setPartContent("set-value-list", genlist_);
42
43         allCheckbox_ = Widget::make<Check>(naviframe, false, std::string{}, [this]() {
44                 auto genlist_size = genlist_->size();
45                 bool all_selected = (switchesToRemoveCounter_ == genlist_size);
46
47                 auto item = genlist_->getFirst();
48                 while (item) {
49                         item->setState(!all_selected);
50                         item = item->next();
51                 }
52
53                 switchesToRemoveCounter_ = all_selected ? 0 : genlist_size;
54                 removeSwitchPopupBtn_->disable(switchesToRemoveCounter_ ? false : true);
55         });
56
57         removeSwitchPopupBtn_ = Widget::make<Button>(
58                 naviframe, [this]() { createPopupForSwitchesRemoving(); }, "IDS_ACCS_DELETE_CAPS", "naviframe/title_right");
59         removeSwitchPopupBtn_->disable(true);
60
61         naviframe->pushBack("IDS_ACCS_SELECT_ITEMS", layout_, {}, allCheckbox_);
62         naviframe->setPartContent("title_right_btn", removeSwitchPopupBtn_);
63
64         for (auto i = 0u; i < context_.config.configuration_items.size(); ++i) {
65                 auto activity_name = setting_accessibility_universal_switch_dbus_config_get_activity_name(
66                         &context_.config, context_.config.configuration_items[i]->activity_type);
67                 genlist_->appendItem({"type1",
68                         context_.config.configuration_items[i]->user_name,
69                         activity_name,
70                         [this](auto item) {
71                                 if (item->getState())
72                                         switchesToRemoveCounter_++;
73                                 else
74                                         switchesToRemoveCounter_--;
75
76                                 auto state = switchesToRemoveCounter_ == genlist_->size();
77                                 allCheckbox_->setState(state);
78
79                                 removeSwitchPopupBtn_->disable(switchesToRemoveCounter_ ? false : true);
80                         },
81                         GenlistItem::WidgetType::check});
82         }
83 }
84
85 void RemoveSwitchPage::attachCallback(UniversalSwitchCb cb, void *cbData)
86 {
87         switch_removed_cb = cb;
88         switch_removed_cb_data = cbData;
89 }
90
91 void RemoveSwitchPage::createPopupForSwitchesRemoving()
92 {
93         auto popup = Widget::make<Popup>(context_.navContext_.getNaviframe(), "IDS_ACCS_UNIVERSAL_SWITCH_DELETE_SWITCHES");
94
95         popup->setOrientation(ELM_POPUP_ORIENT_CENTER);
96         auto areAllSwitchesSelectedToRemove = switchesToRemoveCounter_ == genlist_->size();
97
98         if (Singleton<VConfInterface>::instance().get(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, false) &&
99                 areAllSwitchesSelectedToRemove) {
100                 popup->setText("IDS_ACCS_UNIVERSAL_SWITCH_DELETE_ALL_SWITCHES_DESC");
101         } else {
102                 auto s = std::to_string(switchesToRemoveCounter_) + " " +
103                                  TranslatedString{"IDS_ACCS_UNIVERSAL_SWITCH_DELETE_SWITCHES_DESC"}.str();
104                 popup->setText(s);
105         }
106         auto removeCb = [p = popup, nf = context_.navContext_.getNaviframe()]() { nf->removeChild(p); };
107         popup->setEextEventCallback(EEXT_CALLBACK_BACK, removeCb);
108         popup->setEvasSmartCallback("dismissed", removeCb);
109         popup->setEvasSmartCallback("block,clicked", removeCb);
110
111         auto cancelBtn = Widget::make<Button>(popup, removeCb, "IDS_ST_BUTTON_CANCEL", "bottom");
112         popup->setPartContent("button1", cancelBtn);
113
114         auto removeSwitchCb = [removeCb, areAllSwitchesSelectedToRemove, this]() {
115                 auto item = genlist_->getLast();
116                 size_t i = genlist_->size() - 1;
117                 while (item) {
118                         if (item->getState())
119                                 removeSwitch(i);
120
121                         item = item->prev();
122                         --i;
123                 }
124
125                 removeCb();
126                 context_.navContext_.getNaviframe()->removeChild(removeSwitchPopupBtn_);
127                 context_.navContext_.getNaviframe()->popBack();
128
129                 if (areAllSwitchesSelectedToRemove)
130                         Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, false);
131         };
132
133         auto deleteSwitchBtn = Widget::make<Button>(popup, removeSwitchCb, "IDS_ACCS_DELETE", "bottom");
134         popup->setPartContent("button2", deleteSwitchBtn);
135 }
136
137 void RemoveSwitchPage::removeSwitch(size_t item_id)
138 {
139         if (item_id >= context_.config.configuration_items.size()) {
140                 WARNING("Wrong item id");
141                 return;
142         }
143
144         auto config_item = context_.config.configuration_items[item_id];
145         DEBUG("Removing switch, switch_id: %s", config_item->switch_id.c_str());
146
147         switch_removed_cb(switch_removed_cb_data,
148                 config_item->switch_id,
149                 config_item->activity_type,
150                 config_item->user_name); // TODO try to do this without callback
151 }