Skip to content

Commit db87bc5

Browse files
committed
Merge remote-tracking branch 'origin/pr/225'
* origin/pr/225: Global clipboard clearing option
2 parents c7f45b2 + 942b24b commit db87bc5

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

qui/clipboard.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,13 @@ def show_menu(self, _unused, event):
351351

352352
def update_clipboard_contents(self, vm=None, size=0, message=None, icon=None):
353353
if not vm or not size:
354+
self.clear_menu_item.set_sensitive(False)
354355
self.clipboard_label.set_markup(_("<i>Global clipboard is empty</i>"))
355356
self.icon.set_from_icon_name("qui-clipboard")
356357
# todo the icon should be empty and full depending on state
357358

358359
else:
360+
self.clear_menu_item.set_sensitive(True)
359361
self.clipboard_label.set_markup(
360362
_("<i>Global clipboard contents: {0} from <b>{1}</b></i>").format(
361363
size, vm
@@ -387,7 +389,6 @@ def setup_ui(self, *_args, **_kwargs):
387389
clipboard_content_item = Gtk.MenuItem()
388390
clipboard_content_item.set_sensitive(False)
389391
clipboard_content_item.add(self.clipboard_label)
390-
self.update_clipboard_contents()
391392
self.menu.append(clipboard_content_item)
392393

393394
self.menu.append(Gtk.SeparatorMenuItem())
@@ -406,6 +407,11 @@ def setup_ui(self, *_args, **_kwargs):
406407

407408
self.menu.append(Gtk.SeparatorMenuItem())
408409

410+
self.clear_menu_item = Gtk.MenuItem(_("Clear global clipboard"))
411+
self.clear_menu_item.connect("activate", self.clear_clipboard)
412+
self.menu.append(self.clear_menu_item)
413+
self.update_clipboard_contents()
414+
409415
dom0_item = Gtk.MenuItem(_("Copy dom0 clipboard"))
410416
dom0_item.connect("activate", self.copy_dom0_clipboard)
411417
self.menu.append(dom0_item)
@@ -447,6 +453,9 @@ def copy_dom0_clipboard(self, *_args, **_kwargs):
447453
buffer_size="256000",
448454
)
449455
)
456+
self.update_clipboard_contents(
457+
"dom0", "{} bytes".format(os.path.getsize(DATA))
458+
)
450459
except Exception: # pylint: disable=broad-except
451460
self.send_notify(
452461
_("Error while accessing global clipboard!"),
@@ -478,6 +487,41 @@ def _convert_to_readable(key: str):
478487
return key.upper()
479488
return key
480489

490+
def clear_clipboard(self, *_args, **_kwargs):
491+
try:
492+
with appviewer_lock():
493+
with open(DATA, "w", encoding="utf-8") as contents:
494+
contents.truncate(0)
495+
with open(FROM, "w", encoding="ascii") as source:
496+
source.write("dom0")
497+
with open(XEVENT, "w", encoding="ascii") as timestamp:
498+
timestamp.write(str(Gtk.get_current_event_time()))
499+
with open(METADATA, "w", encoding="ascii") as metadata:
500+
metadata.write(
501+
"{{\n"
502+
'"vmname":"dom0",\n'
503+
'"xevent_timestamp":{xevent_timestamp},\n'
504+
'"successful":1,\n'
505+
'"cleared":1,\n'
506+
'"copy_action":0,\n'
507+
'"paste_action":0,\n'
508+
'"malformed_request":0,\n'
509+
'"qrexec_clipboard":0,\n'
510+
'"sent_size":0,\n'
511+
'"buffer_size":{buffer_size},\n'
512+
'"protocol_version_xside":65544,\n'
513+
'"protocol_version_vmside":65544,\n'
514+
"}}\n".format(
515+
xevent_timestamp=str(Gtk.get_current_event_time()),
516+
buffer_size="256000",
517+
)
518+
)
519+
self.update_clipboard_contents(
520+
vm=None, size=0, message=("Global clipboard cleared")
521+
)
522+
except Exception: # pylint: disable=broad-except
523+
self.send_notify(_("Error while clearing global clipboard!"))
524+
481525

482526
def main():
483527
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)