| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
# mypy: disable-error-code="misc" |
| 5 |
|
| 6 |
from tests.templates.xdp_utils import init_logger |
| 7 |
|
| 8 |
import dbus.service |
| 9 |
import dbus |
| 10 |
import tempfile |
| 11 |
from gi.repository import GLib |
| 12 |
from dataclasses import dataclass |
| 13 |
|
| 14 |
|
| 15 |
BUS_NAME = "org.freedesktop.impl.portal.Test" |
| 16 |
MAIN_OBJ = "/org/freedesktop/portal/desktop" |
| 17 |
SYSTEM_BUS = False |
| 18 |
MAIN_IFACE = "org.freedesktop.impl.portal.Clipboard" |
| 19 |
VERSION = 1 |
| 20 |
|
| 21 |
|
| 22 |
logger = init_logger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
@dataclass |
| 26 |
class ClipboardParameters: |
| 27 |
delay: int |
| 28 |
response: int |
| 29 |
expect_close: bool |
| 30 |
|
| 31 |
|
| 32 |
def load(mock, parameters={}): |
| 33 |
logger.debug(f"Loading parameters: {parameters}") |
| 34 |
|
| 35 |
assert not hasattr(mock, "clipboard_params") |
| 36 |
mock.clipboard_params = ClipboardParameters( |
| 37 |
delay=parameters.get("delay", 200), |
| 38 |
response=parameters.get("response", 0), |
| 39 |
expect_close=parameters.get("expect-close", False), |
| 40 |
) |
| 41 |
|
| 42 |
mock.AddProperties( |
| 43 |
MAIN_IFACE, |
| 44 |
dbus.Dictionary( |
| 45 |
{ |
| 46 |
"version": dbus.UInt32(parameters.get("version", VERSION)), |
| 47 |
} |
| 48 |
), |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
@dbus.service.method( |
| 53 |
MAIN_IFACE, |
| 54 |
in_signature="oa{sv}", |
| 55 |
out_signature="", |
| 56 |
async_callbacks=("cb_success", "cb_error"), |
| 57 |
) |
| 58 |
def RequestClipboard(self, session_handle, options, cb_success, cb_error): |
| 59 |
try: |
| 60 |
logger.debug(f"RequestClipboard({session_handle}, {options})") |
| 61 |
params = self.clipboard_params |
| 62 |
|
| 63 |
if params.expect_close: |
| 64 |
cb_success() |
| 65 |
else: |
| 66 |
logger.debug(f"scheduling delay of {params.delay}") |
| 67 |
GLib.timeout_add(params.delay, cb_success) |
| 68 |
except Exception as e: |
| 69 |
logger.critical(e) |
| 70 |
cb_error(e) |
| 71 |
|
| 72 |
|
| 73 |
@dbus.service.method( |
| 74 |
MAIN_IFACE, |
| 75 |
in_signature="oa{sv}", |
| 76 |
out_signature="", |
| 77 |
async_callbacks=("cb_success", "cb_error"), |
| 78 |
) |
| 79 |
def SetSelection(self, session_handle, options, cb_success, cb_error): |
| 80 |
try: |
| 81 |
logger.debug(f"SetSelection({session_handle}, {options})") |
| 82 |
params = self.clipboard_params |
| 83 |
|
| 84 |
if params.expect_close: |
| 85 |
cb_success() |
| 86 |
else: |
| 87 |
logger.debug(f"scheduling delay of {params.delay}") |
| 88 |
GLib.timeout_add(params.delay, cb_success) |
| 89 |
except Exception as e: |
| 90 |
logger.critical(e) |
| 91 |
cb_error(e) |
| 92 |
|
| 93 |
|
| 94 |
@dbus.service.method( |
| 95 |
MAIN_IFACE, |
| 96 |
in_signature="ou", |
| 97 |
out_signature="h", |
| 98 |
async_callbacks=("cb_success", "cb_error"), |
| 99 |
) |
| 100 |
def SelectionWrite(self, session_handle, serial, cb_success, cb_error): |
| 101 |
try: |
| 102 |
logger.debug(f"SelectionWrite({session_handle}, {serial})") |
| 103 |
params = self.clipboard_params |
| 104 |
|
| 105 |
temp_file = tempfile.TemporaryFile() |
| 106 |
fd = dbus.types.UnixFd(temp_file.fileno()) |
| 107 |
|
| 108 |
if params.expect_close: |
| 109 |
cb_success(fd) |
| 110 |
else: |
| 111 |
|
| 112 |
def reply(): |
| 113 |
cb_success(fd) |
| 114 |
|
| 115 |
logger.debug(f"scheduling delay of {params.delay}") |
| 116 |
GLib.timeout_add(params.delay, reply) |
| 117 |
except Exception as e: |
| 118 |
logger.critical(e) |
| 119 |
cb_error(e) |
| 120 |
|
| 121 |
|
| 122 |
@dbus.service.method( |
| 123 |
MAIN_IFACE, |
| 124 |
in_signature="oub", |
| 125 |
out_signature="", |
| 126 |
async_callbacks=("cb_success", "cb_error"), |
| 127 |
) |
| 128 |
def SelectionWriteDone(self, session_handle, serial, success, cb_success, cb_error): |
| 129 |
try: |
| 130 |
logger.debug(f"SelectionWriteDone({session_handle}, {serial}, {success})") |
| 131 |
params = self.clipboard_params |
| 132 |
|
| 133 |
if params.expect_close: |
| 134 |
cb_success() |
| 135 |
else: |
| 136 |
logger.debug(f"scheduling delay of {params.delay}") |
| 137 |
GLib.timeout_add(params.delay, cb_success) |
| 138 |
except Exception as e: |
| 139 |
logger.critical(e) |
| 140 |
cb_error(e) |
| 141 |
|
| 142 |
|
| 143 |
@dbus.service.method( |
| 144 |
MAIN_IFACE, |
| 145 |
in_signature="os", |
| 146 |
out_signature="h", |
| 147 |
async_callbacks=("cb_success", "cb_error"), |
| 148 |
) |
| 149 |
def SelectionRead(self, session_handle, mime_type, cb_success, cb_error): |
| 150 |
try: |
| 151 |
logger.debug(f"SelectionRead({session_handle}, {mime_type})") |
| 152 |
params = self.clipboard_params |
| 153 |
|
| 154 |
temp_file = tempfile.TemporaryFile() |
| 155 |
fd = dbus.types.UnixFd(temp_file.fileno()) |
| 156 |
|
| 157 |
if params.expect_close: |
| 158 |
cb_success(fd) |
| 159 |
else: |
| 160 |
|
| 161 |
def reply(): |
| 162 |
cb_success(fd) |
| 163 |
|
| 164 |
logger.debug(f"scheduling delay of {params.delay}") |
| 165 |
GLib.timeout_add(params.delay, reply) |
| 166 |
except Exception as e: |
| 167 |
logger.critical(e) |
| 168 |
cb_error(e) |