1
# -.- coding: utf-8 -.-
5
# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
6
# © 2011 Manish Sinha <manishsinha@ubuntu.com>
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License as published by
10
# the Free Software Foundation, either version 2.1 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Lesser General Public License for more details.
18
# You should have received a copy of the GNU Lesser General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
from xdg import BaseDirectory
28
from _zeitgeist.engine.datamodel import Event
29
from _zeitgeist.engine.extension import Extension
30
from _zeitgeist.engine import constants
32
log = logging.getLogger("zeitgeist.blacklist")
34
DATA_FILE = os.path.join(constants.DATA_PATH, "blacklist.json")
35
BLACKLIST_DBUS_OBJECT_PATH = "/org/gnome/zeitgeist/blacklist"
36
BLACKLIST_DBUS_INTERFACE = "org.gnome.zeitgeist.Blacklist"
38
class Blacklist(Extension, dbus.service.Object):
40
The Zeitgeist engine maintains a list of event templates that is known
41
as the blacklist. When inserting events via
42
:meth:`org.gnome.zeitgeist.Log.InsertEvents <_zeitgeist.engine.remote.RemoteInterface.InsertEvents>`
43
they will be checked against the blacklist templates and if they match
44
they will not be inserted in the log, and any matching monitors will not
47
The blacklist of the Zeitgeist engine has DBus object path
48
:const:`/org/gnome/zeitgeist/blacklist` under the bus name
49
:const:`org.gnome.zeitgeist.Engine`.
51
PUBLIC_METHODS = ["add_blacklist", "remove_blacklist", "get_blacklist"]
53
def __init__ (self, engine):
54
Extension.__init__(self, engine)
55
dbus.service.Object.__init__(self, dbus.SessionBus(),
56
BLACKLIST_DBUS_OBJECT_PATH)
57
if os.path.exists(DATA_FILE):
60
with open(DATA_FILE, "r") as data_file:
61
blacklist = json.load(data_file)
62
[self._blacklist.setdefault(key, Event(blacklist[key])) \
64
log.debug("Loaded blacklist config from %s" % DATA_FILE)
66
log.warn("Failed to load blacklist config file %s: %s" \
70
log.debug("No existing blacklist config found")
73
def pre_insert_event(self, event, sender):
74
for tmpl in self._blacklist.itervalues():
75
if event.matches_template(tmpl): return None
78
def _write_to_disk(self):
79
with open(DATA_FILE, "w") as data_file:
80
json.dump(self._blacklist, data_file)
83
def add_blacklist(self, blacklist_id, event_template):
84
Event._make_dbus_sendable(event_template)
85
self._blacklist[blacklist_id] = Event(event_template)
88
log.debug("Blacklist added: %s" % self._blacklist)
91
def remove_blacklist(self, blacklist_id):
92
event_template = self._blacklist[blacklist_id]
93
del self._blacklist[blacklist_id]
96
log.debug("Blacklist deleted: %s" % self._blacklist)
101
def get_blacklist(self):
102
return self._blacklist
104
@dbus.service.method(BLACKLIST_DBUS_INTERFACE,
105
in_signature="s("+constants.SIG_EVENT+")")
106
def AddTemplate(self, blacklist_id, event_template):
108
Adds a new :const:`event_template` to the blacklist. Events
109
matching the template will be blocked from insertion into the
110
log. It is still possible to find and look up events matching
111
the template which were inserted before it was banned.
113
:param blacklist_id: A string identifier for the blacklist template
114
:param event_template: the :class:`Events <zeitgeist.datamodel.Event>`
115
template to be blacklisted
117
self.add_blacklist(blacklist_id, Event(event_template))
118
self.TemplateAdded(blacklist_id, event_template)
120
@dbus.service.method(BLACKLIST_DBUS_INTERFACE,
122
out_signature="a{s("+constants.SIG_EVENT+")}")
123
def GetTemplates(self):
125
Get the current list of blacklist templates.
127
:returns: A dictionary of { string,
128
:class:`Event <zeitgeist.datamodel.Event>` }
130
return self.get_blacklist()
132
@dbus.service.method(BLACKLIST_DBUS_INTERFACE,
135
def RemoveTemplate(self, blacklist_id):
137
Remove the blacklist template identified by the provided blacklist_id.
139
:param blacklist_id: A string identifier for a blacklist template
142
event_template = self.remove_blacklist(blacklist_id)
143
self.TemplateRemoved(blacklist_id, event_template)
145
log.debug('Blacklist template identified as "%s" not found.' % blacklist_id)
147
@dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
148
signature="s("+constants.SIG_EVENT+")")
149
def TemplateAdded(self, blacklist_id, event_template):
151
This signal is emmitted when a template is added.
153
:param blacklist_id: The Id of the newly added blacklist template
154
:param event_template: The blacklist template which was added
158
@dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
159
signature="s("+constants.SIG_EVENT+")")
160
def TemplateRemoved(self, blacklist_id, event_template):
162
This signal is emmitted when a template is deleted.
164
:param blacklist_id: The Id of the Blacklist template which was removed
165
:param event_template: The blacklist template which was removed