~ubuntu-branches/debian/sid/gnome-activity-journal/sid

« back to all changes in this revision

Viewing changes to src/bookmarker.py

  • Committer: Bazaar Package Importer
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2010-01-19 19:02:56 UTC
  • Revision ID: james.westby@ubuntu.com-20100119190256-x0iws5zymbi2oitq
Tags: upstream-0.3.2
Import upstream version 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -.- coding: utf-8 -.-
 
2
#
 
3
# GNOME Activity Journal
 
4
#
 
5
# Copyright © 2009-2010 Seif Lotfy <seif@lotfy.com>
 
6
# Copyright © 2010 Siegfried Gevatter <siegfried@gevatter.com>
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
 
 
21
from __future__ import with_statement
 
22
import os
 
23
import cPickle
 
24
import gobject
 
25
import urllib
 
26
 
 
27
from config import DATA_PATH
 
28
 
 
29
def event_exists(uri):
 
30
        # TODO: Move this into Zeitgeist's datamodel.py
 
31
        return not uri.startswith("file://") or os.path.exists(
 
32
            urllib.unquote(str(uri[7:])))
 
33
 
 
34
class Bookmarker(gobject.GObject):
 
35
 
 
36
    __gsignals__ = {
 
37
        "reload" : (gobject.SIGNAL_RUN_FIRST,
 
38
                    gobject.TYPE_NONE,
 
39
                    (gobject.TYPE_PYOBJECT,))
 
40
        }
 
41
    
 
42
    # PUBLIC!
 
43
    bookmarks = []
 
44
    
 
45
    def __init__(self):
 
46
        gobject.GObject.__init__(self)
 
47
        self.bookmarks_file = os.path.join(DATA_PATH, "bookmarks.pickled")
 
48
        self._load()
 
49
    
 
50
    def _load(self):
 
51
        if os.path.isfile(self.bookmarks_file):
 
52
            try:
 
53
                with open(self.bookmarks_file) as f:
 
54
                    self.bookmarks = cPickle.load(f)
 
55
                    removable = []
 
56
                    for bookmark in self.bookmarks:
 
57
                        if not event_exists(bookmark):
 
58
                            removable.append(bookmark)
 
59
                    for uri in removable:
 
60
                        self.bookmarks.remove(uri)
 
61
            except BadPickleGet:
 
62
                print "Pin database is corrupt."
 
63
    
 
64
    def _save(self):
 
65
        with open(self.bookmarks_file, "w") as f:
 
66
            cPickle.dump(self.bookmarks, f)
 
67
    
 
68
    def bookmark(self, uri):
 
69
        if not uri in self.bookmarks and event_exists(uri):
 
70
            self.bookmarks.append(uri)
 
71
        self._save()
 
72
        self.emit("reload", self.bookmarks)
 
73
 
 
74
    def unbookmark(self, uri):
 
75
        if uri in self.bookmarks:
 
76
            self.bookmarks.remove(uri)
 
77
        self._save()
 
78
        self.emit("reload", self.bookmarks)
 
79
    
 
80
    def is_bookmarked(self, uri):
 
81
        return uri in self.bookmarks
 
82
 
 
83
bookmarker = Bookmarker()