1
# -.- coding: utf-8 -.-
5
# Copyright © 2011 Collabora Ltd.
6
# By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
7
# By Seif Lotfy <seif@lotfy.com>
8
# Copyright © 2011 Manish Sinha <manishsinha@ubuntu.com>
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation; either version 2 of the License, or
13
# (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU Lesser General Public License
21
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
from itertools import imap
25
from zeitgeist.client import ZeitgeistClient
26
from zeitgeist.datamodel import Event, Subject, ResultType
28
__all__ = ['get_interface']
30
class ZeitgeistInterface:
33
self._client = ZeitgeistClient()
34
if self._client.get_version() < [0, 7, 99]:
35
raise SystemExit, _("Zeitgeist version 0.8 or later required.")
37
self._blacklist = self._client._iface.get_extension('Blacklist',
40
self._listeners = set()
43
def all_templates(self):
47
self._blacklist.connect('TemplateAdded', self._template_added)
48
self._blacklist.connect('TemplateRemoved', self._template_removed)
49
# And fetch the current templates
50
for blacklist_id, template in self._blacklist.GetTemplates().iteritems():
51
self._all[blacklist_id] = Event(template)
54
def _template_added(self, blacklist_id, template):
55
if blacklist_id not in self._all:
56
self._all[blacklist_id] = Event(template)
57
for listener in self._listeners:
58
listener.template_added(blacklist_id, template)
60
def _template_removed(self, blacklist_id, template):
61
if blacklist_id in self._all:
62
del self._all[blacklist_id]
63
for listener in self._listeners:
64
listener.template_removed(blacklist_id, template)
66
def add_blacklist_template(self, blacklist_id, template):
68
Add a new blacklist template.
70
self._blacklist.AddTemplate(blacklist_id, template)
71
self._all[blacklist_id] = Event(template)
73
def remove_blacklist_template(self, blacklist_id):
75
Remove the blacklist template for the provided template id.
77
self._blacklist.RemoveTemplate(blacklist_id)
78
if blacklist_id in self._all:
79
del self._all[blacklist_id]
81
def remove_history(self, timerange, callback):
83
Delete all events within the given timestamp and call the given
84
method with the removed event IDs upon completion.
87
def get_ids_callback(ids):
89
def remove_callback(timerange):
91
self._client.delete_events(ids, remove_callback)
92
self._client.find_event_ids_for_templates([], get_ids_callback,
95
def get_all_applications(self, callback):
97
Fetch a list of all .desktop files for which Zeitgeist currently
98
has events and pass it on to the given callback method.
100
def callback_wrapper(events):
103
actor = Event(event).get_actor()
104
if actor.startswith('application://'):
105
actors.add(actor[14:])
107
self._client.find_events_for_template(Event.new_for_values(),
108
callback_wrapper, result_type=ResultType.MostPopularActor)
110
def register_listener(self, obj):
112
Register an object to be notified whenever a blacklist is added
115
This will happen by calling its 'template_added' and 'template_removed'
118
self._listeners.add(obj)
120
class GenericBlacklist(object):
122
_add_listeners = None
123
_del_listeners = None
126
self._zg = get_interface()
127
self._zg.register_listener(self)
128
self._add_listeners = set()
129
self._del_listeners = set()
131
def _parse(self, template_id, template):
132
raise NotImplementedError
135
def _activate_listeners(listeners, *args, **kwargs):
136
for listener in listeners:
137
listener(*args, **kwargs)
139
def subscribe(self, add=None, remove=None):
141
self._add_listeners.add(add)
142
if remove is not None:
143
self._del_listeners.add(remove)
145
def template_added(self, template_id, template):
146
data = self._parse(template_id, Event(template))
148
self._activate_listeners(self._add_listeners, data)
150
def template_removed(self, template_id, template):
151
data = self._parse(template_id, Event(template))
153
self._activate_listeners(self._del_listeners, data)
155
class IncognitoBlacklist(GenericBlacklist):
157
INCOGNITO = Event.new_for_values()
158
INCOGNITO_TEMPLATE_ID = "block-all"
161
def _parse(cls, template_id, template):
162
if cls.INCOGNITO.matches_template(template):
166
def get_incognito(self):
168
Return whether there is a catch-all template or not.
170
This is done by matching the blacklist template for incognito
171
against all the rules in the blacklist.
173
return any(imap(self.INCOGNITO.matches_template,
174
self._zg.all_templates.itervalues()))
176
def set_incognito(self, enabled):
178
Add or remove a blacklist with a catch-all (empty) template that will
179
block everything from being logged.
182
self._zg.add_blacklist_template(self.INCOGNITO_TEMPLATE_ID,
185
self._zg.remove_blacklist_template(self.INCOGNITO_TEMPLATE_ID)
191
ZG = ZeitgeistInterface()