Module: deluge
Branch: master
Commit: f21dd242f6ef54ad791e8812da3ea22fa09a5c84

Author: John Garland <[email protected]>
Date:   Sun Apr 11 22:24:51 2010 +1000

Fix renaming files in add torrent dialog

---

 ChangeLog                     |    1 +
 deluge/core/torrentmanager.py |   13 +++----------
 deluge/ui/common.py           |   21 +++++++++++++++++++--
 3 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 183c811..cf4f44e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
        * Implement #457 progress bars for folders
        * Implement #1012 httpdownloader supports gzip decoding
        * #496: Remove deprecated functions in favour of get_session_status()
+       * #1112: Fix renaming files in add torrent dialog
 
 ==== Blocklist ====
        * Implement local blocklist support
diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py
index baf7047..011e40a 100644
--- a/deluge/core/torrentmanager.py
+++ b/deluge/core/torrentmanager.py
@@ -41,7 +41,6 @@ import os
 import time
 import shutil
 import operator
-import locale
 
 from twisted.internet import reactor
 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 TorrentOptions
 import deluge.core.oldstateupgrader
+from deluge.ui.common import utf8_encoded
 
 from deluge.log import LOG as log
 
@@ -388,8 +388,7 @@ class TorrentManager(component.Component):
             if options["mapped_files"]:
                 for index, name in options["mapped_files"].items():
                     log.debug("renaming file index %s to %s", index, name)
-                    torrent_info.rename_file(index, 
-                        
name.encode("utf-8").decode(locale.getpreferredencoding(), "ignore"))
+                    torrent_info.rename_file(index, utf8_encoded(name))
 
             add_torrent_params["ti"] = torrent_info
             add_torrent_params["resume_data"] = ""
@@ -403,14 +402,8 @@ class TorrentManager(component.Component):
         else:
             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
-        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["paused"] = True
         add_torrent_params["auto_managed"] = False
diff --git a/deluge/ui/common.py b/deluge/ui/common.py
index e8c6e02..8f666d5 100644
--- a/deluge/ui/common.py
+++ b/deluge/ui/common.py
@@ -43,6 +43,7 @@ import sys
 import urlparse
 
 import chardet
+import locale
 
 try:
     from hashlib import sha1 as sha
@@ -67,9 +68,25 @@ def decode_string(s, encoding="utf8"):
     """
 
     try:
-        s = s.decode(encoding).encode("utf8")
+        s = s.decode(encoding).encode("utf8", "ignore")
     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
 
 class TorrentInfo(object):

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to