Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / ScanningProperties.hpp
1 #ifndef SCANNING_PROPERTIES
2 #define SCANNING_PROPERTIES
3
4 #include "AccessibilitySettingLog.hpp"
5 #include "Geometry.hpp"
6 #include "VConf.hpp"
7
8 #include <experimental/any>
9 #include <functional>
10 #include <string>
11 #include <vector>
12
13 enum class ScanMethod
14 {
15         POINT = 1,
16         ROW
17 };
18
19 enum class ScanDirection
20 {
21         TO_BOTTOM = 1,
22         TO_TOP
23 };
24
25 using UniversalSwitchCb = std::function<void(void *, const std::string &, const std::string &, const std::string &)>;
26
27 class ScanningProperty
28 {
29         public:
30         ScanningProperty(const std::string &stateVConfKey = {});
31         friend class ScanningProperties;
32         bool getState() const;
33         void setState(bool state);
34
35         template <typename T>
36         T getFirstValue() const
37         {
38                 return getValue<T>(firstValue_);
39         }
40
41         template <typename T>
42         T getSecondValue() const
43         {
44                 return getValue<T>(secondValue_);
45         }
46
47         template <typename T>
48         void setFirstValue(T value)
49         {
50                 setValue<T>(value, Members::FIRST_VALUE, firstValue_);
51         }
52
53         template <typename T>
54         void setSecondValue(T value)
55         {
56                 setValue(value, Members::SECOND_VALUE, secondValue_);
57         }
58
59         void registerOnValueChangeCallback(std::function<void(ScanningProperty *)> callback);
60         void unregisterOnValueChangeCallback();
61
62         private:
63         enum class Members : size_t
64         {
65                 STATE,
66                 FIRST_VALUE,
67                 SECOND_VALUE,
68                 COUNT
69         };
70
71         template <typename T>
72         T getValue(const std::experimental::any &value) const
73         {
74                 try {
75                         ASSERT(!value.empty(), "no value");
76                         return std::experimental::any_cast<T>(value);
77                 } catch (const std::experimental::bad_any_cast &e) {
78                         ERROR("%s", e.what());
79                         return {};
80                 }
81         }
82
83         template <typename T, typename U>
84         void setValue(T value, Members member, U &field)
85         {
86                 ASSERT(static_cast<size_t>(member) < static_cast<size_t>(Members::COUNT), "Wrong class member");
87                 ASSERT(!vconfKeys_[static_cast<size_t>(member)].empty(), "Property not synced with vconf");
88                 field = value;
89                 Singleton<VConfInterface>::instance().set(vconfKeys_[static_cast<size_t>(member)], value);
90         }
91
92         template <typename T>
93         void syncFirstValueWithVConf(const std::string &vConfKey, const T defaultValue)
94         {
95                 syncValueWithVConf<T>(vConfKey, defaultValue, Members::FIRST_VALUE, firstValue_);
96         }
97
98         template <typename T>
99         void syncSecondValueWithVConf(const std::string &vConfKey, const T defaultValue)
100         {
101                 syncValueWithVConf<T>(vConfKey, defaultValue, Members::SECOND_VALUE, secondValue_);
102         }
103
104         template <typename T, typename U>
105         void syncValueWithVConf(const std::string &vConfKey, const T defaultValue, Members member, U &field)
106         {
107                 ASSERT(static_cast<size_t>(member) < static_cast<size_t>(Members::COUNT), "Wrong class member");
108                 callbackHandle_[static_cast<size_t>(member)] = std::move(Singleton<VConfInterface>::instance().registerAndGet<T>(vConfKey, defaultValue, [this, &field](auto value) {
109                         field = value;
110                         this->onValueChange();
111                 }));
112                 DEBUG("otu: %s", vConfKey.c_str());
113                 vconfKeys_[static_cast<size_t>(member)] = vConfKey;
114         }
115
116         void onValueChange();
117
118         bool state_ = true;
119         std::experimental::any firstValue_;
120         std::experimental::any secondValue_;
121         std::vector<VConfInterface::CallbackHandle> callbackHandle_;
122         std::vector<std::string> vconfKeys_;
123         std::function<void(ScanningProperty *)> onValueChangeCallback_;
124 };
125
126 enum class Property
127 {
128         AUTO_SCAN,
129         SCAN_METHOD,
130         SCAN_DIRECTION,
131         POINT_SCAN_SPEED,
132         PAUSE_ON_FIRST_ITEM,
133         TAP_DURATION,
134         SINGLE_INTERACTION_INTERVAL,
135         AUTO_TAP,
136         AUTO_MOVE_INTERVAL,
137         KEYBOARD_AUTO_TAP,
138         CURSOR_COLOR,
139         FEEDBACK_SOUND,
140         FEEDBACK_VOICE,
141         GESTURES,
142         ACTIONS,
143         SETTINGS,
144         RECENT_APPS,
145         HOME_SCREEN,
146         BACK,
147         BUTTONS_AND_KEYS,
148         CONTEXTUAL_MENU,
149         COUNT
150 };
151
152 class ScanningProperties
153 {
154         public:
155         ScanningProperties();
156         ScanningProperty *getScanningProperty(Property property);
157
158         protected:
159         ScanningProperties(const ScanningProperties &) = delete;
160         ScanningProperties(ScanningProperties &&) = delete;
161         ScanningProperties &operator=(const ScanningProperties &) = delete;
162         ScanningProperties &operator=(ScanningProperties &&) = delete;
163
164         private:
165         std::vector<std::unique_ptr<ScanningProperty>> properties_;
166 };
167
168 #endif