clipboard.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python2
  2. # pylint: skip-file
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Jean-Philippe Ouellet <jpo@vt.edu>
  7. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  8. # Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, see <http://www.gnu.org/licenses/>.
  22. #
  23. #
  24. import os
  25. import fcntl
  26. from math import log
  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, 0o0666)
  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)
  54. def get_qubes_clipboard_formatted_size():
  55. units = ['B', 'KiB', 'MiB', 'GiB']
  56. try:
  57. file_size = os.path.getsize(CLIPBOARD_CONTENTS)
  58. except:
  59. QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!")
  60. else:
  61. formatted_bytes = '1 byte' if file_size == 1 else str(file_size) + ' bytes'
  62. if file_size > 0:
  63. magnitude = min(int(log(file_size)/log(2)*0.1), len(units)-1)
  64. if magnitude > 0:
  65. return '%s (%.1f %s)' % (formatted_bytes, file_size/(2.0**(10*magnitude)), units[magnitude])
  66. return '%s' % (formatted_bytes)
  67. return '? bytes'