1 #pragma once 2 3 #include "ieventmanager.h" 4 #include "iregistry.h" 5 #include "Toggle.h" 6 7 /* greebo: A RegistryToggle is an Toggle Event that changes the value of the 8 * attached registry key to "1" / "0" when toggled. The key is stored internally of course. 9 * 10 * The class functions as a RegistryKeyObserver, so it gets notified on key changes. 11 * 12 * The only method that is different to an ordinary Toggle is the virtual toggle() method, 13 * whose only purpose is to toggle the according RegistryKey. 14 */ 15 class RegistryToggle : 16 public Toggle, 17 public RegistryKeyObserver 18 { 19 // The attached registrykey 20 const std::string _registryKey; 21 22 public: 23 RegistryToggle(const std::string & registryKey)24 RegistryToggle(const std::string& registryKey) : 25 Toggle(MemberCaller<RegistryToggle, &RegistryToggle::doNothing>(*this)), 26 _registryKey(registryKey) 27 { 28 // Initialise the current state 29 _toggled = (GlobalRegistry().get(_registryKey) == "1"); 30 31 // Register self as KeyObserver to get notified on key changes 32 GlobalRegistry().addKeyObserver(this, _registryKey); 33 } 34 ~RegistryToggle()35 virtual ~RegistryToggle() {} 36 37 // Dummy callback for the Toggle base class, we don't need any callbacks... doNothing()38 void doNothing() {} 39 setToggled(const bool toggled)40 virtual bool setToggled(const bool toggled) { 41 // Set the registry key, this triggers the keyChanged() method 42 GlobalRegistry().set(_registryKey, toggled ? "1" : "0"); 43 44 return true; 45 } 46 47 // The RegistryKeyObserver implementation, gets called on key changes keyChanged(const std::string & changedKey,const std::string & newValue)48 void keyChanged(const std::string& changedKey, const std::string& newValue) { 49 // Update the internal toggle state according to the key value 50 _toggled = (GlobalRegistry().get(_registryKey) == "1"); 51 52 updateWidgets(); 53 } 54 toggle()55 virtual void toggle() { 56 if (_callbackActive) { 57 return; 58 } 59 60 // Check if the toggle event is enabled 61 if (_enabled) { 62 // Invert the registry key to <toggled> state 63 GlobalRegistry().set(_registryKey, (GlobalRegistry().get(_registryKey) == "1") ? "0" : "1"); 64 65 // The updates of the widgets are done by the triggered keyChanged() method 66 } 67 } 68 69 }; // class RegistryTogle 70