20 |
20 |
21 from Utilities.AutoSaver import AutoSaver |
21 from Utilities.AutoSaver import AutoSaver |
22 import UI.PixmapCache |
22 import UI.PixmapCache |
23 import Preferences |
23 import Preferences |
24 |
24 |
|
25 |
25 class DownloadManager(QDialog, Ui_DownloadManager): |
26 class DownloadManager(QDialog, Ui_DownloadManager): |
26 """ |
27 """ |
27 Class implementing the download manager |
28 Class implementing the download manager |
28 """ |
29 """ |
29 RemoveNever = 0 |
30 RemoveNever = 0 |
30 RemoveExit = 1 |
31 RemoveExit = 1 |
31 RemoveSuccessFullDownload = 2 |
32 RemoveSuccessFullDownload = 2 |
32 |
33 |
33 def __init__(self, parent = None): |
34 def __init__(self, parent=None): |
34 """ |
35 """ |
35 Constructor |
36 Constructor |
36 |
37 |
37 @param parent reference to the parent widget (QWidget) |
38 @param parent reference to the parent widget (QWidget) |
38 """ |
39 """ |
75 |
76 |
76 if selectedRowsCount == 1: |
77 if selectedRowsCount == 1: |
77 row = self.downloadsView.selectionModel().selectedRows()[0].row() |
78 row = self.downloadsView.selectionModel().selectedRows()[0].row() |
78 itm = self.__downloads[row] |
79 itm = self.__downloads[row] |
79 if itm.downloadCanceled(): |
80 if itm.downloadCanceled(): |
80 menu.addAction(UI.PixmapCache.getIcon("restart.png"), |
81 menu.addAction(UI.PixmapCache.getIcon("restart.png"), |
81 self.trUtf8("Retry"), self.__contextMenuRetry) |
82 self.trUtf8("Retry"), self.__contextMenuRetry) |
82 else: |
83 else: |
83 if itm.downloadedSuccessfully(): |
84 if itm.downloadedSuccessfully(): |
84 menu.addAction(UI.PixmapCache.getIcon("open.png"), |
85 menu.addAction(UI.PixmapCache.getIcon("open.png"), |
85 self.trUtf8("Open"), self.__contextMenuOpen) |
86 self.trUtf8("Open"), self.__contextMenuOpen) |
86 elif itm.downloading(): |
87 elif itm.downloading(): |
87 menu.addAction(UI.PixmapCache.getIcon("stopLoading.png"), |
88 menu.addAction(UI.PixmapCache.getIcon("stopLoading.png"), |
88 self.trUtf8("Cancel"), self.__contextMenuCancel) |
89 self.trUtf8("Cancel"), self.__contextMenuCancel) |
89 menu.addSeparator() |
90 menu.addSeparator() |
90 menu.addAction(self.trUtf8("Open Containing Folder"), |
91 menu.addAction(self.trUtf8("Open Containing Folder"), |
91 self.__contextMenuOpenFolder) |
92 self.__contextMenuOpenFolder) |
92 menu.addSeparator() |
93 menu.addSeparator() |
93 menu.addAction(self.trUtf8("Go to Download Page"), |
94 menu.addAction(self.trUtf8("Go to Download Page"), |
94 self.__contextMenuGotoPage) |
95 self.__contextMenuGotoPage) |
95 menu.addAction(self.trUtf8("Copy Download Link"), |
96 menu.addAction(self.trUtf8("Copy Download Link"), |
96 self.__contextMenuCopyLink) |
97 self.__contextMenuCopyLink) |
97 menu.addSeparator() |
98 menu.addSeparator() |
98 menu.addAction(self.trUtf8("Select All"), self.__contextMenuSelectAll) |
99 menu.addAction(self.trUtf8("Select All"), self.__contextMenuSelectAll) |
99 if selectedRowsCount > 1 or \ |
100 if selectedRowsCount > 1 or \ |
100 (selectedRowsCount == 1 and \ |
101 (selectedRowsCount == 1 and \ |
101 not self.__downloads[ |
102 not self.__downloads[ |
102 self.downloadsView.selectionModel().selectedRows()[0].row()]\ |
103 self.downloadsView.selectionModel().selectedRows()[0].row()]\ |
103 .downloading()): |
104 .downloading()): |
104 menu.addSeparator() |
105 menu.addSeparator() |
105 menu.addAction(self.trUtf8("Remove From List"), |
106 menu.addAction(self.trUtf8("Remove From List"), |
106 self.__contextMenuRemoveSelected) |
107 self.__contextMenuRemoveSelected) |
107 |
108 |
108 menu.exec_(QCursor.pos()) |
109 menu.exec_(QCursor.pos()) |
109 |
110 |
110 def shutdown(self): |
111 def shutdown(self): |
136 """ |
137 """ |
137 if self.activeDownloads() > 0: |
138 if self.activeDownloads() > 0: |
138 res = E5MessageBox.yesNo(self, |
139 res = E5MessageBox.yesNo(self, |
139 self.trUtf8(""), |
140 self.trUtf8(""), |
140 self.trUtf8("""There are %n downloads in progress.\n""" |
141 self.trUtf8("""There are %n downloads in progress.\n""" |
141 """Do you want to quit anyway?""", "", |
142 """Do you want to quit anyway?""", "", |
142 self.activeDownloads()), |
143 self.activeDownloads()), |
143 icon = E5MessageBox.Warning) |
144 icon=E5MessageBox.Warning) |
144 if not res: |
145 if not res: |
145 self.show() |
146 self.show() |
146 return False |
147 return False |
147 return True |
148 return True |
148 |
149 |
149 def download(self, requestOrUrl, requestFileName = False): |
150 def download(self, requestOrUrl, requestFileName=False): |
150 """ |
151 """ |
151 Public method to download a file. |
152 Public method to download a file. |
152 |
153 |
153 @param requestOrUrl reference to a request object (QNetworkRequest) |
154 @param requestOrUrl reference to a request object (QNetworkRequest) |
154 or a URL to be downloaded (QUrl) |
155 or a URL to be downloaded (QUrl) |
155 @keyparam requestFileName flag indicating to ask for the |
156 @keyparam requestFileName flag indicating to ask for the |
156 download file name (boolean) |
157 download file name (boolean) |
157 """ |
158 """ |
158 request = QNetworkRequest(requestOrUrl) |
159 request = QNetworkRequest(requestOrUrl) |
159 if request.url().isEmpty(): |
160 if request.url().isEmpty(): |
160 return |
161 return |
161 self.handleUnsupportedContent(self.__manager.get(request), |
162 self.handleUnsupportedContent(self.__manager.get(request), |
162 requestFileName = requestFileName, |
163 requestFileName=requestFileName, |
163 download = True) |
164 download=True) |
164 |
165 |
165 def handleUnsupportedContent(self, reply, requestFileName = False, |
166 def handleUnsupportedContent(self, reply, requestFileName=False, |
166 webPage = None, download = False): |
167 webPage=None, download=False): |
167 if reply is None or reply.url().isEmpty(): |
168 if reply is None or reply.url().isEmpty(): |
168 return |
169 return |
169 |
170 |
170 size = reply.header(QNetworkRequest.ContentLengthHeader) |
171 size = reply.header(QNetworkRequest.ContentLengthHeader) |
171 if size == 0: |
172 if size == 0: |
172 return |
173 return |
173 |
174 |
174 itm = DownloadItem(reply = reply, requestFilename = requestFileName, |
175 itm = DownloadItem(reply=reply, requestFilename=requestFileName, |
175 webPage = webPage, download = download, parent = self) |
176 webPage=webPage, download=download, parent=self) |
176 self.__addItem(itm) |
177 self.__addItem(itm) |
177 |
178 |
178 if itm.canceledFileSelect(): |
179 if itm.canceledFileSelect(): |
179 return |
180 return |
180 |
181 |
205 # just in case the download finished before the constructor returned |
206 # just in case the download finished before the constructor returned |
206 self.__updateRow(itm) |
207 self.__updateRow(itm) |
207 self.changeOccurred() |
208 self.changeOccurred() |
208 self.__updateActiveItemCount() |
209 self.__updateActiveItemCount() |
209 |
210 |
210 def __updateRow(self, itm = None): |
211 def __updateRow(self, itm=None): |
211 """ |
212 """ |
212 Private slot to update a download item. |
213 Private slot to update a download item. |
213 |
214 |
214 @param itm reference to the download item (DownloadItem) |
215 @param itm reference to the download item (DownloadItem) |
215 """ |
216 """ |
228 if icon.isNull(): |
229 if icon.isNull(): |
229 icon = self.style().standardIcon(QStyle.SP_FileIcon) |
230 icon = self.style().standardIcon(QStyle.SP_FileIcon) |
230 itm.setIcon(icon) |
231 itm.setIcon(icon) |
231 |
232 |
232 oldHeight = self.downloadsView.rowHeight(row) |
233 oldHeight = self.downloadsView.rowHeight(row) |
233 self.downloadsView.setRowHeight(row, |
234 self.downloadsView.setRowHeight(row, |
234 max(oldHeight, itm.minimumSizeHint().height())) |
235 max(oldHeight, itm.minimumSizeHint().height())) |
235 |
236 |
236 remove = False |
237 remove = False |
237 globalSettings = QWebSettings.globalSettings() |
238 globalSettings = QWebSettings.globalSettings() |
238 if not itm.downloading() and \ |
239 if not itm.downloading() and \ |
262 |
263 |
263 def setRemovePolicy(self, policy): |
264 def setRemovePolicy(self, policy): |
264 """ |
265 """ |
265 Public method to set the remove policy. |
266 Public method to set the remove policy. |
266 |
267 |
267 @param policy policy to be set |
268 @param policy policy to be set |
268 (DownloadManager.RemoveExit, DownloadManager.RemoveNever, |
269 (DownloadManager.RemoveExit, DownloadManager.RemoveNever, |
269 DownloadManager.RemoveSuccessFullDownload) |
270 DownloadManager.RemoveSuccessFullDownload) |
270 """ |
271 """ |
271 assert policy in (DownloadManager.RemoveExit, DownloadManager.RemoveNever, |
272 assert policy in (DownloadManager.RemoveExit, DownloadManager.RemoveNever, |
272 DownloadManager.RemoveSuccessFullDownload) |
273 DownloadManager.RemoveSuccessFullDownload) |
273 |
274 |
274 if policy == self.removePolicy(): |
275 if policy == self.removePolicy(): |
275 return |
276 return |
276 |
277 |
308 |
309 |
309 downloads = Preferences.getHelp("DownloadManagerDownloads") |
310 downloads = Preferences.getHelp("DownloadManagerDownloads") |
310 for download in downloads: |
311 for download in downloads: |
311 if not download[0].isEmpty() and \ |
312 if not download[0].isEmpty() and \ |
312 download[1] != "": |
313 download[1] != "": |
313 itm = DownloadItem(parent = self) |
314 itm = DownloadItem(parent=self) |
314 itm.setData(download) |
315 itm.setData(download) |
315 self.__addItem(itm) |
316 self.__addItem(itm) |
316 self.cleanupButton.setEnabled( |
317 self.cleanupButton.setEnabled( |
317 (len(self.__downloads) - self.activeDownloads()) > 0) |
318 (len(self.__downloads) - self.activeDownloads()) > 0) |
318 |
319 |