src/eric7/WebBrowser/Bookmarks/BookmarksImporters/XbelImporter.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
17 17
18 18
19 def getImporterInfo(sourceId): 19 def getImporterInfo(sourceId):
20 """ 20 """
21 Module function to get information for the given XBEL source id. 21 Module function to get information for the given XBEL source id.
22 22
23 @param sourceId id of the browser ("chrome" or "chromium") 23 @param sourceId id of the browser ("chrome" or "chromium")
24 @return tuple with an icon (QPixmap), readable name (string), name of 24 @return tuple with an icon (QPixmap), readable name (string), name of
25 the default bookmarks file (string), an info text (string), 25 the default bookmarks file (string), an info text (string),
26 a prompt (string) and the default directory of the bookmarks file 26 a prompt (string) and the default directory of the bookmarks file
27 (string) 27 (string)
28 @exception ValueError raised to indicate an invalid browser ID 28 @exception ValueError raised to indicate an invalid browser ID
29 """ 29 """
30 if sourceId not in ("e5browser", "konqueror", "xbel"): 30 if sourceId not in ("e5browser", "konqueror", "xbel"):
31 raise ValueError( 31 raise ValueError("Unsupported browser ID given ({0}).".format(sourceId))
32 "Unsupported browser ID given ({0}).".format(sourceId)) 32
33
34 if sourceId == "e5browser": 33 if sourceId == "e5browser":
35 from ..BookmarksManager import BookmarksManager 34 from ..BookmarksManager import BookmarksManager
35
36 bookmarksFile = BookmarksManager.getFileName() 36 bookmarksFile = BookmarksManager.getFileName()
37 return ( 37 return (
38 UI.PixmapCache.getPixmap("ericWeb48"), 38 UI.PixmapCache.getPixmap("ericWeb48"),
39 "eric Web Browser", 39 "eric Web Browser",
40 os.path.basename(bookmarksFile), 40 os.path.basename(bookmarksFile),
41 QCoreApplication.translate( 41 QCoreApplication.translate(
42 "XbelImporter", 42 "XbelImporter",
43 """eric Web Browser stores its bookmarks in the""" 43 """eric Web Browser stores its bookmarks in the"""
44 """ <b>{0}</b> XML file. This file is usually located in""" 44 """ <b>{0}</b> XML file. This file is usually located in""",
45 ).format(os.path.basename(bookmarksFile)), 45 ).format(os.path.basename(bookmarksFile)),
46 QCoreApplication.translate( 46 QCoreApplication.translate(
47 "XbelImporter", 47 "XbelImporter",
48 """Please choose the file to begin importing bookmarks."""), 48 """Please choose the file to begin importing bookmarks.""",
49 ),
49 os.path.dirname(bookmarksFile), 50 os.path.dirname(bookmarksFile),
50 ) 51 )
51 elif sourceId == "konqueror": 52 elif sourceId == "konqueror":
52 if os.path.exists(os.path.expanduser("~/.kde4")): 53 if os.path.exists(os.path.expanduser("~/.kde4")):
53 standardDir = os.path.expanduser("~/.kde4/share/apps/konqueror") 54 standardDir = os.path.expanduser("~/.kde4/share/apps/konqueror")
61 "bookmarks.xml", 62 "bookmarks.xml",
62 QCoreApplication.translate( 63 QCoreApplication.translate(
63 "XbelImporter", 64 "XbelImporter",
64 """Konqueror stores its bookmarks in the""" 65 """Konqueror stores its bookmarks in the"""
65 """ <b>bookmarks.xml</b> XML file. This file is usually""" 66 """ <b>bookmarks.xml</b> XML file. This file is usually"""
66 """ located in"""), 67 """ located in""",
68 ),
67 QCoreApplication.translate( 69 QCoreApplication.translate(
68 "XbelImporter", 70 "XbelImporter",
69 """Please choose the file to begin importing bookmarks."""), 71 """Please choose the file to begin importing bookmarks.""",
72 ),
70 standardDir, 73 standardDir,
71 ) 74 )
72 else: 75 else:
73 return ( 76 return (
74 UI.PixmapCache.getPixmap("xbel"), 77 UI.PixmapCache.getPixmap("xbel"),
75 "XBEL Bookmarks", 78 "XBEL Bookmarks",
76 QCoreApplication.translate( 79 QCoreApplication.translate("XbelImporter", "XBEL Bookmarks")
77 "XbelImporter", "XBEL Bookmarks") + " (*.xbel *.xml)", 80 + " (*.xbel *.xml)",
78 QCoreApplication.translate( 81 QCoreApplication.translate(
79 "XbelImporter", 82 "XbelImporter",
80 """You can import bookmarks from any browser that supports""" 83 """You can import bookmarks from any browser that supports"""
81 """ XBEL exporting. This file has usually the extension""" 84 """ XBEL exporting. This file has usually the extension"""
82 """ .xbel or .xml."""), 85 """ .xbel or .xml.""",
86 ),
83 QCoreApplication.translate( 87 QCoreApplication.translate(
84 "XbelImporter", 88 "XbelImporter",
85 """Please choose the file to begin importing bookmarks."""), 89 """Please choose the file to begin importing bookmarks.""",
90 ),
86 "", 91 "",
87 ) 92 )
88 93
89 94
90 class XbelImporter(BookmarksImporter): 95 class XbelImporter(BookmarksImporter):
91 """ 96 """
92 Class implementing the XBEL bookmarks importer. 97 Class implementing the XBEL bookmarks importer.
93 """ 98 """
99
94 def __init__(self, sourceId="", parent=None): 100 def __init__(self, sourceId="", parent=None):
95 """ 101 """
96 Constructor 102 Constructor
97 103
98 @param sourceId source ID (string) 104 @param sourceId source ID (string)
99 @param parent reference to the parent object (QObject) 105 @param parent reference to the parent object (QObject)
100 """ 106 """
101 super().__init__(sourceId, parent) 107 super().__init__(sourceId, parent)
102 108
103 self.__fileName = "" 109 self.__fileName = ""
104 110
105 def setPath(self, path): 111 def setPath(self, path):
106 """ 112 """
107 Public method to set the path of the bookmarks file or directory. 113 Public method to set the path of the bookmarks file or directory.
108 114
109 @param path bookmarks file or directory (string) 115 @param path bookmarks file or directory (string)
110 """ 116 """
111 self.__fileName = path 117 self.__fileName = path
112 118
113 def open(self): 119 def open(self):
114 """ 120 """
115 Public method to open the bookmarks file. 121 Public method to open the bookmarks file.
116 122
117 @return flag indicating success (boolean) 123 @return flag indicating success (boolean)
118 """ 124 """
119 if not os.path.exists(self.__fileName): 125 if not os.path.exists(self.__fileName):
120 self._error = True 126 self._error = True
121 self._errorString = self.tr( 127 self._errorString = self.tr("File '{0}' does not exist.").format(
122 "File '{0}' does not exist." 128 self.__fileName
123 ).format(self.__fileName) 129 )
124 return False 130 return False
125 return True 131 return True
126 132
127 def importedBookmarks(self): 133 def importedBookmarks(self):
128 """ 134 """
129 Public method to get the imported bookmarks. 135 Public method to get the imported bookmarks.
130 136
131 @return imported bookmarks (BookmarkNode) 137 @return imported bookmarks (BookmarkNode)
132 """ 138 """
133 from ..XbelReader import XbelReader 139 from ..XbelReader import XbelReader
134 140
135 reader = XbelReader() 141 reader = XbelReader()
136 importRootNode = reader.read(self.__fileName) 142 importRootNode = reader.read(self.__fileName)
137 143
138 if reader.error() != QXmlStreamReader.Error.NoError: 144 if reader.error() != QXmlStreamReader.Error.NoError:
139 self._error = True 145 self._error = True
140 self._errorString = self.tr( 146 self._errorString = self.tr(
141 """Error when importing bookmarks on line {0},""" 147 """Error when importing bookmarks on line {0},"""
142 """ column {1}:\n{2}""" 148 """ column {1}:\n{2}"""
143 ).format(reader.lineNumber(), 149 ).format(reader.lineNumber(), reader.columnNumber(), reader.errorString())
144 reader.columnNumber(),
145 reader.errorString())
146 return None 150 return None
147 151
148 from ..BookmarkNode import BookmarkNode 152 from ..BookmarkNode import BookmarkNode
153
149 importRootNode.setType(BookmarkNode.Folder) 154 importRootNode.setType(BookmarkNode.Folder)
150 if self._id == "e5browser": 155 if self._id == "e5browser":
151 importRootNode.title = self.tr("eric Web Browser Import") 156 importRootNode.title = self.tr("eric Web Browser Import")
152 elif self._id == "konqueror": 157 elif self._id == "konqueror":
153 importRootNode.title = self.tr("Konqueror Import") 158 importRootNode.title = self.tr("Konqueror Import")
154 elif self._id == "xbel": 159 elif self._id == "xbel":
155 importRootNode.title = self.tr("XBEL Import") 160 importRootNode.title = self.tr("XBEL Import")
156 else: 161 else:
157 importRootNode.title = self.tr( 162 importRootNode.title = self.tr("Imported {0}").format(
158 "Imported {0}" 163 QDate.currentDate().toString(Qt.DateFormat.SystemLocaleShortDate)
159 ).format(QDate.currentDate().toString( 164 )
160 Qt.DateFormat.SystemLocaleShortDate))
161 return importRootNode 165 return importRootNode

eric ide

mercurial