|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an importer for Apple Safari bookmarks. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import QCoreApplication, QDate, Qt |
|
13 |
|
14 from ..BookmarkNode import BookmarkNode |
|
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 @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 (string) |
|
31 """ |
|
32 if id == "safari": |
|
33 if Globals.isWindowsPlatform(): |
|
34 standardDir = os.path.expandvars( |
|
35 "%APPDATA%\\Apple Computer\\Safari") |
|
36 else: |
|
37 # TODO: changes this on the Mac |
|
38 ## standardDir = os.path.expanduser("~/.config/google-chrome/Default") |
|
39 standardDir = "" |
|
40 return ( |
|
41 UI.PixmapCache.getPixmap("safari.png"), |
|
42 "Apple Safari", |
|
43 "Bookmarks.plist", |
|
44 QCoreApplication.translate("SafariImporter", |
|
45 """Apple Safari stores its bookmarks in the <b>Bookmarks.plist</b> """ |
|
46 """file. This file is usually located in"""), |
|
47 QCoreApplication.translate("SafariImporter", |
|
48 """Please choose the file to begin importing bookmarks."""), |
|
49 standardDir, |
|
50 ) |
|
51 else: |
|
52 raise ValueError("Unsupported browser ID given ({0}).".format(id)) |
|
53 |
|
54 |
|
55 class SafariImporter(BookmarksImporter): |
|
56 """ |
|
57 Class implementing the Apple Safari bookmarks importer. |
|
58 """ |
|
59 def __init__(self, id="", parent=None): |
|
60 """ |
|
61 Constructor |
|
62 |
|
63 @param id source ID (string) |
|
64 @param parent reference to the parent object (QObject) |
|
65 """ |
|
66 super().__init__(id, parent) |
|
67 |
|
68 self.__fileName = "" |
|
69 |
|
70 def setPath(self, path): |
|
71 """ |
|
72 Public method to set the path of the bookmarks file or directory. |
|
73 |
|
74 @param path bookmarks file or directory (string) |
|
75 """ |
|
76 self.__fileName = path |
|
77 |
|
78 def open(self): |
|
79 """ |
|
80 Public method to open the bookmarks file. |
|
81 |
|
82 @return flag indicating success (boolean) |
|
83 """ |
|
84 if not os.path.exists(self.__fileName): |
|
85 self._error = True |
|
86 self._errorString = self.trUtf8("File '{0}' does not exist.")\ |
|
87 .format(self.__fileName) |
|
88 return False |
|
89 return True |
|
90 |
|
91 def importedBookmarks(self): |
|
92 """ |
|
93 Public method to get the imported bookmarks. |
|
94 |
|
95 @return imported bookmarks (BookmarkNode) |
|
96 """ |
|
97 try: |
|
98 bookmarksDict = binplistlib.readPlist(self.__fileName) |
|
99 except binplistlib.InvalidPlistException as err: |
|
100 self._error = True |
|
101 self._errorString = self.trUtf8( |
|
102 "Bookmarks file cannot be read.\nReason: {0}".format(str(err))) |
|
103 return None |
|
104 |
|
105 importRootNode = BookmarkNode(BookmarkNode.Folder) |
|
106 if bookmarksDict["WebBookmarkFileVersion"] == 1 and \ |
|
107 bookmarksDict["WebBookmarkType"] == "WebBookmarkTypeList": |
|
108 self.__processChildren(bookmarksDict["Children"], importRootNode) |
|
109 |
|
110 if self._id == "safari": |
|
111 importRootNode.title = self.trUtf8("Apple Safari Import") |
|
112 else: |
|
113 importRootNode.title = self.trUtf8("Imported {0}")\ |
|
114 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate)) |
|
115 return importRootNode |
|
116 |
|
117 def __processChildren(self, children, rootNode): |
|
118 """ |
|
119 Private method to process the list of children. |
|
120 |
|
121 @param children list of child nodes to be processed (list of dict) |
|
122 @param rootNode node to add the bookmarks to (BookmarkNode) |
|
123 """ |
|
124 for child in children: |
|
125 if child["WebBookmarkType"] == "WebBookmarkTypeList": |
|
126 folder = BookmarkNode(BookmarkNode.Folder, rootNode) |
|
127 folder.title = child["Title"].replace("&", "&&") |
|
128 if "Children" in child: |
|
129 self.__processChildren(child["Children"], folder) |
|
130 elif child["WebBookmarkType"] == "WebBookmarkTypeLeaf": |
|
131 url = child["URLString"] |
|
132 if url.startswith(("place:", "about:")): |
|
133 continue |
|
134 |
|
135 bookmark = BookmarkNode(BookmarkNode.Bookmark, rootNode) |
|
136 bookmark.url = url |
|
137 bookmark.title = child["URIDictionary"]["title"].replace("&", "&&") |