OpenCPN Partial API docs
Loading...
Searching...
No Matches
dbus_client.cpp
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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
20#include <gio/gio.h>
21
22#include "model/dbus_client.h"
23
24static GDBusProxy* GetProxy() {
25 // session_bus_up();
26 // 0std::this_thread::sleep_for(50ms);
27 GError* error = 0;
28 GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SESSION, 0, &error);
29 g_assert_no_error(error);
30 // /* we shouldn't have a name owner nor any cached properties //
31 // g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
32 // g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
33 error = 0;
34 GDBusProxy* p = g_dbus_proxy_new_sync(
35 conn, G_DBUS_PROXY_FLAGS_NONE, 0 /* GDBusInterfaceInfo* */, kDbusName,
36 kDbusObject, kDbusInterface, 0 /* GCancellable */, &error);
37 g_assert_no_error(error);
38 return p;
39}
40
41LocalApiResult DbusLocalClient::SendRaise() {
42 auto proxy = GetProxy();
43 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
44 GError* error = 0;
45 GVariant* result = g_dbus_proxy_call_sync(
46 proxy, "Raise", 0 /* parameters */, G_DBUS_CALL_FLAGS_NONE,
47 -1 /* timeout msec */, 0 /* cancellable */, &error);
48 const std::string message(error ? error->message : "");
49 bool ok(error == 0 && g_variant_is_container(result) &&
50 g_variant_n_children(result) == 0);
51 if (error) g_clear_error(&error);
52 g_variant_unref(result);
53 g_object_unref(proxy);
54 return LocalApiResult(ok, message);
55}
56
57LocalApiResult DbusLocalClient::SendQuit() {
58 auto proxy = GetProxy();
59 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
60 GError* error = 0;
61 GVariant* result = g_dbus_proxy_call_sync(
62 proxy, "Quit", 0 /* parameters */, G_DBUS_CALL_FLAGS_NONE,
63 -1 /* timeout msec */, 0 /* cancellable */, &error);
64 const std::string message(error ? error->message : "");
65 bool ok(error == 0 && g_variant_is_container(result) &&
66 g_variant_n_children(result) == 0);
67 if (error) g_clear_error(&error);
68 g_variant_unref(result);
69 g_object_unref(proxy);
70 return LocalApiResult(ok, message);
71}
72
73LocalApiResult DbusLocalClient::SendOpen(const char* path) {
74 auto proxy = GetProxy();
75 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
76 GError* error = 0;
77 GVariant* result = g_dbus_proxy_call_sync(
78 proxy, "Open", g_variant_new("(s)", path), G_DBUS_CALL_FLAGS_NONE,
79 -1 /* timeout msec */, 0 /* cancellable */, &error);
80 const std::string message(error ? error->message : "");
81 bool ok(error == 0 && g_variant_is_container(result) &&
82 g_variant_n_children(result) == 1);
83 gboolean result_code = false;
84 if (ok) {
85 GVariant* result_value = g_variant_get_child_value(result, 0);
86 result_code = g_variant_get_boolean(result_value);
87 g_variant_unref(result_value);
88 }
89 if (error) g_clear_error(&error);
90 g_variant_unref(result);
91 g_object_unref(proxy);
92 if (!ok) return LocalApiResult(false, "Error invoking DBus server command.");
93 if (result_code) return LocalApiResult(true, path);
94 return LocalApiResult(false, "Error opening file");
95}
96
97LocalApiResult DbusLocalClient::GetRestEndpoint() {
98 auto proxy = GetProxy();
99 if (!proxy) return LocalApiResult(false, "Cannot create proxy");
100 GError* error = 0;
101 GVariant* result = g_dbus_proxy_call_sync(
102 proxy, "GetRestEndpoint", 0 /*arguments */, G_DBUS_CALL_FLAGS_NONE,
103 -1 /* timeout msec */, 0 /* cancellable */, &error);
104 const std::string message(error ? error->message : "");
105 bool ok(error == 0 && g_variant_is_container(result) &&
106 g_variant_n_children(result) == 1);
107 std::string result_str;
108 if (ok) {
109 GVariant* result_value = g_variant_get_child_value(result, 0);
110 gsize length;
111 const gchar* s = g_variant_get_string(result_value, &length);
112 result_str = std::string(s, length);
113 g_variant_unref(result_value);
114 }
115 if (error) g_clear_error(&error);
116 g_variant_unref(result);
117 g_object_unref(proxy);
118 if (ok)
119 return LocalApiResult(true, result_str.c_str());
120 else
121 return LocalApiResult(false, "Error invoking DBus server command.");
122}