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