Quit when requested from the menu
[ladish.git] / dbus / service.c
blob72f5d86f5d32753e35c55cd59baefb5196c870de
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains D-Bus service helpers
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h> /* strerror() */
32 #include "../common/safety.h"
33 #include "../common/debug.h"
34 #include "service.h"
36 service_t *
37 service_new(const char *service_name,
38 bool *quit,
39 int num_paths,
40 ...)
42 if (!quit || num_paths < 1) {
43 lash_debug("Invalid arguments");
44 return NULL;
47 lash_debug("Creating D-Bus service");
49 service_t *service;
50 DBusError err;
51 int r;
52 va_list argp;
53 object_path_t **path_pptr, *path_ptr;
55 service = lash_calloc(1, sizeof(service_t));
56 service->object_paths = lash_calloc(num_paths + 1,
57 sizeof(object_path_t *));
59 dbus_error_init(&err);
61 service->connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
62 if (dbus_error_is_set(&err)) {
63 lash_error("Failed to get bus: %s", err.message);
64 goto fail_free_err;
67 if (!(service->unique_name = dbus_bus_get_unique_name(service->connection))) {
68 lash_error("Failed to read unique bus name");
69 goto fail_free_err;
72 if (service_name) {
73 r = dbus_bus_request_name(service->connection, service_name,
74 DBUS_NAME_FLAG_DO_NOT_QUEUE,
75 &err);
76 if (r == -1) {
77 lash_error("Failed to acquire bus name: %s", err.message);
78 goto fail_free_err;
79 } else if (r == DBUS_REQUEST_NAME_REPLY_EXISTS) {
80 lash_error("Requested bus name already exists");
81 goto fail_free_err;
84 service->name = lash_strdup(service_name);
85 } else {
86 service->name = lash_strdup("");
89 /* Populate the array and register object paths */
90 va_start(argp, num_paths);
91 for (path_pptr = service->object_paths;
92 (*path_pptr = va_arg(argp, object_path_t *));
93 ++path_pptr) {
94 if (!object_path_register(service->connection,
95 *path_pptr)) {
96 lash_error("Failed to register object path");
97 va_end(argp);
98 goto fail;
101 va_end(argp);
103 /* Set the keepalive pointer */
104 service->quit = quit;
106 return service;
108 fail_free_err:
109 dbus_error_free(&err);
111 fail:
112 lash_free(&service->object_paths);
113 service_destroy(service);
115 /* Always free the object paths which were passed to us so that
116 way we can guarantee that the caller need not worry. */
117 va_start(argp, num_paths);
118 while ((path_ptr = va_arg(argp, object_path_t *)))
119 free(path_ptr);
120 va_end(argp);
122 return NULL;
125 void
126 service_destroy(service_t *service)
128 lash_debug("Destroying D-Bus service");
130 if (service) {
131 /* cut the bus connection */
132 if (service->connection)
134 /* reap the object path(s) */
135 if (service->object_paths)
137 object_path_t **path_pptr;
139 for (path_pptr = service->object_paths; *path_pptr != NULL; ++path_pptr)
141 object_path_destroy(service->connection, *path_pptr);
142 *path_pptr = NULL;
145 free(service->object_paths);
146 service->object_paths = NULL;
149 dbus_connection_unref(service->connection);
150 service->connection = NULL;
153 /* other stuff */
154 if (service->name) {
155 free(service->name);
156 service->name = NULL;
158 service->unique_name = NULL;
159 service->quit = NULL;
161 /* finalize */
162 free(service);
163 service = NULL;
165 #ifdef LASH_DEBUG
166 else
167 lash_debug("Nothing to destroy");
168 #endif
171 /* EOF */