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