~desrt/libvalmynd/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace Valmynd {
	public class MenuItem {
		internal HashTable<uint, Variant> attributes;
		internal HashTable<uint, Model> links;
		static Quark action_quark;
		static Quark target_quark;
		static Quark label_quark;
		static Quark type_quark;
		static Quark menu_quark;
		static bool initialised;

		void initialise () {
			if (!initialised) {
				// XXX: vala misses Quark.from_static_string
				action_quark = Quark.from_string ("action");
				target_quark = Quark.from_string ("target");
				label_quark = Quark.from_string ("label");
				type_quark = Quark.from_string ("type");
				menu_quark = Quark.from_string ("menu");

				initialised = true;
			}

			attributes = new HashTable<uint, Variant> (null, null);
			links = new HashTable<uint, Model> (null, null);
		}

		public Variant? get_qattribute_value (Quark name, VariantType? expected_type = null) {
			var value = attributes.lookup (name);

			if (expected_type != null && value != null && !value.is_of_type (expected_type)) {
				value = null;
			}

			return value;
		}

		public Variant? get_attribute_value (string name, VariantType? expected_type = null) {
			return get_qattribute_value (Quark.try_string (name), expected_type);
		}

		public void set_qattribute_value (Quark name, Variant? value) {
			if (value != null) {
				attributes.insert (name, value);
			} else {
				attributes.remove (name);
			}
		}

		public void set_attribute_value (string name, Variant? value) {
			set_qattribute_value (Quark.from_string (name), value);
		}

		public void set_qattribute (Quark name, string format_string, ...) {
			var ap = va_list ();
			set_qattribute_value (name, new Variant.va (format_string, null, &ap));
		}

		public void set_attribute (string name, string format_string, ...) {
			var ap = va_list ();
			set_attribute_value (name, new Variant.va (format_string, null, &ap));
		}

		public Model? get_qlink (Quark name) {
			return links.lookup (name);
		}

		public Model? get_link (string name) {
			return get_qlink (Quark.try_string (name));
		}

		public void set_qlink (Quark name, Model? link) {
			if (link != null) {
				links.insert (name, link);
			} else {
				links.remove (name);
			}
		}

		public void set_link (string name, Model? link) {
			set_qlink (Quark.from_string (name), link);
		}

		public void set_menu (Model? menu) {
			set_qlink (menu_quark, menu);
		}

		public void set_label (string? label) {
			set_qattribute_value (label_quark, label != null ? new Variant.string (label) : null);
		}

		public void set_action_and_target_value (string? action, Variant? target = null) {
			set_qattribute_value (action_quark, action != null ? new Variant.string (action) : null);
			set_qattribute_value (target_quark, target);
		}

		public void set_action_and_target (string? action, string? format_string, ...) {
			if (format_string != null) {
				var ap = va_list ();
				set_action_and_target_value (action, new Variant.va (format_string, null, &ap));
			} else {
				set_action_and_target_value (action);
			}
		}

		public void set_action (string? action) {
			set_action_and_target_value (action, null);
		}

		public void set_type_name (string? type) {
			if (type == "item") {
				type = null;
			}

			set_qattribute_value (type_quark, type != null ? new Variant.string (type) : null);
		}

		public void set_type (MenuItemType type) {
			switch (type) {
			case MenuItemType.ITEM:
				set_type_name (null);
				break;

			case MenuItemType.SECTION:
				set_type_name ("section");
				break;
			
			case MenuItemType.SUBMENU:
				set_type_name ("submenu");
				break;
			
			case MenuItemType.SEPARATOR:
				set_type_name ("separator");
				break;

			case MenuItemType.OTHER:
				return_if_fail (type != MenuItemType.OTHER);
				break;
			}
		}

		public MenuItem (string? label = null, string? detailed_action = null) {
			initialise ();

			if (label != null) {
				set_label (label);
			}

			if (detailed_action != null) {
				if ("::" in detailed_action) {
					var parts = detailed_action.split ("::", 2);
					set_action_and_target_value (parts[0], new Variant.string (parts[1]));
				} else {
					set_action (detailed_action);
				}
			}
		}

		public MenuItem.submenu (string label, Model menu) {
			initialise ();

			set_type (MenuItemType.SUBMENU);

			set_label (label);
			set_menu (menu);
		}

		public MenuItem.section (string? label, Model menu) {
			initialise ();

			set_type (MenuItemType.SECTION);

			if (label != null) {
				set_label (label);
			}

			set_menu (menu);
		}

		public MenuItem.other (string type) {
			initialise ();

			set_type_name (type);
		}
	}
}