From 8c33fab699bf4ef95c12baf02866b4917fc08e66 Mon Sep 17 00:00:00 2001 From: Nikos Kouremenos Date: Wed, 18 Jan 2006 19:50:16 +0000 Subject: [PATCH] make decode filenames work allover --- src/filetransfers_window.py | 12 +++++------- src/gtkgui_helpers.py | 17 +++++++++++++++++ src/vcard.py | 18 +++++++----------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/filetransfers_window.py b/src/filetransfers_window.py index 7965c5922..770e5b6cd 100644 --- a/src/filetransfers_window.py +++ b/src/filetransfers_window.py @@ -265,12 +265,9 @@ _('Connection with peer cannot be established.')) if response == gtk.RESPONSE_OK: file_dir = None files_path_list = dialog.get_filenames() + files_path_list = gtkgui_helpers.decode_filechooser_file_paths( + files_path_list) for file_path in files_path_list: - # decode as UTF-8 under win - if os.name == 'nt': - file_path = file_path.decode('utf8') - else: - file_path = file_path.decode(sys.getfilesystemencoding()) if self.send_file(account, contact, file_path) and file_dir is None: file_dir = os.path.dirname(file_path) if file_dir: @@ -306,7 +303,7 @@ _('Connection with peer cannot be established.')) def confirm_overwrite_cb(self, dialog, file_props): file_path = dialog.get_filename() - file_path = file_path.decode('utf-8') + file_path = gtkgui_helpers.decode_filechooser_file_paths((file_path,))[0] if os.path.exists(file_path): stat = os.stat(file_path) dl_size = stat.st_size @@ -360,7 +357,8 @@ _('Connection with peer cannot be established.')) response = dialog.run() if response == gtk.RESPONSE_OK: file_path = dialog.get_filename() - file_path = file_path.decode('utf-8') + file_path = gtkgui_helpers.decode_filechooser_file_paths( + (file_path,))[0] if not gtk28 and os.path.exists(file_path): primtext = _('This file already exists') sectext = _('Would you like to overwrite it?') diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py index a800dcf05..95a568f17 100644 --- a/src/gtkgui_helpers.py +++ b/src/gtkgui_helpers.py @@ -474,3 +474,20 @@ def make_pixbuf_grayscale(pixbuf): pixbuf2 = pixbuf.copy() pixbuf.saturate_and_pixelate(pixbuf2, 0.0, False) return pixbuf2 + +def decode_filechooser_file_paths(file_paths): + '''decode as UTF-8 under Windows and + ask sys.getfilesystemencoding() in POSIX + file_paths MUST be LIST''' + file_paths_list = list() + + if os.name == 'nt': # decode as UTF-8 under Windows + for file_path in file_paths: + file_path = file_path.decode('utf8') + file_paths_list.append(file_path) + else: + for file_path in file_paths: + file_path = file_path.decode(sys.getfilesystemencoding()) + file_paths_list.append(file_path) + + return file_paths_list diff --git a/src/vcard.py b/src/vcard.py index 0d34b58c9..07ca60243 100644 --- a/src/vcard.py +++ b/src/vcard.py @@ -200,17 +200,13 @@ class VcardWindow: response = dialog.run() if response == gtk.RESPONSE_OK: f = dialog.get_filename() - try: - f = f.decode('utf-8') - except UnicodeError: - pass - else: - filesize = os.path.getsize(f) # in bytes - if filesize > 32768: # 32 kb - dialogs.ErrorDialog(_('The filesize of image "%s" is too large')\ - % os.path.basename(f), - _('The file must not be more than 32 kilobytes.')).get_response() - continue + f = gtkgui_helpers.decode_filechooser_file_paths((f,))[0] + filesize = os.path.getsize(f) # in bytes + if filesize > 32768: # 32 kb + dialogs.ErrorDialog(_('The filesize of image "%s" is too large')\ + % os.path.basename(f), + _('The file must not be more than 32 kilobytes.')).get_response() + continue if self.image_is_ok(f): done = True else: # Cancel or WM X button