Skip to content

Commit

Permalink
#3229 remove utf8 conversion in the clipboard layer
Browse files Browse the repository at this point in the history
let the packet layer handle it, mostly. (still use net_utf8() to retrieve string values that may have been converted to bytes)
  • Loading branch information
totaam committed Aug 31, 2021
1 parent bbfcdf7 commit f4f8a65
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
13 changes: 5 additions & 8 deletions xpra/gtk_common/gtk_clipboard.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Xpra.
# Copyright (C) 2019-2020 Antoine Martin <antoine@xpra.org>
# Copyright (C) 2019-2021 Antoine Martin <antoine@xpra.org>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

Expand All @@ -10,7 +10,7 @@
ClipboardProxyCore, TEXT_TARGETS,
)
from xpra.clipboard.clipboard_timeout_helper import ClipboardTimeoutHelper
from xpra.util import ellipsizer, envint
from xpra.util import ellipsizer, envint, net_utf8
from xpra.os_util import bytestostr, monotonic_time
from xpra.log import Logger

Expand Down Expand Up @@ -71,10 +71,7 @@ def got_token(self, targets, target_data=None, claim=True, synchronous_client=Fa
dtype, dformat, data = target_data.get(text_target)
if dformat!=8:
continue
try:
text = data.decode("utf8")
except UnicodeDecodeError:
text = bytestostr(data)
text = net_utf8(data)
log("setting text data %s / %s of size %i: %s",
dtype, dformat, len(text), ellipsizer(text))
self._block_owner_change = monotonic_time()
Expand Down Expand Up @@ -108,7 +105,7 @@ def send_token(*token_data):
if text:
#should verify the target is actually utf8...
text_target = text_targets[0]
send_token(targets, (text_target, "UTF8_STRING", 8, text.encode("utf8")))
send_token(targets, (text_target, "UTF8_STRING", 8, text))
return
send_token(text_targets)

Expand Down Expand Up @@ -139,7 +136,7 @@ def get_contents(self, target, got_contents, time=0):
elif target in TEXT_TARGETS:
text = self.clipboard.wait_for_text()
if text:
got_contents(target, 8, text.encode("utf8"))
got_contents(target, 8, text)
return
else:
#data = wait_for_contents(target)?
Expand Down
11 changes: 1 addition & 10 deletions xpra/net/file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,6 @@ def send_file(self, filename, mimetype, data, filesize=0, printit=False, openit=
data = data[:filesize] #gio may null terminate it
l("send_file%s action=%s, ask=%s",
(filename, mimetype, type(data), "%i bytes" % filesize, printit, openit, options), action, ask)
try:
filename = bytestostr(filename).encode("utf8")
except Exception:
filename = strtobytes(filename)
self.dump_remote_caps()
if not self.check_file_size(action, filename, filesize):
return False
Expand Down Expand Up @@ -889,12 +885,7 @@ def do_send_file(self, filename, mimetype, data, filesize=0, printit=False, open
assert len(cdata)<=filesize #compressed wrapper ensures this is true
filelog("sending full file: %i bytes (chunk size=%i)", filesize, chunk_size)
basefilename = os.path.basename(filename)
#convert str to utf8 bytes:
try:
base = basefilename.encode("utf8")
except (AttributeError, UnicodeEncodeError):
base = strtobytes(basefilename)
self.send("send-file", base, mimetype, printit, openit, filesize, cdata, options, send_id)
self.send("send-file", basefilename, mimetype, printit, openit, filesize, cdata, options, send_id)
return True

def _check_chunk_sending(self, chunk_id, chunk_no):
Expand Down
8 changes: 4 additions & 4 deletions xpra/platform/darwin/osx_clipboard.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Xpra.
# Copyright (C) 2012-2020 Antoine Martin <antoine@xpra.org>
# Copyright (C) 2012-2021 Antoine Martin <antoine@xpra.org>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

Expand All @@ -16,7 +16,7 @@
_filter_targets, ClipboardProxyCore, TEXT_TARGETS,
)
from xpra.platform.ui_thread_watcher import get_UI_watcher
from xpra.util import csv
from xpra.util import csv, net_utf8
from xpra.os_util import bytestostr
from xpra.log import Logger

Expand Down Expand Up @@ -221,7 +221,7 @@ def got_contents(self, target, dtype=None, dformat=None, data=None):
return
if dformat==8 and dtype in TEXT_TARGETS:
log("we got a byte string: %s", data)
self.set_clipboard_text(data)
self.set_clipboard_text(net_utf8(data))
if dformat==8 and dtype in IMAGE_FORMATS:
log("we got a %s image", dtype)
self.set_image_data(dtype, data)
Expand Down Expand Up @@ -255,7 +255,7 @@ def set_image_data(self, dtype, data):

def set_clipboard_text(self, text):
self.pasteboard.clearContents()
r = self.pasteboard.setString_forType_(text.decode("utf8"), NSStringPboardType)
r = self.pasteboard.setString_forType_(text, NSStringPboardType)
log("set_clipboard_text(%s) success=%s", text, r)
self.update_change_count()

Expand Down
10 changes: 5 additions & 5 deletions xpra/platform/win32/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ClipboardProxyCore, log, _filter_targets,
TEXT_TARGETS, MAX_CLIPBOARD_PACKET_SIZE,
)
from xpra.util import csv, ellipsizer, envint, envbool, roundup
from xpra.util import net_utf8, csv, ellipsizer, envint, envbool, roundup
from xpra.os_util import bytestostr, strtobytes
from xpra.platform.win32.constants import PROCESS_QUERY_INFORMATION

Expand Down Expand Up @@ -217,8 +217,8 @@ def w_to_utf8(data):
s = buf.raw[:l]
log("got %i bytes of UNICODE data: %s", len(s), ellipsizer(s))
if CONVERT_LINE_ENDINGS:
return s.decode("utf8").replace("\r\n", "\n").encode("utf8")
return strtobytes(s)
return s.decode("utf8").replace("\r\n", "\n")
return str(s)


class Win32Clipboard(ClipboardTimeoutHelper):
Expand Down Expand Up @@ -526,7 +526,7 @@ def got_contents(self, target, dtype=None, dformat=None, data=None):
self.send_clipboard_request_handler(self, self._selection, image_formats[0])
elif dformat==8 and dtype in TEXT_TARGETS:
log("we got a byte string: %s", data)
self.set_clipboard_text(data)
self.set_clipboard_text(net_utf8(data))
elif dformat==8 and dtype.startswith("image/"):
img_format = dtype.split("/")[-1] #ie: 'png'
self.set_clipboard_image(img_format, data)
Expand Down Expand Up @@ -676,7 +676,7 @@ def set_clipboard_text(self, text):
#convert to wide char
#get the length in wide chars:
if CONVERT_LINE_ENDINGS:
text = text.decode("utf8").replace("\n", "\r\n").encode("utf8")
text = text.replace("\n", "\r\n").encode("utf8")
wlen = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, text, len(text), None, 0)
if not wlen:
self.set_err("failed to prepare to convert to wide char")
Expand Down

0 comments on commit f4f8a65

Please sign in to comment.