~ubuntu-branches/ubuntu/quantal/zeitgeist/quantal

« back to all changes in this revision

Viewing changes to src/ext-blacklist.vala

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2011-11-15 11:15:56 UTC
  • mto: (6.2.2 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20111115111556-so7cmhfbqongw7hf
Tags: upstream-0.8.99~alpha1
Import upstream version 0.8.99~alpha1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ds-registry.vala
 
2
 *
 
3
 * Copyright © 2011 Collabora Ltd.
 
4
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 
5
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 
6
 *
 
7
 * Based upon a Python implementation (2009-2011) by:
 
8
 *  Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
 
9
 *  Manish Sinha <manishsinha@ubuntu.com>
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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/>.
 
23
 *
 
24
 */
 
25
 
 
26
namespace Zeitgeist
 
27
{
 
28
    [DBus (name = "org.gnome.zeitgeist.Blacklist")]
 
29
    public interface RemoteBlacklist: Object
 
30
    {
 
31
        public abstract void add_template (string template_id,
 
32
            [DBus (signature = "(asaasay)")] Variant event_template)
 
33
            throws Error;
 
34
        [DBus (signature = "a{s(asaasay)}")]
 
35
        public abstract Variant get_templates () throws Error;
 
36
        public abstract void remove_template (string template_id)
 
37
            throws Error;
 
38
 
 
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);
 
43
    }
 
44
 
 
45
    namespace BlacklistTemplates
 
46
    {
 
47
        private const string SIG_BLACKLIST = "a{s("+Utils.SIG_EVENT+")}";
 
48
 
 
49
        private static HashTable<string, Event> from_variant (
 
50
            Variant templates_variant)
 
51
        {
 
52
            var blacklist = new HashTable<string, Event> (str_hash, str_equal);
 
53
 
 
54
            warn_if_fail (
 
55
                templates_variant.get_type_string () == SIG_BLACKLIST);
 
56
            foreach (Variant template_variant in templates_variant)
 
57
            {
 
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);
 
63
            }
 
64
 
 
65
            return blacklist;
 
66
        }
 
67
 
 
68
        public static Variant to_variant (HashTable<string, Event> blacklist)
 
69
        {
 
70
            var vb = new VariantBuilder (new VariantType (SIG_BLACKLIST));
 
71
            {
 
72
                var iter = HashTableIter<string, Event> (blacklist);
 
73
                string template_id;
 
74
                Event event_template;
 
75
                while (iter.next (out template_id, out event_template))
 
76
                {
 
77
                    vb.open (new VariantType ("{s("+Utils.SIG_EVENT+")}"));
 
78
                    vb.add ("s", template_id);
 
79
                    vb.add_value (event_template.to_variant ());
 
80
                    vb.close ();
 
81
                }
 
82
            }
 
83
            return vb.end ();
 
84
        }
 
85
    }
 
86
 
 
87
    class Blacklist: Extension, RemoteBlacklist
 
88
    {
 
89
        private HashTable<string, Event> blacklist;
 
90
        private uint registration_id;
 
91
 
 
92
        Blacklist ()
 
93
        {
 
94
            Object ();
 
95
        }
 
96
 
 
97
        construct
 
98
        {
 
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);
 
104
            else
 
105
                blacklist = new HashTable<string, Event> (str_hash, str_equal);
 
106
 
 
107
            // This will be called after bus is acquired, so it shouldn't block
 
108
            try
 
109
            {
 
110
                var connection = Bus.get_sync (BusType.SESSION, null);
 
111
                registration_id = connection.register_object<RemoteBlacklist> (
 
112
                    "/org/gnome/zeitgeist/blacklist", this);
 
113
            }
 
114
            catch (Error err)
 
115
            {
 
116
                warning ("%s", err.message);
 
117
            }
 
118
        }
 
119
 
 
120
        public override void unload ()
 
121
        {
 
122
            try
 
123
            {
 
124
                var connection = Bus.get_sync (BusType.SESSION, null);
 
125
                if (registration_id != 0)
 
126
                {
 
127
                    connection.unregister_object (registration_id);
 
128
                    registration_id = 0;
 
129
                }
 
130
            }
 
131
            catch (Error err)
 
132
            {
 
133
                warning ("%s", err.message);
 
134
            }
 
135
 
 
136
            debug ("%s, this.ref_count = %u", Log.METHOD, this.ref_count);
 
137
        }
 
138
 
 
139
        private void flush ()
 
140
        {
 
141
            Variant v = BlacklistTemplates.to_variant (blacklist);
 
142
            store_config ("blacklist", v);
 
143
        }
 
144
 
 
145
        public override void pre_insert_events (GenericArray<Event?> events,
 
146
            BusName? sender)
 
147
        {
 
148
            for (int i = 0; i < events.length; i++)
 
149
            {
 
150
                if (events[i] == null) continue;
 
151
                foreach (var tmpl in blacklist.get_values ())
 
152
                {
 
153
                    if (events[i].matches_template (tmpl))
 
154
                    {
 
155
                        events[i] = null;
 
156
                        break;
 
157
                    }
 
158
                }
 
159
            }
 
160
        }
 
161
 
 
162
        public void add_template (string template_id, Variant event_template)
 
163
            throws EngineError
 
164
        {
 
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);
 
169
            flush ();
 
170
        }
 
171
 
 
172
        public void remove_template (string template_id)
 
173
        {
 
174
            Event event_template = blacklist.lookup (template_id);
 
175
            if (blacklist.remove (template_id))
 
176
            {
 
177
                debug ("Removed blacklist template: %s", template_id);
 
178
                template_removed (template_id, event_template.to_variant ());
 
179
                flush ();
 
180
            }
 
181
            else
 
182
            {
 
183
                debug ("Blacklist template \"%s\" not found.", template_id);
 
184
            }
 
185
        }
 
186
 
 
187
        public Variant get_templates ()
 
188
        {
 
189
            return BlacklistTemplates.to_variant (blacklist);
 
190
        }
 
191
 
 
192
    }
 
193
 
 
194
    [ModuleInit]
 
195
#if BUILTIN_EXTENSIONS
 
196
    public static Type blacklist_init (TypeModule module)
 
197
    {
 
198
#else
 
199
    public static Type extension_register (TypeModule module)
 
200
    {
 
201
#endif
 
202
        return typeof (Blacklist);
 
203
    }
 
204
}
 
205
 
 
206
// vim:expandtab:ts=4:sw=4