eric7/WebBrowser/Download/DownloadItem.py

branch
eric7
changeset 9153
506e35e424d5
parent 8881
54e42bc2437a
equal deleted inserted replaced
9152:8a68afaf1ba2 9153:506e35e424d5
7 Module implementing a widget controlling a download. 7 Module implementing a widget controlling a download.
8 """ 8 """
9 9
10 import enum 10 import enum
11 import os 11 import os
12 import pathlib
12 13
13 from PyQt6.QtCore import ( 14 from PyQt6.QtCore import (
14 pyqtSlot, pyqtSignal, QTime, QUrl, QStandardPaths, QFileInfo, QDateTime 15 pyqtSlot, pyqtSignal, QTime, QUrl, QStandardPaths, QDateTime
15 ) 16 )
16 from PyQt6.QtGui import QDesktopServices 17 from PyQt6.QtGui import QDesktopServices
17 from PyQt6.QtWidgets import QWidget, QStyle, QDialog 18 from PyQt6.QtWidgets import QWidget, QStyle, QDialog
18 from PyQt6.QtWebEngineCore import QWebEngineDownloadRequest 19 from PyQt6.QtWebEngineCore import QWebEngineDownloadRequest
19 20
178 if not savePage: 179 if not savePage:
179 from .DownloadAskActionDialog import DownloadAskActionDialog 180 from .DownloadAskActionDialog import DownloadAskActionDialog
180 url = self.__downloadRequest.url() 181 url = self.__downloadRequest.url()
181 mimetype = Utilities.MimeTypes.mimeType(originalFileName) 182 mimetype = Utilities.MimeTypes.mimeType(originalFileName)
182 dlg = DownloadAskActionDialog( 183 dlg = DownloadAskActionDialog(
183 QFileInfo(originalFileName).fileName(), 184 pathlib.Path(originalFileName).name,
184 mimetype, 185 mimetype,
185 "{0}://{1}".format(url.scheme(), url.authority()), 186 "{0}://{1}".format(url.scheme(), url.authority()),
186 self) 187 self)
187 188
188 if ( 189 if (
191 ): 192 ):
192 self.progressBar.setVisible(False) 193 self.progressBar.setVisible(False)
193 self.on_stopButton_clicked() 194 self.on_stopButton_clicked()
194 self.filenameLabel.setText( 195 self.filenameLabel.setText(
195 self.tr("Download canceled: {0}").format( 196 self.tr("Download canceled: {0}").format(
196 QFileInfo(defaultFileName).fileName())) 197 pathlib.Path(defaultFileName).name))
197 self.__canceledFileSelect = True 198 self.__canceledFileSelect = True
198 self.__setDateTime() 199 self.__setDateTime()
199 return 200 return
200 201
201 if dlg.getAction() == "scan": 202 if dlg.getAction() == "scan":
203 204
204 self.progressBar.setVisible(False) 205 self.progressBar.setVisible(False)
205 self.on_stopButton_clicked() 206 self.on_stopButton_clicked()
206 self.filenameLabel.setText( 207 self.filenameLabel.setText(
207 self.tr("VirusTotal scan scheduled: {0}").format( 208 self.tr("VirusTotal scan scheduled: {0}").format(
208 QFileInfo(defaultFileName).fileName())) 209 pathlib.Path(defaultFileName).name))
209 self.__canceledFileSelect = True 210 self.__canceledFileSelect = True
210 return 211 return
211 212
212 self.__autoOpen = dlg.getAction() == "open" 213 self.__autoOpen = dlg.getAction() == "open"
213 214
214 tempLocation = QStandardPaths.writableLocation( 215 tempLocation = QStandardPaths.writableLocation(
215 QStandardPaths.StandardLocation.TempLocation) 216 QStandardPaths.StandardLocation.TempLocation)
216 fileName = ( 217 fileName = (
217 tempLocation + '/' + 218 tempLocation + '/' +
218 QFileInfo(fileName).completeBaseName() 219 pathlib.Path(fileName).stem
219 ) 220 )
220 221
221 if ask and not self.__autoOpen: 222 if ask and not self.__autoOpen:
222 self.__gettingFileName = True 223 self.__gettingFileName = True
223 fileName = EricFileDialog.getSaveFileName( 224 fileName = EricFileDialog.getSaveFileName(
230 if not fileName: 231 if not fileName:
231 self.progressBar.setVisible(False) 232 self.progressBar.setVisible(False)
232 self.on_stopButton_clicked() 233 self.on_stopButton_clicked()
233 self.filenameLabel.setText( 234 self.filenameLabel.setText(
234 self.tr("Download canceled: {0}") 235 self.tr("Download canceled: {0}")
235 .format(QFileInfo(defaultFileName).fileName())) 236 .format(pathlib.Path(defaultFileName).name))
236 self.__canceledFileSelect = True 237 self.__canceledFileSelect = True
237 self.__setDateTime() 238 self.__setDateTime()
238 return 239 return
239 240
240 self.__setFileName(fileName) 241 self.__setFileName(fileName)
244 Private method to set the file name to save the download into. 245 Private method to set the file name to save the download into.
245 246
246 @param fileName name of the file to save into 247 @param fileName name of the file to save into
247 @type str 248 @type str
248 """ 249 """
249 fileInfo = QFileInfo(fileName) 250 fpath = pathlib.Path(fileName)
250 WebBrowserWindow.downloadManager().setDownloadDirectory( 251 WebBrowserWindow.downloadManager().setDownloadDirectory(
251 fileInfo.absoluteDir().absolutePath()) 252 fpath.parent.resolve())
252 self.filenameLabel.setText(fileInfo.fileName()) 253 self.filenameLabel.setText(fpath.name)
253 254
254 self.__fileName = fileName 255 self.__fileName = str(fpath)
255 256
256 # check file path for saving 257 # check file path for saving
257 saveDirPath = QFileInfo(self.__fileName).dir() 258 saveDirPath = pathlib.Path(self.__fileName).parent()
258 if ( 259 if not saveDirPath.exists():
259 not saveDirPath.exists() and 260 saveDirPath.mkdir(parents=True)
260 not saveDirPath.mkpath(saveDirPath.absolutePath())
261 ):
262 self.progressBar.setVisible(False)
263 self.on_stopButton_clicked()
264 self.infoLabel.setText(self.tr(
265 "Download directory ({0}) couldn't be created.")
266 .format(saveDirPath.absolutePath()))
267 self.__setDateTime()
268 return
269
270 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName())
271 261
272 def __saveFileName(self, directory): 262 def __saveFileName(self, directory):
273 """ 263 """
274 Private method to calculate a name for the file to download. 264 Private method to calculate a name for the file to download.
275 265
276 @param directory name of the directory to store the file into (string) 266 @param directory name of the directory to store the file into (string)
277 @return proposed filename and original filename (string, string) 267 @return proposed filename and original filename (string, string)
278 """ 268 """
279 path = self.__downloadRequest.downloadFileName() 269 fpath = pathlib.Path(self.__downloadRequest.downloadFileName())
280 info = QFileInfo(path) 270 origName = fpath.name
281 baseName = info.completeBaseName() 271 name = os.path.join(directory, origName)
282 endName = info.suffix()
283
284 origName = baseName
285 if endName:
286 origName += '.' + endName
287
288 name = os.path.join(directory, baseName)
289 if endName:
290 name += '.' + endName
291 return name, origName 272 return name, origName
292 273
293 @pyqtSlot(bool) 274 @pyqtSlot(bool)
294 def on_pauseButton_clicked(self, checked): 275 def on_pauseButton_clicked(self, checked):
295 """ 276 """
336 317
337 def openFile(self): 318 def openFile(self):
338 """ 319 """
339 Public slot to open the downloaded file. 320 Public slot to open the downloaded file.
340 """ 321 """
341 info = QFileInfo(self.__fileName) 322 url = QUrl.fromLocalFile(pathlib.Path(self.__fileName).resolve())
342 url = QUrl.fromLocalFile(info.absoluteFilePath())
343 QDesktopServices.openUrl(url) 323 QDesktopServices.openUrl(url)
344 324
345 def openFolder(self): 325 def openFolder(self):
346 """ 326 """
347 Public slot to open the folder containing the downloaded file. 327 Public slot to open the folder containing the downloaded file.
348 """ 328 """
349 info = QFileInfo(self.__fileName) 329 url = QUrl.fromLocalFile(pathlib.Path(self.__fileName).resolve())
350 url = QUrl.fromLocalFile(info.absolutePath())
351 QDesktopServices.openUrl(url) 330 QDesktopServices.openUrl(url)
352 331
353 @pyqtSlot() 332 @pyqtSlot()
354 def __downloadProgress(self): 333 def __downloadProgress(self):
355 """ 334 """
552 """ 531 """
553 Public method to get the absolute path of the output file. 532 Public method to get the absolute path of the output file.
554 533
555 @return absolute path of the output file (string) 534 @return absolute path of the output file (string)
556 """ 535 """
557 return QFileInfo(self.__fileName).absoluteFilePath() 536 return pathlib.Path(self.__fileName).resolve()
558 537
559 def getData(self): 538 def getData(self):
560 """ 539 """
561 Public method to get the relevant download data. 540 Public method to get the relevant download data.
562 541
566 @rtype dict of {"URL": QUrl, "Location": str, "Done": bool, 545 @rtype dict of {"URL": QUrl, "Location": str, "Done": bool,
567 "PageURL": QUrl, "Downloaded": QDateTime} 546 "PageURL": QUrl, "Downloaded": QDateTime}
568 """ 547 """
569 return { 548 return {
570 "URL": self.__url, 549 "URL": self.__url,
571 "Location": QFileInfo(self.__fileName).filePath(), 550 "Location": self.__fileName,
572 "Done": self.downloadedSuccessfully(), 551 "Done": self.downloadedSuccessfully(),
573 "PageURL": self.__pageUrl, 552 "PageURL": self.__pageUrl,
574 "Downloaded": self.__downloadedDateTime 553 "Downloaded": self.__downloadedDateTime
575 } 554 }
576 555
586 """ 565 """
587 self.__url = data["URL"] 566 self.__url = data["URL"]
588 self.__fileName = data["Location"] 567 self.__fileName = data["Location"]
589 self.__pageUrl = data["PageURL"] 568 self.__pageUrl = data["PageURL"]
590 569
591 self.filenameLabel.setText(QFileInfo(self.__fileName).fileName()) 570 self.filenameLabel.setText(pathlib.Path(self.__fileName).name)
592 self.infoLabel.setText(self.__fileName) 571 self.infoLabel.setText(self.__fileName)
593 572
594 try: 573 try:
595 self.__setDateTime(data["Downloaded"]) 574 self.__setDateTime(data["Downloaded"])
596 except KeyError: 575 except KeyError:

eric ide

mercurial