clipboard.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2016 Jean-Philippe Ouellet <jpo@vt.edu>
  6. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  7. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import os
  25. import fcntl
  26. from qubes.qubes import QubesException
  27. from PyQt4.QtGui import QApplication
  28. APPVIEWER_LOCK = "/var/run/qubes/appviewer.lock"
  29. CLIPBOARD_CONTENTS = "/var/run/qubes/qubes-clipboard.bin"
  30. CLIPBOARD_SOURCE = CLIPBOARD_CONTENTS + ".source"
  31. def do_dom0_copy():
  32. copy_text_to_qubes_clipboard(QApplication.clipboard().text())
  33. def copy_text_to_qubes_clipboard(text):
  34. #inter-appviewer lock
  35. try:
  36. fd = os.open(APPVIEWER_LOCK, os.O_RDWR|os.O_CREAT, 0666)
  37. except:
  38. QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!")
  39. else:
  40. try:
  41. fcntl.flock(fd, fcntl.LOCK_EX)
  42. except:
  43. QMessageBox.warning(None, "Warning!", "Error while locking Qubes clipboard!")
  44. else:
  45. try:
  46. with open(CLIPBOARD_CONTENTS, "w") as contents:
  47. contents.write(text)
  48. with open(CLIPBOARD_SOURCE, "w") as source:
  49. source.write("dom0")
  50. except:
  51. QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!")
  52. fcntl.flock(fd, fcntl.LOCK_UN)
  53. os.close(fd)