Make deluge-gtk get arguments as unicode.

Fix a few places that use those arguments.
Make sure gtkui loads strings as unicode from rencode.
This commit is contained in:
Chase Sterling 2012-11-15 23:36:20 -05:00
parent 8658be3b05
commit c2d301bf52
6 changed files with 41 additions and 6 deletions

View File

@ -32,6 +32,7 @@
* Implemented sequential downloads UI handling. * Implemented sequential downloads UI handling.
* #378: Allow showing a pieces bar instead of a regular progress bar in a * #378: Allow showing a pieces bar instead of a regular progress bar in a
torrent's status tab. torrent's status tab.
* #2093: Make torrent opening compatible with all unicode paths.
==== Blocklist Plugin ==== ==== Blocklist Plugin ====
* #1382: Implemented whitelist support to both core and GTK UI. * #1382: Implemented whitelist support to both core and GTK UI.

View File

@ -37,6 +37,7 @@
import logging import logging
from twisted.internet.protocol import ClientFactory from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor, ssl, defer from twisted.internet import reactor, ssl, defer
import sys
import subprocess import subprocess
import deluge.common import deluge.common
@ -624,6 +625,8 @@ class Client(object):
:raises OSError: received from subprocess.call() :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: try:
subprocess.Popen(["deluged", "--port=%s" % port, "--config=%s" % config]) subprocess.Popen(["deluged", "--port=%s" % port, "--config=%s" % config])
except OSError, e: except OSError, e:

View File

@ -213,9 +213,6 @@ class AddTorrentDialog(component.Component):
new_row = None new_row = None
for filename in filenames: for filename in filenames:
# Convert the path to unicode
filename = unicode(filename)
# Get the torrent data from the torrent file # Get the torrent data from the torrent file
try: try:
info = deluge.ui.common.TorrentInfo(filename) info = deluge.ui.common.TorrentInfo(filename)

View File

@ -60,7 +60,7 @@ log = logging.getLogger(__name__)
class IPCProtocolServer(Protocol): class IPCProtocolServer(Protocol):
def dataReceived(self, data): def dataReceived(self, data):
config = ConfigManager("gtkui.conf") 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"]: if not data or config["focus_main_window_on_add"]:
component.get("MainWindow").present() component.get("MainWindow").present()
process_args(data) process_args(data)

View File

@ -173,7 +173,7 @@ class QueuedTorrents(component.Component):
def on_button_add_clicked(self, widget): def on_button_add_clicked(self, widget):
# Add all the torrents in the liststore # Add all the torrents in the liststore
def add_torrent(model, path, iter, data): 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]) process_args([torrent_path])
self.liststore.foreach(add_torrent, None) self.liststore.foreach(add_torrent, None)

View File

@ -64,6 +64,35 @@ if 'dev' not in deluge.common.get_version():
import warnings import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted') 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): class _UI(object):
def __init__(self, name="gtk"): def __init__(self, name="gtk"):
@ -107,7 +136,12 @@ class _UI(object):
return self.__args return self.__args
def start(self): 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: if self.__options.quiet:
self.__options.loglevel = "none" self.__options.loglevel = "none"