summaryrefslogtreecommitdiff
path: root/extensions/blacklist.vala
blob: 23034e3d4d9a29302de32663e15374b87ae22765 (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
/* ds-registry.vala
 *
 * Copyright © 2011 Collabora Ltd.
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 *
 * Based upon a Python implementation (2009-2011) by:
 *  Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
 *  Manish Sinha <manishsinha@ubuntu.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Zeitgeist
{
    [DBus (name = "org.gnome.zeitgeist.Blacklist")]
    public interface RemoteBlacklist: Object
    {
        public abstract void add_template (string template_id,
            [DBus (signature = "(asaasay)")] Variant event_template)
            throws Error;
        [DBus (signature = "a{s(asaasay)}")]
        public abstract Variant get_templates () throws Error;
        public abstract void remove_template (string template_id)
            throws Error;

        public signal void template_added (string template_id,
            [DBus (signature = "(asaasay)")] Variant event_template);
        public signal void template_removed (string template_id,
            [DBus (signature = "(asassay)")] Variant event_template);
    }

    namespace BlacklistTemplates
    {
        private const string SIG_BLACKLIST = "a{s("+Utils.SIG_EVENT+")}";

        private static HashTable<string, Event> from_variant (
            Variant templates_variant)
        {
            var blacklist = new HashTable<string, Event> (str_hash, str_equal);

            warn_if_fail (
                templates_variant.get_type_string () == SIG_BLACKLIST);
            foreach (Variant template_variant in templates_variant)
            {
                VariantIter iter = template_variant.iterator ();
                string template_id = iter.next_value ().get_string ();
                // FIXME: throw exception upon error instead of aborting
                Event template = new Event.from_variant (iter.next_value ());
                blacklist.insert (template_id, template);
            }

            return blacklist;
        }

        public static Variant to_variant (HashTable<string, Event> blacklist)
        {
            var vb = new VariantBuilder (new VariantType (SIG_BLACKLIST));
            {
                var iter = HashTableIter<string, Event> (blacklist);
                string template_id;
                Event event_template;
                while (iter.next (out template_id, out event_template))
                {
                    vb.open (new VariantType ("{s("+Utils.SIG_EVENT+")}"));
                    vb.add ("s", template_id);
                    vb.add_value (event_template.to_variant ());
                    vb.close ();
                }
            }
            return vb.end ();
        }
    }

    class Blacklist: Extension, RemoteBlacklist
    {
        private HashTable<string, Event> blacklist;
        private uint registration_id;

        Blacklist ()
        {
            Object ();
        }

        construct
        {
            // Restore previous blacklist from database, or create an empty one
            Variant? templates = retrieve_config ("blacklist",
                BlacklistTemplates.SIG_BLACKLIST);
            if (templates != null)
                blacklist = BlacklistTemplates.from_variant (templates);
            else
                blacklist = new HashTable<string, Event> (str_hash, str_equal);

            // This will be called after bus is acquired, so it shouldn't block
            try
            {
                var connection = Bus.get_sync (BusType.SESSION, null);
                registration_id = connection.register_object<RemoteBlacklist> (
                    "/org/gnome/zeitgeist/blacklist", this);
            }
            catch (Error err)
            {
                warning ("%s", err.message);
            }
        }

        public override void unload ()
        {
            try
            {
                var connection = Bus.get_sync (BusType.SESSION, null);
                if (registration_id != 0)
                {
                    connection.unregister_object (registration_id);
                    registration_id = 0;
                }
            }
            catch (Error err)
            {
                warning ("%s", err.message);
            }

            debug ("%s, this.ref_count = %u", Log.METHOD, this.ref_count);
        }

        private void flush ()
        {
            Variant v = BlacklistTemplates.to_variant (blacklist);
            store_config ("blacklist", v);
        }

        public override void pre_insert_events (GenericArray<Event?> events,
            BusName? sender)
        {
            for (int i = 0; i < events.length; i++)
            {
                if (events[i] == null) continue;
                foreach (var tmpl in blacklist.get_values ())
                {
                    if (events[i].matches_template (tmpl))
                    {
                        events[i] = null;
                        break;
                    }
                }
            }
        }

        public void add_template (string template_id, Variant event_template)
            throws EngineError
        {
            Event template = new Event.from_variant (event_template);
            blacklist.insert (template_id, template);
            debug ("Added blacklist template: [#%u]", template_id.hash ());
            template_added (template_id, event_template);
            flush ();
        }

        public void remove_template (string template_id)
        {
            Event event_template = blacklist.lookup (template_id);
            if (blacklist.remove (template_id))
            {
                debug ("Removed blacklist template: [#%u]", template_id.hash ());
                template_removed (template_id, event_template.to_variant ());
                flush ();
            }
            else
            {
                debug ("Blacklist template [#%u] not found.", template_id.hash ());
            }
        }

        public Variant get_templates ()
        {
            return BlacklistTemplates.to_variant (blacklist);
        }

    }

    [ModuleInit]
#if BUILTIN_EXTENSIONS
    public static Type blacklist_init (TypeModule module)
    {
#else
    public static Type extension_register (TypeModule module)
    {
#endif
        return typeof (Blacklist);
    }
}

// vim:expandtab:ts=4:sw=4