|
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 HTML bookmark files. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import QCoreApplication, 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 HTML 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 != "html": |
|
31 raise ValueError( |
|
32 "Unsupported browser ID given ({0}).".format(sourceId)) |
|
33 |
|
34 return ( |
|
35 UI.PixmapCache.getPixmap("html"), |
|
36 "HTML Netscape Bookmarks", |
|
37 QCoreApplication.translate( |
|
38 "HtmlImporter", |
|
39 "HTML Netscape Bookmarks") + " (*.htm *.html)", |
|
40 QCoreApplication.translate( |
|
41 "HtmlImporter", |
|
42 """You can import bookmarks from any browser that supports""" |
|
43 """ HTML exporting. This file has usually the extension""" |
|
44 """ .htm or .html."""), |
|
45 QCoreApplication.translate( |
|
46 "HtmlImporter", |
|
47 """Please choose the file to begin importing bookmarks."""), |
|
48 "", |
|
49 ) |
|
50 |
|
51 |
|
52 class HtmlImporter(BookmarksImporter): |
|
53 """ |
|
54 Class implementing the HTML bookmarks importer. |
|
55 """ |
|
56 def __init__(self, sourceId="", parent=None): |
|
57 """ |
|
58 Constructor |
|
59 |
|
60 @param sourceId source ID (string) |
|
61 @param parent reference to the parent object (QObject) |
|
62 """ |
|
63 super().__init__(sourceId, parent) |
|
64 |
|
65 self.__fileName = "" |
|
66 self.__inFile = None |
|
67 |
|
68 def setPath(self, path): |
|
69 """ |
|
70 Public method to set the path of the bookmarks file or directory. |
|
71 |
|
72 @param path bookmarks file or directory (string) |
|
73 """ |
|
74 self.__fileName = path |
|
75 |
|
76 def open(self): |
|
77 """ |
|
78 Public method to open the bookmarks file. |
|
79 |
|
80 @return flag indicating success (boolean) |
|
81 """ |
|
82 if not os.path.exists(self.__fileName): |
|
83 self._error = True |
|
84 self._errorString = self.tr( |
|
85 "File '{0}' does not exist." |
|
86 ).format(self.__fileName) |
|
87 return False |
|
88 return True |
|
89 |
|
90 def importedBookmarks(self): |
|
91 """ |
|
92 Public method to get the imported bookmarks. |
|
93 |
|
94 @return imported bookmarks (BookmarkNode) |
|
95 """ |
|
96 from ..BookmarkNode import BookmarkNode |
|
97 from ..NsHtmlReader import NsHtmlReader |
|
98 |
|
99 reader = NsHtmlReader() |
|
100 importRootNode = reader.read(self.__fileName) |
|
101 |
|
102 importRootNode.setType(BookmarkNode.Folder) |
|
103 if self._id == "html": |
|
104 importRootNode.title = self.tr("HTML Import") |
|
105 else: |
|
106 importRootNode.title = self.tr( |
|
107 "Imported {0}" |
|
108 ).format(QDate.currentDate().toString( |
|
109 Qt.DateFormat.SystemLocaleShortDate)) |
|
110 return importRootNode |