Merge lp:~azzar1/unity/gsettings-property into lp:unity
- gsettings-property
- Merge into trunk
Status: | Work in progress |
---|---|
Proposed branch: | lp:~azzar1/unity/gsettings-property |
Merge into: | lp:unity |
Diff against target: |
1297 lines (+882/-144) 15 files modified
UnityCore/CMakeLists.txt (+3/-0) UnityCore/GSettingsProperty-inl.h (+129/-0) UnityCore/GSettingsProperty.cpp (+118/-0) UnityCore/GSettingsProperty.h (+85/-0) UnityCore/GSettingsScopes.cpp (+1/-1) UnityCore/GnomeSessionManager.cpp (+3/-5) UnityCore/Variant.cpp (+39/-0) UnityCore/Variant.h (+2/-0) launcher/DevicesSettingsImp.cpp (+14/-59) lockscreen/LockScreenAcceleratorController.cpp (+12/-26) plugins/unityshell/src/WindowMinimizeSpeedController.cpp (+17/-40) plugins/unityshell/src/WindowMinimizeSpeedController.h (+5/-13) tests/CMakeLists.txt (+1/-0) tests/data/external.gschema.xml (+30/-0) tests/test_gsettings_property.cpp (+423/-0) |
To merge this branch: | bzr merge lp:~azzar1/unity/gsettings-property |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Unity Team | Pending | ||
Review via email: mp+248368@code.launchpad.net |
Commit message
Description of the change
- 3915. By Andrea Azzarone
-
Actually connect to changed::* signal in MenuManager.cpp. Fixes: #1411620
Approved by: PS Jenkins bot, Marco Trevisan (Treviño) - 3916. By Andrea Azzarone
-
Actually connect to changed::* signal in UnitySettings.cpp. Fixes: #1408212
Approved by: PS Jenkins bot, Marco Trevisan (Treviño) - 3917. By Brandon Schaefer
-
Try to set the folder permission otherwise Handle permission error. Fixes: #1418255
Approved by: PS Jenkins bot, Andrea Azzarone, Marco Trevisan (Treviño) - 3918. By CI Train Bot Account
-
Releasing 7.3.1+15.
04.20150205- 0ubuntu1 - 3919. By Stephen M. Webb
-
Release 7.3.1
- 3920. By Marco Trevisan (Treviño)
-
Decorations, Panel: add menus for unfocused windows as well
Now the indicator-appmenu exports the menus for all the windows,
then it's up to us to filter them based on their parent window and
show on relevant place.
Also, set LIM as default now. Fixes: #1309778
Approved by: Andrea Azzarone, PS Jenkins bot - 3921. By CI Train Bot Account
-
Releasing 7.3.1+15.
04.20150219. 2-0ubuntu1 - 3922. By Andrea Azzarone
-
Merge with trunk and change API.
- 3923. By Andrea Azzarone
-
Use GSettingsProperty in GnomeSessionMan
ager.cpp - 3924. By Andrea Azzarone
-
Lazy initialize GSettingsProperty.
- 3925. By Andrea Azzarone
-
Use GsettingsProperty in DevicesSettings
Imp.cpp - 3926. By Andrea Azzarone
-
Move template specialiazation in a cpp file.
- 3927. By Andrea Azzarone
-
Use GSettingsProperty in LockScreenAccel
eratorControlle r.cpp - 3928. By Andrea Azzarone
-
Commit to save changes.
Unmerged revisions
- 3928. By Andrea Azzarone
-
Commit to save changes.
- 3927. By Andrea Azzarone
-
Use GSettingsProperty in LockScreenAccel
eratorControlle r.cpp - 3926. By Andrea Azzarone
-
Move template specialiazation in a cpp file.
- 3925. By Andrea Azzarone
-
Use GsettingsProperty in DevicesSettings
Imp.cpp - 3924. By Andrea Azzarone
-
Lazy initialize GSettingsProperty.
- 3923. By Andrea Azzarone
-
Use GSettingsProperty in GnomeSessionMan
ager.cpp - 3922. By Andrea Azzarone
-
Merge with trunk and change API.
Preview Diff
1 | === modified file 'UnityCore/CMakeLists.txt' |
2 | --- UnityCore/CMakeLists.txt 2014-12-19 16:37:49 +0000 |
3 | +++ UnityCore/CMakeLists.txt 2015-02-27 17:17:17 +0000 |
4 | @@ -33,6 +33,8 @@ |
5 | GLibWrapper.h |
6 | GLibWrapper-inl.h |
7 | GnomeSessionManager.h |
8 | + GSettingsProperty.h |
9 | + GSettingsProperty-inl.h |
10 | GSettingsScopes.h |
11 | Hud.h |
12 | IndicatorEntry.h |
13 | @@ -86,6 +88,7 @@ |
14 | GLibSource.cpp |
15 | GLibWrapper.cpp |
16 | GnomeSessionManager.cpp |
17 | + GSettingsProperty.cpp |
18 | GSettingsScopes.cpp |
19 | Hud.cpp |
20 | Indicator.cpp |
21 | |
22 | === added file 'UnityCore/GSettingsProperty-inl.h' |
23 | --- UnityCore/GSettingsProperty-inl.h 1970-01-01 00:00:00 +0000 |
24 | +++ UnityCore/GSettingsProperty-inl.h 2015-02-27 17:17:17 +0000 |
25 | @@ -0,0 +1,129 @@ |
26 | +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- |
27 | +/* |
28 | + * Copyright (C) 2015 Canonical Ltd |
29 | + * |
30 | + * This program is free software: you can redistribute it and/or modify |
31 | + * it under the terms of the GNU General Public License version 3 as |
32 | + * published by the Free Software Foundation. |
33 | + * |
34 | + * This program is distributed in the hope that it will be useful, |
35 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
36 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
37 | + * GNU General Public License for more detais. |
38 | + * |
39 | + * You shoud have received a copy of the GNU General Public License |
40 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
41 | + * |
42 | + * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com> |
43 | + */ |
44 | + |
45 | +#ifndef UNITY_GSETTINGS_PROPERTY_INL_H |
46 | +#define UNITY_GSETTINGS_PROPERTY_INL_H |
47 | + |
48 | +#include <type_traits> |
49 | + |
50 | +namespace unity |
51 | +{ |
52 | + |
53 | +template<typename VALUE_TYPE> |
54 | +GSettingsProperty<VALUE_TYPE>::GSettingsProperty(glib::Object<GSettings> const& gsettings, |
55 | + std::string const& key, |
56 | + CallbackType const& cb) |
57 | + : gsettings_(gsettings) |
58 | + , key_(key) |
59 | + , cb_(cb) |
60 | + , loaded_(false) |
61 | +{ |
62 | + if (cb_) |
63 | + key_changed_.Connect(gsettings_, "changed::"+key, [this] (GSettings*, gchar*) { OnChangedCb(); }); |
64 | +} |
65 | + |
66 | +template<typename VALUE_TYPE> |
67 | +GSettingsProperty<VALUE_TYPE>::GSettingsProperty(std::string const& schema_id, |
68 | + std::string const& key, |
69 | + CallbackType const& cb) |
70 | + : GSettingsProperty(glib::Object<GSettings>(g_settings_new(schema_id.c_str())), key, cb) |
71 | +{} |
72 | + |
73 | +template <typename VALUE_TYPE> |
74 | +VALUE_TYPE GSettingsProperty<VALUE_TYPE>::operator=(VALUE_TYPE const& value) |
75 | +{ |
76 | + return Set(value); |
77 | +} |
78 | + |
79 | +template <typename VALUE_TYPE> |
80 | +GSettingsProperty<VALUE_TYPE>::operator VALUE_TYPE() const |
81 | +{ |
82 | + return Get(); |
83 | +} |
84 | + |
85 | +template <typename VALUE_TYPE> |
86 | +VALUE_TYPE GSettingsProperty<VALUE_TYPE>::operator()() const |
87 | +{ |
88 | + return Get(); |
89 | +} |
90 | + |
91 | +template <typename VALUE_TYPE> |
92 | +VALUE_TYPE GSettingsProperty<VALUE_TYPE>::operator()(VALUE_TYPE const& value) |
93 | +{ |
94 | + return Set(value); |
95 | +} |
96 | + |
97 | +template <typename VALUE_TYPE> |
98 | +VALUE_TYPE GSettingsProperty<VALUE_TYPE>::Get() const |
99 | +{ |
100 | + if (cb_ && loaded_) |
101 | + { |
102 | + return value_; |
103 | + } |
104 | + else |
105 | + { |
106 | + loaded_ = true; |
107 | + return getter_function<VALUE_TYPE>(); |
108 | + } |
109 | +} |
110 | + |
111 | +template <typename VALUE_TYPE> |
112 | +VALUE_TYPE GSettingsProperty<VALUE_TYPE>::Set(VALUE_TYPE const& value) |
113 | +{ |
114 | + if (cb_ && loaded_ && value != value_) |
115 | + { |
116 | + if (setter_function<VALUE_TYPE>(value)) |
117 | + value_ = value; |
118 | + return value_; |
119 | + } |
120 | + else |
121 | + { |
122 | + setter_function<VALUE_TYPE>(value); |
123 | + return value; |
124 | + } |
125 | +} |
126 | + |
127 | +template<typename VALUE_TYPE> |
128 | +void GSettingsProperty<VALUE_TYPE>::OnChangedCb() |
129 | +{ |
130 | + auto value = getter_function<VALUE_TYPE>(); |
131 | + |
132 | + if (value_ != value) |
133 | + cb_(value_ = value); |
134 | +} |
135 | + |
136 | +// enum |
137 | +template<typename VALUE_TYPE> template<typename X> |
138 | +typename std::enable_if<std::is_enum<X>::value, bool>::type |
139 | +GSettingsProperty<VALUE_TYPE>::setter_function(X const& value) |
140 | +{ |
141 | + return g_settings_set_enum(gsettings_, key_.c_str(), static_cast<int>(value)) != FALSE; |
142 | +} |
143 | + |
144 | +template<typename VALUE_TYPE> template<typename X> |
145 | +typename std::enable_if<std::is_enum<X>::value, X>::type |
146 | +GSettingsProperty<VALUE_TYPE>::getter_function() const |
147 | +{ |
148 | + return static_cast<X>(g_settings_get_enum(gsettings_, key_.c_str())); |
149 | +} |
150 | + |
151 | + |
152 | +} |
153 | + |
154 | +#endif |
155 | \ No newline at end of file |
156 | |
157 | === added file 'UnityCore/GSettingsProperty.cpp' |
158 | --- UnityCore/GSettingsProperty.cpp 1970-01-01 00:00:00 +0000 |
159 | +++ UnityCore/GSettingsProperty.cpp 2015-02-27 17:17:17 +0000 |
160 | @@ -0,0 +1,118 @@ |
161 | +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- |
162 | +/* |
163 | +* Copyright (C) 2015 Canonical Ltd |
164 | +* |
165 | +* This program is free software: you can redistribute it and/or modify |
166 | +* it under the terms of the GNU General Public License version 3 as |
167 | +* published by the Free Software Foundation. |
168 | +* |
169 | +* This program is distributed in the hope that it will be useful, |
170 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
171 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
172 | +* GNU General Public License for more details. |
173 | +* |
174 | +* You should have received a copy of the GNU General Public License |
175 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
176 | +* |
177 | +* Authored by: Andrea Azzarone <andrea.azzarone@canonical.com> |
178 | +*/ |
179 | + |
180 | +#include "GSettingsProperty.h" |
181 | +#include <vector> |
182 | + |
183 | +namespace unity |
184 | +{ |
185 | + |
186 | +// bool |
187 | +template<>template<> |
188 | +bool GSettingsProperty<bool>::setter_function<bool>(bool const& value) |
189 | +{ |
190 | + return g_settings_set_boolean(gsettings_, key_.c_str(), value != 0 ? TRUE : FALSE) != FALSE; |
191 | +} |
192 | + |
193 | +template<>template<> |
194 | +bool GSettingsProperty<bool>::getter_function<bool>() const |
195 | +{ |
196 | + return g_settings_get_boolean(gsettings_, key_.c_str()) != FALSE; |
197 | +} |
198 | + |
199 | +// int |
200 | +template<>template<> |
201 | +bool GSettingsProperty<int32_t>::setter_function<int32_t>(int32_t const& value) |
202 | +{ |
203 | + return g_settings_set_int(gsettings_, key_.c_str(), value) != FALSE; |
204 | +} |
205 | + |
206 | +template<>template<> |
207 | +int32_t GSettingsProperty<int32_t>::getter_function<int32_t>() const |
208 | +{ |
209 | + return g_settings_get_int(gsettings_, key_.c_str()); |
210 | +} |
211 | + |
212 | +// uint |
213 | +template<>template<> |
214 | +bool GSettingsProperty<uint32_t>::setter_function<uint32_t>(uint32_t const& value) |
215 | +{ |
216 | + return g_settings_set_uint(gsettings_, key_.c_str(), value) != FALSE; |
217 | +} |
218 | + |
219 | +template<>template<> |
220 | +uint32_t GSettingsProperty<uint32_t>::getter_function<uint32_t>() const |
221 | +{ |
222 | + return g_settings_get_uint(gsettings_, key_.c_str()); |
223 | +} |
224 | + |
225 | +// double |
226 | +template<>template<> |
227 | +bool GSettingsProperty<double>::setter_function<double>(double const& value) |
228 | +{ |
229 | + return g_settings_set_double(gsettings_, key_.c_str(), value) != FALSE; |
230 | +} |
231 | + |
232 | +template<>template<> |
233 | +double GSettingsProperty<double>::getter_function<double>() const |
234 | +{ |
235 | + return g_settings_get_double(gsettings_, key_.c_str()); |
236 | +} |
237 | + |
238 | +// std::string |
239 | +template<>template<> |
240 | +bool GSettingsProperty<std::string>::setter_function<std::string>(std::string const& value) |
241 | +{ |
242 | + return g_settings_set_string(gsettings_, key_.c_str(), value.c_str()) != FALSE; |
243 | +} |
244 | + |
245 | +template<>template<> |
246 | +std::string GSettingsProperty<std::string>::getter_function<std::string>() const |
247 | +{ |
248 | + glib::String ret(g_settings_get_string(gsettings_, key_.c_str())); |
249 | + return ret.Str(); |
250 | +} |
251 | + |
252 | +// std::vector<std::string> |
253 | +template<>template<> |
254 | +bool GSettingsProperty<std::vector<std::string>>::setter_function(std::vector<std::string> const& value) |
255 | +{ |
256 | + std::vector<char const*> c_style_value; |
257 | + c_style_value.reserve(value.size()+1); |
258 | + |
259 | + for (auto const & str : value) |
260 | + c_style_value.push_back(str.c_str()); |
261 | + c_style_value.push_back(nullptr); |
262 | + |
263 | + return g_settings_set_strv(gsettings_, key_.c_str(), c_style_value.data()) != FALSE; |
264 | +} |
265 | + |
266 | +template<>template<> |
267 | +std::vector<std::string> GSettingsProperty<std::vector<std::string>>::getter_function<std::vector<std::string>>() const |
268 | +{ |
269 | + std::shared_ptr<gchar*> c_style_ret(g_settings_get_strv(gsettings_, key_.c_str()), g_strfreev); |
270 | + std::vector<std::string> ret; |
271 | + |
272 | + for (int i = 0; c_style_ret.get()[i]; ++i) |
273 | + ret.push_back(c_style_ret.get()[i]); |
274 | + |
275 | + return ret; |
276 | +} |
277 | + |
278 | +} |
279 | |
280 | === added file 'UnityCore/GSettingsProperty.h' |
281 | --- UnityCore/GSettingsProperty.h 1970-01-01 00:00:00 +0000 |
282 | +++ UnityCore/GSettingsProperty.h 2015-02-27 17:17:17 +0000 |
283 | @@ -0,0 +1,85 @@ |
284 | +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- |
285 | +/* |
286 | + * Copyright (C) 2015 Canonical Ltd |
287 | + * |
288 | + * This program is free software: you can redistribute it and/or modify |
289 | + * it under the terms of the GNU General Public License version 3 as |
290 | + * published by the Free Software Foundation. |
291 | + * |
292 | + * This program is distributed in the hope that it will be useful, |
293 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
294 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
295 | + * GNU General Public License for more detais. |
296 | + * |
297 | + * You shoud have received a copy of the GNU General Public License |
298 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
299 | + * |
300 | + * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com> |
301 | + */ |
302 | + |
303 | +#ifndef UNITY_GSETTING_PROPERTY |
304 | +#define UNITY_GSETTING_PROPERTY |
305 | + |
306 | +#include <boost/noncopyable.hpp> |
307 | +#include <string> |
308 | + |
309 | +#include <UnityCore/GLibWrapper.h> |
310 | +#include <UnityCore/GLibSignal.h> |
311 | + |
312 | +namespace unity |
313 | +{ |
314 | + |
315 | +template <typename VALUE_TYPE> |
316 | +class GSettingsProperty : private boost::noncopyable |
317 | +{ |
318 | +public: |
319 | + typedef std::function<void(VALUE_TYPE const&)> CallbackType; |
320 | + |
321 | + GSettingsProperty(std::string const& schema_id, std::string const& key, CallbackType const& cb = nullptr); |
322 | + GSettingsProperty(glib::Object<GSettings> const& gsettings, std::string const& key, CallbackType const& cb = nullptr); |
323 | + |
324 | + VALUE_TYPE operator=(VALUE_TYPE const& value); |
325 | + operator VALUE_TYPE() const; |
326 | + |
327 | + // function call access |
328 | + VALUE_TYPE operator()() const; |
329 | + VALUE_TYPE operator()(VALUE_TYPE const& value); |
330 | + |
331 | + // get and set access |
332 | + VALUE_TYPE Get() const; |
333 | + VALUE_TYPE Set(VALUE_TYPE const& value); |
334 | + |
335 | +private: |
336 | + void OnChangedCb(); |
337 | + |
338 | + template<typename X> |
339 | + typename std::enable_if<!std::is_enum<X>::value, bool>::type |
340 | + setter_function(X const& value); |
341 | + |
342 | + template<typename X> |
343 | + typename std::enable_if<std::is_enum<X>::value, bool>::type |
344 | + setter_function(X const& value); |
345 | + |
346 | + template<typename X> |
347 | + typename std::enable_if<!std::is_enum<X>::value, X>::type |
348 | + getter_function() const; |
349 | + |
350 | + template<typename X> |
351 | + typename std::enable_if<std::is_enum<X>::value, X>::type |
352 | + getter_function() const; |
353 | + |
354 | +private: |
355 | + glib::Object<GSettings> gsettings_; |
356 | + std::string key_; |
357 | + CallbackType cb_; |
358 | + mutable bool loaded_; |
359 | + |
360 | + glib::Signal<void, GSettings*, gchar*> key_changed_; |
361 | + VALUE_TYPE value_; |
362 | +}; |
363 | + |
364 | +} |
365 | + |
366 | +#include "GSettingsProperty-inl.h" |
367 | + |
368 | +#endif |
369 | |
370 | === modified file 'UnityCore/GSettingsScopes.cpp' |
371 | --- UnityCore/GSettingsScopes.cpp 2013-11-14 03:00:29 +0000 |
372 | +++ UnityCore/GSettingsScopes.cpp 2015-02-27 17:17:17 +0000 |
373 | @@ -78,7 +78,7 @@ |
374 | { |
375 | LoadScopes(); |
376 | owner_->scopes_changed.emit(scopes_order_); |
377 | - } |
378 | + } |
379 | }; |
380 | |
381 | scopes_changed.Connect(settings_, "changed::"+SCOPE_SETTINGS_KEY, change_func); |
382 | |
383 | === modified file 'UnityCore/GnomeSessionManager.cpp' |
384 | --- UnityCore/GnomeSessionManager.cpp 2015-01-24 00:56:37 +0000 |
385 | +++ UnityCore/GnomeSessionManager.cpp 2015-02-27 17:17:17 +0000 |
386 | @@ -20,6 +20,7 @@ |
387 | #include "GnomeSessionManagerImpl.h" |
388 | |
389 | #include <NuxCore/Logger.h> |
390 | +#include "GSettingsProperty.h" |
391 | #include "Variant.h" |
392 | |
393 | #include <grp.h> |
394 | @@ -218,8 +219,7 @@ |
395 | if (!schema_found) |
396 | return true; |
397 | |
398 | - glib::Object<GSettings> setting(g_settings_new(SESSION_OPTIONS.c_str())); |
399 | - return g_settings_get_boolean(setting, SUPPRESS_DIALOGS_KEY.c_str()) != TRUE; |
400 | + return !GSettingsProperty<bool>(SESSION_OPTIONS, SUPPRESS_DIALOGS_KEY)(); |
401 | } |
402 | |
403 | GVariant* GnomeManager::Impl::OnShellMethodCall(std::string const& method, GVariant* parameters) |
404 | @@ -665,9 +665,7 @@ |
405 | if (is_locked()) |
406 | return true; |
407 | |
408 | - glib::Object<GSettings> lockdown_settings(g_settings_new(GNOME_LOCKDOWN_OPTIONS.c_str())); |
409 | - |
410 | - if (g_settings_get_boolean(lockdown_settings, DISABLE_LOCKSCREEN_KEY.c_str()) || |
411 | + if (GSettingsProperty<bool>(GNOME_LOCKDOWN_OPTIONS, DISABLE_LOCKSCREEN_KEY)() || |
412 | UserName().find("guest-") == 0 || |
413 | impl_->IsUserInGroup(UserName(), "nopasswdlogin")) |
414 | { |
415 | |
416 | === modified file 'UnityCore/Variant.cpp' |
417 | --- UnityCore/Variant.cpp 2013-11-19 20:28:13 +0000 |
418 | +++ UnityCore/Variant.cpp 2015-02-27 17:17:17 +0000 |
419 | @@ -58,6 +58,18 @@ |
420 | : Variant(g_variant_new_string(value.c_str())) |
421 | {} |
422 | |
423 | +Variant::Variant(std::vector<std::string> const& strings) |
424 | +{ |
425 | + GVariantBuilder b; |
426 | + g_variant_builder_init(&b, G_VARIANT_TYPE("as")); |
427 | + |
428 | + for (auto const& str : strings) |
429 | + g_variant_builder_add(&b, "s", str.c_str()); |
430 | + |
431 | + variant_ = g_variant_builder_end(&b); |
432 | + g_variant_ref_sink(variant_); |
433 | +} |
434 | + |
435 | Variant::Variant(unsigned char value) |
436 | : Variant(g_variant_new_byte(value)) |
437 | {} |
438 | @@ -174,6 +186,33 @@ |
439 | return result ? result : ""; |
440 | } |
441 | |
442 | +bool Variant::ASToVectorOfStrings(std::vector<std::string>& vector) const |
443 | +{ |
444 | + GVariantIter* iter; |
445 | + gchar* value = nullptr; |
446 | + |
447 | + if (!variant_) |
448 | + return false; |
449 | + |
450 | + if (!g_variant_is_of_type (variant_, G_VARIANT_TYPE ("(av)")) && |
451 | + !g_variant_is_of_type (variant_, G_VARIANT_TYPE ("as"))) |
452 | + { |
453 | + return false; |
454 | + } |
455 | + |
456 | + g_variant_get(variant_, g_variant_get_type_string(variant_), &iter); |
457 | + |
458 | + vector.clear(); |
459 | + while (g_variant_iter_loop(iter, "s", &value)) |
460 | + { |
461 | + vector.push_back(value); |
462 | + } |
463 | + |
464 | + g_variant_iter_free(iter); |
465 | + |
466 | + return true; |
467 | +} |
468 | + |
469 | template <typename TYPE, typename GTYPE> |
470 | TYPE get_numeric_value(GVariant *variant_, const char *type_str, const char *fallback_type_str) |
471 | { |
472 | |
473 | === modified file 'UnityCore/Variant.h' |
474 | --- UnityCore/Variant.h 2013-11-19 20:28:13 +0000 |
475 | +++ UnityCore/Variant.h 2015-02-27 17:17:17 +0000 |
476 | @@ -48,6 +48,7 @@ |
477 | Variant(HintsMap const& hints); |
478 | explicit Variant(std::nullptr_t); |
479 | explicit Variant(std::string const&); |
480 | + explicit Variant(std::vector<std::string> const&); |
481 | explicit Variant(const char*); |
482 | explicit Variant(unsigned char); |
483 | explicit Variant(int16_t); |
484 | @@ -80,6 +81,7 @@ |
485 | float GetFloat() const; |
486 | Variant GetVariant() const; |
487 | |
488 | + bool ASToVectorOfStrings(std::vector<std::string>&) const; |
489 | bool ASVToHints(HintsMap& hints) const; |
490 | |
491 | template <typename T> |
492 | |
493 | === modified file 'launcher/DevicesSettingsImp.cpp' |
494 | --- launcher/DevicesSettingsImp.cpp 2012-10-29 09:34:54 +0000 |
495 | +++ launcher/DevicesSettingsImp.cpp 2015-02-27 17:17:17 +0000 |
496 | @@ -17,25 +17,19 @@ |
497 | * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com> |
498 | */ |
499 | |
500 | -#include <list> |
501 | - |
502 | -#include <gio/gio.h> |
503 | -#include <NuxCore/Logger.h> |
504 | - |
505 | #include "DevicesSettingsImp.h" |
506 | -#include <UnityCore/GLibSignal.h> |
507 | -#include <UnityCore/GLibWrapper.h> |
508 | + |
509 | +#include <vector> |
510 | +#include <UnityCore/GSettingsProperty.h> |
511 | |
512 | namespace unity |
513 | { |
514 | namespace launcher |
515 | { |
516 | -DECLARE_LOGGER(logger, "unity.device.settings"); |
517 | namespace |
518 | { |
519 | const std::string SETTINGS_NAME = "com.canonical.Unity.Devices"; |
520 | const std::string KEY_NAME = "blacklist"; |
521 | - |
522 | } |
523 | |
524 | // |
525 | @@ -46,51 +40,13 @@ |
526 | public: |
527 | Impl(DevicesSettingsImp* parent) |
528 | : parent_(parent) |
529 | - , settings_(g_settings_new(SETTINGS_NAME.c_str())) |
530 | - { |
531 | - DownloadBlacklist(); |
532 | - ConnectSignals(); |
533 | - } |
534 | - |
535 | - void ConnectSignals() |
536 | - { |
537 | - settings_changed_signal_.Connect(settings_, "changed::" + KEY_NAME, [this] (GSettings*, gchar*) { |
538 | - DownloadBlacklist(); |
539 | - parent_->changed.emit(); |
540 | - }); |
541 | - } |
542 | - |
543 | - void DownloadBlacklist() |
544 | - { |
545 | - std::shared_ptr<gchar*> downloaded_blacklist(g_settings_get_strv(settings_, KEY_NAME.c_str()), g_strfreev); |
546 | - |
547 | - blacklist_.clear(); |
548 | - |
549 | - auto downloaded_blacklist_raw = downloaded_blacklist.get(); |
550 | - for (int i = 0; downloaded_blacklist_raw[i]; ++i) |
551 | - blacklist_.push_back(downloaded_blacklist_raw[i]); |
552 | - } |
553 | - |
554 | - void UploadBlacklist() |
555 | - { |
556 | - const int size = blacklist_.size(); |
557 | - const char* blacklist_to_be_uploaded[size+1]; |
558 | - |
559 | - int index = 0; |
560 | - for (auto item : blacklist_) |
561 | - blacklist_to_be_uploaded[index++] = item.c_str(); |
562 | - blacklist_to_be_uploaded[index] = nullptr; |
563 | - |
564 | - if (!g_settings_set_strv(settings_, KEY_NAME.c_str(), blacklist_to_be_uploaded)) |
565 | - { |
566 | - LOG_WARNING(logger) << "Saving blacklist failed."; |
567 | - } |
568 | - } |
569 | + , blacklist_(SETTINGS_NAME, KEY_NAME, sigc::hide(sigc::mem_fun(&parent_->changed, &sigc::signal<void>::emit))) |
570 | + {} |
571 | |
572 | bool IsABlacklistedDevice(std::string const& uuid) const |
573 | { |
574 | - auto begin = std::begin(blacklist_); |
575 | - auto end = std::end(blacklist_); |
576 | + auto begin = std::begin(blacklist_()); |
577 | + auto end = std::end(blacklist_()); |
578 | return std::find(begin, end, uuid) != end; |
579 | } |
580 | |
581 | @@ -99,8 +55,9 @@ |
582 | if (uuid.empty() || IsABlacklistedDevice(uuid)) |
583 | return; |
584 | |
585 | - blacklist_.push_back(uuid); |
586 | - UploadBlacklist(); |
587 | + auto temp = blacklist_(); |
588 | + temp.push_back(uuid); |
589 | + blacklist_ = temp; |
590 | } |
591 | |
592 | void TryToUnblacklist(std::string const& uuid) |
593 | @@ -108,15 +65,13 @@ |
594 | if (uuid.empty() || !IsABlacklistedDevice(uuid)) |
595 | return; |
596 | |
597 | - blacklist_.remove(uuid); |
598 | - UploadBlacklist(); |
599 | + auto temp = blacklist_(); |
600 | + temp.erase(std::remove(std::begin(temp), std::end(temp), uuid), std::end(temp)); |
601 | + blacklist_ = temp; |
602 | } |
603 | |
604 | DevicesSettingsImp* parent_; |
605 | - glib::Object<GSettings> settings_; |
606 | - std::list<std::string> blacklist_; |
607 | - glib::Signal<void, GSettings*, gchar*> settings_changed_signal_; |
608 | - |
609 | + GSettingsProperty<std::vector<std::string>> blacklist_; |
610 | }; |
611 | |
612 | // |
613 | |
614 | === modified file 'lockscreen/LockScreenAcceleratorController.cpp' |
615 | --- lockscreen/LockScreenAcceleratorController.cpp 2014-05-16 04:51:34 +0000 |
616 | +++ lockscreen/LockScreenAcceleratorController.cpp 2015-02-27 17:17:17 +0000 |
617 | @@ -20,6 +20,7 @@ |
618 | #include "LockScreenAcceleratorController.h" |
619 | |
620 | #include <UnityCore/GLibDBusProxy.h> |
621 | +#include <UnityCore/GSettingsProperty.h> |
622 | |
623 | namespace unity |
624 | { |
625 | @@ -103,8 +104,7 @@ |
626 | |
627 | void PowerAction(session::Manager::Ptr const& session, const char *action_key) |
628 | { |
629 | - glib::Object<GSettings> settings(g_settings_new(POWER_SCHEMA)); |
630 | - auto const& action = glib::String(g_settings_get_string(settings, action_key)).Str(); |
631 | + auto const& action = GSettingsProperty<std::string>(POWER_SCHEMA, action_key)(); |
632 | |
633 | if (action == "interactive") |
634 | session->shutdown_requested.emit(session->HasInhibitors()); |
635 | @@ -124,15 +124,15 @@ |
636 | { |
637 | auto settings = glib::Object<GSettings>(g_settings_new(MEDIA_KEYS_SCHEMA)); |
638 | |
639 | - auto accelerator = std::make_shared<Accelerator>(glib::String(g_settings_get_string(settings, MEDIA_KEYS_KEY_VOLUME_MUTE))); |
640 | + auto accelerator = std::make_shared<Accelerator>(GSettingsProperty<std::string>(settings, MEDIA_KEYS_KEY_VOLUME_MUTE)); |
641 | accelerator->activated.connect(std::function<void()>(MuteIndicatorSound)); |
642 | accelerators_->Add(accelerator); |
643 | |
644 | - accelerator = std::make_shared<Accelerator>(glib::String(g_settings_get_string(settings, MEDIA_KEYS_KEY_VOLUME_DOWN))); |
645 | + accelerator = std::make_shared<Accelerator>(GSettingsProperty<std::string>(settings, MEDIA_KEYS_KEY_VOLUME_DOWN)); |
646 | accelerator->activated.connect(std::bind(ScrollIndicatorSound, -1)); |
647 | accelerators_->Add(accelerator); |
648 | |
649 | - accelerator = std::make_shared<Accelerator>(glib::String(g_settings_get_string(settings, MEDIA_KEYS_KEY_VOLUME_UP))); |
650 | + accelerator = std::make_shared<Accelerator>(GSettingsProperty<std::string>(settings, MEDIA_KEYS_KEY_VOLUME_UP)); |
651 | accelerator->activated.connect(std::bind(ScrollIndicatorSound, +1)); |
652 | accelerators_->Add(accelerator); |
653 | |
654 | @@ -154,31 +154,17 @@ |
655 | |
656 | settings = glib::Object<GSettings>(g_settings_new(INPUT_SWITCH_SCHEMA)); |
657 | |
658 | - auto variant = glib::Variant(g_settings_get_value(settings, INPUT_SWITCH_KEY_PREVIOUS_SOURCE), glib::StealRef()); |
659 | - |
660 | - if (g_variant_n_children(variant) > 0) |
661 | - { |
662 | - const gchar* string; |
663 | - |
664 | - g_variant_get_child(variant, 0, "&s", &string); |
665 | - |
666 | + auto strings = GSettingsProperty<std::vector<std::string>>(settings, INPUT_SWITCH_KEY_PREVIOUS_SOURCE)(); |
667 | + std::for_each(std::begin(strings), std::end(strings), [&](std::string const& string) { |
668 | accelerator = std::make_shared<Accelerator>(string); |
669 | accelerator->activated.connect(std::bind(ScrollIndicatorKeyboard, -1)); |
670 | - accelerators_->Add(accelerator); |
671 | - } |
672 | - |
673 | - variant = glib::Variant(g_settings_get_value(settings, INPUT_SWITCH_KEY_NEXT_SOURCE), glib::StealRef()); |
674 | - |
675 | - if (g_variant_n_children(variant) > 0) |
676 | - { |
677 | - const gchar* string; |
678 | - |
679 | - g_variant_get_child(variant, 0, "&s", &string); |
680 | - |
681 | + }); |
682 | + |
683 | + strings = GSettingsProperty<std::vector<std::string>>(settings, INPUT_SWITCH_KEY_NEXT_SOURCE)(); |
684 | + std::for_each(std::begin(strings), std::end(strings), [&](std::string const& string) { |
685 | accelerator = std::make_shared<Accelerator>(string); |
686 | accelerator->activated.connect(std::bind(ScrollIndicatorKeyboard, +1)); |
687 | - accelerators_->Add(accelerator); |
688 | - } |
689 | + }); |
690 | } |
691 | |
692 | Accelerators::Ptr const& AcceleratorController::GetAccelerators() const |
693 | |
694 | === modified file 'plugins/unityshell/src/WindowMinimizeSpeedController.cpp' |
695 | --- plugins/unityshell/src/WindowMinimizeSpeedController.cpp 2013-11-14 03:00:29 +0000 |
696 | +++ plugins/unityshell/src/WindowMinimizeSpeedController.cpp 2015-02-27 17:17:17 +0000 |
697 | @@ -35,39 +35,18 @@ |
698 | |
699 | WindowMinimizeSpeedController::WindowMinimizeSpeedController() |
700 | : _settings(g_settings_new(local::UNITY_SCHEMA.c_str())) |
701 | - , _minimize_count(g_settings_get_int(_settings, "minimize-count")) |
702 | - , _minimize_speed_threshold(g_settings_get_int(_settings, "minimize-speed-threshold")) |
703 | - , _minimize_slow_duration(g_settings_get_int(_settings, "minimize-slow-duration")) |
704 | - , _minimize_fast_duration(g_settings_get_int(_settings, "minimize-fast-duration")) |
705 | + , _minimize_count(_settings, "minimize-count", sigc::hide(sigc::mem_fun(this, &WindowMinimizeSpeedController::SetDuration))) |
706 | + , _minimize_speed_threshold(_settings, "minimize-speed-threshold", sigc::hide(sigc::mem_fun(this, &WindowMinimizeSpeedController::SetDuration))) |
707 | + , _minimize_slow_duration(_settings, "minimize-slow-duration", sigc::hide(sigc::mem_fun(this, &WindowMinimizeSpeedController::SetDuration))) |
708 | + , _minimize_fast_duration(_settings, "minimize-fast-duration", sigc::hide(sigc::mem_fun(this, &WindowMinimizeSpeedController::SetDuration))) |
709 | , _duration(200) // going to be overridden anyway, but at least it is initialised |
710 | -{ |
711 | - _minimize_count_changed.Connect(_settings, "changed::minimize-count", |
712 | - [this] (GSettings*, gchar* name) { |
713 | - _minimize_count = g_settings_get_int(_settings, name); |
714 | - SetDuration(); |
715 | - }); |
716 | - _minimize_speed_threshold_changed.Connect(_settings, "changed::minimize-speed-threshold", |
717 | - [this] (GSettings*, gchar* name) { |
718 | - _minimize_speed_threshold = g_settings_get_int(_settings, name); |
719 | - SetDuration(); |
720 | - }); |
721 | - _minimize_fast_duration_changed.Connect(_settings, "changed::minimize-fast-duration", |
722 | - [this] (GSettings*, gchar* name) { |
723 | - _minimize_fast_duration = g_settings_get_int(_settings, name); |
724 | - SetDuration(); |
725 | - }); |
726 | - _minimize_slow_duration_changed.Connect(_settings, "changed::minimize-slow-duration", |
727 | - [this] (GSettings*, gchar* name) { |
728 | - _minimize_slow_duration = g_settings_get_int(_settings, name); |
729 | - SetDuration(); |
730 | - }); |
731 | -} |
732 | +{} |
733 | |
734 | void WindowMinimizeSpeedController::UpdateCount() |
735 | { |
736 | - if (_minimize_count < _minimize_speed_threshold) { |
737 | - _minimize_count += 1; |
738 | - g_settings_set_int(_settings, "minimize-count", _minimize_count); |
739 | + if (_minimize_count() < _minimize_speed_threshold()) { |
740 | + auto temp = _minimize_count(); |
741 | + _minimize_count = ++temp; |
742 | } |
743 | } |
744 | |
745 | @@ -79,26 +58,24 @@ |
746 | void WindowMinimizeSpeedController::SetDuration() |
747 | { |
748 | /* Perform some sanity checks on the configuration values */ |
749 | - if (_minimize_fast_duration > _minimize_slow_duration) |
750 | + if (_minimize_fast_duration() > _minimize_slow_duration()) |
751 | { |
752 | LOG_WARN(logger) << "Configuration mismatch: minimize-fast-duration (" |
753 | - << _minimize_fast_duration |
754 | + << _minimize_fast_duration() |
755 | << ") is longer than minimize-slow-duration (" |
756 | - << _minimize_slow_duration << "). Not changing speed."; |
757 | + << _minimize_slow_duration() << "). Not changing speed."; |
758 | return; |
759 | } |
760 | |
761 | - if (_minimize_count < 0) |
762 | - _minimize_count = 0; |
763 | - if (_minimize_count > _minimize_speed_threshold) |
764 | - _minimize_count = _minimize_speed_threshold; |
765 | + auto minimize_count = _minimize_count(); |
766 | + minimize_count = std::max(0, std::min(minimize_count, _minimize_speed_threshold())); |
767 | |
768 | /* Adjust the speed so that it gets linearly closer to maximum speed as we |
769 | approach the threshold */ |
770 | - int speed_range = _minimize_slow_duration - _minimize_fast_duration; |
771 | - float position = (_minimize_speed_threshold <= 0) ? 1.0 : |
772 | - static_cast<float>(_minimize_count) / _minimize_speed_threshold; |
773 | - int duration = _minimize_slow_duration - std::ceil(position * speed_range); |
774 | + int speed_range = _minimize_slow_duration() - _minimize_fast_duration(); |
775 | + float position = (_minimize_speed_threshold() <= 0) ? 1.0 : |
776 | + static_cast<float>(_minimize_count) / _minimize_speed_threshold(); |
777 | + int duration = _minimize_slow_duration() - std::ceil(position * speed_range); |
778 | |
779 | if (duration != _duration) { |
780 | _duration = duration; |
781 | |
782 | === modified file 'plugins/unityshell/src/WindowMinimizeSpeedController.h' |
783 | --- plugins/unityshell/src/WindowMinimizeSpeedController.h 2012-09-12 04:08:06 +0000 |
784 | +++ plugins/unityshell/src/WindowMinimizeSpeedController.h 2015-02-27 17:17:17 +0000 |
785 | @@ -22,13 +22,9 @@ |
786 | #ifndef WINDOWMINIMIZESPEEDCONTROLLER_H |
787 | #define WINDOWMINIMIZESPEEDCONTROLLER_H |
788 | |
789 | -#include <core/core.h> |
790 | -#include <UnityCore/GLibWrapper.h> |
791 | -#include <UnityCore/GLibSignal.h> |
792 | +#include <UnityCore/GSettingsProperty.h> |
793 | #include <sigc++/sigc++.h> |
794 | |
795 | -typedef struct _GSettings GSettings; |
796 | - |
797 | using namespace unity; |
798 | |
799 | class WindowMinimizeSpeedController |
800 | @@ -43,14 +39,10 @@ |
801 | void SetDuration(); |
802 | |
803 | glib::Object<GSettings> _settings; |
804 | - int _minimize_count; |
805 | - int _minimize_speed_threshold; |
806 | - int _minimize_slow_duration; |
807 | - int _minimize_fast_duration; |
808 | - glib::Signal<void, GSettings*, gchar* > _minimize_count_changed; |
809 | - glib::Signal<void, GSettings*, gchar* > _minimize_speed_threshold_changed; |
810 | - glib::Signal<void, GSettings*, gchar* > _minimize_slow_duration_changed; |
811 | - glib::Signal<void, GSettings*, gchar* > _minimize_fast_duration_changed; |
812 | + GSettingsProperty<int> _minimize_count; |
813 | + GSettingsProperty<int> _minimize_speed_threshold; |
814 | + GSettingsProperty<int> _minimize_slow_duration; |
815 | + GSettingsProperty<int> _minimize_fast_duration; |
816 | int _duration; |
817 | }; |
818 | |
819 | |
820 | === modified file 'tests/CMakeLists.txt' |
821 | --- tests/CMakeLists.txt 2014-11-04 19:00:40 +0000 |
822 | +++ tests/CMakeLists.txt 2015-02-27 17:17:17 +0000 |
823 | @@ -119,6 +119,7 @@ |
824 | test_glib_source.cpp |
825 | test_glib_variant.cpp |
826 | test_grabhandle.cpp |
827 | + test_gsettings_property.cpp |
828 | test_gsettings_scopes.cpp |
829 | test_desktop_utilities.cpp |
830 | test_desktop_application_subject.cpp |
831 | |
832 | === modified file 'tests/data/external.gschema.xml' |
833 | --- tests/data/external.gschema.xml 2014-04-18 11:45:59 +0000 |
834 | +++ tests/data/external.gschema.xml 2015-02-27 17:17:17 +0000 |
835 | @@ -142,4 +142,34 @@ |
836 | <default>["<Super>space"]</default> |
837 | </key> |
838 | </schema> |
839 | + |
840 | + <enum id='com.canonical.unity.test.enum'> |
841 | + <value nick='e1' value='0'/> |
842 | + <value nick='e2' value='1'/> |
843 | + <value nick='e3' value='2'/> |
844 | + </enum> |
845 | + |
846 | + <schema path = "/com/canonical/unity/test/" id="com.canonical.unity.test"> |
847 | + <key type="b" name="bool"> |
848 | + <default>true</default> |
849 | + </key> |
850 | + <key type="i" name="int"> |
851 | + <default>-123</default> |
852 | + </key> |
853 | + <key type="u" name="uint"> |
854 | + <default>123</default> |
855 | + </key> |
856 | + <key type="d" name="double"> |
857 | + <default>123.456</default> |
858 | + </key> |
859 | + <key type="s" name="string"> |
860 | + <default>'abcdef'</default> |
861 | + </key> |
862 | + <key name='enum' enum='com.canonical.unity.test.enum'> |
863 | + <default>'e1'</default> |
864 | + </key> |
865 | + <key type="as" name='vector-string'> |
866 | + <default>['a', 'b', 'c']</default> |
867 | + </key> |
868 | + </schema> |
869 | </schemalist> |
870 | |
871 | === added file 'tests/test_gsettings_property.cpp' |
872 | --- tests/test_gsettings_property.cpp 1970-01-01 00:00:00 +0000 |
873 | +++ tests/test_gsettings_property.cpp 2015-02-27 17:17:17 +0000 |
874 | @@ -0,0 +1,423 @@ |
875 | +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- |
876 | +/* |
877 | + * Copyright (C) 2015 Canonical Ltd |
878 | + * |
879 | + * This program is free software: you can redistribute it and/or modify |
880 | + * it under the terms of the GNU General Public License version 3 as |
881 | + * published by the Free Software Foundation. |
882 | + * |
883 | + * This program is distributed in the hope that it will be useful, |
884 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
885 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
886 | + * GNU General Public License for more details. |
887 | + * |
888 | + * You should have received a copy of the GNU General Public License |
889 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
890 | + * |
891 | + * Authored by: Andrea Azzarone <andrea.azzarone@canonical.com> |
892 | + */ |
893 | + |
894 | +#include <gtest/gtest.h> |
895 | +using namespace testing; |
896 | + |
897 | +#include <UnityCore/GSettingsProperty.h> |
898 | +#include <UnityCore/Variant.h> |
899 | + |
900 | +namespace |
901 | +{ |
902 | + |
903 | +const gchar* SETTINGS_NAME = "com.canonical.unity.test"; |
904 | + |
905 | +/* Define setter and getter external functions. For the getter functions we need to be more specific |
906 | + * because unity::glib::Variant is not templated. |
907 | + */ |
908 | +template<typename T> |
909 | +typename std::enable_if<!std::is_enum<T>::value, void>::type |
910 | +setter_function(unity::glib::Object<GSettings> gsettings, std::string const& key, T const& value) |
911 | +{ |
912 | + g_settings_set_value(gsettings, key.c_str(), unity::glib::Variant(value)); |
913 | +} |
914 | + |
915 | +template<typename T> |
916 | +typename std::enable_if<std::is_enum<T>::value, void>::type |
917 | +setter_function(unity::glib::Object<GSettings> gsettings, std::string const& key, T const& value) |
918 | +{ |
919 | + g_settings_set_enum(gsettings, key.c_str(), static_cast<int>(value)); |
920 | +} |
921 | + |
922 | +template<typename T> |
923 | +typename std::enable_if<std::is_same<T, bool>::value, T>::type |
924 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
925 | +{ |
926 | + return g_settings_get_boolean(gsettings, key.c_str()); |
927 | +} |
928 | + |
929 | +template<typename T> |
930 | +typename std::enable_if<std::is_same<T, int>::value, T>::type |
931 | + getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
932 | +{ |
933 | + return g_settings_get_int(gsettings, key.c_str()); |
934 | +} |
935 | + |
936 | +template<typename T> |
937 | +typename std::enable_if<std::is_same<T, uint>::value, T>::type |
938 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
939 | +{ |
940 | + return g_settings_get_uint(gsettings, key.c_str()); |
941 | +} |
942 | + |
943 | +template<typename T> |
944 | +typename std::enable_if<std::is_same<T, double>::value, T>::type |
945 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
946 | +{ |
947 | + return g_settings_get_double(gsettings, key.c_str()); |
948 | +} |
949 | + |
950 | +template<typename T> |
951 | +typename std::enable_if<std::is_same<T, std::string>::value, T>::type |
952 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
953 | +{ |
954 | + unity::glib::Variant ret(g_settings_get_value(gsettings, key.c_str())); |
955 | + return ret.GetString(); |
956 | +} |
957 | + |
958 | +template<typename T> |
959 | +typename std::enable_if<std::is_same<T, std::vector<std::string>>::value, T>::type |
960 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
961 | +{ |
962 | + std::vector<std::string> vec; |
963 | + |
964 | + unity::glib::Variant variant(g_settings_get_value(gsettings, key.c_str())); |
965 | + variant.ASToVectorOfStrings(vec); |
966 | + |
967 | + return vec; |
968 | +} |
969 | + |
970 | +template<typename T> |
971 | +typename std::enable_if<std::is_enum<T>::value, T>::type |
972 | +getter_function(unity::glib::Object<GSettings> gsettings, std::string const& key) |
973 | +{ |
974 | + return static_cast<T>(g_settings_get_enum(gsettings, key.c_str())); |
975 | +} |
976 | + |
977 | +// End defintion setter/getter functions |
978 | + |
979 | +template<typename T> |
980 | +class TestGSettingsProperty : public Test |
981 | +{ |
982 | +public: |
983 | + unity::glib::Object<GSettings> gsettings_; |
984 | + |
985 | + static T initial_value; |
986 | + static T target_value; |
987 | + static std::string key; |
988 | + |
989 | + void SetUp() |
990 | + { |
991 | + // Setting the test values |
992 | + gsettings_ = g_settings_new(SETTINGS_NAME); |
993 | + setter_function<T>(gsettings_, key, initial_value); |
994 | + } |
995 | +}; |
996 | + |
997 | +TYPED_TEST_CASE_P(TestGSettingsProperty); |
998 | + |
999 | +TYPED_TEST_P(TestGSettingsProperty, ConstructionSchemaId) |
1000 | +{ |
1001 | + { |
1002 | + unity::GSettingsProperty<TypeParam> prop(SETTINGS_NAME, this->key); |
1003 | + EXPECT_EQ(this->initial_value, prop()); |
1004 | + } |
1005 | + |
1006 | + { |
1007 | + unity::GSettingsProperty<TypeParam> prop(SETTINGS_NAME, this->key, [](TypeParam const&) {}); |
1008 | + EXPECT_EQ(this->initial_value, prop()); |
1009 | + } |
1010 | +} |
1011 | + |
1012 | +TYPED_TEST_P(TestGSettingsProperty, ConstructionGSetting) |
1013 | +{ |
1014 | + { |
1015 | + ASSERT_EQ(1, G_OBJECT(this->gsettings_.RawPtr())->ref_count); |
1016 | + |
1017 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key); |
1018 | + |
1019 | + ASSERT_EQ(2, G_OBJECT(this->gsettings_.RawPtr())->ref_count); |
1020 | + EXPECT_EQ(this->initial_value, prop()); |
1021 | + } |
1022 | + |
1023 | + { |
1024 | + ASSERT_EQ(1, G_OBJECT(this->gsettings_.RawPtr())->ref_count); |
1025 | + |
1026 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [](TypeParam const&) {}); |
1027 | + |
1028 | + ASSERT_EQ(2, G_OBJECT(this->gsettings_.RawPtr())->ref_count); |
1029 | + EXPECT_EQ(this->initial_value, prop()); |
1030 | + } |
1031 | + |
1032 | + ASSERT_EQ(1, G_OBJECT(this->gsettings_.RawPtr())->ref_count); |
1033 | +} |
1034 | + |
1035 | +TYPED_TEST_P(TestGSettingsProperty, Cast) |
1036 | +{ |
1037 | + { |
1038 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_,this->key); |
1039 | + EXPECT_EQ(this->initial_value, static_cast<TypeParam>(prop)); |
1040 | + } |
1041 | + |
1042 | + { |
1043 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_,this->key, [](TypeParam const&) {}); |
1044 | + EXPECT_EQ(this->initial_value, static_cast<TypeParam>(prop)); |
1045 | + } |
1046 | +} |
1047 | + |
1048 | +TYPED_TEST_P(TestGSettingsProperty, ChangedSignal) |
1049 | +{ |
1050 | + bool signal_received = false; |
1051 | + TypeParam signal_value = this->initial_value; |
1052 | + |
1053 | + // Make sure we get the changed signal if the settings key is modified by an external agent |
1054 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [&] (TypeParam value) { |
1055 | + signal_received = true; |
1056 | + signal_value = value; |
1057 | + }); |
1058 | + |
1059 | + setter_function(this->gsettings_, this->key.c_str(), this->target_value); |
1060 | + |
1061 | + ASSERT_TRUE(signal_received); |
1062 | + EXPECT_EQ(this->target_value, signal_value); |
1063 | +} |
1064 | + |
1065 | + |
1066 | +TYPED_TEST_P(TestGSettingsProperty, OperatorEqCb) |
1067 | +{ |
1068 | + bool signal_received = false; |
1069 | + TypeParam signal_value = this->initial_value; |
1070 | + |
1071 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [&](TypeParam value) { |
1072 | + signal_received = true; |
1073 | + signal_value = value; |
1074 | + }); |
1075 | + |
1076 | + // Make sure ::opertor= works: |
1077 | + // - when old_value != new_value |
1078 | + prop = this->target_value; |
1079 | + |
1080 | + ASSERT_TRUE(signal_received); |
1081 | + EXPECT_EQ(this->target_value, signal_value); |
1082 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1083 | + EXPECT_EQ(this->target_value, prop()); |
1084 | + |
1085 | + // - when old_value == new_value |
1086 | + signal_received = false; |
1087 | + prop = this->target_value; |
1088 | + |
1089 | + ASSERT_FALSE(signal_received); |
1090 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1091 | + EXPECT_EQ(this->target_value, prop()); |
1092 | +} |
1093 | + |
1094 | +TYPED_TEST_P(TestGSettingsProperty, OperatorEqNoCb) |
1095 | +{ |
1096 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key); |
1097 | + |
1098 | + // Make sure ::opertor= works: |
1099 | + // - when old_value != new_value |
1100 | + prop = this->target_value; |
1101 | + |
1102 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1103 | + EXPECT_EQ(this->target_value, prop()); |
1104 | + |
1105 | + // - when old_value == new_value |
1106 | + prop = this->target_value; |
1107 | + |
1108 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1109 | + EXPECT_EQ(this->target_value, prop()); |
1110 | +} |
1111 | + |
1112 | + |
1113 | +TYPED_TEST_P(TestGSettingsProperty, OperatorCallCb) |
1114 | +{ |
1115 | + bool signal_received = false; |
1116 | + TypeParam signal_value = this->initial_value; |
1117 | + |
1118 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [&](TypeParam value) { |
1119 | + signal_received = true; |
1120 | + signal_value = value; |
1121 | + }); |
1122 | + |
1123 | + // Make sure ::opertor() works: |
1124 | + // - when old_value != new_value |
1125 | + prop(this->target_value); |
1126 | + |
1127 | + ASSERT_TRUE(signal_received); |
1128 | + EXPECT_EQ(this->target_value, signal_value); |
1129 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1130 | + EXPECT_EQ(this->target_value, prop()); |
1131 | + |
1132 | + // - when old_value == new_value |
1133 | + signal_received = false; |
1134 | + prop(this->target_value); |
1135 | + |
1136 | + ASSERT_FALSE(signal_received); |
1137 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1138 | + EXPECT_EQ(this->target_value, prop()); |
1139 | +} |
1140 | + |
1141 | +TYPED_TEST_P(TestGSettingsProperty, OperatorCallNoCb) |
1142 | +{ |
1143 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key); |
1144 | + |
1145 | + // Make sure ::opertor() works: |
1146 | + // - when old_value != new_value |
1147 | + prop(this->target_value); |
1148 | + |
1149 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1150 | + EXPECT_EQ(this->target_value, prop()); |
1151 | + |
1152 | + // - when old_value == new_value |
1153 | + prop(this->target_value); |
1154 | + |
1155 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1156 | + EXPECT_EQ(this->target_value, prop()); |
1157 | +} |
1158 | + |
1159 | +TYPED_TEST_P(TestGSettingsProperty, GetCb) |
1160 | +{ |
1161 | + // Make sure ::Get works: |
1162 | + // - after construction |
1163 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [](TypeParam const&){}); |
1164 | + EXPECT_EQ(this->initial_value, prop.Get()); |
1165 | + |
1166 | + // - after operator= |
1167 | + prop = this->target_value; |
1168 | + EXPECT_EQ(this->target_value, prop.Get()); |
1169 | + |
1170 | + // - after extenal editing |
1171 | + setter_function(this->gsettings_, this->key, this->initial_value); |
1172 | + EXPECT_EQ(this->initial_value, prop.Get()); |
1173 | +} |
1174 | + |
1175 | +TYPED_TEST_P(TestGSettingsProperty, GetNoCb) |
1176 | +{ |
1177 | + // Make sure ::Get works: |
1178 | + // - after construction |
1179 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key); |
1180 | + EXPECT_EQ(this->initial_value, prop.Get()); |
1181 | + |
1182 | + // - after operator= |
1183 | + prop = this->target_value; |
1184 | + EXPECT_EQ(this->target_value, prop.Get()); |
1185 | + |
1186 | + // - after extenal editing |
1187 | + setter_function(this->gsettings_, this->key, this->initial_value); |
1188 | + EXPECT_EQ(this->initial_value, prop.Get()); |
1189 | +} |
1190 | + |
1191 | +TYPED_TEST_P(TestGSettingsProperty, SetCb) |
1192 | +{ |
1193 | + bool signal_received = false; |
1194 | + TypeParam signal_value = this->initial_value; |
1195 | + |
1196 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key, [&](TypeParam value){ |
1197 | + signal_received = true; |
1198 | + signal_value = value; |
1199 | + }); |
1200 | + |
1201 | + // Make sure ::Set works: |
1202 | + // - when old_value != new_value |
1203 | + prop.Set(this->target_value); |
1204 | + |
1205 | + ASSERT_TRUE(signal_received); |
1206 | + EXPECT_EQ(this->target_value, signal_value); |
1207 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1208 | + EXPECT_EQ(this->target_value, prop()); |
1209 | + |
1210 | + // - when old_value == new_value |
1211 | + signal_received = false; |
1212 | + prop.Set(this->target_value); |
1213 | + |
1214 | + ASSERT_FALSE(signal_received); |
1215 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1216 | + EXPECT_EQ(this->target_value, prop()); |
1217 | +} |
1218 | + |
1219 | +TYPED_TEST_P(TestGSettingsProperty, SetNoCb) |
1220 | +{ |
1221 | + unity::GSettingsProperty<TypeParam> prop(this->gsettings_, this->key); |
1222 | + |
1223 | + // Make sure ::Set works: |
1224 | + // - when old_value != new_value |
1225 | + prop.Set(this->target_value); |
1226 | + |
1227 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1228 | + EXPECT_EQ(this->target_value, prop()); |
1229 | + |
1230 | + // - when old_value == new_value |
1231 | + prop.Set(this->target_value); |
1232 | + |
1233 | + EXPECT_EQ(this->target_value, getter_function<TypeParam>(this->gsettings_, this->key)); |
1234 | + EXPECT_EQ(this->target_value, prop()); |
1235 | +} |
1236 | + |
1237 | + |
1238 | +REGISTER_TYPED_TEST_CASE_P(TestGSettingsProperty, |
1239 | + ConstructionSchemaId, ConstructionGSetting, |
1240 | + Cast, ChangedSignal, |
1241 | + OperatorEqCb, OperatorEqNoCb, |
1242 | + OperatorCallCb, OperatorCallNoCb, |
1243 | + GetCb, GetNoCb, |
1244 | + SetCb, SetNoCb); |
1245 | + |
1246 | +enum class EC { |
1247 | + E1 = 0, E2, E3 |
1248 | +}; |
1249 | + |
1250 | +enum E { |
1251 | + E1 = 0, E2, E3 |
1252 | +}; |
1253 | + |
1254 | +typedef ::testing::Types<bool, int, uint, double, std::string, std::vector<std::string>, EC, E> MyTypes; |
1255 | +INSTANTIATE_TYPED_TEST_CASE_P(TestGSettingsProperty, TestGSettingsProperty, MyTypes); |
1256 | + |
1257 | +// bool |
1258 | +template<> bool TestGSettingsProperty<bool>::initial_value = true; |
1259 | +template<> bool TestGSettingsProperty<bool>::target_value = false; |
1260 | +template<> std::string TestGSettingsProperty<bool>::key = "bool"; |
1261 | + |
1262 | +// int |
1263 | +template<> int TestGSettingsProperty<int>::initial_value = -123; |
1264 | +template<> int TestGSettingsProperty<int>::target_value = 456; |
1265 | +template<> std::string TestGSettingsProperty<int>::key = "int"; |
1266 | + |
1267 | +// uint |
1268 | +template<> uint TestGSettingsProperty<uint>::initial_value = 123; |
1269 | +template<> uint TestGSettingsProperty<uint>::target_value = 456; |
1270 | +template<> std::string TestGSettingsProperty<uint>::key = "uint"; |
1271 | + |
1272 | +// double |
1273 | +template<> double TestGSettingsProperty<double>::initial_value = 123.456; |
1274 | +template<> double TestGSettingsProperty<double>::target_value = 456.123; |
1275 | +template<> std::string TestGSettingsProperty<double>::key = "double"; |
1276 | + |
1277 | +// std::string |
1278 | +template<> std::string TestGSettingsProperty<std::string>::initial_value = "abcdef"; |
1279 | +template<> std::string TestGSettingsProperty<std::string>::target_value = "dlkasdf"; |
1280 | +template<> std::string TestGSettingsProperty<std::string>::key = "string"; |
1281 | + |
1282 | +// std::vector<std::string> |
1283 | +template<> std::vector<std::string> TestGSettingsProperty<std::vector<std::string>>::initial_value = {"a", "b", "c"}; |
1284 | +template<> std::vector<std::string> TestGSettingsProperty<std::vector<std::string>>::target_value = {"d", "e", "f"}; |
1285 | +template<> std::string TestGSettingsProperty<std::vector<std::string>>::key = "vector-string"; |
1286 | + |
1287 | +// enum class |
1288 | +template<> EC TestGSettingsProperty<EC>::initial_value = EC::E1; |
1289 | +template<> EC TestGSettingsProperty<EC>::target_value = EC::E2; |
1290 | +template<> std::string TestGSettingsProperty<EC>::key = "enum"; |
1291 | + |
1292 | +// enum |
1293 | +template<> E TestGSettingsProperty<E>::initial_value = E::E1; |
1294 | +template<> E TestGSettingsProperty<E>::target_value = E::E2; |
1295 | +template<> std::string TestGSettingsProperty<E>::key = "enum"; |
1296 | + |
1297 | +} |