OpenCPN Partial API docs
Loading...
Searching...
No Matches
dbus_client.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <gio/gio.h>
25
26#include "model/dbus_client.h"
27
28static GDBusProxy* GetProxy() {
29 // session_bus_up();
30 // 0std::this_thread::sleep_for(50ms);
31 GError* error = 0;
32 GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SESSION, 0, &error);
33 g_assert_no_error(error);
34 // /* we shouldn't have a name owner nor any cached properties //
35 // g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
36 // g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
37 error = 0;
38 GDBusProxy* p = g_dbus_proxy_new_sync(
39 conn, G_DBUS_PROXY_FLAGS_NONE, 0 /* GDBusInterfaceInfo* */, kDbusName,
40 kDbusObject, kDbusInterface, 0 /* GCancellable */, &error);
41 g_assert_no_error(error);
42 return p;
43}
44
45LocalApiResult DbusLocalClient::SendRaise() {
46 auto proxy = GetProxy();
47 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
48 GError* error = 0;
49 GVariant* result = g_dbus_proxy_call_sync(
50 proxy, "Raise", 0 /* parameters */, G_DBUS_CALL_FLAGS_NONE,
51 -1 /* timeout msec */, 0 /* cancellable */, &error);
52 const std::string message(error ? error->message : "");
53 bool ok(error == 0 && g_variant_is_container(result) &&
54 g_variant_n_children(result) == 0);
55 if (error) g_clear_error(&error);
56 g_variant_unref(result);
57 g_object_unref(proxy);
58 return LocalApiResult(ok, message);
59}
60
61LocalApiResult DbusLocalClient::SendQuit() {
62 auto proxy = GetProxy();
63 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
64 GError* error = 0;
65 GVariant* result = g_dbus_proxy_call_sync(
66 proxy, "Quit", 0 /* parameters */, G_DBUS_CALL_FLAGS_NONE,
67 -1 /* timeout msec */, 0 /* cancellable */, &error);
68 const std::string message(error ? error->message : "");
69 bool ok(error == 0 && g_variant_is_container(result) &&
70 g_variant_n_children(result) == 0);
71 if (error) g_clear_error(&error);
72 g_variant_unref(result);
73 g_object_unref(proxy);
74 return LocalApiResult(ok, message);
75}
76
77LocalApiResult DbusLocalClient::SendOpen(const char* path) {
78 auto proxy = GetProxy();
79 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
80 GError* error = 0;
81 GVariant* result = g_dbus_proxy_call_sync(
82 proxy, "Open", g_variant_new("(s)", path), G_DBUS_CALL_FLAGS_NONE,
83 -1 /* timeout msec */, 0 /* cancellable */, &error);
84 const std::string message(error ? error->message : "");
85 bool ok(error == 0 && g_variant_is_container(result) &&
86 g_variant_n_children(result) == 1);
87 gboolean result_code = false;
88 if (ok) {
89 GVariant* result_value = g_variant_get_child_value(result, 0);
90 result_code = g_variant_get_boolean(result_value);
91 g_variant_unref(result_value);
92 }
93 if (error) g_clear_error(&error);
94 g_variant_unref(result);
95 g_object_unref(proxy);
96 if (!ok) return LocalApiResult(false, "Error invoking DBus server command.");
97 if (result_code) return LocalApiResult(true, path);
98 return LocalApiResult(false, "Error opening file");
99}
100
101LocalApiResult DbusLocalClient::GetRestEndpoint() {
102 auto proxy = GetProxy();
103 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
104 GError* error = 0;
105 GVariant* result = g_dbus_proxy_call_sync(
106 proxy, "GetRestEndpoint", 0 /*arguments */, G_DBUS_CALL_FLAGS_NONE,
107 -1 /* timeout msec */, 0 /* cancellable */, &error);
108 const std::string message(error ? error->message : "");
109 bool ok(error == 0 && g_variant_is_container(result) &&
110 g_variant_n_children(result) == 1);
111 std::string result_str;
112 if (ok) {
113 GVariant* result_value = g_variant_get_child_value(result, 0);
114 gsize length;
115 const gchar* s = g_variant_get_string(result_value, &length);
116 result_str = std::string(s, length);
117 g_variant_unref(result_value);
118 }
119 if (error) g_clear_error(&error);
120 g_variant_unref(result);
121 g_object_unref(proxy);
122 if (ok)
123 return LocalApiResult(true, result_str.c_str());
124 else
125 return LocalApiResult(false, "Error invoking DBus server command.");
126}
Dbus local API implementation (Linux only)