3
* Copyright © 2011 Collabora Ltd.
4
* By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
5
* Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
7
* Based upon a Python implementation (2009-2011) by:
8
* Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
9
* Manish Sinha <manishsinha@ubuntu.com>
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU Lesser General Public License as published by
13
* the Free Software Foundation, either version 2.1 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28
[DBus (name = "org.gnome.zeitgeist.Blacklist")]
29
public interface RemoteBlacklist: Object
31
public abstract void add_template (string template_id,
32
[DBus (signature = "(asaasay)")] Variant event_template)
34
[DBus (signature = "a{s(asaasay)}")]
35
public abstract Variant get_templates () throws Error;
36
public abstract void remove_template (string template_id)
39
public signal void template_added (string template_id,
40
[DBus (signature = "s(asaasay)")] Variant event_template);
41
public signal void template_removed (string template_id,
42
[DBus (signature = "s(asassay)")] Variant event_template);
45
namespace BlacklistTemplates
47
private const string SIG_BLACKLIST = "a{s("+Utils.SIG_EVENT+")}";
49
private static HashTable<string, Event> from_variant (
50
Variant templates_variant)
52
var blacklist = new HashTable<string, Event> (str_hash, str_equal);
55
templates_variant.get_type_string () == SIG_BLACKLIST);
56
foreach (Variant template_variant in templates_variant)
58
VariantIter iter = template_variant.iterator ();
59
string template_id = iter.next_value ().get_string ();
60
// FIXME: throw exception upon error instead of aborting
61
Event template = new Event.from_variant (iter.next_value ());
62
blacklist.insert (template_id, template);
68
public static Variant to_variant (HashTable<string, Event> blacklist)
70
var vb = new VariantBuilder (new VariantType (SIG_BLACKLIST));
72
var iter = HashTableIter<string, Event> (blacklist);
75
while (iter.next (out template_id, out event_template))
77
vb.open (new VariantType ("{s("+Utils.SIG_EVENT+")}"));
78
vb.add ("s", template_id);
79
vb.add_value (event_template.to_variant ());
87
class Blacklist: Extension, RemoteBlacklist
89
private HashTable<string, Event> blacklist;
90
private uint registration_id;
99
// Restore previous blacklist from database, or create an empty one
100
Variant? templates = retrieve_config ("blacklist",
101
BlacklistTemplates.SIG_BLACKLIST);
102
if (templates != null)
103
blacklist = BlacklistTemplates.from_variant (templates);
105
blacklist = new HashTable<string, Event> (str_hash, str_equal);
107
// This will be called after bus is acquired, so it shouldn't block
110
var connection = Bus.get_sync (BusType.SESSION, null);
111
registration_id = connection.register_object<RemoteBlacklist> (
112
"/org/gnome/zeitgeist/blacklist", this);
116
warning ("%s", err.message);
120
public override void unload ()
124
var connection = Bus.get_sync (BusType.SESSION, null);
125
if (registration_id != 0)
127
connection.unregister_object (registration_id);
133
warning ("%s", err.message);
136
debug ("%s, this.ref_count = %u", Log.METHOD, this.ref_count);
139
private void flush ()
141
Variant v = BlacklistTemplates.to_variant (blacklist);
142
store_config ("blacklist", v);
145
public override void pre_insert_events (GenericArray<Event?> events,
148
for (int i = 0; i < events.length; i++)
150
if (events[i] == null) continue;
151
foreach (var tmpl in blacklist.get_values ())
153
if (events[i].matches_template (tmpl))
162
public void add_template (string template_id, Variant event_template)
165
Event template = new Event.from_variant (event_template);
166
blacklist.insert (template_id, template);
167
debug ("Added blacklist template: %s", template_id);
168
template_added (template_id, event_template);
172
public void remove_template (string template_id)
174
Event event_template = blacklist.lookup (template_id);
175
if (blacklist.remove (template_id))
177
debug ("Removed blacklist template: %s", template_id);
178
template_removed (template_id, event_template.to_variant ());
183
debug ("Blacklist template \"%s\" not found.", template_id);
187
public Variant get_templates ()
189
return BlacklistTemplates.to_variant (blacklist);
195
#if BUILTIN_EXTENSIONS
196
public static Type blacklist_init (TypeModule module)
199
public static Type extension_register (TypeModule module)
202
return typeof (Blacklist);
206
// vim:expandtab:ts=4:sw=4