Fix renaming files in add torrent dialog

This commit is contained in:
John Garland 2010-04-11 22:24:51 +10:00
commit f21dd242f6
3 changed files with 23 additions and 12 deletions

View file

@ -4,6 +4,7 @@
* Implement #457 progress bars for folders * Implement #457 progress bars for folders
* Implement #1012 httpdownloader supports gzip decoding * Implement #1012 httpdownloader supports gzip decoding
* #496: Remove deprecated functions in favour of get_session_status() * #496: Remove deprecated functions in favour of get_session_status()
* #1112: Fix renaming files in add torrent dialog
==== Blocklist ==== ==== Blocklist ====
* Implement local blocklist support * Implement local blocklist support

View file

@ -41,7 +41,6 @@ import os
import time import time
import shutil import shutil
import operator import operator
import locale
from twisted.internet import reactor from twisted.internet import reactor
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
@ -57,6 +56,7 @@ from deluge.configmanager import ConfigManager, get_config_dir
from deluge.core.torrent import Torrent from deluge.core.torrent import Torrent
from deluge.core.torrent import TorrentOptions from deluge.core.torrent import TorrentOptions
import deluge.core.oldstateupgrader import deluge.core.oldstateupgrader
from deluge.ui.common import utf8_encoded
from deluge.log import LOG as log from deluge.log import LOG as log
@ -388,8 +388,7 @@ class TorrentManager(component.Component):
if options["mapped_files"]: if options["mapped_files"]:
for index, name in options["mapped_files"].items(): for index, name in options["mapped_files"].items():
log.debug("renaming file index %s to %s", index, name) log.debug("renaming file index %s to %s", index, name)
torrent_info.rename_file(index, torrent_info.rename_file(index, utf8_encoded(name))
name.encode("utf-8").decode(locale.getpreferredencoding(), "ignore"))
add_torrent_params["ti"] = torrent_info add_torrent_params["ti"] = torrent_info
add_torrent_params["resume_data"] = "" add_torrent_params["resume_data"] = ""
@ -403,14 +402,8 @@ class TorrentManager(component.Component):
else: else:
storage_mode = lt.storage_mode_t(1) storage_mode = lt.storage_mode_t(1)
try:
# Try to encode this as utf8 if needed
options["download_location"] = options["download_location"].encode("utf8")
except UnicodeDecodeError:
pass
# Fill in the rest of the add_torrent_params dictionary # Fill in the rest of the add_torrent_params dictionary
add_torrent_params["save_path"] = options["download_location"] add_torrent_params["save_path"] = utf8_encoded(options["download_location"])
add_torrent_params["storage_mode"] = storage_mode add_torrent_params["storage_mode"] = storage_mode
add_torrent_params["paused"] = True add_torrent_params["paused"] = True
add_torrent_params["auto_managed"] = False add_torrent_params["auto_managed"] = False

View file

@ -43,6 +43,7 @@ import sys
import urlparse import urlparse
import chardet import chardet
import locale
try: try:
from hashlib import sha1 as sha from hashlib import sha1 as sha
@ -67,9 +68,25 @@ def decode_string(s, encoding="utf8"):
""" """
try: try:
s = s.decode(encoding).encode("utf8") s = s.decode(encoding).encode("utf8", "ignore")
except UnicodeDecodeError: except UnicodeDecodeError:
s = s.decode(chardet.detect(s)["encoding"]).encode("utf8") s = s.decode(chardet.detect(s)["encoding"], "ignore").encode("utf8", "ignore")
return s
def utf8_encoded(s):
"""
Returns a utf8 encoded string of s
:param s: (unicode) string to (re-)encode
:type s: basestring
:returns: a utf8 encoded string of s
:rtype: str
"""
if isinstance(s, str):
s = decode_string(s, locale.getpreferredencoding())
elif isinstance(s, unicode):
s = s.encode("utf8", "ignore")
return s return s
class TorrentInfo(object): class TorrentInfo(object):