OpenCPN Partial API docs
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 
24 static 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(conn, G_DBUS_PROXY_FLAGS_NONE,
35  0 /* GDBusInterfaceInfo* */,
36  kDbusName, kDbusObject, kDbusInterface,
37  0 /* GCancellable */,
38  &error);
39  g_assert_no_error(error);
40  return p;
41 }
42 
43 LocalApiResult DbusLocalClient::SendRaise() {
44  auto proxy = GetProxy();
45  if (!proxy) return LocalApiResult(false, "Cannot create proxy");
46  GError* error = 0;
47  GVariant* result = g_dbus_proxy_call_sync (proxy,
48  "Raise",
49  0 /* parameters */,
50  G_DBUS_CALL_FLAGS_NONE,
51  -1 /* timeout msec */,
52  0 /* cancellable */,
53  &error);
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);
61 }
62 
63 LocalApiResult DbusLocalClient::SendQuit() {
64  auto proxy = GetProxy();
65  if (!proxy) return LocalApiResult(false, "Cannot create proxy");
66  GError* error = 0;
67  GVariant* result = g_dbus_proxy_call_sync (proxy,
68  "Quit",
69  0 /* parameters */,
70  G_DBUS_CALL_FLAGS_NONE,
71  -1 /* timeout msec */,
72  0 /* cancellable */,
73  &error);
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);
81 }
82 
83 LocalApiResult DbusLocalClient::SendOpen(const char* path) {
84  auto proxy = GetProxy();
85  if (!proxy) return LocalApiResult(false, "Cannot create proxy");
86  GError* error = 0;
87  GVariant* result = g_dbus_proxy_call_sync (proxy,
88  "Open",
89  g_variant_new("(s)", path),
90  G_DBUS_CALL_FLAGS_NONE,
91  -1 /* timeout msec */,
92  0 /* cancellable */,
93  &error);
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;
98  if (ok) {
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);
102  }
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");
109 }
110 
111 LocalApiResult DbusLocalClient::GetRestEndpoint() {
112  auto proxy = GetProxy();
113  if (!proxy) return LocalApiResult(false, "Cannot create proxy");
114  GError* error = 0;
115  GVariant* result = g_dbus_proxy_call_sync (proxy,
116  "GetRestEndpoint",
117  0 /*arguments */,
118  G_DBUS_CALL_FLAGS_NONE,
119  -1 /* timeout msec */,
120  0 /* cancellable */,
121  &error);
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;
126  if (ok) {
127  GVariant* result_value = g_variant_get_child_value(result, 0);
128  gsize length;
129  const gchar* s = g_variant_get_string(result_value, &length);
130  result_str = std::string(s, length);
131  g_variant_unref(result_value);
132  }
133  if (error) g_clear_error(&error);
134  g_variant_unref(result);
135  g_object_unref(proxy);
136  if (ok)
137  return LocalApiResult(true, result_str.c_str());
138  else
139  return LocalApiResult(false, "Error invoking DBus server command.");
140 }