Initial support for renaming files

Save .torrent file for torrents added by infohash or magnet uri
This commit is contained in:
Andrew Resch 2008-09-30 18:45:32 +00:00
parent 1cb4559362
commit 87f3e1e3b8
6 changed files with 77 additions and 7 deletions

View File

@ -700,6 +700,11 @@ class Core(
"""Rescans the plugin folders for new plugins"""
component.get("PluginManager").scan_for_plugins()
def export_rename_files(self, torrent_id, filenames):
"""Renames files in 'torrent_id'. The 'filenames' parameter should be a
list of (index, filename) pairs."""
self.torrents[torrent_id].rename_files(filenames)
## Queueing functions ##
def export_queue_top(self, torrent_ids):
log.debug("Attempting to queue %s to top", torrent_ids)

View File

@ -723,6 +723,22 @@ class Torrent:
except Exception, e:
log.warning("Unable to delete the fastresume file: %s", e)
def write_torrentfile(self):
"""Writes the torrent file"""
path = "%s/%s.torrent" % (
self.config["state_location"],
self.torrent_id)
log.debug("Writing torrent file: %s", path)
try:
ti = self.handle.get_torrent_info()
md = lt.bdecode(ti.metadata())
log.debug("md: %s", md)
torrent_file = {}
torrent_file["info"] = md
open(path, "wb").write(lt.bencode(torrent_file))
except Exception, e:
log.warning("Unable to save torrent file: %s", e)
def delete_torrentfile(self):
"""Deletes the .torrent file in the state"""
path = "%s/%s.torrent" % (
@ -762,3 +778,10 @@ class Torrent:
log.debug("Unable to force recheck: %s", e)
return False
return True
def rename_files(self, filenames):
"""Renames files in the torrent. 'filenames' should be a list of
(index, filename) pairs."""
for index, filename in filenames:
self.handle.rename_file(index, filename)

View File

@ -158,7 +158,11 @@ class TorrentManager(component.Component):
self.on_alert_save_resume_data)
self.alerts.register_handler("save_resume_data_failed_alert",
self.on_alert_save_resume_data_failed)
self.alerts.register_handler("file_renamed_alert",
self.on_alert_file_renamed)
self.alerts.register_handler("metadata_received_alert",
self.on_alert_metadata_received)
def start(self):
# Get the pluginmanager reference
self.plugins = component.get("PluginManager")
@ -738,4 +742,17 @@ class TorrentManager(component.Component):
log.debug("on_alert_save_resume_data_failed: %s", alert.message())
torrent = self.torrents[str(alert.handle.info_hash())]
torrent.waiting_on_resume_data = False
def on_alert_file_renamed(self, alert):
log.debug("on_alert_file_renamed")
log.debug("index: %s name: %s", alert.index, alert.name)
torrent_id = str(alert.handle.info_hash())
torrent = self.torrents[torrent_id]
torrent.files[alert.index]["path"] = alert.name
component.get("SignalManager").emit("torrent_file_renamed", torrent_id, alert.index, alert.name)
def on_alert_metadata_received(self, alert):
log.debug("on_alert_metadata_received")
torrent = self.torrents[str(alert.handle.info_hash())]
torrent.write_torrentfile()

View File

@ -170,7 +170,7 @@ class BaseClient(object):
"set_torrent_stop_ratio", "set_torrent_stop_at_ratio",
"set_torrent_remove_at_ratio", "set_torrent_move_on_completed",
"set_torrent_move_on_completed_path", "add_torrent_magnets",
"create_torrent", "upload_plugin", "rescan_plugins"]
"create_torrent", "upload_plugin", "rescan_plugins", "rename_files"]
def __init__(self):
self.core = _core

View File

@ -111,6 +111,8 @@ class FilesTab(Tab):
column.pack_start(render, False)
column.add_attribute(render, "stock-id", 6)
render = gtk.CellRendererText()
render.set_property("editable", True)
render.connect("edited", self._on_filename_edited)
column.pack_start(render, True)
column.add_attribute(render, "text", 0)
column.set_sort_column_id(0)
@ -179,6 +181,9 @@ class FilesTab(Tab):
"on_menuitem_highest_activate": self._on_menuitem_highest_activate,
"on_menuitem_expand_all_activate": self._on_menuitem_expand_all_activate
})
# Connect to the 'torrent_file_renamed' signal
component.get("Signals").connect_to_signal("torrent_file_renamed", self._on_torrent_file_renamed_signal)
# Attempt to load state
self.load_state()
@ -338,6 +343,7 @@ class FilesTab(Tab):
###
def update_files(self):
self.treestore.clear()
self.prepare_file_store(self.files_list[self.torrent_id])
self.listview.expand_row("0", False)
@ -382,9 +388,15 @@ class FilesTab(Tab):
self.get_files_from_tree(self.treestore, files_list, 0)
files_list.sort()
for index, row in files_list:
row[2] = "%.2f%%" % (status["file_progress"][index] * 100)
row[3] = status["file_progress"][index] * 100
row[4] = status["file_priorities"][index]
progress_string = "%.2f%%" % (status["file_progress"][index] * 100)
if row[2] != progress_string:
row[2] = progress_string
progress_value = status["file_progress"][index] * 100
if row[3] != progress_value:
row[3] = progress_value
file_priority = status["file_priorities"][index]
if row[4] != file_priority:
row[4] = file_priority
def _on_button_press_event(self, widget, event):
"""This is a callback for showing the right-click context menu."""
@ -450,3 +462,15 @@ class FilesTab(Tab):
def _on_menuitem_expand_all_activate(self, menuitem):
self.listview.expand_all()
def _on_filename_edited(self, renderer, path, new_text):
index = self.treestore[path][5]
client.rename_files(self.torrent_id, [(index, new_text)])
def _on_torrent_file_renamed_signal(self, torrent_id, index, name):
log.debug("index: %s name: %s", index, name)
self.files_list[torrent_id][index]["path"] = name
# We need to update the filename displayed if we're currently viewing
# this torrents files.
if torrent_id == self.torrent_id:
self.update_files()

View File

@ -196,6 +196,9 @@ class GtkUI:
client.connect_on_new_core(self._on_new_core)
client.connect_on_no_core(self._on_no_core)
# Start the signal receiver
self.signal_receiver = Signals()
# Initialize various components of the gtkui
self.mainwindow = MainWindow()
self.menubar = MenuBar()
@ -209,8 +212,6 @@ class GtkUI:
self.statusbar = StatusBar()
self.addtorrentdialog = AddTorrentDialog()
# Start the signal receiver
self.signal_receiver = Signals()
self.coreconfig = CoreConfig()
# Initalize the plugins