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"
27 DBusPyConnection_NewForBus(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
29 PyObject
*first
= NULL
, *mainloop
= NULL
;
35 static char *argnames
[] = {"address_or_type", "mainloop", NULL
};
37 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OO", argnames
,
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
;
51 Py_BEGIN_ALLOW_THREADS
52 ret
= dbus_bus_register(self
->conn
, &error
);
55 DBusPyException_ConsumeError(&error
);
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");
72 type
= PyInt_AsLong(first
);
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
);
84 Py_BEGIN_ALLOW_THREADS
85 conn
= dbus_bus_get_private(type
, &error
);
89 DBusPyException_ConsumeError(&error
);
92 return DBusPyConnection_NewConsumingDBusConnection(cls
, conn
, mainloop
);
96 DBusPyConnection_GetUniqueName(Connection
*self
, PyObject
*args UNUSED
)
101 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self
->conn
);
102 Py_BEGIN_ALLOW_THREADS
103 name
= dbus_bus_get_unique_name(self
->conn
);
106 return DBusPyException_SetString("This connection has no unique name "
109 return PyString_FromString(name
);
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
)) {
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.
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
);
146 dbus_bus_set_unique_name(self
->conn
, new_name
);
151 /* vim:set ft=c cino< sw=4 sts=4 et: */