aboutsummaryrefslogtreecommitdiff
path: root/lib/ibus-menu.vala
blob: a240f00b7fa785f050a391f3b4864b5cabbb9af7 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: William Hua <william.hua@canonical.com>
 */

public class Indicator.Keyboard.IBusMenu : MenuModel {

	private static uint radio_counter = 0;

	private IBus.PropList? properties;

	private Menu menu;
	private ActionMap? action_map;

	private string? radio_name;
	private SimpleAction? radio_action;
	private Gee.HashMap<string, IBus.Property> radio_properties;

	/* A list of the action names this menu registers. */
	private Gee.LinkedList<string> names;

	public IBusMenu (ActionMap? action_map = null, IBus.PropList? properties = null) {
		menu = new Menu ();

		menu.items_changed.connect ((position, removed, added) => {
			items_changed (position, removed, added);
		});

		names = new Gee.LinkedList<string> ();
		set_action_map (action_map);
		set_properties (properties);
	}

	~IBusMenu () {
		remove_actions ();
	}

	public signal void activate (IBus.Property property, IBus.PropState state);

	private string get_action_name (string key) {
		string name;

		if (!action_name_is_valid (key)) {
			var builder = new StringBuilder.sized (key.length + 1);

			unichar letter = 0;
			int index = 0;

			while (key.get_next_char (ref index, out letter)) {
				if (letter == '-' || letter == '.' || letter.isalnum ()) {
					builder.append_unichar (letter);
				} else {
					builder.append_c ('-');
				}
			}

			name = @"ibus-$(builder.str)";
		} else {
			name = @"ibus-$key";
		}

		/* Find an unused action name using a counter. */
		if (action_map != null && (Action?) ((!) action_map).lookup_action (name) != null) {
			var i = 0;
			var unique_name = @"$name-$i";

			while ((Action?) ((!) action_map).lookup_action (unique_name) != null) {
				i++;
				unique_name = @"$name-$i";
			}

			name = unique_name;
		}

		return name;
	}

	private string? get_label (IBus.Property property) {
		string? label = null;

		if ((IBus.Text?) property.label != null) {
			label = property.label.text;
		}

		if (label == null && (IBus.Text?) property.symbol != null) {
			label = property.symbol.text;
		}

		return label;
	}

	private void append_normal_property (IBus.Property property) {
		if (property.prop_type == IBus.PropType.NORMAL) {
			if ((string?) property.key != null) {
				var name = get_action_name (property.key);

				if (action_map != null) {
					var action = new SimpleAction (name, null);
					action.activate.connect ((parameter) => { activate (property, property.state); });
					((!) action_map).add_action (action);
					names.add (name);
				}

				menu.append (get_label (property), property.sensitive ? @"indicator.$name" : "-private-disabled");
			}
		}
	}

	private void append_toggle_property (IBus.Property property) {
		if (property.prop_type == IBus.PropType.TOGGLE) {
			if ((string?) property.key != null) {
				var name = get_action_name (property.key);

				if (action_map != null) {
					var state = new Variant.boolean (property.state == IBus.PropState.CHECKED);
					var action = new SimpleAction.stateful (name, null, state);

					action.change_state.connect ((value) => {
						if (value != null) {
							action.set_state ((!) value);
							activate (property, ((!) value).get_boolean () ? IBus.PropState.CHECKED : IBus.PropState.UNCHECKED);
						}
					});

					((!) action_map).add_action (action);
					names.add (name);
				}

				menu.append (get_label (property), property.sensitive ? @"indicator.$name" : "-private-disabled");
			}
		}
	}

