Skip to content

Commit

Permalink
Fix adding torrent to personal channel after creating torrent
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Jun 4, 2021
1 parent 72bbf49 commit 3165a72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
21 changes: 3 additions & 18 deletions src/tribler-gui/tribler_gui/dialogs/createtorrentdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from PyQt5.QtCore import QDir, pyqtSignal
from PyQt5.QtWidgets import QAction, QFileDialog, QSizePolicy, QTreeWidgetItem

from tribler_core.utilities.unicode import ensure_unicode

from tribler_gui.defs import BUTTON_TYPE_NORMAL
from tribler_gui.dialogs.confirmationdialog import ConfirmationDialog
from tribler_gui.dialogs.dialogcontainer import DialogContainer
Expand All @@ -22,6 +20,7 @@ def __init__(self, parent):
class CreateTorrentDialog(DialogContainer):

create_torrent_notification = pyqtSignal(dict)
add_to_channel_selected = pyqtSignal(str)

def __init__(self, parent):
DialogContainer.__init__(self, parent)
Expand Down Expand Up @@ -127,23 +126,9 @@ def on_torrent_created(self, result):
self.dialog_widget.edit_channel_create_torrent_progress_label.setText(tr("Created torrent"))
if 'torrent' in result:
self.create_torrent_notification.emit({"msg": tr("Torrent successfully created")})
if self.dialog_widget.add_to_channel_checkbox.isChecked():
self.add_torrent_to_channel(result['torrent'])
self.close_dialog()

def add_torrent_to_channel(self, torrent):
data = {"torrent": torrent}
if self.name:
data.update({"title": ensure_unicode(self.name, 'utf8')})
self.rest_request2 = TriblerNetworkRequest(
"mychannel/torrents", self.on_torrent_to_channel_added, data=data, method='PUT'
)

def on_torrent_to_channel_added(self, result):
if not result:
return
if 'added' in result:
self.create_torrent_notification.emit({"msg": tr("Torrent successfully added to the channel")})
if self.dialog_widget.add_to_channel_checkbox.isChecked():
self.add_to_channel_selected.emit(result['torrent'])

def on_select_save_directory(self, checked):
chosen_dir = QFileDialog.getExistingDirectory(
Expand Down
47 changes: 32 additions & 15 deletions src/tribler-gui/tribler_gui/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,24 +559,40 @@ def perform_start_download_request(
self.update_recent_download_locations(destination)

if add_to_channel:
self.show_add_torrent_to_channel_dialog_from_uri(uri)

def show_add_torrent_to_channel_dialog_from_uri(self, uri):
def on_add_button_pressed(channel_id):
post_data = {}
if uri.startswith("file:"):
with open(uri_to_path(uri), "rb") as torrent_file:
post_data['torrent'] = b64encode(torrent_file.read()).decode('utf8')
elif uri.startswith("magnet:"):
post_data['uri'] = uri

if post_data:
TriblerNetworkRequest(
f"channels/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
method='PUT',
data=post_data,
)

def on_add_button_pressed(channel_id):
post_data = {}
if uri.startswith("file:"):
with open(uri_to_path(uri), "rb") as torrent_file:
post_data['torrent'] = b64encode(torrent_file.read()).decode('utf8')
elif uri.startswith("magnet:"):
post_data['uri'] = uri
self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")

if post_data:
TriblerNetworkRequest(
f"channels/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
method='PUT',
data=post_data,
)
def show_add_torrent_to_channel_dialog_from_torrent_data(self, torrent_data):
def on_add_button_pressed(channel_id):
post_data = {'torrent': torrent_data}

if post_data:
TriblerNetworkRequest(
f"channels/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
method='PUT',
data=post_data,
)

self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")

def on_new_version_available(self, version):
if version == str(self.gui_settings.value('last_reported_version')):
Expand Down Expand Up @@ -740,6 +756,7 @@ def on_create_torrent(self, checked):

self.create_dialog = CreateTorrentDialog(self)
connect(self.create_dialog.create_torrent_notification, self.on_create_torrent_updates)
connect(self.create_dialog.add_to_channel_selected, self.show_add_torrent_to_channel_dialog_from_torrent_data)
self.create_dialog.show()

def on_create_torrent_updates(self, update_dict):
Expand Down

0 comments on commit 3165a72

Please sign in to comment.