Create doc directory before writing HTML into it
[dbus-python-phuang.git] / _dbus_bindings / bus.c
blob1e3b81f92ee6b285e4ad3040247b3b6df3414e9f
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 PyErr_SetString(DBusPyException, "This connection has no unique "
107 "name yet");
108 return NULL;
110 return PyString_FromString(name);
113 PyObject *
114 DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
116 const char *old_name, *new_name;
118 if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
119 return NULL;
122 TRACE(self);
123 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
125 /* libdbus will assert if we try to set a unique name when there's
126 * already one, so we need to make sure that can't happen.
127 * (Thanks, libdbus.)
129 * The things that can set the unique name are:
130 * - this function - but we don't release the GIL, so only one instance of
131 * this function can run
132 * - dbus_bus_get - but this is only called in a __new__ or __new__-like
133 * function, so the new connection isn't available to other code yet
134 * and this function can't be called on it
135 * - dbus_bus_register - same as dbus_bus_get
137 * Code outside dbus-python shouldn't be setting the unique name, because
138 * we're using a private connection; we have to trust the authors
139 * of mainloop bindings not to do silly things like that.
141 old_name = dbus_bus_get_unique_name(self->conn);
142 if (old_name != NULL) {
143 PyErr_Format(PyExc_ValueError, "This connection already has a "
144 "unique name: '%s'", old_name);
145 return NULL;
147 dbus_bus_set_unique_name(self->conn, new_name);
149 Py_RETURN_NONE;
152 /* vim:set ft=c cino< sw=4 sts=4 et: */