	private void append_radio_property (IBus.Property property) {
		if (property.prop_type == IBus.PropType.RADIO) {
			if ((string?) property.key != null) {
				/* Create a single action for all radio properties. */
				if (action_map != null && radio_name == null) {
					radio_counter++;

					var name = @"-private-radio-$radio_counter";
					var action = new SimpleAction.stateful (name, VariantType.STRING, new Variant.string (""));

					action.change_state.connect ((value) => {
						if (value != null) {
							var key = ((!) value).get_string ();

							if (radio_properties.has_key (key)) {
								action.set_state ((!) value);
								activate (radio_properties[key], IBus.PropState.CHECKED);
							}
						}
					});

					((!) action_map).add_action (action);
					names.add (name);

					radio_name = name;
					radio_action = action;
				}

				radio_properties[property.key] = property;

				if (property.state == IBus.PropState.CHECKED) {
					((!) radio_action).change_state (new Variant.string (property.key));
				}

				var item = new MenuItem (get_label (property), "-private-disabled");

				if (property.sensitive) {
					item.set_action_and_target_value (@"indicator.$((!) radio_name)", new Variant.string (property.key));
				}

				menu.append_item (item);
			}
		}
	}

	private void append_menu_property (IBus.Property property) {
		if (property.prop_type == IBus.PropType.MENU) {
			var submenu = new IBusMenu (action_map, property.sub_props);
			submenu.activate.connect ((property, state) => { activate (property, state); });
			menu.append_submenu (get_label (property), submenu);
		}
	}

	private void append_property (IBus.Property? property) {
		if (property != null && ((!) property).visible) {
			switch (((!) property).prop_type) {
			case IBus.PropType.NORMAL:
				append_normal_property ((!) property);
				break;

			case IBus.PropType.TOGGLE:
				append_toggle_property ((!) property);
				break;

			case IBus.PropType.RADIO:
				append_radio_property ((!) property);
				break;

			case IBus.PropType.MENU:
				append_menu_property ((!) property);
				break;

			case IBus.PropType.SEPARATOR:
				break;
			}
		}
	}

	private void update_menu () {
		/* Break reference cycle between action map and submenus. */
		for (var i = 0; i < menu.get_n_items (); i++) {
			var submenu = menu.get_item_link (i, Menu.LINK_SUBMENU) as IBusMenu;

			if (submenu != null) {
				((!) submenu).remove_actions ();
			}
		}

		menu.remove_all ();

		if (properties != null) {
			for (var i = 0; i < ((!) properties).properties.length; i++) {
				append_property (((!) properties).get (i));
			}
		}
	}

	private void remove_actions () {
		radio_action = null;
		radio_name = null;

		if (action_map != null) {
			foreach (var name in names) {
				((!) action_map).remove_action (name);
			}
		}

		names.clear ();
	}

	public void set_action_map (ActionMap? action_map) {
		if (action_map != this.action_map) {
			remove_actions ();
			this.action_map = action_map;
			update_menu ();
		}
	}

	public void set_properties (IBus.PropList? properties) {
		if (properties != this.properties) {
			remove_actions ();
			radio_properties = new Gee.HashMap<string, IBus.Property> ();
			this.properties = properties;
			update_menu ();
		}
	}

	public void update_property (IBus.Property property) {
		remove_actions ();
		radio_properties = new Gee.HashMap<string, IBus.Property> ();
		update_menu ();
	}

	/* Forward all menu model calls to our internal menu. */

	public override Variant get_item_attribute_value (int item_index, string attribute, VariantType? expected_type) {
		return menu.get_item_attribute_value (item_index, attribute, expected_type);
	}

	public override void get_item_attributes (int item_index, out HashTable<string, Variant>? attributes) {
		menu.get_item_attributes (item_index, out attributes);
	}

	public override MenuModel get_item_link (int item_index, string link) {
		return menu.get_item_link (item_index, link);
	}

	public override void get_item_links (int item_index, out HashTable<string, MenuModel> links) {
		menu.get_item_links (item_index, out links);
	}

	public override int get_n_items () {
		return menu.get_n_items ();
	}

	public override bool is_mutable () {
		return menu.is_mutable ();
	}

	public override MenuAttributeIter iterate_item_attributes (int item_index) {
		return menu.iterate_item_attributes (item_index);
	}

	public override MenuLinkIter iterate_item_links (int item_index) {
		return menu.iterate_item_links (item_index);
	}
}