7 Module implementing a widget controlling a download. |
7 Module implementing a widget controlling a download. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTime, QFileInfo, QUrl, \ |
12 import os |
13 PYQT_VERSION_STR |
13 |
|
14 from PyQt5.QtCore import pyqtSlot, pyqtSignal, qVersion, Qt, QTime, QUrl, \ |
|
15 QStandardPaths, QFileInfo |
14 from PyQt5.QtGui import QPalette, QDesktopServices |
16 from PyQt5.QtGui import QPalette, QDesktopServices |
15 from PyQt5.QtWidgets import QWidget, QStyle, QDialog |
17 from PyQt5.QtWidgets import QWidget, QStyle, QDialog |
16 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem |
18 from PyQt5.QtWebEngineWidgets import QWebEngineDownloadItem |
17 |
19 |
18 from E5Gui import E5FileDialog |
20 from E5Gui import E5FileDialog |
119 Private method to get the file name to save to from the user. |
122 Private method to get the file name to save to from the user. |
120 """ |
123 """ |
121 if self.__gettingFileName: |
124 if self.__gettingFileName: |
122 return |
125 return |
123 |
126 |
|
127 if qVersion() >= "5.7.0": |
|
128 savePage = self.__downloadItem.savePageFormat() != \ |
|
129 QWebEngineDownloadItem.UnknownSaveFormat |
|
130 else: |
|
131 savePage = self.__downloadItem.path().lower().endswith( |
|
132 (".mhtml", ".mht")) |
|
133 |
|
134 documentLocation = QStandardPaths.writableLocation( |
|
135 QStandardPaths.DocumentsLocation) |
124 downloadDirectory = WebBrowserWindow\ |
136 downloadDirectory = WebBrowserWindow\ |
125 .downloadManager().downloadDirectory() |
137 .downloadManager().downloadDirectory() |
126 |
138 |
127 if self.__fileName: |
139 if self.__fileName: |
128 fileName = self.__fileName |
140 fileName = self.__fileName |
129 originalFileName = self.__originalFileName |
141 originalFileName = self.__originalFileName |
130 self.__toDownload = True |
142 self.__toDownload = True |
131 ask = False |
143 ask = False |
132 else: |
144 else: |
133 defaultFileName, originalFileName = \ |
145 defaultFileName, originalFileName = \ |
134 self.__saveFileName(downloadDirectory) |
146 self.__saveFileName( |
|
147 documentLocation if savePage else downloadDirectory) |
135 fileName = defaultFileName |
148 fileName = defaultFileName |
136 self.__originalFileName = originalFileName |
149 self.__originalFileName = originalFileName |
137 ask = True |
150 ask = True |
138 self.__autoOpen = False |
151 self.__autoOpen = False |
139 |
152 |
140 if not originalFileName.lower().endswith(".mhtml"): |
153 if not savePage: |
141 from .DownloadAskActionDialog import DownloadAskActionDialog |
154 from .DownloadAskActionDialog import DownloadAskActionDialog |
142 url = self.__downloadItem.url() |
155 url = self.__downloadItem.url() |
143 mimetype = Utilities.MimeTypes.mimeType(originalFileName) |
156 mimetype = Utilities.MimeTypes.mimeType(originalFileName) |
144 dlg = DownloadAskActionDialog( |
157 dlg = DownloadAskActionDialog( |
145 QFileInfo(originalFileName).fileName(), |
158 QFileInfo(originalFileName).fileName(), |
166 QFileInfo(defaultFileName).fileName())) |
179 QFileInfo(defaultFileName).fileName())) |
167 self.__canceledFileSelect = True |
180 self.__canceledFileSelect = True |
168 return |
181 return |
169 |
182 |
170 self.__autoOpen = dlg.getAction() == "open" |
183 self.__autoOpen = dlg.getAction() == "open" |
|
184 |
|
185 tempLocation = QStandardPaths.writableLocation( |
|
186 QStandardPaths.TempLocation) |
|
187 fileName = tempLocation + '/' + \ |
|
188 QFileInfo(fileName).completeBaseName() |
|
189 |
|
190 if ask and not self.__autoOpen: |
|
191 self.__gettingFileName = True |
|
192 fileName = E5FileDialog.getSaveFileName( |
|
193 None, |
|
194 self.tr("Save File"), |
|
195 defaultFileName, |
|
196 "") |
|
197 self.__gettingFileName = False |
171 else: |
198 else: |
172 self.__autoOpen = False |
199 self.__autoOpen = False |
173 |
200 |
174 if PYQT_VERSION_STR >= "5.0.0": |
201 filterList = [ |
175 from PyQt5.QtCore import QStandardPaths |
202 self.tr("Web Archive (*.mhtml *.mht)"), |
176 tempLocation = QStandardPaths.standardLocations( |
203 self.tr("HTML File (*.html *.htm)"), |
177 QStandardPaths.TempLocation)[0] |
204 self.tr("HTML File with all resources (*.html *.htm)"), |
178 else: |
205 ] |
179 from PyQt5.QtGui import QDesktopServices |
206 extensionsList = [ |
180 tempLocation = QDesktopServices.storageLocation( |
207 # tuple of extensions for *nix and Windows |
181 QDesktopServices.TempLocation) |
208 # keep in sync with filters list |
182 fileName = tempLocation + '/' + \ |
209 (".mhtml", ".mht"), |
183 QFileInfo(fileName).completeBaseName() |
210 (".html", ".htm"), |
184 |
211 (".html", ".htm"), |
185 if ask and not self.__autoOpen: |
212 ] |
186 self.__gettingFileName = True |
213 self.__gettingFileName = True |
187 fileName = E5FileDialog.getSaveFileName( |
214 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
188 None, |
215 None, |
189 self.tr("Save File"), |
216 self.tr("Save Web Page"), |
190 defaultFileName, |
217 defaultFileName, |
191 "") |
218 ";;".join(filterList), |
|
219 None) |
192 self.__gettingFileName = False |
220 self.__gettingFileName = False |
193 if not fileName: |
221 if fileName: |
194 self.progressBar.setVisible(False) |
222 index = filterList.index(selectedFilter) |
195 self.on_stopButton_clicked() |
223 if index == 0: |
196 self.filenameLabel.setText( |
224 self.__downloadItem.setSavePageFormat( |
197 self.tr("Download canceled: {0}") |
225 QWebEngineDownloadItem.MimeHtmlSaveFormat) |
198 .format(QFileInfo(defaultFileName).fileName())) |
226 elif index == 1: |
199 self.__canceledFileSelect = True |
227 self.__downloadItem.setSavePageFormat( |
200 return |
228 QWebEngineDownloadItem.SingleHtmlSaveFormat) |
|
229 else: |
|
230 self.__downloadItem.setSavePageFormat( |
|
231 QWebEngineDownloadItem.CompleteHtmlSaveFormat) |
|
232 extension = os.path.splitext(fileName)[1] |
|
233 if not extension: |
|
234 # add the platform specific default extension |
|
235 if Globals.isWindowsPlatform(): |
|
236 extensionsIndex = 1 |
|
237 else: |
|
238 extensionsIndex = 0 |
|
239 extensions = extensionsList[index] |
|
240 fileName += extensions[extensionsIndex] |
|
241 |
|
242 if not fileName: |
|
243 self.progressBar.setVisible(False) |
|
244 self.on_stopButton_clicked() |
|
245 self.filenameLabel.setText( |
|
246 self.tr("Download canceled: {0}") |
|
247 .format(QFileInfo(defaultFileName).fileName())) |
|
248 self.__canceledFileSelect = True |
|
249 return |
201 |
250 |
202 fileInfo = QFileInfo(fileName) |
251 fileInfo = QFileInfo(fileName) |
203 WebBrowserWindow.downloadManager()\ |
252 WebBrowserWindow.downloadManager()\ |
204 .setDownloadDirectory(fileInfo.absoluteDir().absolutePath()) |
253 .setDownloadDirectory(fileInfo.absoluteDir().absolutePath()) |
205 self.filenameLabel.setText(fileInfo.fileName()) |
254 self.filenameLabel.setText(fileInfo.fileName()) |