1 /* Implementation of Bus, a subtype of Connection.
3 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 #include "dbus_bindings-internal.h"
27 #include "conn-internal.h"
30 DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
32 PyObject *first = NULL, *mainloop = NULL;
36 static char *argnames[] = {"address_or_type", "mainloop", NULL};
38 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
43 dbus_error_init(&error);
45 if (first && PyString_Check(first)) {
48 /* It's a custom address. First connect to it, then register. */
50 self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs);
51 if (!self) return NULL;
54 Py_BEGIN_ALLOW_THREADS
55 ret = dbus_bus_register(self->conn, &error);
58 DBusPyException_ConsumeError(&error);
63 return (PyObject *)self;
65 else if (!first || PyInt_Check(first)) {
67 PyObject *libdbusconn;
71 /* If the first argument isn't a string, it must be an integer
72 representing one of the well-known bus types. The default is
76 type = PyInt_AsLong(first);
78 if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
79 && type != DBUS_BUS_STARTER) {
80 PyErr_Format(PyExc_ValueError, "Unknown bus type %ld", type);
85 type = DBUS_BUS_SESSION;
88 Py_BEGIN_ALLOW_THREADS
89 conn = dbus_bus_get_private(type, &error);
93 DBusPyException_ConsumeError(&error);
97 libdbusconn = DBusPyLibDBusConnection_New (conn);
98 dbus_connection_unref (conn);
103 new_args = PyTuple_Pack(2, libdbusconn, mainloop ? mainloop : Py_None);
104 Py_DECREF(libdbusconn);
110 new_kwargs = PyDict_New();
117 self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, new_args,
120 Py_DECREF(new_kwargs);
122 return (PyObject *)self; /* whether NULL or not */
125 PyErr_SetString(PyExc_TypeError, "A string address or an integer "
126 "bus type is required");
132 DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED)
137 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
138 Py_BEGIN_ALLOW_THREADS
139 name = dbus_bus_get_unique_name(self->conn);
142 return DBusPyException_SetString("This connection has no unique name "
145 return PyString_FromString(name);
149 DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
151 const char *old_name, *new_name;
153 if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
158 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
160 /* libdbus will assert if we try to set a unique name when there's
161 * already one, so we need to make sure that can't happen.
164 * The things that can set the unique name are:
165 * - this function - but we don't release the GIL, so only one instance of
166 * this function can run
167 * - dbus_bus_get - but this is only called in a __new__ or __new__-like
168 * function, so the new connection isn't available to other code yet
169 * and this function can't be called on it
170 * - dbus_bus_register - same as dbus_bus_get
172 * Code outside dbus-python shouldn't be setting the unique name, because
173 * we're using a private connection; we have to trust the authors
174 * of mainloop bindings not to do silly things like that.
176 old_name = dbus_bus_get_unique_name(self->conn);
177 if (old_name != NULL) {
178 PyErr_Format(PyExc_ValueError, "This connection already has a "
179 "unique name: '%s'", old_name);
182 dbus_bus_set_unique_name(self->conn, new_name);
187 /* vim:set ft=c cino< sw=4 sts=4 et: */