1 #pragma once
2 
3 #include "ieventmanager.h"
4 
5 #include <gdk/gdkkeysyms.h>
6 
7 #include "xmlutil/Node.h"
8 
9 #include "Accelerator.h"
10 
11 /* greebo: The visitor class that saves each visited event to the XMLRegistry
12  *
13  * The key the events are saved under is passed to the constructor of this class,
14  * for example: user/ui/input is passed.
15  *
16  * The resulting shortcut nodes are like: user/ui/input/shortcuts/shortcut
17  */
18 class SaveEventVisitor :
19 	public IEventVisitor
20 {
21 	const std::string _rootKey;
22 
23 	// The node containing all the <shortcut> tags
24 	xml::Node _shortcutsNode;
25 
26 	IEventManager* _eventManager;
27 
28 public:
SaveEventVisitor(const std::string & rootKey,IEventManager * eventManager)29 	SaveEventVisitor(const std::string& rootKey, IEventManager* eventManager) :
30 		_rootKey(rootKey),
31 		_shortcutsNode(NULL),
32 		_eventManager(eventManager)
33 	{
34 		// Remove any existing shortcut definitions
35 		GlobalRegistry().deleteXPath(_rootKey + "//shortcuts");
36 
37 		_shortcutsNode = GlobalRegistry().createKey(_rootKey + "/shortcuts");
38 	}
39 
~SaveEventVisitor()40 	~SaveEventVisitor() {
41 	}
42 
visit(const std::string & eventName,const IEvent * event)43 	void visit(const std::string& eventName, const IEvent* event) {
44 		// Only export events with non-empty name
45 		if (!eventName.empty()) {
46 			// Try to find an accelerator connected to this event
47 			Accelerator* accelerator = dynamic_cast<Accelerator*>(_eventManager->findAccelerator(event));
48 
49 			if (accelerator != NULL) {
50 				unsigned int keyVal = accelerator->getKey();
51 
52 				const std::string keyStr = (keyVal != 0) ? gdk_keyval_name(keyVal) : "";
53 				const std::string modifierStr = _eventManager->getModifierStr(accelerator->getModifiers());
54 
55 				// Create a new child under the _shortcutsNode
56 				xml::Node createdNode = _shortcutsNode.createChild("shortcut");
57 
58 				// Convert it into an xml::Node and set the attributes
59 				createdNode.setAttributeValue("command", eventName);
60 				createdNode.setAttributeValue("key", keyStr);
61 				createdNode.setAttributeValue("modifiers", modifierStr);
62 
63 				// Add some whitespace to the node (nicer output formatting)
64 				createdNode.addText("\n\t");
65 			}
66 		}
67 	}
68 
69 }; // class SaveEvent
70