Module: deluge
Branch: master
Commit: c2d301bf52978e3ea7438afd11bb8061f3174468

Author: Chase Sterling <chase.sterl...@gmail.com>
Date:   Thu Nov 15 23:36:20 2012 -0500

Make deluge-gtk get arguments as unicode.
Fix a few places that use those arguments.
Make sure gtkui loads strings as unicode from rencode.

---

 ChangeLog                           |    1 +
 deluge/ui/client.py                 |    3 ++
 deluge/ui/gtkui/addtorrentdialog.py |    3 --
 deluge/ui/gtkui/ipcinterface.py     |    2 +-
 deluge/ui/gtkui/queuedtorrents.py   |    2 +-
 deluge/ui/ui.py                     |   36 ++++++++++++++++++++++++++++++++++-
 6 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b1062d3..cc00f12 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -32,6 +32,7 @@
        * Implemented sequential downloads UI handling.
        * #378: Allow showing a pieces bar instead of a regular progress bar in 
a
        torrent's status tab.
+       * #2093: Make torrent opening compatible with all unicode paths.
 
 ==== Blocklist Plugin ====
        * #1382: Implemented whitelist support to both core and GTK UI.
diff --git a/deluge/ui/client.py b/deluge/ui/client.py
index cf9dee8..342480e 100644
--- a/deluge/ui/client.py
+++ b/deluge/ui/client.py
@@ -37,6 +37,7 @@
 import logging
 from twisted.internet.protocol import ClientFactory
 from twisted.internet import reactor, ssl, defer
+import sys
 import subprocess
 
 import deluge.common
@@ -624,6 +625,8 @@ class Client(object):
         :raises OSError: received from subprocess.call()
 
         """
+        # subprocess.popen does not work with unicode args (with non-ascii 
characters) on windows
+        config = config.encode(sys.getfilesystemencoding())
         try:
             subprocess.Popen(["deluged", "--port=%s" % port, "--config=%s" % 
config])
         except OSError, e:
diff --git a/deluge/ui/gtkui/addtorrentdialog.py 
b/deluge/ui/gtkui/addtorrentdialog.py
index e2df0c9..976f95e 100644
--- a/deluge/ui/gtkui/addtorrentdialog.py
+++ b/deluge/ui/gtkui/addtorrentdialog.py
@@ -213,9 +213,6 @@ class AddTorrentDialog(component.Component):
         new_row = None
 
         for filename in filenames:
-            # Convert the path to unicode
-            filename = unicode(filename)
-
             # Get the torrent data from the torrent file
             try:
                 info = deluge.ui.common.TorrentInfo(filename)
diff --git a/deluge/ui/gtkui/ipcinterface.py b/deluge/ui/gtkui/ipcinterface.py
index 242e6b2..6600e4e 100644
--- a/deluge/ui/gtkui/ipcinterface.py
+++ b/deluge/ui/gtkui/ipcinterface.py
@@ -60,7 +60,7 @@ log = logging.getLogger(__name__)
 class IPCProtocolServer(Protocol):
     def dataReceived(self, data):
         config = ConfigManager("gtkui.conf")
-        data = rencode.loads(data)
+        data = rencode.loads(data, decode_utf8=True)
         if not data or config["focus_main_window_on_add"]:
             component.get("MainWindow").present()
         process_args(data)
diff --git a/deluge/ui/gtkui/queuedtorrents.py 
b/deluge/ui/gtkui/queuedtorrents.py
index 3b63934..7eeaaa7 100644
--- a/deluge/ui/gtkui/queuedtorrents.py
+++ b/deluge/ui/gtkui/queuedtorrents.py
@@ -173,7 +173,7 @@ class QueuedTorrents(component.Component):
     def on_button_add_clicked(self, widget):
         # Add all the torrents in the liststore
         def add_torrent(model, path, iter, data):
-            torrent_path = model.get_value(iter, 1)
+            torrent_path = model.get_value(iter, 1).decode('utf-8')
             process_args([torrent_path])
 
         self.liststore.foreach(add_torrent, None)
diff --git a/deluge/ui/ui.py b/deluge/ui/ui.py
index 792f416..c10a2c3 100644
--- a/deluge/ui/ui.py
+++ b/deluge/ui/ui.py
@@ -64,6 +64,35 @@ if 'dev' not in deluge.common.get_version():
     import warnings
     warnings.filterwarnings('ignore', category=DeprecationWarning, 
module='twisted')
 
+def win32_unicode_argv():
+    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
+    strings.
+
+    Versions 2.x of Python don't support Unicode in sys.argv on
+    Windows, with the underlying Windows API instead replacing multi-byte
+    characters with '?'.
+    """
+
+    from ctypes import POINTER, byref, cdll, c_int, windll
+    from ctypes.wintypes import LPCWSTR, LPWSTR
+
+    GetCommandLineW = cdll.kernel32.GetCommandLineW
+    GetCommandLineW.argtypes = []
+    GetCommandLineW.restype = LPCWSTR
+
+    CommandLineToArgvW = windll.shell32.CommandLineToArgvW
+    CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
+    CommandLineToArgvW.restype = POINTER(LPWSTR)
+
+    cmd = GetCommandLineW()
+    argc = c_int(0)
+    argv = CommandLineToArgvW(cmd, byref(argc))
+    if argc.value > 0:
+        # Remove Python executable and commands if present
+        start = argc.value - len(sys.argv)
+        return [argv[i] for i in
+                xrange(start, argc.value)]
+
 class _UI(object):
 
     def __init__(self, name="gtk"):
@@ -107,7 +136,12 @@ class _UI(object):
         return self.__args
 
     def start(self):
-        (self.__options, self.__args) = self.__parser.parse_args()
+        # Make sure all arguments are unicode
+        if deluge.common.windows_check():
+            argv = win32_unicode_argv()[1:]
+        else:
+            argv = [arg.decode(sys.stdin.encoding) for arg in sys.argv[1:]]
+        (self.__options, self.__args) = self.__parser.parse_args(argv)
 
         if self.__options.quiet:
             self.__options.loglevel = "none"

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

Reply via email to