~kelemeng/activity-log-manager/bug944362

« back to all changes in this revision

Viewing changes to src/remote.py

  • Committer: Bazaar Package Importer
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2011-05-16 23:41:37 UTC
  • Revision ID: james.westby@ubuntu.com-20110516234137-cqbb23suvxxo806s
Tags: upstream-0.8.0
Import upstream version 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -.- coding: utf-8 -.-
 
2
#
 
3
# Activity Log Manager
 
4
#
 
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>
 
9
#
 
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.
 
14
#
 
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.
 
19
#
 
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/>.
 
22
 
 
23
from itertools import imap
 
24
 
 
25
from zeitgeist.client import ZeitgeistClient
 
26
from zeitgeist.datamodel import Event, Subject
 
27
 
 
28
class ZeitgeistInterface:
 
29
 
 
30
        INCOGNITO = Event.new_for_values()
 
31
        INCOGNITO_TEMPLATE_ID = "block-all"
 
32
 
 
33
        def __init__(self):
 
34
                self._client = ZeitgeistClient()
 
35
                if self._client.get_version() < [0, 7, 99]:
 
36
                        raise SystemExit, _("Zeitgeist version 0.8 or later required.")
 
37
                
 
38
                self._blacklist = self._client._iface.get_extension('Blacklist',
 
39
                        'blacklist')
 
40
                self._all = None
 
41
 
 
42
        @property
 
43
        def all_templates(self):
 
44
                if self._all is None:
 
45
                        self._all = {}
 
46
                        # Connect to signals
 
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)
 
52
                return self._all
 
53
 
 
54
        def _template_added(self, blacklist_id, template):
 
55
                if blacklist_id not in self._all:
 
56
                        self._all[blacklist_id] = template
 
57
        
 
58
        def _template_removed(self, blacklist_id, template):
 
59
                if blacklist_id in self._all:
 
60
                        del self._all[blacklist_id]
 
61
        
 
62
        def add_blacklist_template(self, blacklist_id, template):
 
63
                """
 
64
                Add a new blacklist template.
 
65
                """
 
66
                self._blacklist.AddTemplate(blacklist_id, template)
 
67
                self._all[blacklist_id] = template
 
68
 
 
69
        def remove_blacklist_template(self, blacklist_id):
 
70
                """
 
71
                Remove the blacklist template for the provided template id.
 
72
                """
 
73
                self._blacklist.RemoveTemplate(blacklist_id)
 
74
                del self._all[blacklist_id]
 
75
 
 
76
        def get_incognito(self):
 
77
                """
 
78
                The blacklist template for incognito is matched against all the 
 
79
                blacklist items.
 
80
                """
 
81
                return any(imap(self.INCOGNITO.matches_template, self.all_templates.itervalues()))
 
82
 
 
83
        def set_incognito(self, enabled):
 
84
                if enabled:
 
85
                        self.add_blacklist_template(self.INCOGNITO_TEMPLATE_ID, self.INCOGNITO)
 
86
                else:
 
87
                        self.remove_blacklist_template(self.INCOGNITO_TEMPLATE_ID)
 
88
                        
 
89
        def remove_history(self, timerange, callback):
 
90
                last_ids = []
 
91
                def get_ids_callback(ids):
 
92
                        last_ids = ids
 
93
                        def remove_callback(timerange):
 
94
                                callback(last_ids)
 
95
                        self._client.delete_events(ids, remove_callback)
 
96
                self._client.find_event_ids_for_templates([], get_ids_callback, timerange, 2, 0)
 
97