|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog for importing bookmarks from other sources. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt, QSize |
|
13 from PyQt5.QtWidgets import QDialog, QListWidgetItem |
|
14 |
|
15 from E5Gui import E5MessageBox |
|
16 from E5Gui.E5PathPicker import E5PathPickerModes |
|
17 |
|
18 from .Ui_BookmarksImportDialog import Ui_BookmarksImportDialog |
|
19 |
|
20 from . import BookmarksImporters |
|
21 |
|
22 import Globals |
|
23 |
|
24 |
|
25 class BookmarksImportDialog(QDialog, Ui_BookmarksImportDialog): |
|
26 """ |
|
27 Class implementing a dialog for importing bookmarks from other sources. |
|
28 """ |
|
29 SourcesListIdRole = Qt.ItemDataRole.UserRole |
|
30 |
|
31 def __init__(self, parent=None): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param parent reference to the parent widget (QWidget) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.filePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
41 |
|
42 self.sourcesList.setIconSize(QSize(48, 48)) |
|
43 for icon, displayText, idText in BookmarksImporters.getImporters(): |
|
44 itm = QListWidgetItem(icon, displayText, self.sourcesList) |
|
45 itm.setData(self.SourcesListIdRole, idText) |
|
46 |
|
47 self.__currentPage = 0 |
|
48 self.__selectedSource = "" |
|
49 self.__topLevelBookmarkNode = None |
|
50 self.__sourceFile = "" |
|
51 self.__sourceDir = "" |
|
52 |
|
53 self.pagesWidget.setCurrentIndex(self.__currentPage) |
|
54 self.__enableNextButton() |
|
55 |
|
56 def __enableNextButton(self): |
|
57 """ |
|
58 Private slot to set the enabled state of the next button. |
|
59 """ |
|
60 if self.__currentPage == 0: |
|
61 self.nextButton.setEnabled( |
|
62 len(self.sourcesList.selectedItems()) == 1) |
|
63 elif self.__currentPage == 1: |
|
64 self.nextButton.setEnabled(self.filePicker.text() != "") |
|
65 |
|
66 @pyqtSlot() |
|
67 def on_sourcesList_itemSelectionChanged(self): |
|
68 """ |
|
69 Private slot to handle changes of the selection of the import source. |
|
70 """ |
|
71 self.__enableNextButton() |
|
72 |
|
73 @pyqtSlot(str) |
|
74 def on_filePicker_textChanged(self, txt): |
|
75 """ |
|
76 Private slot handling changes of the file to import bookmarks form. |
|
77 |
|
78 @param txt text of the line edit (string) |
|
79 """ |
|
80 self.__enableNextButton() |
|
81 |
|
82 @pyqtSlot() |
|
83 def on_nextButton_clicked(self): |
|
84 """ |
|
85 Private slot to switch to the next page. |
|
86 """ |
|
87 if self.sourcesList.currentItem() is None: |
|
88 return |
|
89 |
|
90 if self.__currentPage == 0: |
|
91 self.__selectedSource = self.sourcesList.currentItem().data( |
|
92 self.SourcesListIdRole) |
|
93 (pixmap, sourceName, self.__sourceFile, info, prompt, |
|
94 self.__sourceDir) = BookmarksImporters.getImporterInfo( |
|
95 self.__selectedSource) |
|
96 |
|
97 self.iconLabel.setPixmap(pixmap) |
|
98 self.importingFromLabel.setText( |
|
99 self.tr("<b>Importing from {0}</b>").format(sourceName)) |
|
100 self.fileLabel1.setText(info) |
|
101 self.fileLabel2.setText(prompt) |
|
102 self.standardDirLabel.setText( |
|
103 "<i>{0}</i>".format(self.__sourceDir)) |
|
104 |
|
105 self.nextButton.setText(self.tr("Finish")) |
|
106 |
|
107 self.__currentPage += 1 |
|
108 self.pagesWidget.setCurrentIndex(self.__currentPage) |
|
109 self.__enableNextButton() |
|
110 |
|
111 if self.__selectedSource == "ie": |
|
112 self.filePicker.setMode(E5PathPickerModes.DirectoryMode) |
|
113 else: |
|
114 self.filePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
115 if Globals.isMacPlatform(): |
|
116 fileFilter = "*{0}".format( |
|
117 os.path.splitext(self.__sourceFile)[1]) |
|
118 else: |
|
119 fileFilter = self.__sourceFile |
|
120 self.filePicker.setFilters(fileFilter) |
|
121 self.filePicker.setDefaultDirectory(self.__sourceDir) |
|
122 |
|
123 elif self.__currentPage == 1: |
|
124 if self.filePicker.text() == "": |
|
125 return |
|
126 |
|
127 importer = BookmarksImporters.getImporter(self.__selectedSource) |
|
128 importer.setPath(self.filePicker.text()) |
|
129 if importer.open(): |
|
130 self.__topLevelBookmarkNode = importer.importedBookmarks() |
|
131 if importer.error(): |
|
132 E5MessageBox.critical( |
|
133 self, |
|
134 self.tr("Error importing bookmarks"), |
|
135 importer.errorString()) |
|
136 return |
|
137 |
|
138 self.accept() |
|
139 |
|
140 @pyqtSlot() |
|
141 def on_cancelButton_clicked(self): |
|
142 """ |
|
143 Private slot documentation goes here. |
|
144 """ |
|
145 self.reject() |
|
146 |
|
147 def getImportedBookmarks(self): |
|
148 """ |
|
149 Public method to get the imported bookmarks. |
|
150 |
|
151 @return top level bookmark (BookmarkNode) |
|
152 """ |
|
153 return self.__topLevelBookmarkNode |