~ssh-rdp/deskbar-applet/history-count

« back to all changes in this revision

Viewing changes to src/browser_bookmarks_epiphany.py

  • Committer: rslinckx
  • Date: 2005-09-13 16:36:46 UTC
  • Revision ID: vcs-imports@canonical.com-20050913163646-gx8uamx1ea5564up
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os.path
 
2
import xml.sax
 
3
 
 
4
 
 
5
class EpiphanyFormatBookmarksParser(xml.sax.ContentHandler):
 
6
        def __init__(self, index):
 
7
                xml.sax.ContentHandler.__init__(self)
 
8
                self.chars = ""
 
9
                self.title = None
 
10
                self.href = None
 
11
                
 
12
                self.bookmarks = set()
 
13
                self.index = index
 
14
 
 
15
 
 
16
        def startElement(self, name, attrs):
 
17
                self.chars = ""
 
18
                if name == "item":
 
19
                        self.title = None
 
20
                        self.href = None
 
21
 
 
22
 
 
23
        def endElement(self, name):
 
24
                if name == "title":
 
25
                        self.title = self.chars
 
26
                elif name == "link":
 
27
                        if self.href == None:
 
28
                                self.href = self.chars
 
29
                elif name == "ephy:smartlink":
 
30
                        self.href = self.chars
 
31
                elif name == "item":
 
32
                        # We don't want bookmarks that are themselves queries
 
33
                        # such as "Search for %s"
 
34
                        if (not self.href.startswith("javascript:")) and (self.href.find("%s") == -1):
 
35
                                self.bookmarks.add((self.title, self.href))
 
36
 
 
37
 
 
38
        def characters(self, chars):
 
39
                self.chars = self.chars + chars
 
40
 
 
41
        
 
42
        def endDocument(self):
 
43
                self.bookmarks = list(self.bookmarks)
 
44
                # sort by titles (element 0 of the title,href tuple)
 
45
                self.bookmarks.sort(lambda x, y: cmp(x[0].lower(), y[0].lower()))
 
46
                for b in self.bookmarks:
 
47
                        self.index.add(b[0], b)
 
48
 
 
49
 
 
50
def add_to_index(index):
 
51
        bookmarks_file_name = os.path.expanduser("~/.gnome2/epiphany/bookmarks.rdf")
 
52
        if os.path.exists(bookmarks_file_name):
 
53
                parser = xml.sax.make_parser()
 
54
                parser.setContentHandler(EpiphanyFormatBookmarksParser(index))
 
55
                parser.parse(bookmarks_file_name)