aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/grouplist.vala
blob: 932fc92d47a1d03d07f51bebe787accccd9fb419 (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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

using Gee;

namespace ContextProvider {

	internal class GroupList : GLib.Object {
		// List of groups
		Gee.ArrayList<Group> groups = new Gee.ArrayList<Group> ();

		// Valid keys provided by all groups
		public StringSet valid_keys {get; private set;}

		public GroupList () {
			valid_keys = new StringSet();
		}

		public void add (Group group) {
			//debug ("New group registered: %s (%p)", group.keys.debug(), group);
			groups.add (group);
			valid_keys = new StringSet.union (valid_keys, group.keys);
		}

		public void remove (Group group) {
			//debug ("Group unregistered: %s (%p)", group.keys.debug(), group);
			groups.remove (group);
			StringSet s = new StringSet();

			/* a bit inefficient, but it'll do for now...
			   TODO: make the stringset stuff not use ctors..
			 */
			foreach (var g in groups) {
				s = new StringSet.union (s, g.keys);
			}
			valid_keys = s;
		}

		public void first_subscribed(KeyUsageCounter? k, StringSet keys) {
			foreach (var group in groups) {
				StringSet intersection = new StringSet.intersection (keys, group.keys);
				if (intersection.size() > 0) {
					group.subscribe(true);
				}
			}
		}

		public void last_unsubscribed(KeyUsageCounter? k, StringSet keys_unsubscribed, StringSet keys_remaining) {
			foreach (var group in groups) {
				StringSet group_remaining = new StringSet.intersection (keys_remaining, group.keys);
				if (group_remaining.size() == 0) {
					group.subscribe (false);
				}
			}
		}
	}
}