Skip to content

Commit e429bb4

Browse files
committed
Global clipboard clearing option
Menu item to Global Clipboard widget to allow users to manually clear it related: QubesOS/qubes-issues#6641
1 parent 2614254 commit e429bb4

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

qui/clipboard.py

Lines changed: 44 additions & 2 deletions
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,9 +389,7 @@ 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)
392-
393393
self.menu.append(Gtk.SeparatorMenuItem())
394394

395395
help_label = Gtk.Label(xalign=0)
@@ -406,6 +406,11 @@ def setup_ui(self, *_args, **_kwargs):
406406

407407
self.menu.append(Gtk.SeparatorMenuItem())
408408

409+
self.clear_menu_item = Gtk.MenuItem(_("Clear global clipboard"))
410+
self.clear_menu_item.connect('activate', self.clear_clipboard)
411+
self.menu.append(self.clear_menu_item)
412+
self.update_clipboard_contents()
413+
409414
dom0_item = Gtk.MenuItem(_("Copy dom0 clipboard"))
410415
dom0_item.connect("activate", self.copy_dom0_clipboard)
411416
self.menu.append(dom0_item)
@@ -447,6 +452,10 @@ def copy_dom0_clipboard(self, *_args, **_kwargs):
447452
buffer_size="256000",
448453
)
449454
)
455+
self.update_clipboard_contents(
456+
"dom0",
457+
"{} 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,39 @@ 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(vm=None, size=0, message= \
520+
_("Global clipboard cleared"))
521+
except Exception: # pylint: disable=broad-except
522+
self.send_notify(_("Error while clearing global clipboard!"))
481523

482524
def main():
483525
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)