[#2406] [Core] [GTKUI] Implement core.add_torrent_files

* Speeds up adding multiple torrents
This commit is contained in:
bendikro 2014-12-01 16:01:20 +01:00 committed by Calum Lind
parent 41f08e4e29
commit 2aaae7c6a1
3 changed files with 78 additions and 33 deletions

View File

@ -19,6 +19,7 @@ from urlparse import urljoin
import twisted.web.client import twisted.web.client
import twisted.web.error import twisted.web.error
from twisted.internet import reactor, task
import deluge.common import deluge.common
import deluge.component as component import deluge.component as component
@ -186,36 +187,69 @@ class Core(component.Component):
return self.new_release return self.new_release
return False return False
def _add_torrent_file(self, filename, filedump, options, save_state=True):
"""Adds a torrent file to the session.
Args:
filename (str): The filename of the torrent.
filedump (str): A base64 encoded string of torrent file contents.
options (dict): The options to apply to the torrent upon adding.
save_state (bool): If the state should be saved after adding the file.
Returns:
str: The torrent ID or None.
"""
try:
filedump = base64.decodestring(filedump)
except Exception as ex:
log.error("There was an error decoding the filedump string: %s", ex)
try:
torrent_id = self.torrentmanager.add(
filedump=filedump, options=options, filename=filename, save_state=save_state
)
except RuntimeError as ex:
log.error("There was an error adding the torrent file %s: %s", filename, ex)
torrent_id = None
return torrent_id
# Exported Methods # Exported Methods
@export @export
def add_torrent_file(self, filename, filedump, options): def add_torrent_file(self, filename, filedump, options):
"""Adds a torrent file to the session. """Adds a torrent file to the session.
Args: Args:
filename (str): the filename of the torrent filename (str): The filename of the torrent.
filedump (str): A base64 encoded string of the torrent file contents filedump (str): A base64 encoded string of the torrent file contents.
options (dict): The options to apply to the torrent on add options (dict): The options to apply to the torrent upon adding.
Returns: Returns:
str: The torrent_id or None str: The torrent_id or None.
""" """
try: return self._add_torrent_file(filename, filedump, options)
filedump = base64.decodestring(filedump)
except Exception as ex:
log.error("There was an error decoding the filedump string!")
log.exception(ex)
try: @export
torrent_id = self.torrentmanager.add( def add_torrent_files(self, torrent_files):
filedump=filedump, options=options, filename=filename """Adds multiple torrent files to the session.
)
except Exception as ex:
log.error("There was an error adding the torrent file %s", filename)
log.exception(ex)
torrent_id = None
return torrent_id Args:
torrent_files (list of tuples): Torrent files as tuple of (filename, filedump, options).
Returns:
Deferred
"""
def add_torrents():
torrent_ids = []
count = len(torrent_files)
for idx, torrent in enumerate(torrent_files):
torrent_id = self._add_torrent_file(torrent[0], torrent[1],
torrent[2], save_state=idx == (count - 1))
torrent_ids.append(torrent_id)
return torrent_ids
return task.deferLater(reactor, 0, add_torrents)
@export @export
def add_torrent_url(self, url, options, headers=None): def add_torrent_url(self, url, options, headers=None):

View File

@ -182,6 +182,7 @@ class AddTorrentDialog(component.Component):
def add_from_files(self, filenames): def add_from_files(self, filenames):
new_row = None new_row = None
already_added = 0
for filename in filenames: for filename in filenames:
# Get the torrent data from the torrent file # Get the torrent data from the torrent file
@ -193,12 +194,7 @@ class AddTorrentDialog(component.Component):
continue continue
if info.info_hash in self.files: if info.info_hash in self.files:
log.debug("Trying to add a duplicate torrent!") already_added += 1
ErrorDialog(
_("Duplicate Torrent"),
_("You cannot add the same torrent twice."),
self.dialog
).run()
continue continue
new_row = self.torrent_liststore.append([info.info_hash, info.name, filename]) new_row = self.torrent_liststore.append([info.info_hash, info.name, filename])
@ -213,6 +209,16 @@ class AddTorrentDialog(component.Component):
if not row and new_row: if not row and new_row:
self.listview_torrents.get_selection().select_iter(new_row) self.listview_torrents.get_selection().select_iter(new_row)
self.builder.get_object("label_torrent_count").set_text("Torrents (%d)" % len(self.torrent_liststore))
if already_added:
log.debug("Tried to add %d duplicate torrents!", already_added)
ErrorDialog(
_("Duplicate Torrent(s)"),
_("You cannot add the same torrent twice. %d torrents were already added." % already_added),
self.dialog
).run()
def add_from_magnets(self, uris): def add_from_magnets(self, uris):
new_row = None new_row = None
@ -712,11 +718,16 @@ class AddTorrentDialog(component.Component):
def _on_button_add_clicked(self, widget): def _on_button_add_clicked(self, widget):
log.debug("_on_button_add_clicked") log.debug("_on_button_add_clicked")
# Save the options for selected torrent prior to adding self.add_torrents()
self.hide()
def add_torrents(self):
(model, row) = self.listview_torrents.get_selection().get_selected() (model, row) = self.listview_torrents.get_selection().get_selected()
if row is not None: if row is not None:
self.save_torrent_options(row) self.save_torrent_options(row)
torrents_to_add = []
row = self.torrent_liststore.get_iter_first() row = self.torrent_liststore.get_iter_first()
while row is not None: while row is not None:
torrent_id = self.torrent_liststore.get_value(row, 0) torrent_id = self.torrent_liststore.get_value(row, 0)
@ -734,14 +745,14 @@ class AddTorrentDialog(component.Component):
del options["file_priorities"] del options["file_priorities"]
client.core.add_torrent_magnet(filename, options) client.core.add_torrent_magnet(filename, options)
else: else:
client.core.add_torrent_file( torrents_to_add.append((os.path.split(filename)[-1],
os.path.split(filename)[-1],
base64.encodestring(self.infos[torrent_id]), base64.encodestring(self.infos[torrent_id]),
options options))
)
row = self.torrent_liststore.iter_next(row) row = self.torrent_liststore.iter_next(row)
self.hide()
def on_torrents_added(torrent_ids):
log.info("Added %d torrents", len(torrent_ids))
client.core.add_torrent_files(torrents_to_add).addCallback(on_torrents_added)
def _on_button_apply_clicked(self, widget): def _on_button_apply_clicked(self, widget):
log.debug("_on_button_apply_clicked") log.debug("_on_button_apply_clicked")

View File

@ -288,7 +288,7 @@
</object> </object>
</child> </child>
<child type="label"> <child type="label">
<object class="GtkLabel" id="label7"> <object class="GtkLabel" id="label_torrent_count">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="label" translatable="yes">Torrents</property> <property name="label" translatable="yes">Torrents</property>