我一直试图用python编写flatpak应用程序,该应用程序使用freedesktop门户获取屏幕截图。我正在改编用c语言编写的Gimp的freedesktop屏幕截图插件,在获取并与我的应用程序共享后访问屏幕截图时遇到了麻烦。我得到一个格式为'/ org / freedesktop / portal / desktop / request / 1_326 / t'的uri,freedesktop门户文档指出该uri指向安装在run / user / $ UID / doc /上的融合文件系统我可以确认屏幕截图已保存在此处。但是,我似乎无法为他们获取有用的标识符,这样我就可以通过我的应用将其附加到电子邮件中。
我已经在freedesktop Documents门户中尝试了所有方法,但在那里没有成功。调用返回uri并将其保存在opath变量中的uri的dbus代理后,Gimp实现会在此处设置另一个dbus代理,然后将该代理连接到回调函数:我在其中复制这些摘要的完整文件位于此处<{ {3}}>
if (opath)
{
GDBusProxy *proxy2 = NULL;
proxy2 = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
NULL,
"org.freedesktop.portal.Desktop",
opath,
"org.freedesktop.portal.Request",
NULL, NULL);
*image_ID = 0;
g_signal_connect (proxy2, "g-signal",
G_CALLBACK (screenshot_freedesktop_dbus_signal),
image_ID);
gtk_main ();
g_object_unref (proxy2);
g_free (opath);
并且screenshot_freedesktop_dbus_signal函数如下所示:
static void
screenshot_freedesktop_dbus_signal (GDBusProxy *proxy,
gchar *sender_name,
gchar *signal_name,
GVariant *parameters,
gint32 *image_ID)
{
if (g_strcmp0 (signal_name, "Response") == 0)
{
GVariant *results;
guint32 response;
g_variant_get (parameters, "(u@a{sv})",
&response,
&results);
/* Possible values:
* 0: Success, the request is carried out
* 1: The user cancelled the interaction
* 2: The user interaction was ended in some other way
* Cf. https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.portal.Request.xml
*/
if (response == 0)
{
gchar *uri;
if (g_variant_lookup (results, "uri", "s", &uri))
{
GFile *file = g_file_new_for_uri (uri);
gchar *path = g_file_get_path (file);
*image_ID = gimp_file_load (GIMP_RUN_NONINTERACTIVE,
path, path);
gimp_image_set_filename (*image_ID, "screenshot.png");
/* Delete the actual file. */
g_file_delete (file, NULL, NULL);
g_object_unref (file);
g_free (path);
g_free (uri);
}
}
g_variant_unref (results);
gtk_main_quit ();
}
}
直到回调函数,我的代码都以相同的方式工作。我找不到g_signal_connect函数的pythonic等效项。我已经尝试将回调添加到新代理的初始化中,例如
proxy2 = Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
Gio.DBusProxyFlags.NONE,
None,
"org.freedesktop.portal.Desktop",
returned_uri[0],
"org.freedesktop.portal.Request",
None,
G_CALLBACK(self.receive_screenshot_signal),
None)
但是我的receive_screenshot_signal函数没有传递任何值,传递的任务对象说它们还没有完成。
我不确定从这里要去哪里,所以请提供任何有关如何更有效地使用dbusproxy库和/或freedesktop门户的建议或见解。谢谢!
答案 0 :(得分:0)
我通过在请求句柄的路径上订阅来自总线的信号来解决了这个问题。
args = GLib.Variant('(sa{sv})', (filename, {}))
result = self.proxy.call_sync('Screenshot',
args,
Gio.DBusCallFlags.NONE,
-1,
None)
request_handle = result.unpack()[0]
self.bus.signal_subscribe("org.freedesktop.portal.Desktop",
"org.freedesktop.portal.Request",
"Response",
request_handle,
None,
Gio.DBusSignalFlags.NO_MATCH_RULE,
self.receive_screenshot_signal)
我在Gio.bus.signal_subscribe上找不到任何特定的文档,但这是回调函数处理返回的方式:
def receive_screenshot_signal(self,
connection,
sender,
path,
interface,
signal,
params):
if not isinstance(params, GLib.Variant):
print("It's not a variant")
return
response_code, results = params.unpack()
# Response code 0 signifies success
if response_code != 0 or 'uri' not in results:
print("Error taking screenshot")
return
parsed_uri = urlparse(results['uri'])
assert parsed_uri.scheme == "file"
self.screenshot = unquote(parsed_uri.path)