Debian Codegraph @

tests/libertine-service.h

raw symbols SCIP
1 /*
2 * Copyright © 2017 Canonical Ltd.
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; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ted Gould <[email protected]>
18 */
19
20 #include <gio/gio.h>
21
22 #include <libdbustest/dbus-test.h>
23
24 class LibertineService
25 {
26 private:
27 DbusTestProcess *process = nullptr;
28 std::string uniquename{};
29 std::once_flag uniqueflag;
30 guint namewatcher;
31
32 public:
33 LibertineService()
34 {
35 g_setenv("LIBERTINE_DEBUG", "2", TRUE);
36
37 process = dbus_test_process_new("/usr/bin/libertined");
38 dbus_test_process_append_param(process, "--verbosity");
39
40 dbus_test_task_set_bus(DBUS_TEST_TASK(process), DBUS_TEST_SERVICE_BUS_SESSION);
41 dbus_test_task_set_name(DBUS_TEST_TASK(process), "libertine");
42 dbus_test_task_set_return(DBUS_TEST_TASK(process), DBUS_TEST_TASK_RETURN_IGNORE);
43 dbus_test_task_set_wait_finished(DBUS_TEST_TASK(process), FALSE);
44 }
45
46 ~LibertineService()
47 {
48 g_debug("Destroying the Libertined Task");
49 g_clear_object(&process);
50
51 if (namewatcher)
52 {
53 g_bus_unwatch_name(namewatcher);
54 }
55 }
56
57 operator std::shared_ptr<DbusTestTask>()
58 {
59 std::shared_ptr<DbusTestTask> retval(DBUS_TEST_TASK(g_object_ref(process)),
60 [](DbusTestTask *process) { g_clear_object(&process); });
61 return retval;
62 }
63
64 operator DbusTestTask *()
65 {
66 return DBUS_TEST_TASK(process);
67 }
68
69 const std::string &getUniqueName()
70 {
71 std::call_once(uniqueflag, [this]() {
72 namewatcher = g_bus_watch_name(
73 G_BUS_TYPE_SESSION, "com.lomiri.libertine.Service", G_BUS_NAME_WATCHER_FLAGS_NONE,
74 [](GDBusConnection *con, const gchar *name, const gchar *name_owner, gpointer user_data) {
75 auto pthis = static_cast<LibertineService *>(user_data);
76 pthis->uniquename = name_owner;
77 },
78 [](GDBusConnection *con, const gchar *name, gpointer user_data) {
79 auto pthis = static_cast<LibertineService *>(user_data);
80 pthis->uniquename.clear();
81 },
82 this, nullptr);
83 });
84 return uniquename;
85 }
86 };