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