Skip to content

Commit d5db2fd

Browse files
authored
Merge pull request #2942 from devos50/fix_gui_freeze
Fix gui freeze
2 parents b4c10c2 + 1c88e05 commit d5db2fd

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

TriblerGUI/tribler_request_manager.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ class TriblerRequestManager(QNetworkAccessManager):
2020
window = None
2121

2222
received_json = pyqtSignal(object, int)
23-
received_file = pyqtSignal(str, object)
23+
received_file = pyqtSignal(object)
2424

2525
def __init__(self):
2626
QNetworkAccessManager.__init__(self)
27-
self.request_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
27+
self.request_id = None
2828
self.base_url = "http://localhost:%d/" % API_PORT
2929
self.reply = None
30+
self.generate_request_id()
31+
32+
def generate_request_id(self):
33+
self.request_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
3034

3135
def perform_request(self, endpoint, read_callback, data="", method='GET', capture_errors=True):
3236
"""
@@ -110,16 +114,23 @@ def on_finished(self, reply, capture_errors):
110114
self.received_json.emit(None, reply.error())
111115
logging.error("No json object could be decoded from data: %s" % data)
112116

117+
# We disconnect the slot since we want the finished only to be emitted once. This allows us to reuse the
118+
# request manager.
119+
try:
120+
self.finished.disconnect()
121+
self.received_json.disconnect()
122+
except TypeError:
123+
pass # We probably didn't have any connected slots.
124+
113125
def download_file(self, endpoint, read_callback):
114126
url = self.base_url + endpoint
115127
self.reply = self.get(QNetworkRequest(QUrl(url)))
116128
self.received_file.connect(read_callback)
117129
self.finished.connect(self.on_file_download_finished)
118130

119131
def on_file_download_finished(self, reply):
120-
content_header = str(reply.rawHeader("Content-Disposition"))
121132
data = reply.readAll()
122-
self.received_file.emit(content_header.split("=")[1], data)
133+
self.received_file.emit(data)
123134

124135
def show_error(self, error_text):
125136
main_text = "An error occurred during the request:\n\n%s" % error_text

TriblerGUI/widgets/downloadspage.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self):
3232
self.downloads_timeout_timer = QTimer()
3333
self.selected_item = None
3434
self.dialog = None
35-
self.downloads_request_mgr = None
35+
self.downloads_request_mgr = TriblerRequestManager()
3636
self.request_mgr = None
3737

3838
def initialize_downloads_page(self):
@@ -90,7 +90,7 @@ def load_downloads(self):
9090
if self.window().download_details_widget.currentIndex() == 3:
9191
url = "downloads?get_peers=1&get_pieces=1"
9292

93-
self.downloads_request_mgr = TriblerRequestManager()
93+
self.downloads_request_mgr.generate_request_id()
9494
self.downloads_request_mgr.perform_request(url, self.on_received_downloads)
9595

9696
def on_received_downloads(self, downloads):
@@ -289,9 +289,27 @@ def on_export_download(self):
289289
QFileDialog.ShowDirsOnly)
290290

291291
if len(self.export_dir) > 0:
292+
# Show confirmation dialog where we specify the name of the file
293+
infohash = self.selected_item.download_info['infohash']
294+
self.dialog = ConfirmationDialog(self, "Export torrent file",
295+
"Please enter the name of the torrent file:",
296+
[('SAVE', BUTTON_TYPE_NORMAL), ('CANCEL', BUTTON_TYPE_CONFIRM)],
297+
show_input=True)
298+
self.dialog.dialog_widget.dialog_input.setPlaceholderText('Torrent file name')
299+
self.dialog.dialog_widget.dialog_input.setText("%s.torrent" % infohash)
300+
self.dialog.dialog_widget.dialog_input.setFocus()
301+
self.dialog.button_clicked.connect(self.on_export_download_dialog_done)
302+
self.dialog.show()
303+
304+
def on_export_download_dialog_done(self, action):
305+
if action == 0:
306+
filename = self.dialog.dialog_widget.dialog_input.text()
292307
self.request_mgr = TriblerRequestManager()
293308
self.request_mgr.download_file("downloads/%s/torrent" % self.selected_item.download_info['infohash'],
294-
self.on_export_download_request_done)
309+
lambda data: self.on_export_download_request_done(filename, data))
310+
311+
self.dialog.setParent(None)
312+
self.dialog = None
295313

296314
def on_export_download_request_done(self, filename, data):
297315
dest_path = os.path.join(self.export_dir, filename)
@@ -304,7 +322,8 @@ def on_export_download_request_done(self, filename, data):
304322
"Error when exporting file",
305323
"An error occurred when exporting the torrent file: %s" % str(exc))
306324
else:
307-
self.window().tray_icon.showMessage("Torrent file exported", "Torrent file exported to %s" % dest_path)
325+
if QSystemTrayIcon.isSystemTrayAvailable():
326+
self.window().tray_icon.showMessage("Torrent file exported", "Torrent file exported to %s" % dest_path)
308327

309328
def on_right_click_item(self, pos):
310329
item_clicked = self.window().downloads_list.itemAt(pos)

0 commit comments

Comments
 (0)