Relicense Collabora code under the MIT/X11 license proposed for dbus core, removing...
[dbus-python-phuang.git] / _dbus_bindings / bus.c
blob5d64a6ad2ad70331f75d9dfe8af3c2d75a75ba69
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"
29 PyObject *
30 DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
32 PyObject *first = NULL, *mainloop = NULL;
33 DBusConnection *conn;
34 DBusError error;
35 Connection *self;
36 dbus_bool_t ret;
37 long type;
38 static char *argnames[] = {"address_or_type", "mainloop", NULL};
40 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
41 &first, &mainloop)) {
42 return NULL;
45 dbus_error_init(&error);
47 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;
52 TRACE(self);
54 Py_BEGIN_ALLOW_THREADS
55 ret = dbus_bus_register(self->conn, &error);
56 Py_END_ALLOW_THREADS
57 if (!ret) {
58 DBusPyException_ConsumeError(&error);
59 Py_DECREF(self);
60 return NULL;
63 return (PyObject *)self;
66 /* If the first argument isn't a string, it must be an integer
67 representing one of the well-known bus types. */
69 if (first && !PyInt_Check(first)) {
70 PyErr_SetString(PyExc_TypeError, "A string address or an integer "
71 "bus type is required");
72 return NULL;
74 if (first) {
75 type = PyInt_AsLong(first);
77 else {
78 type = DBUS_BUS_SESSION;
81 if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
82 && type != DBUS_BUS_STARTER) {
83 PyErr_Format(PyExc_ValueError, "Unknown bus type %d", (int)type);
84 return NULL;
87 Py_BEGIN_ALLOW_THREADS
88 conn = dbus_bus_get_private(type, &error);
89 Py_END_ALLOW_THREADS
91 if (!conn) {
92 DBusPyException_ConsumeError(&error);
93 return NULL;
95 return DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop);
98 PyObject *
99 DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED)
101 const char *name;
103 TRACE(self);
104 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
105 Py_BEGIN_ALLOW_THREADS
106 name = dbus_bus_get_unique_name(self->conn);
107 Py_END_ALLOW_THREADS
108 if (!name) {
109 return DBusPyException_SetString("This connection has no unique name "
110 "yet");
112 return PyString_FromString(name);
115 PyObject *
116 DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
118 const char *old_name, *new_name;
120 if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
121 return NULL;
124 TRACE(self);
125 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
127 /* libdbus will assert if we try to set a unique name when there's
128 * already one, so we need to make sure that can't happen.
129 * (Thanks, libdbus.)
131 * The things that can set the unique name are:
132 * - this function - but we don't release the GIL, so only one instance of
133 * this function can run
134 * - dbus_bus_get - but this is only called in a __new__ or __new__-like
135 * function, so the new connection isn't available to other code yet
136 * and this function can't be called on it
137 * - dbus_bus_register - same as dbus_bus_get
139 * Code outside dbus-python shouldn't be setting the unique name, because
140 * we're using a private connection; we have to trust the authors
141 * of mainloop bindings not to do silly things like that.
143 old_name = dbus_bus_get_unique_name(self->conn);
144 if (old_name != NULL) {
145 PyErr_Format(PyExc_ValueError, "This connection already has a "
146 "unique name: '%s'", old_name);
147 return NULL;
149 dbus_bus_set_unique_name(self->conn, new_name);
151 Py_RETURN_NONE;
154 /* vim:set ft=c cino< sw=4 sts=4 et: */