clipboard.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/python3
  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 Lesser General Public License along
  20. # with this program; if not, see <http://www.gnu.org/licenses/>.
  21. #
  22. #
  23. import os
  24. import fcntl
  25. from math import log
  26. # pylint: disable=import-error
  27. from PyQt5.QtWidgets import QApplication, QMessageBox
  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. file = os.open(APPVIEWER_LOCK, os.O_RDWR | os.O_CREAT, 0o0666)
  37. except Exception: # pylint: disable=broad-except
  38. QMessageBox.warning(None, "Warning!",
  39. "Error while accessing Qubes clipboard!")
  40. else:
  41. try:
  42. fcntl.flock(file, fcntl.LOCK_EX)
  43. except Exception: # pylint: disable=broad-except
  44. QMessageBox.warning(None, "Warning!",
  45. "Error while locking Qubes clipboard!")
  46. else:
  47. try:
  48. with open(CLIPBOARD_CONTENTS, "w") as contents:
  49. contents.write(text)
  50. with open(CLIPBOARD_SOURCE, "w") as source:
  51. source.write("dom0")
  52. except Exception: # pylint: disable=broad-except
  53. QMessageBox.warning(None, "Warning!",
  54. "Error while writing to Qubes clipboard!")
  55. fcntl.flock(file, fcntl.LOCK_UN)
  56. os.close(file)
  57. # pylint: disable=invalid-name
  58. def get_qubes_clipboard_formatted_size():
  59. units = ['B', 'KiB', 'MiB', 'GiB']
  60. try:
  61. file_size = os.path.getsize(CLIPBOARD_CONTENTS)
  62. except Exception: # pylint: disable=broad-except
  63. QMessageBox.warning(None, "Warning!",
  64. "Error while accessing Qubes clipboard!")
  65. else:
  66. formatted_bytes = '1 byte' if file_size == 1 \
  67. else str(file_size) + ' bytes'
  68. if file_size > 0:
  69. magnitude = min(int(log(file_size)/log(2)*0.1), len(units)-1)
  70. if magnitude > 0:
  71. return '%s (%.1f %s)' % (formatted_bytes,
  72. file_size/(2.0**(10*magnitude)),
  73. units[magnitude])
  74. return '%s' % formatted_bytes
  75. return '? bytes'