Helpviewer/Bookmarks/BookmarksImporters/FirefoxImporter.py

changeset 1716
d634df56a664
child 1719
c65aefefa2ff
equal deleted inserted replaced
1715:558e44df025a 1716:d634df56a664
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an importer for Firefox bookmarks.
8 """
9
10 import os
11
12 from PyQt4.QtCore import QCoreApplication, QDate, Qt, QUrl
13 from PyQt4.QtSql import QSqlDatabase, QSqlQuery
14
15 from ..BookmarkNode import BookmarkNode
16
17 from .BookmarksImporter import BookmarksImporter
18
19 import UI.PixmapCache
20 import Globals
21
22
23 def getImporterInfo(id):
24 """
25 Module function to get information for the given source id.
26
27 @return tuple with an icon (QPixmap), readable name (string), name of
28 the default bookmarks file (string), an info text (string),
29 a prompt (string) and the default directory of the bookmarks file (string)
30 """
31 if id == "firefox":
32 if Globals.isWindowsPlatform():
33 standardDir = os.path.expandvars(
34 "%APPDATA%\\Mozilla\\Firefox\\Profiles")
35 else:
36 standardDir = os.path.expanduser("~/.mozilla/firefox")
37 return (
38 UI.PixmapCache.getPixmap("chrome.png"),
39 "Mozilla Firefox",
40 "places.sqlite",
41 QCoreApplication.translate("FirefoxImporter",
42 """Mozilla Firefox stores its bookmarks in the <b>places.sqlite</b> """
43 """SQLite database. This file is usually located in"""),
44 QCoreApplication.translate("FirefoxImporter",
45 """Please choose the file to begin importing bookmarks."""),
46 standardDir,
47 )
48 else:
49 raise ValueError("Unsupported browser ID given ({0}).".format(id))
50
51
52 class FirefoxImporter(BookmarksImporter):
53 """
54 Class implementing the Chrome bookmarks importer.
55 """
56 def __init__(self, id="", parent=None):
57 """
58 Constructor
59
60 @param id source ID (string)
61 @param parent reference to the parent object (QObject)
62 """
63 super().__init__(id, parent)
64
65 self.__fileName = ""
66 self.__db = None
67
68 def setPath(self, path):
69 """
70 Public method to set the path of the bookmarks file or directory.
71
72 @param path bookmarks file or directory (string)
73 """
74 self.__fileName = path
75
76 def open(self):
77 """
78 Public method to open the bookmarks file.
79
80 @return flag indicating success (boolean)
81 """
82 if not os.path.exists(self.__fileName):
83 self._error = True
84 self._errorString = self.trUtf8("File '{0}' does not exist.")\
85 .format(self.__fileName)
86 return False
87
88 self.__db = QSqlDatabase.addDatabase("QSQLITE")
89 self.__db.setDatabaseName(self.__fileName)
90 opened = self.__db.open()
91
92 if not opened:
93 self._error = True
94 self._errorString = self.trUtf8("Unable to open database.\nReason: {0}")\
95 .format(self.__db.lastError().text())
96 return False
97
98 return True
99
100 def importedBookmarks(self):
101 """
102 Public method to get the imported bookmarks.
103
104 @return imported bookmarks (BookmarkNode)
105 """
106 importRootNode = BookmarkNode(BookmarkNode.Folder)
107
108 # step 1: build the hierarchy of bookmark folders
109 folders = {}
110 query = QSqlQuery(self.__db)
111 query.exec_(
112 "SELECT id, parent, title FROM moz_bookmarks WHERE type = 2 and title !=''")
113 while query.next():
114 id_ = int(query.value(0))
115 parent = int(query.value(1))
116 title = query.value(2)
117 if parent in folders:
118 folder = BookmarkNode(BookmarkNode.Folder, folders[parent])
119 else:
120 folder = BookmarkNode(BookmarkNode.Folder, importRootNode)
121 folder.title = title
122 folders[id_] = folder
123
124 query = QSqlQuery(self.__db)
125 query.exec_(
126 "SELECT parent, title, fk, position FROM moz_bookmarks"
127 " WHERE type = 1 and title != '' ORDER BY position")
128 while query.next():
129 parent = int(query.value(0))
130 title = query.value(1).replace("&", "&&")
131 placesId = int(query.value(2))
132
133 query2 = QSqlQuery(self.__db)
134 query2.exec_("SELECT url FROM moz_places WHERE id = {0}".format(placesId))
135 if not query2.next():
136 continue
137
138 url = QUrl(query2.value(0))
139 if not title or url.isEmpty() or url.scheme() in ["place", "about"]:
140 continue
141
142 if parent in folders:
143 bookmark = BookmarkNode(BookmarkNode.Bookmark, folders[parent])
144 else:
145 bookmark = BookmarkNode(BookmarkNode.Bookmark, importRootNode)
146 bookmark.url = url.toString()
147 bookmark.title = title
148
149 if query.lastError().isValid():
150 self._error = True
151 self._errorString = query.lastError().text()
152
153 if self._id == "firefox":
154 importRootNode.title = self.trUtf8("Mozilla Firefox Import")
155 else:
156 importRootNode.title = self.trUtf8("Imported {0}")\
157 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
158 return importRootNode

eric ide

mercurial