22 #include "model/dbus_client.h"
24 static GDBusProxy* GetProxy() {
28 GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SESSION, 0, &error);
29 g_assert_no_error(error);
34 GDBusProxy* p = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
36 kDbusName, kDbusObject, kDbusInterface,
39 g_assert_no_error(error);
43 LocalApiResult DbusLocalClient::SendRaise() {
44 auto proxy = GetProxy();
45 if (!proxy)
return LocalApiResult(
false,
"Cannot create proxy");
47 GVariant* result = g_dbus_proxy_call_sync (proxy,
50 G_DBUS_CALL_FLAGS_NONE,
54 const std::string message(error ? error->message :
"");
55 bool ok(error == 0 && g_variant_is_container(result)
56 && g_variant_n_children(result) == 0);
57 if (error) g_clear_error(&error);
58 g_variant_unref(result);
59 g_object_unref(proxy);
60 return LocalApiResult(ok, message);
63 LocalApiResult DbusLocalClient::SendQuit() {
64 auto proxy = GetProxy();
65 if (!proxy)
return LocalApiResult(
false,
"Cannot create proxy");
67 GVariant* result = g_dbus_proxy_call_sync (proxy,
70 G_DBUS_CALL_FLAGS_NONE,
74 const std::string message(error ? error->message :
"");
75 bool ok(error == 0 && g_variant_is_container(result)
76 && g_variant_n_children(result) == 0);
77 if (error) g_clear_error(&error);
78 g_variant_unref(result);
79 g_object_unref(proxy);
80 return LocalApiResult(ok, message);
83 LocalApiResult DbusLocalClient::SendOpen(
const char* path) {
84 auto proxy = GetProxy();
85 if (!proxy)
return LocalApiResult(
false,
"Cannot create proxy");
87 GVariant* result = g_dbus_proxy_call_sync (proxy,
89 g_variant_new(
"(s)", path),
90 G_DBUS_CALL_FLAGS_NONE,
94 const std::string message(error ? error->message :
"");
95 bool ok(error == 0 && g_variant_is_container(result)
96 && g_variant_n_children(result) == 1);
97 gboolean result_code =
false;
99 GVariant* result_value = g_variant_get_child_value(result, 0);
100 result_code = g_variant_get_boolean(result_value);
101 g_variant_unref(result_value);
103 if (error) g_clear_error(&error);
104 g_variant_unref(result);
105 g_object_unref(proxy);
106 if (!ok)
return LocalApiResult(
false,
"Error invoking DBus server command.");
107 if (result_code)
return LocalApiResult(
true, path);
108 return LocalApiResult(
false,
"Error opening file");
111 LocalApiResult DbusLocalClient::GetRestEndpoint() {
112 auto proxy = GetProxy();
113 if (!proxy)
return LocalApiResult(
false,
"Cannot create proxy");
115 GVariant* result = g_dbus_proxy_call_sync (proxy,
118 G_DBUS_CALL_FLAGS_NONE,
122 const std::string message(error ? error->message :
"");
123 bool ok(error == 0 && g_variant_is_container(result)
124 && g_variant_n_children(result) == 1);
125 std::string result_str;
127 GVariant* result_value = g_variant_get_child_value(result, 0);
129 const gchar* s = g_variant_get_string(result_value, &length);
130 result_str = std::string(s, length);
131 g_variant_unref(result_value);
133 if (error) g_clear_error(&error);
134 g_variant_unref(result);
135 g_object_unref(proxy);
137 return LocalApiResult(
true, result_str.c_str());
139 return LocalApiResult(
false,
"Error invoking DBus server command.");