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