2019-05-22 23:10:09 +02:00
|
|
|
#!/usr/bin/python3
|
2016-11-27 01:12:51 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
2016-11-27 02:46:15 +01:00
|
|
|
# Copyright (C) 2016 Jean-Philippe Ouellet <jpo@vt.edu>
|
2016-11-27 01:12:51 +01:00
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
# Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
2017-11-06 21:06:30 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2016-11-27 01:12:51 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import fcntl
|
2017-05-28 17:49:34 +02:00
|
|
|
from math import log
|
2016-11-27 01:12:51 +01:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
# pylint: disable=import-error
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMessageBox
|
2019-10-23 18:18:39 +02:00
|
|
|
from PyQt5.QtCore import QCoreApplication
|
2016-11-27 01:12:51 +01:00
|
|
|
|
2016-11-27 02:35:45 +01:00
|
|
|
APPVIEWER_LOCK = "/var/run/qubes/appviewer.lock"
|
|
|
|
CLIPBOARD_CONTENTS = "/var/run/qubes/qubes-clipboard.bin"
|
|
|
|
CLIPBOARD_SOURCE = CLIPBOARD_CONTENTS + ".source"
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2016-11-27 01:12:51 +01:00
|
|
|
def do_dom0_copy():
|
|
|
|
copy_text_to_qubes_clipboard(QApplication.clipboard().text())
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
|
2016-11-27 01:12:51 +01:00
|
|
|
def copy_text_to_qubes_clipboard(text):
|
2019-05-22 23:10:09 +02:00
|
|
|
# inter-appviewer lock
|
2016-11-27 01:12:51 +01:00
|
|
|
|
|
|
|
try:
|
2019-05-22 23:10:09 +02:00
|
|
|
file = os.open(APPVIEWER_LOCK, os.O_RDWR | os.O_CREAT, 0o0666)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-10-23 18:18:39 +02:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
|
|
|
QCoreApplication.translate("Clipboard", "Warning!"),
|
|
|
|
QCoreApplication.translate(
|
|
|
|
"Clipboard", "Error while accessing Qubes clipboard!"))
|
2016-11-27 02:46:15 +01:00
|
|
|
else:
|
|
|
|
try:
|
2019-05-22 23:10:09 +02:00
|
|
|
fcntl.flock(file, fcntl.LOCK_EX)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-10-23 18:18:39 +02:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
|
|
|
QCoreApplication.translate("Clipboard", "Warning!"),
|
|
|
|
QCoreApplication.translate(
|
|
|
|
"Clipboard", "Error while locking Qubes clipboard!"))
|
2016-11-27 02:46:15 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with open(CLIPBOARD_CONTENTS, "w") as contents:
|
|
|
|
contents.write(text)
|
|
|
|
with open(CLIPBOARD_SOURCE, "w") as source:
|
|
|
|
source.write("dom0")
|
2019-05-22 23:10:09 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-10-23 18:18:39 +02:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
|
|
|
QCoreApplication.translate("Clipboard", "Warning!"),
|
|
|
|
QCoreApplication.translate(
|
|
|
|
"Clipboard", "Error while writing to Qubes clipboard!"))
|
2019-05-22 23:10:09 +02:00
|
|
|
fcntl.flock(file, fcntl.LOCK_UN)
|
|
|
|
os.close(file)
|
|
|
|
|
2017-05-28 17:49:34 +02:00
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
# pylint: disable=invalid-name
|
2017-05-28 17:49:34 +02:00
|
|
|
def get_qubes_clipboard_formatted_size():
|
|
|
|
units = ['B', 'KiB', 'MiB', 'GiB']
|
|
|
|
|
|
|
|
try:
|
|
|
|
file_size = os.path.getsize(CLIPBOARD_CONTENTS)
|
2019-05-22 23:10:09 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-10-23 18:18:39 +02:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
|
|
|
QCoreApplication.translate("Clipboard", "Warning!"),
|
|
|
|
QCoreApplication.translate(
|
|
|
|
"Clipboard", "Error while accessing Qubes clipboard!"))
|
2017-05-28 17:49:34 +02:00
|
|
|
else:
|
2019-10-23 18:18:39 +02:00
|
|
|
formatted_bytes = QCoreApplication.translate(
|
|
|
|
"Clipboard", "%n byte(s)", "", file_size)
|
2017-05-28 17:49:34 +02:00
|
|
|
if file_size > 0:
|
|
|
|
magnitude = min(int(log(file_size)/log(2)*0.1), len(units)-1)
|
|
|
|
if magnitude > 0:
|
2019-05-22 23:10:09 +02:00
|
|
|
return '%s (%.1f %s)' % (formatted_bytes,
|
|
|
|
file_size/(2.0**(10*magnitude)),
|
|
|
|
units[magnitude])
|
|
|
|
return '%s' % formatted_bytes
|
2017-05-28 17:49:34 +02:00
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
return QCoreApplication.translate("Clipboard", '? bytes')
|