clipboard.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 math import log
  27. from qubes.qubes import QubesException
  28. from PyQt4.QtGui import QApplication
  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. fd = os.open(APPVIEWER_LOCK, os.O_RDWR|os.O_CREAT, 0o0666)
  38. except:
  39. QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!")
  40. else:
  41. try:
  42. fcntl.flock(fd, fcntl.LOCK_EX)
  43. except:
  44. QMessageBox.warning(None, "Warning!", "Error while locking Qubes clipboard!")
  45. else:
  46. try:
  47. with open(CLIPBOARD_CONTENTS, "w") as contents:
  48. contents.write(text)
  49. with open(CLIPBOARD_SOURCE, "w") as source:
  50. source.write("dom0")
  51. except:
  52. QMessageBox.warning(None, "Warning!", "Error while writing to Qubes clipboard!")
  53. fcntl.flock(fd, fcntl.LOCK_UN)
  54. os.close(fd)
  55. def get_qubes_clipboard_formatted_size():
  56. units = ['B', 'KiB', 'MiB', 'GiB']
  57. try:
  58. file_size = os.path.getsize(CLIPBOARD_CONTENTS)
  59. except:
  60. QMessageBox.warning(None, "Warning!", "Error while accessing Qubes clipboard!")
  61. else:
  62. formatted_bytes = '1 byte' if file_size == 1 else str(file_size) + ' bytes'
  63. if file_size > 0:
  64. magnitude = min(int(log(file_size)/log(2)*0.1), len(units)-1)
  65. if magnitude > 0:
  66. return '%s (%.1f %s)' % (formatted_bytes, file_size/(2.0**(10*magnitude)), units[magnitude])
  67. return '%s' % (formatted_bytes)
  68. return '? bytes'