Update NEWS
[dbus-python-phuang.git] / _dbus_bindings / bus.c
blob5d671baf50566c0d6563e32352d1ea427e6c4134
1 /* Implementation of Bus, a subtype of Connection.
3 * Copyright (C) 2006 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus_bindings-internal.h"
24 #include "conn-internal.h"
26 PyObject *
27 DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
29 PyObject *first = NULL, *mainloop = NULL;
30 DBusConnection *conn;
31 DBusError error;
32 Connection *self;
33 dbus_bool_t ret;
34 long type;
35 static char *argnames[] = {"address_or_type", "mainloop", NULL};
37 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
38 &first, &mainloop)) {
39 return NULL;
42 dbus_error_init(&error);
44 if (first && PyString_Check(first)) {
45 /* It's a custom address. First connect to it, then register. */
47 self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs);
48 if (!self) return NULL;
49 TRACE(self);
51 Py_BEGIN_ALLOW_THREADS
52 ret = dbus_bus_register(self->conn, &error);
53 Py_END_ALLOW_THREADS
54 if (!ret) {
55 DBusPyException_ConsumeError(&error);
56 Py_DECREF(self);
57 return NULL;
60 return (PyObject *)self;
63 /* If the first argument isn't a string, it must be an integer
64 representing one of the well-known bus types. */
66 if (first && !PyInt_Check(first)) {
67 PyErr_SetString(PyExc_TypeError, "A string address or an integer "
68 "bus type is required");
69 return NULL;
71 if (first) {
72 type = PyInt_AsLong(first);
74 else {
75 type = DBUS_BUS_SESSION;
78 if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
79 && type != DBUS_BUS_STARTER) {
80 PyErr_Format(PyExc_ValueError, "Unknown bus type %d", (int)type);
81 return NULL;
84 Py_BEGIN_ALLOW_THREADS
85 conn = dbus_bus_get_private(type, &error);
86 Py_END_ALLOW_THREADS
88 if (!conn) {
89 DBusPyException_ConsumeError(&error);
90 return NULL;
92 return DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop);
95 PyObject *
96 DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED)
98 const char *name;
100 TRACE(self);
101 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
102 Py_BEGIN_ALLOW_THREADS
103 name = dbus_bus_get_unique_name(self->conn);
104 Py_END_ALLOW_THREADS
105 if (!name) {
106 return DBusPyException_SetString("This connection has no unique name "
107 "yet");
109 return PyString_FromString(name);
112 PyObject *
113 DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
115 const char *old_name, *new_name;
117 if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
118 return NULL;
121 TRACE(self);
122 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
124 /* libdbus will assert if we try to set a unique name when there's
125 * already one, so we need to make sure that can't happen.
126 * (Thanks, libdbus.)
128 * The things that can set the unique name are:
129 * - this function - but we don't release the GIL, so only one instance of
130 * this function can run
131 * - dbus_bus_get - but this is only called in a __new__ or __new__-like
132 * function, so the new connection isn't available to other code yet
133 * and this function can't be called on it
134 * - dbus_bus_register - same as dbus_bus_get
136 * Code outside dbus-python shouldn't be setting the unique name, because
137 * we're using a private connection; we have to trust the authors
138 * of mainloop bindings not to do silly things like that.
140 old_name = dbus_bus_get_unique_name(self->conn);
141 if (old_name != NULL) {
142 PyErr_Format(PyExc_ValueError, "This connection already has a "
143 "unique name: '%s'", old_name);
144 return NULL;
146 dbus_bus_set_unique_name(self->conn, new_name);
148 Py_RETURN_NONE;
151 /* vim:set ft=c cino< sw=4 sts=4 et: */