clipboard.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. from PyQt5.QtCore import QCoreApplication
  29. APPVIEWER_LOCK = "/var/run/qubes/appviewer.lock"
  30. CLIPBOARD_CONTENTS = "/var/run/qubes/qubes-clipboard.bin"
  31. CLIPBOARD_SOURCE = CLIPBOARD_CONTENTS + ".source"
  32. def do_dom0_copy():
  33. copy_text_to_qubes_clipboard(QApplication.clipboard().text())
  34. def copy_text_to_qubes_clipboard(text):
  35. # inter-appviewer lock
  36. try:
  37. file = os.open(APPVIEWER_LOCK, os.O_RDWR | os.O_CREAT, 0o0666)
  38. except Exception: # pylint: disable=broad-except
  39. QMessageBox.warning(
  40. None,
  41. QCoreApplication.translate("Clipboard", "Warning!"),
  42. QCoreApplication.translate(
  43. "Clipboard", "Error while accessing Qubes clipboard!"))
  44. else:
  45. try:
  46. fcntl.flock(file, fcntl.LOCK_EX)
  47. except Exception: # pylint: disable=broad-except
  48. QMessageBox.warning(
  49. None,
  50. QCoreApplication.translate("Clipboard", "Warning!"),
  51. QCoreApplication.translate(
  52. "Clipboard", "Error while locking Qubes clipboard!"))
  53. else:
  54. try:
  55. with open(CLIPBOARD_CONTENTS, "w") as contents:
  56. contents.write(text)
  57. with open(CLIPBOARD_SOURCE, "w") as source:
  58. source.write("dom0")
  59. except Exception: # pylint: disable=broad-except
  60. QMessageBox.warning(
  61. None,
  62. QCoreApplication.translate("Clipboard", "Warning!"),
  63. QCoreApplication.translate(
  64. "Clipboard", "Error while writing to Qubes clipboard!"))
  65. fcntl.flock(file, fcntl.LOCK_UN)
  66. os.close(file)
  67. # pylint: disable=invalid-name
  68. def get_qubes_clipboard_formatted_size():
  69. units = ['B', 'KiB', 'MiB', 'GiB']
  70. try:
  71. file_size = os.path.getsize(CLIPBOARD_CONTENTS)
  72. except Exception: # pylint: disable=broad-except
  73. QMessageBox.warning(
  74. None,
  75. QCoreApplication.translate("Clipboard", "Warning!"),
  76. QCoreApplication.translate(
  77. "Clipboard", "Error while accessing Qubes clipboard!"))
  78. else:
  79. formatted_bytes = QCoreApplication.translate(
  80. "Clipboard", "%n byte(s)", "", file_size)
  81. if file_size > 0:
  82. magnitude = min(int(log(file_size)/log(2)*0.1), len(units)-1)
  83. if magnitude > 0:
  84. return '%s (%.1f %s)' % (formatted_bytes,
  85. file_size/(2.0**(10*magnitude)),
  86. units[magnitude])
  87. return '%s' % formatted_bytes
  88. return QCoreApplication.translate("Clipboard", '? bytes')