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