1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an importer for Chrome bookmarks. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 import json |
|
14 |
|
15 from PyQt5.QtCore import QCoreApplication, QDate, Qt |
|
16 |
|
17 from .BookmarksImporter import BookmarksImporter |
|
18 |
|
19 import UI.PixmapCache |
|
20 import Globals |
|
21 |
|
22 |
|
23 def getImporterInfo(sourceId): |
|
24 """ |
|
25 Module function to get information for the given source id. |
|
26 |
|
27 @param sourceId id of the browser ("chrome" or "chromium") |
|
28 @return tuple with an icon (QPixmap), readable name (string), name of |
|
29 the default bookmarks file (string), an info text (string), |
|
30 a prompt (string) and the default directory of the bookmarks file |
|
31 (string) |
|
32 @exception ValueError raised to indicate an invalid browser ID |
|
33 """ |
|
34 if sourceId == "chrome": |
|
35 if Globals.isWindowsPlatform(): |
|
36 standardDir = os.path.expandvars( |
|
37 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" |
|
38 "User Data\\Default") |
|
39 elif Globals.isMacPlatform(): |
|
40 standardDir = os.path.expanduser( |
|
41 "~/Library/Application Support/Google/Chrome/Default") |
|
42 else: |
|
43 standardDir = os.path.expanduser("~/.config/google-chrome/Default") |
|
44 return ( |
|
45 UI.PixmapCache.getPixmap("chrome.png"), |
|
46 "Google Chrome", |
|
47 "Bookmarks", |
|
48 QCoreApplication.translate( |
|
49 "ChromeImporter", |
|
50 """Google Chrome stores its bookmarks in the""" |
|
51 """ <b>Bookmarks</b> text file. This file is usually""" |
|
52 """ located in"""), |
|
53 QCoreApplication.translate( |
|
54 "ChromeImporter", |
|
55 """Please choose the file to begin importing bookmarks."""), |
|
56 standardDir, |
|
57 ) |
|
58 elif sourceId == "chromium": |
|
59 if Globals.isWindowsPlatform(): |
|
60 standardDir = os.path.expandvars( |
|
61 "%USERPROFILE%\\AppData\\Local\\Google\\Chrome\\" |
|
62 "User Data\\Default") |
|
63 else: |
|
64 standardDir = os.path.expanduser("~/.config/chromium/Default") |
|
65 return ( |
|
66 UI.PixmapCache.getPixmap("chromium.png"), |
|
67 "Chromium", |
|
68 "Bookmarks", |
|
69 QCoreApplication.translate( |
|
70 "ChromeImporter", |
|
71 """Chromium stores its bookmarks in the <b>Bookmarks</b>""" |
|
72 """ text file. This file is usually located in"""), |
|
73 QCoreApplication.translate( |
|
74 "ChromeImporter", |
|
75 """Please choose the file to begin importing bookmarks."""), |
|
76 standardDir, |
|
77 ) |
|
78 else: |
|
79 raise ValueError( |
|
80 "Unsupported browser ID given ({0}).".format(sourceId)) |
|
81 |
|
82 |
|
83 class ChromeImporter(BookmarksImporter): |
|
84 """ |
|
85 Class implementing the Chrome bookmarks importer. |
|
86 """ |
|
87 def __init__(self, sourceId="", parent=None): |
|
88 """ |
|
89 Constructor |
|
90 |
|
91 @param sourceId source ID (string) |
|
92 @param parent reference to the parent object (QObject) |
|
93 """ |
|
94 super(ChromeImporter, self).__init__(sourceId, parent) |
|
95 |
|
96 self.__fileName = "" |
|
97 |
|
98 def setPath(self, path): |
|
99 """ |
|
100 Public method to set the path of the bookmarks file or directory. |
|
101 |
|
102 @param path bookmarks file or directory (string) |
|
103 """ |
|
104 self.__fileName = path |
|
105 |
|
106 def open(self): |
|
107 """ |
|
108 Public method to open the bookmarks file. |
|
109 |
|
110 @return flag indicating success (boolean) |
|
111 """ |
|
112 if not os.path.exists(self.__fileName): |
|
113 self._error = True |
|
114 self._errorString = self.tr( |
|
115 "File '{0}' does not exist.").format(self.__fileName) |
|
116 return False |
|
117 return True |
|
118 |
|
119 def importedBookmarks(self): |
|
120 """ |
|
121 Public method to get the imported bookmarks. |
|
122 |
|
123 @return imported bookmarks (BookmarkNode) |
|
124 """ |
|
125 try: |
|
126 f = open(self.__fileName, "r", encoding="utf-8") |
|
127 contents = json.load(f) |
|
128 f.close() |
|
129 except IOError as err: |
|
130 self._error = True |
|
131 self._errorString = self.tr( |
|
132 "File '{0}' cannot be read.\nReason: {1}")\ |
|
133 .format(self.__fileName, str(err)) |
|
134 return None |
|
135 |
|
136 from ..BookmarkNode import BookmarkNode |
|
137 importRootNode = BookmarkNode(BookmarkNode.Folder) |
|
138 if contents["version"] == 1: |
|
139 self.__processRoots(contents["roots"], importRootNode) |
|
140 |
|
141 if self._id == "chrome": |
|
142 importRootNode.title = self.tr("Google Chrome Import") |
|
143 elif self._id == "chromium": |
|
144 importRootNode.title = self.tr("Chromium Import") |
|
145 else: |
|
146 importRootNode.title = self.tr("Imported {0}")\ |
|
147 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate)) |
|
148 return importRootNode |
|
149 |
|
150 def __processRoots(self, data, rootNode): |
|
151 """ |
|
152 Private method to process the bookmark roots. |
|
153 |
|
154 @param data dictionary with the bookmarks data (dict) |
|
155 @param rootNode node to add the bookmarks to (BookmarkNode) |
|
156 """ |
|
157 for node in data.values(): |
|
158 if node["type"] == "folder": |
|
159 self.__generateFolderNode(node, rootNode) |
|
160 elif node["type"] == "url": |
|
161 self.__generateUrlNode(node, rootNode) |
|
162 |
|
163 def __generateFolderNode(self, data, rootNode): |
|
164 """ |
|
165 Private method to process a bookmarks folder. |
|
166 |
|
167 @param data dictionary with the bookmarks data (dict) |
|
168 @param rootNode node to add the bookmarks to (BookmarkNode) |
|
169 """ |
|
170 from ..BookmarkNode import BookmarkNode |
|
171 folder = BookmarkNode(BookmarkNode.Folder, rootNode) |
|
172 folder.title = data["name"].replace("&", "&&") |
|
173 for node in data["children"]: |
|
174 if node["type"] == "folder": |
|
175 self.__generateFolderNode(node, folder) |
|
176 elif node["type"] == "url": |
|
177 self.__generateUrlNode(node, folder) |
|
178 |
|
179 def __generateUrlNode(self, data, rootNode): |
|
180 """ |
|
181 Private method to process a bookmarks node. |
|
182 |
|
183 @param data dictionary with the bookmarks data (dict) |
|
184 @param rootNode node to add the bookmarks to (BookmarkNode) |
|
185 """ |
|
186 from ..BookmarkNode import BookmarkNode |
|
187 bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) |
|
188 bookmark.url = data["url"] |
|
189 bookmark.title = data["name"].replace("&", "&&") |
|