src/eric7/WebBrowser/Bookmarks/BookmarksImporters/FirefoxImporter.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 19
20 20
21 def getImporterInfo(sourceId): 21 def getImporterInfo(sourceId):
22 """ 22 """
23 Module function to get information for the given source id. 23 Module function to get information for the given source id.
24 24
25 @param sourceId id of the browser ("chrome" or "chromium") 25 @param sourceId id of the browser ("chrome" or "chromium")
26 @return tuple with an icon (QPixmap), readable name (string), name of 26 @return tuple with an icon (QPixmap), readable name (string), name of
27 the default bookmarks file (string), an info text (string), 27 the default bookmarks file (string), an info text (string),
28 a prompt (string) and the default directory of the bookmarks file 28 a prompt (string) and the default directory of the bookmarks file
29 (string) 29 (string)
30 @exception ValueError raised to indicate an invalid browser ID 30 @exception ValueError raised to indicate an invalid browser ID
31 """ 31 """
32 if sourceId != "firefox": 32 if sourceId != "firefox":
33 raise ValueError( 33 raise ValueError("Unsupported browser ID given ({0}).".format(sourceId))
34 "Unsupported browser ID given ({0}).".format(sourceId)) 34
35
36 if Globals.isWindowsPlatform(): 35 if Globals.isWindowsPlatform():
37 standardDir = os.path.expandvars( 36 standardDir = os.path.expandvars("%APPDATA%\\Mozilla\\Firefox\\Profiles")
38 "%APPDATA%\\Mozilla\\Firefox\\Profiles")
39 elif Globals.isMacPlatform(): 37 elif Globals.isMacPlatform():
40 standardDir = os.path.expanduser( 38 standardDir = os.path.expanduser(
41 "~/Library/Application Support/Firefox/Profiles") 39 "~/Library/Application Support/Firefox/Profiles"
40 )
42 else: 41 else:
43 standardDir = os.path.expanduser("~/.mozilla/firefox") 42 standardDir = os.path.expanduser("~/.mozilla/firefox")
44 return ( 43 return (
45 UI.PixmapCache.getPixmap("chrome"), 44 UI.PixmapCache.getPixmap("chrome"),
46 "Mozilla Firefox", 45 "Mozilla Firefox",
47 "places.sqlite", 46 "places.sqlite",
48 QCoreApplication.translate( 47 QCoreApplication.translate(
49 "FirefoxImporter", 48 "FirefoxImporter",
50 """Mozilla Firefox stores its bookmarks in the""" 49 """Mozilla Firefox stores its bookmarks in the"""
51 """ <b>places.sqlite</b> SQLite database. This file is""" 50 """ <b>places.sqlite</b> SQLite database. This file is"""
52 """ usually located in"""), 51 """ usually located in""",
52 ),
53 QCoreApplication.translate( 53 QCoreApplication.translate(
54 "FirefoxImporter", 54 "FirefoxImporter",
55 """Please choose the file to begin importing bookmarks."""), 55 """Please choose the file to begin importing bookmarks.""",
56 ),
56 standardDir, 57 standardDir,
57 ) 58 )
58 59
59 60
60 class FirefoxImporter(BookmarksImporter): 61 class FirefoxImporter(BookmarksImporter):
61 """ 62 """
62 Class implementing the Chrome bookmarks importer. 63 Class implementing the Chrome bookmarks importer.
63 """ 64 """
65
64 def __init__(self, sourceId="", parent=None): 66 def __init__(self, sourceId="", parent=None):
65 """ 67 """
66 Constructor 68 Constructor
67 69
68 @param sourceId source ID (string) 70 @param sourceId source ID (string)
69 @param parent reference to the parent object (QObject) 71 @param parent reference to the parent object (QObject)
70 """ 72 """
71 super().__init__(sourceId, parent) 73 super().__init__(sourceId, parent)
72 74
73 self.__fileName = "" 75 self.__fileName = ""
74 self.__db = None 76 self.__db = None
75 77
76 def setPath(self, path): 78 def setPath(self, path):
77 """ 79 """
78 Public method to set the path of the bookmarks file or directory. 80 Public method to set the path of the bookmarks file or directory.
79 81
80 @param path bookmarks file or directory (string) 82 @param path bookmarks file or directory (string)
81 """ 83 """
82 self.__fileName = path 84 self.__fileName = path
83 85
84 def open(self): 86 def open(self):
85 """ 87 """
86 Public method to open the bookmarks file. 88 Public method to open the bookmarks file.
87 89
88 @return flag indicating success (boolean) 90 @return flag indicating success (boolean)
89 """ 91 """
90 if not os.path.exists(self.__fileName): 92 if not os.path.exists(self.__fileName):
91 self._error = True 93 self._error = True
92 self._errorString = self.tr( 94 self._errorString = self.tr("File '{0}' does not exist.").format(
93 "File '{0}' does not exist." 95 self.__fileName
94 ).format(self.__fileName) 96 )
95 return False 97 return False
96 98
97 try: 99 try:
98 self.__db = sqlite3.connect(self.__fileName) 100 self.__db = sqlite3.connect(self.__fileName)
99 except sqlite3.DatabaseError as err: 101 except sqlite3.DatabaseError as err:
100 self._error = True 102 self._error = True
101 self._errorString = self.tr( 103 self._errorString = self.tr("Unable to open database.\nReason: {0}").format(
102 "Unable to open database.\nReason: {0}").format(str(err)) 104 str(err)
105 )
103 return False 106 return False
104 107
105 return True 108 return True
106 109
107 def importedBookmarks(self): 110 def importedBookmarks(self):
108 """ 111 """
109 Public method to get the imported bookmarks. 112 Public method to get the imported bookmarks.
110 113
111 @return imported bookmarks (BookmarkNode) 114 @return imported bookmarks (BookmarkNode)
112 """ 115 """
113 from ..BookmarkNode import BookmarkNode 116 from ..BookmarkNode import BookmarkNode
117
114 importRootNode = BookmarkNode(BookmarkNode.Root) 118 importRootNode = BookmarkNode(BookmarkNode.Root)
115 119
116 # step 1: build the hierarchy of bookmark folders 120 # step 1: build the hierarchy of bookmark folders
117 folders = {} 121 folders = {}
118 122
119 try: 123 try:
120 cursor = self.__db.cursor() 124 cursor = self.__db.cursor()
121 cursor.execute( 125 cursor.execute(
122 "SELECT id, parent, title FROM moz_bookmarks " 126 "SELECT id, parent, title FROM moz_bookmarks "
123 "WHERE type = 2 and title !=''") 127 "WHERE type = 2 and title !=''"
128 )
124 for row in cursor: 129 for row in cursor:
125 id_ = row[0] 130 id_ = row[0]
126 parent = row[1] 131 parent = row[1]
127 title = row[2] 132 title = row[2]
128 folder = ( 133 folder = (
129 BookmarkNode(BookmarkNode.Folder, folders[parent]) 134 BookmarkNode(BookmarkNode.Folder, folders[parent])
130 if parent in folders else 135 if parent in folders
131 BookmarkNode(BookmarkNode.Folder, importRootNode) 136 else BookmarkNode(BookmarkNode.Folder, importRootNode)
132 ) 137 )
133 folder.title = title.replace("&", "&&") 138 folder.title = title.replace("&", "&&")
134 folders[id_] = folder 139 folders[id_] = folder
135 except sqlite3.DatabaseError as err: 140 except sqlite3.DatabaseError as err:
136 self._error = True 141 self._error = True
137 self._errorString = self.tr( 142 self._errorString = self.tr("Unable to open database.\nReason: {0}").format(
138 "Unable to open database.\nReason: {0}").format(str(err)) 143 str(err)
144 )
139 return None 145 return None
140 146
141 try: 147 try:
142 cursor = self.__db.cursor() 148 cursor = self.__db.cursor()
143 cursor.execute( 149 cursor.execute(
144 "SELECT parent, title, fk, position FROM moz_bookmarks" 150 "SELECT parent, title, fk, position FROM moz_bookmarks"
145 " WHERE type = 1 and title != '' ORDER BY position") 151 " WHERE type = 1 and title != '' ORDER BY position"
152 )
146 for row in cursor: 153 for row in cursor:
147 parent = row[0] 154 parent = row[0]
148 title = row[1] 155 title = row[1]
149 placesId = row[2] 156 placesId = row[2]
150 157
151 cursor2 = self.__db.cursor() 158 cursor2 = self.__db.cursor()
152 cursor2.execute( 159 cursor2.execute(
153 "SELECT url FROM moz_places WHERE id = {0}" # secok 160 "SELECT url FROM moz_places WHERE id = {0}".format( # secok
154 .format(placesId)) 161 placesId
162 )
163 )
155 row2 = cursor2.fetchone() 164 row2 = cursor2.fetchone()
156 if row2: 165 if row2:
157 url = QUrl(row2[0]) 166 url = QUrl(row2[0])
158 if ( 167 if not title or url.isEmpty() or url.scheme() in ["place", "about"]:
159 not title or
160 url.isEmpty() or
161 url.scheme() in ["place", "about"]
162 ):
163 continue 168 continue
164 169
165 if parent in folders: 170 if parent in folders:
166 bookmark = BookmarkNode(BookmarkNode.Bookmark, 171 bookmark = BookmarkNode(BookmarkNode.Bookmark, folders[parent])
167 folders[parent])
168 else: 172 else:
169 bookmark = BookmarkNode(BookmarkNode.Bookmark, 173 bookmark = BookmarkNode(BookmarkNode.Bookmark, importRootNode)
170 importRootNode)
171 bookmark.url = url.toString() 174 bookmark.url = url.toString()
172 bookmark.title = title.replace("&", "&&") 175 bookmark.title = title.replace("&", "&&")
173 except sqlite3.DatabaseError as err: 176 except sqlite3.DatabaseError as err:
174 self._error = True 177 self._error = True
175 self._errorString = self.tr( 178 self._errorString = self.tr("Unable to open database.\nReason: {0}").format(
176 "Unable to open database.\nReason: {0}").format(str(err)) 179 str(err)
180 )
177 return None 181 return None
178 182
179 importRootNode.setType(BookmarkNode.Folder) 183 importRootNode.setType(BookmarkNode.Folder)
180 if self._id == "firefox": 184 if self._id == "firefox":
181 importRootNode.title = self.tr("Mozilla Firefox Import") 185 importRootNode.title = self.tr("Mozilla Firefox Import")
182 else: 186 else:
183 importRootNode.title = self.tr( 187 importRootNode.title = self.tr("Imported {0}").format(
184 "Imported {0}" 188 QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate)
185 ).format(QDate.currentDate().toString( 189 )
186 Qt.DateFormat.SystemLocaleShortDate))
187 return importRootNode 190 return importRootNode

eric ide

mercurial