|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an importer for Apple Safari bookmarks. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import QCoreApplication, QDate, Qt |
|
15 |
|
16 from .BookmarksImporter import BookmarksImporter |
|
17 |
|
18 import UI.PixmapCache |
|
19 import Globals |
|
20 |
|
21 from Utilities import binplistlib |
|
22 |
|
23 |
|
24 def getImporterInfo(id): |
|
25 """ |
|
26 Module function to get information for the given source id. |
|
27 |
|
28 @param id id of the browser ("chrome" or "chromium") |
|
29 @return tuple with an icon (QPixmap), readable name (string), name of |
|
30 the default bookmarks file (string), an info text (string), |
|
31 a prompt (string) and the default directory of the bookmarks file |
|
32 (string) |
|
33 @exception ValueError raised to indicate an invalid browser ID |
|
34 """ |
|
35 if id == "safari": |
|
36 if Globals.isWindowsPlatform(): |
|
37 standardDir = os.path.expandvars( |
|
38 "%APPDATA%\\Apple Computer\\Safari") |
|
39 elif Globals.isMacPlatform(): |
|
40 standardDir = os.path.expanduser("~/Library/Safari") |
|
41 else: |
|
42 standardDir = "" |
|
43 return ( |
|
44 UI.PixmapCache.getPixmap("safari.png"), |
|
45 "Apple Safari", |
|
46 "Bookmarks.plist", |
|
47 QCoreApplication.translate( |
|
48 "SafariImporter", |
|
49 """Apple Safari stores its bookmarks in the""" |
|
50 """ <b>Bookmarks.plist</b> file. This file is usually""" |
|
51 """ located in"""), |
|
52 QCoreApplication.translate( |
|
53 "SafariImporter", |
|
54 """Please choose the file to begin importing bookmarks."""), |
|
55 standardDir, |
|
56 ) |
|
57 else: |
|
58 raise ValueError("Unsupported browser ID given ({0}).".format(id)) |
|
59 |
|
60 |
|
61 class SafariImporter(BookmarksImporter): |
|
62 """ |
|
63 Class implementing the Apple Safari bookmarks importer. |
|
64 """ |
|
65 def __init__(self, id="", parent=None): |
|
66 """ |
|
67 Constructor |
|
68 |
|
69 @param id source ID (string) |
|
70 @param parent reference to the parent object (QObject) |
|
71 """ |
|
72 super(SafariImporter, self).__init__(id, parent) |
|
73 |
|
74 self.__fileName = "" |
|
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("File '{0}' does not exist.")\ |
|
93 .format(self.__fileName) |
|
94 return False |
|
95 return True |
|
96 |
|
97 def importedBookmarks(self): |
|
98 """ |
|
99 Public method to get the imported bookmarks. |
|
100 |
|
101 @return imported bookmarks (BookmarkNode) |
|
102 """ |
|
103 try: |
|
104 bookmarksDict = binplistlib.readPlist(self.__fileName) |
|
105 except binplistlib.InvalidPlistException as err: |
|
106 self._error = True |
|
107 self._errorString = self.tr( |
|
108 "Bookmarks file cannot be read.\nReason: {0}".format(str(err))) |
|
109 return None |
|
110 |
|
111 from ..BookmarkNode import BookmarkNode |
|
112 importRootNode = BookmarkNode(BookmarkNode.Folder) |
|
113 if bookmarksDict["WebBookmarkFileVersion"] == 1 and \ |
|
114 bookmarksDict["WebBookmarkType"] == "WebBookmarkTypeList": |
|
115 self.__processChildren(bookmarksDict["Children"], importRootNode) |
|
116 |
|
117 if self._id == "safari": |
|
118 importRootNode.title = self.tr("Apple Safari Import") |
|
119 else: |
|
120 importRootNode.title = self.tr("Imported {0}")\ |
|
121 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate)) |
|
122 return importRootNode |
|
123 |
|
124 def __processChildren(self, children, rootNode): |
|
125 """ |
|
126 Private method to process the list of children. |
|
127 |
|
128 @param children list of child nodes to be processed (list of dict) |
|
129 @param rootNode node to add the bookmarks to (BookmarkNode) |
|
130 """ |
|
131 from ..BookmarkNode import BookmarkNode |
|
132 for child in children: |
|
133 if child["WebBookmarkType"] == "WebBookmarkTypeList": |
|
134 folder = BookmarkNode(BookmarkNode.Folder, rootNode) |
|
135 folder.title = child["Title"].replace("&", "&&") |
|
136 if "Children" in child: |
|
137 self.__processChildren(child["Children"], folder) |
|
138 elif child["WebBookmarkType"] == "WebBookmarkTypeLeaf": |
|
139 url = child["URLString"] |
|
140 if url.startswith(("place:", "about:")): |
|
141 continue |
|
142 |
|
143 bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) |
|
144 bookmark.url = url |
|
145 bookmark.title = child["URIDictionary"]["title"]\ |
|
146 .replace("&", "&&") |